Symptoms
The server reports SQLSTATE 40000 (transaction_rollback), a condition in the Transaction Rollback class.
- The error is written to the server log and returned to the client carrying
SQLSTATE 40000. - 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 transaction_rollback 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
40000 belongs to Class 40 — Transaction Rollback. In this class, the transaction was rolled back due to a serialization or deadlock conflict.
The first two characters (40) identify the error class, so application code can match the whole class via 40000 when the specific code is not needed.
Diagnostic Queries
Recovery
Retry the whole transaction with exponential backoff (this is expected under concurrency), keep transactions short, and reduce contention with consistent lock ordering and indexes on foreign keys.
Reference: PostgreSQL error codes — Class 40 (Transaction Rollback).
Thanks — noted. This helps keep the database accurate.