multicolumn indexes
column order is everything. the leftmost prefix, equality first, range last.
- address
- everything.indexes.composite_indexes
- part
- part 8, indexes, from the outside
- chapter
- chapter 3 of 12
- reading time
- 7 min
an index on (a, b) is sorted by a, and by b only among rows with the same a. so it narrows a scan for a, for a and b, and for a range on b once a is fixed. that's the leftmost prefix. for b on its own, the matches are scattered across the whole index.
the rule of thumb that falls out of it: equality columns first, the range column last.
the picture
you know the phone book? sorted by last name, then first name. finding every Patel is easy: they're all together. finding Ravi Patel is easy too. finding everyone called Ravi means reading the whole book, because the Ravis are scattered one per surname.
an index on (a, b) is that phone book. here's a real one, 240 entries: six values of a, forty of b. each row of the grid is one value of a, and reading the rows left to right, top to bottom, is reading the index in order. pick a query.
a = 3: one descent to the first (3, 1), then read forward until a changes.
a = 3, for a = 3 AND b > 30 and for a = 3 AND b = 7. for b = 7 alone they're scattered, one per value of a, across every leaf page.the first three need one trip down the tree (Index Searches: 1) and then a short walk to the right, stopping when the run ends. the index is built with fillfactor = 10, so 240 entries take seven leaf pages instead of one, and the grid has page boundaries worth seeing.
b = 7 is different. its six entries sit in six different places. postgres 18 can still use the index for it, by hopping: one trip down the tree per value of a (the figure says Index Searches: 7). that's a skip scan, and it only pays off because a has six values here. before postgres 18, the choice was reading the whole index or the whole table.
the leftmost prefix
shop already has a composite index: order_items' primary key is (order_id, line_no). the leading column works on its own:
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM order_items WHERE order_id = 42;both columns, one descent, one entry:
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM order_items WHERE order_id = 42 AND line_no = 2;EXPLAIN SELECT * FROM order_items WHERE line_no = 4;EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM order_items WHERE line_no = 4;the manual states the exact rule: equality conditions on the leading columns, plus a range condition on the first column without an equality, are what limit the part of the index that's scanned. conditions on columns after that are still checked inside the index (so they save heap visits), but they don't shrink the scan.
- ├── WHERE a = 1 AND b = 2 AND c > 3all three narrow the scan
- ├── WHERE a = 1 AND b > 2 AND c = 3a and b narrow it, c is checked
- ├── WHERE a > 1 AND b = 2a narrows it, b is checked (or hopped, in 18)
- └── WHERE b = 2 AND c = 3nothing leads: a skip scan, or no index
equality first, range last
the order of the columns decides which of those you get. say you look up cancelled orders since january, all the time. two candidate indexes: (status, ordered_at) and (ordered_at, status). same columns, same size.
equality first: status = 'cancelled' picks one slice of the index, and the date range is a contiguous run inside it.
equality on status jumps to the one slice of the index where status is 'cancelled'. inside it, entries are sorted by date, so ordered_at >= '2026-01-01' is a single run from a starting point to the end of the slice. flip the columns and the date range comes first: the scan starts at january 1 and reads every order from there on, cancelled or not (4,906 of them), testing the status of each.
so: columns you compare with = go first, the one you compare with <, > or BETWEEN goes last. if there are several equality columns, their order among themselves matters less for this query; pick it for the other queries that should share the index (the most common one leading).
what else the order decides
- ORDER BY. an index on
(customer_id, ordered_at)hands back one customer's orders already sorted by date, soWHERE customer_id = 42 ORDER BY ordered_at DESC LIMIT 3needs no sort at all. indexes for ORDER BY has the rules for directions and NULLs. - covering. extra columns at the end can make an index-only scan possible. if they're never searched on,
INCLUDEthem instead of adding them as keys. - the prefix comes free. an index on
(a, b)also servesWHERE a = ..., so a separate index onaalone is usually dead weight (what indexes cost). the reverse isn't true.
how the tree stores the two columns (one key, compared column by column, with suffix truncation in the internal pages) is in a real b-tree, page by page.
keylines
- an index on (a, b) is sorted by a, then by b inside each a.
- it narrows scans on a leftmost prefix: a, or a and b.
- b alone is scattered across the index. postgres 18 can hop it if a has few values.
- equality columns first, the range column last.
- one composite index beats two single-column ones for queries that use both.