PostgreSQL Connection Tuning
Tune shared_buffers, work_mem by RAM.
Tuning PostgreSQL Memory and Connections
PostgreSQL's default configuration is conservative — designed to run on minimal hardware. Tuning memory settings based on your server's RAM and workload type can improve query performance by 2-5x.
Key Memory Parameters
| Parameter | Default | Recommended | Purpose |
|---|---|---|---|
| shared_buffers | 128 MB | 25% of RAM | Shared cache for data pages |
| effective_cache_size | 4 GB | 75% of RAM | Planner's estimate of OS cache size |
| work_mem | 4 MB | 32-256 MB | Memory per sort/hash operation |
| maintenance_work_mem | 64 MB | 512 MB - 2 GB | Memory for VACUUM, CREATE INDEX |
| wal_buffers | -1 (auto) | 64 MB | WAL write buffer |
Tuning by Server Size
16 GB RAM Server (Common for SMBs)
shared_buffers = 4GB
effective_cache_size = 12GB
work_mem = 64MB
maintenance_work_mem = 1GB
wal_buffers = 64MB
max_connections = 200
64 GB RAM Server (Production)
shared_buffers = 16GB
effective_cache_size = 48GB
work_mem = 128MB
maintenance_work_mem = 2GB
wal_buffers = 64MB
max_connections = 300
Connection Limits
Each connection uses ~10 MB of RAM. With max_connections = 200, that's 2 GB just for connections. For applications needing more than 200 concurrent connections, use PgBouncer for connection pooling instead of increasing max_connections.
work_mem Considerations
Warning: work_mem is allocated per-operation, not per-connection. A complex query with 5 sorts could use 5 × work_mem. Set it conservatively at the server level and increase per-session for analytics queries:
-- For an analytics session
SET work_mem = '512MB';
SELECT ... complex aggregation query ...;
RESET work_mem;
Eazy SaaS Tip: Use PGTune as a starting point for your server size, then adjust based on monitoring. Watch for
temp_filesin pg_stat_database — if high, increase work_mem.