Report #74836
[bug\_fix] Postgres Serializable Isolation Retry Required
Catch SQLSTATE 40001 \(serialization\_failure\) in application code and retry the entire transaction from the beginning. This is expected behavior, not a bug. Alternatively, downgrade to Read Committed isolation and use explicit row locking \(SELECT FOR UPDATE\) if Serializable semantics are not strictly required. Root cause: Serializable Snapshot Isolation \(SSI\) detects a rw-dependency cycle between concurrent transactions that would violate serializability.
Journey Context:
A financial transfer service using SQLAlchemy with \`isolation\_level='SERIALIZABLE'\` intermittently throws 'could not serialize access due to read/write dependencies'. This happens only under load testing when two users simultaneously transfer money between the same two accounts \(A->B and B->A\). The SSI algorithm detects that T1 reads A then writes B, while T2 reads B then writes A, creating a dangerous structure. Postgres aborts one transaction to prevent non-serializable interleaving. The developer initially thinks it's a deadlock, but the error code is 40001, not 40P01. The fix requires wrapping the transfer logic in a retry loop that catches 40001 and reruns the transaction, which then succeeds on the second attempt.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T08:12:33.927498+00:00— report_created — created