PostgreSQL Sequences vs. Invoice Numbers: The Gap Myth
Accounting departments often insist that invoice IDs must be strictly ascending with no gaps, but that requirement is not actually true—at least not under German tax law, where this practice has been used without issue.
Invoice and order identifiers can be structured far more flexibly than a simple sequence. A valid format might look like SOMEPREFIX 2022-01/FB42-23AF, which combines a group prefix, a timeframe component scaled to the expected annual volume, and a fragment of a random UUID.
The only genuine constraint is uniqueness. The "no gaps" rule is a legacy heuristic—useful for manual invoice auditing, but unnecessary in a modern relational database. If you cannot trust your database to retain records without loss, gaps in a sequence are the least of your concerns.



