Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdZFn-0007P0-HT for pgsql-performance@arkaria.postgresql.org; Thu, 12 Jul 2018 10:55:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fdZFk-000077-Tn for pgsql-performance@arkaria.postgresql.org; Thu, 12 Jul 2018 10:55:16 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdZFk-000068-Go for pgsql-performance@lists.postgresql.org; Thu, 12 Jul 2018 10:55:16 +0000 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fdZFh-0006YU-Gm for pgsql-performance@postgresql.org; Thu, 12 Jul 2018 10:55:15 +0000 Received: by mail-ed1-x541.google.com with SMTP id w14-v6so21545554eds.6 for ; Thu, 12 Jul 2018 03:55:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:mime-version:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=L7oGmkcnUlQDzPDgVcsSy6ogbgapOcig1YKF/6xpeLI=; b=nMVuEQjbzzJkLgGYyYUzIyQqh5YZy+BWn1xAmg8Djw9W024yL1x6KOX8YzvievlRFH EX7ysIvINn10GxMZeIwXQVscYxuZdOpuFcz1+jSh38Zdpo6x7UAN5ctb+w3ywzbA7LKK 1oiggTJY6uO5CDSs5y7qkmaiNj7Q3nz5WcLdKSinmbIyZccAL4G53mHRyFK5Pw8PnoTi j/2wP2ItUqwZCeX/qZv7liTSRo9I2yht+kGmNu+CT01V/g5vlwFgtYDAMpfjVugOumoO h+LU+j8g8vhTww6iwnzuj5EMeI7sw9F0ddygqNHd905BQO+bSWfaK9OzUUhXfPOIBWgC cZJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:mime-version:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=L7oGmkcnUlQDzPDgVcsSy6ogbgapOcig1YKF/6xpeLI=; b=kRShjBdsF/B2tk/dhXcDqrumhQJSXgqTerFUzkP0KzHvywvmqIvCAyVy9/cYej2cVl jRGnA/Rq7v4Hm2yXPHxRixJDhXqwQZxvyJl15h+sJ+sqGJy1LCVM4fg/EjQ2SmhxjyZU 9YzPqHC35R7uZcLGUNkxL8DAG07X9XvU8M7j4tbRYPTfCz7YZ/XqMH8Ev//siFBRjNjm NejrsyX84tvRYCV3+F6bn9IFXAcjawTUjaVgIhdYYCtqbUJ66/QTTzziMUPMp1s/kf7U 1cXxhMP7EbkTnxCk9RciC4F0JmfPlx18kgtH3OjrI650aH0O69PQH/LpVGiULutbyydq DP6A== X-Gm-Message-State: AOUpUlFeqYBQsWBfxKC+prl07aAZ4RMeEWAVxr1Car0ltiDiFDSm8xAv x5U2NEYXnuWmhym/xKpJSEToSzee X-Google-Smtp-Source: AAOMgpcJdLI3tEbSh0P9mvUl17QkcxVy5ZvEpwkXBO+yCqOQr8uXwV2B+ZAARTHT7my8Q1weNy21/w== X-Received: by 2002:a50:be05:: with SMTP id a5-v6mr2095697edi.258.1531392912054; Thu, 12 Jul 2018 03:55:12 -0700 (PDT) Received: from ip-172-20-1-26.eu-west-1.compute.internal (ec2-52-209-59-238.eu-west-1.compute.amazonaws.com. [52.209.59.238]) by smtp.gmail.com with ESMTPSA id d89-v6sm2273180edc.93.2018.07.12.03.55.10 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 12 Jul 2018 03:55:11 -0700 (PDT) Subject: Re: High concurrency but simple updating causes deadlock Mime-Version: 1.0 (Mac OS X Mail 9.3 \(3124\)) Content-Type: text/plain; charset=utf-8 From: Roman Konoval X-Priority: 3 In-Reply-To: Date: Thu, 12 Jul 2018 12:55:09 +0200 Cc: pgsql-performance Content-Transfer-Encoding: quoted-printable Message-Id: <2828B266-ED39-4D1F-A1F2-3F28B4125EAD@gmail.com> References: To: =?utf-8?B?5p6r?= X-Mailer: Apple Mail (2.3124) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk In this case this happens because the update modifies several rows and = different transactions may try to modify those rows (and obtain locks = for them) in different order. E.g. one transaction first gets row 1 and then row 2, and the second = transaction first updates row 2 and then row 1. The only way to overcome this that I know is to first to select for = update with order by clause so that all transactions lock rows in the = same order and do not cause deadlock conflicts. Regards, Roman Konoval rkonoval@gmail.com > On Jul 11, 2018, at 16:16, =E6=9E=AB wrote: >=20 > Hi, >=20 > All. > Can anyone give me a hand? >=20 > I meet a problem:High concurrency but simple updating causes deadlock >=20 > 1.System info >=20 > Linux version 4.8.0 >=20 > Ubuntu 5.4.0-6ubuntu1~16.04.4 >=20 > 2.Pg info >=20 > PostgreSQL 9.5.12 on i686-pc-linux-gnu, compiled by gcc (Ubuntu = 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609, 32-bit >=20 > Changes inpostgresql.conf: > max_connections =3D 1000 //100 to 1000 >=20 > 3.Database for test=E2=80=94=E2=80=942000 row same data=EF=BC=8C >=20 > ipcid | surdevip | surdevport | devfactory | surchanmode | = surchannum | username | password | transprotocol | mediastreamtype | = streamid | bsmvalid | smdevip | smdevport | smtransprotocol >=20 > = ------------+----------+------------+------------+-------------+----------= --+----------+----------+---------------+-----------------+----------+----= ------+---------+-----------+----------------- >=20 > 320460291 | Name | 8000 | 100 | 100 | = 100 | admin | 666666 | 100 | 100 | hello = | 1 | smpIp | 666 | 17 >=20 > 168201188 | Name | 8000 | 100 | 100 | = 100 | admin | 666666 | 100 | 100 | hello = | 1 | smpIp | 666 | 27 >=20 > 1360154585 | Name | 8000 | 100 | 100 | = 100 | admin | 666666 | 100 | 100 | hello | = 1 | smpIp | 666 | 70 >=20 > 820068220 | Name | 8000 | 100 | 100 | = 100 | admin | 666666 | 100 | 100 | hello = | 1 | smpIp | 666 | 49 >=20 > =E3=80=82=E3=80=82=E3=80=82=E3=80=82=E3=80=82=E3=80=822k row totally >=20 > 4.Operation=EF=BC=9AMulti-user thread update > Each thread do the same cmd : Pgexc(=E2=80=9CUPDATE = TEST6_CHAN_LIST_INFO SET streamId=3D 'hello', smDevPort=3D '666' = WHERE transProtocol=3D100=E2=80=9D) >=20 > =20 >=20 > 5.Error info >=20 > Error info in my code >=20 > ERROR: [func:insetDB line:1284]DB_Table_Update >=20 > ERROR: [func:DB_Table_Update line:705]PQexec(UPDATE = TEST6_CHAN_LIST_INFO SET streamId=3D 'hello', smDevPort=3D '666' = WHERE transProtocol=3D100) : ERROR: deadlock detected >=20 > DETAIL: Process 2366 waits for ShareLock on transaction 12316; = blocked by process 2368. >=20 > Process 2368 waits for ShareLock on transaction 12289; blocked by = process 2342. >=20 > Process 2342 waits for ExclusiveLock on tuple (9,1) of relation 18639 = of database 18638; blocked by process 2366. >=20 > HINT: See server log for query details. >=20 > CONTEXT: while locking tuple (9,1) in relation "test6_chan_list_info" >=20 > Error info in pg log >=20 > ERROR: deadlock detected >=20 > DETAIL: Process 10938 waits for ExclusiveLock on tuple (1078,61) of = relation 18639 of database 18638; blocked by process 10911. >=20 > Process 10911 waits for ShareLock on transaction 19494; = blocked by process 10807. >=20 > Process 10807 waits for ShareLock on transaction 19560; = blocked by process 10938. >=20 > Process 10938: UPDATE TEST6_CHAN_LIST_INFO SET streamId=3D = 'hello', smDevPort=3D '666' WHERE transProtocol=3D100 >=20 > Process 10911: UPDATE TEST6_CHAN_LIST_INFO SET streamId=3D = 'hello', smDevPort=3D '666' WHERE transProtocol=3D100 >=20 > Process 10807: UPDATE TEST6_CHAN_LIST_INFO SET streamId=3D = 'hello', smDevPort=3D '666' WHERE transProtocol=3D100 >=20 > HINT: See server log for query details. >=20 > STATEMENT: UPDATE TEST6_CHAN_LIST_INFO SET streamId=3D 'hello', = smDevPort=3D '666' WHERE transProtocol=3D100 >=20 > ERROR: deadlock detected >=20 > DETAIL: Process 10939 waits for ShareLock on transaction 19567; = blocked by process 10945. >=20 > Process 10945 waits for ShareLock on transaction 19494; = blocked by process 10807. >=20 > Process 10807 waits for ExclusiveLock on tuple (279,1) of = relation 18639 of database 18638; blocked by process 10939. >=20 > Process 10939: UPDATE TEST6_CHAN_LIST_INFO SET streamId=3D = 'hello', smDevPort=3D '666' WHERE transProtocol=3D100 >=20 > Process 10945: UPDATE TEST6_CHAN_LIST_INFO SET streamId=3D = 'hello', smDevPort=3D '666' WHERE transProtocol=3D100 >=20 > Process 10807: UPDATE TEST6_CHAN_LIST_INFO SET streamId=3D = 'hello', smDevPort=3D '666' WHERE transProtocol=3D100 >=20 > HINT: See server log for query details. >=20 > CONTEXT: while locking tuple (279,1) in relation = "test6_chan_list_info" >=20 > STATEMENT: UPDATE TEST6_CHAN_LIST_INFO SET streamId=3D 'hello', = smDevPort=3D '666' WHERE transProtocol=3D100 > =20 > 6.my quetion > 6.1.is it possible meet dead lock with high conurrency simple update? > 6.2.if yes, why,and how to avoid? > =20 > thanks very much!!! > =20 > Yours, > =20 > Leo from China