Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 55P03 Lock Not Available: How to Fix It

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 psql or another SQL client
  • Permission to query pg_stat_activity and pg_locks (any user can see their own sessions; superusers or roles with pg_read_all_stats see all)
  • Permission to cancel or terminate backends (pg_cancel_backend works on your own sessions; ending others' sessions typically requires superuser or membership in the same role)
  • Basic familiarity with ALTER TABLE, VACUUM and transactions

Step-by-Step

  1. Read the full error message. The SQLSTATE is 55P03 and the message is usually lock not available, sometimes with detail like Could not obtain lock on row or canceling statement due to lock timeout (that variant uses SQLSTATE 55P03 as well when raised by lock_timeout; statement_timeout is 57014). Note which statement failed: ALTER TABLE, SELECT ... FOR UPDATE NOWAIT, and LOCK TABLE are the usual culprits.
  2. Identify the blocking session. Run:
    SELECT 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;
    Look for an old transaction in idle in transaction state — that is the classic cause. A query waiting on a lock shows wait_event_type = 'Lock'.
  3. Confirm what it holds. Join pg_locks to see which locks the suspect holds:
    SELECT l.pid, l.locktype, l.mode, l.relation::regclass
    FROM pg_locks l
    WHERE l.pid = <blocking pid>;
    Lock modes like AccessExclusiveLock on a table block everything else.
  4. End or resolve the blocker. If the blocking session is an abandoned transaction you own, roll it back in that session, or cancel it:
    SELECT pg_cancel_backend(<pid>);
    If it does not respond, terminate it (this aborts its transaction — be sure nobody is relying on it):
    SELECT pg_terminate_backend(<pid>);
    If the blocker is legitimate long-running work (a VACUUM, a batch job), simply wait for it to finish instead.
  5. 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 retry ALTER TABLE ... ADD COLUMN in a loop until it succeeds.
  6. Adjust wait behavior if useful. Instead of failing immediately:
    • Drop NOWAIT from SELECT ... FOR UPDATE NOWAIT so it waits, or use SKIP LOCKED to skip contended rows.
    • Set a bounded wait instead of an instant failure: SET lock_timeout = '5s'; (session-level, reverts on reconnect).
    • Alternatively keep lock_timeout low for DDL so it fails fast rather than queueing behind a long transaction and blocking all subsequent readers.
  7. Prevent recurrence. Keep transactions short — commit promptly, never leave a session idle in transaction. Set idle_in_transaction_session_timeout (e.g. SET GLOBAL idle_in_transaction_session_timeout = '60s' in postgresql.conf or per role with ALTER ROLE ... SET) so abandoned transactions get killed automatically. In application code, always close transactions in a finally block.

Common Mistakes

  • Killing the wrong backend — always confirm the PID in pg_stat_activity before terminating; pg_terminate_backend on a healthy replication or application session causes real damage.
  • Terminating when pg_cancel_backend would 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 transaction sessions, which are the most common root cause of this error in production.
  • Running ALTER TABLE during peak traffic with no lock_timeout, which can queue behind one long query and stall every later request to that table.

Tips

  • The log_lock_waits setting logs any wait longer than deadlock_timeout, giving you a paper trail of contention.
  • Some ALTER TABLE forms take only a light AccessUpdateExclusiveLock — for example ALTER TABLE ... VALIDATE CONSTRAINT after adding a constraint NOT VALID — so restructuring your migration can avoid the heavy lock entirely.
  • Queue-based tools and migration frameworks often handle lock_timeout plus retry for you; check yours before hand-rolling it.
  • Schedule VACUUM FULL and other AccessExclusiveLock operations for maintenance windows.

Troubleshooting

  • No obvious blocker in pg_stat_activity: the conflict may have already cleared. Check pg_locks for 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 with pg_read_all_stats to run the queries.
  • pg_cancel_backend has no effect: the session may be stuck; escalate to pg_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 VALID forms 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.