Fwd: question on foreign key lock

From: Filip Rembiałkowski <filip(dot)rembialkowski(at)gmail(dot)com>
To: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Fwd: question on foreign key lock
Date: 2012-11-08 08:45:58
Message-ID: CAP_rwwmCVbaKAydiYTykpV6axt0LWFCt9pDf8YC+wTBHSwEXng@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Hi,

maybe this is a better group for this question?

I can't see why creating foreign key on table A referencing table B,
generates an AccessExclusiveLock on B.
It seems (to a layman :-) ) that only writes to B should be blocked.

I'm really interested if this is either expected effect or any open TODO
item or suboptimal behavior of postgres.

Thanks

---------- Forwarded message ----------
From: Filip Rembiałkowski <filip(dot)rembialkowski(at)gmail(dot)com>
Date: Thu, Nov 1, 2012 at 5:33 PM
Subject: question on foreign key lock
To: pgsql-general list <pgsql-general(at)postgresql(dot)org>

Hello.

Why adding FK creates AccessExclusiveLock on referenced tabble?

{{{
CREATE TABLE A ( id integer, idb integer );
INSERT INTO A (id,idb) SELECT x, x%4 FROM generate_series(1,100) x;

CREATE TABLE B ( id int primary key );
INSERT INTO B VALUES (0),(1),(2),(3);

BEGIN;
ALTER TABLE A ADD CONSTRAINT a_idb_fkey FOREIGN KEY (idb) REFERENCES b;
SELECT * FROM pg_locks l, pg_class c WHERE l.pid = pg_backend_pid() AND
l.locktype='relation' AND l.mode ilike '%exclusive%' AND l.relation=c.oid;
ROLLBACK;
}}}

Last SELECT is showing AccessExclusive on B.
Why not Exclusive?

Thanks,
Filip

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Marko Kreen 2012-11-08 09:47:37 Re: Comparing txid_current() to xmin
Previous Message Pavel Stehule 2012-11-08 07:55:24 Re: find a substring on a text (data type) column

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2012-11-08 08:53:53 Re: [BUGS] BUG #7534: walreceiver takes long time to detect n/w breakdown
Previous Message Heikki Linnakangas 2012-11-08 08:33:42 Re: [BUGS] BUG #7534: walreceiver takes long time to detect n/w breakdown