Skip to content
dbexplore

Safety

Timeouts as a safety net

For anyone who has watched one query hold a lock while the application piles up behind it.

Three settings stop three different failures, and statement_timeout never touches the session that is idle in transaction. Scope decides the rest.

Reference page, revised in place. Last updated .

Three settings, three different failures

They get grouped together and they are not interchangeable. Each one ends a distinct way of hurting a database, and setting the wrong one leaves the failure you actually have untouched.

statement_timeout caps how long a single statement may run. It is the one people reach for first, and it is the right answer to a runaway query consuming CPU and I/O. It is the wrong answer to a query that is not running at all because it is waiting for a lock, since a statement blocked on a lock is still inside its statement time and will be cancelled only when the whole limit expires, by which point everything behind it has been waiting just as long.

lock_timeout caps how long a statement will wait to acquire a lock before giving up. This is the setting that protects you from the classic outage shape: a migration takes an ACCESS EXCLUSIVE lock, waits behind a long-running read, and every subsequent query queues behind the migration’s own lock request. With a short lock_timeout the migration fails fast and harmlessly instead of forming the queue.

idle_in_transaction_session_timeout ends sessions that opened a transaction and stopped doing anything. Those are not slow queries. They are an application that forgot to commit, and while they sit there they hold whatever locks they took and pin the xmin horizon, which stops vacuum cleaning up anywhere in the database.

What is set now, and where it came from

pg_settings reports the current value and, usefully, the source, so you can tell a deliberate setting from a default:

SELECT name,
       setting,
       unit,
       source,
       boot_val,
       reset_val
  FROM pg_settings
 WHERE name IN ('statement_timeout',
                'lock_timeout',
                'idle_in_transaction_session_timeout',
                'idle_session_timeout',
                'deadlock_timeout')
 ORDER BY name;

A setting of 0 means disabled, which is the default for the first three. A source of default means nobody has made a decision here, which is the common case and usually not the intended one.

deadlock_timeout is in the list because it is frequently confused with the others and does something different again: it is how long a session waits before checking whether it is in a deadlock, not a limit on anything. Lowering it makes deadlock detection more eager and more expensive, and it does not cap any wait.

Scope is most of the decision

Set globally in postgresql.conf, statement_timeout applies to everything, including the nightly report that legitimately runs for forty minutes and the CREATE INDEX you are running by hand. That is how a safety net becomes an outage of its own.

The useful scopes are narrower. Per role is the usual answer, because the application’s role and the analyst’s role want different limits:

ALTER ROLE app_readwrite SET statement_timeout = '30s';
ALTER ROLE app_readwrite SET lock_timeout = '3s';
ALTER ROLE reporting     SET statement_timeout = '30min';

Per session is the right scope for a migration, set in the same transaction as the DDL so it cannot leak:

BEGIN;
SET LOCAL lock_timeout = '3s';
SET LOCAL statement_timeout = '60s';
COMMIT;

SET LOCAL reverts at the end of the transaction, which is what you want for a one-off. Plain SET persists for the session, and through a connection pooler in transaction mode that session is handed to somebody else afterwards, carrying your setting with it. That interaction is the subject of connection pooling and PgBouncer, and it is the reason SET LOCAL is worth the habit.

Finding who would have been caught

Before setting a limit, it is worth knowing what it would have killed. Current activity shows what is running and waiting right now:

SELECT pid,
       state,
       wait_event_type,
       wait_event,
       now() - xact_start  AS xact_age,
       now() - query_start AS query_age,
       left(query, 60)     AS query
  FROM pg_stat_activity
 WHERE backend_type = 'client backend'
   AND state <> 'idle'
 ORDER BY xact_start NULLS LAST;

Read xact_age and query_age as different things. A session whose query_age is small but whose xact_age is hours old is the idle-in-transaction case, and statement_timeout will never touch it however low you set it. The blocking relationships behind a wait are on lock contention and blocking trees.

Where to start

A timeout you are afraid to enable is not protecting anything. Start with lock_timeout on the roles that run migrations, because it has the smallest blast radius and prevents the worst outage shape. Add idle_in_transaction_session_timeout next, generously, since nothing legitimate holds an open transaction for an hour. Leave statement_timeout for last and set it per role, because it is the one most likely to cancel something that was supposed to take a long time.

Put every Postgres you run on autopilot.

We onboard teams in small batches. Tell us about your fleet and we will reach out when a seat opens. One email, no drip campaign.