MultiXacts
when several transactions lock one row at once. the other counter that can wrap.
- address
- everything.mvcc.multixacts
- part
- part 14, MVCC and vacuum
- chapter
- chapter 11 of 12
- reading time
- 8 min
a row lock is stored in the tuple's xmax, and xmax has room for one id. when a second transaction locks a row that's already share-locked, postgres creates a MultiXact: a new id, from a separate counter, that stands for a list of (transaction, lock mode) members. the tuple gets the MultiXact id in xmax and a flag that says so. that counter is 32 bits too, so it needs freezing, just like xids.
the picture
you know how SELECT ... FOR UPDATE leaves its xid in xmax? postgres has no lock table for rows: a row lock lives on the row itself, which is how millions of rows can be locked without running out of memory. that works for one locker. but FOR SHARE and FOR KEY SHARE are made to be held by many transactions at once.
think of a meeting room booking sheet with space for one name. a second person wants to share the room. instead of writing two names in a one-name box, the receptionist starts a group, writes "group 1" in the box, and keeps the list of who's in group 1 in a binder at the desk (pg_multixact).
here it is on a real postgres 18.6 server. T1 and T2 both take FOR SHARE on alice, then T3 tries to update her:
| lp | ctid | xmin | xmax | t_ctid | id | owner | balance |
|---|---|---|---|---|---|---|---|
| (0,1) | 759 | 760 | (0,1) | 1 | alice | 100.00 | |
| (0,2) | 759 | 0 | (0,2) | 2 | bob | 100.00 |
- T1RowShareLockaccounts
- T1RowShareLockaccounts_pkey
- T1ExclusiveLockxid 760
- T1ExclusiveLock4/4
T1 locks alice's row FOR SHARE. a row lock is written into the tuple itself: T1 gets an xid, and it goes into xmax, marked lock-only.
/* T1 */ BEGIN; SELECT owner FROM accounts WHERE id = 1 FOR SHARE;
/* T2 */ BEGIN; SELECT owner FROM accounts WHERE id = 1 FOR SHARE;
SELECT t_xmax FROM heap_page_items(get_raw_page('accounts', 0)) WHERE lp = 1;what's inside one
two ways to look inside. pgrowlocks reads the row's xmax and expands it; pg_get_multixact_members() takes the MultiXact id directly:
| locked_rowtid | lockerxid | multibool | xids_xid | modes_text |
|---|---|---|---|---|
| (0,1) | 1 | t | {760,761} | {"For Share","For Share"} |
| xidxid | modetext |
|---|---|
| 760 | sh |
| 761 | sh |
sh, FOR SHARE.on disk, the tuple only has the id. the rest is in two SLRU areas under pg_multixact/, read through small caches like the commit log:
- └──
- └── multixact 1 → where its members start
- └── the list, one entry per member
- ├── 760 shT1, FOR SHARE
- └── 761 shT2, FOR SHARE
a MultiXact is immutable. when a third locker comes along, postgres doesn't edit the list: it creates a new MultiXact with three members and swaps the id in xmax. busy rows can go through a lot of them.
one session can make one too
the browser engine has one session, but a MultiXact doesn't strictly need two. a subtransaction has its own xid, so a savepoint that updates a row its parent has share-locked gives the row two "lockers":
BEGIN;
SELECT owner FROM accounts WHERE id = 1 FOR SHARE;
SAVEPOINT s;
UPDATE accounts SET balance = balance + 1 WHERE id = 1;
SELECT lp, t_xmax, (heap_tuple_infomask_flags(t_infomask, t_infomask2)).raw_flags
FROM heap_page_items(get_raw_page('accounts', 0)) WHERE lp = 1;
SELECT * FROM pg_get_multixact_members(
(SELECT t_xmax FROM heap_page_items(get_raw_page('accounts', 0)) WHERE lp = 1))
ORDER BY xid::text::bigint;
COMMIT;| ownertext |
|---|
| alice |
| lpint2 | t_xmaxxid | raw_flagstext[] |
|---|---|---|
| 1 | 1 |
| xidxid | modetext |
|---|---|
| 753 | sh |
| 754 | nokeyupd |
MultiXact 1 again, with two members: the parent's share lock (sh) and the subtransaction's update (nokeyupd, an update that didn't change a key column). so a MultiXact isn't only for locks: it can hold one updater alongside lockers.
where they come from in real life
hardly anyone writes FOR SHARE. the usual source is foreign keys. inserting a row that references a parent takes FOR KEY SHARE on the parent row, to make sure nobody deletes it or changes its key before you commit:
CREATE TABLE owners (id int PRIMARY KEY, name text);
CREATE TABLE pets (id int PRIMARY KEY, owner_id int REFERENCES owners);
INSERT INTO owners VALUES (1, 'ada');
BEGIN;
INSERT INTO pets VALUES (1, 1);
SELECT t_xmax, (heap_tuple_infomask_flags(t_infomask, t_infomask2)).raw_flags
FROM heap_page_items(get_raw_page('owners', 0));
COMMIT;| t_xmaxxid | raw_flagstext[] |
|---|---|
| 756 |
HEAP_XMAX_KEYSHR_LOCK: ada's row is key-share locked by the inserting transaction. two sessions inserting pets for the same owner at the same time make a MultiXact on ada's row, and an app with a few very popular parent rows (one tenant, one account, one product) makes them constantly. the four row lock modes and how they conflict are in row-locking clauses and how row locks are stored.
the other counter that can wrap
MultiXact ids are 32 bits, compared on a circle like xids, so they need freezing too: vacuum replaces old MultiXacts in xmax (with nothing, or with a plain xid if only one member still matters), and tracks the oldest one left per table in relminmxid, per database in datminmxid:
SELECT datname, datminmxid, mxid_age(datminmxid) FROM pg_database ORDER BY datname;
SELECT name, setting FROM pg_settings
WHERE name LIKE '%multixact%freeze%' OR name LIKE '%multixact%failsafe%' ORDER BY name;| datnamename | datminmxidxid | mxid_ageint4 |
|---|---|---|
| postgres | 1 | 0 |
| template0 | 1 | 0 |
| template1 | 1 | 0 |
| nametext | settingtext |
|---|---|
| autovacuum_multixact_freeze_max_age | 400000000 |
| vacuum_multixact_failsafe_age | 1600000000 |
| vacuum_multixact_freeze_min_age | 5000000 |
| vacuum_multixact_freeze_table_age | 150000000 |
the same ladder as for xids, with its own settings: autovacuum_multixact_freeze_max_age defaults to 400 million, twice the xid one, and the failsafe sits at 1.6 billion. there's a second limit too: the members area is a 32-bit space of its own, so lots of MultiXacts with many members can run out of member space before running out of ids. the manual says vacuum turns aggressive when members storage passes about 10 GB, and, as with xids, postgres stops assigning new MultiXacts rather than wrap (routine vacuuming).
keylines
- one locker: its xid goes in xmax. two or more: a MultiXact id goes in xmax.
- a MultiXact is an id from its own counter, mapping to (xid, mode) members in pg_multixact.
- foreign key checks (FOR KEY SHARE) are where most of them come from.
- they're immutable: a new locker means a new MultiXact.
- 32 bits, so they wrap too: relminmxid, datminmxid, mxid_age(), their own freeze ages.