CtrlK
BlogDocsLog inGet started
Tessl Logo
PluginPublic

g14wxz/query-explain-plan-debugging

Provides EXPLAIN ANALYZE workflow for identifying missing indexes, sequential scans, and query plan issues.

Quality

94%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide
SecuritybySnyk

Passed

No findings from the security scan

Overview
Quality
Evals
Security
Files

SKILL.mdskills/query-explain-plan-debugging/

name:
query-explain-plan-debugging
description:
Executes EXPLAIN ANALYZE via MCP to debug slow queries, identify missing indexes, detect sequential scans, and optimize query plans. Use when debugging slow SQL, analyzing query plans, finding unused indexes, optimizing Postgres queries, or investigating index-not-used issues.

Pre-Conditions

  • The agent MUST have MCP execute_sql access to the target database.
  • The supabase-mcp-verification tile MUST be installed and passing. See .skills/supabase-mcp-verification.md.
  • The db-diagnostics-inspection tile MUST be available for follow-up index analysis. See .skills/db-diagnostics-inspection.md.
  • The slow query or suspect query text MUST be provided or identified.

Execution Phases

Phase 1 — Capture the Explain Plan

  1. Wrap the target query with EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON).
  2. Execute via MCP execute_sql. Example call:
    EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
    SELECT orders.id, customers.name
    FROM orders
    JOIN customers ON orders.customer_id = customers.id
    WHERE orders.status = 'pending';
  3. The result is a JSON array. Store the full output for analysis. A minimal node looks like:
    {
      "Node Type": "Seq Scan",
      "Relation Name": "orders",
      "Filter": "(status = 'pending')",
      "Rows Removed by Filter": 48200,
      "Plan Rows": 100,
      "Actual Rows": 312,
      "Shared Hit Blocks": 5,
      "Shared Read Blocks": 980
    }
  4. NEVER run EXPLAIN ANALYZE on destructive statements (DELETE, UPDATE, INSERT) in production without explicit user approval — these execute the query.

Phase 2 — Identify Sequential Scans

  1. Search the plan for nodes where "Node Type": "Seq Scan".
  2. For each Seq Scan, check if the filtered column has an existing index.
  3. If an index exists but is not used, record the column and table as a planner anomaly.
  4. If no index exists on a WHERE or JOIN column, flag it as a missing index.
  5. Defer to db-diagnostics-inspection when: there are multiple Seq Scans across several tables, existing indexes are consistently ignored by the planner, or you need a broader audit of index health and bloat across the schema.

Before/After Example:

  • Before: Seq Scan on orders with Filter: (status = 'pending'), scanning 50 000 rows to return 312.
  • Fix: CREATE INDEX CONCURRENTLY idx_orders_status ON orders (status);
  • After: Index Scan using idx_orders_status on orders, returning 312 rows with no full-table scan.

Phase 3 — Evaluate Row Estimates vs Actuals

  1. Compare "Plan Rows" to "Actual Rows" at each node.
  2. Flag any node where the ratio exceeds 10x in either direction.
  3. A high overestimate or underestimate indicates stale statistics — recommend ANALYZE table_name.
  4. Defer to db-diagnostics-inspection when stale statistics appear across many tables, suggesting a systemic autovacuum or maintenance issue rather than a single-query problem.

Phase 4 — Detect Problematic Join Patterns

  1. For any "Node Type": "Nested Loop" node, flag it if the inner relation's "Actual Rows" exceeds 1000 — recommend a Hash Join, Merge Join, or a join-column index to enable an Index Nested Loop.
  2. NEVER suggest restructuring the join without first checking whether a join-column index alone resolves it.

Phase 5 — Check Buffer Usage

  1. For each major node, compute the read ratio: Shared Read Blocks / (Shared Hit Blocks + Shared Read Blocks).
  2. Flag the node when this ratio exceeds 0.5 (i.e., more than half of block accesses required disk reads).
  3. Recommend ANALYZE table_name when stale statistics are also present; recommend reviewing shared_buffers configuration when the ratio is high across multiple unrelated nodes.

Phase 6 — Produce Fix Recommendations

  1. For each identified issue, emit a specific SQL fix:
    • Missing index: CREATE INDEX CONCURRENTLY idx_<table>_<col> ON <table> (<col>);
    • Stale stats: ANALYZE table_name;
    • Nested Loop on large table: suggest restructuring the query or adding a join index.
  2. NEVER suggest CREATE INDEX without CONCURRENTLY on production tables.

Phase 7 — Verification Report

  1. Re-run EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) after applying fixes.
  2. Compare total execution time before and after.
  3. Confirm: sequential scans eliminated (yes/no).
  4. Confirm: row estimate accuracy improved (yes/no).
  5. Report: before-time, after-time, indexes added, nodes changed.
  6. If performance remains poor after fixes, hand off to db-diagnostics-inspection for deeper schema-level analysis.

skills

query-explain-plan-debugging

tile.json