Covering and partial indexes
An index finds rows quickly, and then usually goes to the table to read them. A covering index holds the answer itself, so that trip can be skipped. A partial index holds only the rows your query can ever want, so it stays tiny. Here are both, measured on a real table.
01 Two queries, two kinds of waste
An orders table and two everyday queries: a customer’s order totals for their account page, and the ten oldest pending orders for a worker to ship next.
With a plain index on customer_id, query 1 finds its 20 index entries in a couple of page reads, and then reads 20 table pages to fetch total. Query 2 could use an index on (status, created_at), but 99% of that index describes shipped orders the query never asks for. Every result on this page is from Postgres 16, with the table freshly vacuumed.
02 Where an index scan spends its reads
A B-tree entry holds the indexed value and a pointer to the row: a page number and a slot. Anything else the query wants is in the table, which Postgres calls the heap. Rows for one customer were inserted at different times, so they sit on different pages.
EXPLAIN (ANALYZE, BUFFERS) counts the pages. The index took 2 page reads. The table took 20, one per row, because each row is on its own page. The index did its job; the trip to the heap is ten times the cost.
03 Covering: put the answer in the index
If every column the query uses is in the index, Postgres can answer from the index alone. INCLUDE (Postgres 11 and later, for B-trees) stores extra columns in the leaf entries without making them part of the key:
Bitmap Heap Scan on ordersRecheck Cond: (customer_id = 42)Heap Blocks: exact=20Buffers: shared hit=20 read=2
Index Only Scan using orders_customer_total_idxIndex Cond: (customer_id = 42)Heap Fetches: 0Buffers: shared hit=1 read=3
The word that matters is every. Add one more column to the query and the index no longer covers it. SELECT customer_id, total, status went straight back to a Bitmap Heap Scan with 20 heap blocks.
04 Why the index alone is not quite enough
There is a catch. Postgres keeps old row versions around after an UPDATE or DELETE, and an index entry does not say whether its row version is visible to your query. That information lives in the heap. So how can an index-only scan skip the heap?
Through the visibility map: one bit per table page, set by VACUUM when every row on the page is visible to every transaction. If the bit is set, the index entry can be trusted. If not, Postgres reads the page to check. Here is the scan on a toy table of six pages, where page 3 was changed after the last vacuum:
- reading now
- checked, all fine
- new result row
- answer
- not read
The query wants customer_id and total for customer 42. Both are in the index, so the plan is an Index Only Scan. Postgres has walked down the tree to the leaf where the 42s start. Nothing is read yet.
The same thing on the real table. Five of customer 42’s rows were updated, which clears the bits of their pages until the next vacuum:
Index Only Scan using orders_customer_total_idxIndex Cond: (customer_id = 42)Heap Fetches: 5Buffers: shared hit=9
Index Only Scan using orders_customer_total_idxIndex Cond: (customer_id = 42)Heap Fetches: 0Buffers: shared hit=4
An Index Only Scan is only as good as its Heap Fetches. On a table that changes faster than autovacuum visits it, most pages are not all-visible, every match costs a heap read again, and you pay for the bigger index for nothing. On a copy of the table that had never been vacuumed, the planner did not even choose the index-only plan; it picked a bitmap heap scan.
05 INCLUDE or a second key column?
CREATE INDEX ON orders (customer_id, total) would also cover query 1. Both store total in the leaf entries. The difference is whether total is part of the sort order.
| Property | Key column | INCLUDE column |
|---|---|---|
| Sorted by it within a customer | yes | no |
| Can start or stop the scan (`Index Cond`) | yes | no; only filters entries |
| Stored in upper tree levels | yes | no, leaf pages only |
| Counts in a UNIQUE check | yes | no |
| Needs a B-tree operator for its type | yes | no |
Use a key column when queries filter or sort by it: WHERE customer_id = 42 ORDER BY total reads entries already in order. Use INCLUDE when the column is only carried along to be returned. Its best-known use is a unique index that must stay unique on the key alone: CREATE UNIQUE INDEX ON users (id) INCLUDE (email) keeps id unique while also covering lookups of email.
06 Partial: index only the rows you ask for
Query 2 only ever wants pending orders, 2,000 of the 200,000. A partial index has a WHERE clause, and only rows that match it get an entry:
Both sizes come from pg_relation_size on the same table. With both indexes present, the planner picked the partial one; the plan reads 12 pages to return 10 ids:
It is also cheaper to keep. Inserting a shipped order adds nothing to the partial index, and a smaller index is more likely to stay in memory. Notice the index does not store status at all: every entry has the same one, so the WHERE clause says it once for all of them.
07 The query must promise the index’s WHERE
The planner uses a partial index only when it can prove, from the query text, that every row the query wants satisfies the index’s condition. It does not look at the data to check. Two real cases where it cannot:
Limit-> Gather Merge-> SortSort Key: created_at-> Parallel Seq Scan on ordersFilter: (status = 'shipped'::text)
Limit-> Gather Merge-> SortSort Key: created_at-> Parallel Seq Scan on ordersFilter: (status = $1)
On the left, shipped orders are simply not in the index. On the right, a prepared statement planned once for any parameter cannot use it either, since $1 might not be pending; the plan came from PREPARE with plan_cache_mode = force_generic_plan. Drivers and ORMs often prepare statements, so write the constant into the SQL when you depend on a partial index.
The same idea gives you a rule that ordinary constraints cannot express. With soft deletes, an email must be unique among live users but may repeat among deleted ones:
08 When it bites
SELECT * ... with a covering indexOne extra columnSELECT * or a single new column in the query sends every match back to the heap. Check that the plan still says Index Only Scan after the query changes.Heap Fetches: 18000A busy table and no vacuumIf pages are rarely all-visible, an index-only scan reads the heap anyway. Tune autovacuum for the table, or accept that covering will not pay off there.INCLUDE (a, b, c, d)Covering everythingEach included column makes every leaf entry bigger and every write dearer. Cover the few queries that run constantly, not every query.WHERE status IN ('pending', 'held')A query wider than the indexAn index WHERE status = 'pending' cannot answer this, since held rows are missing. Widen the index condition or split the query.WHERE created_at > now() - interval '1 day'A moving conditionThe WHERE of an index may only use immutable functions, and now() is not one: Postgres refuses with functions in index predicate must be marked IMMUTABLE. Index on a flag or a fixed date you rebuild from time to time instead.