From: Tom Lane <tgl@sss.pgh.pa.us>
To: chjischj@163.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #13723: "duplicate key" error occurs when update delete and insert the same row concurrently
Date: Sun, 25 Oct 2015 11:38:23 -0400
Message-ID: <31956.1445787503@sss.pgh.pa.us> (raw)
In-Reply-To: <20151025110136.3017.39398@wrigleys.postgresql.org>
References: <20151025110136.3017.39398@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
chjischj@163.com writes:
> When i run sysbench's complex test with PostgreSQL, the following error
> always occured.
> duplicate key value violates unique constraint "%s"
> It seems to be a bug which occurs when executing update,delete and
> insert(within one transaction) the same row concurrently.
I see no bug here; you're just making a mistaken assumption about how
cross-transaction serialization works. At some point you're going to end
up with a timing in which both clients are trying to do the DELETE. Only
one does it; the other waits for that row change to commit, sees it's
done, and concludes that there's nothing for it to do. (In particular,
it will not see the row that was inserted later in the other transaction,
because that's too new.) Now the second one's INSERT fails because
there's already a row with id=1.
If you want this sort of coding to execute stably, you could consider
taking out a table-level lock, or some other way of preventing clients
from concurrently deleting+inserting the same key. Or, just don't do
that in the first place.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
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: tgl@sss.pgh.pa.us, chjischj@163.com
Subject: Re: BUG #13723: "duplicate key" error occurs when update delete and insert the same row concurrently
In-Reply-To: <31956.1445787503@sss.pgh.pa.us>
References: <20151025110136.3017.39398@wrigleys.postgresql.org> <31956.1445787503@sss.pgh.pa.us>
* 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