Sub topic 13
Partial Covering Indexes
Prerequisites and the main idea
This chapter assumes that you know basic SQL, understand B-tree indexes at a high level, and can read a simple EXPLAIN plan.
A covering index contains every column needed by a query. PostgreSQL can then answer the query from the index without reading the table heap. A partial index contains only rows that satisfy a condition. A partial covering index combines both ideas: it stores a selected set of rows and includes the columns needed by common queries.
Suppose an orders table contains one million orders, but most application requests ask only about orders whose status is new:
CREATE INDEX orders_new_customer_idx
ON orders (customer_id) INCLUDE (total)
WHERE status = 'new';Here, customer_id is the search key. total is included payload. The predicate status = 'new' limits which rows enter the index.
Making the planner use the index
The query must contain a condition that proves the partial-index predicate applies:
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE status = 'new'
AND customer_id = 42;The planner can see that every matching row must satisfy status = 'new', so it can use orders_new_customer_idx. Because the index contains both selected columns, an Index Only Scan is possible. The visibility map still matters. Pages that are not marked all-visible can produce heap fetches.
The predicate must be logically implied by the query. A query without the condition cannot use this index safely:
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;This query might match orders with any status. The partial index contains only new orders, so using it would omit valid results.
Predicate matching is stricter than it looks
The planner recognizes many simple logical relationships, but it does not prove every equivalent expression. Keep the query predicate and index predicate clear and consistent.
For example, this index uses a partial condition:
CREATE INDEX orders_paid_customer_idx
ON orders (customer_id) INCLUDE (total)
WHERE status = 'paid';A query with WHERE status = 'paid' directly matches it. A query using a parameter may be harder for the planner to match:
PREPARE orders_for_status(text, integer) AS
SELECT customer_id, total
FROM orders
WHERE status = $1
AND customer_id = $2;A generic prepared plan does not know that $1 will always be 'paid'. It may therefore choose another index or a sequential scan. Check the actual plan rather than assuming that the partial index will be selected.
When a partial covering index is useful
A partial covering index is a good fit when:
- A small, stable subset of rows receives most of the reads.
- Queries consistently include the partial predicate.
- The included columns make heap visits avoidable.
- The index would otherwise be much larger than its useful working set.
It is less useful when the predicate matches most rows, changes frequently, or is absent from normal queries. Remember that updates can move rows into or out of the index when the predicate column changes.
Verify the benefit
Use EXPLAIN (ANALYZE, BUFFERS) before and after creating the index. Look for the index name, the scan type, execution time, and Heap Fetches. Zero or near-zero heap fetches indicate that the visibility map allowed the index-only scan to avoid most heap visits.
Exercise
Design an index for a query that returns customer IDs and totals only for shipped orders, filtered by customer ID. Which column is the key, which is payload, and which condition is partial?
Show answer
Use CREATE INDEX orders_shipped_customer_idx ON orders (customer_id) INCLUDE (total) WHERE status = 'shipped';. The key is customer_id, total is payload, and status = 'shipped' is the partial predicate.