When scaling PostgreSQL behind PgBouncer, pair max_prepared_statements with SET LOCAL so transactions stay stateless and fast.
TL;DR30-second simplified summary▾
Protocol-level statement tracking avoids both the 98.75% backend miss rate and the 35%-50% CPU penalty of prepareThreshold=0.
Why Session-Scoped Statements Break Under Multiplexing.
With 80 backend connections, consecutive transactions from one pod have a 79/80 (98.75%) chance of hitting a different PostgreSQL process that lacks S_1.
Wire-Protocol Statement Hash Mapping.
Configure max_prepared_statements > 0 in pgbouncer.ini and enforce zero session-level state (SET LOCAL only).
Wire-Protocol Statement Hash Mapping
from psycopg_pool import ConnectionPool
pool = ConnectionPool('postgresql://pgbouncer:6432/ledger', min_size=2, max_size=10)
def query_ledger(acct_id: int):
with pool.connection() as conn, conn.transaction():
return conn.execute('SELECT balance FROM accounts WHERE id = %s', (acct_id,)).fetchone()
CPU Savings vs. Memory Bounds.
Protocol-level tracking avoids the 35%-50% CPU penalty of disabling prepared statements (prepareThreshold=0) while bounding per-backend statement cache memory. [INSERT YOUR BENCHMARK / PROD NOTE] for statement cache eviction rates.
Disabling Prepared Statements vs. Protocol-Level Tracking
- 80 backend connections
- +35-50% Parse CPU overhead
- 80 backend connections
- <4ms p99 with cached Parse
- Problem
- How to run high-concurrency PostgreSQL clusters (>500 app connections) behind PgBouncer transaction pooling without 'prepared statement S_1 does not exist' errors or a 35%-50% CPU regression.
- Naive Solution
- Set prepareThreshold=0 in the client driver so every query uses the simple query protocol or unnamed statements.
- Why It Fails
- Disabling prepared statements forces PostgreSQL to lex, parse, and plan every execution on a 20,000 QPS primary, burning 35%-50% extra CPU.
- Alternatives
- Connect directly to PostgreSQL (4,000+ processes with OOM risk), use session pooling (loses multiplexing), or enable PgBouncer 1.21+ max_prepared_statements.
- Engineering Decision
- Enable max_prepared_statements > 0 in pgbouncer.ini for transaction pooling workloads and enforce SET LOCAL; keep LISTEN/NOTIFY, advisory session locks, or session-scoped temp tables on a dedicated session pool.
- Trade-Offs
- Preserves 80-backend multiplexing and low parse CPU (<4ms p99) in exchange for bounded per-backend statement cache memory and sizing max_prepared_statements above distinct hot query templates.
Why does setting prepareThreshold=0 fix the prepared statement crash under PgBouncer transaction pooling but still degrade a 20,000 QPS PostgreSQL primary?
| Choice | What it buys | What it costs |
|---|---|---|
| Direct Postgres | Native session-scoped prepared statements Low parse CPU overhead | 4,000 backend connections High memory footprint and OOM risk |
| PgBouncer Tx + prepareThreshold=0 | Multiplexes down to 80 backend connections Avoids ERROR 26000 across backend switches | High parse CPU overhead (+35-50% CPU) Re-parses and re-plans every query execution |
| PgBouncer 1.21+ max_prepared_statements | Multiplexes down to 80 backend connections Prepared statements work safely with low parse CPU (<4ms p99) | Requires sizing max_prepared_statements above hot query templates Uses bounded per-backend statement cache memory |
Practice: Make the trade-off yourself.
Why does 'ERROR: prepared statement S_1 does not exist' occur under PgBouncer transaction pooling?