skip to content

the commit log and hint bits

pg_xact, two bits per transaction, and the SELECT that dirties a page.

address
everything.mvcc.commit_log_and_hint_bits
part
part 14, MVCC and vacuum
chapter
chapter 5 of 12
reading time
8 min

whether a transaction committed is stored in exactly one place: the commit log, pg_xact, two bits per transaction. tuples don't hear about it. the first reader that needs the answer looks it up and writes it into the tuple header as hint bits, so nobody has to look again. which means a plain SELECT can dirty a page, and even write WAL.

the picture

you know how COMMIT is supposed to be the moment a transaction's rows become real? postgres doesn't go back and touch them. a transaction might have written a million tuples on ten thousand pages, and visiting them all at commit would make COMMIT as slow as the work itself.

think of a cloakroom. when you leave, the attendant doesn't walk the racks flipping every one of your tickets to "collected". they tick your number in one ledger at the desk (the commit log). the next person who wonders about one of your coats checks the ledger, and pencils "collected" on that ticket so the person after them doesn't have to walk to the desk again (a hint bit).

pg_xact, one page, by xid
01
↑758
01
↑759
10
↑760
00
↑761
the commit log, as it stands at the end of the capture below: two bits per xid, packed four to a byte, indexed by xid. committed is 01, aborted is 10, and an xid nobody has finished yet is 00.

