SQLSTATE 40001 ERROR Class 40: Transaction Rollback

serialization_failure Serialization Failure — SQLSTATE 40001

The transaction was rolled back due to a serialization conflict.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

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:

  1. Retry the entire transaction — this is expected and safe; use exponential backoff with a retry cap.
  2. Keep transactions short to shrink the conflict window.
  3. Use serializable only where you need it; READ COMMITTED avoids most of these.

Reference: PostgreSQL error codes — Class 40 (Transaction Rollback).

Was this helpful?