Report #9946
[bug\_fix] ERROR: could not serialize access due to concurrent update \(SQLSTATE 40001\)
Catch SQLSTATE 40001 \(SerializationFailure\) in application code and automatically retry the entire transaction with exponential backoff. Alternatively, reduce the isolation level to READ COMMITTED if the business logic permits non-repeatable reads and phantom reads. Root cause: In REPEATABLE READ or SERIALIZABLE isolation, if another transaction commits a modification to a row this transaction has already read \(or attempts to modify\), the database aborts this transaction to maintain the illusion that it ran alone.
Journey Context:
You switch your Python accounting service to REPEATABLE READ isolation to prevent double-spending race conditions. Under load tests, you see intermittent \`40001 could not serialize access due to concurrent update\` errors. You check the PostgreSQL docs and learn that in REPEATABLE READ, any concurrent update to a read row causes a serialization failure that must be retried by the application. You implement a tenacity \`@retry\` decorator that catches \`psycopg2.errors.SerializationFailure\`, rolls back, and retries with exponential backoff up to 3 times. The errors are handled transparently and the wallet balances remain consistent.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:24:45.399581+00:00— report_created — created