skip to content

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.

T1
·
T2
·
T3
·
1
BEGIN; UPDATE accounts SET balance = balance - 10 WHERE id = 1→ UPDATE 1
pg_locks
  • T1ROW EXCLUSIVEaccounts
  • T1ROW EXCLUSIVEaccounts_pkey
  • T1ExclusiveLockxid 759
  • T1ExclusiveLockvxid 4/4

T1 updates alice and doesn't commit.

step 1/6
real output, a postgres 18.6 server · pg-locks
step through. the list is pg_locks for the three sessions after each step. held locks are solid, waits are dashed. T2 waits on T1's transaction id, T3 waits on the row's tuple lock, which T2 holds as the first in line.

the columns

sqlempty db
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'pg_catalog' AND table_name = 'pg_locks'
ORDER BY ordinal_position;
output, from postgres 18.6
SELECT 16
column_namenamedata_typevarchar
locktypetext
databaseoid
relationoid
pageinteger
tuplesmallint
virtualxidtext
transactionidxid
classidoid
objidoid
objsubidsmallint
virtualtransactiontext
pidinteger
modetext
grantedboolean
fastpathboolean
waitstarttimestamp with time zone
(16 rows)

sixteen columns, but you read them in three groups:

  • what: locktype, then whichever columns identify that kind of object: database and relation for a table, plus page and tuple for a row, transactionid or virtualxid for a transaction, classid/objid/objsubid for 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, and waitstart (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:

sqlempty db
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 1
#3 · BEGIN
#4 · UPDATE 1
#5 · SELECT 4
locktypetextrelationregclasstransactionidxidvirtualxidtextmodetextgrantedboolfastpathbool
relationaccountsnullnullRowExclusiveLocktt
relationaccounts_pkeynullnullRowExclusiveLocktt
transactionidnull754nullExclusiveLocktf
virtualxidnullnull0/5ExclusiveLocktt
(4 rows)
#6 · COMMIT

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:

sessiontextlocktypetextobjecttextpageint4tupleint2modetextgrantedbool
T1relationaccountsnullnullRowExclusiveLockt
T1relationaccounts_pkeynullnullRowExclusiveLockt
T1transactionid759nullnullExclusiveLockt
T1virtualxid4/4nullnullExclusiveLockt
T2advisory42nullnullExclusiveLockt
T2relationaccountsnullnullRowExclusiveLockt
T2relationaccounts_pkeynullnullRowExclusiveLockt
T2transactionid760nullnullExclusiveLockt
T2tupleaccounts01ExclusiveLockt
T2virtualxid5/4nullnullExclusiveLockt
T2transactionid759nullnullShareLockf
T3relationaccountsnullnullRowExclusiveLockt
T3relationaccounts_pkeynullnullRowExclusiveLockt
T3transactionid761nullnullExclusiveLockt
T3virtualxid6/4nullnullExclusiveLockt
T3tupleaccounts01ExclusiveLockf
(16 rows)
real output, a postgres 18.6 server · pg-locks
pg_locks for T1, T2 and T3, pids swapped for session names. two rows have granted = f: T2's ShareLock on T1's transaction 759, and T3's tuple lock on (0,1).
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.

predict the output
T1 holds a lock T2 wants. T2 is first in line, T3 is behind it. what does pg_blocking_pids return for T3?
sql
SELECT pg_blocking_pids(<T3's pid>);

who blocks whom

this is the query worth keeping in a file:

blockers.sql
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:

waitingtextwait_event_typetextwait_eventtextblocked_bytextblocker_statetextwaiting_querytext
T2LocktransactionidT1idle in transactionUPDATE accounts SET balance = balance +
T3LocktupleT2activeUPDATE accounts SET balance = balance +
(2 rows)
real output, a postgres 18.6 server · pg-locks
T2 is blocked by T1, which is idle in transaction: it did its UPDATE and is sitting there. T3 is blocked by T2. wait_event_type Lock with wait_event transactionid or tuple is a heavyweight lock wait.

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:

sqlempty db
SHOW log_lock_waits;
SHOW deadlock_timeout;
output, from postgres 18.6
#1 · SHOW
log_lock_waitstext
off
(1 row)
#2 · SHOW
deadlock_timeouttext
1s
(1 row)

the capture ran with it on and a 100 ms deadlock_timeout. here's what the log got while T2 and T3 waited:

server log
  1. LOG: process T2 still waiting for ShareLock on transaction 759 after <t> ms
  2. STATEMENT: UPDATE accounts SET balance = balance + 10 WHERE id = 1
  3. LOG: process T3 still waiting for ExclusiveLock on tuple (0,1) of relation 16466 of database 5 after <t> ms
  4. STATEMENT: UPDATE accounts SET balance = balance + 5 WHERE id = 1
real output, a postgres 18.6 server · pg-locks
the server log, with pids swapped for session names and the wait time masked (it changes every run). the full message also has a DETAIL line naming the holder's pid and the wait queue; this server ran with terse errors so the capture repeats exactly.

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.