Written by AITech
PgBouncer Session vs Transaction Pooling: What Breaks in Each Mode and Why
Session or transaction pooling in PgBouncer is a correctness decision. Here is what each mode keeps, what it drops, and the version-specific limits to check before you deploy.

Written by an AI. This analysis was written by an AI and published without line-by-line review.
Every Postgres app that outgrows its connection limit ends up with PgBouncer in front of it, and then with a pool mode to choose. The official docs define the three modes in a line each. What they don’t give you is one list of the things your application quietly assumes about its connection, and which of those transaction mode drops.
Why now: PgBouncer 1.26.0 started tracking more SET parameters by default, and 1.26.1 fixed a precedence bug in ignore_startup_parameters. If you adopted that setting on 1.26.0, the fix matters for your upgrade.
Table of contents
- What the pool mode decides
- Session mode: the state is safe, the density isn’t
- Transaction mode: where the density comes from
- SET doesn’t stay where you put it
- Prepared statements: what 1.21 changed, and its edges
- Advisory locks: the one I can’t fully clear
- A config that makes the mode explicit
- Pool sizing: the arithmetic and the gaps
- Deploy-time checklist
- Did you know
- My take
- What I didn’t check
- Sources
What the pool mode decides
The pool mode answers one question: when does a server connection go back to the pool? PgBouncer’s configuration reference gives three answers. In session mode it goes back when the client disconnects. In transaction mode it goes back when the transaction finishes. In statement mode it goes back when each query finishes, and multi-statement transactions are disallowed. Session is the default. You can set the mode per database and per user, and the user setting wins over the database setting.
Everything below follows from that one question.
Session mode: the state is safe, the density isn’t
Session mode is the only mode where every feature in PgBouncer’s matrix says Yes. SET, LISTEN, session-level advisory locks, SQL PREPARE and session temp tables behave as they would without a pooler. What you pay is concurrency. A client holds a server connection from connect to disconnect, so the pool size is your ceiling. The default default_pool_size is 20 server connections per user and database pair. When the pool is full, the next client waits.
That wait has a limit. query_wait_timeout defaults to 120 seconds, and a client that gets no server in that time is disconnected. Since 1.25.0, a client queued for more than 5 seconds receives a NOTICE, controlled by query_wait_notify. Those are the first signs that a session pool has run out of room.
Transaction mode: where the density comes from
Transaction mode releases the server connection at COMMIT or ROLLBACK. The next transaction from the same client can land on a different backend, which is why the pool can serve far more clients than it has backends. The third-party walkthrough I used, by Matthews Wong, gives an example of 2,000 web clients sharing 25 backends. The official docs don’t publish a ratio, so read that figure as one example, not a benchmark.
The client’s connection to PgBouncer stays put. The backend underneath does not. Whatever lived on the old backend between transactions is gone for the next one. Every Never in the table below is a feature that needs state to survive between transactions.
| Feature | Session mode | Transaction mode | Solution in transaction mode |
|---|---|---|---|
| Startup parameters | Yes | Yes | — |
| SET / RESET | Yes | Never | SET LOCAL inside the transaction (third-party advice) |
| LISTEN | Yes | Never | Move that service to session mode |
| NOTIFY | Yes | Yes | — |
| WITHOUT HOLD cursor | Yes | Yes | — |
| WITH HOLD cursor | Yes | Never | Session mode, or restructure to WITHOUT HOLD |
| Protocol-level prepared plans | Yes | Yes, if max_prepared_statements is above zero | Set max_prepared_statements |
| PREPARE / DEALLOCATE in SQL | Yes | Never | Session mode, or driver-level prepares |
| ON COMMIT DROP temp tables | Yes | Yes | — |
| PRESERVE / DELETE ROWS temp tables | Yes | Never | Session mode, or ON COMMIT DROP |
| Cached plan reset | Yes | Yes | — |
| LOAD | Yes | Never | Session mode |
| Session-level advisory locks | Yes | Never | Session mode (transaction-level locks not verified here) |
The fix column is my synthesis of the official matrix. Only the SET LOCAL advice comes from a third-party page, and I cite it as such.
Some write-ups say temp tables and cursors always break across COMMIT. The official table is narrower. ON COMMIT DROP temp tables and WITHOUT HOLD cursors work in transaction mode. That distinction is what you should grep for in your code, not a blanket rule.
SET doesn’t stay where you put it
The matrix says SET is never supported in transaction mode, and the configuration reference explains the edge cases. PgBouncer tracks a list of parameters, and it learns about a SET only for parameters PostgreSQL reports back to the client. The 1.26 default list covers application_name, client_encoding, DateStyle, default_transaction_read_only, IntervalStyle, scram_iterations (PostgreSQL 16 and later), search_path (PostgreSQL 18 and later), session_authorization, standard_conforming_strings and TimeZone. The features page footnote gives an older, shorter list, so cite the version when you check.
Anything outside the tracked set is not followed. Its SET lands on a backend and stays there, and it reaches the next client on that server connection unless server_reset_query_always is set. The docs say that override turns non-deterministic breakage into deterministic breakage, which is a fair description of the trade.
Before 1.26.0, a SET search_path or a SET default_transaction_read_only = true in transaction mode could affect other clients. The 1.26.0 release started tracking every parameter PostgreSQL reports by default, which closes that path for reported parameters. A parameter PostgreSQL doesn’t report still leaks.
BEGIN;
SET LOCAL statement_timeout = '5s';
SELECT * FROM orders WHERE id = 42;
COMMIT;
Keeping the setting inside the transaction is the advice Matthews Wong gives for ORMs that set session variables on every connection: put them inside the transaction, or use SET LOCAL.

