SQLSTATE 25P03 ERROR Class 25: Invalid Transaction State

idle_in_transaction_session_timeout Idle In Transaction Session Timeout — SQLSTATE 25P03

A session sat idle inside an open transaction too long and was terminated.

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

Symptoms

A session sat idle inside an open transaction too long and was terminated.

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

The backend exceeded idle_in_transaction_session_timeout while holding an open transaction, so the server closed it to release locks and prevent bloat.

Common causes:

  • The application opened a transaction then waited on external work or user input.
  • A connection leak left a BEGIN without COMMIT.
  • Long think-time between statements in one transaction.

Diagnostic Queries

Recovery

Steps to resolve 25P03:

  1. Commit or roll back promptly; never hold a transaction open across user or network waits.
  2. Find offenders: SELECT pid, state, query, xact_start FROM pg_stat_activity WHERE state = 'idle in transaction';.
  3. Keep idle_in_transaction_session_timeout set — it protects against bloat and lock retention.
  4. Use shorter transactions and have the pool reset state on connection return.

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

Was this helpful?