FULL AI ANALYSIS

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

Generated Oct 24, 2026, 9:21:01 AM

Analysis 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.

Possible 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%

Network latency or extra hop adding execution variability (less likely given session pooling result)

20%

Suggested fixes

1

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).

2

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).

3

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.

4

Diagnostic step 4

If driver change is infeasible, isolate problematic services onto a session-pooling PgBouncer instance and keep others on transaction pooling.

5

Diagnostic step 5

Instrument: capture pg_stat_statements and PgBouncer logs during tests to confirm plan shape and latency improvements.