opyeb SQLPlan

Guides · Indexes and rewriting

Partial and covering indexes: smaller indexes that answer more

Updated 28 Nov 2026 · 7 min read

A partial index includes only the rows that satisfy a predicate. CREATE INDEX idx_orders_paid ON orders (created_at) WHERE status = 'paid'; indexes only paid orders. If most orders are not paid, the index is a fraction of the size of a full index, fits in cache more easily, and costs less to maintain. The planner uses it when the query's WHERE clause guarantees the same predicate.

A covering index contains every column the query needs, so the scan never visits the heap. In PostgreSQL 11 and later, INCLUDE adds columns to the leaf entries without making them part of the search key: CREATE INDEX idx_orders_status ON orders (status) INCLUDE (created_at, total);. A query that filters status and returns created_at and total can be answered from the index alone, which is much cheaper than following each index entry to a heap page.

TechniqueBenefitRequirement
Partial indexSmaller, cheaper to maintainQuery predicate implies the index predicate
Covering indexNo heap visitsAll needed columns in the index
INCLUDE columnsCovering without widening the keyPostgreSQL 11+, or a composite index
Index-only scanReads the index onlyVisibility map is current (VACUUM)

Writing a predicate the planner can match

The planner compares the query predicate with the index predicate logically, not textually. It can prove that status = 'paid' implies status = 'paid', and that status = 'paid' AND total > 0 implies the first. It cannot prove an implication through a function, a cast, or a parameter whose value is unknown at plan time. Write the predicate in the same simple form and avoid wrapping the column.

Partial indexes are especially useful for soft-deleted rows: WHERE deleted_at IS NULL on a table where most rows are active, or WHERE deleted_at IS NOT NULL for the archive query. They are also useful for a status that is rare but queried often, and for a queue table where only pending rows matter. The index stays small as the table grows with completed rows.

Making index-only scans actually happen

An index-only scan requires that every column referenced by the query is in the index, including columns in the WHERE clause, the SELECT list, and any JOIN or ORDER BY. It also requires that the visibility map marks the relevant pages all-visible, which VACUUM maintains. A table that is updated frequently has few all-visible pages, so the scan visits the heap and the benefit shrinks.

The plan reports Heap Fetches on an index-only scan. A small number relative to the rows returned is fine; a large number means the visibility map is stale. Running VACUUM more aggressively on a hot table, or reducing the update rate, improves it. Autovacuum settings per table are often the right lever, and they are cheaper than a wider index.

Costs and trade-offs

A covering index is wider than a key-only index, so it uses more storage and cache and takes more write time. It can be worth it for a query that runs constantly and returns many rows, because it removes a heap visit per row. For a query that returns few rows, the heap visit is already cheap and the wider index is a cost without much benefit. Measure the actual plan, not the idea.

A partial index has a subtle risk: a query whose predicate is almost but not exactly the index predicate cannot use it, and a query with a parameter may not be provable at plan time. Test with the real query text and, for prepared statements, check the generic plan. A partial index that is never used is pure write cost.

A checklist

The analyzer does not create indexes or know about existing ones. It can show that a scan reads many rows and returns few, or that a sort spills, which are the cases where a partial or covering index might help. The design still belongs to a person who knows the workload and the write cost.

The best index is the smallest one that answers the query. Partial and covering indexes are ways to make it smaller and more specific.

Sources

Questions

When does the planner use a partial index?

When the query's WHERE clause implies the index's WHERE clause, and the planner can prove it. A partial index on orders WHERE status = 'paid' can serve a query that filters status = 'paid', but not one that filters a different status. The predicate must be written in a form the planner recognises.

What is the difference between a covering index and a composite index?

A composite index has multiple key columns and is searched by a prefix. A covering index contains all the columns a query needs, either as keys or as INCLUDE columns, so the query can be answered from the index alone. INCLUDE columns are stored in the leaf entries but are not part of the search key.

Why does my index-only scan still visit the heap?

The visibility map marks pages where all rows are visible to all transactions. VACUUM maintains it. If the table has recent updates or has not been vacuumed, some pages are not all-visible and the scan must visit the heap to check visibility. The Heap Fetches count shows how often.

An index-only scan with few heap fetches is the sign a covering index is working.

Open the analyzer

Written against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.

Back to the guides