Symptoms
The transaction was rolled back due to a serialization conflict.
- The error is written to the server log and returned to the client carrying
SQLSTATE 40001. - Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
- PL/pgSQL can trap it by name:
EXCEPTION WHEN serialization_failure THEN.
Environment
Severity: ERROR | PostgreSQL versions: 12, 13, 14, 15, 16, 17
Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).
Root Cause
Under SERIALIZABLE or REPEATABLE READ, concurrent transactions could not be ordered consistently, so one was aborted to preserve correctness.
Common causes:
- Write skew or read/write dependency cycles under serializable isolation.
- Concurrent updates to overlapping data under repeatable read.
Diagnostic Queries
Recovery
Steps to resolve 40001:
- Retry the entire transaction — this is expected and safe; use exponential backoff with a retry cap.
- Keep transactions short to shrink the conflict window.
- Use serializable only where you need it;
READ COMMITTEDavoids most of these.
Reference: PostgreSQL error codes — Class 40 (Transaction Rollback).
Thanks — noted. This helps keep the database accurate.