work_mem
When a query is running that needs to sort data, the database estimates how much data is involved and then compares it to the work_mem
parameter. If it's larger (and the default is only 1 MB), rather than sorting in memory it will write all the data out and use a disk-based sort instead. This is much, much slower than a memory based one. Accordingly, if you regularly sort data, and have memory to spare, a large increase in work_mem
can be one of the most effective ways to speed up your server. A data warehousing report might on a giant server run with a gigabyte of work_mem
for its larger reports.
The catch is that you can't necessarily predict the number of sorts any one client will be doing, and work_mem
is a per-sort parameter rather than a per-client one. This means that memory use via work_mem
is theoretically unbounded, where a number of clients sorting large enough things to happen concurrently.
In practice, there aren't that many sorts going on in a...