Report #16284
[bug\_fix] ERROR: could not serialize access due to concurrent update \(SQLSTATE 40001\)
Implement mandatory transaction retry logic with exponential backoff and jitter at the application layer. Root cause: When using REPEATABLE READ or SERIALIZABLE isolation, PostgreSQL uses optimistic concurrency control \(SSI\); if another transaction commits a modification to a row this transaction has read, the first committer wins, and subsequent transactions must retry.
Journey Context:
A fintech ledger system switches from READ COMMITTED to SERIALIZABLE to prevent phantom reads and double-spending. Immediately, integration tests start failing with 'could not serialize access'. Developers initially treat it as a bug, but profiling reveals two concurrent transfers updating the same balance row. Under SERIALIZABLE, Postgres detects the rw-conflict and aborts the second transaction. The team implements a Python decorator that catches 40001 errors, waits random\(100ms\)\+exponential backoff, and retries up to 5 times. After deployment, the errors vanish as retries eventually succeed on contention.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T02:18:24.148792+00:00— report_created — created