Lakhani Aliraza
Lakhani Aliraza

Reputation: 477

Why primary key ids are not in sequence?

My last record's primary key was 552 & when I added a new record it's primary key allotted is 584.

I'm surprised & would like to know possible reasons for this behavior.

Application Details :

Additional Info -> I'm using rails admin panel to add new record

Upvotes: 1

Views: 429

Answers (1)

Vasfed
Vasfed

Reputation: 18474

Possible reasons:

  1. Some records were added+deleted
  2. Inserting transaction was reverted for some reason, From postgres manual:

    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.

  3. Corresponding sequence table_name_seq has increment more than 1 (probably not your case, sometimes is useful for sharding)

Upvotes: 4

Related Questions