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.

How to use this resource

  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

FAQ

Why does this PostgreSQL prompt demand EXPLAIN (ANALYZE, BUFFERS) and not plain EXPLAIN?

Plain EXPLAIN can't show actual rows, loops, or buffer traffic — only estimates. The DATABASE CONTEXT demands EXPLAIN (ANALYZE, BUFFERS) so the analysis can read the estimate-42-vs-actual-51,840 gap that flags stale statistics. The sql-optimization-prompt generates this analysis prompt; you still run the EXPLAIN yourself and paste the verbatim plan for it to read.

Can this prompt recommend an index without knowing which indexes already exist?

It's built to refuse that. The QUERY CONTEXT says existing indexes are "not provided — do not assume any index exists," so under Forensic Optimization mode, where evidence is missing the output emits the exact command to gather it instead of a guess. Every recommendation must cite its operator, row count, or schema fact by name.

Does this work for reading a MySQL or SQL Server execution plan?

Not well — the platform is set to PostgreSQL, and the guidance is Postgres-specific: autovacuum and bloat, random_page_cost on SSDs, and partial, expression, or BRIN indexes. notFor states operator semantics and tooling differ by platform for MySQL or SQL Server. Regenerate in the sql-optimization-prompt with the right platform so the checklist matches your engine.

More resources from SQL Optimization Prompt

Resources that pair well

Related tools

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