the free space map and the visibility map
where the next row goes, and the bit that lets vacuum and index-only scans skip a page.
- address
- everything.storage.fsm_and_vm
- part
- part 12, pages and the buffer pool
- chapter
- chapter 6 of 12
- reading time
- 10 min
every table has two small sidecar files. the free space map (the _fsm fork) remembers roughly how much room each page has, so an INSERT can find a page with space without reading the table. the visibility map (the _vm fork) keeps two bits per page: all-visible and all-frozen.
VACUUM keeps both up to date. any change to a page clears its visibility bits. vacuum skips the pages whose bits are set, and an index-only scan trusts them instead of reading the heap.
the picture
you know how an INSERT has to put the row somewhere, and the table might have a million pages? it can't look at them all. and VACUUM, which has to visit pages to clean them, would like to skip the ones that have nothing to clean.
think of a big parking garage. at the entrance there's a board with roughly how many free spots each level has (the free space map), so you drive straight to a level with room. and each level has a sticker on the door, "swept, nothing changed since" (the visibility map), so the cleaner can skip it. park a car on a level and the sticker comes off.
here's an 8-page table living through a delete, two inserts, a VACUUM and an update. each bar is a page, and under each one are its two visibility bits:
272 rows, 34 to a page, 8 pages, all live. never vacuumed, so no page has a visibility map bit yet.
the whole chapter is in that stepper. the DELETE leaves pages 2 and 5 full of dead tuples, and the insert right after it ignores them and grows the table to 9 pages. the VACUUM empties pages 2 and 5, tells the free space map, and sets the bits. the next insert lands on page 2. and the update in the last step costs two pages their all-visible bit: the one the old version was on, and the one the new version went to.
the free space map
the same table, in a block you can run. it starts as 8 pages of 34 rows:
CREATE TABLE t (id int PRIMARY KEY, pad text);
INSERT INTO t SELECT g, repeat('x', 200) FROM generate_series(1, 272) AS g ORDER BY g;
SELECT blkno, avail FROM pg_freespace('t') ORDER BY blkno;| blknoint8 | availint2 |
|---|---|
| 0 | 128 |
| 1 | 128 |
| 2 | 128 |
| 3 | 128 |
| 4 | 128 |
| 5 | 128 |
| 6 | 128 |
| 7 | 0 |
the free space map already knows something: as the insert filled each page and moved on to the next, it wrote down what was left on the page it was leaving. 144 bytes were free on each, and the map says 128. it only stores free space in steps of 1/256 of a page, 32 bytes, rounded down, so a whole page's worth fits in one byte. the last page reads 0 because nobody has moved past it yet.
DELETE FROM t WHERE id BETWEEN 69 AND 102 OR id BETWEEN 171 AND 204;
INSERT INTO t VALUES (1001, repeat('y', 200)) RETURNING ctid;DELETE FROM t WHERE id BETWEEN 69 AND 102 OR id BETWEEN 171 AND 204;
INSERT INTO t VALUES (1001, repeat('y', 200)) RETURNING ctid;
SELECT blkno, avail FROM pg_freespace('t') ORDER BY blkno;| ctidtid |
|---|
| (8,1) |
| blknoint8 | availint2 |
|---|---|
| 0 | 128 |
| 1 | 128 |
| 2 | 128 |
| 3 | 128 |
| 4 | 128 |
| 5 | 128 |
| 6 | 128 |
| 7 | 128 |
| 8 | 0 |
now VACUUM. it removes the dead tuples, and records every page's free space as it goes:
VACUUM t;
SELECT blkno, avail FROM pg_freespace('t') ORDER BY blkno;
INSERT INTO t VALUES (1002, repeat('y', 200)) RETURNING ctid;| blknoint8 | availint2 |
|---|---|
| 0 | 128 |
| 1 | 128 |
| 2 | 8160 |
| 3 | 128 |
| 4 | 128 |
| 5 | 8160 |
| 6 | 128 |
| 7 | 128 |
| 8 | 7904 |
| ctidtid |
|---|
| (2,1) |
pages 2 and 5 now say 8,160, and the next row goes to (2,1). the map knows. inserts don't update it, by the way (only VACUUM does, and an insert moving off a page it found too full), so page 2 still says 8,160 even though it now has a row on it. the map is a hint. the inserting backend always checks the real page, and fixes the map if the hint was wrong.
how does a lookup find "a page with at least 232 bytes" without scanning every entry? the map is a tree. each leaf is one heap page's free space, each node above holds the max of its children, so the root says how much the best page in the whole table has:
the root says some page has 8,160 bytes free, enough for 232. at each level the walk takes a child that still has enough, and ends at page 2: 8,160 bytes. 4 steps down, no page read.
on disk the tree is in pages too. each fsm page is one of these trees with about 4,000 leaves, and three levels of fsm pages cover the biggest table postgres allows. that's why even a tiny table's free space map is three pages, 24 KB:
SELECT pg_relation_size('t', 'main') AS main, pg_relation_size('t', 'fsm') AS fsm,
pg_relation_size('t', 'vm') AS vm;
SELECT fsm_page_contents(get_raw_page('t', 'fsm', 2));| mainint8 | fsmint8 | vmint8 |
|---|---|---|
| 73728 | 24576 | 8192 |
| fsm_page_contentstext |
|---|
- └── one byte per page below it: the most free space under it
- └── one byte per leaf page
- └── block 2: a leaf pageone byte per heap page: 4 4 255 4 4 255 4 4 247
block 2 of the fork is the first leaf page, and it's the figure's tree, for real. the lines from node 4095 on are the leaves, one byte per heap page: 4 is 4 × 32 = 128 bytes, 255 is an empty page, 247 is page 8's 7,904. every line above them is an inner node holding the max of its two children, all the way up to node 0, the root of this page, which says 255: there's an empty page somewhere under here. fp_next_slot is where the next search starts, which spreads concurrent inserts across pages instead of piling them all onto the same one.
the visibility map
the visibility map is 2 bits per heap page, so one 8 KB map page covers 32,672 heap pages, about 255 MB of table. all-visible means every tuple on the page is visible to every transaction, now and later: no dead tuples, nothing in progress. all-frozen means every tuple is also frozen, so the page never needs another look for wraparound.
SELECT blkno, all_visible, all_frozen FROM pg_visibility_map('t') ORDER BY blkno;| blknoint8 | all_visiblebool | all_frozenbool |
|---|---|---|
| 0 | t | f |
| 1 | t | f |
| 2 | f | f |
| 3 | t | f |
| 4 | t | f |
| 5 | t | t |
| 6 | t | f |
| 7 | t | f |
| 8 | t | f |
VACUUM set all-visible everywhere, and all-frozen on page 5, which it emptied (nothing left to freeze). page 2 already lost its bit to the insert. and an UPDATE clears the bit on both pages it touches:
UPDATE t SET pad = repeat('z', 200) WHERE id = 210 RETURNING ctid;
SELECT blkno, all_visible FROM pg_visibility_map('t') WHERE NOT all_visible ORDER BY blkno;| ctidtid |
|---|
| (2,2) |
| blknoint8 | all_visiblebool |
|---|---|
| 2 | f |
| 6 | f |
row 210 lived on page 6, and its new version went to page 2 (the free space map pointed there). both pages are no longer all-visible. clearing the bit is part of the change itself, done before the page is modified and logged with it, so the map can never claim a page is all-visible when it isn't. setting the bit is only ever VACUUM's job.
who uses it
two readers trust those bits:
- VACUUM skips all-visible pages: there's nothing on them to clean. on a big, mostly static table that's the difference between reading all of it and reading the few pages that changed. (an aggressive vacuum for wraparound skips only all-frozen ones.)
- an index-only scan skips the heap for all-visible pages. the index has the value but not the visibility information, so for any page that isn't all-visible it has to go and check the tuple. EXPLAIN counts those as
Heap Fetches:
SET enable_seqscan = off;
SET enable_bitmapscan = off;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT count(*) FROM t WHERE id BETWEEN 1 AND 272;
VACUUM t;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT count(*) FROM t WHERE id BETWEEN 1 AND 272;before the VACUUM, 35 heap fetches: the 34 index entries pointing into page 6 (one of them at row 210's old version), plus the one pointing at its new version on page 2. after it, every page is all-visible again and the count comes straight from the index. that's the whole story of index-only scans: they're only as good as the visibility map, and the visibility map is only as fresh as the last VACUUM.
keylines
- the free space map stores each page's free space in 32-byte steps, as a tree of max values, three levels of pages deep.
- inserts ask it for a page with room; a DELETE doesn't update it, VACUUM does.
- the visibility map has two bits per page: all-visible and all-frozen. one map page covers about 255 MB of table.
- VACUUM sets the bits, any change to a page clears them.
- vacuum skips all-visible pages; index-only scans skip the heap for them.