Sub topic 5
Choosing a Pool Mode
Assume you know basic SQL, can use psql, and understand PostgreSQL roles, databases, and max_connections. This chapter focuses on how PgBouncer assigns PostgreSQL server connections to clients.
PgBouncer supports three pool modes:
| Pool mode | Server connection is released | Best fit |
|---|---|---|
| `session` | When the client disconnects | Applications that use session state |
| `transaction` | When a transaction commits or rolls back | Most web applications |
| `statement` | After each statement finishes | Simple, independent statements |
Session pooling
In session pooling, one client keeps the same PostgreSQL server connection for its entire session. PgBouncer assigns a server connection when the client connects and returns it only after disconnect.
[pgbouncer]
pool_mode = sessionThis mode behaves most like a direct PostgreSQL connection. These features work as expected:
- Temporary tables
- Session-level
SETvalues - Session advisory locks
- Prepared statements stored on the server
- Cursors that remain open between transactions
LISTENandNOTIFYusage tied to a session
The cost is lower sharing efficiency. An idle application client still occupies a PostgreSQL server connection. Use session pooling when the application depends on session state or when compatibility matters more than maximizing connection reuse.
Transaction pooling
In transaction pooling, PgBouncer assigns a server connection only while a client transaction is active. After COMMIT or ROLLBACK, that server connection can serve another client.
[pgbouncer]
pool_mode = transactionThis mode usually gives the best balance for request-based applications. A web server may have hundreds of client connections, while only a smaller number actively run database transactions at one time.
Keep all state needed by a request inside one transaction. For example:
BEGIN;
SET LOCAL statement_timeout = '5s';
SELECT count(*) FROM orders WHERE customer_id = 42;
COMMIT;SET LOCAL ends with the transaction. A plain session-level SET can remain on the server connection and affect a different client later. Temporary tables, session advisory locks, and session-level prepared statements can also fail or behave unexpectedly.
Statement pooling
In statement pooling, PgBouncer returns the server connection after every statement. This is the most aggressive sharing mode.
[pgbouncer]
pool_mode = statementStatement pooling requires statements to work independently. Multi-statement transactions are not suitable because the client cannot reliably keep the same server connection between statements. Features that depend on session or transaction state are generally unavailable.
Use this mode only for narrowly controlled workloads, such as independent reads. Transaction pooling is usually safer for application traffic.
A practical decision
Choose session pooling if the application requires persistent session state. Choose transaction pooling when each request can complete its database work inside a transaction. Choose statement pooling only when every statement is independent and the workload has been tested with those restrictions.
A common error is selecting transaction pooling because it provides high reuse, then discovering that a framework expects a prepared statement or temporary table to survive across transactions. Either change the framework configuration, keep the affected work in one transaction, or place that workload in a separate session-pooled database entry.
Exercise
An application sets a session variable, creates a temporary table, and reads the table in a later transaction. Which pool mode is the safest default, and why?
Show answer
Session pooling is safest because the client keeps the same server connection. Transaction pooling may assign a different server connection after the first transaction, so the session variable and temporary table may not exist.