(the fourth value, 11, means "sub-committed": a subtransaction whose parent hasn't finished. it gets resolved when the parent does.)

pg_xact

pg_xact_status() reads it for you:

sqldb=lab · session clog
BEGIN;
UPDATE accounts SET balance = balance + 1 WHERE id = 1;
COMMIT;
BEGIN;
UPDATE accounts SET balance = balance + 1 WHERE id = 2;
ROLLBACK;
SELECT x AS xid, pg_xact_status(x::text::xid8) AS status
FROM generate_series(751, 754) AS x
ORDER BY x;
output, from postgres 18.6
#1 · BEGIN
#2 · UPDATE 1
#3 · COMMIT
#4 · BEGIN
#5 · UPDATE 1
#6 · ROLLBACK
#7 · SELECT 4
xidint4statustext
751committed
752committed
753committed
754aborted
(4 rows)

752 loaded the dataset, 753 committed, 754 rolled back. COMMIT and ROLLBACK cost the same here: two bits flipped in a page of this log (plus, for a commit, a WAL record that has to reach disk first, see the commit path). on disk it's a directory of files in the data directory:

sqldb=lab · session clog
continues session clog (1 earlier block run first)
SELECT pg_ls_dir('pg_xact') AS file, (pg_stat_file('pg_xact/0000')).size AS bytes;
output, from postgres 18.6
SELECT 1
filetextbytesint8
00008192
(1 row)

one 8 KB page holds the status of 32,768 xids (8,192 bytes × 4 per byte), and a segment file grows to 32 pages before the next one starts. the browser's database has only ever used its first page. the log is read through its own small cache of pages in shared memory, and old segments are deleted once nothing in the database can still need them, which is one of the things freezing is for.

hint bits

a lookup in that cache is cheap, but not free, and a seq scan asks once per tuple. so whoever learns the answer writes it into the tuple's t_infomask: four bits, HEAP_XMIN_COMMITTED, HEAP_XMIN_INVALID (aborted), HEAP_XMAX_COMMITTED, HEAP_XMAX_INVALID. from then on, the visibility check reads the bit and skips the log.

the browser can show this on its own. lab's tables were loaded, checkpointed and never read, so no hint bits are set yet:

sqldb=lab · session hint
SELECT lp, t_xmin, (heap_tuple_infomask_flags(t_infomask, t_infomask2)).raw_flags
FROM heap_page_items(get_raw_page('accounts', 0)) ORDER BY lp;
SELECT isdirty FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('accounts') AND relforknumber = 0
  AND reldatabase = (SELECT oid FROM pg_database WHERE datname = current_database());
output, from postgres 18.6
#1 · SELECT 3
lpint2t_xminxidraw_flagstext[]
1752{HEAP_HASVARWIDTH,HEAP_XMAX_INVALID}
2752{HEAP_HASVARWIDTH,HEAP_XMAX_INVALID}
3752{HEAP_HASVARWIDTH,HEAP_XMAX_INVALID}
(3 rows)
#2 · SELECT 1
isdirtybool
f
(1 row)

clean page, no HEAP_XMIN_COMMITTED. now just read the table, and look again:

sqldb=lab · session hint
continues session hint (1 earlier block run first)
SELECT owner, balance FROM accounts ORDER BY id;
SELECT lp, t_xmin, (heap_tuple_infomask_flags(t_infomask, t_infomask2)).raw_flags
FROM heap_page_items(get_raw_page('accounts', 0)) ORDER BY lp;
SELECT isdirty FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('accounts') AND relforknumber = 0
  AND reldatabase = (SELECT oid FROM pg_database WHERE datname = current_database());
output, from postgres 18.6
#1 · SELECT 3
ownertextbalancenumeric
alice100.00
bob100.00
carol50.00
(3 rows)
#2 · SELECT 3
lpint2t_xminxidraw_flagstext[]
1752{HEAP_HASVARWIDTH,HEAP_XMIN_COMMITTED,HEAP_XMAX_INVALID}
2752{HEAP_HASVARWIDTH,HEAP_XMIN_COMMITTED,HEAP_XMAX_INVALID}
3752{HEAP_HASVARWIDTH,HEAP_XMIN_COMMITTED,HEAP_XMAX_INVALID}
(3 rows)
#3 · SELECT 1
isdirtybool
t
(1 row)

a read-only query changed the page: every tuple now says HEAP_XMIN_COMMITTED, and the buffer is dirty. some day it gets written back to disk, by the checkpointer or the background writer. that's the "dirtied" you see in EXPLAIN (BUFFERS) on a SELECT, like the one in why pages.

predict the output
a table was bulk loaded and checkpointed. you run SELECT count(*) on it twice. which run writes WAL?
sql
SELECT count(*) FROM big_fresh_table;   -- first
SELECT count(*) FROM big_fresh_table;   -- second

a SELECT that writes WAL

this capture is from a real server with checksums on. T1 updates alice and commits; a CHECKPOINT writes everything out; then T2 just reads.

T1
·
T2
·
1
UPDATE accounts SET balance = balance + 10 WHERE id = 1→ UPDATE 1
accounts · page 0
byte 0 header line pointerstuples, byte 8,192
lower 36free 8,036 B, unlitupper 8072
lpctidxminxmaxt_ctididownerbalance
(0,1)758759(0,3)1alice100.00
(0,2)7580(0,2)2bob100.00
(0,3)7590(0,3)1alice110.00
shared buffers (pg_buffercache)
+ 789 frames holding other relations, not drawn
page table: page → frame
WAL
pagesaccounts·0pd_lsn+0xFACC0dirty

T1 updates alice and commits. its xid goes into the tuples, and the commit goes into the WAL and the commit log. the tuples don't hear about it.

step 1/6
real output, a postgres 18.6 server · hint-bits
step through. at T2's first read the tuple headers gain their hint bits (hot), the page's buffer turns dirty again, and the WAL gets a record nobody asked for: FPI_FOR_HINT, the whole page. T2's second read changes nothing. the rolled-back update at the end gets its hints with no WAL at all, because the page has already been logged since the checkpoint.

the record is 205 bytes, 156 of them the page image (postgres leaves out the empty hole in the middle of the page, so a mostly empty page logs small). and it sits past the flush pointer: a read-only transaction has no commit record to wait for, so nothing forces it to disk right away.

the page =
byte 0 header line pointerstuples, byte 8,192
lower 36free 8,036 B, unlitupper 8072
lpctidxminxmaxt_ctididbalance
(0,1)758759(0,3)1100.00
(0,2)7580(0,2)2100.00
(0,3)7590(0,3)1110.00
tuple (0,3) · off 8072 · len 39 B
t_xmin759 committed
t_xmax0 nobody deleted it
t_ctid(0,3) itself: the newest version
infomaskHASVARWIDTHXMIN_COMMITTEDXMAX_INVALIDUPDATEDONLY_TUPLE
t_hoff24 B
dataid 1owner alicebalance 110.00
headerline pointer → tuplefree space, unlitdeadin progresswhat this step changed
real postgres 18.6 output · a native server · traces/hint-bits

T2 just reads alice. to judge the two versions it looks T1's xid up in the commit log, then writes the answer into both tuple headers as hint bits. the page is dirty again, and because checksums are on, the first change since the checkpoint logs the whole page.

one tuple header, before and after T2's read. pick the other slot to compare. alice's old version gains HEAP_XMAX_COMMITTED, the new one HEAP_XMIN_COMMITTED.

why not set them at commit?

because that's the work hint bits exist to avoid. commit would have to find and revisit every page the transaction touched, some of them long since written to disk and evicted. instead the cost is spread out and paid only for tuples someone actually reads: a tuple nobody reads before VACUUM gets its hint from VACUUM, and one that gets deleted unread never needs one at all.

they're called hints because losing one is harmless. a crash that loses a dirty page full of hint bits loses nothing: the next reader asks pg_xact again. that's also why, without checksums, setting them isn't WAL-logged at all.

keylines

  • commit status lives in pg_xact: two bits per xid, 32,768 xids per 8 KB page.
  • COMMIT and ROLLBACK flip those bits. the tuples aren't touched.
  • the first reader to resolve a tuple writes hint bits into its header, and dirties the page.
  • with checksums on, that first change after a checkpoint logs the whole page: a SELECT that writes WAL.
  • hints are only hints: lose one and the next reader asks the commit log again.