Learn Labs
Storage Internals

MVCC Lab: Bloat, HOT and VACUUM

Updating 100,000 rows, measuring the dead ones, and watching one open transaction stop VACUUM

The previous pages say an UPDATE writes a new row version and leaves the old one behind. labs/mvcc_vacuum.py measures what that costs on a 100,000-row table with autovacuum turned off, so nothing cleans up behind our back.

docker compose -f compose_prod.yaml run --rm labs python mvcc_vacuum.py

An UPDATE is an INSERT

before UPDATE: ctid (0,1) xmin 828
after UPDATE:  ctid (735,41) xmin 829  (a new tuple; the old one is now dead)

ctid is the row's physical address: page 0, slot 1. After one UPDATE the row lives on page 735, at the end of the table, written by a new transaction (xmin 829). The table was loaded with fillfactor = 100, so page 0 had no room for the new version and Postgres put it wherever there was space. The old version is still on page 0, now dead.

Bloat, and what VACUUM does about it

100,000 rows loaded5.8 MBUPDATE every row11.5 MB (100,000 dead)VACUUM11.5 MB (0 dead, space reusable)UPDATE every row again11.5 MB (reused)VACUUM FULL5.8 MB (rewritten, exclusive lock)
  • Updating every row doubled the file, 5.8 MB to 11.5 MB, and left exactly 100,000 dead tuples. Every row now exists twice.
  • VACUUM freed the dead tuples but the file stayed 11.5 MB. Plain VACUUM marks the space reusable; it only gives pages back to the operating system if they're empty at the very end of the table.
  • Updating every row again kept it at 11.5 MB. The new versions went into the freed space. This is the steady state a healthy table reaches: some slack, reused over and over.
  • VACUUM FULL rewrote the table to 5.8 MB in 0.10 s, holding an ACCESS EXCLUSIVE lock the whole time: no reads, no writes. Fast here; on a 500 GB table it's hours of downtime. pg_repack does the same rewrite without the long lock.

HOT updates: when the indexes don't notice

If the new version fits on the same page and no indexed column changed, Postgres makes a heap-only tuple (HOT): the index keeps pointing at the old slot, which redirects to the new one. No index is touched.

fillfactor 100 (pages full)30,000 updates
  • HOT: 30
  • not HOT, every index updated: 29,970
fillfactor 70 (30% free on each page)30,000 updates
  • HOT: 30,000

With fillfactor = 100 only 30 of 30,000 updates were HOT (0.1%): the pages were full, so every new version went to another page and the email index got a new entry each time (it grew from 6.8 to 6.9 MB). With fillfactor = 70, leaving 30% of every page free, all 30,000 were HOT and the index didn't grow at all. The price: the table was 9% bigger after the updates (8.2 MB vs 7.5 MB), because every page keeps 30% empty. For a table that's updated constantly, that's a good trade.

One open transaction stops the cleanup

deleted 50,000 rows with another transaction open
VACUUM: 0 removed, 50,000 dead but not yet removable
after it commits, VACUUM: 50,000 removed, 0 not yet removable

A second session ran BEGIN ISOLATION LEVEL REPEATABLE READ and one SELECT. Its snapshot can still see the 50,000 deleted rows, so VACUUM may not remove any of them, however many times it runs. The moment it commits, they all go.

This is how a forgotten idle in transaction session, a long report, or a replica with hot_standby_feedback bloats a busy database: VACUUM runs, reports success, and removes nothing. The monitoring chapter alerts on the oldest open transaction for exactly this reason.

On this page