Sub topic 10
The Free Space Map and Visibility Map
This chapter assumes basic SQL, familiarity with psql, and a general idea of transactions. You should also know that PostgreSQL stores a table in fixed-size pages, usually 8 KB each.
A table has two auxiliary forks that help PostgreSQL manage those pages:
- The free space map (FSM) records how much reusable space each heap page probably has.
- The visibility map (VM) records whether each heap page contains only rows visible to every transaction.
These maps let PostgreSQL avoid scanning the whole table whenever it needs to insert a row, reclaim space, or answer a query.
The free space map
An FSM is a compact index of free space in a table's heap pages. It does not store the free space itself. The heap page still contains the space. The FSM only records an approximate amount available on each page.
When PostgreSQL inserts a row, it can consult the FSM to find a page that might fit the row. It then checks the page itself. If the estimate is stale or too small, PostgreSQL searches again or extends the table with a new page.
The FSM is stored in a separate relation fork whose name ends in _fsm. PostgreSQL maintains one FSM for each relation that can receive tuples, including tables and some indexes.
The map is approximate because page contents change frequently. An update, delete, pruning operation, or VACUUM can change the available space. PostgreSQL updates the FSM as it learns about those changes.
SELECT
'accounts'::regclass AS relation,
pg_relation_size('accounts') AS heap_bytes,
pg_relation_size('accounts', 'fsm') AS fsm_bytes;The pg_relation_size function can inspect a particular fork. The main heap fork is called main; the other commonly inspected forks are fsm and vm.
The visibility map
The VM has information about heap pages, not individual rows. It stores two important bits for each page:
| Bit | Meaning | Main use |
|---|---|---|
| All-visible | Every row on the page is visible to every transaction that could need to see it | Index-only scans and vacuum decisions |
| All-frozen | Every row on the page has been frozen for transaction-age purposes | Avoiding future vacuum work |
A page becomes all-visible only after PostgreSQL verifies that no row version on the page is hidden from any active transaction. A page becomes all-frozen when its row versions no longer need normal transaction IDs for visibility checks.
The VM is stored in a separate _vm fork. Unlike the FSM, it has a correctness role. PostgreSQL cannot treat a page as all-visible unless the VM bit is trustworthy. When a row changes, PostgreSQL clears the page's all-visible bit. VACUUM can set it again after checking the page.
Why the VM helps queries
An index-only scan can return a result from an index without visiting the heap for every matching row. However, an index usually does not contain enough information to prove that a row version is visible to the current transaction.
For an all-visible page, the VM provides that proof. PostgreSQL can use the index entry and skip the heap fetch. If the page is not marked all-visible, PostgreSQL must visit the heap to perform a visibility check.
SELECT
pg_relation_size('accounts', 'main') AS heap_bytes,
pg_relation_size('accounts', 'fsm') AS fsm_bytes,
pg_relation_size('accounts', 'vm') AS vm_bytes;A small VM can therefore save substantial I/O on a large table. The benefit is greatest for read-heavy tables that receive regular vacuuming.
Exercise
Why can an index-only scan still need to read the heap?
Show answer
If the VM does not mark the relevant heap page all-visible, PostgreSQL must visit the heap to check which row version is visible.