watching locks with pg_locks
pg_locks, pg_blocking_pids, and one query that shows who's blocking whom.
- address
- everything.locking.pg_locks
- part
- part 15, locks and concurrency control
- chapter
- chapter 5 of 10
- reading time
- 8 min
pg_locks is the lock manager's table, one row per lock held or wanted: what kind of thing (locktype), which one, the mode, and granted true or false. a false is a wait. pg_blocking_pids(pid) says who that waiter is stuck behind. join them with pg_stat_activity and you have the one query you want during an incident: who is blocking whom, and what are they doing.
the picture
you know the moment: requests time out, the app logs say nothing useful, and somewhere a transaction is holding a lock. pg_locks is where you find it.
think of it as the lock manager's ledger. each line: who (a pid), what (a table, a transaction, a row, a number), how (the mode), and whether they have it or are standing in line. here's a small pile-up on a real server: T1 updates alice and doesn't commit, T2 takes an advisory lock and then wants alice too, T3 wants alice after T2.
- T1ROW EXCLUSIVEaccounts
- T1ROW EXCLUSIVEaccounts_pkey
- T1ExclusiveLockxid 759
- T1ExclusiveLockvxid 4/4
T1 updates alice and doesn't commit.
the columns
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'pg_catalog' AND table_name = 'pg_locks'
ORDER BY ordinal_position;| column_namename | data_typevarchar |
|---|---|
| locktype | text |
| database | oid |
| relation | oid |
| page | integer |
| tuple | smallint |
| virtualxid | text |
| transactionid | xid |
| classid | oid |
| objid | oid |
| objsubid | smallint |
| virtualtransaction | text |
| pid | integer |
| mode | text |
| granted | boolean |
| fastpath | boolean |
| waitstart | timestamp with time zone |
sixteen columns, but you read them in three groups:
- what:
locktype, then whichever columns identify that kind of object:databaseandrelationfor a table, pluspageandtuplefor a row,transactionidorvirtualxidfor a transaction,classid/objid/objsubidfor advisory locks and other objects. - who:
pid(the backend),virtualtransaction(the transaction asking),fastpath(true if it was taken through the per-backend fast path instead of the shared table). - how:
mode,granted, andwaitstart(when a wait began, null if granted).
the browser has one session, so the only locks you'll see are your own. that's still enough to meet three locktypes:
CREATE TABLE accounts (id int PRIMARY KEY, balance numeric(12,2) NOT NULL);
INSERT INTO accounts VALUES (1, 100.00);
BEGIN;
UPDATE accounts SET balance = balance - 10 WHERE id = 1;
SELECT locktype, relation::regclass, transactionid, virtualxid, mode, granted, fastpath
FROM pg_locks
WHERE pid = pg_backend_pid() AND relation IS DISTINCT FROM 'pg_locks'::regclass
ORDER BY locktype, relation::regclass::text;
COMMIT;| locktypetext | relationregclass | transactionidxid | virtualxidtext | modetext | grantedbool | fastpathbool |
|---|---|---|---|---|---|---|
| relation | accounts | null | null | RowExclusiveLock | t | t |
| relation | accounts_pkey | null | null | RowExclusiveLock | t | t |
| transactionid | null | 754 | null | ExclusiveLock | t | f |
| virtualxid | null | null | 0/5 | ExclusiveLock | t | t |
relation, a ROW EXCLUSIVE on the table and its index (taken through the fast path). transactionid, an exclusive lock on the xid the UPDATE was given: every transaction holds one on itself, so others can wait for it to end. virtualxid, the same idea for the virtual xid every transaction has from its first statement.
the five locktypes you'll meet
here's the full dump from the three-session capture, at the moment T2 and T3 are both waiting:
| sessiontext | locktypetext | objecttext | pageint4 | tupleint2 | modetext | grantedbool |
|---|---|---|---|---|---|---|
| T1 | relation | accounts | null | null | RowExclusiveLock | t |
| T1 | relation | accounts_pkey | null | null | RowExclusiveLock | t |
| T1 | transactionid | 759 | null | null | ExclusiveLock | t |
| T1 | virtualxid | 4/4 | null | null | ExclusiveLock | t |
| T2 | advisory | 42 | null | null | ExclusiveLock | t |
| T2 | relation | accounts | null | null | RowExclusiveLock | t |
| T2 | relation | accounts_pkey | null | null | RowExclusiveLock | t |
| T2 | transactionid | 760 | null | null | ExclusiveLock | t |
| T2 | tuple | accounts | 0 | 1 | ExclusiveLock | t |
| T2 | virtualxid | 5/4 | null | null | ExclusiveLock | t |
| T2 | transactionid | 759 | null | null | ShareLock | f |
| T3 | relation | accounts | null | null | RowExclusiveLock | t |
| T3 | relation | accounts_pkey | null | null | RowExclusiveLock | t |
| T3 | transactionid | 761 | null | null | ExclusiveLock | t |
| T3 | virtualxid | 6/4 | null | null | ExclusiveLock | t |
| T3 | tuple | accounts | 0 | 1 | ExclusiveLock | f |
- relation
- a table or index lock, in one of the eight modes.
- transactionid
- held ExclusiveLock by its own transaction until it ends. a ShareLock request on someone else's means "wake me when they're done": that's how every row lock wait works.
- virtualxid
- the same, for the virtual xid. CREATE INDEX CONCURRENTLY waits on these.
- tuple
- a row, but only while someone is queued for it. the first waiter holds it, the rest wait on it. how row locks are stored explains why.
- advisory
- a lock on a number your application picked (advisory locks). the number is in objid.
notice what's not in the dump: alice's row lock itself. T1 holds it, but it lives in the row's xmax, not here. and notice the modes on tuple and transactionid rows: ExclusiveLock and ShareLock there aren't the table modes, they're the same names reused for a different kind of object.
SELECT pg_blocking_pids(<T3's pid>);who blocks whom
this is the query worth keeping in a file:
SELECT w.pid AS waiting_pid, w.wait_event_type, w.wait_event,
b.pid AS blocked_by, b.state AS blocker_state,
now() - b.xact_start AS blocker_xact_age,
left(w.query, 60) AS waiting_query, left(b.query, 60) AS blocker_query
FROM pg_stat_activity w
JOIN LATERAL unnest(pg_blocking_pids(w.pid)) AS bp(pid) ON true
JOIN pg_stat_activity b ON b.pid = bp.pid
ORDER BY blocker_xact_age DESC;one row per (waiter, blocker) pair. on the capture, with names instead of pids and the time columns left out so it repeats:
| waitingtext | wait_event_typetext | wait_eventtext | blocked_bytext | blocker_statetext | waiting_querytext |
|---|---|---|---|---|---|
| T2 | Lock | transactionid | T1 | idle in transaction | UPDATE accounts SET balance = balance + |
| T3 | Lock | tuple | T2 | active | UPDATE accounts SET balance = balance + |
the blocker at the root of the chain is the one to look at. "idle in transaction" with a big xact age is the classic: an app that opened a transaction and forgot it. pg_terminate_backend(pid) ends it (and rolls it back); idle_in_transaction_session_timeout stops it from happening again. a root that's "active" is a slow query; if the queue behind it is an ALTER TABLE, that's the lock queue.
log_lock_waits
pg_locks is a snapshot. to find waits after the fact, turn on log_lock_waits: any wait longer than deadlock_timeout writes a line to the server log. in postgres 18 it's off by default:
SHOW log_lock_waits;
SHOW deadlock_timeout;| log_lock_waitstext |
|---|
| off |
| deadlock_timeouttext |
|---|
| 1s |
the capture ran with it on and a 100 ms deadlock_timeout. here's what the log got while T2 and T3 waited:
- LOG: process T2 still waiting for ShareLock on transaction 759 after <t> ms
- STATEMENT: UPDATE accounts SET balance = balance + 10 WHERE id = 1
- LOG: process T3 still waiting for ExclusiveLock on tuple (0,1) of relation 16466 of database 5 after <t> ms
- STATEMENT: UPDATE accounts SET balance = balance + 5 WHERE id = 1
the tuple wait names the relation by oid and the database by oid, so you'll want SELECT 16466::regclass to read it. postgres 18 also adds log_lock_failures, which logs the lock that made a NOWAIT fail.
keylines
- pg_locks: one row per lock held (granted) or wanted (not granted).
- locktype says what kind of thing: relation, transactionid, virtualxid, tuple, advisory.
- a row lock wait is a ShareLock on the holder's transaction id. the row itself isn't in pg_locks.
- pg_blocking_pids gives one hop. join it to pg_stat_activity and walk to the root.
- log_lock_waits (off by default in 18) logs every wait longer than deadlock_timeout.