Sub topic 9
Choosing Key Columns versus Included Columns
Assume you know basic SQL, understand B-tree indexes at a high level, and can read a simple EXPLAIN plan. This chapter focuses on one design decision: which columns should control index navigation, and which columns should only provide query output.
A covering index contains every column needed by a query. PostgreSQL divides those columns into two groups:
- Key columns appear in the normal index key. PostgreSQL sorts them and can use them for search conditions, ordering, and uniqueness.
- Included columns are payload. They are stored in each index entry so an index-only scan can return them, but they do not control the index order.
For example, this index supports a lookup by customer_id and returns total without reading the table when visibility permits:
CREATE INDEX orders_customer_total_idx
ON orders (customer_id)
INCLUDE (total);
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;customer_id is the key because the query searches by it. total is included because the query returns it but does not search, sort, or group by it.
How key and included columns behave
| Column position | Helps find rows | Helps sort rows | Affects uniqueness | Can cover output |
|---|---|---|---|---|
| Key column | Yes | Yes | Yes in a unique index | Yes |
| Included column | No | No | No | Yes |
A key column should support the way the query locates rows. An included column should satisfy the query's SELECT list or other required data without becoming part of the search structure.
Consider a query that filters by customer and orders by creation time:
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at)
INCLUDE (total);
SELECT customer_id, created_at, total
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC;Here, customer_id and created_at are keys. PostgreSQL can navigate to one customer's entries and read them in creation-time order. Making created_at an included column would still cover the output, but it would not help with the ORDER BY. PostgreSQL might need an extra sort.
A practical selection process
Use this order when designing an index:
- Put columns used in equality or join conditions in the key.
- Add key columns needed for range conditions or ordering.
- Check the column order. B-tree indexes generally use the leading key columns most effectively.
- Put returned columns that do not affect navigation in
INCLUDE. - Test the result with
EXPLAIN (ANALYZE, BUFFERS).
Do not put every selected column in the key. Key columns make the index's ordering structure larger and can affect a unique constraint. Included columns usually communicate the intent more accurately: “store this value for coverage, but do not use it to identify or order entries.”
A column's selectivity matters when deciding whether it belongs in the key. A low-cardinality column such as status may be useful when combined with another selective condition or when it supports ordering. It is not automatically a good leading key just because it appears in WHERE.
Exercise
Exercise
For SELECT total FROM orders WHERE customer_id = 42, which column should be the key and which should be included?
Show answer
Use customer_id as the key and total in INCLUDE, because the query searches by the customer but only returns the total.