System design Level 2 This level · 16 of 48
Hard postgresql sql

How do you choose the column order of a composite index, and when do partial or covering indexes help?

The 30 second answer

A B-tree index is sorted by its first column, then the second, so put columns compared with equals first, then the one range or sort column. The docs say equality on leading columns plus a range on the next column limits the scan. A partial index covers only the hot rows, like open orders. A covering index with INCLUDE lets the query be answered from the index alone, an index-only scan, if the visibility map is current.

Could you say this in an interview?

From the basics. A B-tree index on (customer_id, created_at) is like a phone book sorted by last name, then first name. Finding "customer 42, last 30 days" is one jump to customer 42 and a short walk through their dates. Finding "all orders in the last 30 days" with the same index is like finding everyone named Anna in a phone book sorted by last name: the order doesn't help.

The rule, from the docs. "Equality constraints on leading columns, plus any inequality constraints on the first column that does not have an equality constraint, will always be used to limit the portion of the index that is scanned." Conditions on later columns are checked in the index but don't shrink the scan.

PostgreSQL 18 change. Version 18 added B-tree skip scan, which "allows multi-column btree indexes to be used in more cases such as when there are no restrictions on the first or early indexed columns". It helps most when the leading column has few distinct values. Don't rely on it in an interview answer for older versions.

Badly written. Range column first, so the equality on customer_id can't narrow the scan, and the sort needs an extra step:

CREATE INDEX orders_created_customer ON orders (created_at, customer_id);

SELECT id, status, created_at FROM orders
WHERE customer_id = 42 AND created_at >= now() - interval '30 days'
ORDER BY created_at DESC LIMIT 20;

Better. Equality first, then the range and sort column; INCLUDE the selected column so the heap isn't visited:

CREATE INDEX orders_customer_created ON orders (customer_id, created_at DESC) INCLUDE (status);

Partial index. When queries always target a small subset, index only that subset. The docs' example is unbilled orders that are "a small fraction of the total table and yet… the most-accessed rows":

CREATE INDEX orders_open_created ON orders (created_at) WHERE status IN ('NEW', 'PROCESSING');

SELECT id FROM orders WHERE status IN ('NEW', 'PROCESSING') ORDER BY created_at LIMIT 100;

The query's WHERE has to imply the index predicate, or the planner can't use it.

Covering and index-only scans. If every column the query needs is in the index, PostgreSQL can skip the table. It still checks the visibility map for each page; the docs explain that if a page isn't marked all-visible, the row must be fetched from the heap anyway. Regular vacuuming keeps the map current. Heap Fetches in EXPLAIN ANALYZE shows how often that happened.

Trade-offs.

  • Every index slows inserts and updates and takes space. Prefer one well-ordered composite index over several single-column ones.
  • Put the most selective equality column first only when columns are otherwise equivalent. The query shapes decide the order, not cardinality alone.
  • INCLUDE columns make the index bigger; add only what hot queries select.

Alternate interview scenarios

"Queries filter by brand, by brand and category, and by category alone. Which indexes?"

(brand_id, category_id) serves the first two. Category alone needs its own index, unless you're on PostgreSQL 18 and skip scan proves good enough in EXPLAIN for a low-cardinality brand column.

"The plan says Index Only Scan but Heap Fetches is almost the row count."

The visibility map is stale, typically on a heavily updated table. Check autovacuum on that table and consider tuning it.

Sources (5)
  1. PostgreSQL docs: Multicolumn indexespostgresql.org
  2. PostgreSQL docs: Partial indexespostgresql.org
  3. PostgreSQL docs: Index-only scans and covering indexespostgresql.org
  4. PostgreSQL docs: Using EXPLAIN (Heap Fetches)postgresql.org
  5. PostgreSQL 18 release notes (B-tree skip scan)postgresql.org
Previous
Next