Skip to content

Lesson 5 of 13 · 13m

Composite Indexes and Column Order

Composite B-trees sort lexicographically — equality columns first, range last — and partial and expression indexes target the rows and spellings queries actually use.

A multi-column B-tree doesn't index "several columns." It indexes one composite key built by concatenating the columns in order — (customer_id, created_at) sorts by customer_id first, and within equal customers, by created_at. That ordering detail is the whole game, and it's why WHERE customer_id = 42 AND created_at > '2026-01-01' flies on (customer_id, created_at) while WHERE created_at > '2026-01-01' AND customer_id = 42 flies on the same index — the planner doesn't care about your clause order — but WHERE created_at > '2026-01-01' alone mostly can't use it at all.

The wrong model: column order inside the index doesn't matter

Two versions of this mistake exist. The first is "order doesn't matter, the database figures it out." The second is the folk rule "put the most selective column first," which is cargo cult advice — right by accident sometimes, wrong whenever it collides with the rule that actually governs composite B-trees.

The real rule: equality columns first, then the range column. The B-tree descends through equality on the leading columns until it lands in the contiguous region where the range column varies. WHERE a = ? AND b > ? wants (a, b). Reverse it to (b, a) and the index can bound b but must scan every a bucket inside that range — it still helps, just a fraction as much. Selectivity of the individual columns doesn't enter the decision; which column is equality and which is range does.

The bitmap-AND trap

Postgres can combine multiple single-column indexes: it scans status_idx and customer_id_idx separately, builds bitmaps of matching TIDs, and ANDs them before fetching heap pages. Watching BitmapAnd in EXPLAIN, people conclude that one index per column covers every combination. It doesn't. Bitmap AND discards combined ordering — you get the union of two independent lookups plus a bitmap merge and random heap fetches. It beats a seq scan; it loses to a purpose-built composite, and it can't feed ORDER BY at all. Bitmap combinations are the planner improvising because the composite index the query needed was never created.

Partial indexes: index the rows you query, not the table you have

CREATE INDEX ON orders (created_at) WHERE status = 'pending' indexes only pending rows. On a table where pending is 0.3% of rows, you get a tiny, hot index whose heap fetches are all clustered — and the planner only uses it when your query's WHERE can be proven to imply status = 'pending'. This is the standard fix for "the queue table is huge but the work queue is tiny." The wrong mental model here is "indexes must cover the whole table." They don't. The most valuable indexes in production are usually the ones scoped to the small subset everyone actually reads.

The mirror-image pattern: CREATE UNIQUE INDEX ON users (lower(email)) — an expression index — makes case-insensitive uniqueness enforceable and case-insensitive login lookups indexable. The query must spell the expression identically: WHERE lower(email) = lower(?) uses it; WHERE email ILIKE ? doesn't.

Sorting: the free win nobody takes

A composite index that matches both your WHERE and your ORDER BY eliminates the sort node entirely — rows emerge from the index already in order. WHERE customer_id = ? ORDER BY created_at DESC on (customer_id, created_at DESC) is an index range scan with no sort, no work_mem spill. When EXPLAIN shows Sort Method: external merge Disk: 84200kB, that query was one good composite index away from never sorting at all. Note the DESC matters for mixed orders: (a, b DESC) serves ORDER BY a, b DESC, not ORDER BY a, b.

What to actually do

  • Design composite indexes from the query shape: leading columns for equality predicates, trailing column for the range or sort.
  • When (a, b) exists, the standalone (a) index is dead weight — the composite serves every WHERE a = ? query. Drop the redundant one (verify with pg_stat_user_indexes first).
  • Audit BitmapAnd nodes in your hot-path plans. Each one marks a composite index you haven't built.
  • Queue/hot-subset tables get partial indexes; case-insensitive keys get expression indexes; sorted pagination gets a composite that matches ORDER BY direction exactly.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.