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:
- Commit or roll back promptly; never hold a transaction open across user or network waits.
- Find offenders:
SELECT pid, state, query, xact_start FROM pg_stat_activity WHERE state = 'idle in transaction';. - Keep
idle_in_transaction_session_timeoutset — it protects against bloat and lock retention. - Use shorter transactions and have the pool reset state on connection return.
Reference: PostgreSQL error codes — Class 25 (Invalid Transaction State).
Thanks — noted. This helps keep the database accurate.