Sub topic 7
What breaks under transaction pooling
This chapter assumes basic SQL and psql, familiarity with PostgreSQL roles and max_connections, and a general understanding of client/server applications.
Transaction pooling gives a client a PostgreSQL server connection only for the duration of one transaction. When the transaction ends, PgBouncer can give that server connection to another client. This saves connections, but it means session state cannot be trusted to persist between transactions.
The transaction boundary
Consider this flow:
A setting created by Client A can remain on the server connection after Client A disconnects from it. Client B may then inherit that setting. PgBouncer resets some session state when it releases a server connection, but applications should not depend on every form of state being reset correctly.
The main rule is simple: put state that must apply to a transaction inside that transaction.
-- Unsafe for transaction pooling: this is session-level state.
SET statement_timeout = '5s';
-- Safe: the setting lasts only for this transaction.
BEGIN;
SET LOCAL statement_timeout = '5s';
SELECT count(*) FROM orders;
COMMIT;SET LOCAL is a PostgreSQL command that limits a setting to the current transaction. After COMMIT or ROLLBACK, the setting disappears.
Features that need a session
Some PostgreSQL features attach state to a particular server connection. They can fail when PgBouncer assigns a different connection later.
| Feature | Why it breaks | Safer approach |
|---|---|---|
| Temporary tables | The table exists on one server connection | Use permanent staging tables with unique request IDs |
| Session `SET` values | A later transaction may use another connection | Use `SET LOCAL` inside each transaction |
| Session advisory locks | The lock belongs to one server connection | Use transaction-level advisory locks |
| `LISTEN` and `NOTIFY` listeners | The listener must stay on one session | Use session pooling or a dedicated listener connection |
| SQL `PREPARE` statements | The prepared statement belongs to one session | Use unnamed statements or supported protocol preparation |
Temporary tables are especially common in batch jobs. A transaction can create and use one successfully, but a later transaction may receive a different server connection and report that the table does not exist.
Prepared statements
Prepared statements store a parsed and planned SQL statement on the server. In transaction mode, a prepared statement created on one server connection is unavailable on another.
Many drivers use protocol-level prepared statements automatically. Recent PgBouncer versions can support these in transaction mode when max_prepared_statements is configured, but this adds memory and processing overhead. Support also depends on the driver and PgBouncer version.
For a portable setup, disable automatic server-side preparation or use unnamed statements unless you have tested the configuration.
A practical decision
Choose transaction pooling when requests are independent, transactions are short, and the application can recreate all required state at the start of each transaction. Choose session pooling when the application uses temporary tables, listeners, session variables, or other connection-bound features.
A useful migration test is to run the application through PgBouncer in transaction mode and deliberately use several concurrent clients. Errors that appear only with concurrency often reveal hidden session state.
Exercise
An application runs SET searchpath = tenanta, creates a temporary table, and then commits. Why can the next transaction fail or use the wrong state?
Show answer
Transaction pooling can assign the next transaction a different server connection. The session setting and temporary table belong to the previous connection, so the application must use transaction-scoped commands, avoid the temporary table, or use session pooling.