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 1eyHsE-0007ii-C3 for pgsql-general@arkaria.postgresql.org; Tue, 20 Mar 2018 14:04:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eyHsD-0000Tj-AU for pgsql-general@arkaria.postgresql.org; Tue, 20 Mar 2018 14:04:21 +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 1eyHsC-0000TT-VE for pgsql-general@lists.postgresql.org; Tue, 20 Mar 2018 14:04:21 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyHs9-0007aw-Vb for pgsql-general@postgresql.org; Tue, 20 Mar 2018 14:04:19 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 314E520A64; Tue, 20 Mar 2018 10:04:13 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Tue, 20 Mar 2018 10:04:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=GZ4c6MQQLH0E7SQFvxyZvOxxAyzUi 5sbbbMszu/DCIY=; b=douNsIF1RffuvFuLVxK2OEHtFKaIWHG9jWaFkygYhQzA/ 8N26NFpl/YYwFRlsWrsceAaBCUCf9jwesJtsGjT4xskucu3XmBlnkZHu/H7zq1Kf spIwJfoa604Wa9Ypc+lZTc28mivPqra8vtyLVGzYxKX0GfZB2fvnz604Cc/lf3O8 bBnzUgvU10hmlfi1Axyi4lhJgBDrOCIJb57xn5+CFxTZltXGHmRMPNrQie8o9Jhr 39zgubNHyUnJRXPK6C+hKr6Ysh+xWx6fnd8WSXKmrK4GbQ/A9MIS8qg5A6vkP0jM vWF1B1p4+kX/Ek3zE9+Jur/F/FRw85FiS7SXXrn3A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=GZ4c6M QQLH0E7SQFvxyZvOxxAyzUi5sbbbMszu/DCIY=; b=HqI2OZX1UPBtT4Rf3ABXVk YvnYciVA8q1Q7VTkm43VSJsD/CxTKDe/MVNWNYmTYpd8QEvaZ69S791lT1022cGZ XqDVc9g+jSBhTT+qGPy8JtgnIxcl8BWPrec2PbV2BlEPbGQYjT8DZRtlXFjyweR2 LAYE1kBmWBF+juO87nBCQ/AxYeTyYp13GTjxwKO7cWehDeeBcqecgwqDQldFukn8 NM2Ex07BdZzVG/sXkZV/OZe2k/g4ZPv3E5ZZ1+JhZhkp8dXIjxt65oMR2WI0PWsy K+P4wib1x0CkQxcXNjFAvkxe2NYph+QyMQpKifK30glGtzghPrmhXfqZ3OzfZLhg == X-ME-Sender: Received: from [192.168.1.2] (71-35-123-124.tukw.qwest.net [71.35.123.124]) by mail.messagingengine.com (Postfix) with ESMTPA id 94BE3242CE; Tue, 20 Mar 2018 10:04:12 -0400 (EDT) Subject: Re: Foreign Key locking / deadlock issue. To: HORDER Phil References: <9a2d4dc19e66413e9373652d2b618f81@XEXWIN0001.one-02-priv.grp> <4faf162b-5b3f-9c42-03bf-e8b61da9baef@aklaver.com> Cc: pgsql-general From: Adrian Klaver Message-ID: <8069590f-ca76-e5f8-fd28-2ba381c9fc82@aklaver.com> Date: Tue, 20 Mar 2018 07:04:11 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 03/20/2018 06:55 AM, HORDER Phil wrote: Please reply to list also. Ccing list. Still not certain what the PK is or what key value refers to? > Well this is just sample SQL to demonstrate the problem. > If each process executes lines of code alternately, a deadlock occurs. > > The commit would obviously be required by an application, but one transaction would be cancelled by the deadlock exception, and the demonstrator would rollback the other. > > Phil Horder > Database Mechanic > -----Original Message----- > From: Adrian Klaver [mailto:adrian.klaver@aklaver.com] > Sent: 20 March 2018 13:51 > To: HORDER Phil; pgsql-general@lists.postgresql.org > Subject: Re: Foreign Key locking / deadlock issue. > > On 03/20/2018 04:46 AM, HORDER Phil wrote: >> Hi, >> >> I'm trying to understand why I'm getting a deadlock issue, and how to >> work around it. >> >> At base, I think the problem is: >> >> 1.Updates to a parent table are creating row level write locks, >> >> 2.updates to a child table set the foreign key value to the parent >> table, which are then blocked. >> >> While investigating, I found the row locking documentation, which says >> that I can request read locks that don't block. >> >> But my sample code still gets blocked. >> >> https://www.postgresql.org/docs/9.6/static/explicit-locking.html >> >> Can someone explain what I'm missing please? >> >> parent process >> >> ------------------- >> >> start transaction; >> >> select * from pl where pl_id in (2,3) for no key update of pl;  (1) >> >> update pl set m_number = '234' where pl_id = 2;                 (2) >> >> update pl set m_number = '345' where pl_id = 3;                 (3) >> >> child process >> >> ------------------- >> >> start transaction; >> >> select pl_id from pl where pl_id in (2,3) for key share of pl;  (4) >> >> update eln set pl_id = 3 where event_id = 303;                  (5) >> >> update eln set pl_id = 2 where event_id = 302;                  (6) > > > I do not see a commit for either transaction. > >> >> My Parent process inserts and updates on the PL table, but never >> changes the key value. > > I am assuming when you say key value you are referring to PRIMARY KEY? > > What is the key(PK) column? > >> >> My Child process inserts and updates on the ELN table, and can set the >> FK reference value to the PL table. >> >> I can understand that the default lock on the PL update will block the >> foreign key check from the ELN table. >> >> Why does this example still get blocked? >> >> Is there a way around this without dropping the foreign key? >> >> (And if so.. How can I get this to work in Spring Data / JPA?) >> >> Phil Horder >> >> Database Mechanic >> >> Thales >> >> Land and Air Systems >> >> Horizon House, Throop Road, Templecombe, Somerset, BA8 0DH, UK >> > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com