I can not use a PostgreSQL sequence because invoices need to be gapless, and sequences are designed to not rollback, for performance reasons. In this case, the performance is secondary because the law does not care.