Report #17589
[bug\_fix] could not serialize access due to concurrent update \(SQLSTATE 40001\)
Implement application-level retry logic with exponential backoff specifically for SQLSTATE 40001 \(serialization\_failure\). This error is expected behavior under SERIALIZABLE or REPEATABLE READ isolation when the database detects a rw-conflict that could break serializability; the transaction must be retried from the beginning.
Journey Context:
A Python application using SQLAlchemy with isolation\_level='SERIALIZABLE' for financial ledger entries starts throwing 'could not serialize access due to concurrent update' errors under load. The developer initially treats this as a deadlock and expects Postgres to handle it, but the error persists and doesn't trigger automatic rollback. Investigating, they learn that under SERIALIZABLE isolation, Postgres uses SSI \(Serializable Snapshot Isolation\) to detect anomalies. When two concurrent transactions read a row and then one updates it, creating a rw-dependency cycle, Postgres aborts one transaction with this specific error to prevent serialization anomalies. Unlike deadlocks, this requires the application to explicitly catch the error and retry. The developer implements a decorator that catches SQLAlchemy's IntegrityError and OperationalError checking for error code 40001, then retries the entire function with exponential backoff up to 3 attempts. After implementation, the errors are handled gracefully without data loss.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:48:51.892083+00:00— report_created — created