Skip to content

Lesson 6 of 13 · 13m

Indexes Are Not Free

Every index is a write tax on every INSERT and UPDATE; HOT updates, fillfactor, and per-table autovacuum tuning are how you control it.

Every INSERT into Postgres writes to the heap and to every index on the table. Every UPDATE writes a new tuple version to the heap — Postgres never updates in place under MVCC — and then writes a new entry to every index that contains a changed column. Seven indexes on a hot table means every row mutation is eight writes. When Uber published its 2016 post explaining the move from Postgres to MySQL, this exact write amplification was the headline complaint: an UPDATE touching one column still re-wrote index entries across the table's indexes, multiplying disk traffic and replication volume. The post was controversial and partially rebutted, but the mechanism it described is real and still the first thing to check on write-heavy schemas.

The wrong model: indexes only cost disk space

The assumption is that indexes are a read-side structure — a lookup table sitting inert on disk until a query needs it. Under that model, adding an index is always a free win for reads. The real cost structure is write amplification on every mutation, WAL volume for every index entry (indexes are WAL-logged), longer vacuum cycles, and degraded caching — a bloated index set pushes hot heap pages out of memory.

The HOT update escape hatch

There's one important exception to "UPDATE writes every index": the Heap-Only Tuple update. If the UPDATE doesn't change any indexed column, and there's free space on the same heap page, Postgres writes only the new heap tuple and a pointer chain — no index writes at all. This is the mechanism behind the common advice "don't index columns that get updated frequently": updating an indexed column is what kills HOT and forces the full index-write path. The advice is half right — the problem isn't a frequently-updated column, it's an index sitting on the column being updated.

You can widen the HOT window deliberately. Heap pages are filled to fillfactor percent (default 100) before moving on; a table that sees constant updates benefits from ALTER TABLE events SET (fillfactor = 90) — leaving 10% headroom per page so in-page updates stay in-page. On an update-hot table this routinely cuts index write volume by half or more.

Dead tuples and the vacuum treadmill

MVCC's deleted-and-updated tuples don't disappear; they become dead tuples that autovacuum reclaims. The default autovacuum_vacuum_scale_factor = 0.2 means vacuum wakes when dead tuples exceed 20% of the table. On a 10,000-row table that's fine. On a 200-million-row table, it tolerates 40 million dead tuples between runs — your scans read garbage for days before anyone notices.

Large tables need per-table tuning: ALTER TABLE big_events SET (autovacuum_vacuum_scale_factor = 0.02). Index bloat follows the same logic — an index that's been through heavy churn accumulates dead entries and needs occasional REINDEX CONCURRENTLY to shrink back. You can see it: pg_relation_size('idx_name') growing while pg_stat_user_indexes.idx_scan stays flat means you're paying write cost for a structure reads barely use.

Finding the indexes you're paying for but never using

SELECT schemaname, relname, indexrelname, idx_scan,
       pg_size_pretty(pg_relation_size(indexrelid)) AS size
FROM pg_stat_user_indexes
WHERE idx_scan = 0
ORDER BY pg_relation_size(indexrelid) DESC;

idx_scan = 0 after months of uptime means every write pays for an index no query touches. Two caveats before you drop: primary keys and unique constraints show scans too, but you don't drop constraint indexes for being "unused" — they enforce correctness, not speed. And a freshly-restarted database resets pg_stat_*, so measure across real uptime, not since the last reboot.

What to actually do

  • Count your indexes against your write rate. A table taking 5,000 writes/second with 9 indexes is doing 45,000 index writes/second — that's a design decision, not a default.
  • On update-heavy tables: set fillfactor = 90, keep indexed columns out of frequent UPDATE statements, and check pg_stat_user_tables.n_tup_hot_upd to confirm HOT is actually firing.
  • On large tables: tune autovacuum_vacuum_scale_factor down to 0.01–0.05 per table rather than globally.
  • Quarterly: run the unused-index query, check pg_relation_size per index, drop what reads don't use, REINDEX CONCURRENTLY what's bloated.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.