Performance Tuning
Key configuration settings
Setting | What it controls |
|---|---|
| The amount of memory PostgreSQL dedicates to its own shared cache of data pages. Commonly set to around 25% of system RAM as a starting point. |
| Memory available to a single sort or hash operation before it spills to disk. Too low and complex queries spill temp files to disk; too high and many concurrent connections running such operations can exhaust system memory. |
| An estimate (not an allocation) of how much memory is available for disk caching overall, including the OS cache. It only influences planner cost estimates — set too low, the planner may avoid indexes it should use. |
Inspect current settings
SHOW shared_buffers;
SHOW work_mem;
SHOW effective_cache_size;
-- Or query them directly:
SELECT name, setting, unit FROM pg_settings
WHERE name IN ('shared_buffers', 'work_mem', 'effective_cache_size');Connection pooling
PostgreSQL is process-based: every client connection gets its own backend process on the server. That model is simple and robust, but it means connections are not free.
Routine maintenance
Monitoring slow queries
Top 5 queries by total execution time
CREATE EXTENSION IF NOT EXISTS pg_stat_statements; SELECT query, calls, total_exec_time, mean_exec_time FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
Right-size
shared_buffers,work_mem, andeffective_cache_sizefor your hardware and workload, then re-measure.Put a connection pooler in front of PostgreSQL once your application opens more than a small, fixed number of connections.
Let autovacuum run, and watch for tables it is falling behind on rather than disabling it.
Keep
pg_stat_statementsenabled in every environment where performance matters — it costs little and answers "what is actually slow" directly.