The index contains both columns. The query is still slow. A customer-service page asks for one customer’s newest orders; an index contains customer ID and order date, so the team assumes the lookup is covered. Yet an index beginning with date may scan a broad stretch of recent orders across many customers. Another beginning with customer ID can make a very different trip through the same list of columns.
A multicolumn index orders its keys in a defined sequence. For a PostgreSQL B-tree, equality conditions on leading columns, followed by a range on the first column without equality, provide a particularly useful boundary for an index scan. The current PostgreSQL documentation also describes cases where conditions on later keys can be used. PostgreSQL 18 and later can use B-tree skip scan in some cases; older releases do not have that optimization. “Leftmost” is a useful starting question, not a rule that an index is unusable whenever the first column is absent.
Follow one page request through the index
Consider an orders table with a customer identifier, creation timestamp, and order ID. The service asks for the most recent 25 orders for one customer. With a B-tree ordered by customer ID and then timestamp, entries for that customer sit together in timestamp order within the customer’s range. A query with an equality condition on the customer and a matching order can potentially start at the useful end of that range and stop after enough qualifying rows are found.
Reverse the key order: timestamp first, customer second. That may better suit a different question, such as orders across all customers during a short time window. For one customer, the engine might have to consider more date-ordered entries before finding the desired customer’s rows. The actual number depends on data distribution, filtering, the query, and the plan. This is a comparison of access patterns, not a promise that one index will always be selected.
Even the phrase “newest orders” needs detail. Does the query request descending order? Can multiple orders have the same timestamp? A stable page often needs a tie-breaker such as order ID in both ordering and cursor logic. A filter or join can change how quickly qualifying rows appear. Test the exact query and expected parameters rather than a simplified statement that omits the expensive part.
Equality, then a range: a boundary with consequences
Consider a three-key B-tree on tenant, status, and created time. A query specifying one tenant and one status, then a date range, gives the index an equality prefix and a time boundary. That can narrow the part of the ordered index the engine examines. The index-type guide explains the comparison and ordering operations supported by B-trees.
Now ask for a tenant’s rows over a date range without specifying status. There is a gap between the leading tenant condition and the time condition. PostgreSQL may still use the index, inspect entries, or employ a skip-scan strategy where its cost model finds that useful. The point is not to guess a plan from a slogan; it is to notice the missing condition before assuming that the time predicate creates the same narrow contiguous range as in the first query.
Range predicates also change the shape. With an index on tenant, created time, and status, equality on tenant and a range on created time can constrain the scan, while a condition on status may filter entries inside that range rather than reduce it in the same way as an equality key before the range. Current PostgreSQL versions can make cost-based choices that are more nuanced than this simplified description. Read the plan and current documentation for the installed version.
Same columns, different jobs
An index on customer and date is not automatically equivalent to an index on date and customer. Nor does that mean every permutation deserves its own index. Each index consumes storage and must be maintained as rows are inserted, updated, or deleted. A design that makes one report faster can make a busy checkout write more work, increase backup size, or use memory needed elsewhere.
Start with the workload. List the frequent queries, their equality and range conditions, sort order, and the proportion of reads and writes. Record a representative small customer and a very large customer: a plan that looks good for one can behave differently for the other. A daily report can tolerate a different response time and index cost from a page called on every keystroke.
Separate single-column indexes can sometimes be combined. PostgreSQL’s bitmap-scan documentation explains how it can combine indexes and why this differs from the ordering a single index may provide. Comparing candidates means measuring the whole query, including a sort if the chosen path does not supply the requested order.
What the plan can—and cannot—tell you
Use EXPLAIN to inspect the planner’s proposed operations and estimates. Where the query and environment permit a controlled execution, EXPLAIN ANALYZE runs the statement and reports actual behavior. PostgreSQL’s EXPLAIN guide notes that execution has overhead and that data-changing statements actually make changes unless handled appropriately. Do not run a write against production merely to produce a screenshot.
Compare estimated and actual rows, the number of rows removed by filters, sort work, buffer activity when requested, and total time in context. An index scan is not automatically superior to a sequential scan: reading a large fraction of a table through scattered lookups can cost more than reading it sequentially. A plan also reflects statistics and configuration at the time. Run representative tests, including common and unusual parameter values, and refresh statistics through the normal maintenance process when appropriate.
Suppose an existing index takes 800 milliseconds for a report, while a candidate takes 90 milliseconds in one controlled run. That is an encouraging measurement, not yet a production guarantee. Check cold and warm behavior where relevant, concurrent writes, other queries, and the cost of creating and maintaining the index. Do not assume the difference will survive a changed dataset or a different customer.
The sort direction is part of the request
Return to the customer who wants the newest 25 orders. If the customer has 200,000 orders, reading a large range and sorting it just to return 25 is a different job from reaching the useful end of an ordered range and stopping early. A matching index can sometimes let the database avoid that separate sort. But the useful answer is in the plan: the engine still has to check any conditions that are not satisfied by the index, and it may read well beyond 25 entries to find 25 qualifying rows.
For pagination, imagine two orders share the same creation timestamp. A cursor that stores only that timestamp cannot distinguish those orders. The next page needs a consistent ordering rule that breaks ties, such as creation time plus a unique order ID, and a cursor condition consistent with that rule. Otherwise, depending on the implementation, an order can be repeated or skipped at a page boundary. The index is there to support the ordering the application actually uses, not to repair an ambiguous cursor after the fact.
Descending order and null placement also matter. PostgreSQL can scan a B-tree backward, but a multicolumn ordering with mixed directions may require a specifically defined index to deliver the requested order without a separate sort. The database's index-ordering documentation describes the supported choices. Compare the complete ORDER BY with the index definition; don't infer an exact match merely from seeing the same column names.
A covering index is not a free pass
PostgreSQL can store additional non-key columns with INCLUDE, making an index-only scan possible for suitable queries. Whether heap visits can actually be avoided also depends on visibility information. The index-only scan guide describes these conditions. Including a column to return it is different from making it an ordering key.
Extra payload can enlarge the index and increase write cost. Do not include every field from a page just to make the plan look tidy. Ask which query needs the field, how frequently the value changes, and whether the real plan benefits. A wide index that saves one lookup can become an expensive permanent passenger on every write.
For the customer-order page, write down the exact filter, order, tie-breaker, and selected fields. Test a candidate with customer first against the real workload, then test the cross-customer report separately. The goal is not to collect the largest set of indexes. It is to make the database do less work for the questions the business actually asks, without quietly moving the bill to every transaction that writes a row.
Explore Infinite Upside
For Vijay Sheth’s personal accounts, read Infinite Upside: Building Wealth With Room to Live, published by Sheth Capital, Inc.
Explore a related guide
What Does a Database Index Actually Do?