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).
(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:
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;| xidint4 | statustext |
|---|---|
| 751 | committed |
| 752 | committed |
| 753 | committed |
| 754 | aborted |
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:
SELECT pg_ls_dir('pg_xact') AS file, (pg_stat_file('pg_xact/0000')).size AS bytes;| filetext | bytesint8 |
|---|---|
| 0000 | 8192 |
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:
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());| lpint2 | t_xminxid | raw_flagstext[] |
|---|---|---|
| 1 | 752 | {HEAP_HASVARWIDTH,HEAP_XMAX_INVALID} |
| 2 | 752 | {HEAP_HASVARWIDTH,HEAP_XMAX_INVALID} |
| 3 | 752 | {HEAP_HASVARWIDTH,HEAP_XMAX_INVALID} |
| isdirtybool |
|---|
| f |
clean page, no HEAP_XMIN_COMMITTED. now just read the table, and look again:
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());| ownertext | balancenumeric |
|---|---|
| alice | 100.00 |
| bob | 100.00 |
| carol | 50.00 |
| lpint2 | t_xminxid | raw_flagstext[] |
|---|---|---|
| 1 | 752 | {HEAP_HASVARWIDTH,HEAP_XMIN_COMMITTED,HEAP_XMAX_INVALID} |
| 2 | 752 | {HEAP_HASVARWIDTH,HEAP_XMIN_COMMITTED,HEAP_XMAX_INVALID} |
| 3 | 752 | {HEAP_HASVARWIDTH,HEAP_XMIN_COMMITTED,HEAP_XMAX_INVALID} |
| isdirtybool |
|---|
| t |
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.
SELECT count(*) FROM big_fresh_table; -- first
SELECT count(*) FROM big_fresh_table; -- seconda 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.
| lp | ctid | xmin | xmax | t_ctid | id | owner | balance |
|---|---|---|---|---|---|---|---|
| (0,1) | 758 | 759 | (0,3) | 1 | alice | 100.00 | |
| (0,2) | 758 | 0 | (0,2) | 2 | bob | 100.00 | |
| (0,3) | 759 | 0 | (0,3) | 1 | alice | 110.00 |
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.
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.
| lp | ctid | xmin | xmax | t_ctid | id | balance |
|---|---|---|---|---|---|---|
| (0,1) | 758 | 759 | (0,3) | 1 | 100.00 | |
| (0,2) | 758 | 0 | (0,2) | 2 | 100.00 | |
| (0,3) | 759 | 0 | (0,3) | 1 | 110.00 |
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.
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.