Re: Fwd: question on foreign key lock

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Filip Rembiałkowski <filip(dot)rembialkowski(at)gmail(dot)com>
Cc: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fwd: question on foreign key lock
Date: 2012-11-12 16:20:28
Message-ID: CA+TgmoYp_UfVj392xkpeWCDiv4Ry0Jt30mgcNjSjtRABtCdK-A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Thu, Nov 8, 2012 at 3:45 AM, Filip Rembiałkowski
<filip(dot)rembialkowski(at)gmail(dot)com> wrote:
> maybe this is a better group for this question?
>
> I can't see why creating foreign key on table A referencing table B,
> generates an AccessExclusiveLock on B.
> It seems (to a layman :-) ) that only writes to B should be blocked.
>
> I'm really interested if this is either expected effect or any open TODO
> item or suboptimal behavior of postgres.

This comment explains it:

/*
* Grab an exclusive lock on the pk table, so that someone doesn't delete
* rows out from under us. (Although a lesser lock would do for that
* purpose, we'll need exclusive lock anyway to add triggers to the pk
* table; trying to start with a lesser lock will just create a risk of
* deadlock.)
*/
pkrel = heap_openrv(fkconstraint->pktable, AccessExclusiveLock);

Concurrent DDL is something that's been discussed in detail on this
list in the past; unfortunately, there are some tricky race conditions
are the shared invalidation queue and SnapshotNow that make it hard to
implement properly. I'm hoping to have some time to work on this at
some point, but it hasn't happened yet.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Philippe Amelant 2012-11-12 16:39:36 Re: Understanding streaming replication
Previous Message Ben Chobot 2012-11-12 15:45:55 Re: PSA: XFS and Linux Cache Poisoning

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2012-11-12 16:22:23 Re: TRUNCATE SERIALIZABLE and frozen COPY
Previous Message Tom Lane 2012-11-12 16:20:08 Re: TRUNCATE SERIALIZABLE and frozen COPY