Plan operator
Sort
Plan node that orders its input rows, used for ORDER BY, merge joins, and some grouping operations.
A Sort node materializes its input in a specific order. The planner inserts it for ORDER BY clauses, for the inputs to a Merge Join, and to feed certain grouping nodes that expect sorted input.
If the data fits in work_mem PostgreSQL uses an in-memory quicksort. If not, it spills to a temporary file and merges runs from disk — EXPLAIN ANALYZE reports the method as "quicksort" or "external merge". External sorts are dramatically slower, so a sort showing "Disk: N kB" is a strong tuning signal.
The cheapest way to remove a Sort is to add a B-tree index on the ordering columns, which lets the planner use an Index Scan that already returns rows in order.