pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: HORDER Phil <Phil.Horder@uk.thalesgroup.com>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Foreign Key locking / deadlock issue.
Date: Tue, 20 Mar 2018 06:51:00 -0700
Message-ID: <4faf162b-5b3f-9c42-03bf-e8b61da9baef@aklaver.com> (raw)
In-Reply-To: <9a2d4dc19e66413e9373652d2b618f81@XEXWIN0001.one-02-priv.grp>
References: <9a2d4dc19e66413e9373652d2b618f81@XEXWIN0001.one-02-priv.grp>

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




view thread (5+ messages)  latest in thread

Message-ID: <4faf162b-5b3f-9c42-03bf-e8b61da9baef@aklaver.com>
Permalink:  ../4faf162b-5b3f-9c42-03bf-e8b61da9baef@aklaver.com/
Also on:    postgresql.org/message-id/4faf162b-5b3f-9c42-03bf-e8b61da9baef@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, Phil.Horder@uk.thalesgroup.com
  Subject: Re: Foreign Key locking / deadlock issue.
  In-Reply-To: <4faf162b-5b3f-9c42-03bf-e8b61da9baef@aklaver.com>
  References: <9a2d4dc19e66413e9373652d2b618f81@XEXWIN0001.one-02-priv.grp> <4faf162b-5b3f-9c42-03bf-e8b61da9baef@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox