Report #6746
[bug\_fix] ERROR: could not serialize access due to read/write dependencies among transactions \(40001\)
Implement automatic retry logic in the application that catches the 40001 SQLSTATE error code, rolls back the transaction, and retries with exponential backoff up to a maximum limit. Ensure transaction logic is idempotent.
Journey Context:
You switch your Django app to use SERIALIZABLE isolation to prevent race conditions in financial transfers. Suddenly logs show frequent django.db.utils.OperationalError: could not serialize access due to read/write dependencies. You check Postgres logs and see the 40001 error code. You initially think it's a deadlock, but the error message mentions serialization. You research and learn that unlike READ COMMITTED, SERIALIZABLE uses predicate locks to detect rw-conflicts. When two transactions read the same data and one writes, the first to commit wins, the other must abort. You realize you need to handle this by retrying. You implement a decorator using tenacity that catches psycopg2.errors.SerializationFailure \(SQLSTATE 40001\), waits 50ms \* \(2^attempt\), and retries up to 5 times. You ensure the transaction function is idempotent \(uses ON CONFLICT DO NOTHING for inserts\). The errors disappear from user view because the retry mechanism handles the serialization conflicts transparently, which is the expected pattern for SSI.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T00:48:47.796312+00:00— report_created — created