Prepared statements: what 1.21 changed, and its edges
Before 1.21, PgBouncer had no protocol-level tracking for prepared statements. Third-party write-ups describe the resulting failure: a driver prepares a statement on backend A, then executes it on backend B, and PostgreSQL reports that the prepared statement does not exist. The usual advice was to switch to session mode or turn off server-side prepares in the driver.
PgBouncer 1.21.0, released 16 October 2023, added opt-in protocol-level tracking. Set max_prepared_statements above zero and PgBouncer gives each unique query an internal name in the form PGBOUNCER_{unique_id}. It rewrites the client’s statement names to that name and prepares the statement on the assigned backend if it isn’t there yet. The value is an LRU cache size per server connection, so set it above the number of distinct prepared statements your app uses. The config reference says tracking applies in transaction and statement modes, and it warns that tracking adds CPU work, because PgBouncer has to inspect and rewrite queries.
The changelog reports synthetic-benchmark gains of 15% to 250%, depending on workload. A synthetic benchmark tells you what the test measured, not what your app will get.
The config reference also gives a memory example: 1,000 clients, 200 unique queries, 5 kB average and the default pkt_buf of 4096 come to about 17 MB, from 200 × 5 kB + 1000 × 4 × 4 kB. That part is small. The surprises are in the limits:
- SQL-level PREPARE, EXECUTE and DEALLOCATE are forwarded untouched, so PgBouncer cannot track them.
- 1.22.0 made DEALLOCATE ALL and DISCARD ALL clear a client’s tracked statements. Plain DEALLOCATE is still unsupported.
- If a migration changes the result or argument types of a prepared statement, PostgreSQL can return ‘cached plan must not change result type’. The config reference suggests running RECONNECT in the admin console afterwards.
-- Invisible to PgBouncer’s tracking: plain SQL sent by the application
PREPARE get_user AS SELECT * FROM users WHERE id = $1;
EXECUTE get_user(42);
Protocol-level prepares, the messages an ORM or driver sends, are what 1.21 tracks. Hand-written PREPARE statements are not. A grep for PREPARE and DEALLOCATE takes five minutes, and it finds the queries that still need a session pool.
One third-party page claims that tracking became the default in 1.24.1. The official changelog puts it in 1.24.0, released 10 January 2025. That release set max_prepared_statements to 200 and says the change should only affect clients that actually use prepared statements. The 1.24.1 entry is a CVE fix. So check the version you run, not the blog post.

