Sub topic 6
Giving hash operations more memory with `hash_mem_multiplier`
Assume you know work_mem, can read a simple EXPLAIN plan, and understand that each PostgreSQL operation may use its own memory. This chapter focuses on hash joins and hash aggregation.
What `hash_mem_multiplier` controls
work_mem is the memory target for one sort, hash table, or similar operation. hash_mem_multiplier multiplies that target for hash-based operations only.
For example, with these settings:
SET work_mem = '16MB';
SET hash_mem_multiplier = 2.0;A hash join or hash aggregate can use about 32 MB before PostgreSQL starts using batches and temporary files. A sort still uses only the work_mem target of 16 MB.
The default value of hash_mem_multiplier is commonly 2.0, but check the server rather than assuming:
SHOW work_mem;
SHOW hash_mem_multiplier;Hash operations include:
- The hash side of a hash join.
- A
HashAggregateoperation used forGROUP BYor similar aggregation. - Other executor operations that build hash tables.
The setting is not a server-wide memory limit. work_mem applies per operation, and a query can contain several operations. Parallel workers can also perform their own operations. Concurrent sessions multiply the total possible memory use.
Reading a hash plan
Use EXPLAIN (ANALYZE, BUFFERS) on a representative query:
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, count(*)
FROM orders
GROUP BY customer_id;For a hash aggregate, inspect these fields:
Batches: 1usually means the hash table stayed in memory.Batchesgreater than 1 means PostgreSQL divided the work into batches.Disk Usageindicates that the operation used temporary disk storage.Memory Usageshows the memory used by the hash operation.
A hash join has similar information under its Hash or Hash Join nodes. Batching can be a useful diagnosis, but it is not automatically a problem. A batch operation may be faster overall than a much larger memory allocation.
Choosing a value
Increase the multiplier when representative hash operations frequently spill and the host has memory headroom. Change it gradually and measure again. A higher value can reduce temporary I/O, but it can also cause memory pressure or an out-of-memory event during concurrency spikes.
For a controlled test, change the setting only for the current transaction:
BEGIN;
SET LOCAL work_mem = '16MB';
SET LOCAL hash_mem_multiplier = 4.0;
EXPLAIN (ANALYZE, BUFFERS)
SELECT (g % 10000) AS group_key, count(*)
FROM generate_series(1, 500000) AS data(g)
GROUP BY group_key;
ROLLBACK;Compare the plan with the original settings. Look for fewer batches, lower disk usage, and a lower execution time. If the query does not use a hash aggregate, the change cannot improve that operation.
hash_mem_multiplier is a reloadable configuration setting and can also be changed for a role or session. Test a setting with SET or SET LOCAL first. Make a persistent change only after you have measured workload behavior and checked the memory budget.
Exercise
A hash aggregate reports Batches: 8 and Disk Usage, while a sort in the same query fits in memory. Which setting specifically targets the hash aggregate?
Show answer
Increase hashmemmultiplier, after checking concurrency and available memory. Increasing it affects hash operations, while increasing work_mem affects both the hash operation and the sort.