In postgres 11.2 I created a table with id serial4 NOT NULL
, I thought the id
would always increment by 1
but I noticed it's not.
Table Order
creation SQL
CREATE TABLE public.order (
"createdAt" timestamptz NOT NULL DEFAULT now(),
"updatedAt" timestamptz NOT NULL DEFAULT now(),
"deletedAt" timestamptz NULL,
id serial4 NOT NULL,
.....
);
For example, when I queried select id from device d where id < 4000000 order by "id" desc limit 10
, some ids are not incremented by 1
id
3999794
3999791
3999668
3999660
3999585
3999578
3999543
3999541
3999334
3999023
Although it looks like in some id ranges, the ids are always increment added by 1.
For example:
select id from device d where id < 2000 order by "id" desc limit 10
or
select id from device d where id < 5000000 order by "id" desc limit 10
id
1999
1998
1997
1996
1995
1994
1993
1992
1991
1990
id
4999984
4999983
4999982
4999981
4999980
4999979
4999978
4999977
4999976
4999975
are all sequence id rows added by 1
.
Since I always use soft delete
by the deletedAt
column and I've checked my code that it would never delete the rows.
Would postgres 11.2 increment data column serial4
not by 1
but other values in some conditions? How can we prevent it happening?