pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14909: nextval() bug
2+ messages / 2 participants
[nested] [flat]

* BUG #14909: nextval() bug
@ 2017-11-15 10:51  personal@rysmax.com
  0 siblings, 1 reply; 2+ messages in thread

From: personal@rysmax.com @ 2017-11-15 10:51 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: personal@rysmax.com

The following bug has been logged on the website:

Bug reference:      14909
Logged by:          Max Rys
Email address:      personal@rysmax.com
PostgreSQL version: 10.1
Operating system:   macOS 10.12.6
Description:        

I run this simple queries on PostgeSQL 9.6 and 10.1:

DROP TABLE IF EXISTS "user";
CREATE TABLE "user" (
  id serial primary key not null,
  email varchar(64) not null
);
INSERT INTO "user" (id, email) VALUES ('1', 'admin@example.com');
INSERT INTO "user" (id, email) VALUES ('2', 'user@example.com');

All done without errors.



When i try to run the query bellow:

INSERT INTO "user" (email) VALUES ('test@example.com');

and got the next error in pgAdmin and from the PHP:

ERROR:  duplicate key value violates unique constraint "user_pkey"
DETAIL:  Key (id)=(1) already exists.

but when i try to run this query 2 times the query executed without errors
on 3rd time.



I think this is a bug in nextval() function.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #14909: nextval() bug
@ 2017-11-15 11:26  hubert depesz lubaczewski <depesz@depesz.com>
  parent: personal@rysmax.com
  0 siblings, 0 replies; 2+ messages in thread

From: hubert depesz lubaczewski @ 2017-11-15 11:26 UTC (permalink / raw)
  To: personal@rysmax.com; +Cc: pgsql-bugs

On Wed, Nov 15, 2017 at 10:51:36AM +0000, personal@rysmax.com wrote:
> CREATE TABLE "user" (
>   id serial primary key not null,
>   email varchar(64) not null
> );
> INSERT INTO "user" (id, email) VALUES ('1', 'admin@example.com');
> INSERT INTO "user" (id, email) VALUES ('2', 'user@example.com');
> 
> All done without errors.
> 
> 
> 
> When i try to run the query bellow:
> 
> INSERT INTO "user" (email) VALUES ('test@example.com');
> 
> and got the next error in pgAdmin and from the PHP:
> 
> ERROR:  duplicate key value violates unique constraint "user_pkey"
> DETAIL:  Key (id)=(1) already exists.
> 
> but when i try to run this query 2 times the query executed without errors
> on 3rd time.
> I think this is a bug in nextval() function.


No, it's not.

when you insert providing value for id column, default (nextval) is not
called, so sequence is not updated.

So, when you finally try to insert without id, nextval is called and
returns first unused (from the point of view of sequence) value - 1.
Which already is in the table because you "forcibly" inserted it.

To avoid this problem you have to either:

1. run setval(...) after insertiing data with some forced ids
2. always use nextval/default for id column.

Best regards,

depesz





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2017-11-15 11:26 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-11-15 10:51 BUG #14909: nextval() bug personal@rysmax.com
2017-11-15 11:26 ` hubert depesz lubaczewski <depesz@depesz.com>

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