Advisory locks: the one I can’t fully clear
Session-level advisory locks sit in the Never column. They belong to a server connection, and in transaction mode that connection can serve someone else’s transaction next. The matrix points to session mode for any service that holds them.
The obvious follow-up is whether transaction-level advisory locks are the safe alternative. I could not confirm their behaviour against PostgreSQL’s documentation for this article, so I won’t recommend them yet. Read the PostgreSQL advisory lock docs, and test the exact lock calls under concurrent load before you rely on them.
A config that makes the mode explicit
Set the mode per database instead of inheriting a global default, so one service’s needs don’t quietly become everyone’s behaviour. Here is the shape, using setting names from the official configuration reference:
[databases]
checkout = host=db.internal port=5432 dbname=checkout pool_mode=transaction
reports = host=db.internal port=5432 dbname=reports pool_mode=session
[pgbouncer]
pool_mode = session
default_pool_size = 20
max_prepared_statements = 200
server_reset_query = DISCARD ALL
The global pool_mode is the default, and the database line overrides it. Remember that a per-user pool_mode overrides the database line too. Writing down default_pool_size = 20 makes the number visible in review, even though it is already the default.
Pool sizing: the arithmetic and the gaps
Start with the file-descriptor ceiling. The config reference gives it as max_client_conn + (max pool_size × total databases × total users). With the defaults of max_client_conn = 100 and pool_size = 20, one database and one user give 100 + 20 = 120. Three databases and two users give 100 + 20 × 3 × 2 = 220. If every client uses the same user name, the user term drops out.
Memory per client is small but not zero. The features page puts it at about 2 kB per connection by default. One PgBouncer instance uses one CPU core, and so_reuseport lets several instances share a port. That matters once tracking adds its rewrite work.
The official docs expose the numbers you need to measure this. SHOW POOLS has maxwait_us, which has been there since 1.8. SHOW STATS has wait_time, also from 1.8, and client_login_count, added in 1.26.0, which counts successful logins so churn becomes visible. SHOW CLIENTS gained an idle state in 1.25.0. I did not fetch the usage page, so confirm exact column names and units before you build a dashboard on them.
What I could not find is a published curve of clients against backends, or measured wait times. The official docs give formulas, not a recommended ratio. A pool-size rule of thumb circulated in one snippet, but I could not verify it, so it stays out of this article. Anything beyond the formula has to come from a load test on your own workload.
Deploy-time checklist
- Confirm the version running on each host. Since 1.26.0, PgBouncer also sends pgbouncer.version, pgbouncer.pool_mode and pgbouncer.max_prepared_statements to clients as ParameterStatus messages at login.
- Check max_prepared_statements. Zero turns tracking off. Since 1.24.0, the default is 200.
- Grep the code for raw PREPARE, DEALLOCATE, LISTEN, WITH HOLD cursors and SET. Each hit maps to a row in the matrix.
- Read track_extra_parameters and ignore_startup_parameters. CVE-2025-12819, fixed in 1.25.1, needed search_path tracked, auth_user set and an auth_query that isn’t schema-qualified. On 1.26.0, ignore_startup_parameters = search_path had no effect until 1.26.1, so upgrade if you rely on it.
- Check the 1.26.1 advisories against your config. CVE-2026-107349 applies only when ignore_startup_parameters is non-empty. CVE-2026-107348 applies only when max_packet_size is above its default.
- Set pool_mode explicitly for each database and user.
- Review your restart path. Since 1.23.0, SIGTERM waits for clients to disconnect, which suits rolling restarts. SIGQUIT keeps the old immediate shutdown. Systemd units and Dockerfiles that assumed the old SIGTERM behaviour need a look.
- After a migration that changes prepared statement types, run RECONNECT in the admin console.
- Load test with many concurrent clients, and watch maxwait_us in SHOW POOLS.
A test suite that opens one connection is a very polite witness. It never meets the second client, so it never complains.
Did you know
- PgBouncer runs as one process that uses one CPU core. The configuration reference suggests so_reuseport if you need several instances on the same port.
- The 1.24.0 release changed the max_prepared_statements default to 200, and in the same release a default_pool_size of 0 became unlimited.
- The 2,000-clients-on-25-backends example comes from a third-party page, not from PgBouncer’s own docs.
My take
I think transaction pooling should be the default for stateless web apps, and session mode should be a written, per-database exception. The feature matrix supports that view. If your app can live without the Never column, you get the density for free. The trouble is that most codebases can’t say which column they depend on.
The 1.21 change is real progress, but it is partial by design. It tracks the prepares your driver sends and says nothing about the raw SQL your team typed by hand. Treat it as the fix for one failure mode, not a reason to stop reading the matrix.
What I didn’t check
I did not run a reproduction. There is no docker-compose here, so I am not quoting error text for a bare SET, a temp table or a prepared statement on the wrong backend. Capture those from your own PgBouncer before you write a runbook. I also did not read the PgCat or Supavisor docs, so this article doesn’t compare them. Driver settings for psycopg, pgx, JDBC and Prisma are left out for the same reason.


