PostgreSQL Error 55P03 Lock Not Available: How to Fix It
In this article
PostgreSQL error 55P03 (lock_not_available) means a statement tried to acquire a lock and could not get it — most often because another session is holding a conflicting lock, or the lock request was set to fail instead of wait (for example via lock_timeout or NOWAIT). The fix is to find the blocking session, end the conflict or wait it out, and then adjust your locking strategy so it doesn't recur. This guide walks through the whole process.
What You'll Need
- Access to PostgreSQL via
psqlor another SQL client - Permission to query
pg_stat_activityandpg_locks(any user can see their own sessions; superusers or roles withpg_read_all_statssee all) - Permission to cancel or terminate backends (
pg_cancel_backendworks on your own sessions; ending others' sessions typically requires superuser or membership in the same role) - Basic familiarity with
ALTER TABLE,VACUUMand transactions
Step-by-Step
- Read the full error message. The SQLSTATE is 55P03 and the message is usually
lock not available, sometimes with detail likeCould not obtain lock on roworcanceling statement due to lock timeout(that variant uses SQLSTATE 55P03 as well when raised bylock_timeout; statement_timeout is 57014). Note which statement failed:ALTER TABLE,SELECT ... FOR UPDATE NOWAIT, andLOCK TABLEare the usual culprits. - Identify the blocking session. Run:
Look for an old transaction inSELECT pid, state, wait_event_type, wait_event, now() - xact_start AS xact_age, query FROM pg_stat_activity WHERE datname = current_database() ORDER BY xact_start;idle in transactionstate — that is the classic cause. A query waiting on a lock showswait_event_type = 'Lock'. - Confirm what it holds. Join
pg_locksto see which locks the suspect holds:
Lock modes likeSELECT l.pid, l.locktype, l.mode, l.relation::regclass FROM pg_locks l WHERE l.pid = <blocking pid>;AccessExclusiveLockon a table block everything else. - End or resolve the blocker. If the blocking session is an abandoned transaction you own, roll it back in that session, or cancel it:
If it does not respond, terminate it (this aborts its transaction — be sure nobody is relying on it):SELECT pg_cancel_backend(<pid>);
If the blocker is legitimate long-running work (aSELECT pg_terminate_backend(<pid>);VACUUM, a batch job), simply wait for it to finish instead. - Retry your statement. Once the conflicting lock is gone, re-run the failed command. For schema changes like
ALTER TABLE, run them when the table is quiet — many tools retryALTER TABLE ... ADD COLUMNin a loop until it succeeds. - Adjust wait behavior if useful. Instead of failing immediately:
- Drop
NOWAITfromSELECT ... FOR UPDATE NOWAITso it waits, or useSKIP LOCKEDto skip contended rows. - Set a bounded wait instead of an instant failure:
SET lock_timeout = '5s';(session-level, reverts on reconnect). - Alternatively keep
lock_timeoutlow for DDL so it fails fast rather than queueing behind a long transaction and blocking all subsequent readers.
- Drop
- Prevent recurrence. Keep transactions short — commit promptly, never leave a session
idle in transaction. Setidle_in_transaction_session_timeout(e.g.SET GLOBAL idle_in_transaction_session_timeout = '60s'inpostgresql.confor per role withALTER ROLE ... SET) so abandoned transactions get killed automatically. In application code, always close transactions in afinallyblock.
Common Mistakes
- Killing the wrong backend — always confirm the PID in
pg_stat_activitybefore terminating;pg_terminate_backendon a healthy replication or application session causes real damage. - Terminating when
pg_cancel_backendwould do — cancel is gentler and usually enough. - Confusing 55P03 with a deadlock — deadlocks are SQLSTATE 40P01 and resolve themselves; 55P03 does not.
- Ignoring
idle in transactionsessions, which are the most common root cause of this error in production. - Running
ALTER TABLEduring peak traffic with nolock_timeout, which can queue behind one long query and stall every later request to that table.
Tips
- The
log_lock_waitssetting logs any wait longer thandeadlock_timeout, giving you a paper trail of contention. - Some
ALTER TABLEforms take only a lightAccessUpdateExclusiveLock— for exampleALTER TABLE ... VALIDATE CONSTRAINTafter adding a constraintNOT VALID— so restructuring your migration can avoid the heavy lock entirely. - Queue-based tools and migration frameworks often handle
lock_timeoutplus retry for you; check yours before hand-rolling it. - Schedule
VACUUM FULLand otherAccessExclusiveLockoperations for maintenance windows.
Troubleshooting
- No obvious blocker in
pg_stat_activity: the conflict may have already cleared. Checkpg_locksfor granted locks and the server log for context; simply retry. - You can't see other sessions: non-privileged users see limited rows in
pg_stat_activity. Ask a superuser or a role withpg_read_all_statsto run the queries. pg_cancel_backendhas no effect: the session may be stuck; escalate topg_terminate_backend, and if even that fails the backend may be unresponsive at the OS level — contact your DBA before doing anything more aggressive.- Blocker keeps reappearing: an application is leaking transactions (opening one and never committing). Fix the connection-pool or ORM configuration; killing sessions only treats the symptom.
- Error persists only during migrations: reduce
lock_timeout, add retries, and consider adding columns/constraints with default-bypassing,NOT VALIDforms to avoid long exclusive locks.
Frequently Asked Questions
What does SQLSTATE 55P03 mean?
It is PostgreSQL's lock_not_available code: a statement requested a lock with instructions not to wait (or a timeout expired) and the lock was held by another session.
Is 55P03 a deadlock?
No. Deadlocks are SQLSTATE 40P01 and are detected and resolved by PostgreSQL automatically. 55P03 requires you to clear the conflicting lock or retry.
Will the error resolve itself?
Only if the blocking transaction finishes or rolls back on its own. An idle in transaction session left open will hold its locks indefinitely until it is ended or an idle-in-transaction timeout fires.
What's the difference between lock_timeout and statement_timeout?
lock_timeout limits how long a statement waits to acquire a lock (raising 55P03); statement_timeout limits total statement run time (raising 57014, query_canceled).
How do I see lock waits without extra tooling?
Query pg_stat_activity for rows with wait_event_type = 'Lock', and enable log_lock_waits to have long waits written to the server log.