Sub topic 3
Multicolumn Indexes
Assume you know basic SELECT, WHERE, ORDER BY, and CREATE INDEX. You should also know that an index can speed up a lookup by storing values in an order that PostgreSQL can search.
A multicolumn index, also called a composite index, stores more than one column in one B-tree structure. The column order is part of the index design. An index on (customer_id, created_at) is different from an index on (created_at, customer_id).
The leftmost-prefix rule
PostgreSQL searches a multicolumn B-tree from its first column toward later columns. It can use the leading, or leftmost, columns efficiently when the query constrains them.
For example, this index starts with customer_id and then orders each customer's rows by newest created_at:
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at DESC);This query constrains the first column, so PostgreSQL can navigate directly to customer 42:
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;The index can find the section for customer_id = 42 and read those entries in descending time order. It may avoid both a full table scan and a separate sort.
The same index is less useful for a query that does not constrain customer_id:
EXPLAIN
SELECT *
FROM orders
WHERE created_at > now() - interval '1 day';PostgreSQL may still choose an index scan in some situations, but it cannot jump directly to all recent created_at values because created_at is not the first index column.
| Query pattern | Possible useful index |
|---|---|
| `WHERE customer_id = ...` | `(customer_id)` |
| `WHERE customer_id = ... AND created_at > ...` | `(customer_id, created_at)` |
| `WHERE created_at > ...` | `(created_at)` |
| `WHERE customer_id = ... ORDER BY created_at DESC` | `(customer_id, created_at DESC)` |
Choosing column order
Start with the way the application filters and sorts. A common pattern is:
- Equality conditions, such as
customer_id = 42. - Range conditions, such as
created_at >= .... - Sort columns, such as
ORDER BY created_at DESC.
This is a useful rule, not an absolute law. A range condition can limit how effectively PostgreSQL uses columns that follow it for searching. Once the index scan covers a range of values, later columns may still help with ordering, but they usually cannot narrow the search as much as columns before the range.
For example:
CREATE INDEX orders_customer_status_created_idx
ON orders (customer_id, status, created_at DESC);This suits queries that identify one customer and one status, then retrieve the newest matching orders:
EXPLAIN
SELECT id, status, created_at
FROM orders
WHERE customer_id = 42
AND status = 'pending'
ORDER BY created_at DESC
LIMIT 20;An index does not need to contain every column in a query. Its key columns determine search and ordering. Extra selected columns can require table visits unless you use INCLUDE, which is covered later.
Equality, ranges, and ordering
Suppose a query always filters by customer_id, sometimes filters by status, and sorts by created_at. The best order depends on which conditions are consistently present and how selective they are.
A column is selective when its condition matches relatively few rows. If status has only three possible values, it may be less selective than customer_id. However, a consistently present equality condition can still be valuable before a sort column.
Use EXPLAIN with representative data and real query shapes. The planner's choice depends on table size, value distribution, statistics, and estimated costs. An index definition that looks correct can still lose to a sequential scan when the query returns a large part of the table.
Exercise
Which index is a better match for WHERE customer_id = 42 ORDER BY created_at DESC: (created_at, customer_id) or (customer_id, created_at DESC)? Why?
Show answer
(customer_id, created_at DESC) is the better match because the query constrains the leftmost column and can then read that customer's rows in the requested order.