SQLSTATE 25P02 ERROR Class 25: Invalid Transaction State

in_failed_sql_transaction In Failed Sql Transaction — SQLSTATE 25P02

The transaction is aborted; all further commands are ignored until it ends.

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

Symptoms

The transaction is aborted; all further commands are ignored until it ends.

  • The error is written to the server log and returned to the client carrying SQLSTATE 25P02.
  • 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 in_failed_sql_transaction 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

An earlier statement in the transaction failed, so the server rejects everything (message: current transaction is aborted, commands ignored until end of transaction block) until you roll back.

Common causes:

  • Continuing to send queries after an error inside a transaction.
  • Not handling the first failure.

Diagnostic Queries

Recovery

Steps to resolve 25P02:

  1. Issue ROLLBACK (or ROLLBACK TO SAVEPOINT) to recover the session.
  2. Wrap risky statements in SAVEPOINT so one failure does not poison the whole transaction.
  3. Stop sending statements after the first error; inspect and handle it.

Reference: PostgreSQL error codes — Class 25 (Invalid Transaction State).

Was this helpful?