Sub topic 10
Covering Indexes and Index-Only Scans
What a covering index stores
Prerequisites: you should know basic SQL, B-tree indexes, and how to read the main lines of an EXPLAIN plan.
A normal index stores its key columns and a reference to the matching table row. For example, an index on customer_id can find rows for customer 42, but PostgreSQL must usually visit the table to read total_cents.
A covering index contains all columns needed by a query. PostgreSQL can then read the answer from the index without fetching the table rows. This execution method is called an index-only scan.
In PostgreSQL, use INCLUDE for columns needed in the result but not needed for searching or ordering:
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
total_cents integer NOT NULL
);
CREATE INDEX orders_customer_covering_idx
ON orders (customer_id)
INCLUDE (total_cents);
EXPLAIN
SELECT customer_id, total_cents
FROM orders
WHERE customer_id = 42;The index key is customer_id. The included column, total_cents, is stored in the index payload. The query can use the key to find matching entries and read both selected columns from the index.
Why index-only scans are conditional
An index does not normally contain the entire transaction visibility history of every row. PostgreSQL uses a visibility map, which records whether table pages contain only rows visible to all transactions.
If the relevant page is marked all-visible, PostgreSQL can trust the index entry and avoid a table fetch. If it is not marked all-visible, PostgreSQL checks the table even though the selected columns are present in the index.
Therefore, INCLUDE makes an index-only scan possible, not certain. Frequently updated tables may have fewer all-visible pages. Vacuuming helps PostgreSQL maintain visibility information.
Choosing included columns
Included columns improve reads but enlarge the index. Larger indexes consume storage, need more cache space, and take more time to maintain during inserts and updates. An included column also cannot provide the ordering used by an index scan.
Use a covering index when all of these conditions are broadly true:
- A query or query family runs often.
- The query returns few rows or runs on a busy system.
- The extra columns are small and stable.
- Avoiding table reads matters more than the index's storage and write cost.
| Design choice | Benefit | Cost |
|---|---|---|
| Key column | Supports search and ordering | Adds index size and write work |
| Included column | Can cover the result | Adds payload without search ordering |
| No covering columns | Smallest index | Usually requires table fetches |
Do not include every selected column automatically. First measure the workload, then compare plans before and after the index.
Exercise
A query filters on customerid and selects customerid, totalcents, and status. An index contains customerid and includes total_cents, but not status. Is the query fully covered?
Show answer
No. PostgreSQL may use the index to find matching rows, but it must fetch the table rows to read status.