Gapless number generation

One aspect of database sequences which a lot of people stumble over, is that they can have gaps. Either because a value obtained with nextval wasn’t used, or because rows were deleted.

For a surrogate key (“generated key”) this is not a problem, because those values only have to be unique and gaps are meaningless and can safely be ignored.

In some situations gapless number migh however be a (legal) requirement, e.g. for invoice numbers.