Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 0FE4F9FB4F8 for ; Sat, 14 Oct 2006 23:10:31 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 46914-01 for ; Sun, 15 Oct 2006 02:10:21 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 491C99FB4F4 for ; Sat, 14 Oct 2006 23:10:21 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k9F2ADBT014375; Sat, 14 Oct 2006 22:10:13 -0400 (EDT) To: Michael Fuhr cc: PostgreSQL SQL List , Joost Kraaijeveld Subject: Re: Foreign key reference counting strategy? In-reply-to: <20061015010725.GA21149@winnie.fuhr.org> References: <1160828741.28838.9.camel@panoramix> <4530EB84.4050904@logix-tt.com> <20061014234844.GA20342@winnie.fuhr.org> <13525.1160870286@sss.pgh.pa.us> <20061015001126.GA20713@winnie.fuhr.org> <13667.1160871610@sss.pgh.pa.us> <20061015010725.GA21149@winnie.fuhr.org> Comments: In-reply-to Michael Fuhr message dated "Sat, 14 Oct 2006 19:07:26 -0600" Date: Sat, 14 Oct 2006 22:10:13 -0400 Message-ID: <14374.1160878213@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200610/139 X-Sequence-Number: 26528 Michael Fuhr writes: > My intent wasn't to assert that IF EXISTS adds a new way for the > DELETE to fail. I was just pointing out that the test "if no > referencing rows exist then delete the referenced row" isn't > foolproof, viz., the DELETE can fail even though IF EXISTS said > there were no referencing rows. Actually, it's me that was confused --- I was thinking in terms of the original DELETE being done on the PK row, but of course that's not the case here --- the original DELETE would be on some FK row. It might be possible to make it work by adding a SELECT FOR UPDATE that locks the PK row in the trigger before doing the IF EXISTS test, but I think that that could create deadlock failures. You'd basically be making the transaction upgrade a share row lock to an exclusive lock, and that's generally unsafe. If you could ensure that your trigger runs before the RI integrity trigger, maybe it would work... regards, tom lane