Re: Foreign key reference counting strategy?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL SQL List <pgsql-sql(at)postgresql(dot)org>, Joost Kraaijeveld <J(dot)Kraaijeveld(at)Askesis(dot)nl>
Subject: Re: Foreign key reference counting strategy?
Date: 2006-10-15 00:11:26
Message-ID: 20061015001126.GA20713@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Oct 14, 2006 at 07:58:06PM -0400, Tom Lane wrote:
> Michael Fuhr <mike(at)fuhr(dot)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.

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2006-10-15 00:20:10 Re: Foreign key reference counting strategy?
Previous Message Tom Lane 2006-10-14 23:58:06 Re: Foreign key reference counting strategy?