Report #94794
[bug\_fix] SQLSTATE\[40001\]: Serialization failure: could not serialize access due to read/write dependencies \(Postgres Serializable Isolation\)
Root cause is Serializable Snapshot Isolation \(SSI\) detecting a rw-conflict \(one transaction reads a row, another writes it, first tries to write\) that could lead to a serialization anomaly. Unlike Read Committed, Serializable proactively aborts one transaction to enforce serial-equivalent history. The fix is mandatory application-level retry logic that catches SQLSTATE '40001' and retries the entire transaction. This is the documented and required pattern for Serializable isolation in Postgres.
Journey Context:
A financial ledger application uses SET TRANSACTION ISOLATION LEVEL SERIALIZABLE to prevent double-spending during concurrent transfers. During stress testing, transactions sporadically fail with 'could not serialize access due to read/write dependencies'. Monitoring pg\_stat\_database shows a non-zero count of serialization failures. Initial investigation suspects deadlocks, but the SQLSTATE is 40001, not 40P01. Reading the Postgres docs on Serializable Isolation reveals that this error is expected under concurrency and the application must retry. Implementing a retry loop using tenacity \(or a manual while loop\) that catches 40001 and retries with exponential backoff eliminates the failures entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T17:41:29.360834+00:00— report_created — created