pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Different Lock Behavior With Create and Drop Foreign Key
3+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2020-04-10 f333d35428 a88e5886c5 aa4416dcd9 1d676ffd41 2cab4ad81b 61a9b814c5  Doc: clarify locking requirements for ALTER TABLE ADD FOREIGN KEY.
* Different Lock Behavior With Create and Drop Foreign Key
@ 2020-04-10 01:40  Virendra Kumar <viru_7683@yahoo.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Virendra Kumar @ 2020-04-10 01:40 UTC (permalink / raw)
  To: pgsql-general

Hi Team,
Here is test case.----create table testx
(
    v_code character varying(32),
    client_id bigint
);
alter table testx add constraint testx_pkey primary key (v_code);

create table testy
(
    dxid bigint,
    v_code character varying(32)
);
alter table testy add constraint testy_pkey primary key (dxid);create index on testy (v_code);

Let's begin two session, in session1 I am going begin a transaction and run select on parent table:
Session1----begin;select * from testx;

On another session let's call session2, I am running create FK on second table
Session2--- alter table testy add constraint testy_fkey foreign key (v_code) references testx(v_code); <--This works.alter table testy drop constraint testy_fkey; <--Hangs


Regards,Virendra Kumar

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Different Lock Behavior With Create and Drop Foreign Key
@ 2020-04-10 05:27  Laurenz Albe <laurenz.albe@cybertec.at>
  parent: Virendra Kumar <viru_7683@yahoo.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Laurenz Albe @ 2020-04-10 05:27 UTC (permalink / raw)
  To: Virendra Kumar <viru_7683@yahoo.com>; pgsql-general

On Fri, 2020-04-10 at 01:40 +0000, Virendra Kumar wrote:
> Here is test case.
> ----
> create table testx
> (
>     v_code character varying(32),
>     client_id bigint
> );
> alter table testx add constraint testx_pkey primary key (v_code);
> 
> create table testy
> (
>     dxid bigint,
>     v_code character varying(32)
> );
> alter table testy add constraint testy_pkey primary key (dxid);
> create index on testy (v_code);
> 
> Let's begin two session, in session1 I am going begin a transaction and run select on parent table:
> 
> Session1
> ----
> begin;
> select * from testx;
> 
> 
> On another session let's call session2, I am running create FK on second table
> 
> Session2
> ---
> alter table testy add constraint testy_fkey foreign key (v_code) references testx(v_code); <--This works.
> alter table testy drop constraint testy_fkey; <--Hangs

That is because foreign keys are implemented with system triggers, some of which
are defined on the target table.

Now CREATE TRIGGER does not require an ACCESS EXCLUSIVE lock, but DROP TRIGGER does.

Session 1 holds an ACCESS SHARE lock on the table, which conflicts only with ACCESS EXCLUSIVE.

So creating the foreign key works, but dropping it hangs when the triggers are dropped.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Different Lock Behavior With Create and Drop Foreign Key
@ 2020-04-10 16:13  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2020-04-10 16:13 UTC (permalink / raw)
  To: Laurenz Albe <laurenz.albe@cybertec.at>; +Cc: Virendra Kumar <viru_7683@yahoo.com>; pgsql-general

Laurenz Albe <laurenz.albe@cybertec.at> writes:
> On Fri, 2020-04-10 at 01:40 +0000, Virendra Kumar wrote:
>> [ $subject ]

> That is because foreign keys are implemented with system triggers, some of which
> are defined on the target table.
> Now CREATE TRIGGER does not require an ACCESS EXCLUSIVE lock, but DROP TRIGGER does.

Yeah.  The documentation could be clearer about this though.  The relevant
bit on the ALTER TABLE page is

    Addition of a foreign key constraint requires a SHARE ROW EXCLUSIVE
    lock on the referenced table, in addition to the lock on the table
    receiving the constraint.

which, at least to my eyes, isn't very clear that SHARE ROW EXCLUSIVE
is the lock level used for *both* tables.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2020-04-10 16:13 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-04-10 01:40 Different Lock Behavior With Create and Drop Foreign Key Virendra Kumar <viru_7683@yahoo.com>
2020-04-10 05:27 ` Laurenz Albe <laurenz.albe@cybertec.at>
2020-04-10 16:13   ` Tom Lane <tgl@sss.pgh.pa.us>

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