Most teams treat transaction mode as "session mode, but faster", flip pool_mode in a Friday change, and spend the next week chasing errors like prepared statement "S_1" does not exist. The two modes are not points on a speed scale. They make different promises about which Postgres backend a client talks to, and your application either keeps those promises or it does not.
The gap matters most in containerised setups. Twenty Kubernetes pods with a client-side pool of 10 each already means 200 connections, and Postgres starts to struggle well before that if each backend holds memory for sorts and hashes. PgBouncer exists to fix that, and the mode you pick decides how much it can help.
Three modes, one question: when does the connection go back
Every PgBouncer pool answers one question: at what moment does a server connection return to the pool so another client can use it? The three modes give three answers.
Session pooling: a client gets a server connection when it connects and keeps it until it disconnects. This is PgBouncer's default. It behaves almost exactly like talking to Postgres directly, and it saves you connection setup cost, but it does not reduce the number of server connections you need at peak.
Transaction pooling: a client gets a server connection at the start of a transaction and gives it back at COMMIT or ROLLBACK. Between transactions the client holds nothing on the server side. Hundreds of mostly idle clients can share a few dozen backends.
Statement pooling: the connection goes back after every single statement. Multi-statement transactions are rejected. It suits a narrow set of autocommit workloads and is rarely the right answer for an application.
Session state: anything Postgres remembers between transactions on one backend: settings changed with SET, prepared statements created with PREPARE, temporary tables, LISTEN registrations, cursors declared WITH HOLD and session-level advisory locks.
The last definition is the whole comparison. In session mode, session state belongs to the client because the backend belongs to the client. In transaction mode, state created in one transaction lands on a backend that the next transaction may never see again, and may leak into some other client's transaction instead.
PgBouncer can clean state between clients in session mode with server_reset_query, which defaults to DISCARD ALL. In transaction mode that reset query is not run between transactions unless you set server_reset_query_always, because running it after every transaction would cost a round trip each time.
What actually breaks in transaction mode
The failures are rarely obvious crashes. More often they are intermittent errors or, worse, wrong behaviour that looks like a bug in your own code.
Prepared statements are the classic case. Drivers such as the PostgreSQL JDBC driver, pgx in Go and psycopg 3 prepare frequently used queries on the server for speed. In transaction mode, the statement is prepared on backend A, and the next execution lands on backend B, where it does not exist.
PgBouncer 1.21, released in 2023, added support for protocol-level prepared statements in transaction mode when max_prepared_statements is set above zero, which removes most of this pain. SQL-level PREPARE and DEALLOCATE are still not supported. On older versions, or with drivers that bypass the protocol mechanism, disable server-side preparation: prepareThreshold=0 in JDBC, prepare_threshold=None in psycopg 3, or a simple protocol execution mode in pgx.
SET is the quiet one. SET search_path or SET statement_timeout outside a transaction changes one backend, and your next query may run elsewhere with defaults. Use SET LOCAL inside a transaction, or move settings to the role with ALTER ROLE ... SET, which applies on every connection.
Advisory locks taken with pg_advisory_lock belong to a session. Migration tools that use them to prevent concurrent runs, including Rails migrations by default, can fail to release the lock, or release it from a session that never took it. Run migrations directly against Postgres or through a session-mode pool.
LISTEN and NOTIFY need a persistent session; listeners in transaction mode simply stop receiving events. Django's documentation notes that server-side cursors need DISABLE_SERVER_SIDE_CURSORS = True behind transaction pooling, for the same underlying reason.
Sizing the pool: the arithmetic for each mode
Session mode sizing is simple and unforgiving. You need one server connection per concurrently connected client. Transaction mode sizing depends on how long transactions last, which is a Little's law calculation.
Session mode:
server connections needed = peak concurrent client connections
Transaction mode:
busy server connections = transactions per second x average transaction duration (s)
default_pool_size = busy server connections x headroom (1.5 to 2)
Worked example. A team in Lisbon runs 12 API pods, each with a client pool of 10 connections, plus 4 background workers with 10 connections each. That is 160 client connections at peak. Postgres runs on an instance where max_connections is 100, and the team wants to keep 15 of those for admin access, replicas and monitoring.
Session mode would need 160 server connections. It cannot fit, and raising max_connections to 200 would cost memory the instance does not have.
In transaction mode, monitoring shows a peak of 1,500 transactions per second with an average duration of 8 milliseconds from BEGIN to COMMIT. Busy connections at peak are 1,500 times 0.008, which is 12. With a headroom factor of 2, a default_pool_size of 25 covers the load, and max_client_conn can be set to 300 to absorb deploys, when old and new pods overlap.
Now the catch. That 8 milliseconds must be measured from the database's point of view, including time the application holds a transaction open while doing something else. If one endpoint opens a transaction, calls a payment API for 400 milliseconds and then commits, every call holds a server connection for 400 milliseconds.
At 50 such calls a second, that endpoint alone needs 20 busy connections. Find those endpoints in pg_stat_activity by looking for sessions in the state idle in transaction. Moving the API call outside the transaction usually cuts the pool requirement more than any PgBouncer setting.
Side by side for common workloads
The right mode depends on what the client does. A single PgBouncer can run different modes for different databases or users, so compare workload by workload. Read each row as the default to test first.
| Workload | Session mode | Transaction mode | Usual choice |
|---|---|---|---|
| Stateless web API with short transactions | Works, needs one backend per client connection | Works, multiplexes heavily | Transaction |
| Serverless functions opening many short connections | Exhausts backends quickly | Designed for this | Transaction |
| Schema migrations with advisory locks | Works | Locks misbehave | Session or direct |
| Listeners using LISTEN and NOTIFY | Works | Events stop arriving | Session or direct |
| Reporting jobs with long transactions | Works | Holds a backend anyway, little gain | Session |
| Apps relying on SET search_path per tenant | Works | Wrong schema possible | Session, or SET LOCAL |
| Analytics tools using server-side cursors | Works | Cursors break across transactions | Session |
Managed alternatives follow the same logic with different trade-offs. Amazon RDS Proxy multiplexes like transaction mode but "pins" a client to a backend when it detects session state, which keeps applications correct and quietly reduces the pooling benefit. Knowing which statements trigger pinning is the RDS Proxy version of the audit below.
Poolers such as Supavisor and PgCat offer transaction pooling too, each with its own list of unsupported features, so the questions stay the same even when the tool changes. Before trusting any of them, run the same test: prepare a statement, set a parameter, take an advisory lock, and see which of the three survives the next transaction.
Audit before switching to transaction mode
Run this list against each application before changing its pool. Most items take minutes with grep and a look at the driver configuration.
- Driver and version known, and its prepared statement behaviour checked against your PgBouncer version.
max_prepared_statementsset above zero on PgBouncer 1.21 or later, or server-side preparation disabled in the driver.- No session-level
SEToutside transactions; settings moved toSET LOCALorALTER ROLE. - No
LISTEN, noWITH HOLDcursors, no temporary tables that outlive a transaction. - Migrations and any job using
pg_advisory_lockrouted to Postgres directly or to a session-mode pool. - Endpoints with
idle in transactiontime above about 100 milliseconds identified and either fixed or given their own pool. SHOW POOLSchecked during a load test:cl_waitingshould stay near zero andmaxwaitunder a second.- Rollback plan written: switching
pool_modeback and reloading PgBouncer takes seconds, provided clients reconnect cleanly. - A staging run of at least a day with production-like traffic, because intermittent errors from session state often appear only under concurrency, when two clients actually share one backend.
Teams sometimes object that this audit is overkill for a small service. For a service with 3 pods and 20 connections, it probably is: session mode costs nothing there. The audit pays off once client connections exceed what Postgres can hold, which is exactly when a switch is tempting under pressure. Doing the audit calmly, before the incident that forces the change, is the difference between a ten minute config edit and a week of intermittent errors that nobody can reproduce on a laptop.
Running both modes side by side
The cleanest production setup is often two entries in pgbouncer.ini pointing at the same database. One, for example app, runs transaction mode for the API. Another, app_session, runs session mode with a small pool for migrations, listeners and the occasional admin tool. Applications pick the pool by connection string, and nobody has to choose a single mode for every workload.
Pool size is set per database and user pair, so keep the session pool small, perhaps 5 connections, so it cannot starve the transaction pool. Watch both with SHOW POOLS and alert on cl_waiting for each.
This split also makes troubleshooting cheaper. When a strange error appears, you can point one instance of the service at the session pool for an hour. If the error disappears, session state is the cause, and the audit list tells you where to look. If it stays, the problem lies elsewhere, often in pool exhaustion during deploys.
The recommendation: use transaction mode for stateless application traffic once the audit passes and you run PgBouncer 1.21 or later with prepared statement support enabled. Keep session mode for migrations, listeners and anything with long-lived state. If your client connections already fit comfortably under max_connections, stay in session mode and spend the effort elsewhere.