Set statement timeout per session and role in PostgreSQL
Set the limits once per role and every new session for that role enforces them, with no application changes.
The task
PostgreSQL's timeout parameters bound runaway work at three levels: statement_timeout caps each statement, lock_timeout caps how long anything waits for a lock before erroring, and idle_in_transaction_session_timeout ends sessions that open a transaction and go quiet. PostgreSQL 17 added a fourth, transaction_timeout, for the whole transaction. A SET applies at once to the current session only; ALTER ROLE ... SET makes the value the default for every new session of that role.
The snippet
Written for PostgreSQL.
SET statement_timeout = '5s';
SET lock_timeout = '3s';
SET idle_in_transaction_session_timeout = '60s';
Como funciona
SET applies to the current session only, which suits a reporting session or a migration script that needs its own guardrails. A statement that exceeds statement_timeout is cancelled with an error; inside an explicit transaction that leaves the transaction aborted until the client rolls back. idle_in_transaction_session_timeout terminates the whole connection. The per-role variant makes the policy stick: ALTER ROLE reporting SET statement_timeout = '30s' applies to every new reporting session. Migrations need lock_timeout most. An ALTER TABLE waiting for its ACCESS EXCLUSIVE lock makes every later query on that table queue behind it, so a lock_timeout of a few seconds makes the migration fail fast (and retry) instead of stalling all traffic on the table.
Variations
Per-role defaults that persist across reconnects
ALTER ROLE reporting SET statement_timeout = '30s';
ALTER ROLE app SET statement_timeout = '5s';
ALTER ROLE app SET idle_in_transaction_session_timeout = '60s';
ALTER ROLE etl SET statement_timeout = '2h';
Check the effective values for your session
SELECT name, setting, unit, source
FROM pg_settings
WHERE name IN ('statement_timeout', 'lock_timeout', 'idle_in_transaction_session_timeout')
ORDER BY name;
Questions that come up
Why did my timeout not seem to apply?
statement_timeout counts the whole statement, lock waits included, and lock_timeout separately caps each lock wait; whichever runs out first cancels the statement. Neither counts time between statements, which is idle_in_transaction_session_timeout's job. If a value seems ignored, check pg_settings.source. A SET overrides role and database defaults for the session, but a pooler that runs DISCARD ALL or RESET ALL between clients (PgBouncer's server_reset_query, for example) puts the role default back, and in PgBouncer's transaction mode a SET may land on a different server connection than your next query.
What is a sensible statement_timeout for an app role?
A margin above the slowest legitimate query that role runs, measured rather than guessed (pg_stat_statements max_exec_time is a good source). If the slowest normal request takes 2 seconds, 5 seconds leaves room; a 30-second limit on an interactive application role usually means a slow query is being tolerated rather than fixed. Batch and reporting roles get their own, longer limits.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.