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 deployGotchas
- 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
How to Add an Index Without Locking the Table (Postgres)Add a Postgres index without blocking writes using CREATE INDEX CONCURRENTLY, which must run outside a transa…
How to Add a NOT NULL Constraint Without a Long LockAdd a NOT NULL constraint on a large table without a full-table lock by adding a validated CHECK constraint N…