skip to content

With strong_migrations in a Rails app, where do the 10-second lock timeout and 1-hour statement timeout come from, and why does a migration need them?

level: middleimportance: should knowfreq 28%

answer

  1. the gem's own default is nil
  2. generated initializer sets both
  3. waiting DDL queues every later query
  4. ActiveRecord::LockWaitTimeout on PostgreSQL
  5. PgBouncer transaction mode: set on the role

basics

~20 s

They come from config/initializers/strong_migrations.rb, which bin/rails generate strong_migrations:install writes; the gem itself sets no timeout. The short lock timeout stops a waiting ALTER from queueing all traffic behind it, and the long statement timeout lets index builds finish.

solid answer

~50 s

strong_migrations leaves `StrongMigrations.lock_timeout` and `statement_timeout` at `nil`; the 10 seconds and 1 hour are lines the install generator writes into `config/initializers/strong_migrations.rb`. When a migration makes its first schema call, the gem runs `SET lock_timeout` and `SET statement_timeout` on that connection (on PostgreSQL; MySQL gets `lock_wait_timeout` and `max_execution_time`). The lock timeout matters because a DDL statement waiting for its lock behind a long transaction makes every later query on that table wait behind it; with 10 seconds, the migration fails with `ActiveRecord::LockWaitTimeout` instead, and you rerun it. The long statement timeout exists so the app can keep a short one in `database.yml` while concurrent index builds still finish. On PostgreSQL, outside development and test, the gem warns when no lock timeout, or one over 10 seconds, is set. Behind PgBouncer in transaction mode, set both on the database role instead.

code

ruby · 6 lines
ruby
# config/initializers/strong_migrations.rb
StrongMigrations.start_after = 20261006120000
StrongMigrations.lock_timeout = 10.seconds
StrongMigrations.statement_timeout = 1.hour
StrongMigrations.lock_timeout_retries = 3
StrongMigrations.lock_timeout_retry_delay = 10.seconds

go deeper

for a junior

Know that the strong_migrations install generator writes an initializer with a 10-second lock timeout and a 1-hour statement timeout.

for a middle

Explain why each exists: a waiting ALTER queues all later queries, and long index builds must outlive the app's short statement timeout.

for a senior

Cover failure handling: ActiveRecord::LockWaitTimeout, reruns and retries, finding the blocking session, and role-level timeouts behind PgBouncer.

for a principal

Set timeout policy across app and migrations, including separate migration users and when to accept retries versus scheduling DDL off-peak.

## Two timeouts, two jobs A migration on a live table has two failure modes that timeouts address: - **Waiting too long for a lock.** An `ALTER TABLE` on `events` needs an exclusive lock, even if only for a moment. If a long report query or an idle-in-transaction connection is holding a conflicting lock, the `ALTER` waits, and on PostgreSQL every later query that needs the table queues **behind the waiting ALTER**. A one-second change becomes a multi-minute outage without the change ever running. A **lock timeout** makes the waiting statement give up instead. - **Being killed for running long.** A concurrent index build on 50 million rows can legitimately take many minutes. If the app sets a short **statement timeout** for web requests, the migration must not inherit it. ## Where the values come from `bin/rails generate strong_migrations:install` writes `config/initializers/strong_migrations.rb` with: ```ruby StrongMigrations.lock_timeout = 10.seconds StrongMigrations.statement_timeout = 1.hour StrongMigrations.auto_analyze = true ``` The gem's own defaults are **`nil` for both timeouts**. An app that added the gem but never ran the generator, or deleted those lines, migrates with whatever the database user has, which is often no lock timeout at all. When a migration makes its first schema call, the gem sets the timeouts on the migration's connection: | Adapter | lock timeout | statement timeout | |---|---|---| | PostgreSQL | `SET lock_timeout` | `SET statement_timeout` | | MySQL | `SET lock_wait_timeout` | `SET max_execution_time` | ## What happens when the lock timeout fires On PostgreSQL, Active Record translates the lock-not-available error into **`ActiveRecord::LockWaitTimeout`**, a subclass of `ActiveRecord::StatementInvalid`. A statement timeout becomes `ActiveRecord::QueryCanceled`. With a transactional migration the whole thing rolls back, nothing is recorded in `schema_migrations`, and you rerun `bin/rails db:migrate` once the blocking session is gone. The gem can also retry for you: 1. `StrongMigrations.lock_timeout_retries = 3` turns retries on (default `0`; the feature is marked experimental). 2. A lock timeout outside a transaction retries that statement. 3. A lock timeout inside the migration's transaction retries the whole migration (PostgreSQL only). 4. `StrongMigrations.lock_timeout_retry_delay` sets the pause between tries, 10 seconds by default. ## Safety nets and caveats - **The warning.** Outside development and test, `StrongMigrations.lock_timeout_limit` is 10 seconds. On PostgreSQL the gem reads the session's `lock_timeout` and prints `[strong_migrations] DANGER: No lock timeout set` if it is zero, or a warning if it is longer than the limit. It warns; it does not stop the migration. - **PgBouncer in transaction mode.** A `SET` on one server connection does not follow the migration across pooled connections, so the generator's comment says to delete those lines and set the timeouts on the database user, for example `ALTER ROLE migrator SET lock_timeout = '10s'`. - **App timeouts.** The gem recommends separate, shorter timeouts for the app in `config/database.yml`, such as `variables: { statement_timeout: 15s, lock_timeout: 10s }` on PostgreSQL. The long migration statement timeout is what makes that safe. - **A separate migration user** with `ALTER` rights keeps the app's own user from being able to change the schema at all. ## Reading a failed run A lock timeout looks like any other failed migration: `bin/rails db:migrate` aborts, prints the error, and the version is not recorded, so the next run starts the same migration again. With a transactional migration nothing is left behind. With `disable_ddl_transaction!`, statements that already committed stay, which is why such migrations should hold a single statement, and why a concurrent index build that timed out may leave an invalid index for `remove_invalid_indexes` to clean up. On PostgreSQL 17 and later the gem can also set `StrongMigrations.transaction_timeout`, which caps how long the migration's whole transaction may stay open. ## Choosing values Ten seconds is a compromise: long enough to get through ordinary traffic, short enough that a queue behind the waiting `ALTER` does not exhaust Puma threads and the connection pool. Teams with very hot tables often go lower and rely on retries. The statement timeout should exceed your slowest expected index build; if a build hits it, raise it for that one migration rather than globally.

  • A migration keeps failing with ActiveRecord::LockWaitTimeout. What do you look for?
    Whatever holds a conflicting lock on the table for longer than the timeout: a long report query, a job running one huge transaction, or a connection left idle in a transaction. Find and end it, or schedule the migration away from it; retries help with brief contention, not with a session that never lets go.
  • Why does the gem recommend a long statement timeout for migrations?
    So the app can run with a short statement timeout in `database.yml` to stop runaway queries, while migrations that legitimately take minutes, such as concurrent index builds or constraint validation, still finish. The migration's `SET statement_timeout` overrides the app setting for that connection only.

  • strong_migrations sets a 10-second lock timeout even without an initializer
  • a lock timeout makes the ALTER itself run faster
  • a waiting ALTER only delays itself, not other queries
  • SET lock_timeout works the same through PgBouncer in transaction mode
  • the gem refuses to migrate when no lock timeout is set