Report #15349
[bug\_fix] ERROR: could not serialize access due to concurrent update \(SQLSTATE 40001\)
Catch SQLSTATE 40001 in the application and retry the entire transaction from the beginning. This is mandatory and expected behavior when using REPEATABLE READ or SERIALIZABLE isolation levels in PostgreSQL's SSI \(Serializable Snapshot Isolation\) implementation.
Journey Context:
Financial application uses \`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE\` for critical transfers to prevent write skew. Under load, it throws "could not serialize access". Developer initially treats this as a conflict bug and considers downgrading to READ COMMITTED. Research reveals that PostgreSQL's SSI intentionally throws 40001 when it detects a potential serialization anomaly. The correct pattern is a retry loop: BEGIN; work; COMMIT; if 40001 then rollback and retry. They implement a decorator that retries up to 3 times with exponential backoff, after which the transactions succeed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:49:59.045989+00:00— report_created — created