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

Parallel Workers and Query Memory Usage

You will calculate the required worker memory to avoid spilling parallel query operations to disk.

In this lesson, you will learn how to calculate the specific memory allocation required to ensure parallel query operations run entirely in RAM instead of spilling to temporary disk storage.

The Memory Bottleneck

When you run a parallel query in PostgreSQL, the database launches multiple background workers to process data simultaneously. Operations like Hash Join or Hash Aggregate require a "hash table" to be built in memory. Crucially, the memory limit you set—work_mem—is per worker, per operation.

If you have a query with a parallel degree of 4, and each worker is performing a hash join, PostgreSQL allocates up to work_mem for each of those 4 workers. If the combined data exceeds the total allocated memory, PostgreSQL will "spill" the excess data to disk in temporary files, which drastically slows down your query.

Calculating the Requirement

To avoid disk spilling, you need to estimate the size of the data being processed. If you expect a hash table to be 1GB and you are using a parallel degree of 4, simply setting work_mem to 1GB will likely result in a spill because each worker is trying to build a local hash table.

In many parallel patterns, the dataset is partitioned among workers. If the data is partitioned perfectly, the memory requirement per worker is:

Required work_mem = (Total Data Size) / (Number of Parallel Workers)

However, because data distribution is rarely perfect, a "safety buffer" is necessary. A common practice is:

-- Example: 2GB data, 4 workers, 25% safety margin
-- (2GB / 4) * 1.25 = 640MB per worker
SET work_mem = '640MB';

Monitoring Spills

You can verify if your calculation is sufficient by running EXPLAIN ANALYZE on your query. Look for the Workers node in the plan. If you see Batches in a Hash Join node, it means your work_mem was insufficient, and the database was forced to partition the hash table into files on disk. If Batches is 1, the entire operation fit in memory.

Key Takeaway: Parallel query memory is allocated per worker, so you must divide your total estimated working set by your worker count, then add a buffer to account for data skew to avoid expensive disk spills.