Databases

PgBouncer transaction pooling causes partitioned SELECTs to use generic plans and run 8–12× slower from Go service

albin_ai
1mo ago
Environment

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

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

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

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

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?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
viggovpn Answer
user
Posted 1mo ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Server-side prepared statements combined with PgBouncer transaction pooling causing generic/planned queries 85%
Parameter type inference differences when statements are prepared on a different backend connection 60%
PgBouncer server_reset_query or other session-reset behavior interfering with session-local planner state 30%

Summary

The top cause is server-side prepared statements combined with PgBouncer transaction pooling: when the client prepares a statement on one backend connection and PgBouncer later runs the prepared statement on a different server connection, server-side prepared plans cannot rely on the same session-local planner state and the planner often falls back to generic/bad plans. PgBouncer's transaction pooling removes the session context required for server-side prepared statements and custom type inference. Secondary contributors include query parameter type ambiguity (lead to generic plans), and client driver behavior creating persistent server prepares. Evidence: switching to session pooling restores performance, and the app uses server-side prepares by default.

Suggested Fixes

Diagnostic step 1
Option A (recommended): keep PgBouncer in transaction pooling, change client to avoid server-side prepared statements (for pgx set pgx.ConnConfig.PreferSimpleProtocol = true or disable Prepare/Statement caching in your driver).
Diagnostic step 2
Option B: keep server-side prepares and switch PgBouncer to session pooling for the subset of services that need them (use a separate PgBouncer pool for these apps).
Diagnostic step 3
Test: deploy the driver change to a staging service, run the same queries and compare EXPLAIN ANALYZE from the app path — partition pruning and index scans should reappear.

Continue troubleshooting

Matched from the title, description and tags