DEV Community

Matthew LaFalce
Matthew LaFalce

Posted on

Optimizing PostgreSQL for High Connections: A Comprehensive Guide

PostgreSQL is a powerful and versatile database management system that can handle a wide range of applications. However, optimizing PostgreSQL to handle a high number of concurrent connections requires careful configuration and planning. In this guide, we will walk you through the process of adjusting key PostgreSQL settings to support 300 connections, ensuring your server performs efficiently.

Understanding Key Configuration Parameters

Before diving into the configuration changes, it is important to understand the key parameters that influence memory usage and performance in PostgreSQL:

  1. shared_buffers: This parameter defines the amount of memory allocated for shared memory buffers, which PostgreSQL uses to cache data. It is typically recommended to set shared_buffers to 25% of the total RAM.

  2. work_mem: This parameter determines the amount of memory allocated for internal sort operations and hash tables. Each connection can use multiple times this amount if queries involve multiple sorts or hash operations.

  3. maintenance_work_mem: This parameter specifies the amount of memory allocated for maintenance tasks like VACUUM and CREATE INDEX. It is usually higher than work_mem since maintenance operations can be memory-intensive but are not performed as frequently.

  4. max_connections: This parameter sets the maximum number of concurrent connections to the PostgreSQL server. Increasing this value can significantly impact memory usage and performance.

Calculating Memory Requirements

To ensure your server can handle 300 connections efficiently, let's calculate the memory requirements based on the following settings:

  • shared_buffers: 8GB
  • work_mem: 32MB
  • maintenance_work_mem: 1GB
  • Additional Memory Usage: 15MB per connection for internal operations

Memory Usage per Connection

For each connection, the memory usage can be estimated as follows:

  • work_mem: 32MB
  • Additional memory: 15MB

Total memory per connection:
Memory per connection = 32MB + 15MB = 47MB

Total Memory Usage for 300 Connections

To calculate the total memory usage for 300 connections:
Total memory for connections = 300 x 47MB = 14100MB = 13.77GB

Adding the fixed overhead for shared_buffers:
Total memory usage = 13.77GB + 8GB = 21.77GB

Including maintenance_work_mem for peak usage scenarios:
Peak memory usage = 21.77GB + 1GB = 22.77GB

With these settings, a server with 32GB of RAM will have:
32GB - 22.77GB = 9.23GB

remaining for the operating system and other applications. This margin ensures that the server can function efficiently under the specified load.

Modifying Configuration Settings

To apply these changes, you can either edit the postgresql.conf file directly or use SQL commands.

Dynamic Settings

These settings can be changed without restarting the PostgreSQL server:

ALTER SYSTEM SET work_mem = '32MB';
ALTER SYSTEM SET maintenance_work_mem = '1GB';
Enter fullscreen mode Exit fullscreen mode

Reload the configuration to apply changes:

SELECT pg_reload_conf();
Enter fullscreen mode Exit fullscreen mode

Static Settings

These settings require a server restart after modification:

ALTER SYSTEM SET max_connections = 300;
ALTER SYSTEM SET shared_buffers = '8GB';
Enter fullscreen mode Exit fullscreen mode

After making these changes, restart the PostgreSQL service:

sudo systemctl restart postgresql
Enter fullscreen mode Exit fullscreen mode

Monitoring and Adjusting

After configuring your PostgreSQL server, it is crucial to monitor its performance and make adjustments as needed. Use tools like pg_stat_activity, top, or htop to keep an eye on memory usage and ensure the server is not running out of resources.

Conclusion

Optimizing PostgreSQL to handle a high number of connections involves carefully calculating memory requirements and adjusting key configuration parameters. By setting shared_buffers, work_mem, maintenance_work_mem, and max_connections appropriately, you can ensure your server performs efficiently even under heavy load. Remember to monitor the server's performance continuously and make necessary adjustments to maintain optimal performance.

Top comments (3)

Collapse
 
jangelodev profile image
João Angelo

Hi Matthew LaFalce,
Top, very nice and helpful !
Thanks for sharing.

Collapse
 
whattheportal profile image
WTP | WhatThePortal.com

I'm glad there's people in the world like you, so the rest of us can sleep more soundly knowing our DB configs are in good care : )

Collapse
 
martinbaun profile image
Martin Baun

This is a nice overview of many useful optimization methods! I learned some tricks from it. Which, on SQL, does not happen that much.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.