pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: pgsql-bugs@postgresql.org
Subject: Why does FK creation still take an access exclusive lock on the referenced table?
Date: Thu, 07 Feb 2013 10:06:40 -0800
Message-ID: <5113ED30.1070903@agliodbs.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Folks,

I'd thought this was fixed with some of the ALTER changes in 9.2, but
apparently not (9.2.2):

SESSION1:

sampledata=# create table test1 ( id int not null primary key );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
"test1_pkey" for table "test1"
CREATE TABLE
Time: 55.224 ms       ^
sampledata=# create table test2 ( id int not null primary key, test1 int );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
"test2_pkey" for table "test2"
CREATE TABLE
Time: 40.841 ms
sampledata=# insert into test1 select i from generate_series(1,10) as gs(i);
sampledata=# begin;
BEGIN
Time: 0.088 ms
sampledata=# alter table test2 add constraint test1_fk foreign key (
test1 ) references test1(id);
ALTER TABLE
Time: 2.185 ms
sampledata=#

SESSION2:

sampledata=# select * from test1;
... wait forever
^C

sampledata=# select locktype, mode, relname from pg_locks join pg_class
on relation=oid;
 locktype |        mode         |          relname
----------+---------------------+----------------------------
 relation | AccessShareLock     | test1_pkey
 relation | AccessShareLock     | test2_pkey
 relation | AccessShareLock     | pg_class_relname_nsp_index
 relation | AccessShareLock     | pg_class_oid_index
 relation | AccessShareLock     | pg_class
 relation | AccessShareLock     | pg_locks
 relation | AccessShareLock     | test2
 relation | AccessExclusiveLock | test2
 relation | AccessShareLock     | test1
 relation | RowShareLock        | test1
 relation | AccessExclusiveLock | test1

I understand why establishing an FK needs an ExclusiveLock on the
referenced table, but it doesn't need an AccessExclusiveLock.  This
causes lots of deployment issues for users.


-- 
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (2+ messages)  latest in thread

Message-ID: <5113ED30.1070903@agliodbs.com>
Permalink:  ../5113ED30.1070903@agliodbs.com/
Also on:    postgresql.org/message-id/5113ED30.1070903@agliodbs.com

 · 

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-bugs@postgresql.org
  Cc: josh@agliodbs.com
  Subject: Re: Why does FK creation still take an access exclusive lock on the referenced table?
  In-Reply-To: <5113ED30.1070903@agliodbs.com>
  References: <5113ED30.1070903@agliodbs.com>

* 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