Insufficient work_mem causes disk-based sorting and hashing
warningperformanceUpdated Jan 21, 2026
Technologies:
How to detect:
When work_mem is too low for complex queries with sorts, aggregations, or hash joins, PostgreSQL spills to disk, causing significant performance degradation. This is visible in EXPLAIN ANALYZE output showing disk-based operations.
Recommended action:
Increase work_mem for complex queries using SET work_mem = '256MB' before the query. Run EXPLAIN (ANALYZE, BUFFERS) to verify memory usage and whether operations remain in memory. Reset work_mem after the query. Consider increasing global work_mem in postgresql.conf for sustained workloads.