Skip to content

Order Analysis

Sort operations in a query plan have a desired order — what the query needs — and a provided order — what the data actually arrives in. When these differ, PostgreSQL must perform an explicit sort, which costs CPU and memory. IndeX-Ray surfaces both orders side by side, marked with an asterisk (*), so you can spot mismatches at a glance.

This is augmented by the analyzer and is not part of standard PostgreSQL EXPLAIN output.

Each order entry contains:

FieldDescription
ExpressionThe column or expression being sorted
DirectionASC or DESC
NullsNULLS FIRST or NULLS LAST

IndeX-Ray displays the desired order and, if the provided order differs, highlights the mismatch.

When the provided order matches the desired order, the sort node is essentially free — the data is already in the right sequence, typically because an index delivers it pre-sorted. When they don’t match, PostgreSQL must sort the rows explicitly, which:

  • Uses work_mem (and spills to disk if that’s exceeded)
  • Adds CPU cost proportional to n log n of the input rows
  • Increases latency, especially for LIMIT queries that could otherwise stream early results

A mismatch is a signal that you may be able to eliminate the sort entirely by creating or adjusting an index to deliver rows in the needed order. For example, if the desired order is created_at DESC but the index provides created_at ASC, adding a DESC index — or using an existing index with a backward scan — could remove the sort.

Order mismatches can be partial. If a query needs ORDER BY a ASC, b DESC and the index provides a ASC, b ASC, the first column matches but the second doesn’t. PostgreSQL may use an Incremental Sort in this case — cheaper than a full sort because only the trailing columns need reordering within each group of the leading column.

Order analysis is shown on Sort and Incremental Sort nodes.

The analyzer determines the desired and provided orders by examining the plan node’s sort keys and the access paths feeding into it. This runs entirely on your machine inside the analyzer container.

All augmented fields are marked with an asterisk (*) in the UI and include a tooltip noting they are not part of standard PostgreSQL EXPLAIN output.