Introducing Dragonfly Cloud! Learn More

Question: What is the recommended configuration for PostgreSQL?

Answer

PostgreSQL's performance can be significantly influenced by its configuration. The default configuration it ships with is quite conservative and intended to ensure it runs on a wide variety of hardware. For production environments, several key settings should be tuned based on your server's hardware specifications and the specific workload. Here are some important parameters to consider:

1. max_connections

This parameter defines the maximum number of concurrent connections to the database server. Its optimal value depends on the application's connection needs and available system resources. High numbers can lead to increased memory usage and contention.

max_connections = 200

2. shared_buffers

This setting determines how much memory is dedicated to PostgreSQL for caching data. A general starting point is setting this to about 25% of the total system RAM. However, do not exceed 40% as PostgreSQL also relies on the operating system's cache.

shared_buffers = '4GB'

3. work_mem

This parameter specifies the amount of memory to be used by internal sort operations and hash tables before writing to temporary disk files. Increasing this value might improve performance if you have complex queries that involve sorts or joins.

work_mem = '64MB'

4. maintenance_work_mem

This is used during maintenance tasks such as VACUUM, CREATE INDEX, and ALTER TABLE ADD FOREIGN KEY. It's advisable to make this significantly larger than work_mem.

maintenance_work_mem = '512MB'

5. effective_cache_size

This parameter tells the planner an estimate of how much memory is available for disk caching by the operating system and within PostgreSQL itself. It does not allocate this memory but serves as a guideline to optimize query plans.

effective_cache_size = '8GB'

6. wal_buffers

WAL buffers are crucial for transaction management. If you have a high volume of writes, consider increasing this.

wal_buffers = '16MB'

7. checkpoint_completion_target

This parameter aims to spread out write activity due to checkpoints over time and ideally should be set close to 0.9, allowing more time for checkpoints to occur thus reducing the I/O spikes.

checkpoint_completion_target = 0.9

Monitoring and Logs

To properly adjust these settings, monitoring system and PostgreSQL performance over time is crucial. Utilize tools like pg_stat_activity and pgBadger for log analysis, and always test changes in a staging environment before applying them in production.

Conclusion

Optimizing PostgreSQL configuration is a balance between system resources and workload characteristics. Start with these guidelines, monitor the effects, and iteratively adjust the parameters to suit your specific use cases.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.