Overview

PostgreSQL ships with conservative defaults that run on a laptop. Tuning means setting memory, planner, WAL and autovacuum parameters to match your hardware and workload. Most gains come from queries and indexes (pg-explain-analyze); configuration is the second step.

Core concepts

Defaults observed on PostgreSQL 17:

ParameterDefaultMeaning and guidance
shared_buffers128MBShared page cache. A common starting point is about 25% of RAM on a dedicated server; test upwards
effective_cache_size4GBPlanner hint for total cache (shared buffers + OS cache). Often set to 50 to 75% of RAM; allocates nothing
work_mem4MBPer sort/hash node, per backend. Raise per query or role before raising globally
maintenance_work_mem64MBVACUUM, CREATE INDEX; safe to set much higher (hundreds of MB to a few GB)
max_connections100Keep low and use a pooler (pg-connection-pooling)
random_page_cost4Tuned for spinning disks; on SSD/NVMe, 1.1 to 1.5 favours index scans
max_wal_size1GBRaise on write-heavy systems so checkpoints are timed, not forced (pg-wal-checkpoints)
checkpoint_timeout5minLonger means fewer full-page images, slower crash recovery
autovacuum_vacuum_scale_factor0.2Lower for big tables, per table (pg-vacuum-bloat)
default_transaction_isolationread committedChange per transaction rather than globally

Other levers: max_parallel_workers_per_gather, jit (can help long analytical queries and hurt short ones; consider turning it off for OLTP), huge_pages, synchronous_commit, track_io_timing = on (cheap on modern hardware, adds I/O time to EXPLAIN).

How to apply settings: ALTER SYSTEM SET ... then SELECT pg_reload_conf(); for reloadable ones; shared_buffers and max_connections need a restart. Check pg_settings.context and pending_restart. Scope narrower with ALTER ROLE ... SET, ALTER DATABASE ... SET, SET LOCAL.

Worked example

A tuning loop for a slow reporting query:

SET LOCAL work_mem = '128MB';          -- inside the transaction only
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;  -- 'Sort Method: external merge' should become 'quicksort'

Estimating work_mem risk: a query with 3 sort/hash nodes, run by 50 concurrent backends at 128MB, can use up to 3 x 50 x 128MB = 19GB. Set role-level overrides (ALTER ROLE reporting SET work_mem = '256MB') instead of a large global value.

Find the statements worth tuning first with pg_stat_statements (pg-monitoring): order by total_exec_time, not by slowest single call.

Pitfalls and production notes

  • Change one thing at a time and measure with a repeatable benchmark (pgbench, or replayed production queries).
  • Tuning parameters cannot fix a missing index or a bad query shape.
  • shared_buffers too large leaves less for the OS cache and work_mem; extremely large values rarely help.
  • On Kubernetes or containers set memory limits and size shared_buffers/work_mem against the container limit, and avoid swap.
  • Keep the configuration in version control (or the operator’s custom resource) and record why each setting differs from the default.
  • Start from a generated baseline such as PGTune, then validate; treat it as a guess.

Practice questions

  1. Why is raising work_mem globally dangerous with many connections?
  2. On NVMe storage, which planner setting is most likely wrong at its default?
  3. How do you tell whether autovacuum is keeping up?

References

pg-explain-analyze · pg-vacuum-bloat · pg-wal-checkpoints · pg-monitoring