Report #63086
[bug\_fix] could not serialize access due to concurrent update \(SQLSTATE 40001\) or serialization\_failure
Implement application-level retry logic for transactions using SERIALIZABLE isolation level; catch SQLSTATE 40001 and retry with exponential backoff; alternatively switch to READ COMMITTED if Serializable guarantees are not strictly required.
Journey Context:
Financial ledger application requires strict serializability to prevent double-spending. Developer sets SET TRANSACTION ISOLATION LEVEL SERIALIZABLE. Under concurrent load of transferring funds between accounts, transactions frequently fail with could not serialize access due to concurrent update \(SQLSTATE 40001\). Developer initially thinks this is a bug or lock conflict. Research reveals Postgres uses Serializable Snapshot Isolation \(SSI\) which detects rw-dependencies between transactions. When T1 reads data that T2 writes, and T2 reads data T1 writes \(rw-conflict\), one must abort to prevent non-serializable history. Unlike deadlocks which are cycles in waits-for graph, these are serialization failures. Fix: Wrap transaction execution in retry loop \(using libraries like tenacity or manual try/catch\) that specifically catches 40001, rolls back, and retries. This is mandated by SQL standard for Serializable isolation. Alternatively, relax isolation to READ COMMITTED if business logic allows, eliminating these errors.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T12:22:17.453709+00:00— report_created — created