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 frequentUPDATEstatements, and checkpg_stat_user_tables.n_tup_hot_updto confirm HOT is actually firing. - On large tables: tune
autovacuum_vacuum_scale_factordown to 0.01–0.05 per table rather than globally. - Quarterly: run the unused-index query, check
pg_relation_sizeper index, drop what reads don't use,REINDEX CONCURRENTLYwhat's bloated.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
PostgreSQL Fixed 52 CVEs in One Release. The Upgrade Is the Easy Part.
PostgreSQL 18.7, 17.12, 16.16, 15.20 and 14.25 fix 52 security vulnerabilities, almost twice August's record of 28. The binary swap took under a second in my test. The post-update steps are where the work is, and one of them makes some indexes several times bigger.
What PostgreSQL 15 Changed
A new version of the popular open-source database PostgreSQL is now available, offering performance improvements over PostgreSQL 14 and new functions and capabilities for managing workloads in local and distributed deployments. Released on October 13 by the PostgreSQL Global Development Group,…
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.