If you want gapless, sequential identifiers, don't use a SERIAL
or BIGSERIAL
(SEQUENCE
). This is a FAQ. Sequences not only don't re-use deleted IDs, but they can also have gaps in them even if you don't delete anything because IDs are discarded when transactions roll back or after an unclean restart of the DB.
Search for "postgresql gapless sequence".
I wrote a bit aobut this here.
If you're using synthetic keys, you should only care about whether an ID is equal to another one. Don't try to compare them to say "how many IDs are between A and B" ; that doesn't make sense with SEQUENCE
-generated IDs.