FluentDBFluentDB
PostgreSQL errors

PostgreSQL: canceling statement due to lock timeout

ERRORSQLSTATE 55P03lock_not_available
ERROR: canceling statement due to lock timeout

The 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

01

Another transaction holds the lock

Find the blocker and, if safe, terminate it.

sql
SELECT pid, state, query
FROM pg_stat_activity
WHERE wait_event_type = 'Lock';
-- terminate a blocker:
SELECT pg_terminate_backend(<pid>);
02

A migration is blocked

Set a deliberate lock_timeout for the migration so it fails fast instead of blocking traffic, then retry.

sql
SET lock_timeout = '3s';
03

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.