How to Use Covering Indexes with INCLUDE
Turn queries into index-only scans with covering indexes and INCLUDE, and understand when the visibility map still forces a heap visit.
On this page
Introduction
An index that contains everything a query needs can answer it without touching the table, turning a scan-plus-lookup into an index-only scan. That is dramatically faster on large tables because it avoids random heap access per row. PostgreSQL supports this directly with INCLUDE, which adds payload columns to an index without making them part of the search key, so you get the covering benefit without widening the key that orders the index. The subtlety is that an index-only scan still depends on the visibility map, so a table that is not vacuumed well gains less than expected.
Covering Versus Composite
A composite index has all its columns as part of the key, which orders the index lexicographically by the whole key. A covering index answers a query from the index alone. The two are different goals: a key column participates in ordering and can be searched; an included column is stored in the leaf pages but not used for ordering or searching.
INCLUDE lets you have both: search on the key columns, and carry the payload as included columns so the leaf already has the data the query selects. The key stays narrow, so the index orders efficiently, and the payload rides along for the index-only scan.
-- Key: (customer_id, created_at). Payload: (id, amount, status).
-- Search/sort on the key; answer the query from the leaf pages.
CREATE INDEX CONCURRENTLY idx_orders_cover
ON orders (customer_id, created_at) INCLUDE (id, amount, status);
-- A query projecting only covered columns can be an index-only scan:
SELECT id, amount, status
FROM orders
WHERE customer_id = $1 AND created_at >= $2
ORDER BY created_at;
Why Index-Only Scans Are Faster
A normal index scan finds the matching rows in the index, then reads each row from the heap to fetch the projected columns. That heap access is random and, on a large table, often the dominant cost. An index-only scan reads everything from the index’s leaf pages, which are physical and sequential, avoiding the heap entirely.
The gain is largest when the query returns many rows and projects few columns, and when the heap access would be scattered. For a query that returns one row, the difference is negligible; for one that returns thousands, it can be the difference between milliseconds and seconds.
# normal index scan: index lookup -> heap fetch per row (random IO)
# index-only scan: index lookup -> read projected cols from leaf (sequential)
# gain is large when:
[ ] many rows returned
[ ] few columns projected
[ ] heap access would be scattered
# gain is small for single-row lookups
The Visibility Map Caveat
An index-only scan only avoids the heap when the index marks the row as visible, which relies on the visibility map. The map says which pages are all-visible and known not to contain dead tuples, so the index entry alone is trustworthy. If the map is not up to date, the executor must still check the heap for the row’s visibility, and the scan degrades toward a normal index scan.
This is why vacuum matters for covering indexes: a well-vacuumed table has a fresh visibility map, and index-only scans actually skip the heap. After heavy writes, before vacuum catches up, the same query does more heap fetches. Include a vacuum strategy in your plan, and watch the heap-fetch count in EXPLAIN.
-- Confirm the scan is truly index-only: "Heap Fetches" should be low.
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT id, amount, status FROM orders
WHERE customer_id = 42 AND created_at >= '2026-10-01'
ORDER BY created_at;
-- Index Only Scan using idx_orders_cover
-- Heap Fetches: 0 <- map fresh; heap skipped entirely
-- Heap Fetches: 12000 <- map stale; vacuum to fix
Choosing What to Include
Include exactly the columns the target query projects, and no more. Every included column widens the leaf pages and grows the index, costing storage and slowing scans. A covering index that carries columns no query needs is just a bigger index.
When several queries share a key, one covering index can serve them if their projections overlap; otherwise you may need more than one covering index, or accept a heap fetch for the less common query. The trade is index size and write cost against heap-fetch cost, and it should be decided from measured plans.
# include only what the query projects
[ ] list the columns the target query selects
[ ] include those, nothing else
[ ] if two queries share a key and overlap in projection, one index serves both
[ ] otherwise weigh a second covering index vs a heap fetch for the rarer query
# each included column grows the index and its write cost
Write Cost and Index Maintenance
Every index is updated on writes to its columns, and a covering index with included columns is updated whenever any covered column changes, even a column that is not in the key. Including a frequently updated column such as a status or a counter makes the index expensive to maintain.
Prefer to include stable columns and avoid including hot ones, or split the covering index so the frequently changing data is not carried. Measure the write amplification: an index that speeds reads but slows writes may be a net loss on a write-heavy table.
# a covering index is a write-time cost
[ ] included column changes => index updated on every update
[ ] including a hot column (status, counter) => expensive writes
[ ] prefer stable payload columns in INCLUDE
[ ] on write-heavy tables, weigh read gain vs write amplification
# measure: index size, writes/sec, and the read queries it accelerates
Reading the Plan to Confirm
The plan is the arbiter. Look for Index Only Scan and a low heap-fetch count; if you see a regular Index Scan, the query projects a column the index does not cover, or the planner chose otherwise. Compare the plan before and after adding INCLUDE.
Sometimes the planner will not pick the covering index even when it could, because its cost estimate prefers a different path. If so, check that statistics are fresh and that the projection really matches the index; if the estimate is wrong, that is a statistics problem to fix, not a reason to force the plan.
# plan tells
Index Only Scan + Heap Fetches: 0/低 -> covering works
Index Only Scan + Heap Fetches: 高 -> stale visibility map (vacuum)
Index Scan (not only) -> projection not fully covered
Seq Scan -> planner didn't choose it; check stats
Common Pitfalls and Conclusion
Do not include columns the query does not project. Do not forget that index-only scans depend on a fresh visibility map, so vacuum is part of the design. Do not include hot columns without weighing write cost. Do not expect the planner to pick a covering index whose estimate is off; fix the statistics. And do not build a covering index for a single-row lookup where the heap fetch is trivial.
Covering indexes with INCLUDE answer a query from the index leaf pages instead of the heap, turning scattered random IO into a sequential scan of the index. Include exactly the projected columns, keep the key narrow, vacuum so the visibility map lets the scan skip the heap, and weigh the write cost. Confirm with EXPLAIN that the scan is genuinely index-only, and the same query gets dramatically cheaper on a large table.
Sources: PostgreSQL: Index-only scans, PostgreSQL: CREATE INDEX (INCLUDE), PostgreSQL: Routine vacuuming.
FAQ
- What does INCLUDE do differently from adding a key column?
- A key column participates in ordering and can be searched; an included column is stored in the index leaf pages but not used for ordering or search. INCLUDE adds the payload for an index-only scan while keeping the search key narrow and ordered efficiently.
- Why does my index-only scan still fetch from the heap?
- Because the visibility map is stale. An index-only scan skips the heap only when the index marks the row as all-visible, which depends on vacuum. If Heap Fetches is high, vacuum the table to refresh the map.
Report a problem with this articleOpens a pre-filled issue on GitHub.