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:
- Issue
ROLLBACK(orROLLBACK TO SAVEPOINT) to recover the session. - Wrap risky statements in
SAVEPOINTso one failure does not poison the whole transaction. - Stop sending statements after the first error; inspect and handle it.
Reference: PostgreSQL error codes — Class 25 (Invalid Transaction State).
Thanks — noted. This helps keep the database accurate.