GrepBoard/Manuals
605 manuals ·

PostgreSQL 17 memory tuning: shared_buffers and work_mem

·Tested on PostgreSQL 17.11 (Docker postgres:17) · 2026-10·Test scope ↓·7 steps

Tune PostgreSQL 17 shared_buffers (restart, 25% of RAM to start) and work_mem (per sort/hash node). Measure disk spills with EXPLAIN and temp file logs first.

shared_buffers is the shared page cache (default 128MB, restart required). work_mem is the memory for one sort or hash operation (default 4MB, changeable per session). Measure first whether sorts spill to disk (external merge Disk, temp file log lines), then raise work_mem only for the queries or roles that spill. Start shared_buffers at the official starting point: 25% of RAM on a dedicated server.

[1] Check the current values and how each one is applied. A context of postmaster means restart, sighup means reload, user means a session can SET it.

console
$ psql -U postgres -c "SELECT name, setting, unit, context FROM pg_settings WHERE name IN ('shared_buffers','effective_cache_size','work_mem','hash_mem_multiplier','maintenance_work_mem','autovacuum_work_mem','max_connections','autovacuum_max_workers') ORDER BY name"          name          | setting | unit |  context   ------------------------+---------+------+------------ autovacuum_max_workers | 3       |      | postmaster autovacuum_work_mem    | -1      | kB   | sighup effective_cache_size   | 524288  | 8kB  | user hash_mem_multiplier    | 2       |      | user maintenance_work_mem   | 65536   | kB   | user max_connections        | 100     |      | postmaster shared_buffers         | 16384   | 8kB  | postmaster work_mem               | 4096    | kB   | user(8 rows)

setting is in units: 16384 × 8kB = 128MB for shared_buffers, 524288 × 8kB = 4GB for effective_cache_size. SHOW shared_buffers prints the readable form.

SettingDefault (17)What it does
shared_buffers128MBShared data cache, set at server start only
work_mem4MBMemory per sort/hash node before it writes temp files
hash_mem_multiplier2.0Hash operations may use work_mem × this (8MB by default)
maintenance_work_mem64MBVACUUM, CREATE INDEX and other maintenance
autovacuum_work_mem-1-1 means use maintenance_work_mem
effective_cache_size4GBPlanner's cache assumption, allocates nothing

[2] Check whether a sort fits in memory with EXPLAIN (ANALYZE, BUFFERS). Sorting 200,000 rows spilled to disk at the 4MB default.

console
$ psql -U postgres -c "SET work_mem = '4MB'" -c 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF) SELECT * FROM t ORDER BY v'SET                       QUERY PLAN                        --------------------------------------------------------- Sort (actual rows=200000 loops=1)   Sort Key: v   Sort Method: external merge  Disk: 9208kB   Buffers: shared hit=1731, temp read=1151 written=1154   ->  Seq Scan on t (actual rows=200000 loops=1)         Buffers: shared hit=1728…

Sort Method: external merge Disk: and temp read/written are the signals. The same query with 64MB in that session only stays in memory:

console
$ psql -U postgres -c "SET work_mem = '64MB'" -c 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF) SELECT * FROM t ORDER BY v'SET                    QUERY PLAN                    -------------------------------------------------- Sort (actual rows=200000 loops=1)   Sort Key: v   Sort Method: quicksort  Memory: 17082kB   Buffers: shared hit=1731…

This query needs about 17MB. Start from a measured size like this, not from a round "1GB just in case".

[3] On a live server, find spilling queries with the temp file log. log_temp_files = 0 logs every temp file (applied on reload).

console
$ psql -U postgres -c "ALTER SYSTEM SET log_temp_files = 0" -c "SELECT pg_reload_conf()"
output
LOG:  temporary file: path "base/pgsql_tmp/pgsql_tmp137.0", size 9428992STATEMENT:  EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF) SELECT * FROM t ORDER BY v

