Sub topic 11
Covering Queries with `INCLUDE`
Prerequisites and the main idea
This chapter assumes you know basic SELECT, WHERE, ORDER BY, and CREATE INDEX statements. It also assumes you can read the main parts of a simple PostgreSQL EXPLAIN plan.
An index normally stores its key columns in sorted order. PostgreSQL can use those keys to find matching table rows. Sometimes the query also needs columns that are not part of the search condition. An INCLUDE column stores those extra values in the index without making them part of the index's search order.
An index that contains every column needed by a query is called a covering index. PostgreSQL may then use an index-only scan. An index-only scan reads data from the index without fetching each matching row from the table.
CREATE INDEX orders_customer_cover_idx
ON orders (customer_id)
INCLUDE (total_cents);
EXPLAIN
SELECT customer_id, total_cents
FROM orders
WHERE customer_id = 42;Here, customer_id is the key column. PostgreSQL uses it to search the index. total_cents is an included column. It is returned from the index, but it does not help PostgreSQL locate rows or provide ordering.
How an index-only scan works
An index-only scan is possible only when the index has all columns needed by the query. PostgreSQL must also know that the indexed pages contain no table rows that are invisible to the current transaction.
PostgreSQL records this information in a structure called the visibility map. If the relevant table pages are marked all-visible, PostgreSQL can trust the index contents. Otherwise, it may visit the table to check row visibility. The plan can then become an ordinary index scan, even though the index contains all requested columns.
You can inspect the chosen plan with EXPLAIN (ANALYZE, BUFFERS). ANALYZE runs the query, so use it carefully for queries that modify data. This read-only query is safe for normal testing.
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total_cents
FROM orders
WHERE customer_id = 42;Look for Index Only Scan. The plan may also show Heap Fetches. A value greater than zero means PostgreSQL had to visit the table for some visibility checks. A plan can therefore use an index-only scan while still performing heap fetches.
What `INCLUDE` can and cannot do
| Column kind | Can locate matching rows? | Can provide sort order? | Can provide output values? |
|---|---|---|---|
| Index key | Yes | Yes | Yes |
| `INCLUDE` column | No | No | Yes |
Use INCLUDE for columns that a query returns but does not filter or sort by. If a column appears in WHERE or ORDER BY, it usually belongs among the key columns instead.
Included columns make an index larger. Inserts and updates must maintain those stored values, and updates to included columns can create additional index work. Do not include every selected column automatically. Cover frequent, selective queries where avoiding table reads is worth the storage and write cost.
An included column also cannot replace a key column when uniqueness matters. A unique index checks uniqueness using its key columns; included values do not participate in that rule.
Exercise
In the covering index on orders, why is customerid a key column while totalcents is an INCLUDE column?
Show answer
customerid is used to find matching rows. totalcents is only returned, so storing it in the index can cover the query without making it part of the search order.