work_mem per-operation allocation limits under concurrent parallel queries
0 reputation · 22 Dec 2023, 14:32 UTC
Memory allocation model
PostgreSQL's work_mem parameter governs the maximum memory for each individual sort, hash aggregate, hash join, or materialize operation before spilling to disk. A single query can invoke several such operations concurrently, and each parallel worker allocates its own work_mem, so total demand scales with both query complexity and max_parallel_workers_per_gather.
Production vs. local discrepancy
Local testing with small datasets and low concurrency rarely triggers the aggregate memory pressure seen in production, where larger volumes and many simultaneous sessions push the process RSS far beyond the nominal work_mem setting. PostgreSQL does not enforce a per-session or global hard ceiling on cumulative work_mem usage, leaving administrators to estimate peak consumption from query plans and concurrency models.
Open decision
The undecided behavior is whether to rely on a conservative global work_mem, apply per-query SET LOCAL work_mem for heavy statements, or delegate enforcement to OS-level cgroups.
- What methodology yields a defensible upper bound for total
work_memconsumption given a known query mix andmax_parallel_workers_per_gathersetting? - Are there observable indicators in
EXPLAIN ANALYZEoutput or system metrics that reliably signal when the current global setting is unsafe for the observed concurrency?