PostgreSQL Connection Pooling: Part 3 – Pgpool-II
Tags: postgres, database, developer, architecture
Published: 2020-03-23 21:28:47

---
title: PostgreSQL Connection Pooling: Part 3 – Pgpool-II
published: true
description: ScaleGrid Blog - Learn about Pgpool-II connection pooling for PostgreSQL. How to set up, how it works, and limitations to see if it's right for your application.
tags: PostgreSQL, Database, Developer, Architecture,
---
<link rel=“canonical” href=“https://scalegrid.io/blog/postgresql-connection-pooling-part-3-pgpool-ii/” />
<a href="https://scalegrid.io/blog/postgresql-connection-pooling-part-3-pgpool-ii/"><img src="https://scalegrid.io/blog/wp-content/uploads/2020/03/PostgreSQL-Connection-Pooling-Part-3-Pgpool-II-ScaleGrid-Blog.jpg"></a>
<p style="text-align: justify;">In our previous posts in this series, we discussed the <a style="color: #2da964;" href="https://scalegrid.io/blog/postgresql-connection-pooling-part-1-pros-and-cons/" target="_blank">case for connection pooling</a> and <a style="color: #2da964;" href="https://scalegrid.io/blog/postgresql-connection-pooling-part-2-pgbouncer/" target="_blank">introduced PgBouncer</a>. In this post, we will discuss its most popular alternative - <a style="color: #2da964;" title="PgPool-II" href="https://www.pgpool.net/" target="_blank">Pgpool-II</a> - <a href="https://scalegrid.io/blog/postgresql-connection-pooling-part-3-pgpool-ii/">PostgreSQL Connection Pooling: Part 3 – Pgpool-II</a>
<p style="text-align: justify; padding-bottom: 15px;">Pgpool-II is the swiss army knife of PostgreSQL middleware. It supports high-availability, provides automated load balancing, and has the intelligence to balance 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 has 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!</p>


<h2><a id="1"></a>Setting up Pgpool-II</h2>
<p style="text-align: justify;">Pgpool-II binaries are distributed through Pgpool-II’s repositories - you can read more about installation in this <a style="color: #2da964;" href="http://www.pgpool.net/docs/pgpool-II-3.7.5/en/html/installation.html" target="_blank">help doc</a>. 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 <a style="color: #2da964;" href="http://www.pgpool.net/docs/pgpool-II-3.7.5/en/html/configuring-pcp-conf.html" target="_blank">here</a>.</p>
<p style="text-align: justify;">To get a minimal pooling setup up, you must provide the following:</p>

<ul style="text-align: justify;">
	<li>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.</li>
	<li>Interfaces/IP-addresses and port number to listen to for incoming connections - this must be defined in the configuration file.</li>
	<li>The hostname of the backend server(s) [More than one server is specified only if we wish to use replication and/or load balancing].</li>
	<li>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.</li>
</ul>
<p style="text-align: justify;">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!</p>

<h2><a id="2"></a>How it works</h2>
<p style="text-align: justify;">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.</p>
<p style="text-align: justify;">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.</p>
<a href="https://scalegrid.io/blog/wp-content/uploads/2020/03/Pgpool-II-Connection-Pool-Diagram-ScaleGrid-Blog.jpg"><img class="alignnone size-full wp-image-5194" style="padding: 15px 0px;" src="https://scalegrid.io/blog/wp-content/uploads/2020/03/Pgpool-II-Connection-Pool-Diagram-ScaleGrid-Blog.jpg" alt="Pgpool-II Connection Pool Diagram - ScaleGrid Blog" width="100%" height="auto" /></a>
<p style="text-align: justify;">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 - <i>this can cause applications like <a style="color: #2da964;" href="https://www.pgpool.net/mantisbt/view.php?id=364" target="_blank">pgbench to hang</a>, waiting for client connections.</i></p>
<p style="text-align: justify;">Once an idle Pgpool-II child receives a connection request, it:</p>

<ol>
	<li style="text-align: justify;">Checks for the username in its password file. If not found, it rejects the connection.</li>
	<li style="text-align: justify;">If the username is found, it checks the provided password against the md5 hash stored in this file.</li>
	<li style="text-align: justify;">Once authentication succeeds, it checks if it already has a cached connection for this database+user combination.</li>
	<li style="text-align: justify;">If it does, it returns the connection to the client. If it does not, it opens a new connection.</li>
	<li style="text-align: justify;">All requests and responses pass through Pgpool-II while it waits for the client to disconnect.</li>
	<li style="text-align: justify;">Once the client disconnects, Pgpool-II has to decide whether to cache the connection:
<ul>
	<li>If it has an empty slot, it caches it.</li>
	<li>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.</li>
</ul>
</li>
	<li style="text-align: justify;">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.</li>
	<li style="text-align: justify;">Now the child process is free to pick up more connections.</li>
</ol>
<a href="https://scalegrid.io/blog/wp-content/uploads/2020/03/Pgpool-II-Connection-Pool-Architecture-Diagram-ScaleGrid-Blog.jpg"><img class="alignnone size-full wp-image-5193" style="padding: 15px 0px;" src="https://scalegrid.io/blog/wp-content/uploads/2020/03/Pgpool-II-Connection-Pool-Architecture-Diagram-ScaleGrid-Blog.jpg" alt="Pgpool-II Connection Pool Architecture Diagram - ScaleGrid Blog" width="100%" height="auto" /></a>

<h2><a id="3"></a>What doesn’t Pgpool-II do?</h2>
<p style="text-align: justify;">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.</p>
<p style="text-align: justify; padding-bottom: 20px;">As you can see, Pgpool and PgBouncer have rather differing strengths - in our final post of the series, we will do a head-to-head testing, and feature comparison! Stay tuned!</p>