PgBouncer transaction pooling causes partitioned SELECTs to use generic plans and run 8–12× slower from Go service
Environment
PostgreSQL 14 primary on dedicated VM, PgBouncer 1.17.1 in transaction pooling mode, Go service (database/sql + pgx v4) issuing prepared statements and parameterized queries; partitioned tables with per-partition statistics and constraint exclusion.
Symptoms
Select queries against partitioned tables are fast when run in psql (warm), but the same queries from the Go app via PgBouncer take 8–12× longer and show generic sequential scans. Switching PgBouncer to session pooling restores original performance.
Observations
EXPLAIN (ANALYZE, BUFFERS) run in psql produces partition-pruned plans and index scans. Tracing queries from the app via PgBouncer shows simple-plans with nested loops and full partition scans. App uses server-side prepared statements by default. PgBouncer transaction pooling reuses server connections for different clients.
Already attempted
Temporarily changed PgBouncer to session pooling (performance recovered), increased statement_timeout, upgraded pgx and PgBouncer, tested client-side query parameterization (no server prepare) on a single endpoint but not app-wide.
Question
How can I keep PgBouncer in transaction pooling mode for high connection scalability without regressing partitioned query performance? What config or code changes are safest?
Verified community troubleshooting
Don't let server-side prepared statements clash with PgBouncer's transaction pooling. Either run PgBouncer in session pooling for that app, or configure the Go driver to use simple protocol (pgx: PreferSimpleProtocol=true or disable server prepares) so queries are parsed on each backend connection and the planner can pick partition-aware plans. Verify by comparing EXPLAIN (ANALYZE) from the app before/after: you should see partition-pruning/index scans restored. Rollback by reverting driver flag or switching the app back to a session-pooling PgBouncer instance if you hit regressions.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases37% match
JDBC prepared statements on application server suddenly select a generic plan causing a 15× slowdown for partitioned table queries
Open0 answers - Databases35% match
PostgreSQL prepared statements pick a generic plan after ANALYZE on a partitioned table, causing 10× slower execution
Open0 answers - Databases33% match
Partitioned-table queries run fast in psql but 8–12× slower when routed through PgBouncer transaction pooling
Open0 answers - Databases25% match
PostgreSQL transactions block unexpectedly when mixing SERIALIZABLE snapshot reads and FOR UPDATE in prepared statements
Open0 answers - Databases24% match
Autovacuum stalls and partition bloat due to lingering prepared transactions holding old snapshots in PostgreSQL 13
Open0 answers