Skip to content
Latchkey

How to Avoid Long Locks in Migrations (lock_timeout)

A DDL statement that waits for a lock queues every query behind it; a short lock_timeout makes the migration fail fast instead of stalling production.

When DDL cannot get its lock immediately it waits, and every query needing that table queues behind it, which looks like an outage. Set a low lock_timeout (and a statement_timeout) so the statement gives up quickly and CI can retry at a quieter moment.

Set timeouts before the DDL

migration.sql
SET lock_timeout = '3s';
SET statement_timeout = '15s';
ALTER TABLE orders ADD COLUMN note text;

Set them for the session in CI

Terminal
PGOPTIONS="-c lock_timeout=3s -c statement_timeout=15s" \
  npx prisma migrate deploy

Gotchas

  • lock_timeout bounds the wait for a lock; statement_timeout bounds the run once acquired. Set both.
  • A failed timeout leaves the schema unchanged, so the migration is safe to retry.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →