Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 17 / Topic 5 of 8

Covering indexes: INCLUDE

Part 16's index-only scan needed every selected column in the index. INCLUDE bolts extra columns onto the leaf pages — payload, not sort keys — exactly for that:

CREATE INDEX idx_users_status_cover ON big_users (status) INCLUDE (email);
VACUUM big_users;
EXPLAIN (COSTS OFF)
SELECT email FROM big_users WHERE status = 'pending';
CREATE INDEX
VACUUM
                        QUERY PLAN                         
-----------------------------------------------------------
 Index Only Scan using idx_users_status_cover on big_users
   Index Cond: (status = 'pending'::text)
(2 rows)

Index Only Scan — the query never touches the heap: the filter used the key, the SELECT was satisfied from the INCLUDE payload (visibility map permitting — Part 15's VACUUM earning its keep again). Why INCLUDE instead of a wider key (status, email)? The payload doesn't bloat the tree's sortable part, can hold types keys can't, and in UNIQUE indexes it adds columns without changing what's unique. Reserve it for genuinely hot query shapes — each INCLUDE column is leaf-page weight everyone pays.

🧪 Quick quiz — answer from memory, then check.

  1. What does INCLUDE add, and where?
  2. UNIQUE (email) INCLUDE (name) — what's unique?
Show the answers
  1. Non-key payload columns on the leaf pages — fetchable, not searchable/sortable.
  2. Still just email — INCLUDE columns don't participate in uniqueness.

Done here? The practice set for Part 17 — 5 exercises with solutions — is on the part page.

Search the guide