pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <kgrittn@ymail.com>
To: nikita.y.volkov@mail.ru <nikita.y.volkov@mail.ru>
To: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Subject: Re: BUG #12330: ACID is broken for unique constraints
Date: Fri, 26 Dec 2014 15:23:37 +0000 (UTC)
Message-ID: <969937206.892402.1419607417808.JavaMail.yahoo@jws10072.mail.ne1.yahoo.com> (raw)
In-Reply-To: <20141224140153.11501.49752@wrigleys.postgresql.org>
References: <20141224140153.11501.49752@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

"nikita.y.volkov@mail.ru" <nikita.y.volkov@mail.ru> wrote:

> Executing concurrent transactions inserting the same value of a
> unique key fails with the "duplicate key" error under code
> "23505" instead of any of transaction conflict errors with a
> "40***" code.

This is true, and can certainly be inconvenient when using
serializable transactions to simplify handling of race conditions,
because you can't just test for a SQLSTATE of '40001' or '40P01' to
indicate the need to retry the transaction.  You have two
reasonable ways to avoid duplicate keys if the values are synthetic
and automatically generated.  One is to use a SEQUENCE object to
generate the values.  The other (really only recommended if gaps in
the sequence are a problem) is to have the serializable transaction
update a row to "claim" the number.

Otherwise you need to consider errors related to duplicates as
possibly being caused by a concurrent transaction.  You may want to
do one transaction retry in such cases, and fail if an identical
error is seen.  Keep in mind that these errors do not allow
serialization anomalies to appear in the committed data, so are
arguably not violations of ACID principles -- more of a wart on the
otherwise clean technique of using serializable transactions to
simplify application programming under concurrency.

Thinking about it just now I think we might be able to generate a
write conflict instead of a duplicate key error for this case by
checking the visibility information for the duplicate row.  It
might not even have a significant performance impact, since we need
to check visibility information to generate the duplicate key
error.  That would still leave similar issues (where similar
arguments can be made) relating to foreign keys; but those can
largely be addressed already by declaring the constraints to be
DEFERRED -- and anyway, that would be a separate fix.

I'm moving this discussion to the -hackers list so that I can ask
other developers:

Are there any objections to generating a write conflict instead of
a duplicate key error if the duplicate key was added by a
concurrent transaction?  Only for transactions at isolation level
REPEATABLE READ or higher?

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers



view thread (27+ messages)  latest in thread

Message-ID: <969937206.892402.1419607417808.JavaMail.yahoo@jws10072.mail.ne1.yahoo.com>
Permalink:  ../969937206.892402.1419607417808.JavaMail.yahoo@jws10072.mail.ne1.yahoo.com/
Also on:    postgresql.org/message-id/969937206.892402.1419607417808.JavaMail.yahoo@jws10072.mail.ne1.yahoo.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: kgrittn@ymail.com, nikita.y.volkov@mail.ru, pgsql-hackers@postgresql.org
  Subject: Re: BUG #12330: ACID is broken for unique constraints
  In-Reply-To: <969937206.892402.1419607417808.JavaMail.yahoo@jws10072.mail.ne1.yahoo.com>
  References: <20141224140153.11501.49752@wrigleys.postgresql.org> <969937206.892402.1419607417808.JavaMail.yahoo@jws10072.mail.ne1.yahoo.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