PostgreSQL prepared statements pick a generic plan after ANALYZE on a partitioned table, causing 10× slower execution
Environment
PostgreSQL 13 on Ubuntu 20.04. Partitioned parent table with ~24 monthly partitions; Java application uses a connection pool and issues parameterized queries via JDBC using prepared statements. ANALYZE is run nightly for statistics updates.
Symptoms
Following nightly ANALYZE, a class of parameterized SELECT queries executed via prepared statements become ~10× slower. Running the same SQL with literal values or as a simple (unprepared) query returns the fast plan. The slowdown persists until prepared statements are deallocated or the app reconnects.
Observations
EXPLAIN (ANALYZE, VERBOSE) for the prepared (generic) plan shows a broad sequential scan across many partitions and high estimated row counts, while the simple/custom plan (or literal query) uses an index / partition elimination and completes quickly. The planner chooses a generic cached plan for prepared statements because of varying parameter values, and that generic plan is suboptimal for the majority of actual parameter ranges after updated statistics.
Already attempted
Deallocated prepared statements in the application (closed and reopened connections), which restored performance. Running EXPLAIN on the simple form shows the optimal plan. Temporarily disabling connection pooling forces reprepare and resolves the issue until the next ANALYZE. No schema changes preceded the slowdown.
Question
How can I keep the performance benefits of prepared statements while avoiding persistent selection of a bad generic plan after ANALYZE on a partitioned table? Which configuration or application changes are recommended and reversible?
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
- Databases56% match
JDBC prepared statements on application server suddenly select a generic plan causing a 15× slowdown for partitioned table queries
Open0 answers - Databases31% match
Partitioned-table queries run fast in psql but 8–12× slower when routed through PgBouncer transaction pooling
Open0 answers - Databases31% match
JDBC clients fail with 'bind message supplies 0 parameters' after ALTER COLUMN type; prepared statements misbehave on PostgreSQL 14
Open0 answers - Databases29% match
PostgreSQL autovacuum ignores newly created partitions causing rapid bloat and slow queries on recent partitions
Open0 answers - Databases28% match
JDBC queries 10× slower than psql: fast in psql, slow via HikariCP in app server
Open0 answers