RE: BUG #15935: Auto increment column changes on error while inserting (violating unique constraint)

From: David Raymond <David(dot)Raymond(at)tomtom(dot)com>
To: "muhammadalinazarov(at)gmail(dot)com" <muhammadalinazarov(at)gmail(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: RE: BUG #15935: Auto increment column changes on error while inserting (violating unique constraint)
Date: 2019-07-31 13:36:11
Message-ID: VI1PR07MB5792827D83F27490A89F5A7487DF0@VI1PR07MB5792.eurprd07.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

That's expected. You can see the note here:
https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL

Note

Because smallserial, serial and bigserial are implemented using sequences, there may be "holes" or gaps in the sequence of values which appears in the column, even if no rows are ever deleted. A value allocated from the sequence is still "used up" even if a row containing that value is never successfully inserted into the table column. This may happen, for example, if the inserting transaction rolls back. See nextval() in Section 9.16 for details.

-----Original Message-----
From: PG Bug reporting form <noreply(at)postgresql(dot)org>
Sent: Wednesday, July 31, 2019 7:30 AM
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: muhammadalinazarov(at)gmail(dot)com
Subject: BUG #15935: Auto increment column changes on error while inserting (violating unique constraint)

The following bug has been logged on the website:

Bug reference: 15935
Logged by: Muhammadali Nazarov
Email address: muhammadalinazarov(at)gmail(dot)com
PostgreSQL version: 11.1
Operating system: Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-96-generic x86
Description:

I have seen one interesting behavior.
Let me show on example.

create table tmp (
id serial,
user_id int unique not null);

insert into tmp(user_id)
select 1;

our id => 1, autoincrement value 2;
insert into tmp(user_id)
select 1;

violating unique constraint, autoincrement value 3;
insert into tmp(user_id)
select 2;

select id, user_id
from tmp;
id | user_id
----+---------
1 | 1
3 | 2

Is this a bug or no?
Thank you for your attention.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2019-07-31 16:07:11 Re: BUG #15934: pg_dump output in wrong order if custom operator class is used as subtype_opclass in a range type
Previous Message PG Bug reporting form 2019-07-31 11:30:06 BUG #15935: Auto increment column changes on error while inserting (violating unique constraint)