Why every Rails app needs timeouts#
Postgres has no time limit on anything by default. A query can run for an hour, a statement can wait for a lock forever, and a transaction left open by a crashed request can hold its locks until someone kills it. In a Rails app, each of those holds a pool connection the whole time. A few stuck queries are enough to empty the pool, and then requests that never touch the slow table start failing with ActiveRecord::ConnectionTimeoutError. Rails connection pool exhaustion walks through that failure; timeouts are how you stop one slow thing from becoming every request’s problem.
This builder sets four timeouts for three kinds of database session, web requests, background jobs and migrations, because each needs different limits. A web query that runs for a minute is a bug. A job query that runs for a minute may be normal. A migration that waits a minute for a lock is an outage.
How it works#
The four settings, as the PostgreSQL 17 documentation defines them:
statement_timeoutaborts a statement that runs longer than the limit, measured from when it reaches the server.lock_timeoutaborts a statement that waits longer than the limit to acquire a lock, counted for each lock separately. The docs note that setting it at or above a non-zerostatement_timeoutis pointless, because the statement timeout always fires first.idle_in_transaction_session_timeoutends a session that sits idle inside an open transaction. Besides freeing locks, the docs point out that an open transaction stops vacuum from removing recently dead rows.transaction_timeoutends a session whose transaction runs longer than the limit. It exists only from PostgreSQL 17. If it is not longer than the statement or idle timeout, Postgres ignores the longer one.
A value without a unit is milliseconds, and 0 turns a timeout off. The first three end differently from the last two: statement_timeout and lock_timeout cancel one statement and leave the connection usable, while the idle and transaction timeouts close the session.
Enter your request deadline and the p99 time of your slowest normal web and job queries. The tool suggests a value for each setting from a stated rule and shows the rule beside it. The suggestions update as you type, and Use suggested values copies them into the fields. Edit any value; the checks run on what you will actually ship. Nothing you enter leaves the page.
The rules are deliberately simple. Web statement_timeout is about half the request deadline, so one query cannot use the whole request, as long as that is at least three times your p99. Each lock_timeout is about a quarter of its statement_timeout. The web idle timeout equals the request deadline. Migrations get no statement_timeout, since index builds can legitimately run long, and a lock_timeout no longer than the web one, because while a migration waits for its lock, web queries on that table queue behind it.
Worked example#
The example profile: PostgreSQL 17, a 15 second request deadline, a 250 ms p99 for web queries and a 2 second p99 for job queries. The builder suggests:
| Session | statement_timeout | lock_timeout | idle_in_transaction | transaction_timeout |
|---|---|---|---|---|
| Web requests | 5s | 1s | 15s | 30s |
| Background jobs | 10s | 2s | 1min | 0 (off) |
| Migrations | 0 (off) | 1s | not set | 0 (off) |
The web statement_timeout is 7.5 seconds, half of 15, rounded down to 5 seconds, which is still 20 times the 250 ms p99. The job value is three times 2 seconds, rounded up to 10. The verdict is Consistent: no setting cancels another out.
Now change the web lock_timeout to 5s. It equals the statement_timeout, so it can never fire, and the verdict becomes Fix. Put it back and set the web statement_timeout to 200ms instead. The checks now report that it is below your p99, so at least one normal query in a hundred would be cancelled, and that the 1s lock_timeout is no longer below it.
The Rails angle#
Rails passes anything under variables: in database.yml to Postgres as SET SESSION each time it opens a connection. That holds in the PostgreSQL adapter for Rails 7.1, 8.0 and 8.1. The generated file reads each value from an environment variable, so Puma, the job workers and the migration task can share one database.yml and still get their own limits.
Database roles are the second option. ALTER ROLE … SET makes a value the session default for that role at login, which also covers psql sessions and other clients that never read database.yml. Rails sets its variables after login, so where both exist, the database.yml value wins for Rails connections.
For migrations, the timeout belongs in the migration itself. In a normal migration, SET LOCAL lock_timeout lasts until the migration’s transaction ends. A migration with disable_ddl_transaction!, which every algorithm: :concurrently index needs, has no transaction, and Postgres ignores SET LOCAL there with a warning. The generated code uses SET instead and restores the old value in ensure. When a migration hits its lock_timeout, Rails raises ActiveRecord::LockWaitTimeout; retry it at a quieter moment rather than raising the limit. The Rails safe migration checker shows which statements in a migration need a lock at all, and the lock mode lookup shows what each lock blocks. If timeouts start surfacing as deadlocks instead, fixing Rails deadlocks covers the next step.
Limitations and assumptions#
- Rules, not measurements. The suggestions follow the rules above from the numbers you type. They know nothing about your traffic, and the example profile is an example.
- One deadline per workload. A single web value applies to every endpoint. Reports and exports that run longer need their own role or a
SET LOCAL statement_timeoutinside that code path. - Connection poolers. A pooler that shares one server connection between clients can carry a session
SETfrom one client to the next. Check your pooler’s documentation, and prefer per-role defaults where session state is not kept. - Versions. The semantics are the same across PostgreSQL 13 to 17, except
transaction_timeout, which only 17 and later have; the builder leaves it out for earlier versions. The Rails behaviour was checked in the adapter source for 7.1.5, 8.0.2 and 8.1.0.
Frequently asked questions
What is the difference between statement_timeout and lock_timeout?
statement_timeout limits the whole statement, from the moment it reaches the server until it finishes. lock_timeout limits only the time a statement waits to acquire a lock, and it applies to each lock separately. A query blocked behind a migration hits lock_timeout; a slow query that never waits hits statement_timeout.
Why should lock_timeout be lower than statement_timeout?
If it is the same or higher, statement_timeout always fires first, so lock_timeout never does anything. The PostgreSQL documentation calls that setting rather pointless. Keep lock_timeout well below statement_timeout so lock waits fail fast and are reported as lock timeouts.
Should I set statement_timeout in postgresql.conf?
The PostgreSQL documentation says no, because it would affect every session, including maintenance, backups and migrations. Set it per role with ALTER ROLE, or per connection from Rails with the variables key in database.yml, so each workload gets its own value.
What does Rails raise when a timeout fires?
A statement cancelled by statement_timeout raises ActiveRecord::QueryCanceled, and a lock wait that hits lock_timeout raises ActiveRecord::LockWaitTimeout. The idle-in-transaction and transaction timeouts end the whole session, so the connection is closed and the next query on it fails with a connection error.
Can I use transaction_timeout?
Only on PostgreSQL 17 or later; earlier versions do not have it. It ends the session when a transaction, idle or not, runs longer than the limit. If it is not longer than statement_timeout or idle_in_transaction_session_timeout, Postgres ignores the longer of the two.
Why does SET LOCAL lock_timeout do nothing in my concurrent index migration?
A migration with disable_ddl_transaction! does not run inside a transaction, and SET LOCAL outside a transaction only produces a warning. Use a plain SET there and restore the previous value afterwards, as the generated migration does.
Are the suggested values recommendations?
They are starting points produced by simple rules from the numbers you enter, such as half the request deadline for web queries. They are not measured from your traffic. Check them against your slowest real queries before you ship them, and watch for new timeout errors after the change.