Planning & executionsenior🔒 Pro concept

How PostgreSQL sorts: quicksort, external merge, and top-N heapsort

Simple terms

A sort tries to stay in memory, and work_mem is the budget it gets. Fits? One quicksort and you're done. Doesn't fit? Postgres sorts as much as it can hold, spills that run to a temporary file, repeats, then merges the runs back together, correct, but paying disk the whole way. There is a third case worth knowing too: ORDER BY with a LIMIT doesn't sort everything. It keeps a small heap of just the rows you asked for and discards the rest as it goes.

You might be asked

EXPLAIN ANALYZE shows three different 'Sort Method' lines depending on the query: 'quicksort Memory', 'external merge Disk', and 'top-N heapsort Memory'. Walk me through what each one means, what makes PostgreSQL choose between them, and where work_mem comes into it.

TopicHow PostgreSQL sorts: quicksort vs external merge vs top-N heapsort, and the work_mem spill boundary
PostgreSQL17.10 (mechanism is the same on 14-17; method strings unchanged since 9.6)
Tools usedpgi17 lab (PostgreSQL 17.10), EXPLAIN (ANALYZE) Sort Method line, source @REL_17_10 src/backend/utils/sort/tuplesort.c
Last reviewed2026-06-19

Pro concept

Full answer and evidence depth sit behind Pro

You have the question and a plain-English lead. Pro unlocks the short answer, what the docs say, what the code does, the labeled lab evidence or reproduction protocol, how to use it under pressure, and the references.

ProFull answer + evidence depth

Unlock the full breakdown for How PostgreSQL sorts: quicksort, external merge, and top-N heapsort

You have the interview question and a plain-English lead. Pro opens the short answer, the manual walkthrough, the source decode, and a 18-line evidence section that states whether it is raw output, a captured run summary, or a protocol to run yourself, plus how to use it under pressure.
  • How you'd answer it, the full senior-level short answer
  • What the docs say, the manual's actual wording, with the citations
  • What the code does, the mechanism decoded from PostgreSQL's own source at a pinned tag
  • Proof from a real run, labeled as raw output, a captured run summary, or a run-it-yourself protocol
  • Using it under pressure, the situation, the call you'd make, what goes wrong, and what people get wrong
  • Version notes, what changed across PostgreSQL releases

Card required. Cancel before day 7 and you are not charged.

Compare plans

How this was verified

The open teaser is the question and a plain-English lead. The short answer, the manual walkthrough, the source decode, the lab evidence, and how to use it under pressure unlock with Pro. Evidence is labeled as raw output, a captured run summary, or a run-it-yourself protocol.

Connected

Where this concept connects

How this concept links across the library, the interview questions that test it, its plain-English glossary definition, and the guided pathways it belongs to. Open the full map to explore further.

Open in the interactive map →