pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re:  Behavior of identity columns
2+ messages / 2 participants
[nested] [flat]

* Re:  Behavior of identity columns
@ 2022-08-04 08:52 sivapostgres@yahoo.com <sivapostgres@yahoo.com>
  2022-08-04 10:32 ` Re: Behavior of identity columns Francisco Olarte <folarte@peoplecall.com>
  0 siblings, 1 reply; 2+ messages in thread

From: sivapostgres@yahoo.com @ 2022-08-04 08:52 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hello,
I see 'identity' column values increment even when some error(s) occurs while inserting data.  Is that a known bug or known behavior?
Create script of table:**************************** CREATE TABLE public.users(    id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),    username character varying(255) COLLATE pg_catalog."default",    email character varying(255) COLLATE pg_catalog."default",    first_name character varying(255) COLLATE pg_catalog."default",    last_name character varying(255) COLLATE pg_catalog."default",    CONSTRAINT users_pkey PRIMARY KEY (id),    CONSTRAINT "test_UX" UNIQUE (username))WITH (    OIDS = FALSE)TABLESPACE pg_default;
ALTER TABLE public.users    OWNER to postgres;***********************************column id is int and identity columncolumn username is unique 
When I insert data that violates the unique constraint, I see that the id value gets incremented. And I see missing values in between when I add the correct data next time.
my Insert SQLinsert into users (username, email, first_name, last_name) values ('ONE', 'one@gmail.com', 'one', '1'); // id = 1insert into users (username, email, first_name, last_name) values ('ONE', 'one@gmail.com', 'one', '1'); // Insert failsinsert into users (username, email, first_name, last_name) values ('TWO', 'one@gmail.com', 'one', '1'); // id = 3insert into users0(username, email, first_name, last_name) values ('TWO', 'one@gmail.com', 'one', '1'); // insert fails as there is no users0 tableinsert into users (username, email, first_name, last_name) values ('THREE', 'one@gmail.com', 'one', '1'); // id = 5
ID gets incremented even when some wrong table name is mentioned in the query. Check 4th query.
Is it a known behavior or a known bug?

Happiness AlwaysBKR Sivaprakash

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

* Re: Behavior of identity columns
  2022-08-04 08:52 Re:  Behavior of identity columns sivapostgres@yahoo.com <sivapostgres@yahoo.com>
@ 2022-08-04 10:32 ` Francisco Olarte <folarte@peoplecall.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Francisco Olarte @ 2022-08-04 10:32 UTC (permalink / raw)
  To: sivapostgres@yahoo.com <sivapostgres@yahoo.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thu, 4 Aug 2022 at 10:52, sivapostgres@yahoo.com
<sivapostgres@yahoo.com> wrote:
> I see 'identity' column values increment even when some error(s) occurs while inserting data.  Is that a known bug or known behavior?

Known behaviour, explained thousand of times over the years.  Identity
is not a way to generate consecutive values, not ever guaranteed
ascending values, it is for generating unique values. The
implementation burn the values before commit/rollback for better
concurreency, search archives or docs for details. In your example, id
is allocated and burnt to generate the complete failing row, when it
fails it is gone. Search lists, google, the docs, its been explained
ad nauseam.

Francisco Olarte.





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


end of thread, other threads:[~2022-08-04 10:32 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-08-04 08:52 Re:  Behavior of identity columns sivapostgres@yahoo.com <sivapostgres@yahoo.com>
2022-08-04 10:32 ` Francisco Olarte <folarte@peoplecall.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