Full answer
Covering index
- Normally the index finds row locations and the engine fetches each row from the table (random reads). If the index also holds the selected columns, no table visit is needed.
- CREATE INDEX ON orders (customer_id) INCLUDE (total) keeps total as payload. Use INCLUDE for columns you only select, not filter, so the search key stays small.
- In PostgreSQL an index-only scan also needs the visibility map to be current (VACUUM), otherwise it still fetches heap pages. EXPLAIN ANALYZE shows Heap Fetches.
Partial index
- CREATE INDEX ON orders (created_at) WHERE status = 'pending'. The index holds only pending rows. Queries must contain a condition that implies the index predicate (status = 'pending') for the planner to use it.
- Typical uses: queue tables, soft deleted rows (WHERE deleted_at IS NULL), unique constraints on a subset (UNIQUE (email) WHERE active).
Costs and cautions
- Wider indexes use more memory and slow writes. Do not cover everything.
- Updates to included columns update the index.
- Partial index predicates are fixed at creation, and parameterised queries may not match them.
SET client_min_messages = warning;
DROP TABLE IF EXISTS idx_orders;
CREATE TABLE idx_orders (id serial PRIMARY KEY, customer_id int NOT NULL, status text NOT NULL, created_at timestamptz NOT NULL, total numeric(10,2) NOT NULL);
INSERT INTO idx_orders (customer_id, status, created_at, total)
SELECT (g % 2000) + 1,
CASE WHEN g % 200 = 0 THEN 'pending' WHEN g % 3 = 0 THEN 'shipped' ELSE 'paid' END,
timestamptz '2026-01-01' + (g || ' minutes')::interval,
(g % 500) + 10
FROM generate_series(1, 100000) g;
CREATE INDEX idx_orders_cust ON idx_orders (customer_id);
VACUUM ANALYZE idx_orders;
\echo 1. ordinary index: each row is fetched from the table
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT customer_id, total FROM idx_orders WHERE customer_id = 5;
DROP INDEX idx_orders_cust;
CREATE INDEX idx_orders_cust_cov ON idx_orders (customer_id) INCLUDE (total);
VACUUM ANALYZE idx_orders;
\echo 2. covering index: an index-only scan with no heap fetches
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT customer_id, total FROM idx_orders WHERE customer_id = 5;
CREATE INDEX idx_orders_created_all ON idx_orders (created_at);
CREATE INDEX idx_orders_created_pending ON idx_orders (created_at) WHERE status = 'pending';
ANALYZE idx_orders;
\echo 3. a query that matches the partial index condition
EXPLAIN (COSTS OFF) SELECT * FROM idx_orders WHERE status = 'pending' ORDER BY created_at LIMIT 5;
\echo 4. the partial index is much smaller than the full one
SELECT pg_relation_size('idx_orders_created_pending') * 10 < pg_relation_size('idx_orders_created_all') AS partial_is_under_a_tenth;
DROP TABLE idx_orders;Run with psql on PostgreSQL 17. EXPLAIN ANALYZE is used with timing off so the output is stable. The partial index is about 1 percent of the rows, so the size comparison is safe. Save it as app.sql and run it with psql -f app.sql. Output (PostgreSQL 17 via psql, run once): 1. ordinary index: each row is fetched from the table QUERY PLAN --------------------------------------------------------------------- Bitmap Heap Scan on idx_orders (actual rows=50 loops=1) Recheck Cond: (customer_id = 5) Heap Blocks: exact=50 -> Bitmap Index Scan on idx_orders_cust (actual rows=50 loops=1) Index Cond: (customer_id = 5) (5 rows) 2. covering index: an index-only scan with no heap fetches QUERY PLAN ---------------------------------------------------------------------------------- Index Only Scan using idx_orders_cust_cov on idx_orders (actual rows=50 loops=1) Index Cond: (customer_id = 5) Heap Fetches: 0 (3 rows) 3. a query that matches the partial index condition QUERY PLAN ----------------------------------------------------------------- Limit -> Index Scan using idx_orders_created_pending on idx_orders (2 rows) 4. the partial index is much smaller than the full one partial_is_under_a_tenth -------------------------- t (1 row)
Key points
- Covering indexes answer queries without visiting the table.
- INCLUDE adds payload columns that are not search keys.
- Index-only scans need an up-to-date visibility map.
- Partial indexes cover a subset of rows and stay small.
- Use them for hot, repeated queries.
Common mistakes
- Including too many columns and making a huge index.
- Expecting an index-only scan on a table that was never vacuumed.
- Writing a query that does not imply the partial index predicate.
- Using INCLUDE for columns that are used in filters.
- Forgetting the write cost of wide indexes.
Follow-ups
- What is the visibility map?
- How does a unique partial index enforce 'one active row per user'?
- How would you find the queries that deserve a covering index?
Tips
- Explain 'no table visit'.
- Give one use case for a partial index.
- Mention VACUUM.