PgBouncer Enhances PostgreSQL Performance in High-Traffic Environments

05 Jul 2024

Managing database connections effectively is crucial for maintaining the performance and reliability of your PostgreSQL instances. PostgreSQL’s performance can degrade with an excessive number of concurrent connections, making connection pooling solutions like PgBouncer essential for high-traffic environments. In this article, we’ll walk through the steps to set up PgBouncer, a lightweight connection pooler for PostgreSQL, and configure it to optimize your database connections.

Why Use PgBouncer?

PostgreSQL can handle a limited number of concurrent connections efficiently. It is recommended to set the maximum connections lower than 10 per CPU core, with a maximum of 20 per CPU core as the limit. If your application needs to handle more user sessions, using a connection pooler like PgBouncer becomes necessary. PgBouncer reduces the overhead of establishing and closing connections by reusing a pool of established connections.

Prerequisites

Before we start, ensure you have the following dependencies installed on your system:

  • PostgreSQL
  • Python3 with psycopg2

You can install these dependencies using the following commands:

dnf install python3-psycopg2-2.9.9-1PGDG.rhel9.x86_64.rpm # Dependency
dnf install pgbouncer-1.22.1-42PGDG.rhel9.x86_64.rpm

Setting Up PgBouncer

1. Configure User Authentication

First, we’ll create a userlist file for PgBouncer to manage user authentication.

psql -Atq -h 10.*.**.** -p 5432 -U postgres -d postgres -c "SELECT concat('"', usename, '" "', passwd, '"') FROM pg_shadow" >> /etc/pgbouncer/userlist.txt

2. Edit the PgBouncer Configuration

Next, we’ll configure the pgbouncer.ini file. Open the file in your preferred text editor:

vi /etc/pgbouncer/pgbouncer.ini

Add the following configuration:

[databases]
* = port=5432

[pgbouncer]
pool_mode = session
listen_addr = 10.5.**.**
default_pool_size = 100
reserve_pool_size = 20
max_client_conn = 500
admin_users = postgres
ignore_startup_parameters = extra_float_digits
logfile = /var/log/pgbouncer/pgbouncer.log
pidfile = /var/run/pgbouncer/pgbouncer.pid
listen_port = 6432
auth_file = /etc/pgbouncer/userlist.txt
client_tls_sslmode = require
client_tls_ca_file = /etc/pgbouncer/root.crt
client_tls_key_file = /etc/pgbouncer/pgbouncer.key
client_tls_cert_file = /etc/pgbouncer/pgbouncer.crt

3. Restart and Enable PgBouncer

After editing the configuration, reload and restart the PgBouncer service to apply the changes:

systemctl restart pgbouncer
systemctl enable pgbouncer

You can check the status of the PgBouncer service to ensure it is running correctly:

systemctl status pgbouncer

4. Connect to PgBouncer

Now, you can perform your database operations through the PgBouncer port:

psql -p 6432

To connect to the PgBouncer manager interface:

psql -p 6432 pgbouncer

5. Monitoring and Managing PgBouncer

To monitor pool management:

show pools;

To review statistics:

show stats;

6. Update System Limits

Finally, update the systemd service file to increase the file descriptor limits. To ensure that PgBouncer can handle a large number of concurrent connections, you need to increase the file descriptor limits. File descriptors are integral resources used by the operating system to manage open files and sockets. Each connection to the database through PgBouncer consumes a file descriptor. If the limit is too low, PgBouncer will not be able to accept additional connections.

What is pgbouncer in postgresql?

PgBouncer is a lightweight connection pooler for PostgreSQL. It reduces the overhead of creating new connections by maintaining a pool of connections that can be reused by incoming client requests. This helps improve performance, especially in high-load environments, by managing connection limits and providing efficient use of database resources.

Pgbouncer why?

Using PgBouncer is beneficial because it optimizes the handling of database connections. It reduces the overhead associated with establishing new connections, thus improving application performance and reducing resource consumption. PgBouncer also helps manage the maximum number of connections to the database, preventing overloads and ensuring more reliable application behavior under heavy load.
uTorrent

uTorrent

Latest update uTorrent download for free for Windows PC or Android mobile

5
1032 reviews
4861512
downloads
Zona

Zona

Latest update Zona download for free for Windows PC or Android mobile

4
614 reviews
806337
downloads
Minecraft

Minecraft

Latest update Minecraft download for free for Windows PC or Android mobile

5
750 reviews
415591
downloads
Geometry Dash

Geometry Dash

Latest update Geometry Dash download for free for Windows PC or Android mobile

4
539 reviews
292977
downloads

Exploring Techniques for Water Extraction in Dune Awakening

Water is an essential element in Dune Awakening. Learn about the innovative methods of water acquisition including Blood Purifier, Stillsuit, and Windtraps.

Read more

Helldivers Update Enhances Stealth as New Threats Emerge

Arrowhead's Helldivers 2 patch enhances stealth detection and introduces new Terminid threats to Super Earth.

Read more

Warhammer 40,000: Space Marine Return With a Remaster

Space Marine enthusiasts are in for a treat as Warhammer 40,000: Space Marine returns fully remastered with updated visuals, controls, and multiplayer features, including cross-play capabilities. The Master Crafted Edition is now available on Steam for $39.99.

Read more

Exploring Compatibility on Arm Windows Devices

Arm highlights compatibility challenges for Windows games, focusing on anti-cheat programs. Continued growth of Arm devices may encourage developers to bolster support, improving gaming compatibility.

Read more

Norton 360 Deluxe Offers Comprehensive Antivirus Protection

Norton 360 Deluxe delivers a robust antivirus solution with extensive protections, scanning options, and user-friendly interface, though at a higher price compared to competitors. Frequent upgrade prompts are a noted downside.

Read more

Dune: Awakening Surges in Steam Sales Rankings

Dune: Awakening achieved the second-highest sales on Steam, just behind Counter-Strike 2. Despite only releasing on June 10th, early access boosted its position. Deltarune and Elden Ring: Nightreign followed closely, reflecting a vibrant mix in the gaming market.

Read more

UFL: A New Player Emerges in the PC Football Simulator Arena

Explore the latest PC football simulation game, UFL, developed by Xten. With a mix of real players and fictional teams, UFL offers a PES-style gameplay experience. Available for free on Steam.

Read more

Clair Obscur Expedition Unveils Major Game Updates

Clair Obscur Expedition 33's latest patch modifies RPG difficulty, allowing players to enjoy the narrative by easing story mode. Players can rematch bosses or opt for an easier mode with expanded parry and dodge abilities, while adjusting challenge modifiers for varied play experiences.

Read more

Wildgate Gains Momentum with New Demo and Open Beta

Wildgate, an FPS by Dreamhaven's Mike Morhaime, surges on Steam due to a new demo and open beta, drawing over 13,000 players. Release set for July 22, 2025.

Read more

Gladius Available Free on GOG; Limited Time Offer

Gladius is now free on GOG, offering players a chance to explore epic battles on Gladius Prime. This Warhammer 40k game includes sale discounts on its DLCs. Hurry, the offer expires soon.

Read more