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 1eyHfO-0006yk-T7 for pgsql-general@arkaria.postgresql.org; Tue, 20 Mar 2018 13:51:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eyHfN-0000gc-TC for pgsql-general@arkaria.postgresql.org; Tue, 20 Mar 2018 13:51:05 +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 1eyHfN-0000fR-BQ for pgsql-general@lists.postgresql.org; Tue, 20 Mar 2018 13:51:05 +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 1eyHfK-0007KS-MZ for pgsql-general@lists.postgresql.org; Tue, 20 Mar 2018 13:51:04 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id E8EBE20D6C; Tue, 20 Mar 2018 09:51:01 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Tue, 20 Mar 2018 09:51:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= 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=htwztbJ+sdvlsizsRVTCRyRlmuu8v sJZanXVa4sF5ZM=; b=Endi86c76kzA56HZWKPGCA94EE5NpSWeWqAriEX/N1+yk f9/2NH/CTY0znntMDy6IM6Buzj6kYAg3hij3rVNqKdB6jihjpa7GFD5VSliRerOr FQehViq3kam5eSUm04Zc+ng3Or+5dYyXZhs1I2lKRD/RJyqDnGkKB1nJTHZH9cg3 alR7/i8L09xpG8KJskS4L1wm83W2SmwphQvVdNyaWkbemKGWi4SgTbXR9g294y9s fZanRa0AUiZNSbM+UTTOQt+CsUzA9mJLGBZTGQK1ia8ni/tuslUhCZJcLqMpI58M qoi42pYMdcXw+bUIeq88/kcT1Itc7UqqjT5v6KMKw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=htwztb J+sdvlsizsRVTCRyRlmuu8vsJZanXVa4sF5ZM=; b=cvLG0cdcnATixcg7Rd9Hgn V86s00q+livZfucXibuk94LSq3nSAj1pYg0c5FVoqd/zFGs5HlLiaPe5Tu05GeBv 3g3QnJr0Xy/lQMYXEp+iWJ6JqqeJH21ME7OILpzMYEM+smg2Iw47BADmdkigvKjW p5faEh0GuWq+CuLHuPfHtaKk1MPpVx0b1yZdkhA0K9VrtSjxfYgtVO2sc8NCkh/2 r1wh3sML2NnXJcwkYYKz3/5eHU1ouNsExAyOzqXXSA6+uC+aeqZ1bqmlFxHXPdnA i36FC1oRoPf/fusCaxtOeKma9fJGBsCvb0bvDLPIKZzSKirsajX8nxfTPUIFyq8w == 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 6ECDB24311; Tue, 20 Mar 2018 09:51:01 -0400 (EDT) Subject: Re: Foreign Key locking / deadlock issue. To: HORDER Phil , "pgsql-general@lists.postgresql.org" References: <9a2d4dc19e66413e9373652d2b618f81@XEXWIN0001.one-02-priv.grp> From: Adrian Klaver Message-ID: <4faf162b-5b3f-9c42-03bf-e8b61da9baef@aklaver.com> Date: Tue, 20 Mar 2018 06:51:00 -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: <9a2d4dc19e66413e9373652d2b618f81@XEXWIN0001.one-02-priv.grp> Content-Type: text/plain; charset=windows-1252; 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 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