That is a common misconception. It is not one work_mem buffer per SQL, but one work_mem buffer per required operation within that SQL. So you can have manner work_mem buffers per SQL statement!
Right from the official docs:
work_mem sets the maximum amount of memory to be used by a query operation (such as a sort or hash table) before writing to temporary disk files. If this value is specified without units, it is taken as kilobytes... Note that for a complex query, several sort or hash operations might be running in parallel; each operation will be allowed to use as much memory as this value specifies before it starts to write data into temporary files. Also, several running sessions could be doing such operations concurrently. Therefore, the total memory used could be many times the value ofwork_mem
; it is necessary to keep this fact in mind when choosing the value. Sort operations are used forORDER BY
,DISTINCT
, and merge joins. Hash tables are used in hash joins, hash-based aggregation, and hash-based processing ofIN
subqueries.
Regards,
Michael Vitale
If multiple users are querying the same regions of the same tables, does each process have to read the same blocks, or can they share ("oh look, some other process has already read into memory some of the data blocks I need, so I'll just share those buffers")?
--
Angular momentum makes the world go 'round.
Angular momentum makes the world go 'round.