Sub topic 7
Designing a Covering Index
This chapter assumes basic SQL, familiarity with B-tree indexes, and the ability to read a simple EXPLAIN plan. The examples use an existing PostgreSQL table named orders.
A covering index contains every column needed to answer a query. PostgreSQL can then read the result from the index without fetching rows from the table heap. This can make frequent, selective queries faster, especially when the table rows are wide.
Start with the query
Design the index from a real query, not from the table definition. Consider this query:
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;The query uses customer_id to find rows and returns total. A basic index contains only the search column:
CREATE INDEX orders_customer_idx
ON orders (customer_id);This index helps PostgreSQL find matching rows. However, PostgreSQL must still visit the heap to read total. The index does not contain that value.
A covering index stores both columns:
CREATE INDEX orders_customer_total_idx
ON orders (customer_id) INCLUDE (total);Here, customer_id is the key column. total is an included, or payload, column. The index can find matching entries using customer_id and return total directly.
Choose key columns and payload columns
Key columns control how PostgreSQL searches and orders the index. Put columns used by equality, range, join, or ordering conditions in the key portion.
Payload columns are values returned by the query but not used to search or order the index. Put those columns in INCLUDE.
| Query role | Index location | Example |
|---|---|---|
| Filter, join, or sort column | Key | `customer_id` |
| Returned value only | `INCLUDE` payload | `total` |
| Column requiring index ordering | Key | `created_at` |
| Large value returned occasionally | Usually neither | `note` |
For example, this query filters by customer and orders by creation time:
SELECT id, total, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC;A suitable index is:
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at DESC)
INCLUDE (id, total);The key columns support both the filter and the ordering. The included columns provide the selected values.
Understand the trade-off
Every included column makes the index larger. Larger indexes require more storage, take longer to build, and create more work when rows are inserted or updated. Include only columns that support an important query.
An included column also does not improve searching. This index does not efficiently support a query filtering only by total:
CREATE INDEX orders_customer_total_idx
ON orders (customer_id) INCLUDE (total);The planner cannot use total as an index search key merely because it appears in the index.
Verify the design
Use EXPLAIN (ANALYZE, BUFFERS) to test the actual query:
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;Look for Index Only Scan. Check Heap Fetches as well. A low value, especially zero, means PostgreSQL answered most or all visibility checks without reading heap tuples. If the plan chooses another scan type, investigate table size, estimated row counts, statistics, visibility-map coverage, and whether the index is selective enough.
Exercise
In an index on (customer_id) INCLUDE (total, status), which column can PostgreSQL use to search, and which columns are payload?
Show answer
PostgreSQL can search using customer_id. The total and status columns are payload columns that can be returned without being search keys.