Sub topic 10
The price of covering: size, writes and HOT updates
What a covering index costs
A covering index stores every column needed by a query. PostgreSQL can then return the result from the index without reading the table heap. This can reduce read cost, but the index now contains more data.
Assume the orders table from the earlier examples. Compare these indexes:
CREATE INDEX orders_customer_idx
ON orders (customer_id);
CREATE INDEX orders_customer_total_idx
ON orders (customer_id) INCLUDE (total);Both indexes use customer_id to find rows. The second index also stores total in each leaf entry. That extra payload makes the index larger.
You can compare their sizes with pg_relation_size:
SELECT
pg_size_pretty(pg_relation_size('orders_customer_idx'))
AS key_only_size,
pg_size_pretty(pg_relation_size('orders_customer_total_idx'))
AS covering_size;The exact difference depends on the data types, number of rows, page layout, and whether PostgreSQL has split index pages. Larger indexes need more storage and may require more memory and I/O when PostgreSQL reads them.
How writes become more expensive
When an inserted row matches an index, PostgreSQL must add an entry to that index. An update can also require index maintenance when it changes a column stored in the index.
An INCLUDE column is not a search key, but it is still stored in the index. Therefore, changing an included column can require PostgreSQL to update the index entry.
For example, this update changes the included total column:
UPDATE orders
SET total = total + 1
WHERE customer_id = 42;The table update and the covering index update both consume CPU, WAL, and disk bandwidth. WAL, or write-ahead logging, records changes so PostgreSQL can recover them after a crash.
A covering index is most useful when its read savings outweigh these write costs. It is usually a better choice for frequently read data that changes less often than it is queried.
HOT updates and included columns
A HOT update is a heap-only tuple update. PostgreSQL can use this optimization when an update does not change any indexed column and the new tuple fits on the same heap page.
A HOT update avoids creating new entries in every affected index. This reduces index maintenance and can reduce table and index bloat.
Consider an index on customer_id:
CREATE INDEX orders_customer_only_idx
ON orders (customer_id);
UPDATE orders
SET note = 'packed'
WHERE id = 100;If note is not indexed and the new row fits on the same page, PostgreSQL may perform a HOT update.
Now add note as included payload:
CREATE INDEX orders_customer_note_idx
ON orders (customer_id) INCLUDE (note);Updates to note can no longer be HOT because note is part of an index. The index must reflect the new value. This is an important trade-off: covering a read query can make unrelated-looking updates more expensive.
Choosing deliberately
Use a covering index when:
- The query runs often and returns a small set of columns.
- The table is large enough for heap visits to matter.
- The included columns do not change frequently.
- The extra index size fits your storage and maintenance budget.
Avoid or reconsider it when the table has heavy updates, especially to included columns. Check the real effect with EXPLAIN (ANALYZE, BUFFERS) and monitor index sizes and write activity.
Exercise
Why can adding total with INCLUDE prevent a HOT update that changes only total?
Show answer
Because total becomes part of an index entry. PostgreSQL must maintain that entry when total changes, so the update is no longer heap-only.