Sub topic 4
Why Column Order Matters in PostgreSQL Indexes
This chapter assumes you know basic SELECT, WHERE, ORDER BY, and CREATE INDEX statements. You should also understand that an index is a separate, ordered data structure that PostgreSQL can search instead of scanning every table row.
The leftmost-prefix rule
A multicolumn index stores keys in the order you declare them. Consider this index:
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at DESC);PostgreSQL first orders entries by customer_id. Within each customer, it orders entries by created_at from newest to oldest.
The first column is the leading column. The index is most useful when a query restricts that column. PostgreSQL can then navigate directly to the relevant section of the index.
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;This query constrains the leading column, so PostgreSQL can find the entries for customer 42 efficiently.
The same index also supports a customer-specific time range:
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42
AND created_at >= now() - interval '7 days';The customer_id condition identifies a narrow part of the index. PostgreSQL can then search the created_at values inside that part.
A useful model is a sorted address book. You can quickly find every person in a particular city, then search by last name within that city. You cannot efficiently search by last name alone if the book is sorted by city first.
Choosing column order
Column order should follow the query workload, not simply the order of columns in a table. These patterns are common:
| Query need | Often useful index order |
|---|---|
| Equality, then sorting or range | `(customer_id, created_at)` |
| Equality on two columns | `(tenant_id, status)` |
| Range on one column | Put the frequently used equality column first |
| Sorting for one customer | `(customer_id, created_at DESC)` |
An index on (created_at, customer_id) is different from an index on (customer_id, created_at). The first can efficiently find a time range across all customers. The second can efficiently find one customer's orders and then examine them by time.
-- Good for one customer's newest orders.
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at DESC);
SELECT id, status, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;The index order matches both the filter and the requested ordering. PostgreSQL may return the first 20 matching entries directly from the index without a separate sort.
Equality, range, and sort conditions
A practical design rule is:
- Put columns used with equality conditions early.
- Put a range condition after those equality columns.
- Use the remaining ordering columns to support
ORDER BY, when useful.
For example, this index fits a query that selects one tenant, one status, and then sorts by creation time:
CREATE INDEX orders_tenant_status_created_idx
ON orders (tenant_id, status, created_at DESC);
SELECT id, total_cents
FROM orders
WHERE tenant_id = 7
AND status = 'pending'
ORDER BY created_at DESC
LIMIT 50;A range condition can limit how much of the later index ordering PostgreSQL can use. After finding entries such as created_at > ..., the planner may still need additional work for later columns.
Common design errors
An index with many columns is not automatically better. It takes more storage and makes inserts, updates, and deletes more expensive. Also, an index whose leading column is rarely constrained may not help the queries you care about.
If your workload needs both of these access patterns, one index may not serve both efficiently:
-- One customer's recent orders:
WHERE customer_id = 42
ORDER BY created_at DESC
-- Recent orders for every customer:
WHERE created_at > now() - interval '1 day'The first favors (customer_id, created_at). The second may favor an index beginning with created_at.
Exercise
Which index order better supports 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 better because PostgreSQL can first locate customer 42 and then read that customer's entries in newest-first order.