PostgreSQL: canceling statement due to lock timeout
ERROR: canceling statement due to lock timeoutThe statement waited longer than lock_timeout to acquire a lock, so Postgres cancelled it. Something else holds the lock; find and clear the blocker, or run heavy DDL off-peak with a deliberate lock_timeout and retry.
What this error means
lock_timeout caps how long a statement waits for a lock before giving up. When a statement, often a migration or DDL, cannot get its lock in time because another transaction holds it, Postgres aborts with this error.
It is common when an ALTER TABLE waits behind a long-running transaction.
How to fix it
Another transaction holds the lock
Find the blocker and, if safe, terminate it.
SELECT pid, state, query
FROM pg_stat_activity
WHERE wait_event_type = 'Lock';
-- terminate a blocker:
SELECT pg_terminate_backend(<pid>);A migration is blocked
Set a deliberate lock_timeout for the migration so it fails fast instead of blocking traffic, then retry.
SET lock_timeout = '3s';Heavy DDL during peak hours
Run it off-peak, and use CREATE INDEX CONCURRENTLY to avoid long exclusive locks.
How to avoid it next time
- Set lock_timeout for migrations so they fail fast rather than block queries.
- Build indexes concurrently and keep transactions short.
Frequently asked questions
How is this different from statement_timeout?
statement_timeout limits total run time. lock_timeout limits only the time spent waiting for a lock. A statement can hit either.
How do I find what is blocking me?
Query pg_stat_activity for sessions with wait_event_type = 'Lock', or join pg_locks to see the holder and the waiter.
Stop struggling with SQL
Use FluentDB, the AI database client for macOS.
Related errors
Part of the PostgreSQL error reference. Last reviewed 2026-07-31.