Different Lock Behavior With Create and Drop Foreign Key

Started by Virendra Kumaralmost 6 years ago3 messagesgeneral
Jump to latest
#1Virendra Kumar
viru_7683@yahoo.com

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

#2Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Virendra Kumar (#1)
Re: Different Lock Behavior With Create and Drop Foreign Key

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

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Laurenz Albe (#2)
Re: Different Lock Behavior With Create and Drop Foreign Key

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