Databases

PostgreSQL query becomes slow only with prepared statements

Environment

Environment

PostgreSQL 16 application using Prisma and a table with highly skewed tenant data.

What happens

What happens

The same SQL is fast in psql but intermittently takes seconds through the application.

What I have tried

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.

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
matte_dev Answer
user
Posted 2w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

A generic prepared plan is poor for tenants whose row counts differ significantly. 85%
A recent software, firmware or configuration change is exposing an existing compatibility issue. 39%

Summary

The reported behaviour is consistent with a generic prepared plan is poor for tenants whose row counts differ significantly. The safest next step is to verify that hypothesis with a reversible test before making permanent configuration changes.

Suggested Fixes

Verify the likely cause
Compare custom and generic plans with EXPLAIN ANALYZE, improve statistics and use an appropriate plan cache mode for the affected workload.
Validate and monitor
Reproduce the issue once after the change, review relevant logs or metrics, and keep a rollback path until the system has remained stable.

Continue troubleshooting

Matched from the title, description and tags