Sub topic 15
A Repeatable Tuning Workflow
Assume you can run basic SQL in psql, edit postgresql.conf, reload or restart PostgreSQL, and read a simple EXPLAIN plan. You also need a workload that represents real use, such as a test script or a known reporting query.
Tuning is a controlled experiment. Measure the current behavior, change one related setting, run the same workload, and compare the results. Do not change several memory and I/O settings at once. If performance changes, you will not know which setting caused it.
Establish a baseline
Record the current settings and the symptoms you are trying to improve. A symptom might be temporary-file growth, slow checkpoints, high read latency, or an out-of-memory event.
SELECT name, setting, unit, context, pending_restart
FROM pg_settings
WHERE name IN (
'shared_buffers', 'work_mem', 'maintenance_work_mem',
'effective_cache_size', 'max_wal_size',
'checkpoint_timeout', 'checkpoint_completion_target',
'random_page_cost', 'effective_io_concurrency'
)
ORDER BY name;Collect measurements before changing anything:
- Query latency and throughput for a representative workload.
EXPLAIN (ANALYZE, BUFFERS)output for important queries.- Temporary-file counts and sizes from
pg_stat_database. - Checkpoint messages and temporary-file messages from the server log.
- Host memory, swap, disk latency, and I/O utilization.
Save these results with a timestamp. A tuning change is useful only if you can compare it with the baseline.
Change and verify safely
Prefer the smallest change that tests your hypothesis. For example, if an analytical sort spills to disk, increase work_mem for the reporting role instead of increasing it globally.
ALTER ROLE reporting SET work_mem = '256MB';Run the same query or workload again. Check whether the plan changed, whether the sort still spills, and whether memory pressure or concurrent-query latency increased.
Use context and pending_restart to determine how PostgreSQL applies a setting:
- A
userorsessionsetting can usually change for a session or role. - A
sighupsetting needs a configuration reload. - A
postmastersetting needs a server restart. pending_restartindicates that a changed value is waiting for a restart.
For a reload-only setting, apply the change deliberately and verify the value:
ALTER SYSTEM SET work_mem = '32MB';
SELECT pg_reload_conf();
SHOW work_mem;Use ALTER SYSTEM sparingly. It writes to postgresql.auto.conf, so document why the setting exists and how to remove it. A role or database setting is often narrower and safer.
Decide whether to keep the change
Keep a change only when it improves the target metric without causing unacceptable side effects. For example, fewer temporary files do not justify a change that causes the operating system to swap.
Test under realistic concurrency. work_mem applies to each sort or hash operation, not once per connection. A query with multiple memory-hungry nodes can therefore use many times the configured value. Include concurrent maintenance jobs when evaluating maintenance_work_mem.
After each experiment, record:
- The old and new values.
- The hypothesis and workload used.
- The observed metric changes.
- Any errors, warnings, or memory pressure.
- Whether the setting needs a reload or restart.
- The rollback value.
A rollback is part of the experiment, not an admission of failure. If the result is unclear, restore the old value and collect better measurements before trying again.
Exercise
A query spills to temporary files, but increasing global work_mem causes memory pressure. What is the safest next experiment?
Show answer
Keep the global value unchanged and set a larger work_mem only for the role or session that runs the query. Re-run the same workload, inspect EXPLAIN (ANALYZE, BUFFERS), and check host memory and temporary-file metrics.