What an index is
An index is a redundant copy of your data in another order, so every write pays to keep it true and every read it serves gets faster.
Reads that match the index's key skip the scan and jump straight to the matching position, while the table and every index must still be updated on each insert, update, and delete. The cost moves from read to write rather than disappearing, so an index is a bet about which queries will run more often than which rows will change.
A B-tree keeps keys sorted inside fixed-size pages, holding roughly 100 to 500 keys per page. A 16 to 32 byte entry packs many keys into each page, so a million-row table fits in a tree three or four levels tall. A lookup walks from the root to one leaf, then reads the matching rows in order from the leaves. An insert lands in a leaf page and writes one log record, and when that page is full it splits, rewriting the parent as well. One logical insert can therefore dirty several pages. A hash index works differently: it hashes the key to find one bucket in a single probe. That makes equality lookups cheap, but hashing scatters neighboring keys, so range and prefix queries cannot use it at all.
The abstraction hides that the second copy is maintained inside the same write. Picture a write-heavy orders table with three new indexes added during a latency incident. Reads on the chosen column speed up, which is what the dashboard shows. Then ingest throughput falls. The disk did not slow down. Each insert now writes a page and a log record per index, and a full page splits and dirties its parent too. Worse, one index was built on a status column that most rows share. A query filtering on that status still touches most of the table's pages, so the binary search it saved was never the bottleneck. You paid write cost and storage for a lookup that the selectivity could never reward.
Name the query before you create the index, including its predicate column and sort order. Index the leading column of predicates you actually run, and confirm that the predicate it matches is selective enough that the index skips most pages. When a query constrains two fields in a fixed order, build one composite index in that order, because separate single-column indexes cannot serve it. Measure write latency and throughput before and after each addition. Drop any index that no production query uses, since its write cost recurs on every row. If a hot query needs only a few fields, add a copy that includes just those fields, and accept that extra write cost for that copy alone.
- index entry size
- 16-32 bytes
- keys per B-tree page
- 100-500
- tree height at 1M rows
- 3-4 levels
- page writes per insert
- 1 page + 1 log record
One key and its row pointer, stored in a leaf page. Each index pays this per row.
Sorted keys in one fixed-size page, the unit the tree reads and writes.
Pages read from root to leaf for one lookup, before reaching the row.
Cost of one insert into one index. A split adds the parent page.
PostgreSQL's B-tree stored one entry per row, even when many rows shared the same key. On a low-cardinality column such as a status or a tenant ID, the keys are short, so the natural assumption was that the index would stay cheap. Key size was never the problem. The problem was how many entries had to be stored and read.
Millions of duplicate entries filled pages with identical keys. An index scan for one value read more pages than it needed, and the cache held fewer useful pages. The usual response was heavier vacuum and rebuild schedules, which reclaimed space but did not change how the duplicates were stored.
The fix stored each duplicate key once, with a posting list of row locations hanging off it. The release notes credit this change with space savings and performance gains. The lesson is that an index's cost is pages touched, not keys stored. Collapsing duplicates cut the structure and the trips through it in one move.