pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: rasmus@mindplay.dk
To: pgsql-bugs@postgresql.org
Subject: BUG #14596: False primary/unique key constraint violations
Date: Wed, 22 Mar 2017 12:30:53 +0000
Message-ID: <20170322123053.1421.55154@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      14596
Logged by:          Rasmus Schultz
Email address:      rasmus@mindplay.dk
PostgreSQL version: 9.5.6
Operating system:   Win10 Pro/64
Description:        

Given the following schema:

CREATE TABLE public.test
(
   name character varying(100), 
   index integer, 
   CONSTRAINT unique_index PRIMARY KEY (index)
) 
WITH (
  OIDS = FALSE
);

And the following sample data:

INSERT INTO "test" ("name", "index") VALUES ('A', 0);
INSERT INTO "test" ("name", "index") VALUES ('B', 1);
INSERT INTO "test" ("name", "index") VALUES ('C', 2);

The following query will fail:

UPDATE "test" SET "index" = "index" + 1 WHERE "index" >= 0;

With the following error-message:

ERROR: duplicate key value violates unique constraint "unique_index"
SQL state: 23505
Detail: Key (index)=(1) already exists.

The reported constraint violation is incorrect - the net update does not
produce any violation of the constraint.

Dropping the index and executing the query, then recreating the index,
proves that the query does not in fact lead to a key violation.

The same happens with a non-primary unique constraint.

The same happens even if I wrap the update in a transaction. (which
shouldn't be necessary, given that a single statement should be atomic
either way.)

It looks like constraints are being checked row-by-row while the udpate is
happening?

I was expecting constraints would be checked at the end of an update, such
that an update producing a valid net update would execute fully - the fact
that constraints are checked while the update is still in progress seems
like an implementation detail, and I was not expecting that such a detail
would affect my ability to perform an update with a net valid result.

I was quite surprised by this, as PostgreSQL is generally super "correct"
about things, but in this case I was surprised.

It looks like my only option at this time is to forego any index on this
table?



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


view thread (7+ messages)  latest in thread

Message-ID: <20170322123053.1421.55154@wrigleys.postgresql.org>
Permalink:  ../20170322123053.1421.55154@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20170322123053.1421.55154@wrigleys.postgresql.org

 · 

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: rasmus@mindplay.dk
  Subject: Re: BUG #14596: False primary/unique key constraint violations
  In-Reply-To: <20170322123053.1421.55154@wrigleys.postgresql.org>
  References: <20170322123053.1421.55154@wrigleys.postgresql.org>

* 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