Sub topic 8
Adding Payload Columns with `INCLUDE`
This chapter assumes you know basic SELECT, WHERE, CREATE INDEX, B-tree indexes, and simple EXPLAIN output. You should also understand that PostgreSQL stores table rows in a heap and uses a visibility map to decide whether an index-only scan can skip heap checks.
What `INCLUDE` adds
A covering index contains every column needed by a query. PostgreSQL can then read the result from the index without retrieving the table row from the heap.
The INCLUDE clause adds payload columns to an index:
CREATE INDEX orders_customer_total_idx
ON orders (customer_id)
INCLUDE (total);Here, customer_id is the key column. PostgreSQL uses it to search the index. total is an included column. PostgreSQL stores it in the index entry, but does not use it to order or search the B-tree.
This index can support the following query:
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;The query filters by customer_id and returns total. Both columns are available in the index.
Key columns versus payload columns
The distinction matters because B-tree keys affect the structure and behavior of the index.
| Column role | Example | Used for index search and ordering? | Used to cover a query? |
|---|---|---|---|
| Key | `customer_id` | Yes | Yes |
| Payload | `total` | No | Yes |
Suppose a query filters and sorts by two columns:
SELECT customer_id, created_at, total
FROM orders
WHERE customer_id = 42
ORDER BY created_at;An index such as this may be appropriate:
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at)
INCLUDE (total);customer_id and created_at are keys because they control filtering and ordering. total is payload because the query only returns it.
The planner can use the index for an index-only scan only when all referenced columns are present. It may still choose another scan type if the query returns many rows or if heap visibility requires too many checks.
A complete test
The following example assumes the orders table from the earlier setup exists:
DROP INDEX IF EXISTS orders_customer_idx;
DROP INDEX IF EXISTS orders_customer_total_idx;
CREATE INDEX orders_customer_total_idx
ON orders (customer_id)
INCLUDE (total);
VACUUM (ANALYZE) orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;After VACUUM, many pages can be marked all-visible. If the planner chooses an index-only scan, the plan should contain a line similar to:
Index Only Scan using orders_customer_total_idx on orders
Index Cond: (customer_id = 42)
Heap Fetches: 0Heap Fetches: 0 means PostgreSQL answered the query without visiting heap pages for these rows. A value greater than zero means PostgreSQL had to check some heap tuples because their pages were not known to contain only visible rows.
What `INCLUDE` does not do
Included columns do not participate in uniqueness checks. For example:
CREATE UNIQUE INDEX orders_id_status_idx
ON orders (id)
INCLUDE (status);This index rejects duplicate id values. It does not reject two rows with the same id and different status; id alone determines uniqueness.
Included columns also increase index size. Every insert can add their values to the index, and updates may require index maintenance. Large or frequently changing payload columns can therefore make writes and storage more expensive.
Use INCLUDE when a measured read benefit justifies that cost. Keep filtering and ordering columns in the key, and place result-only columns in the payload.
Exercise
Write an index that supports SELECT status, total FROM orders WHERE id = 7 and enforces unique id values.
Show answer
CREATE UNIQUE INDEX orders_id_covering_idx ON orders (id) INCLUDE (status, total);