Re: adding foreign key constraint locks up table - Mailing list pgsql-performance

From Tom Lane
Subject Re: adding foreign key constraint locks up table
Date
Msg-id 19238.1293548900@sss.pgh.pa.us
Whole thread Raw
In response to Re: adding foreign key constraint locks up table  (Florian Weimer <fweimer@bfk.de>)
Responses Re: adding foreign key constraint locks up table  (Florian Weimer <fweimer@bfk.de>)
List pgsql-performance
Florian Weimer <fweimer@bfk.de> writes:
>> Whenever I try to create a new table "bbb" with foreign key pointing
>> to "aaa". The operation locks, and reading "aaa" is not possible. The
>> query also never seems to finish.

What that sounds like to me is there's some long-running (probably idle)
open transaction that's holding AccessShare lock on aaa.  The ALTER is
blocked waiting for that xact to finish and release its lock.
Everything else queues up behind the ALTER.  A bit of looking in
pg_locks would find the culprit, if this theory is right.

> Do you mean that the ALTER query and subsequent queries are shown as
> "waiting" in pg_stat_activity?  In this case, I'm also wondering why
> this is inecessary.

ALTER ADD FOREIGN KEY must lock both tables to add triggers to them.

            regards, tom lane

pgsql-performance by date:

Previous
From: "Kevin Grittner"
Date:
Subject: Re: adding foreign key constraint locks up table
Next
From: Greg Smith
Date:
Subject: Re: Regression: 8.3 2 seconds -> 8.4 100+ seconds