Quick revision
The short answer
Batches: 64 means the hash table for the build side did not fit in the memory allowed, so PostgreSQL split both inputs into 64 partitions by hash value, kept one in memory and wrote the other 63 to temporary files to process one at a time. A hash may use work_mem times hash_mem_multiplier, which defaults to 2.0 since PostgreSQL 15. Raising work_mem for that session or role can make the join run in one batch, but raising it globally risks running out of memory, because every sort and hash in every session can use it; reducing the build input with earlier filters or fewer columns is often the better fix, and log_temp_files and pg_stat_statements temp_blks_written show how often it happens.
What to remember
In this answer, we saw that Batches: 64 means the hash table did not fit in memory, so PostgreSQL partitioned both inputs into 64 batches and processed most of them through temp files, and that a hash may use work_mem times hash_mem_multiplier, 2.0 by default since PostgreSQL 15. We raised memory only per transaction or role, because work_mem applies to every operation in every session, reduced the build side with fewer columns and earlier filters, and monitored spills with log_temp_files and pg_stat_statements temp_blks_written.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Batches: 64 means the hash table for the build side did not fit in the memory allowed, so PostgreSQL split both inputs into 64 partitions by hash value, kept one in memory and wrote the other 63 to temporary files to process one at a time. A hash may use work_mem times hash_mem_multiplier, which defaults to 2.0 since PostgreSQL 15. Raising work_mem for that session or role can make the join run in one batch, but raising it globally risks running out of memory, because every sort and hash in every session can use it; reducing the build input with earlier filters or fewer columns is often the better fix, and log_temp_files and pg_stat_statements temp_blks_written show how often it happens.
Why this question is asked
A hash join is usually the fastest way to join two large tables, but only while its hash table fits in memory. When it does not, PostgreSQL does not fail; it quietly switches to a slower, disk-based mode, and the only signs are a Batches number in the plan and temp files on disk. Interviewers ask about this because it connects a plan detail to a configuration choice with real risk: the obvious fix, more memory, can take down the server if applied carelessly. In this answer, we will recap how a hash join works, explain what batches are, see how work_mem and hash_mem_multiplier set the limit, and then go through safe fixes and monitoring.
1A Quick Recap of the Hash Join
A hash join has two inputs. It reads the build side, usually the smaller one, and puts every row into a hash table keyed by the join column. Then it reads the probe side and, for each row, looks in the hash table for rows with the same key. Each input is read once, which is why hash joins are so effective for large equality joins. The catch is that the whole build side must be held in the hash table at once, so its size in memory decides how the join runs.
2What Batches: 64 Means
When PostgreSQL estimates or discovers that the build side is larger than the memory it may use, it partitions the work. It picks a number of batches, a power of two, and assigns every row to a batch using bits of its hash value. Rows of batch 0 go into the in-memory hash table. Rows of the other batches, from both the build side and the probe side, are written to temporary files. After the first pass, PostgreSQL loads batch 1's build rows into memory, reads batch 1's probe rows from their file, joins them, and so on for every batch. Because a matching pair always has the same hash value, both rows land in the same batch, so the result is correct.
- Estimate build side size30M rows, about 3 GB
- Fits in work_mem x hash_mem_multiplier?YesSingle batch, all in memoryNoSplit into 2^k batches
- Batch 0 in memory, others to temp filesboth build and probe rows
- Join batch 0, then load and join each other batchreads temp files back
- Batch grew past the limit?YesDouble the batch countNoContinue
5 steps in order. Play them, or step through them one at a time.
Notice three details. Batches: 64 (originally 16) means the planner started with 16 batches based on its estimate and had to double twice during execution because the data was bigger than expected, which is a sign of a poor estimate as well as too little memory. Memory Usage near 64 MB shows the limit the hash worked within. And temp read and written in BUFFERS, about 4 GB here at 8 kB per block, is the disk traffic the batching caused. Writing and rereading most of both inputs is why this join takes 96 seconds instead of perhaps 25.
3How work_mem and hash_mem_multiplier Set the Limit
work_mem is the amount of memory a single sort or hash operation may use before spilling to disk. It is per operation, not per query or per connection: a query with three hash joins and a sort can use four times work_mem, and a parallel query multiplies that by the number of workers. The default is only 4 MB.
Hash operations, hash joins and hash aggregates, were given their own multiplier in PostgreSQL 13, hash_mem_multiplier, because they degrade more sharply than sorts when they spill. The limit for a hash is work_mem times hash_mem_multiplier. Its default was 1.0 in version 13 and 14 and became 2.0 in PostgreSQL 15. So with work_mem = 32MB on PostgreSQL 17, a hash may use 64 MB, which matches the Memory Usage in our plan. To fit 3 GB in one batch, we would need work_mem around 1.5 GB, which tells us immediately that memory alone is not the right fix for this join.
4Fixing It Safely
The first option is more memory, but only where it is needed. Raising work_mem globally to 1 GB on a server with 300 connections could, in the worst case, try to use hundreds of gigabytes and get the server killed by the operating system. Instead we raise it for one transaction, or for a role that runs heavy reports with few connections.
The second option, often the better one, is to make the build side smaller. A hash table stores the columns the query needs from the build side, so selecting only those columns, not SELECT *, shrinks every entry. Filtering earlier, for example joining only active customers or only this year's orders, reduces the row count. Aggregating one side first, if the query only needs totals, can reduce millions of rows to thousands. And a correct estimate matters: the doubling from 16 to 64 batches shows the planner underestimated the size, so running ANALYZE may also let it choose a different, better plan. In our example, selecting two columns instead of twelve cut the build side to about 600 MB, and with work_mem at 256 MB for the transaction, the join ran in two batches.
5Monitoring Temp File Usage
Spilling hash joins and sorts show up as temp files, so we watch temp files. log_temp_files logs every temp file above a size with the query that created it; setting it to, say, 100MB catches the heavy ones without flooding the log. pg_stat_statements has temp_blks_written per query, which lets us rank queries by how much they spill. pg_stat_database has temp_files and temp_bytes for the whole database, a good number to graph and alert on.
With these in place, the decision becomes data-driven: if one report spills 4 GB per run, we tune that report; if many queries spill a little, a modest global increase in work_mem may be justified, sized against the connection count and available RAM. temp_file_limit per role is a useful safety net so that one query cannot fill the disk.
6Follow-up questions
Why are hash joins given a multiplier instead of just a bigger work_mem?
Because sorts and hashes degrade differently. A sort that spills switches to an external merge that is often only moderately slower, while a hash that spills writes and rereads both inputs. Before PostgreSQL 13, hash aggregates did not spill at all and could exceed memory badly. The multiplier lets us give hashes more room without also giving every sort more, which is safer than raising work_mem for everything.
Does a parallel hash join change the picture?
Yes. With a Parallel Hash, the workers build one shared hash table, and its memory budget is the per-process limit times the number of participants, so parallel plans can avoid batching where a serial plan would not. But each worker also counts against the server's memory, so the total use is higher. EXPLAIN shows the shared hash with its own batch information.
Can too much memory make a join slower?
Rarely by itself, but it can change plans: with a large work_mem the planner may choose hash joins and hash aggregates where a nested loop or a sorted plan would have used less memory and been just as fast. The bigger risk is not speed but total memory, which is why we raise it narrowly and watch the effect.
7Conclusion
In this answer, we saw that Batches: 64 means the hash table did not fit in memory, so PostgreSQL partitioned both inputs into 64 batches and processed most of them through temp files, and that a hash may use work_mem times hash_mem_multiplier, 2.0 by default since PostgreSQL 15. We raised memory only per transaction or role, because work_mem applies to every operation in every session, reduced the build side with fewer columns and earlier filters, and monitored spills with log_temp_files and pg_stat_statements temp_blks_written.
Open with
Batches: 64 means the build side of the hash join was bigger than the memory the hash may use, so PostgreSQL partitioned the work. It assigns each row to one of 64 batches by its hash value, keeps batch 0 in memory, writes the build and probe rows of the other 63 to temp files, and then processes them one batch at a time.
Then, if they want more
Matching rows always land in the same batch, so the result is correct, but most of both inputs is written to disk and read back, which is where the temp files and the time go. If the plan says originally 16, the planner also underestimated the size.
The memory limit for a hash is work_mem times hash_mem_multiplier. work_mem applies per sort or hash operation, so one query can use it several times, and parallel workers multiply that. hash_mem_multiplier was added in 13 and defaults to 2 since PostgreSQL 15. So with work_mem at 32 megabytes a hash gets 64.
I would not raise work_mem globally, because with hundreds of connections that can exhaust memory. Instead I would SET LOCAL work_mem for that transaction, or set it on a reporting role with a small pool. Often the better fix is a smaller build side: select only needed columns, filter earlier, pre-aggregate, and run ANALYZE so estimates are right.
To monitor, I would set log_temp_files to around 100 megabytes and rank queries by temp_blks_written in pg_stat_statements.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 15, PostgreSQL 13, PostgreSQL 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A hash join in PostgreSQL shows Batches: 64 and the query writes large temp files....": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/727d94b9ea/