pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Joseph Krogh <andreas@visena.com>
To: pgsql-general@lists.postgresql.org
Subject: Sv: Primary Key Update issue ?
Date: Fri, 6 Sep 2019 11:16:39 +0200 (CEST)
Message-ID: <VisenaEmail.c.cb96f40a4968df75.16d05db8153@tc7-visena> (raw)
In-Reply-To: <VI1PR0501MB25747C214477E3CBD8102DFDEFBA0@VI1PR0501MB2574.eurprd05.prod.outlook.com>

På fredag 06. september 2019 kl. 11:06:04, skrev Patrick FICHE <
Patrick.Fiche@aqsacom.com <mailto:Patrick.Fiche@aqsacom.com>>: 
Hello,



While doing some testing on a Postgresql database, I encountered a strange 
behavior which is very simple to reproduce.

I just wanted to know if this is expected behavior or if it should be 
considered as an issue.



The scenario to reproduce it is the following.



CREATE TABLE Test ( pKey integer, Val integer, CONSTRAINT PK_Test PRIMARY Key( 
pKey ) );



INSERT INTO Test( pKey, Val ) VALUES ( 1, 1 );

INSERT INTO Test( pKey, Val ) VALUES ( 2, 2 );



UPDATE Test SET pKey = pKey + 1;



Here is the error that I get.



SQL Error [23505]: ERROR: duplicate key value violates unique constraint 
"pk_test"

 Detail: Key (pkey)=(2) already exists.



I was expecting pKey to be incremented for each row, which would still respect 
the unique constraint….



I’m currently using PostgreSQL 11.5 but have the same problem on PostgreSQL 
10.3 server.



Best Regards, 
It works if you add "DEFERRABLE INITIALLY DEFERRED" to the PK: CREATE TABLE 
Test ( pKey integer, Val integer, CONSTRAINT PK_Test PRIMARY Key( pKey ) 
DEFERRABLE INITIALLY DEFERRED );
andreak@[local]:5432 11.5 test=# CREATE TABLE Test ( pKey integer, Val 
integer, CONSTRAINT PK_Test PRIMARY Key( pKey ) DEFERRABLE INITIALLY DEFERRED );
 CREATE TABLE
andreak@[local]:5432 11.5 test=# INSERT INTO Test( pKey, Val ) VALUES ( 1, 1 );
 INSERT 0 1
andreak@[local]:5432 11.5 test=# INSERT INTO Test( pKey, Val ) VALUES ( 2, 2 );
 INSERT 0 1
andreak@[local]:5432 11.5 test=# UPDATE Test SET pKey = pKey + 1; 
 UPDATE 2
-- Andreas Joseph Krogh CTO / Partner - Visena AS Mobile: +47 909 56 963 
andreas@visena.com <mailto:andreas@visena.com> www.visena.com 
<https://www.visena.com;  <https://www.visena.com;

Message-ID: <VisenaEmail.c.cb96f40a4968df75.16d05db8153@tc7-visena>
Permalink:  ../VisenaEmail.c.cb96f40a4968df75.16d05db8153@tc7-visena/
Also on:    postgresql.org/message-id/VisenaEmail.c.cb96f40a4968df75.16d05db8153@tc7-visena

 · 

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-general@postgresql.org
  Cc: andreas@visena.com
  Subject: Re: Primary Key Update issue ?
  In-Reply-To: <VisenaEmail.c.cb96f40a4968df75.16d05db8153@tc7-visena>
  References: <VI1PR0501MB25747C214477E3CBD8102DFDEFBA0@VI1PR0501MB2574.eurprd05.prod.outlook.com> <VisenaEmail.c.cb96f40a4968df75.16d05db8153@tc7-visena>

* 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