On busy servers use a size floor ('10MB') instead of 0. Per-database totals are in pg_stat_database.temp_files and temp_bytes:

console
$ psql -U postgres -c "SELECT datname, temp_files, temp_bytes FROM pg_stat_database WHERE datname = 'postgres'" datname  | temp_files | temp_bytes ----------+------------+------------ postgres |          2 |   12228992(1 row)

[4] Raise work_mem only where it spills. One reporting role is safer than the whole server (ALTER SYSTEM). A role setting applies to connections opened afterwards; RESET reverts it.

console
$ psql -U postgres -c 'CREATE ROLE report LOGIN' -c "ALTER ROLE report SET work_mem = '64MB'"CREATE ROLEALTER ROLE$ psql -U report -d postgres -Atc 'SHOW work_mem'64MB$ psql -U postgres -c 'ALTER ROLE report RESET work_mem'ALTER ROLE$ psql -U report -d postgres -Atc 'SHOW work_mem'4MB

[5] Change shared_buffers. The docs' starting point on a dedicated server with 1GB or more is 25% of RAM, and because PostgreSQL also relies on the OS cache, more than 40% is unlikely to help. Go lower when the application shares the machine.

console
$ psql -U postgres -c "ALTER SYSTEM SET shared_buffers = '256MB'" -c 'SELECT pg_reload_conf()'ALTER SYSTEM pg_reload_conf ---------------- t(1 row) $ psql -U postgres -c "SELECT name, setting, unit, pending_restart FROM pg_settings WHERE name = 'shared_buffers'"      name      | setting | unit | pending_restart ----------------+---------+------+----------------- shared_buffers | 16384   | 8kB  | t(1 row)

A reload leaves the value at 16384 (128MB) and sets pending_restart to t. A restart applies it. The reload signal is processed asynchronously, so check pending_restart a moment later from a new connection.

console
$ psql -U postgres -c 'SHOW shared_buffers' shared_buffers ---------------- 256MB(1 row)

ALTER SYSTEM writes postgresql.auto.conf, which overrides postgresql.conf. A -c shared_buffers=… on the service or container command line overrides both; change that option instead.

[6] Roll back with ALTER SYSTEM RESET and a restart (log_temp_files returns on reload).

console
$ psql -U postgres -c 'ALTER SYSTEM RESET shared_buffers' -c 'ALTER SYSTEM RESET log_temp_files'ALTER SYSTEMALTER SYSTEM

After the restart SHOW shared_buffers was back to 128MB and SHOW log_temp_files to -1. If a large value keeps the server from starting, remove the line from postgresql.auto.conf and start it.

[7] Handle the remaining two this way:

  • maintenance_work_mem: autovacuum can allocate up to autovacuum_max_workers (default 3) times this. Give a large value only in the session that builds an index (SET maintenance_work_mem = '1GB'), and cap autovacuum separately with autovacuum_work_mem.
  • effective_cache_size: a planner assumption that allocates no memory. Set it to shared_buffers plus the part of the OS file cache PostgreSQL will use. Too low makes sequential scans more likely than index scans.

The cache hit ratio is blks_hit / (blks_hit + blks_read) in pg_stat_database. blks_read also counts reads served by the OS cache, so do not grow shared_buffers on this ratio alone.

console
$ psql -U postgres -c "SELECT datname, blks_hit, blks_read, round(100.0 * blks_hit / nullif(blks_hit + blks_read, 0), 2) AS hit_pct FROM pg_stat_database WHERE datname = 'postgres'" datname  | blks_hit | blks_read | hit_pct ----------+----------+-----------+--------- postgres |    11609 |       342 |   97.14(1 row)

Official references: Resource Consumption — shared_buffers, work_mem, hash_mem_multiplier, maintenance_work_mem · effective_cache_size · log_temp_files · pg_settings · ALTER SYSTEM

#postgresql#shared_buffers#work_mem#effective_cache_size#maintenance_work_mem