Prompt Engineering SQL PostgreSQL

PostgreSQL Query Analysis — EXPLAIN (ANALYZE, BUFFERS) Reading

A real plan with the story inside: estimate says 42 rows, actual is 51,840. PostgreSQL analysis grounded in the plan — stale statistics, bloat, planner costs, and the index arsenal.

Overview

PostgreSQL analysis starts with the right plan command: EXPLAIN (ANALYZE, BUFFERS) — plain EXPLAIN cannot show actual rows, loops, or buffer traffic. This prompt loads a real plan whose story is in the numbers: a Seq Scan with estimate 42 rows against actual 51,840, and nearly 8M rows removed by the filter — the statistics-staleness signature that invalidates every downstream planner choice. The forensic evidence mode requires every conclusion to cite its operator by name, and the platform guidance brings the PostgreSQL-specific checklist: autovacuum and bloat state, random_page_cost on SSDs, partial and expression indexes where evidence supports them, and whether the query shape blocks parallelism.

Workflow

  1. Capture the right plan

    EXPLAIN (ANALYZE, BUFFERS) — actuals and buffer traffic, not estimates alone.

  2. Read the gaps

    Estimate 42 vs actual 51,840 is not noise — it is the planner working from a false map.

  3. Fix causes, not symptoms

    Stale statistics and bloat get fixed before index advice — the plan may fix itself.

Why This Works

  • Plan-grounded analysis cannot drift into generic advice
  • The estimate-vs-actual lens finds the root cause behind wrong plans
  • Platform guidance covers what PostgreSQL specifically rewards and punishes

Best for

  • PostgreSQL queries that ignore an index you expected
  • Plans whose estimates and actuals disagree wildly
  • Teams moving from guessing to plan-reading

Not for

  • MySQL or SQL Server plans — operator semantics and tooling differ by platform
  • Explaining what the query does functionally — that's the Code Explanation Prompt

Use cases

  • Reading an EXPLAIN ANALYZE output operator by operator
  • Diagnosing estimate-vs-actual gaps as statistics problems
  • Choosing between partial, expression, and BRIN indexes with evidence

Tip: Save time by exploring related resources and tools that integrate with this workflow.

Explore all resources