← Back to Articles Technology & IT Infrastructure

What Is PgPool? How It Works and How It Differs from PgBouncer

How Pgpool-II sits in front of PostgreSQL to reuse connections, spread read queries, and handle failover, where its limits lie, and when PgBouncer is enough.

PostgreSQL starts one server process for every client connection. The model is simple and stable, but each process uses memory, and opening a connection takes time for authentication and session setup. Once an application runs dozens of workers across several machines, the total can approach max_connections (typically 100 by default) even though most of those connections are idle.

Pgpool-II, commonly called PgPool, is middleware placed between an application and PostgreSQL. The application connects to it as if it were an ordinary PostgreSQL server, and Pgpool-II forwards the queries. From that position it can reuse connections, distribute read queries across servers, and redirect traffic when a server fails.

How Pgpool-II reuses connections

At startup, Pgpool-II prepares a number of child processes. Each one serves a single client session at a time, and num_init_children sets how many there are. When a client disconnects, the backend connection is not closed right away. The child keeps up to max_pool connections in its cache and reuses one when the next client arrives with the same user name, database, and run-time parameters.

Before a connection is handed to another client, its session state is cleared with the commands in reset_query_list, which default to ABORT and DISCARD ALL. Temporary tables and session settings from the previous client therefore do not carry over.

Two consequences should be calculated up front:

  • Clients that arrive while every child process is busy wait in a queue, or are refused if you configure it that way.
  • Connections to each PostgreSQL server can reach num_init_children × max_pool. As an illustration, 32 children with a max_pool of 4 can produce up to 128 connections, which already exceeds a limit of 100. If the application uses a single user and database, max_pool can be lowered to 1.

Distributing read queries

With one primary and several standby servers, Pgpool-II can send SELECT statements to the standbys and writes to the primary. The routing follows rules. SELECT ... FOR UPDATE and queries that call data-writing functions still go to the primary. Inside an explicit transaction, reads are balanced only if the isolation level is not SERIALIZABLE and no write has been issued yet.

PostgreSQL replication is asynchronous by default, so a standby may lag slightly. A user who has just saved an order could open the order list and not see it yet. Pgpool-II offers disable_load_balance_on_write to control where reads go after a write, and delay_threshold to avoid standbys that have fallen too far behind.

Failover, watchdog, and the query cache

Pgpool-II monitors PostgreSQL servers and can remove a failed one from its list. Promoting a standby is carried out by a script the administrator provides, so the setup needs testing, including the case where the old primary comes back online.

Pgpool-II can itself become a single point of failure. Its watchdog feature coordinates several Pgpool-II instances so that another takes over if one stops. The official documentation calls for at least three nodes to avoid split brain, a state in which two nodes both believe they are active.

There is also an in-memory query cache that stores SELECT results. It helps with data that rarely changes. On tables that are written often, the cache is invalidated so frequently that the overhead can outweigh the benefit.

Compared with PgBouncer

AspectPgpool-IIPgBouncer
FocusPooling, read balancing, failoverConnection pooling
Pooling methodReused between client sessionsPer session, transaction, or statement
Many idle clientsLimited by the number of childrenWell suited, especially in transaction mode
Routing SELECTs to replicasYesNo
ConfigurationMore parametersMore compact

The most important difference is PgBouncer’s transaction mode. There, a server connection is lent only for the length of a transaction, which lets hundreds of clients share a few dozen connections. Pgpool-II does not rotate connections per transaction, because a connection stays tied to the client session until that client leaves.

The limits of transaction pooling

Transaction mode saves connections but breaks the link between a client and its database session. Features that depend on the session are unavailable, including LISTEN, session-level SET, session-level advisory locks, and WITH HOLD cursors. PgBouncer’s documentation marks LISTEN as unsupported in this mode, while NOTIFY can still be sent.

Prepared statements need a closer look. The SQL commands PREPARE and EXECUTE are not supported in transaction mode. Protocol-level prepared statements, which many drivers use, are supported as long as max_prepared_statements is not zero. Current PgBouncer releases default to 200, while older ones may lack support, so check the version you have installed.

For applications that rely on LISTEN/NOTIFY, such as a kitchen display or live notifications, a safer pattern is to separate the paths. Ordinary queries go through the pooler, while a limited number of listener processes connect directly to PostgreSQL.

When you need a pooler

Start by measuring. Look at pg_stat_activity, count active and idle connections, and check whether the pool built into your framework has been configured. An application with one server and a handful of workers often does not need a separate pooler yet.

PgBouncer makes sense when the number of connections is the problem. Pgpool-II is worth considering when you already run replicas, want to spread reads without changing application code, and have someone ready to manage the configuration. A pooler will not speed up queries that are slow because of missing indexes or poor table design. Table design is covered in PostgreSQL, RDBMS, and MongoDB compared, and the operating system side in why Linux is widely used on servers.

References

← All Articles
Contact Us