skip to content

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:

T1
snapshot 760:760:
T2
·
T3
·
1
BEGIN; SELECT owner FROM accounts WHERE id = 1 FOR SHARE→ alice
accounts · page 0
byte 0 header line pointerstuples, byte 8,192
lower 32free 8,080 B, unlitupper 8112
lpctidxminxmaxt_ctididownerbalance
(0,1)759760(0,1)1alice100.00
(0,2)7590(0,2)2bob100.00
locks
lock manager
  • 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.

step 1/7
real output, a postgres 18.6 server · multixact
step through and watch slot 1's xmax. after T1 it's T1's xid, marked lock-only. after T2 it's 1, a MultiXact id, with HEAP_XMAX_IS_MULTI set. T3's UPDATE waits: first for T1, then, once T1 commits, for T2. the locks panel shows each wait.
predict the output
T1 and T2 both hold FOR SHARE on the same row. what's in the tuple's xmax?
sql
/* 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_rowtidlockerxidmultiboolxids_xidmodes_text
(0,1)1t{760,761}{"For Share","For Share"}
(1 row)
xidxidmodetext
760sh
761sh
(2 rows)
real output, a postgres 18.6 server · multixact
the row's lock, from pgrowlocks, and the MultiXact's members. multi = t: the locker is a MultiXact id, not an xid. its members are T1 (760) and T2 (761), both 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
where a MultiXact lives. the id in xmax indexes the offsets area, which points into the members area: one entry per member.

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":

sqldb=lab · session mx
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;
output, from postgres 18.6
#1 · BEGIN
#2 · SELECT 1
ownertext
alice
(1 row)
#3 · SAVEPOINT
#4 · UPDATE 1
#5 · SELECT 1
lpint2t_xmaxxidraw_flagstext[]
11
(1 row)
#6 · SELECT 2
xidxidmodetext
753sh
754nokeyupd
(2 rows)
#7 · COMMIT

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:

sqldb=lab · session fk
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · CREATE TABLE
#3 · INSERT 0 1
#4 · BEGIN
#5 · INSERT 0 1
#6 · SELECT 1
t_xmaxxidraw_flagstext[]
756
(1 row)
#7 · COMMIT

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:

sqldb=lab · session mxage
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;
output, from postgres 18.6
#1 · SELECT 3
datnamenamedatminmxidxidmxid_ageint4
postgres10
template010
template110
(3 rows)
#2 · SELECT 4
nametextsettingtext
autovacuum_multixact_freeze_max_age400000000
vacuum_multixact_failsafe_age1600000000
vacuum_multixact_freeze_min_age5000000
vacuum_multixact_freeze_table_age150000000
(4 rows)

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.