The Hidden Write Cost of 'Just Add an Index'
The advice to “just add an index” is common in PostgreSQL performance tuning, and it is often correct. A well-chosen index can turn a sequential scan into an index scan and reduce query latency from seconds to milliseconds. What receives less attention is that every index is a write-side liability. Each new index must be maintained on every INSERT, UPDATE, and DELETE, which means read gains can come at the expense of write throughput.
Why Indexes Slow Down Writes
Every index on a PostgreSQL table is a separate data structure that must stay consistent with the heap. When a row is inserted, PostgreSQL writes to the table and to every applicable index. For UPDATE statements, the database can sometimes avoid index maintenance through a heap-only tuple (HOT) update, but only if the indexed columns are unchanged and there is room on the same page. Otherwise, each relevant index receives a new entry, and the old entry is marked dead until vacuuming removes it.
This overhead consumes buffer cache, generates WAL records, and competes for disk I/O. On write-heavy tables, the cumulative effect is higher write latency and lower throughput. The slowdown may not appear in read benchmarks, which is why the cost often remains hidden until production traffic increases.
Quantifying the Tradeoff Before CREATE INDEX
Developers can estimate the cost by comparing read and write patterns. The pg_stat_user_tables view shows insert, update, and delete counts per table, while pg_stat_user_indexes shows how often each index is scanned. An index with few scans on a high-write table is a liability, not an asset. Before creating a composite index, examine how volatile the indexed columns are. Columns that change frequently, such as status flags or timestamps, incur maintenance on nearly every update. Static columns, such as immutable identifiers, impose a lower per-write cost.
Column order in a composite index also affects write cost. A wider key consumes more space per entry, which means more pages to update and more WAL to write. Running EXPLAIN ANALYZE on representative queries reveals whether the planner can use an index-only scan or whether it still needs heap fetches. If the index is not selective enough to be chosen, the write cost is pure overhead.
Composite Indexes and Write Amplification
Composite indexes magnify write amplification because each entry is larger and because updates to any indexed column require maintenance. An index on (tenant_id, created_at, status) must be updated whenever any of those columns change, even if a query filters only on tenant_id. If status changes often, every update touches the index. Index size also affects cache efficiency: larger indexes evict other useful pages from shared_buffers, increasing read I/O for unrelated queries. On memory-constrained systems, this secondary effect can hurt overall performance more than the direct maintenance cost.
A Practical Checklist
Before running CREATE INDEX, check whether the query is slow enough to justify the penalty, how often the indexed columns change, and whether the index will support index-only scans. Review pg_stat_user_indexes for duplicative or unused indexes, and assess whether the workload can absorb additional WAL and buffer pressure.
Monitoring write latency before and after index creation provides the clearest signal. Tools such as Querk can help surface index usage and write patterns, making the tradeoff visible rather than assumed.
When reads dominate and the index is highly selective, the write cost is usually worth it. When writes are frequent and reads are occasional, “just add an index” can quietly degrade the throughput the database was meant to protect.
