Partitioned-table queries run fast in psql but 8–12× slower when routed through PgBouncer transaction pooling
Environment
PostgreSQL 14.5 primary on Debian 11 (dedicated server), partitioned table (range partitions by date, ~120 partitions), application servers connect via PgBouncer v1.17.0 in transaction pooling mode; app uses psycopg2 with prepared statements via server PREPARE.
Symptoms
Complex SELECT queries that run in ~120–200 ms when executed directly in psql execute in ~1.0–2.5 s when issued by the application through PgBouncer. Problem only appears under load in production; single-user test from psql always fast. EXPLAIN shows different plans between direct and pooled executions.
Observations
- Reproduced by logging into the same DB with psql on the app host: direct execution fast. 2) When connecting via PgBouncer in session pooling mode, performance matches direct psql. 3) EXPLAIN (ANALYZE, BUFFERS) for the slow run shows a generic plan using bitmap heap scans across many partitions; direct psql uses index-only scans on recent partitions. 4) Setting 'server_prepared_statements' = off in the client removes some variation but not all. 5) No significant CPU or I/O differences observed on DB server during tests; planner estimates differ dramatically between paths.
Already attempted
Switched PgBouncer to session pooling temporarily — queries returned to normal. Disabled client-side prepared statements and replaced with simple statements; performance improved partially. Gathered pg_stat_statements and compared plans. Confirmed application uses connection pooling with prepared statements that are bound at client side.
Question
Why do the same SQL statements choose a generic/inefficient plan when executed through PgBouncer transaction pooling, but optimal plans when run directly or via session pooling? What changes should I make to keep PgBouncer in front of the app without regressing query performance?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases41% match
JDBC prepared statements on application server suddenly select a generic plan causing a 15× slowdown for partitioned table queries
Open0 answers - Databases37% match
PostgreSQL prepared statements pick a generic plan after ANALYZE on a partitioned table, causing 10× slower execution
Open0 answers - Databases31% match
JDBC queries 10× slower than psql: fast in psql, slow via HikariCP in app server
Open0 answers - Databases31% match
PostgreSQL transactions block unexpectedly when mixing SERIALIZABLE snapshot reads and FOR UPDATE in prepared statements
Open0 answers - Databases26% match
PostgreSQL bulk INSERT throughput collapses on partitioned table after stats collector restart
Open0 answers