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. Query Doctor shows both orders side by side, marked with an asterisk (*), so you can spot mismatches at a glance.

Stock PostgreSQL does not report either one. They come from the patched planner Query Doctor costs your queries on.

Each order entry contains:

Field Description
Expression The column or expression being sorted
Direction ASC or DESC
Nulls NULLS FIRST or NULLS LAST

Query Doctor 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 means you can often remove the sort 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 planner reports both orders itself, from the sort keys the node needs and the order the path beneath it delivers.

The planner runs on the Query Doctor instance: a PostgreSQL container matched to your database’s major version, holding your schema and your table statistics and none of your rows. The Collector sends them.

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.