Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17
SQLSTATE 55P03 (lock_not_available) means PostgreSQL stopped a statement because the lock it needed was held by another transaction. You see it in two forms: canceling statement due to lock timeout when lock_timeout runs out, and could not obtain lock on row in relation ... when the query used NOWAIT.
In these experiments session B set lock_timeout = '3s'. The blocking side was always a transaction that had done its work and was sitting in idle in transaction, so the fix was to end that transaction or to stop waiting for locked rows.
01Another session updated the row and has not committed
Session A ran BEGIN and updated item 1 without committing. Session B's UPDATE on the same row waited, and after 3 seconds it failed with 55P03 canceling statement due to lock timeout (CONTEXT: while updating tuple (0,1) in relation "stock").
While B was waiting, pg_blocking_pids(pid) in pg_stat_activity showed the chain: B (active) had A's pid in blocked_by, and A was idle in transaction.
In the retry, A ran COMMIT while B was waiting, and B's UPDATE finished. qty became 97.
Setup
CREATE TABLE stock (item_id int PRIMARY KEY, qty int);
INSERT INTO stock VALUES (1, 100), (2, 50), (3, 70);CREATE TABLE
INSERT 0 3BEGIN;
UPDATE stock SET qty = qty - 1 WHERE item_id = 1;BEGIN
UPDATE 1SET lock_timeout = '3s';SETUPDATE stock SET qty = qty - 2 WHERE item_id = 1;SELECT pid, pg_blocking_pids(pid) AS blocked_by, state, left(query, 50) AS query
FROM pg_stat_activity
WHERE usename = 'lab_a' AND pid <> pg_backend_pid()
ORDER BY pid; pid | blocked_by | state | query
------+------------+---------------------+---------------------------------------------------
4428 | {} | idle in transaction | UPDATE stock SET qty = qty - 1 WHERE item_id = 1;
4436 | {4428} | active | UPDATE stock SET qty = qty - 2 WHERE item_id = 1;
(2 rows)ERROR: 55P03: canceling statement due to lock timeout
CONTEXT: while updating tuple (0,1) in relation "stock"An idle in transaction session at the head of the chain is the usual culprit: an application or SQL client that opened a transaction and never committed.
BEGIN;
UPDATE stock SET qty = qty - 1 WHERE item_id = 1;BEGIN
UPDATE 1SET lock_timeout = '3s';SETUPDATE stock SET qty = qty - 2 WHERE item_id = 1;COMMIT;COMMITUPDATE 1SELECT item_id, qty FROM lab_a.stock ORDER BY item_id; item_id | qty
---------+-----
1 | 97
2 | 50
3 | 70
(3 rows)02SELECT ... FOR UPDATE NOWAIT on a row another transaction has locked
Session A locked item 1 with SELECT ... FOR UPDATE. Session B ran the same query with NOWAIT and got 55P03 could not obtain lock on row in relation "stock" immediately, without waiting.
For a job queue or work list where any free row will do, FOR UPDATE SKIP LOCKED is the alternative: in the retry B skipped item 1 and got items 2 and 3 locked for itself.
Setup
CREATE TABLE stock (item_id int PRIMARY KEY, qty int);
INSERT INTO stock VALUES (1, 100), (2, 50), (3, 70);CREATE TABLE
INSERT 0 3BEGIN;
SELECT item_id, qty FROM stock WHERE item_id = 1 FOR UPDATE;BEGIN
item_id | qty
---------+-----
1 | 100
(1 row)BEGIN;
SELECT item_id, qty FROM stock WHERE item_id = 1 FOR UPDATE NOWAIT;BEGIN
ERROR: 55P03: could not obtain lock on row in relation "stock"NOWAIT is intentional: it tells you right away that someone else is working on the row. Catch 55P03 and retry later, or use SKIP LOCKED if another row is fine.
BEGIN;
SELECT item_id, qty FROM stock WHERE item_id = 1 FOR UPDATE;BEGIN
item_id | qty
---------+-----
1 | 100
(1 row)BEGIN;
SELECT item_id, qty FROM stock ORDER BY item_id FOR UPDATE SKIP LOCKED;BEGIN
item_id | qty
---------+-----
2 | 50
3 | 70
(2 rows)03ALTER TABLE waits for a transaction that only read the table
Session A ran BEGIN and SELECT count(*) FROM stock, then did nothing. Session B ran ALTER TABLE stock ADD COLUMN memo text, which needs an exclusive lock on the table. It waited behind A's read and failed after 3 seconds with 55P03 lock timeout. pg_stat_activity showed the ALTER blocked by A's pid, again idle in transaction.
In the retry, A committed while the ALTER was waiting, and ALTER TABLE completed.
Setup
CREATE TABLE stock (item_id int PRIMARY KEY, qty int);
INSERT INTO stock VALUES (1, 100), (2, 50), (3, 70);CREATE TABLE
INSERT 0 3BEGIN;
SELECT count(*) FROM stock;BEGIN
count
-------
3
(1 row)SET lock_timeout = '3s';SETALTER TABLE stock ADD COLUMN memo text;SELECT pid, pg_blocking_pids(pid) AS blocked_by, state, left(query, 50) AS query
FROM pg_stat_activity
WHERE usename = 'lab_a' AND pid <> pg_backend_pid()
ORDER BY pid; pid | blocked_by | state | query
------+------------+---------------------+-----------------------------------------
4789 | {} | idle in transaction | SELECT count(*) FROM stock;
4797 | {4789} | active | ALTER TABLE stock ADD COLUMN memo text;
(2 rows)ERROR: 55P03: canceling statement due to lock timeoutEven a plain SELECT holds a table lock until its transaction ends. Before a migration, look for idle in transaction sessions; running DDL with a short lock_timeout keeps it from hanging.
BEGIN;
SELECT count(*) FROM stock;BEGIN
count
-------
3
(1 row)SET lock_timeout = '3s';SETALTER TABLE stock ADD COLUMN memo text;COMMIT;COMMITALTER TABLE04INSERT into a child table waits for a parent row another transaction is deleting
Session B only inserts an order, yet it times out. CONTEXT shows why: the foreign key check runs SELECT 1 FROM ONLY "lab_a"."customers" ... FOR KEY SHARE, which needs a lock on customer 1, and session A has deleted that customer without committing.
The activity query shows B's blocked_by pointing to A's pid, with A idle in transaction on DELETE FROM customers. When A rolls back, B's waiting INSERT completes (INSERT 0 1) and the order is linked to Kim.
Setup
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers(id), amount int);
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');CREATE TABLE
CREATE TABLE
INSERT 0 2BEGIN;
DELETE FROM customers WHERE id = 1;BEGIN
DELETE 1SET lock_timeout = '3s';SETINSERT INTO orders VALUES (10, 1, 15000);SELECT pid, pg_blocking_pids(pid) AS blocked_by, state, left(query, 50) AS query
FROM pg_stat_activity
WHERE usename = 'lab_a' AND pid <> pg_backend_pid()
ORDER BY pid; pid | blocked_by | state | query
------+------------+---------------------+-------------------------------------------
4982 | {} | idle in transaction | DELETE FROM customers WHERE id = 1;
4990 | {4982} | active | INSERT INTO orders VALUES (10, 1, 15000);
(2 rows)ERROR: 55P03: canceling statement due to lock timeout
CONTEXT: while locking tuple (0,1) in relation "customers"
SQL statement "SELECT 1 FROM ONLY "lab_a"."customers" x WHERE "id" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x"BEGIN;
DELETE FROM customers WHERE id = 1;BEGIN
DELETE 1SET lock_timeout = '3s';SETINSERT INTO orders VALUES (10, 1, 15000);ROLLBACK;ROLLBACKINSERT 0 1SELECT o.id, o.customer_id, c.name FROM lab_a.orders o JOIN lab_a.customers c ON c.id = o.customer_id; id | customer_id | name
----+-------------+------
10 | 1 | Kim
(1 row)05LOCK TABLE ... NOWAIT in a batch job while another transaction still reads the table
With NOWAIT, PostgreSQL does not wait at all: the batch's LOCK TABLE daily_sales IN ACCESS EXCLUSIVE MODE NOWAIT fails at once with a different message, could not obtain lock on relation "daily_sales", same 55P03.
pg_locks shows the holder: session A has AccessShareLock (granted = t) and is idle in transaction after a plain SELECT sum(amount). Even a SELECT keeps its lock until the transaction ends. After A commits, the batch takes the lock and runs TRUNCATE.
Setup
CREATE TABLE daily_sales (sale_date date, amount int);
INSERT INTO daily_sales VALUES (DATE '2026-12-01', 15000), (DATE '2026-12-01', 8000);CREATE TABLE
INSERT 0 2BEGIN;
SELECT sum(amount) FROM daily_sales;BEGIN
sum
-------
23000
(1 row)BEGIN;
LOCK TABLE daily_sales IN ACCESS EXCLUSIVE MODE NOWAIT;BEGIN
ERROR: 55P03: could not obtain lock on relation "daily_sales"SELECT l.pid, l.mode, l.granted, a.state, left(a.query, 40) AS query
FROM pg_locks l JOIN pg_stat_activity a ON a.pid = l.pid
WHERE l.relation = 'lab_a.daily_sales'::regclass
ORDER BY l.pid; pid | mode | granted | state | query
------+-----------------+---------+---------------------+--------------------------------------
5183 | AccessShareLock | t | idle in transaction | SELECT sum(amount) FROM daily_sales;
(1 row)BEGIN;
SELECT sum(amount) FROM daily_sales;BEGIN
sum
-------
23000
(1 row)COMMIT;COMMITBEGIN;
LOCK TABLE daily_sales IN ACCESS EXCLUSIVE MODE NOWAIT;
TRUNCATE daily_sales;
COMMIT;BEGIN
LOCK TABLE
TRUNCATE TABLE
COMMITFAQ
How do I find what is blocking my query in PostgreSQL?
While it is waiting, query pg_stat_activity with pg_blocking_pids(pid). The pid in blocked_by is the session holding the lock; on this page it was always idle in transaction.
What is the difference between lock timeout and could not obtain lock on row?
Both are SQLSTATE 55P03. canceling statement due to lock timeout comes after waiting for lock_timeout; could not obtain lock on row in relation comes immediately from FOR UPDATE NOWAIT.
Why does ALTER TABLE wait when nobody is writing to the table?
An open transaction that only ran SELECT on the table still holds a lock that conflicts with ALTER TABLE. In the third experiment the ALTER waited for a transaction that had just counted rows, and finished as soon as it committed.
How can several workers pick rows without 55P03?
Use FOR UPDATE SKIP LOCKED. In the second experiment it skipped the row session A had locked and returned the other two rows.
Why does an INSERT get canceling statement due to lock timeout?
With a foreign key, the INSERT locks the parent row (FOR KEY SHARE, shown in CONTEXT). If another open transaction is deleting or changing that parent, the INSERT waits for it.
What does could not obtain lock on relation mean?
A LOCK TABLE ... NOWAIT (or similar) could not get the table lock right away. Find the holder in pg_locks; even a plain SELECT in an open transaction holds AccessShareLock.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12