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:
| Parameter | Default | Meaning and guidance |
|---|---|---|
shared_buffers | 128MB | Shared page cache. A common starting point is about 25% of RAM on a dedicated server; test upwards |
effective_cache_size | 4GB | Planner hint for total cache (shared buffers + OS cache). Often set to 50 to 75% of RAM; allocates nothing |
work_mem | 4MB | Per sort/hash node, per backend. Raise per query or role before raising globally |
maintenance_work_mem | 64MB | VACUUM, CREATE INDEX; safe to set much higher (hundreds of MB to a few GB) |
max_connections | 100 | Keep low and use a pooler (pg-connection-pooling) |
random_page_cost | 4 | Tuned for spinning disks; on SSD/NVMe, 1.1 to 1.5 favours index scans |
max_wal_size | 1GB | Raise on write-heavy systems so checkpoints are timed, not forced (pg-wal-checkpoints) |
checkpoint_timeout | 5min | Longer means fewer full-page images, slower crash recovery |
autovacuum_vacuum_scale_factor | 0.2 | Lower for big tables, per table (pg-vacuum-bloat) |
default_transaction_isolation | read committed | Change 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_bufferstoo large leaves less for the OS cache andwork_mem; extremely large values rarely help.- On Kubernetes or containers set memory limits and size
shared_buffers/work_memagainst 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
- Why is raising
work_memglobally dangerous with many connections? - On NVMe storage, which planner setting is most likely wrong at its default?
- How do you tell whether autovacuum is keeping up?
References
- PostgreSQL manual, Server Configuration: https://www.postgresql.org/docs/current/runtime-config.html
- PostgreSQL wiki, Tuning Your PostgreSQL Server: https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server
Related
pg-explain-analyze · pg-vacuum-bloat · pg-wal-checkpoints · pg-monitoring