// postgresql · memory

work_mem

Memory each sort or hash operation may use before it spills to temporary files.

Padrão
4MB
Range
64kB to 2TB
Restart needed
Não
Default checked on
PostgreSQL 18

O que faz

The limit is per plan node, not per query or per connection: a query with three sorts and two hash joins can use five times work_mem, and every parallel worker gets its own allowance. Hash operations may use work_mem times hash_mem_multiplier.

Valor padrão

4MB. 4MB (PostgreSQL 18), unchanged since 9.4.

Check the current value

SHOW work_mem;

SELECT setting, unit, boot_val, context, source, pending_restart
FROM pg_settings
WHERE name = 'work_mem';

Change it

SET work_mem = '64MB';   -- this session
ALTER ROLE reporting SET work_mem = '64MB';   -- new sessions of one role

ALTER SYSTEM SET work_mem = '64MB';
SELECT pg_reload_conf();

No restart. Any role can SET it for a session or with SET LOCAL for one transaction; a server-wide ALTER SYSTEM change applies after a reload (context is user).

When to change it

When EXPLAIN (ANALYZE) shows 'Sort Method: external merge' or hash batches greater than 1, or log_temp_files logs frequent spills. Raise it for the reporting role or the session that needs it rather than server-wide.

What can go wrong

Memory use is roughly work_mem x operations x active connections. A large server-wide value with many connections can push the OS into swap or the OOM killer.

The closest setting on other engines

MotorConfiguraçãoPadrãoRestart
PostgreSQL work_mem this page 4MB Não
MySQL sort_buffer_size 256KB Não
Oracle pga_aggregate_target 10MB or 20% of SGA Não

Questions about work_mem

What is the default value of work_mem in PostgreSQL?

4MB. 4MB (PostgreSQL 18), unchanged since 9.4.

Does changing work_mem require a restart?

No. No restart. Any role can SET it for a session or with SET LOCAL for one transaction; a server-wide ALTER SYSTEM change applies after a reload (context is user).

When should I change work_mem?

When EXPLAIN (ANALYZE) shows 'Sort Method: external merge' or hash batches greater than 1, or log_temp_files logs frequent spills. Raise it for the reporting role or the session that needs it rather than server-wide.

Back to all PostgreSQL settings, or check the PostgreSQL manual for every option.

// one client for every engine

Check work_mem on your own server

SQLly runs the query above against PostgreSQL and the other engines in this reference, from one desktop app on macOS, Windows and Linux.