Re: BUG #4648: needless deadlock on tables having foreign-key

From: Konstantin <kostya2702(at)rambler(dot)ru>
To: Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Bugs <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #4648: needless deadlock on tables having foreign-key
Date: 2009-02-12 23:31:06
Message-ID: 43315358.1234481467.162293012.47975@mcgi44.rambler.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

* Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com> [Thu, 12 Feb
2009 19:34:46 +0200]:
> It's certainly not ideal. It's an implementation artifact of the way
> MVCC and RI triggers work. The purpose is to protect from this
potential
> bug:

As I can see you are agree with problem existence and problem will not
be solved soon.
May be some workaround exists (without using `for share` locks)?
Additionally take a look at example below close to real world DB usage.
I just want to emphasize that mentioned implementation artifact born not
obvious
issues at applications side.
Pay attention on foreign keys order in tables.
============
Preparation:
-- Application CORE tables
CREATE TABLE account (aid integer PRIMARY KEY, temp integer);
CREATE TABLE plan (pid integer PRIMARY KEY, temp integer );
-- Plugin1 table
CREATE TABLE bill1 (bid integer PRIMARY KEY, aid integer REFERENCES
account, pid integer REFERENCES plan, temp integer );
-- Plugin2 table
CREATE TABLE bill2 (bid integer PRIMARY KEY, pid integer REFERENCES
plan, aid integer REFERENCES account, temp integer );
insert into account values(1,1);
insert into plan values(1,1);
insert into bill1 values(1,1,1,1);
insert into bill2 values(1,1,1,1);
Session1:
============
test=# begin; select * from plan where pid=1 for update;
BEGIN
pid | temp
-----+------
1 | 1
(1 row)
============

Session2:
============
test=# begin; select temp from bill1 where bid=1 for update;
BEGIN
temp
------
1
(1 row)
test=# update bill1 set temp=1 where bid=1;
UPDATE 1
test=# update bill1 set temp=1 where bid=1;
============
Transaction of session2 is locked

Session1:
============
test=# select * from account where aid=1 for update;
ERROR: deadlock detected
DETAIL: Process 2836 waits for ShareLock on transaction 3329597;
blocked by process 2838.
Process 2838 waits for ShareLock on transaction 3329596; blocked by
process 2836.
============
Now repeat the same using `bill2` table instead of `bill1` in session2 -
no deadlock.
Thank you for help.
Please let me know if workaround exists.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2009-02-13 00:10:34 Re: BUG #4648: needless deadlock on tables having foreign-key
Previous Message Tom Lane 2009-02-12 19:04:45 Re: BUG #4648: needless deadlock on tables having foreign-key