PostgreSQL Field Guide

SQLSTATE error fieldbook

Diagnose constraints, transactions, privilege, resources, and connections with stable five-character codes

Applications branch on SQLSTATE, not error text that can vary by version and locale.

Frequent states

SQLSTATENameCommon meaningSafe action
23505unique_violationUnique key conflictReturn conflict or use explicit ON CONFLICT semantics
23503foreign_key_violationTarget absent or still referencedFix operation order; do not disable the constraint
23502not_null_violationRequired column absentFix input or migration order
23514check_violationCHECK rejected the rowExplain boundary and correct the value
22P02invalid_text_representationType conversion failedValidate at the boundary and bind the right type
40001serialization_failureIsolation guarantee cannot be maintainedRoll back and retry the entire transaction
40P01deadlock_detectedWait cycleRoll back whole transaction; normalize lock order
55P03lock_not_availableNOWAIT or lock timeoutRetry later or return conflict
57014query_canceledStatement timeout or cancellationDistinguish cancel from timeout; optimize or narrow
25P02in_failed_sql_transactionEarlier statement failedROLLBACK; do not continue business SQL
42501insufficient_privilegeAction/object not grantedFix grant/owner; do not jump to superuser
42P01undefined_tableMissing table or wrong search pathCheck database/schema/migration version
42703undefined_columnColumn absentCheck schema contract and deploy version
53300too_many_connectionsConnection slots exhaustedInspect pools, leaks, and reserved admin access
57P03cannot_connect_nowStarting, recovering, or shutting downBounded backoff and instance inspection
08006connection_failureConnection failedDetermine unknown commit state before safe retry

After a transaction error

An error inside a transaction normally leaves it aborted:

ERROR: current transaction is aborted...
SQLSTATE: 25P02

Issue ROLLBACK, or roll back to a savepoint created before the failure. More statements do not repair it automatically.

Retry classes

  • Retry the whole transaction: 40001, 40P01; bounded attempts, exponential backoff, and jitter.
  • Possibly transient: 55P03, 57P03, selected 08***; verify idempotency and unknown commit state.
  • Input/model errors—do not blind retry: 22***, 23***, 42***, 42501.
  • Resource conditions: 53300, disk full, memory pressure; retries amplify the incident. Shed load and repair capacity.

Diagnostic context

Record SQLSTATE, constraint/table/column fields, database and schema, application and migration versions, transaction/request id, parameter types with sensitive values redacted, and known commit state. Drivers usually expose structured fields; use them directly.

AI tool response

{
  "ok": false,
  "sqlstate": "23505",
  "category": "constraint",
  "retryable": false,
  "constraint": "customers_email_unique",
  "message_safe": "A customer with this email already exists"
}

Do not return raw database errors to end users without filtering. They may reveal object names, paths, or data fragments.

Last updated on

On this page