pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: chenhj <chjischj@163.com>
To: Jeff Janes <jeff.janes@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #13723: "duplicate key" error occurs when update delete and insert the same row concurrently
Date: Tue, 27 Oct 2015 23:03:23 +0800 (GMT+08:00)
Message-ID: <5adc38f6.16e.150a9d339b0.Coremail.chjischj@163.com> (raw)
In-Reply-To: <CAMkU=1yaj_42p8zC2Gh7W0gmNsh4mU+sDB4sAPzCvTP_W3h4Zg@mail.gmail.com>
References: <20151025110136.3017.39398@wrigleys.postgresql.org>
	<31956.1445787503@sss.pgh.pa.us>
	<377d9d8.31c6.150a00b83ea.Coremail.chjischj@163.com>
	<CAMkU=1yaj_42p8zC2Gh7W0gmNsh4mU+sDB4sAPzCvTP_W3h4Zg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Hi,Jeff


> That is true only if the UPDATE actually updates the row.  If you log the query completion tags, I think you will
> find the error is preceded by an UPDATE of zero rows.  Now you could argue that this itself is a violation: 
> that if a single statement sees that a row was deleted it is obliged to also see the row that was inserted 
> in the same transaction as the deletion.  But this is documented, and is unlikely to change.


My test result is just as you said, as the following:


test9.sql:
---------------------------------------
begin;
select * from tb1 where id=1;
update tb1 set c=2 where id=1;
delete from tb1 where id=1;
insert into tb1 values(1,2);
commit;
begin;
select * from tb1 where id=1;
update tb1 set c=2 where id=1;
delete from tb1 where id=1;
insert into tb1 values(1,2);
commit;
...
---------------------------------------


[postgres@localhost ~]$ psql -f test9.sql >a1.log 2>&1 &
[postgres@localhost ~]$ psql -f test9.sql >a2.log  2>&1 &


a2.log:
---------------------------------------
...
BEGIN
 id | c
----+---
  1 | 2
(1 row)


UPDATE 0
DELETE 0
psql:test9.sql:17: ERROR:  duplicate key value violates unique constraint "tb1_pkey"
DETAIL:  Key (id)=(1) already exists.
ROLLBACK
...
---------------------------------------




Even "EXPLICITLY" add "select ... for update" at first, "duplicate key " error still ocurrs.


test10.sql:
---------------------------------------
begin;
select * from tb1 where id=1 for update;
update tb1 set c=2 where id=1;
delete from tb1 where id=1;
insert into tb1 values(1,2);
commit;
begin;
select * from tb1 where id=1 for update;
update tb1 set c=2 where id=1;
delete from tb1 where id=1;
insert into tb1 values(1,2);
commit;
...
---------------------------------------


[postgres@localhost ~]$ psql -f test10.sql >b1.log 2>&1 &
[postgres@localhost ~]$ psql -f test10.sql >b2.log  2>&1 &


b1.log:
---------------------------------------
...
BEGIN
 id | c
----+---
(0 rows)


UPDATE 0
DELETE 0
psql:test10.sql:29: ERROR:  duplicate key value violates unique constraint "tb1_pkey"
DETAIL:  Key (id)=(1) already exists.
ROLLBACK
...
---------------------------------------


So, "select * from tb1 where id=1 for update" also could see an inconsistent snapshot (sort of "Dirty Read").




Best Regard,
Chen Huajun

view thread (10+ messages)

Message-ID: <5adc38f6.16e.150a9d339b0.Coremail.chjischj@163.com>
Permalink:  ../5adc38f6.16e.150a9d339b0.Coremail.chjischj@163.com/
Also on:    postgresql.org/message-id/5adc38f6.16e.150a9d339b0.Coremail.chjischj@163.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: chjischj@163.com, jeff.janes@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: BUG #13723: "duplicate key" error occurs when update delete and insert the same row concurrently
  In-Reply-To: <5adc38f6.16e.150a9d339b0.Coremail.chjischj@163.com>
  References: <20151025110136.3017.39398@wrigleys.postgresql.org> <31956.1445787503@sss.pgh.pa.us> <377d9d8.31c6.150a00b83ea.Coremail.chjischj@163.com> <CAMkU=1yaj_42p8zC2Gh7W0gmNsh4mU+sDB4sAPzCvTP_W3h4Zg@mail.gmail.com> <5adc38f6.16e.150a9d339b0.Coremail.chjischj@163.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox