Re: Foreign key reference counting strategy? - Mailing list pgsql-sql

From Tom Lane
Subject Re: Foreign key reference counting strategy?
Date
Msg-id 13525.1160870286@sss.pgh.pa.us
Whole thread Raw
In response to Re: Foreign key reference counting strategy?  (Michael Fuhr <mike@fuhr.org>)
Responses Re: Foreign key reference counting strategy?
List pgsql-sql
Michael Fuhr <mike@fuhr.org> 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


pgsql-sql by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: Foreign key reference counting strategy?
Next
From: Michael Fuhr
Date:
Subject: Re: Foreign key reference counting strategy?