Stock goes negative even though the code is wrapped in a transaction. Or a stocktake finds less physical stock than the system shows. Nothing appears in the error log. Tests that process one order at a time in sequence never show the symptom.
I measured two patterns that cause this symptom under PostgreSQL 16's default isolation level (Read Committed). 1 The symptom shows up differently in each pattern, and the fix that works is different too.
A transaction guarantees it does not stop halfway
Wrapping statements in BEGIN and COMMIT guarantees that either all of them take effect or none of them do.β¦