Memory per sort/hash operation before spilling to disk.
At a glance
| Property | Value |
|---|---|
| Parameter | work_mem |
| Category | Memory |
| Default | 4MB |
| Value type | memory (kB/MB/GB) |
| Change scope | Per-session (SET) |
What it does
The maximum memory used by an internal sort or hash operation before it spills to temporary disk files. Crucially this is per operation (per node) per query, not per connection — a complex query can use many multiples.
How to apply a change
Can be set per session with SET, per role/database with ALTER ROLE/DATABASE ... SET, or globally in postgresql.conf.
Inspect the current value and source with SHOW work_mem; or SELECT name, setting, unit, context, source FROM pg_settings WHERE name = 'work_mem';.
Tuning guidance
Size it as roughly RAM / (max_connections x average sort nodes), then raise per-session for known heavy analytical queries. Too high invites OOM (SQLSTATE 53200) under concurrency; too low causes excessive temp-file I/O — watch log_temp_files.