What does adding an index cost, and what does it buy?
Sketch your own answer first, then compare it with the one below.
A strong answer
An index lets a query find rows by a column without scanning the table. A lookup reads a few pages instead of all of them.
The cost lands on writes. Each insert, delete or update of an indexed column must also change the index, which is another tree with its own page writes and its own log records. The index also takes disk space and memory in the cache.
So each index is a bet that the reads it speeds up are worth the writes it slows. A good answer says to index for the queries you actually run and to drop indexes that nothing uses.
Sources
- PostgreSQL Global Development Group (2024). Index Types.
- Goetz Graefe (2011). Modern B-Tree Techniques.