SQLNirvana by Pyro

Blog › postgresql · tuning

Tuning PostgreSQL memory: shared_buffers, effective_cache_size and work_mem

The four memory settings that matter most in PostgreSQL, what each one actually does, and sensible starting values for your server.

PostgreSQL's defaults are deliberately small so it can start on almost anything. On a real server, four settings make most of the difference.

shared_buffers: PostgreSQL's own cache

This is memory PostgreSQL uses to cache table and index pages. The default is 128 MB.

A common starting point on a dedicated database server is about 25% of RAM. PostgreSQL also relies on the operating system's page cache, so going much higher rarely helps and can hurt. There's also little point making it much larger than your data.

ALTER SYSTEM SET shared_buffers = '4GB';   -- needs a restart

Check the hit rate (it should be well above 99% on a warmed-up OLTP server):

SELECT round(100.0 * sum(blks_hit) / nullif(sum(blks_hit) + sum(blks_read), 0), 2) AS cache_hit_pct
FROM pg_stat_database;

effective_cache_size: a hint, not an allocation

This tells the planner how much memory is available for caching in total (shared_buffers plus the OS cache). It allocates nothing. Set too low, the planner underestimates how likely data is to be in memory and avoids index scans it should use. 50–75% of RAM is typical.

ALTER SYSTEM SET effective_cache_size = '12GB';
SELECT pg_reload_conf();

work_mem: per operation, not per server

work_mem is how much memory each sort or hash operation may use before spilling to temporary files on disk. The default is 4 MB. The catch: one query can run several sort and hash steps at once, and every connection can do this at the same time. So work_mem × operations × connections can add up fast.

Raise it carefully, and only when you can see spills:

ALTER SYSTEM SET log_temp_files = '10MB';   -- log any temp file over 10 MB
SELECT pg_reload_conf();

Then check temp_bytes in pg_stat_database and the log. You can also raise it for one session or one role that runs reports:

ALTER ROLE reporting SET work_mem = '128MB';

maintenance_work_mem: for VACUUM and CREATE INDEX

Used by VACUUM, CREATE INDEX and ALTER TABLE ADD FOREIGN KEY. The default of 64 MB makes index builds and vacuum of big tables slower than they need to be. 256 MB to 1 GB is common on servers with 8 GB or more. Autovacuum workers use autovacuum_work_mem, which falls back to this value, and each worker can use it, so don't go overboard if you run many workers.

A starting point

Setting8 GB server32 GB server
shared_buffers2 GB8 GB
effective_cache_size6 GB24 GB
work_mem16 MB32 MB
maintenance_work_mem512 MB1 GB

These are starting points for a dedicated server with a typical web workload. Measure and adjust: the right values depend on your data size, connection count and queries.

SQLNirvana sizes these from your server's actual memory, data size and measured spills, and applies them with ALTER SYSTEM after you approve. The free PostgreSQL check gives you a one-off version if you enter your RAM.

Let SQLNirvana watch it for you

Health, tuning, security and upgrade checks every minute. Free for one server.

Start free