Blog
PostgreSQL Index Column Order: The Left Edge Decides
An index on (merchant_id, status, created_at) is not three independent indexes glued together. It is one ordered structure, and the leftmost usable prefix controls how narrowly PostgreSQL can navigate it.
Equality Before Range Is A Useful Start
For this query:
select id, amount
from payments
where merchant_id = $1
and status = 'PENDING'
and created_at >= $2
order by created_at;
(merchant_id, status, created_at) is a sensible candidate: two equality conditions narrow the tree before the range and ordering column.
Change the workload and the answer changes. A query filtering only status cannot efficiently seek through an index beginning with merchant_id. A low-cardinality status may also be poor standalone key.
Query Order Is Not Index Order
Rearranging predicates in SQL does not change semantics or force index navigation order. Design from access patterns, not visual order of the WHERE clause.
Include columns can support index-only scans without making those columns part of search order:
create index payments_pending_lookup
on payments (merchant_id, status, created_at)
include (id, amount);
That improves one read shape but increases index size and write cost.
Partial Indexes Need Stable Predicates
If pending rows are a small stable subset, a partial index may be better:
create index payments_pending_by_merchant
on payments (merchant_id, created_at)
where status = 'PENDING';
The query predicate must imply the index predicate. Verify with EXPLAIN, real parameter patterns, and production-like statistics.
Every extra index slows inserts and updates. Build the smallest set that serves real queries, then watch usage before collecting indexes like badges.