Course contentsShow
← Databases & SQL
Lesson 938 of 1,9538. PostgreSQL InternalsFree lesson

Sorting and External Disk Merges

You will manage work memory to ensure complex sorts execute within RAM limits.

In this lesson, you will learn how PostgreSQL manages memory during large-scale sorts to prevent performance degradation when data exceeds your available RAM.

The Memory Sorting Limit

When you execute a query with an ORDER BY clause, PostgreSQL attempts to sort the result set in memory using a specialized data structure, such as a quicksort algorithm. The size of this "playground" is governed by the configuration parameter work_mem.

If the data set is small enough to fit within the work_mem limit, the sort happens entirely in memory, which is lightning-fast. However, if your result set is larger than work_mem, the database cannot finish the sort in RAM. It must spill the data to temporary files on disk.

External Disk Merges (Tapes)

Once the data exceeds work_mem, PostgreSQL transitions to an external merge sort. It divides the data into smaller chunks (often called "runs") that fit into memory, sorts each chunk individually, and writes them back to temporary files on disk.

After all chunks are sorted, the database performs a "merge" phase. It opens multiple temporary files simultaneously and reads the smallest value from the head of each file, merging them into a single, ordered stream. This is essentially a multi-way merge process. Because disk I/O is exponentially slower than memory access, this operation significantly increases query latency.

Practical Management

You can observe this behavior in an EXPLAIN ANALYZE output. If you see "Sort Method: external merge" followed by "Disk: [X]kB," you know your work_mem was insufficient for that specific operation.

-- Check if your sort is spilling to disk
EXPLAIN ANALYZE 
SELECT * FROM large_table 
ORDER BY user_activity_score DESC;

To optimize, you can temporarily increase work_mem for a specific session to accommodate the sort:

SET work_mem = '64MB';
-- Now execute your complex sort
SELECT * FROM large_table ORDER BY user_activity_score DESC;
RESET work_mem;

Be careful: work_mem is allocated per sort operation per query. If a complex query uses multiple parallel workers or nested sorts, setting this value too high can lead to "Out of Memory" errors that crash the server.

Key Takeaway: When a sort exceeds your work_mem allocation, PostgreSQL spills data to disk; monitoring this with EXPLAIN ANALYZE allows you to tune memory settings to favor high-speed RAM over slow disk-based merge operations.