Accounting systems
Invoices, ledgers and money arithmetic: correctness that has to hold for years, not just for the demo.
An invoice is a snapshot, not a calculation
Why recomputing invoice totals from current rules breaks, and how to freeze integer minor-unit amounts, rule versions and document numbers in PostgreSQL.
Snippets
Gapless invoice numbering in PostgreSQL without a sequence
Increment a counter row with UPDATE ... RETURNING inside the issuing transaction: the row lock orders issuers and a rollback gives the number back.
Make issued invoices immutable in PostgreSQL
A BEFORE UPDATE OR DELETE trigger catches mistakes, privileges enforce it, and corrections are linked credit notes with a derived adjusted total.
Store money as integer minor units and round tax per line or document
Do the arithmetic in bigint minor units with an explicit tie rule. Five 1.10 lines at 8% tax to 0.45 per line or 0.44 per document, so store the scope.