Report #75010
[bug\_fix] could not serialize access due to concurrent update \(SQLSTATE 40001\)
Implement an application-level retry loop with exponential backoff specifically for SQLSTATE 40001. Do not switch to a lower isolation level.
Journey Context:
Financial ledger service using SET TRANSACTION ISOLATION LEVEL SERIALIZABLE to prevent double-spending starts throwing 500s under load with 'could not serialize access'. Developers initially think it's a bug in Postgres. Research reveals this is Serializable Snapshot Isolation \(SSI\) working as designed: when Postgres detects a potential serialization anomaly \(rw-conflict\), it aborts one transaction to guarantee serializable equivalence. The fix is not to lower isolation to READ COMMITTED \(which would allow race conditions\), but to catch SQLSTATE 40001 in the application, rollback, and retry. Implementation: wrap the transaction in a loop with max 5 retries, sleep 2^attempt \* random\(0-10\)ms. After deployment, 99.9% of serialization conflicts resolve on first retry, maintaining strict serializability guarantees without data corruption.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T08:30:15.301682+00:00— report_created — created