Report #40762
[bug\_fix] could not serialize access due to concurrent update \(SQLSTATE 40001\)
Implement application-level retry logic with exponential backoff that catches the 40001 error and re-runs the transaction. Root cause: Under REPEATABLE READ or SERIALIZABLE isolation, Postgres detects that another transaction committed a write to data this transaction has read \(for REPEATABLE READ\) or detects a serialization anomaly \(for SERIALIZABLE\), and aborts the current transaction to maintain the isolation guarantee per SQL standard.
Journey Context:
You switch a financial transfer function to use REPEATABLE READ isolation to prevent non-repeatable reads during balance checks. Immediately, Sentry reports \`psycopg2.errors.SerializationFailure: could not serialize access due to concurrent update\`. You examine the logs and see Transaction A reads account 123, Transaction B updates account 123 and commits, then Transaction A tries to update account 123 and fails with SQLSTATE 40001. Unlike READ COMMITTED which would block or use the latest row, REPEATABLE READ honors the initial snapshot and aborts to prevent phantom reads. You implement a tenacity retry decorator that catches SerializationFailure, waits with exponential jitter, and retries the entire transfer function. The errors convert into brief latency spikes instead of user-facing failures, satisfying the isolation requirements.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T22:53:19.122405+00:00— report_created — created