← Back to PostgreSQL, page 2

postgresql-0039 · Medium mid

Get an index-only scan with a covering index using INCLUDE.

An index-only scan answers a query from the index without visiting the table, which needs every selected column in the index and pages marked all-visible by VACUUM. INCLUDE adds extra columns to the index leaf entries without making them part of the search key. It suits queries that look up by one column and return a few others.

Full answer

Schema and sample rows:

sql
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);
sql
VACUUM ANALYZE orders;

Result on PostgreSQL 17:

text
VACUUM
sql
EXPLAIN (COSTS OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;

Result:

text
Bitmap Heap Scan on orders
  Recheck Cond: (customer_id = 42)
  ->  Bitmap Index Scan on orders_customer
        Index Cond: (customer_id = 42)
sql
CREATE INDEX orders_customer_cov ON orders (customer_id) INCLUDE (total);

Output:

text
CREATE INDEX
sql
VACUUM ANALYZE orders;

PostgreSQL returns:

text
VACUUM
sql
EXPLAIN (COSTS OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;

Result on PostgreSQL 17:

text
Index Only Scan using orders_customer_cov on orders
  Index Cond: (customer_id = 42)
sql
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT customer_id, total FROM orders WHERE customer_id = 42;

Result:

text
Index Only Scan using orders_customer_cov on orders (actual rows=40 loops=1)
  Index Cond: (customer_id = 42)
  Heap Fetches: 0

VACUUM 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.

sql
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