Full answer
Schema and sample rows:
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, status text, total numeric(10,2), notes text);
INSERT INTO orders SELECT g, g % 500, CASE WHEN g % 3 = 0 THEN 'paid' ELSE 'new' END, g % 1000, repeat('x', 50) FROM generate_series(1, 20000) g;
CREATE INDEX orders_customer ON orders (customer_id);VACUUM ANALYZE orders;Result on PostgreSQL 17:
VACUUMEXPLAIN (COSTS OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;Result:
Bitmap Heap Scan on orders
Recheck Cond: (customer_id = 42)
-> Bitmap Index Scan on orders_customer
Index Cond: (customer_id = 42)CREATE INDEX orders_customer_cov ON orders (customer_id) INCLUDE (total);Output:
CREATE INDEXVACUUM ANALYZE orders;PostgreSQL returns:
VACUUMEXPLAIN (COSTS OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;Result on PostgreSQL 17:
Index Only Scan using orders_customer_cov on orders
Index Cond: (customer_id = 42)EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;Result:
Index Only Scan using orders_customer_cov on orders (actual rows=40 loops=1)
Index Cond: (customer_id = 42)
Heap Fetches: 0VACUUM runs as a separate statement because it cannot run inside a transaction block, and a multi-statement script is one implicit transaction. With only customer_id indexed, the plan must fetch table rows to read total. After adding the covering index, the plan becomes an Index Only Scan. EXPLAIN ANALYZE shows 'Heap Fetches: 0' because VACUUM marked every page all-visible; on a table with many recent changes, heap fetches rise and the benefit shrinks until autovacuum catches up. INCLUDE columns cannot be used for searching or ordering, and every included column makes the index larger, so include only what hot queries need.
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, status text, total numeric(10,2), notes text);
INSERT INTO orders SELECT g, g % 500, CASE WHEN g % 3 = 0 THEN 'paid' ELSE 'new' END, g % 1000, repeat('x', 50) FROM generate_series(1, 20000) g;
CREATE INDEX orders_customer ON orders (customer_id);
VACUUM ANALYZE orders;
EXPLAIN (COSTS OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;
CREATE INDEX orders_customer_cov ON orders (customer_id) INCLUDE (total);
VACUUM ANALYZE orders;
EXPLAIN (COSTS OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- Index-only scans skip the table
- All selected columns must be in the index
- INCLUDE adds non-key columns
- The visibility map decides heap fetches
- Keep included columns few
Common mistakes
- Expecting index-only scans on freshly changed tables
- Including wide columns
Follow-ups
- How does the visibility map work?
- When is a multicolumn key better than INCLUDE?
Tips
- Show Heap Fetches