pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: HORDER Phil <Phil.Horder@uk.thalesgroup.com>
To: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: RE: Foreign Key locking / deadlock issue.... v2
Date: Wed, 21 Mar 2018 15:26:17 +0000
Message-ID: <2d10a11d4b624988a21133d218a95c19@XEXWIN0001.one-02-priv.grp> (raw)

OK,

Let's try again, with a full script, and including the bit that makes the difference…



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 – because there is Row Level Security on the parent table.







-- The setup

------------



drop table if exists ELN;

drop table if exists PL;



Create table PL

(pl_id integer,

m_number text

);





alter table PL ENABLE row level security;

alter table PL FORCE row level security;



-- A dummy security policy, that allows everybody access to everything

drop policy if exists security_policy on PL ;

create policy security_policy on PL TO public using (true);   -- (1)



Alter table PL add constraint PL_PK primary key (pl_id);

Insert into PL values (1, null);

Insert into PL values (2, null);

Insert into PL values (3, null);



Create table ELN

(event_id integer,

pl_id integer

);





Alter table ELN add constraint ELN_PK primary key (event_id);

Alter table ELN add constraint ELN_PL_FK foreign key (pl_id) references PL (pl_id);



Insert into ELN values (301, null);

Insert into ELN values (302, null);

Insert into ELN values (303, null);



-- Parent table processing – long running process

------------------------------

start transaction;

update pl set m_number = '234' where pl_id = 2;       -- (2)

update pl set m_number = '345' where pl_id = 3;       -- (3)



-- …. Pause while other processing happens …..

(commit;)





-- Child table processing – occurs often & quickly. Starts after parent update.

----------------------------

start transaction;

update eln set pl_id = 3 where event_id = 303;                 -- (4)

update eln set pl_id = 2 where event_id = 302;                 -- (5)



-- blocked by parent Update.

(commit;)





I think that the RLS on the parent table is upgrading the row lock, even if the RLS policy doesn’t do anything.

I can understand that the RLS will normally want access to fields in the row to validate the user request – but it has access to those anyway, for the update.



The RLS documentation says that Foreign Keys bypass RLS, so RLS on the parent shouldn’t make any difference to it.



If the RLS policy at (1) is dropped, or RLS disabled on table PL, the problem goes away.

With RLS, statement (4) is blocked by statement (3).

If the parent updates interleave with child updates, deadlock occurs, as statement (2) is also blocked by (4).



Is the RLS responsible for a lock upgrade?

Can I get around it, without disabling RLS for the update process on PL?



Thanks.



Phil Horder

Database Mechanic



Thales

Land and Air Systems

Horizon House, Throop Road, Templecombe, Somerset, BA8 0DH, UK


view thread (9+ messages)  latest in thread

Message-ID: <2d10a11d4b624988a21133d218a95c19@XEXWIN0001.one-02-priv.grp>
Permalink:  ../2d10a11d4b624988a21133d218a95c19@XEXWIN0001.one-02-priv.grp/
Also on:    postgresql.org/message-id/2d10a11d4b624988a21133d218a95c19@XEXWIN0001.one-02-priv.grp

 · 

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: Phil.Horder@uk.thalesgroup.com, adrian.klaver@aklaver.com
  Subject: Re: Foreign Key locking / deadlock issue.... v2
  In-Reply-To: <2d10a11d4b624988a21133d218a95c19@XEXWIN0001.one-02-priv.grp>
  References: <2d10a11d4b624988a21133d218a95c19@XEXWIN0001.one-02-priv.grp>

* 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