Sub topic 2
Measuring Before Tuning: Statistics Views, EXPLAIN, and Logs
Assume you know basic SQL, can run queries in psql, can read a simple EXPLAIN plan, and can reload PostgreSQL configuration. Tuning starts with evidence. A setting that looks reasonable can still waste memory or increase disk I/O on a different workload.
Establish a baseline
PostgreSQL exposes cumulative statistics through views such as pg_stat_database. The following query estimates the share of block requests served from PostgreSQL's shared buffer cache:
SELECT datname,
blks_hit,
blks_read,
round(
blks_hit::numeric / nullif(blks_hit + blks_read, 0),
4
) AS hit_ratio
FROM pg_stat_database
WHERE datname = current_database();A low ratio can indicate insufficient caching, but it is not proof that shared_buffers is too small. A block counted in blks_read may have been served by the operating system's page cache after PostgreSQL requested it from storage. Also record the workload period, database size, active connections, and representative query response times.
You can inspect the current value and whether a change needs a reload or restart:
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',
'random_page_cost',
'max_connections'
)
ORDER BY name;The context column describes when PostgreSQL reads a setting. A user or sighup setting can usually change without restarting the server. A postmaster setting requires a restart. The pending_restart column shows whether a restart is still needed after a change.
Find memory-related work with EXPLAIN
Use EXPLAIN (ANALYZE, BUFFERS) on a representative query, preferably in a safe environment first. ANALYZE runs the query, while BUFFERS reports buffer activity.
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
ORDER BY created_at;Look for the sort details in the output. Sort Method: quicksort Memory: ... means the sort fit in memory. Sort Method: external merge Disk: ... means it spilled to temporary files. Spilling can increase latency, but raising work_mem globally may create an out-of-memory condition because each sort or hash node can use its own allocation.
Temporary-file statistics show the broader pattern:
SELECT datname,
temp_files,
pg_size_pretty(temp_bytes) AS temp_size
FROM pg_stat_database
ORDER BY temp_bytes DESC;These counters are cumulative since the statistics were reset. Compare them over equal time windows, or reset statistics only when your monitoring process allows it.
Use logs for checkpoints and spills
Enable logging that connects observed behavior to configuration:
# postgresql.conf
log_checkpoints = on
log_temp_files = 0log_checkpoints records checkpoint timing, write volume, and synchronization time. log_temp_files = 0 logs every temporary file, including its size. A high number of temporary files identifies pressure, but the log entry does not by itself prove that a global work_mem increase is safe.
Exercise
An EXPLAIN plan reports an external merge sort, but temporary files are rare and the server has many active connections. What should you do first?
Show answer
Confirm the query and workload are representative, then test a session- or role-level workmem increase. Do not immediately raise workmem globally, because several memory-hungry nodes can run concurrently.