PostgreSQL Mechanics
7 minutes
work_mem is how much memory a single sort, hash or grouping step may use, and it defaults to only 4 MB. When a step needs more, it does not fail; it spills to temporary files on disk and gets much slower, which EXPLAIN ANALYZE reports as 'Sort Method: external merge Disk: 84320kB' or as extra batches under a Hash node. This is one of the few slow-query causes you can see as a single word in the plan. It also multiplies: the limit is per step and per parallel worker, not per query.