Sub topic 5
Waiting for Locks: the Queue and How Blocking Chains Form
Assume you know basic SQL, transactions, primary keys, and how to use two concurrent psql sessions. This chapter focuses on what happens after one transaction asks for a lock that another transaction already holds.
PostgreSQL uses multiversion concurrency control, or MVCC, so ordinary readers usually do not wait for writers. A query can read an older committed row version while another transaction updates a newer version. Locks are still needed when transactions modify the same data, change table structure, or explicitly coordinate access.
A lock wait is a queue
When a transaction requests a lock that conflicts with an existing lock, PostgreSQL does not normally fail immediately. The requesting session waits. The session holding the conflicting lock is the blocker.
For example, suppose two sessions update the same account:
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric NOT NULL
);
INSERT INTO accounts (id, balance)
VALUES (1, 100), (2, 100);
-- Session A
BEGIN;
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
-- Session B: waits for Session A
BEGIN;
UPDATE accounts
SET balance = balance + 5
WHERE id = 1;
-- Session A
COMMIT;
-- Session B continues after Session A commits
COMMIT;The UPDATE in Session A takes a row-level lock. That lock lasts until Session A commits or rolls back. Session B has reached the same row, but it cannot finish its update until the conflicting lock is released.
The queue can contain more than one waiting session. PostgreSQL manages the requests so that sessions wait for locks to become available rather than repeatedly retrying the operation themselves.
| Session | State | Lock relationship |
|---|---|---|
| A | Running or idle in transaction | Holds the row lock |
| B | Waiting | Blocked by A |
| C | Waiting | May be blocked by A or by an earlier request |
Blocking chains
A blocking chain forms when one waiting transaction holds a lock that another waiting transaction needs. Consider this sequence:
- Session A locks row 1.
- Session B locks row 2, then waits for row 1.
- Session C requests row 2 and waits for B.
The dependency chain is C → B → A. The arrow means “is waiting for.” Session C might appear to have a simple row-lock problem, but its delay is caused indirectly by Session A.
A blocking chain is different from a deadlock. In a chain, the dependencies eventually reach a transaction that can continue and release its locks. In a deadlock, the dependencies form a cycle. For example, A waits for B while B waits for A. No transaction in that cycle can make progress without intervention.
Finding the blocker
The pg_blocking_pids() function reports the process IDs blocking a server process. The following query lists sessions that are currently waiting for another session:
SELECT
a.pid,
pg_blocking_pids(a.pid) AS blocked_by,
a.usename,
a.state,
a.wait_event_type,
a.wait_event,
a.xact_start,
left(a.query, 120) AS query
FROM pg_stat_activity AS a
WHERE cardinality(pg_blocking_pids(a.pid)) > 0;A nonempty blocked_by array identifies the immediate blocker. Inspect that blocker’s pid as well. It may itself be waiting for another session.
pg_locks shows the lock requests. Rows with granted = false represent requests that are waiting:
SELECT
locktype,
relation::regclass AS relation,
mode,
granted,
pid
FROM pg_locks
WHERE NOT granted;Join these results with pg_stat_activity when you need the SQL text, transaction start time, or application name. An old transaction in idle in transaction state is a common blocker because it has finished its last statement but still holds locks.
Practical responses
Keep transactions short and commit promptly after the protected work is complete. Investigate long-running or idle transactions. For operations that should fail rather than wait, use NOWAIT or set a suitable lock_timeout; these controls are covered later.
When diagnosing an incident, record both sides of the wait: the blocked query and the transaction holding the lock. The blocked query explains the symptom. The blocker often explains the cause.
Exercise
Session B is waiting for Session A, and Session C is waiting for Session B. Which session should you investigate first, and why?
Show answer
Investigate Session A first because it is at the head of the blocking chain. If it can commit or roll back safely, it may release the lock that allows both other sessions to proceed.