X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id D8CD232A043 for ; Tue, 28 Sep 2004 07:59:17 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 92948-08 for ; Tue, 28 Sep 2004 06:59:14 +0000 (GMT) Received: from matrix.gatewaynet.com (matrix.gatewaynet.com [217.19.69.50]) by svr1.postgresql.org (Postfix) with ESMTP id 4E7F932A057 for ; Tue, 28 Sep 2004 07:59:04 +0100 (BST) Received: from matrix.gatewaynet.com (localhost.localdomain [127.0.0.1]) by matrix.gatewaynet.com (8.12.8/8.12.8) with ESMTP id i8S6ELCC029233 for ; Tue, 28 Sep 2004 09:14:21 +0300 Received: from localhost (achill@localhost) by matrix.gatewaynet.com (8.12.8/8.12.8/Submit) with ESMTP id i8S6EKk0029229 for ; Tue, 28 Sep 2004 09:14:21 +0300 Date: Tue, 28 Sep 2004 09:14:20 +0300 (EEST) From: Achilleus Mantzios To: pgsql-sql@postgresql.org Subject: FOREIGN KEY and AccessExclusiveLock In-Reply-To: <4121D55D.1040105@archonet.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/239 X-Sequence-Number: 19085 Hi, all the below are for PostgreSQL 7.4.2. I noticed that during ALTER TABLE kid ADD CONSTRAINT "parcon" FOREIGN KEY (parid) on parent(id) pgsql tries to acquire an AccessExclusiveLock on *both* kid (which is pretty natural since it adheres with the docs, and it is an alter command) *and* parent. Whats the purpose of the AccessExclusiveLock on parent table? Is there a way this alter command will affect parent's data or schema? Lets see a simple example: dynacom=# CREATE TABLE parent(id int PRIMARY KEY); CREATE TABLE dynacom=# CREATE TABLE kid(parid int); CREATE TABLE dynacom=# Then in session 1: dynacom=# BEGIN ; BEGIN dynacom=# SELECT * from parent ; id ---- (0 rows) dynacom=# In Session 2: dynacom=# BEGIN ; BEGIN dynacom=# ALTER TABLE kid ADD CONSTRAINT "parcon" FOREIGN KEY (parid) references parent(id); *here Session 2 is deadlocked* In Session 1: dynacom=# SELECT c.relname,l.mode,l.granted from pg_locks l,pg_class c where l.relation=c.oid; relname | mode | granted --------------+---------------------+--------- kid | AccessExclusiveLock | t pg_locks | AccessShareLock | t pg_class | AccessShareLock | t parent | AccessExclusiveLock | f parent | AccessShareLock | t pg_namespace | AccessShareLock | t (6 rows) dynacom=# Again in Session 1: dynacom=# end; COMMIT dynacom=# In Session 2: ALTER TABLE dynacom=# Now imagine that session 2 is "called" by session 1, with commiting after session 2 is done, we have clearly a deadlock situation. The question is why an AccessExclusiveLock must be created for the FK table? Actually it puzzled me alot, since for me Session 1 is a java program "executing" XML in various forms, one of them being plain UNIX (exec()) commands, which in turn sometimes are psql commands. It was hard to imagine that an innocent select on the parent table in the java program and an alter table on a child table as a pgsql UNIX command would cause a deadlock situation. The natural workaround was to immediately commit in the java program after select and before UNIX command (psql) is executed. Thanx. -- -Achilleus