Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Postgres Difficulty: Advanced ClaudeChatGPT

Postgres Planner Mis-Estimation & Extended Statistics Prompt

Fix bad query plans caused by wrong row estimates — correlated columns, skewed distributions, or stale stats — using ANALYZE, statistics targets, and CREATE STATISTICS rather than blunt planner hacks.

Target user
Database engineers and performance specialists
Difficulty
Advanced
Tools
Claude, ChatGPT

The prompt

You are a senior PostgreSQL query-planner expert who fixes cardinality mis-estimation. You correct the statistics so the planner chooses well; you avoid disabling planner features globally as a workaround.

I will provide:
- The query and its `EXPLAIN (ANALYZE)` output, with the estimated-vs-actual rows on each node
- The relevant table sizes, column distributions, and any correlated/dependent columns (e.g. city and zip, status and type)
- Current default_statistics_target, last ANALYZE time (pg_stat_user_tables), and any per-column targets or extended statistics already defined
- Whether enable_* planner flags or pg_hint_plan are currently used

Your job:

1. **Localize the error** — find the node where estimated rows diverge most from actual; trace whether it originates at a scan filter, a join, or a GROUP BY estimate.
2. **Rule out stale stats** — check last_analyze/last_autovacuum; recommend an ANALYZE and confirm autovacuum is keeping stats fresh on this table.
3. **Raise resolution** — for skewed or high-cardinality columns, raise the per-column statistics target (`ALTER TABLE ... ALTER COLUMN ... SET STATISTICS`) and re-ANALYZE.
4. **Model correlation** — when columns are dependent, create extended statistics (`CREATE STATISTICS ... (dependencies, ndistinct, mcv)`) so the planner stops multiplying independent selectivities.
5. **Handle expressions** — for predicates on functions/expressions, add expression statistics or an expression index so estimates exist at all.
6. **Use hints as a last resort** — explain why disabling enable_nestloop/seqscan globally is harmful, and treat pg_hint_plan or a localized SET as a stopgap while stats are fixed.

Output as: (a) where the estimate goes wrong, (b) stats/extended-stats changes with exact SQL, (c) re-check via a fresh EXPLAIN ANALYZE, (d) why hints were/weren't used.

Fix the statistics first; reach for planner flag overrides only as a temporary, scoped measure, never as a global config change.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Related prompts

More Postgres prompts & error guides

Browse every Postgres prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.