Skip to content

Lesson 14 of 14 · 22m

MVCC and Vacuum: Why Updates Are Inserts

Every UPDATE leaves a dead tuple behind — autovacuum, HOT updates, fillfactor, visibility maps, and the wraparound shutdown to never see.

In Postgres, UPDATE does not modify a row in place. It writes a new version of the tuple and marks the old version dead, and DELETE just marks dead without the new version — every tuple header carries xmin/xmax transaction ids that say which snapshots may see it. That's MVCC, and it's why readers never block writers: your SELECT reads the version valid for its snapshot while an UPDATE is already writing the next. The price is that dead tuples pile up until something removes them, and that something is VACUUM.

The wrong model is "the database deletes rows when you DELETE." It marks them; the space is reclaimed later. A table that churns — sessions, jobs, counters — grows pg_relation_size steadily while its row count is flat, and every scan wades through dead versions. That's bloat, and it hits indexes too: index entries point at dead heap versions until vacuum and page cleanup remove them.

One optimizer's detail worth knowing: a HOT update (Heap-Only Tuple) happens when the new version fits on the same heap page and no indexed column changed — the index doesn't need a new entry, and the chain stays inside one page. This is why fillfactor exists: ALTER TABLE hot SET (fillfactor = 90) leaves 10% of every heap page empty so HOT updates have room. For a table updated constantly on non-indexed columns, that trades a bit of space for meaningfully less index churn.

Autovacuum runs this continuously in the background — it is not optional maintenance you schedule someday, it's part of the write path. The default trigger is autovacuum_vacuum_scale_factor = 0.2 (plus autovacuum_vacuum_threshold = 50): vacuum when dead tuples exceed 20% of the table. At 1,000 rows that's fine; at 500 million rows it means 100 million dead tuples accumulate before anyone cleans. Set it per-table on your big ones:

ALTER TABLE events SET (autovacuum_vacuum_scale_factor = 0.02);

And index-only scans — the covering-index trick from the B-tree lesson — depend on vacuum maintaining the visibility map. A churned table whose visibility map has gone stale makes "Index Only Scan" do a heap fetch per row anyway. Vacuum debt shows up disguised as index regressions.

The dark corner is transaction ID wraparound. xmin/xmax are 32-bit: ~4 billion transactions total, and Postgres can only see ~2 billion back before old tuples would look like they're from the future. autovacuum_freeze_max_age (200M default) forces an anti-wraparound vacuum that freezes old tuples to a permanent "always visible" marker. If a database outruns it — disabled autovacuum, a leaked idle in transaction pinning xmin for weeks — Postgres does the only safe thing: database is not accepting commands to avoid wraparound data loss in database "x", and then forced shutdown and single-user recovery. Sentry's public 2015 postmortem on their wraparound incident is the canonical story: days of degraded service while they emergency-froze a multi-terabyte table. Monitor age(datfrozenxid) per database — it's the countdown.

Watch the gauges, not the folklore: pg_stat_user_tables.n_dead_tup (dead tuples pending), n_dead_tup / n_live_tup (bloat ratio), last_autovacuum/last_autoanalyze. Rebuild bloated indexes with REINDEX CONCURRENTLY; for tables, pg_repack does an online rebuild — VACUUM FULL/CLUSTER rewrite the table under ACCESS EXCLUSIVE, which is downtime you didn't schedule.

Try this: create a small table, run 50k updates in a loop on the same rows, and watch n_dead_tup climb in pg_stat_user_tables. Then VACUUM VERBOSE it and read what it reports removed.

Pitfalls

  • idle in transaction sessions and long analytics queries pin xmin — vacuum can't remove anything newer than the oldest snapshot. Your wraparound clock keeps ticking either way.
  • Manual VACUUM on a cron is a patch on a tuning problem. Tune autovacuum_* per table; let the daemon do its job.
  • A table vacuumed constantly is a signal too: it means churn, and the fix is often fillfactor + fewer indexed columns updated, not more vacuum.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.