Sub topic 3
How PgBouncer Works: Clients, Servers, and Pools
Assume you know basic SQL, PostgreSQL roles and databases, the psql client, and the purpose of PostgreSQL's max_connections setting. This chapter builds a working model of what PgBouncer does between an application and PostgreSQL.
The two sides of a connection
An application opens a client connection to PgBouncer. PgBouncer accepts that connection on its listening address and port, commonly 6432.
PgBouncer separately opens server connections to PostgreSQL, commonly on port 5432. The application does not communicate directly with those server connections. PgBouncer sends client requests through an available server connection and returns the database response to the client.
This separation lets many client connections share fewer PostgreSQL server connections.
A client connection can remain open while its assigned server connection changes. The exact timing depends on the pool mode.
What a pool contains
A pool is a group of reusable server connections for a particular routing key. In the common case, PgBouncer keeps separate pools for each database and user pair.
For example, these connections can use different pools:
| Client request | Pool key |
|---|---|
| `app_user` connecting to `appdb` | `app_user` + `appdb` |
| `report_user` connecting to `appdb` | `report_user` + `appdb` |
| `app_user` connecting to `testdb` | `app_user` + `testdb` |
The pool has a limited number of server connections. If all of them are busy, a new client request waits in a queue until a server connection becomes available. PgBouncer does not create an unlimited number of PostgreSQL connections just because many clients are waiting.
The database name in the client connection string determines the destination database. A PgBouncer database entry can also define the PostgreSQL host, port, and database name used for that destination.
When connections are assigned
PgBouncer supports three assignment styles:
- In session pooling, one server connection stays with a client until the client disconnects.
- In transaction pooling, a server connection returns to the pool when the current transaction ends. Another client can then use it.
- In statement pooling, a server connection returns after each statement. This mode has the strictest limitations.
Transaction pooling is often the useful compromise for web applications. Applications can keep many client connections open, while PostgreSQL handles only the smaller number of active server connections.
Following one request
Suppose an application starts a transaction and runs a query. PgBouncer finds the pool for that user's database, selects an idle server connection, and forwards the transaction. When the application commits or rolls back, the server connection becomes available for another transaction in transaction pooling mode.
Because another client may receive that connection next, session-specific state must be handled carefully. Settings that are intended to last only for one transaction should be applied with SET LOCAL, rather than relying on a persistent session setting.
Exercise
An application has 500 open client connections, but its PgBouncer pool contains 20 server connections. Does PostgreSQL necessarily have 500 connections?
Show answer
No. PgBouncer can multiplex the 500 client connections onto the 20 server connections, although clients needing busy server connections may wait.