concept Data & storage
Composite index column order
Column order determines which keys form a continuous range. For the same query, a range scan may examine fewer entries in one order than in another while returning exactly the same matches.
What it means
Column order changes which entries sit together before a scan applies any remaining filters. In an index ordered by (category, number), the A entries sit together, sorted by number. Reverse the columns to (number, category) and the entries with the same number sit together, sorted by category.
The simulation scans one continuous range in ascending key order, without skipping between groups. An entry inside the range is a candidate, not necessarily a match. A residual filter checks a condition that the scan bounds did not enforce.
Try it
Start with the default Composite key order of category-number, Category predicate of A, and inclusive numeric bounds of 1 and 4. The Declared access algorithm is Declared range scan, without skip scan. The four synthetic keys, in order, are (A,1), (A,3), (B,1), (B,4). Within the A group, the scan can use both numeric bounds: the range from (A,1) through (A,4) contains just A1 and A3. Both are scanned, both match, and neither is rejected.
Composite index column order
Build a small explicitly ordered composite index; change equality/range predicates and show candidate entries, residual filtering and assumptions.
Synthetic inputs only. This is not a measurement of your hardware or service.
The declared range scan can constrain a leading key range. Any remaining predicate filters the candidate entries.
Ordered entries → candidate range → residual predicate
- Index entry order
- A1 · A3 · B1 · B4
- Candidates for the declared range scan
- A1 · A3
- Rows matching all predicates
- A1 · A3
- Candidates rejected by residual filtering
- None
Evaluated evidence
- Composite key order
- category-number
- Category predicate
- A
- Inclusive numeric lower bound
- 1
- Inclusive numeric upper bound
- 4
- Declared access algorithm
- bounded-range
This is an explicit no-skip scan algorithm, not a query optimizer. PostgreSQL18 can use skip scans and other strategies; no universal leftmost-column prohibition or actual query cost is claimed.
About this model
Every input is local synthetic evidence. Numbers stay as raw text until this model validates them. The shown evaluated values come from the same result as the explanation.
Presets replace the entire scenario. Reset restores all starting fields, scenario selection and model state; it does not change the open or closed state of this help or article FAQ.
No external request, device command, stored-input history or measurement is performed.
- Set Category predicate to
anyand Inclusive numeric upper bound to1, leaving Inclusive numeric lower bound at1. You now want every entry with number 1.A3sits betweenA1andB1, so the matches do not form one continuous group. Without a category bound, this algorithm scansA1, A3, B1, B4. The number filter rejectsA3andB4, retainingA1andB1. - Change only Composite key order to
number-category. The same entries now appear asA1, B1, A3, B4; the labels still name the category first. The number-1 range scans justA1andB1. Both match. The answer stayed the same, but the candidate range shrank from four entries to two. - Choose Inspect residual category filtering. The number bounds are still
1through1, but the category must now beA. This strategy still scansA1andB1; the category filter rejectsB1, leaving onlyA1.
An empty range is different from an unknown one. With category-number, category A, and both bounds at 2, there is no A2: nothing is scanned or matched. Inspect the evidence boundary instead leaves the access algorithm unknown, so the candidates are not established. The numeric bounds accept whole-number strings from 1 through 4; a decimal string such as 1.0 or a lower bound above the upper bound is invalid and produces no result.
Reset restores the default inputs and scenario, returns the step and virtual time to zero, and clears errors, history, and comparison.
In practice
When comparing key orders for a query, write down the first and last keys the proposed scan would visit. Then ask which entries between those endpoints still fail a condition. This separates a narrower scan from a more selective filter: either can leave two matches, but they need not examine the same entries.
Do not turn that distinction into a rule that an index is unusable without a condition on its first column. In PostgreSQL 18, leading equality and range conditions influence B-tree scan bounds, and skip scan is also available in suitable cases. Multicolumn indexes
If the query needs ordered output, consider that separately. In PostgreSQL 18, B-tree key order and scan direction affect the order a scan can supply; the planner may choose another scan and sort instead. Indexes and ORDER BY
Use key order to explain which entries a specified strategy examines. The four-key comparison does not predict which plan a database will choose or how long it will take.
Frequently asked questions
Editorial questions based on the listed sources and explicitly labeled local examples.
Why does the first key matter in this example?
The example scans one contiguous range in the declared lexicographic order. A leading-key condition can narrow that range; a condition on the other key may instead be applied as a residual filter.
Can filtering still happen after an index scan?
Yes. Candidate entries are not automatically matching rows. The model shows which entries were examined and which still failed the remaining predicate.
Do all engines follow the same prefix rule?
No universal rule is claimed. PostgreSQL 18 documents skip scan in suitable cases; this lab intentionally excludes that strategy. A statement that a composite index is always unusable without its first key would be too broad.
Does this predict an actual query plan?
No. It uses one declared scan strategy over four synthetic keys. Statistics, cost estimates, alternative indexes and actual planner choices are not evaluated.
Sources & model boundaries
Primary references used for this explanation. The interactive example is an educational model, not a live check.
- Multicolumn indexes
B-tree bounds and skip scan. Leading equality/range constraints influence index scan bounds; PostgreSQL18 also supports skip scan in suitable cases.
Checked .
- PostgreSQL: Indexes and ORDER BY
B-tree ordering and multicolumn scan direction. B-tree key order and scan direction affect available ordered output; the planner can instead choose another scan and sort.
Checked .