work_mem is the memory each sort, hash or grouping step is allowed before it spills to disk, and a single query can contain many such steps while many queries run at once — so the true worst case is work_mem multiplied by operations multiplied by connections. When the plan says 'Sort Method: external merge Disk: 240MB', raising work_mem for that statement or that session turns a disk sort into a memory sort; raising it globally is how people run their server out of memory. Set it per session or per role for the heavy reports.
Questions this Concept answers
Why is raising `work_mem` globally a common way to run a server out of memory?
J
jeremy
Video
Setting work_mem Per Session in PostgreSQL to Avoid OOM Kills and Disk Spills
`work_mem` in PostgreSQL bounds the memory available to a single memory-consuming plan node — each sort, hash aggregate, or hash join — and therefore is allocated *per operation per connection*, not …