Sub topic 3
Covering indexes and index-only scans
This chapter assumes you know basic SQL, B-tree indexes, reading EXPLAIN output, and PostgreSQL syntax.
Covering indexes and index-only scans
An index stores its key columns plus a pointer to each table row. If a query needs only columns held in the index, PostgreSQL can answer it from the index alone. This is an index-only scan. The INCLUDE clause adds non-key columns to the index:
CREATE INDEX orders_customer_covering_idx
ON orders (customer_id) INCLUDE (total_cents);
EXPLAIN
SELECT customer_id, total_cents FROM orders WHERE customer_id = 42;An index-only scan also depends on the visibility map. Pages changed recently still need a table check, and VACUUM updates that map.
Unique indexes and constraints
A unique index rejects duplicate values. PRIMARY KEY and UNIQUE constraints create one automatically.
CREATE UNIQUE INDEX users_email_uidx ON users (email);What indexes cost on writes and in storage
Each INSERT writes to every index on the table. An UPDATE writes to every index whose columns changed. Indexes also consume disk space and buffer memory. Create only the indexes that your queries use.
Partial and expression indexes
A partial index covers only the rows that match a WHERE condition. An expression index stores the result of a function or calculation. A query can use either one only when its predicate matches the index definition.
CREATE INDEX orders_open_created_idx
ON orders (created_at) WHERE status = 'open';
CREATE INDEX users_lower_email_idx ON users (lower(email));
SELECT * FROM users WHERE lower(email) = '[email protected]';Hash, GIN, GiST, BRIN, and other index types
B-tree indexes handle most equality and range queries. Other index types suit specific data:
| Index type | Typical use |
|---|---|
| Hash | Equality comparisons only |
| GIN | Arrays, JSONB, and full-text search |
| GiST | Geometry, ranges, and nearest-neighbor searches |
| BRIN | Very large tables where values follow physical row order, such as timestamps |
Statistics, bloat, and maintaining indexes
The planner chooses plans from table statistics. Run ANALYZE after large data changes. Heavy updates and deletes leave indexes bloated, so rebuild them without blocking writes:
ANALYZE orders;
REINDEX INDEX CONCURRENTLY orders_customer_id_idx;To find indexes that no query has used since statistics were last reset:
SELECT indexrelname, idx_scan
FROM pg_stat_user_indexes
WHERE schemaname = 'public'
ORDER BY idx_scan ASC;A practical approach to choosing indexes
Start from your slowest queries and list their WHERE, JOIN, and ORDER BY columns. Check each candidate with EXPLAIN. Prefer one composite index that serves several queries. Measure the write cost before adding more indexes, and remove any index that no query uses.
Exercise
The query SELECT id FROM orders WHERE status = 'open' AND created_at > '2026-01-01' runs often, and only a small share of orders are open. Which index would you create?
Show answer
A partial index that also includes the selected column: CREATE INDEX orders_open_created_idx ON orders (created_at) INCLUDE (id) WHERE status = 'open';. It stays small because it covers only open orders, and the query can be answered from the index alone.