DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Database Query Service With OpenAI and PostgreSQL in .NET
  • PostgreSQL 12 End of Life: What to Know and How to Prepare
  • Formulating a Robust Strategy for Storage in Amazon Relational Database Service PostgreSQL Deployments
  • Point-In-Time Recovery (PITR) in PostgreSQL

Trending

  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • My LLM Journey as a Software Engineer Exploring a New Domain
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  1. DZone
  2. Data Engineering
  3. Databases
  4. PostgreSQL Connection Pooling: Part 3 – Pgpool-II

PostgreSQL Connection Pooling: Part 3 – Pgpool-II

In this article, look at part three of PostgreSQL connection pooling.

By 
Aparajita Raychaudhury user avatar
Aparajita Raychaudhury
·
Apr. 13, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
14.6K Views

Join the DZone community and get the full member experience.

Join For Free

In our previous posts in this series, we discussed the case for connection pooling and introduced PgBouncer for PostgreSQL. In this post, we will discuss its most popular alternative – Pgpool-II.

Pgpool-II is the swiss army knife of PostgreSQL middleware. It supports high-availability, provides automated load balancing, and has the intelligence to balance the load between masters and slaves so write loads are always directed at masters, while read loads are directed to slaves. Pgpool-II also provides logical replication. While its use and importance have decreased as the inbuilt replication options improved on PostgreSQL server side, this still remains a valuable option for older versions of PostgreSQL. On top of all this, it also provides connection pooling!

At a glance

Setting up Pgpool-II

Follow these steps to set up Pgpool-II, enable the connection pool services you need, and connect to your PostgreSQL server.

ScaleGrid Blog - At a glance overview - 2nd point

How it works

Check out the Pgpool-II architecture that supports all of its features, and learn how the connection pooler works.

ScaleGrid Blog - At a glance overview - 3rd point

What doesn’t Pgpool-II do?

Review the limitations of Pgpool-II to see if it’s the right connection pooler for your application.

Setting up Pgpool-II

Pgpool-II binaries are distributed through Pgpool-II’s repositories – you can read more about installation in this help doc. Once installed, we must configure Pgpool-II to enable the services we want, and connect to the PostgreSQL server. You can read more about it here.

To get a minimal pooling setup up, you must provide the following:

  • The username and md5 encrypted password of the user(s) who’ll connect to Pgpool-II — this must be defined in a separate file, which can be easily generated using the pg_md5 util.
  • Interfaces/IP-addresses and port number to listen to for incoming connections — this must be defined in the configuration file.
  • The hostname of the backend server(s) [More than one server is specified only if we wish to use replication and/or load balancing].
  • The services you wish to enable. By default, connection pooling is on, and other services are off in the configuration file installed with the binaries.

And that’s it — we are ready to go! While the configurations available with Pgpool-II might be more daunting at first sight, the folks behind Pgpool-II have really made it easy for us!

How It Works

Pgpool-II has a more involved architecture than PgBouncer in order to support all the features it does. However, in this section, we will limit ourselves to describing how connection pooling works.

The Pgpool-II parent process forks 32 child processes by default – these are available for connection. The architecture is similar to PostgreSQL server: one process = one connection. It also forks the ‘pcp process’ which is used for administrative tasks, and beyond the scope of this post. The 32 children are now ready to accept connections. Like PgBouncer, these also emulate a PostgreSQL server – clients can connect with the exact same connection string as they would to a normal PostgreSQL server.

Pgpool-II Connection Pool Diagram - ScaleGrid Blog

The kernel directs incoming connections to one of the child processes that have registered as listeners. Neither the main Pgpool-II process nor the end-users have any control over which child process responds to an incoming request. Any idle child can pick up the request. If no idle children are found, the connection request will be queued on the kernel side – this can cause applications like pgbench to hang, waiting for client connections.

Once an idle Pgpool-II child receives a connection request, it:

  1. Checks for the username in its password file. If not found, it rejects the connection.
  2. If the username is found, it checks the provided password against the md5 hash stored in this file.
  3. Once authentication succeeds, it checks if it already has a cached connection for this database+user combination.
  4. If it does, it returns the connection to the client. If it does not, it opens a new connection.
  5. All requests and responses pass through Pgpool-II while it waits for the client to disconnect.
  6. Once the client disconnects, Pgpool-II has to decide whether to cache the connection:
    • If it has an empty slot, it caches it.
    • If it doesn’t have an empty slot (that is, caching this connection would exceed the max_pool_size allowed), it will decide based on an internal algorithm.
  7. If it does decide to cache the connection, it will run the preconfigured reset query to clean up all session details and make it safe for reuse by a different client.
  8. Now the child process is free to pick up more connections.


Expert Tip

It is important to continuously monitor the health of your MySQL master and slave servers so you can detect potential issues and take corrective actions.
Learn how

What Doesn’t Pgpool-II Do?

Unfortunately, for those focusing only on connection pooling, what Pgpool-II doesn’t do very well is connection pooling, especially for a small number of clients. Because each child process has its own pool, and there is no way to control which client connects to which child process, too much is left to luck when it comes to reusing connections.

As you can see, Pgpool and PgBouncer have rather differing strengths. In our final post of the series, we will do head-to-head testing and feature comparison! Stay tuned!

Connection (dance) PostgreSQL Database

Published at DZone with permission of Aparajita Raychaudhury. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Database Query Service With OpenAI and PostgreSQL in .NET
  • PostgreSQL 12 End of Life: What to Know and How to Prepare
  • Formulating a Robust Strategy for Storage in Amazon Relational Database Service PostgreSQL Deployments
  • Point-In-Time Recovery (PITR) in PostgreSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!