DEV Community

Jason Y. (dev_in_the_fog)
Jason Y. (dev_in_the_fog)

Posted on Originally published at global-utils.com

PgBouncer Transaction Mode ERROR 42P05 & PostgreSQL FATAL 53300 Connection Exhaustion

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)
Enter fullscreen mode Exit fullscreen mode

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.

  1. Request A gets backend connection conn_17 and prepares statement s1.
  2. Request A commits; conn_17 returns to the pool, still holding s1 in backend memory.
  3. Request B gets conn_17 (on a different client session) and tries to prepare s1 again -> 42P05: prepared statement "s1" already exists.
  4. Or Request A runs EXECUTE s1 on its next query, lands on conn_04, and hits 26000: 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
Enter fullscreen mode Exit fullscreen mode

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=true to DATABASE_URL
  • postgres.js / Drizzle:
  import postgres from 'postgres';
  const sql = postgres(process.env.DATABASE_URL!, { prepare: false });
Enter fullscreen mode Exit fullscreen mode
  • Go (pgx): set DefaultQueryExecMode: pgx.QueryExecModeSimpleProtocol or pgx.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

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

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.

Collapse
 
hritik_chaudhary_5db0056f profile image
Hritik Chaudhary •

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.