PostgreSQL query becomes slow only with prepared statements
Environment
PostgreSQL 16 application using Prisma and a table with highly skewed tenant data.
What happens
The same SQL is fast in psql but intermittently takes seconds through the application.
What I have tried
Added the expected indexes and ran ANALYZE on the table.
I would appreciate advice on how to identify the root cause without risking existing data or configuration.
Verified community troubleshooting
The logs and behaviour point in the same direction here.
Likely cause: A generic prepared plan is poor for tenants whose row counts differ significantly.
What I would try: Compare custom and generic plans with EXPLAIN ANALYZE, improve statistics and use an appropriate plan cache mode for the affected workload.
Document the original value before changing it and restore it if the symptom remains.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases29% match
JDBC prepared statements on application server suddenly select a generic plan causing a 15× slowdown for partitioned table queries
Open0 answers - Databases27% match
PostgreSQL prepared statements pick a generic plan after ANALYZE on a partitioned table, causing 10× slower execution
Open0 answers - Databases26% match
Partitioned-table queries run fast in psql but 8–12× slower when routed through PgBouncer transaction pooling
Open0 answers