Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id E79369FB4D8 for ; Sat, 14 Oct 2006 20:58:21 -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 12458-03 for ; Sat, 14 Oct 2006 23:58:17 +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 71AB29FA621 for ; Sat, 14 Oct 2006 20:58:15 -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 k9ENw7Dr013526; Sat, 14 Oct 2006 19:58:07 -0400 (EDT) To: Michael Fuhr cc: PostgreSQL SQL List , Joost Kraaijeveld Subject: Re: Foreign key reference counting strategy? In-reply-to: <20061014234844.GA20342@winnie.fuhr.org> References: <1160828741.28838.9.camel@panoramix> <4530EB84.4050904@logix-tt.com> <20061014234844.GA20342@winnie.fuhr.org> Comments: In-reply-to Michael Fuhr message dated "Sat, 14 Oct 2006 17:48:44 -0600" Date: Sat, 14 Oct 2006 19:58:06 -0400 Message-ID: <13525.1160870286@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200610/135 X-Sequence-Number: 26524 Michael Fuhr writes: > On Sat, Oct 14, 2006 at 03:52:04PM +0200, Markus Schaber wrote: >> Create an "after delete" trigger on the referencing table that checks >> whether there still are records with the same key (IF EXISTS()), and >> deletes the referenced row otherwise. > In a concurrent environment that delete can fail with a foreign key > constraint violation because IF EXISTS won't see uncommitted changes > in other transactions. No, I don't think so, because the DELETE will already be holding exclusive lock on the doomed PK row, which any would-be inserters of matching FK rows will be blocked on. AFAICS the DELETE should go through and then the inserters will fail. Which is not necessarily better of course ... but if you want logic like this, you probably should have code to make the FK inserters deal with no-such-key failures. regards, tom lane