简体   繁体   中英

Hibernate and postgresql bigserial - nonsequential

I'm using postgres and Hibernate, and I'm noticing something strange with my generated id's. It makes huge jumps in the sequence, I have a table with 1524 rows, still the highest id is 602778.

My id column is defined as this:

id bigserial

and is backed by nextval('my_id_seq'::regclass)

my_id_seq has a start value of 1 and an increment of 1, and increments nicely when invoking nextval on it through SQuirreL.

In my Hibernate entity, id is mapped like this:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

What could cause such sudden jumps in the id sequence (at one point it jumps from 4152 to 12041)?

rollbacks and erros would do that. Eg:

t=# create table s(i serial);
CREATE TABLE
t=# insert into s values (DEFAULT);
INSERT 0 1
t=# insert into s values (DEFAULT) returning i;;
 i
---
 2
(1 row)

INSERT 0 1

now start a transaction:

t=# begin;
BEGIN
t=# insert into s values (DEFAULT) returning i;
 i
---
 3
(1 row)

INSERT 0 1
t=# rollback;
ROLLBACK

value 3 used, and there's a gap now:

t=# insert into s values (DEFAULT) returning i;
 i
---
 4
(1 row)

INSERT 0 1

check:

t=# select * from s;
 i
---
 1
 2
 4
(3 rows)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM