Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id B85CE9FA621 for ; Sat, 14 Oct 2006 21:11:48 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 63038-04 for ; Sat, 14 Oct 2006 21:11:33 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id E9A669FB4F1 for ; Sat, 14 Oct 2006 21:11:32 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.8/8.13.8) with ESMTP id k9F0BRWq004441 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 14 Oct 2006 18:11:29 -0600 (MDT) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.8/8.13.8) with ESMTP id k9F0BQgt020783; Sat, 14 Oct 2006 18:11:26 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.8/8.13.8/Submit) id k9F0BQrI020782; Sat, 14 Oct 2006 18:11:26 -0600 (MDT) (envelope-from mfuhr) Date: Sat, 14 Oct 2006 18:11:26 -0600 From: Michael Fuhr To: Tom Lane Cc: PostgreSQL SQL List , Joost Kraaijeveld Subject: Re: Foreign key reference counting strategy? Message-ID: <20061015001126.GA20713@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> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <13525.1160870286@sss.pgh.pa.us> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.142 tagged_above=0 required=5 tests=AWL, SPF_PASS X-Spam-Level: X-Archive-Number: 200610/136 X-Sequence-Number: 26525 On Sat, Oct 14, 2006 at 07:58:06PM -0400, Tom Lane wrote: > 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. Unless the inserters got there first. I just tested both ways; if the insert acquires the lock first then the delete fails, but if the delete acquires the lock first then the insert fails. -- Michael Fuhr