Smart Systems for Home

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

PostgreSQL Error 25P04: Fix Transaction Timeout

· 6 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

Error 25P04 means a PostgreSQL transaction was cancelled because it exceeded the transaction_timeout limit set for the session, user, database or cluster. The fix is to find where that limit is coming from and either give the transaction more time or make it finish sooner. PostgreSQL added transaction_timeout in version 17; on older servers the same symptom usually comes from statement_timeout, idle_in_transaction_session_timeout or a client-side timeout, so check the exact message text before changing anything.

Nothing here needs a restart of the whole cluster if you only change a session or role setting. Changing postgresql.conf or ALTER SYSTEM does need a reload (or a restart for some parameters), so plan that for a quiet moment. You will need a role that can read pg_settings and, for permanent changes, superuser or a role with the right grants.

What You'll Need

  • Access to psql or another SQL client logged in as a role that can read pg_settings.
  • The exact error text and the statement that failed, from the application log or the PostgreSQL server log.
  • Permission to change role or database settings, or superuser for ALTER SYSTEM and postgresql.conf edits.
  • A maintenance window if the change requires a reload or restart.

Step-by-Step

  1. Confirm the error code and the timeout that fired. In psql run \errverbose right after the failure, or read the server log line. The message names the timeout, for example canceling statement due to transaction timeout or canceling statement due to statement timeout. That tells you which setting to look at. If the message mentions an idle transaction, the cause is different and the fix is to end idle transactions, not to raise a limit.
  2. Read the current values. Run SHOW transaction_timeout;, SHOW statement_timeout; and SHOW idle_in_transaction_session_timeout;. A value of 0 means disabled. Then find where the value is set: SELECT name, setting, source, sourcefile, sourceline FROM pg_settings WHERE name IN ('transaction_timeout','statement_timeout','idle_in_transaction_session_timeout');. The source column tells you whether it comes from the config file, ALTER SYSTEM, a database setting, a role setting or the session itself. Change it at the same level or a higher one, otherwise your edit will be overridden.
  3. Decide whether to raise the limit or shorten the transaction. If the transaction is a legitimate long batch job, raise the limit for that role or session only. If it is an ordinary web request, the timeout is doing its job: look for a missing index, a lock wait, a slow query or a transaction left open while the application waits on something else.
  4. Test the change in one session first. In psql run SET transaction_timeout = '5min'; (or another value that suits the job) and rerun the statement. SET only affects the current session and disappears when you disconnect, so it is safe to experiment. Use SET LOCAL inside a transaction if you want the change to end with the transaction.
  5. Make the change permanent at the narrowest level that works. For one role: ALTER ROLE app_user SET transaction_timeout = '5min';. For one database: ALTER DATABASE appdb SET transaction_timeout = '5min';. For the whole cluster: ALTER SYSTEM SET transaction_timeout = '5min'; followed by SELECT pg_reload_conf();. Role and database settings take effect on the next connection; cluster settings take effect after reload for sessions that have not overridden them.
  6. Fix the application side. If the client library has its own timeout (connection pool checkout, query timeout, HTTP gateway timeout), raise or align it. A server limit longer than the client limit just moves the failure to the client, and a client limit shorter than the server limit produces a confusing error that looks like 25P04 but is not. Check the pool configuration for a statement or transaction timeout option and set it consistently.
  7. Verify. Reconnect and run SHOW transaction_timeout; to confirm the new value is in effect for that role or database. Rerun the failing operation and check the server log for a clean completion. If the error persists, go back to step 2 and check the source column again, because a session-level SET or a connection pool initialisation string can override everything else.

Common Mistakes

  • Setting transaction_timeout on a PostgreSQL version older than 17. The parameter does not exist there; the error you are seeing is almost certainly statement_timeout or a client timeout.
  • Changing the value in postgresql.conf while a role or database setting overrides it. The source column in pg_settings shows which one wins.
  • Disabling the timeout entirely with 0 to make the error go away. That hides runaway transactions and long lock waits instead of fixing them.
  • Raising the server timeout without raising the connection pool or client timeout, so the failure simply reappears at a different layer.
  • Confusing transaction timeout with idle-in-transaction timeout. The first limits total transaction duration; the second kills sessions that sit idle inside an open transaction.
  • Forgetting that ALTER SYSTEM writes to postgresql.auto.conf and needs a reload. The change is not live until pg_reload_conf() runs or the server is reloaded.

Tips

  • Set the timeout per role for batch jobs rather than globally, so interactive queries keep a tight limit.
  • Log the statement that timed out. The server log already includes it when log_min_error_statement is at its default; use that to find the slow query instead of guessing.
  • Break very long transactions into smaller batches with commits in between. This reduces lock contention and makes each transaction short enough to fit any reasonable timeout.
  • Use EXPLAIN (ANALYZE, BUFFERS) on the slow statement to see whether the time goes to a sequential scan, a lock wait or a sort that spills to disk.
  • Keep server, pool and client timeouts in a deliberate order: client longest, then pool, then server, so the server reports the real problem first.

Troubleshooting

If the error still appears after you change the setting, confirm the session actually picked up the new value with SHOW transaction_timeout;. A connection pool that opens connections once and keeps them can hold old settings until it recycles; restart the pool or wait for connections to expire.

If SHOW returns the value you set but the transaction still fails, the cancellation may be coming from statement_timeout on a single statement inside the transaction. Check both values and the exact log message.

If the transaction is waiting on a lock, raising the timeout only makes it wait longer. Query pg_locks and pg_stat_activity to find the blocking session, and fix the cause: a forgotten open transaction, a missing index causing lock escalation, or two jobs updating the same rows in different orders.

If the change requires a restart and you cannot restart now, apply it per role or per database instead, which takes effect on new connections without a restart.

Frequently Asked Questions

Which PostgreSQL versions have error 25P04?

The transaction_timeout parameter and its error code were added in PostgreSQL 17. On earlier versions, look at statement_timeout and idle_in_transaction_session_timeout instead.

Does raising transaction_timeout affect other users?

Only if you change it at cluster or database level. A role-level or session-level setting affects just that role or session.

What is the difference between transaction_timeout and statement_timeout?

transaction_timeout limits the total duration of a transaction, including time spent waiting. statement_timeout limits each individual statement. A long transaction made of many fast statements can hit the first but not the second.

Can I disable the timeout completely?

Yes, by setting it to 0, but that removes a safeguard against runaway transactions and long lock waits. Prefer a generous limit over no limit.

Why does the error appear only in production?

Production usually has different role settings, a connection pool with its own timeouts, and larger data volumes that make queries slower. Compare pg_settings between environments before changing code.

Do I need to restart PostgreSQL after changing the setting?

No. Role and database settings apply to new connections, and ALTER SYSTEM plus pg_reload_conf() applies without a restart. Only a few parameters require a full restart, and transaction_timeout is not one of them.