how row locks are stored
four strengths, kept in the tuple header instead of the lock table. pgrowlocks shows them.
- address
- everything.locking.row_lock_internals
- part
- part 15, locks and concurrency control
- chapter
- chapter 4 of 10
- reading time
- 8 min
postgres doesn't keep row locks in its lock table. a row lock is written into the row: the locker's transaction id goes in the tuple's xmax, and a few infomask bits say it's only a lock (HEAP_XMAX_LOCK_ONLY) and how strong. so locking a million rows costs no lock-table memory. the lock table only hears about a row when someone has to wait for it.
the picture
you know how SELECT ... FOR UPDATE on a million rows works fine, while max_locks_per_transaction is 64? if each row lock were an entry in shared memory, that would run out fast. it doesn't, because there's no entry.
think of a library book with a slip inside the cover. to reserve it you write your name on the slip (xmax) and tick a box saying "reserved, not checked out" (LOCK_ONLY). the front desk's list (the lock table) doesn't mention the book at all. only when a second person wants the same book does the desk get involved: they leave their name at the desk and wait for you (your transaction id), not for the book.
four strengths, a few bits
SELECT ... FOR UPDATE has four strengths. here they are on one page, one row each, in one transaction, read straight off the page with pageinspect:
CREATE TABLE t (id int PRIMARY KEY, note text NOT NULL);
INSERT INTO t VALUES (1, 'key share'), (2, 'share'), (3, 'no key update'), (4, 'update');
BEGIN;
SELECT id FROM t WHERE id = 1 FOR KEY SHARE;
SELECT id FROM t WHERE id = 2 FOR SHARE;
SELECT id FROM t WHERE id = 3 FOR NO KEY UPDATE;
SELECT id FROM t WHERE id = 4 FOR UPDATE;
SELECT lp, t_xmax, t_infomask, t_infomask2,
(heap_tuple_infomask_flags(t_infomask, t_infomask2)).combined_flags AS combined,
(heap_tuple_infomask_flags(t_infomask, t_infomask2)).raw_flags AS raw
FROM heap_page_items(get_raw_page('t', 0))
ORDER BY lp;
ROLLBACK;| idint4 |
|---|
| 1 |
| idint4 |
|---|
| 2 |
| idint4 |
|---|
| 3 |
| idint4 |
|---|
| 4 |
| lpint2 | t_xmaxxid | t_infomaskint4 | t_infomask2int4 | combinedtext[] | rawtext[] |
|---|---|---|---|---|---|
| 1 | 755 | 402 | 2 | {} | |
| 2 | 755 | 466 | 2 | {HEAP_XMAX_SHR_LOCK} | |
| 3 | 755 | 450 | 2 | {} | |
| 4 | 755 | 450 | 8194 | {} |
every row has the same xmax: this transaction's id. what tells them apart is the flags. the four strengths are encoded with just three bits in t_infomask and one in t_infomask2:
| row · strength | XMAX_KEYSHR_LOCKinfomask 0x0010 | XMAX_EXCL_LOCKinfomask 0x0040 | XMAX_LOCK_ONLYinfomask 0x0080 | XMAX_IS_MULTIinfomask 0x1000 | KEYS_UPDATEDinfomask2 0x2000 |
|---|---|---|---|---|---|
| 1 FOR KEY SHARE | 1 | 0 | 1 | 0 | 0 |
| 2 FOR SHARE | 1 | 1 | 1 | 0 | 0 |
| 3 FOR NO KEY UPDATE | 0 | 1 | 1 | 0 | 0 |
| 4 FOR UPDATE | 0 | 1 | 1 | 0 | 1 |
that last bit is the clever part. FOR UPDATE sets the same KEYS_UPDATED flag a key-changing UPDATE or a DELETE would. so the conflict rules for "someone might change this row's key" are one rule, whether it was a lock or a real update. a MultiXact adds one more bit, HEAP_XMAX_IS_MULTI, when two transactions share a row and xmax has to point at a list instead of one xid.
BEGIN;
SELECT count(*) FROM (SELECT id FROM accounts FOR UPDATE) AS locked;
SELECT locktype, count(*) FROM pg_locks GROUP BY locktype;BEGIN;
SELECT count(*) AS locked FROM (SELECT id FROM accounts FOR UPDATE) AS l;
SELECT locktype, mode, count(*) FROM pg_locks
WHERE pid = pg_backend_pid() AND (relation IS NULL OR relation <> 'pg_locks'::regclass)
GROUP BY locktype, mode ORDER BY locktype, mode;
ROLLBACK;| lockedint8 |
|---|
| 1000 |
| locktypetext | modetext | countint8 |
|---|---|---|
| relation | RowShareLock | 2 |
| transactionid | ExclusiveLock | 1 |
| virtualxid | ExclusiveLock | 1 |
1,000 rows locked, and pg_locks lists a handful of entries, none of them rows. the price is a write: every locked tuple's header changes, so the page is dirtied and WAL is written. locking is an update to the header.
pgrowlocks reads the page
pgrowlocks is an extension that scans a table and reports rows whose xmax says "locked". it's not in the browser build, so this is from a real postgres 18.6 server, with T1 holding alice FOR UPDATE:
| locked_rowtid | lockerxid | multibool | xids_xid | modes_text |
|---|---|---|---|---|
| (0,1) | 760 | f | {760} | {"For Update"} |
multi is false (one locker, a plain xid), and the mode comes from the infomask bits. it reads the heap, so on a big table it's a full scan.when the lock table does get involved
nothing about a row lock is in shared memory until somebody waits. then two kinds of entry show up:
- T1ExclusiveLockxid 760
T1 locks alice's row FOR UPDATE. pg_locks shows T1's transaction id and its table lock, and nothing about the row: the lock is T1's xid, written into the row's xmax.
so a row lock wait is two locks deep:
- heap page, tuple (0,1)xmax = T1's xid, LOCK_ONLY + EXCL + KEYS_UPDATED
- ├── ExclusiveLock, held by T1 from its first write or row lock
- │ └── T2 waits ShareLock"wake me when T1 ends"
- └──
- ├── T2 holdsfirst in line for this row
- └── T3 waitseveryone after the first queues here
why both? waiting on the xid is how you learn the holder is done: a transaction holds an exclusive lock on its own xid until it ends, so a ShareLock request on it sleeps until then. the tuple lock is the line-keeper: without it, when T1 committed, T2 and T3 would wake together and race for the row, and an unlucky waiter could lose every race forever. with it, the first waiter in line gets the row, the rest queue in order. it's held only while waiting, never once the row is yours, which is why it disappears from pg_locks after T2 gets the row.
keylines
- a row lock is xmax = your xid, plus HEAP_XMAX_LOCK_ONLY and a strength in the infomask.
- KEYSHR, EXCL, both (SHARE), and KEYS_UPDATED for FOR UPDATE. two share-lockers make a MultiXact.
- no lock-table entry per row, so a million row locks fit. locking writes the page, though.
- a waiter waits on the holder's transaction id. the tuple lock keeps the waiters in line.
- pg_locks shows a row only while someone waits. pgrowlocks reads the page instead.