Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5YL4-0003yV-VR for pgsql-general@arkaria.postgresql.org; Mon, 09 Apr 2018 15:04:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f5YL3-0007WD-VR for pgsql-general@arkaria.postgresql.org; Mon, 09 Apr 2018 15:04:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5YL3-0007W4-No for pgsql-general@lists.postgresql.org; Mon, 09 Apr 2018 15:04:09 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5YL1-0006il-5i for pgsql-general@lists.postgresql.org; Mon, 09 Apr 2018 15:04:09 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w39F44BY014200; Mon, 9 Apr 2018 11:04:04 -0400 From: Tom Lane To: Christophe Pettus cc: Thomas Poty , Stephen Frost , pgsql-general@lists.postgresql.org Subject: Re: algo for canceling a deadlocked transaction In-reply-to: <316F0FB1-4B35-4E2C-B9A9-4DCF281AC8E5@thebuild.com> References: <20180409135121.GJ27724@tamriel.snowman.net> <316F0FB1-4B35-4E2C-B9A9-4DCF281AC8E5@thebuild.com> Comments: In-reply-to Christophe Pettus message dated "Mon, 09 Apr 2018 07:42:24 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14198.1523286244.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 09 Apr 2018 11:04:04 -0400 Message-ID: <14199.1523286244@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Christophe Pettus writes: >> On Apr 9, 2018, at 07:33, Thomas Poty wrote: >> ok, and long answer ? is it random? > It's not literally random, but from the application point of view, it's = not predictable. For example, it's not always the one that opened first, = or any other consistent measure. It's whichever one runs the deadlock detector first after the circular wait becomes established. For instance: * Process A takes lock L1 * Process B takes lock L2 * Process A tries to take lock L2, blocks * Process B tries to take lock L1, blocks (now a deadlock exists) Process A will run the deadlock detector one deadlock_timeout after blocking. If that happens before B has blocked, then A will see no deadlock and will go back to waiting. In that case, when B's own deadlock_timeout expires and it runs the deadlock detector, it will see the deadlock and fix it by canceling its own wait. On the other hand, if B started to wait less than one deadlock_timeout after A did, then A will be first to observe the deadlock and it will cancel itself, not B. So you can't predict it unless you have a lot of knowledge about the timing of events. You could probably make it more predictable by making deadlock_timeout either very short or very long, but neither of those are desirable things to do. regards, tom lane