Re: Strange result using transactions

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Matthijs Melissen <melissen(at)phil(dot)uu(dot)nl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Strange result using transactions
Date: 2007-03-27 11:30:53
Message-ID: 20070327113053.GA89497@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Mar 27, 2007 at 12:41:53PM +0200, Matthijs Melissen wrote:
> I get DELETE 0 even if a record with id=20 already exists before both
> transactions.

Transaction 2 (T2) is deleting the version of the row with id = 20
that was visible to T2 when it executed its DELETE. Since T1 deleted
that version of the row first, T2 finds no row to delete after T1
commits and releases its locks. T2 doesn't know about the row that
T1 inserted because T1 hadn't committed yet when T2 executed its
DELETE.

Run T2 as a Serializable transaction and you'll see different
behavior:

1) begin;
1) delete from forum where id = 20;
1) insert into forum (id, name) values (20, 'test');
2) begin isolation level serializable;
2) delete from forum where id = 20;
1) commit;

When T1 commits T2 should fail with SQLSTATE 40001 SERIALIZATION
FAILURE ("could not serialize access due to concurrent update").
T2 still doesn't know about the row that T1 inserted but now T2
knows that something happened to the version of the row it was
trying to delete.

--
Michael Fuhr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2007-03-27 12:51:50 Re: cutting out the middleperl
Previous Message Denis Gasparin 2007-03-27 10:54:49 Query that does not use indexes