When rolling out PgBouncer transaction mode, test >10 repeated queries per endpoint and enable max_prepared_statements.
TL;DR30-second simplified summary▾
Protocol-level statement tracking fixes the 40% error spike without incurring the 35%-50% CPU tax of prepareThreshold=0.
Triaging the 'prepared statement S_1 does not exist' Storm.
Errors spike only after the 5th execution of a query (PgJDBC/psycopg prepareThreshold=5), making staging tests with low iteration counts pass.
Named Prepared Statement Collision Across Pooled Backends
- Executions 1-4: unnamed Parse/Execute (0 errors)
- Execution 5+: driver prepares S_1 on Backend A
- 98.75% of follow-up Execute calls hit other backends -> 26000 error
- PgBouncer maps client S_1 to canonical hash PGB_42
- Lazy Parse re-prepares PGB_42 on Backend B if missing
- 0% 26000 errors while preserving Postgres plan-cache CPU savings
Configuring Bounded Pools & Zero Session State.
Combine HikariCP/psycopg_pool with PgBouncer max_prepared_statements and replace SET with SET LOCAL.
Protocol-Level Prepared Statement Tracking
HikariConfig cfg = new HikariConfig();
cfg.setJdbcUrl("jdbc:postgresql://pgbouncer:6432/ledger");
cfg.setMaximumPoolSize(10);
cfg.setConnectionTimeout(250);
// Emergency fallback for PgBouncer < 1.21: cfg.addDataSourceProperty("prepareThreshold", "0");
Avoiding Session Pollution & Statement Cache Eviction.
Ensure no middleware issues session-level SET TIME ZONE or temp tables without DISCARD ALL. [INSERT YOUR BENCHMARK / PROD NOTE] for p99 latency.
400 Pods Multiplexed Across 80 PostgreSQL Backends
- Problem
- How to eliminate 26000 prepared-statement errors across 400 pods at 20,000 QPS behind PgBouncer transaction pooling without spiking PostgreSQL CPU or risking session-state leaks.
- Naive Solution
- Set prepareThreshold=0 in every application's JDBC/psycopg driver or keep PgBouncer in session-pooling mode.
- Why It Fails
- Setting prepareThreshold=0 forces PostgreSQL to re-parse and re-plan every query, spiking CPU by 35%-50%, while session mode defeats 50x connection multiplexing and exhausts backend process memory.
- Alternatives
- Compare client-side prepareThreshold=0 emergency mitigation against PgBouncer 1.21+ protocol-level statement tracking (max_prepared_statements) paired with SET LOCAL.
- Engineering Decision
- Enable max_prepared_statements in PgBouncer 1.21+ sized above active query templates, enforce HikariCP/psycopg_pool fail-fast timeouts, and ban session-level SET or temp tables in favor of SET LOCAL.
- Trade-Offs
- Preserves <4ms p99 latency and server-side plan caching in exchange for upgrading to PgBouncer 1.21+ and enforcing strict zero-session-state discipline in application queries.
Why did unit and smoke tests (running 2-3 queries per test) pass before the production outage?
| Choice | What it buys | What it costs |
|---|---|---|
| prepareThreshold=0 in app | Works on PgBouncer versions older than 1.21 as an emergency fallback | Requires app redeploy across 400 microservice pods +35-50% PostgreSQL parser/planner CPU |
| PgBouncer 1.21+ max_prepared_statements | Proxy config reload without redeploying application pods 0% extra PostgreSQL CPU; preserves server-side plan caching | Requires PgBouncer 1.21+ and sizing max_prepared_statements above active query templates |
Practice: Make the trade-off yourself.
Why do smoke tests running 3 iterations of a query pass even when PgBouncer transaction pooling is misconfigured?