Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 25P03: Idle in Transaction Timeout

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 25P03 means a session was idle inside a transaction longer than the configured idle_in_transaction_session_timeout and the server terminated it. The fix is to raise or disable that timeout if long idle transactions are legitimate, and to fix the application or connection pool that leaves transactions open. You can confirm the setting with SHOW idle_in_transaction_session_timeout; and change it per session with SET idle_in_transaction_session_timeout = '5min'; or globally in postgresql.conf followed by a reload.

What You'll Need

  • Access to the PostgreSQL server as a superuser or a role with permission to change server settings.
  • A SQL client such as psql or pgAdmin.
  • Access to the application code or connection pool configuration that owns the failing session.
  • Knowledge of the server's configuration file location, typically postgresql.conf in the data directory.

Step-by-Step

  1. Confirm the error and the current timeout. Connect with psql and run SHOW idle_in_transaction_session_timeout;. A value of 0 means the timeout is disabled; any other value is the limit after which idle transactions are terminated. The error appears in the client log as FATAL: terminating connection due to idle-in-transaction timeout with SQLSTATE 25P03.
  2. Identify which sessions are being killed. Query SELECT pid, usename, application_name, state, now() - state_change AS idle_time, query FROM pg_stat_activity WHERE state = 'idle in transaction';. The application_name and query columns usually point to the application or ORM responsible. If the session is already gone, check the server log for the FATAL line and the statement that preceded it.
  3. Decide whether the timeout is correct. If the application opens a transaction and then waits on user input, a slow external API, or a long report, the idle period is expected and the timeout is too aggressive. If the application opens a transaction and forgets to commit or roll back, the timeout is doing its job and the application must be fixed.
  4. Change the timeout for a single session. Run SET idle_in_transaction_session_timeout = '10min'; in the session that needs more time. This affects only that connection and resets when the session ends. Use SET idle_in_transaction_session_timeout = 0; to disable it for that session only.
  5. Change the timeout for a role. Run ALTER ROLE app_user SET idle_in_transaction_session_timeout = '10min';. New sessions for that role pick up the value. This is useful when only one application needs a longer limit.
  6. Change the timeout globally. Edit postgresql.conf and set idle_in_transaction_session_timeout = '10min', then reload with SELECT pg_reload_conf(); or the equivalent service command. A reload is enough; a restart is not required. Labels and file locations can differ slightly by version, operating system and hosting provider.
  7. Fix the application or pool. Ensure every transaction is committed or rolled back as soon as its work is done. In connection pools, set a short statement timeout and a transaction timeout at the pool level so a leaked transaction is closed before the server has to kill it. For example, many pools expose a transactionTimeout or idleInTransactionTimeout option; check your pool's documentation for the exact name.
  8. Add monitoring. Schedule a query against pg_stat_activity that alerts when any session has been idle in transaction for more than a few minutes. Catching the session before the server terminates it gives you the query text and application name needed to fix the root cause.
  9. Verify the change. Open a new session, run BEGIN; SELECT 1; and wait longer than the old timeout. If the session survives past the new limit, the setting is in effect. Then run ROLLBACK; to clean up.

Common Mistakes

  • Disabling the timeout globally as a first response. That hides leaked transactions and allows them to hold locks and bloat the database indefinitely.
  • Changing postgresql.conf but forgetting to reload, so the old value stays active.
  • Confusing idle_in_transaction_session_timeout with idle_session_timeout. The first applies only inside an open transaction; the second applies to any idle connection.
  • Setting the timeout lower than the longest legitimate batch job, which then fails partway through and may leave partial work.
  • Assuming the client can retry safely. A terminated transaction is rolled back, so any work in it is lost; retries must start a new transaction.

Tips

  • Prefer per-role or per-database settings over global changes so one application's needs do not weaken protection for everyone else.
  • Pair the timeout with statement_timeout and lock_timeout so no single query or lock wait can hang a session indefinitely.
  • Use application_name in connection strings so pg_stat_activity identifies the source of idle transactions.
  • Keep transactions short. Move external calls, file uploads and user prompts outside the transaction boundary.
  • Review connection pool settings after any change; a pool that holds a transaction open while waiting for a free connection will trigger the timeout repeatedly.

Troubleshooting

If the error persists after changing the setting, confirm the change reached the server: run SHOW idle_in_transaction_session_timeout; in a new session and compare it with the file. If a role-level setting exists, it overrides the global value, so check SELECT rolname, rolconfig FROM pg_roles WHERE rolconfig IS NOT NULL;. If the session is killed almost immediately, the timeout may be set very low in a connection pool or in an ALTER DATABASE ... SET statement; check SELECT datname, datconfig FROM pg_database WHERE datconfig IS NOT NULL;. If the application still leaks transactions, enable log_min_duration_statement or log_statement temporarily to capture the statements that open transactions without closing them. If locks remain after the session is terminated, the rollback should release them; if not, investigate prepared transactions with SELECT * FROM pg_prepared_xacts; and resolve them according to your recovery plan.

Frequently Asked Questions

What does error 25P03 mean exactly?

It is the SQLSTATE for a session terminated because it stayed idle inside an open transaction longer than idle_in_transaction_session_timeout. The transaction is rolled back.

Does raising the timeout fix the problem?

Only if the idle period is legitimate. If the application forgets to commit, raising the timeout delays the failure and keeps locks held longer.

Can I disable the timeout?

Yes, set it to 0 globally, per role or per session. Disabling it removes protection against leaked transactions, so do it only when you have another way to detect them.

Is a reload enough, or do I need a restart?

A reload is enough for idle_in_transaction_session_timeout. Restarting is unnecessary and disrupts connections.

Why did only one application start failing?

The setting may be applied at role or database level, or that application may be the only one that leaves transactions open. Check pg_stat_activity and the role and database settings.

What happens to work in the killed transaction?

It is rolled back. The client receives the error and must start a new transaction to retry, so the application should handle the failure explicitly.