Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 42ABE1337BC8 for ; Tue, 22 Mar 2011 06:50:42 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 54695-08 for ; Tue, 22 Mar 2011 09:50:30 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by mail.postgresql.org (Postfix) with ESMTP id E448F1336FA7 for ; Tue, 22 Mar 2011 06:50:29 -0300 (ADT) Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by wwwmaster.postgresql.org (8.14.3/8.14.3) with ESMTP id p2M9oT1o088720 for ; Tue, 22 Mar 2011 09:50:29 GMT (envelope-from www@wwwmaster.postgresql.org) Received: (from www@localhost) by wwwmaster.postgresql.org (8.14.3/8.14.3/Submit) id p2M9oTLW088719; Tue, 22 Mar 2011 09:50:29 GMT (envelope-from www) Date: Tue, 22 Mar 2011 09:50:29 GMT Message-Id: <201103220950.p2M9oTLW088719@wwwmaster.postgresql.org> To: pgsql-bugs@postgresql.org Subject: BUG #5940: Creating a foreign key on an empty table locks table From: "Adriaan" Content-Type: text/plain; charset=utf-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 201103/268 X-Sequence-Number: 29778 The following bug has been logged online: Bug reference: 5940 Logged by: Adriaan Email address: adriaanvk@gmail.com PostgreSQL version: 8.4 / 9.0 Operating system: Ubuntu Description: Creating a foreign key on an empty table locks table Details: I am using creation of table during the runtime of my application. This gives some locking issues which are actually not needed. Let me describe what i am doing. 1. Create a table A 2. Create a table B 3. Insert a record in A 4. start transaction t1 5. select a record from A -> read lock on A (the proces is not starting another parallel trans:) 6. in another transaction t2 create a foreign key from B to A. --> this is causing a lock on table A, thus causing a hanging lock I think the lock during the creation on foreign key is not needed since B has no records and since the table definition of A is NOT changed. Is it possible to remove the lock during creation of foreign keys in such situations so the database is more robust for runtime table alters? Test queries: 1. create table A (field1 int primary key); 2. create table B (field2 int); 3. insert into A (field1) VALUES (3); 4. start transaction; select * from A where field1=3; in another shell start another trans: 5. alter table B add constraint "x" foreign key (field2) references A (field1) on update no action --> hanging lock now, since waiting for query 4 btw: mention that database engines lik mssql are very flesible in such scenarios, which makes it feeling robust.