Postgres error 40001: serialization failure
could not serialize access due to concurrent update
A serialization failure: under REPEATABLE READ or SERIALIZABLE isolation, this transaction conflicted with another one and was rolled back so results stay consistent.
Common causes
- Two transactions read and then wrote overlapping data under a strict isolation level.
- Hot rows (counters, balances) updated by many transactions at once.
How to fix it
- Retry the whole transaction. This error is designed to be retried; wrap the transaction in a retry loop with a small random backoff.
- Keep transactions short and touch hot rows last.
- If you do not need strict isolation for that work, use the default READ COMMITTED.
Through a REST API
PostgREST, the REST layer behind supabase-js and Scribase, answers HTTP 500 for this error and returns the SQLSTATE in the code field of the JSON error body.
Questions
What does Postgres error 40001 mean?
40001 is serialization_failure in class 40 (Transaction Rollback). A serialization failure: under REPEATABLE READ or SERIALIZABLE isolation, this transaction conflicted with another one and was rolled back so results stay consistent.
How do I fix 40001?
Retry the whole transaction. This error is designed to be retried; wrap the transaction in a retry loop with a small random backoff. Keep transactions short and touch hot rows last. If you do not need strict isolation for that work, use the default READ COMMITTED.
What HTTP status does a REST API return for 40001?
PostgREST, the REST layer behind supabase-js, answers 500.
Other class 40 errors
- 40P01 deadlock detected