Report #91970
[bug\_fix] PostgreSQL serialization failure \(SQLSTATE 40001\) under REPEATABLE READ
Implement an application-level retry loop with exponential backoff to re-execute the transaction when catching error code 40001.
Journey Context:
A financial trading platform using SQLAlchemy with isolation\_level='REPEATABLE READ' began experiencing sporadic exceptions: sqlalchemy.exc.OperationalError: \(psycopg2.errors.SerializationFailure\) could not serialize access due to concurrent update. The code logic read account balances, calculated new positions, and updated rows. Under high concurrency, when two transactions simultaneously read the same rows and attempted to update them, PostgreSQL's Serializable Snapshot Isolation \(SSI\) detected a read-write conflict that could lead to serialization anomalies. Rather than allowing inconsistent state, PostgreSQL aborted one transaction with SQLSTATE 40001. Initial attempts to 'fix' the isolation level by lowering it to READ COMMITTED were rejected due to business requirements for consistent reads. The resolution involved wrapping the transaction execution in a retry decorator that caught SerializationFailure \(40001\), rolled back the session, waited an exponentially increasing random delay \(e.g., 10ms, 20ms, 40ms\), and retried up to 3 times. This works because the serialization conflict is a transient concurrency issue; re-executing the transaction with a fresh snapshot typically succeeds on the next attempt.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T12:57:45.249395+00:00— report_created — created