Putting PgBouncer (pool_mode = transaction) in front of PostgreSQL solves connection storms (FATAL: sorry, too many clients already / SQLSTATE 53300), and then immediately triggers a new production outage:
ERROR: prepared statement "lrupsc_1_0" already exists (SQLSTATE 42P05)
ERROR: prepared statement "s1" does not exist (SQLSTATE 26000)
Here is why PgBouncer transaction pooling breaks drivers/ORMs (Prisma, Drizzle, postgres.js, pgx, psycopg) and how to configure both sides cleanly.
1. Why ERROR 42P05 Happens in Transaction Mode
In transaction pooling mode, PgBouncer returns the underlying PostgreSQL server connection to the pool as soon as a COMMIT or ROLLBACK finishes.
- Request A gets backend connection
conn_17and prepares statements1. - Request A commits;
conn_17returns to the pool, still holdings1in backend memory. - Request B gets
conn_17(on a different client session) and tries to prepares1again ->42P05: prepared statement "s1" already exists. - Or Request A runs
EXECUTE s1on its next query, lands onconn_04, and hits26000: prepared statement "s1" does not exist.
2. Clean Fixes by Stack
Option A: PgBouncer >= 1.21 Protocol-Level Prepared Statements
Upgrade PgBouncer to 1.21+ and enable prepared statement tracking across client-server pairs:
[pgbouncer]
pool_mode = transaction
max_prepared_statements = 200
Option B: Disable Named Prepared Statements in Your Driver / ORM
If you run an older PgBouncer or managed pooler without protocol statement support:
-
Prisma: add
?pgbouncer=truetoDATABASE_URL - postgres.js / Drizzle:
import postgres from 'postgres';
const sql = postgres(process.env.DATABASE_URL!, { prepare: false });
-
Go (
pgx): setDefaultQueryExecMode: pgx.QueryExecModeSimpleProtocolorpgx.QueryExecModeExec
3. Verifying Real Query Bottlenecks Before Tuning pool_size
Increasing max_connections on PostgreSQL without checking query plans makes lock contention and context-switching worse (FATAL 53300). Always inspect slow queries with EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) first — and never paste proprietary SQL schemas into third-party websites that log server payloads.
Full Guides & Offline PostgreSQL Tools
-
PgBouncer
42P05Guide: PgBouncer Transaction Pooling Mode Prepared Statement Collision Resolution -
PostgreSQL
FATAL 53300Guide: PostgreSQL Connection Exhaustion and PgBouncer Pooling Architecture - Client-Side Query Plan Flamegraph (0 bytes uploaded): NerdKit SQL EXPLAIN Visualizer
- SQL DDL to Drizzle ORM Converter: NerdKit SQL to Drizzle
- Saved Gist Checklist: pgbouncer-transaction-mode-42p05-checklist.md
Top comments (2)
The conn_17/conn_04 example is the clearest part of this — the failure isn't the pool, it's that a prepared statement is per-backend state while the pool treats connections as interchangeable. That mismatch is the whole bug.
The 26000 side is sneakier than the 42P05 side, because it only shows up when a request happens to land on a different backend than the one that prepared the statement — intermittent, load-dependent, and it looks like a random error until you see the pattern.
One question on the fix: with max_prepared_statements=200, what happens when the 201st statement arrives — does PgBouncer evict the oldest one, or does the request fail? If it evicts silently, the same class of bug could come back as a performance surprise instead of an error.
One caveat on Option A: the tracking added in 1.21 covers protocol-level Parse/Bind/Describe only. A SQL-level PREPARE or EXECUTE sent as plain text is not tracked, and the PgBouncer feature map says to avoid those in transaction mode, so a script or ORM that issues them still gets 26000 even with max_prepared_statements set. Check what your driver actually sends before assuming the setting covers it. Also check the PgBouncer version behind a managed pooler, since some lag behind 1.21 and Option B is then the only choice. A quick way to reproduce the 42P05/26000 pair on purpose: set default_pool_size = 1 and run two client sessions that prepare the same name. The second session lands on the first one's backend every time, so the error stops being intermittent.