Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 185Data · PostgreSQL

How to tune PostgreSQL memory settings

A fresh PostgreSQL install uses tiny memory defaults (shared_buffers = 128 MB) that fit a laptop, not a 16 GB database server. A few settings make a big difference: the shared cache, how much the planner may count on the OS cache, memory per sort, and memory for maintenance. Set them too high and the Linux OOM killer stops the database. This how-to sets safe starting values, applies them, and checks whether queries still spill to disk.

  • 5 Lessons
  • 5 Commands

$ run as a normal user# run as root (or with sudo)

185.1

The quick answer

For a dedicated server with 16 GB RAM:

sudo -u postgres psql -c "ALTER SYSTEM SET shared_buffers = '4GB';"
sudo -u postgres psql -c "ALTER SYSTEM SET effective_cache_size = '12GB';"
sudo -u postgres psql -c "ALTER SYSTEM SET work_mem = '32MB';"
sudo -u postgres psql -c "ALTER SYSTEM SET maintenance_work_mem = '1GB';"
systemctl restart postgresql
sudo -u postgres psql -c "SHOW shared_buffers;"

185.2

Step by step

  1. See the current values and which need a restart:
sudo -u postgres psql -c "SELECT name, setting, unit, context FROM pg_settings WHERE name IN ('shared_buffers','effective_cache_size','work_mem','maintenance_work_mem','max_connections');"

context = postmaster means restart needed; user can even be set per session.

  1. shared_buffers ≈ 25 % of RAM — PostgreSQL's own page cache. More is rarely better, because the OS cache also holds the data.
  2. effective_cache_size ≈ 50–75 % of RAM — no memory is reserved; it only tells the planner how much data is probably cached, so it chooses index scans more often.
  3. work_mem with care — memory per sort or hash step, per connection. One complex query can use it several times; 200 connections × 3 steps × 32 MB = 19 GB. Start at 16–64 MB and raise it only for heavy reports:
sudo -u postgres psql -d shop -c "SET work_mem = '256MB'; EXPLAIN (ANALYZE, BUFFERS) SELECT customer_id, sum(total) FROM orders GROUP BY customer_id ORDER BY 2 DESC;"

Sort Method: external merge Disk: … means the sort did not fit in memory.

  1. maintenance_work_mem 512 MB – 2 GB — faster VACUUM, CREATE INDEX and restores.
  2. Fewer connections, not more memory — keep max_connections around 100–200 and put PgBouncer in front of busy apps.
  3. Apply — ALTER SYSTEM writes postgresql.auto.conf. Reload for most settings (SELECT pg_reload_conf();), restart for shared_buffers. Check SELECT name FROM pg_settings WHERE pending_restart;.
  4. Protect against the OOM killer — set vm.overcommit_memory = 2 (with a suitable vm.overcommit_ratio) on dedicated servers, so a big query gets an error instead of the whole server being killed.

185.3

Check that it worked

SHOW shared_buffers; returns 4GB; pending_restart is empty. Over the next days the cache hit ratio stays high and temporary files go down:

sudo -u postgres psql -c "SELECT datname, round(100.0*blks_hit/nullif(blks_hit+blks_read,0),2) AS hit_pct, temp_files, pg_size_pretty(temp_bytes) AS temp FROM pg_stat_database WHERE datname NOT LIKE 'template%';"

185.4

Common problems

ProblemCauseFix
PostgreSQL does not start after the changeshared_buffers larger than the kernel allows or than RAM.Lower it; check the log; ALTER SYSTEM RESET shared_buffers if needed.
Server killed by OOM killerwork_mem × connections too high.Lower work_mem; PgBouncer; vm.overcommit_memory = 2.
Setting has no effectNeeds a restart, or overridden in postgresql.auto.conf.Check pending_restart and SELECT * FROM pg_file_settings WHERE name='…';.
Many temp filesSorts and hashes spill to disk.log_temp_files = 0 to find them; more work_mem for those queries only; indexes.
Hit ratio low after restartCache is cold.Normal; it warms up (or use pg_prewarm).
Container/VM limit smaller than "RAM"Values based on the host, not the cgroup limit.Calculate from the container's memory limit.

185.5

Go further

1Why is work_mem dangerous to set high?

It is used per sort or hash step in every connection, so many connections can use many times that amount at once.

2Does effective_cache_size reserve memory?

No, it is only a hint for the planner about how much data the OS cache probably holds.

All commands in this chapter