agentleFS
rafapeixerrafapeixer/db-skills/skills/db-explain/SKILL.md

db-explain

Skill1 starsChanged 26 days agoMITView on GitHub

Use when the user asks why a specific SELECT is slow on their PostgreSQL or MySQL database, wants its plan explained, or asks which index or rewrite would fix it. Read-only: runs plain EXPLAIN (FORMAT JSON), and EXPLAIN ANALYZE only for a SELECT, only with the user's consent and under a timeout, reads index and table statistics for the tables involved, and proposes an index or rewrite as a migration without applying it.

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Run queries/<engine>/00-session.sql first: Stop under the same conditions as /db-skills:db-audit: superuser, a risky role or privilege, …

    line 21
  2. Very specific:

    Never: run ANALYZE or ANALYZE TABLE, change settings with SET GLOBAL, create indexes or extensions, or reset statistics.

    line 35
  3. Very specific:

    Guard: Run 00-session.sql. Note the version and whether this is a replica.

    line 43
  4. Distinctive:

    Use the node and field definitions from Using EXPLAIN and EXPLAIN output format.

    line 86
  5. Very specific:

    Optional what-if (PG): If the hypopg extension is already installed, a hypothetical index plus plain EXPLAIN shows whether the planner …

    line 87
  6. Distinctive:

    Format: use the repo's migration tool format.

    line 91

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Explain a query

Purpose

The user gets the plan of one query explained in plain words: where the time or the rows go, why the planner chose that path, and the one change most likely to fix it. That change is either an index, delivered as a migration with its rollback, or a rewrite of the SQL. Each comes with the plan line that proves it worked. Nothing is changed on the database.

Safety

  • Read-only is enforced by the database role. Connect with the role from skills/db-audit/setup/pg-audit-role.sql or skills/db-audit/setup/mysql-audit-role.sql. The user creates it; you never run setup files.
    • Privileges: EXPLAIN needs the same privileges as the statement it explains (MySQL EXPLAIN; Postgres checks table permissions the same way). So this workflow needs SELECT on the tables involved: the optional, consented grant in the setup file. Ask for the narrow per-table grant before pg_read_all_data.
    • Why the role: client-side read-only modes are not a boundary. AWS's MCP servers were bypassed through COPY ... TO PROGRAM (CVE-2026-87911) and SQL comments (CVE-2026-85788). AWS says the role's permissions are the effective limit.
  • Run queries/<engine>/00-session.sql first. Stop under the same conditions as /db-skills:db-audit: superuser, a risky role or privilege, writable tables, or a session that is not read-only.
  • Plain EXPLAIN by default. It plans the statement without running it.
  • EXPLAIN ANALYZE runs the statement. Postgres says the statement is actually executed and its side effects happen as usual (EXPLAIN). MySQL's EXPLAIN ANALYZE runs the statement too, including multi-table UPDATE and DELETE (EXPLAIN). Use it only when all of these hold:

    1. The statement is a single SELECT, WITH ... SELECT with no data-modifying CTE, TABLE or VALUES.
    2. It has no FOR UPDATE/FOR SHARE (or other locking clause), no SELECT ... INTO, and calls no function you cannot see is read-only: nextval, advisory locks, dblink, user functions.
    3. The user said yes after seeing the exact statement, the host, whether it is a replica, and the timeout.
    4. It runs inside BEGIN READ ONLY / START TRANSACTION READ ONLY with a statement timeout, and ends in ROLLBACK.

    In a read-only transaction, Postgres rejects SELECT ... FOR UPDATE and the timeout cancels a run that is too long. Those are a second guard, not the reason to try.

  • Never EXPLAIN ANALYZE a write. Not even inside BEGIN ... ROLLBACK: row locks are held until the rollback, triggers fire, and sequences advance. The audit role also cannot plain-EXPLAIN a write, because it holds no write privilege. To see how a write finds its rows, explain the equivalent SELECT with the same FROM and WHERE.
  • Literals.
    • The user's query and its parameter values may contain personal data. Use real values only in the session, never in the report.
    • Plan JSON repeats constants in Filter, Index Cond and attached_condition: replace them with ? when you quote plan lines.
    • The helper files never read pg_stats.most_common_vals/histogram_bounds or histogram buckets.
  • Never run ANALYZE or ANALYZE TABLE, change settings with SET GLOBAL, create indexes or extensions, or reset statistics. When stale statistics are the cause, the report tells the user to run ANALYZE themselves.

Steps

  1. Get the query. Take it from the argument, or ask. If the user points at code (an ORM call, a query builder), get the SQL it actually sends. Prefer the normalized text from 05-statement-stats.sql (PG) or 04-statement-stats.sql (MySQL), or the app's SQL log, over a reconstruction. If the query has parameters:
    • PG 16+: plan it as a generic plan with GENERIC_PLAN (EXPLAIN). That option cannot be combined with ANALYZE.
    • Otherwise: ask for representative values, including a common value and a rare one, since they can produce different plans.
  2. Classify it with the rules in Safety. A write gets rewritten as its row-finding SELECT. More than one statement means refuse, and ask for one.
  3. Guard. Run 00-session.sql. Note the version and whether this is a replica.
  4. Collect context. List the tables the query reads. Run the helper files after the one allowed edit: replace the table names in the ARRAY[...]/IN (...) literal, and the fragment in the LIKE pattern.
    • PG: 01-indexes-for-tables, 02-table-stats, 03-column-stats, 04-planner-settings, 05-statement-stats (skip it without pg_stat_statements).
    • MySQL: 01-indexes-for-tables, 02-table-stats, 03-planner-settings, 04-statement-stats.
  5. Plain plan. Build the statement at run time from the user's query:
    • PostgreSQL: EXPLAIN (FORMAT JSON, SETTINGS) <query>; with parameters on 16+, EXPLAIN (GENERIC_PLAN, FORMAT JSON) <query>.
    • MySQL: EXPLAIN FORMAT=JSON <query>; add EXPLAIN FORMAT=TREE <query> when the JSON is hard to read. MySQL 9.x defaults the JSON to layout version 2, which has no access_type or key fields; run SET SESSION explain_json_format_version = 1; first so the plan has the fields this workflow reads (explain_json_format_version).
  6. Measured plan, only with consent. Offer it when estimates and reality may differ, for example a large row-estimate gap or a production mean far from the plan's cost. Show the exact block, then run it only on a yes:

    -- PostgreSQL (BUFFERS is automatic on 18; TIMING OFF lowers overhead)
    BEGIN READ ONLY;
    SET LOCAL statement_timeout = '5s';
    SET LOCAL lock_timeout = '1s';
    EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) <select>;
    ROLLBACK;
    -- MySQL (EXPLAIN ANALYZE prints TREE format only)
    SET SESSION max_execution_time = 5000;
    START TRANSACTION READ ONLY;
    EXPLAIN ANALYZE SELECT /*+ MAX_EXECUTION_TIME(5000) */ ...;
    ROLLBACK;

    A timeout is a finding in itself: report "runs longer than 5 s". Do not raise the timeout without asking.

  7. Read the plan. Find the nodes that hold most of the cost or time, and the row estimates that are wrong by 10x or more. Then match them to a cause:

    What the plan showsLikely causeFix to proposeDecision skill
    PG Seq Scan with Rows Removed by Filter far above rows returned; MySQL access_type: ALL, high rows_examined_per_scan, low filteredno index for the filtercomposite index: equality columns first, then the range or sort columnpg-indexing / MySQL indexing skill
    Index Scan followed by a Filter that drops most rows; MySQL used_key_parts shorter than the indexwrong column order, or a range before an equality columnreorder or extend the indexpg-indexing
    Sort with external merge Disk; MySQL using_filesort on a large row countno index delivers the order, or sort memory is too smallindex matching WHERE + ORDER BY; else a per-role work_mempg-indexing, pg-config-by-size
    Hash with Batches > 1; MySQL using_temporary_table spillinghash or temp table larger than memoryreduce rows earlier, or raise memory per rolepg-config-by-size
    Nested Loop with many loops on an inner Seq Scanmissing index on the join columnindex the inner table's join column (often an FK)pg-indexing
    Limit above a large Offset, or a sort of every row for the first pageOFFSET paginationkeyset pagination on an indexed keypg-query-patterns
    Filter on lower(col), col::text, date(col); MySQL comparing a string column with a numberfunction or type mismatch hides the indexrewrite to compare the bare column, or an expression indexpg-indexing, pg-query-patterns
    Index Only Scan with high Heap Fetchesvisibility map stale, vacuum behindautovacuum tuning for the tablepg-vacuum-and-bloat
    Estimate far from actual on correlated columnsplanner assumes independenceCREATE STATISTICS on the columns (docs), then the user runs ANALYZEpg-observability
    Estimate far from actual and n_mod_since_analyze high or last_analyze oldstale statisticsthe user runs ANALYZE <table>; check autovacuum settingspg-vacuum-and-bloat
    Large JIT time on a short queryJIT cost above the query's own costjit = off for the OLTP rolepg-config-by-size

    Use the node and field definitions from Using EXPLAIN and EXPLAIN output format. A fix must remove most of the cost, not 10%. If no index or rewrite does that, say so and point to the data model or caching.

  8. Optional what-if (PG). If the hypopg extension is already installed, a hypothetical index plus plain EXPLAIN shows whether the planner would use the proposed index. It is session-local and builds nothing (hypopg). Installing the extension is DDL: suggest it, do not do it.
  9. Write the fix.
    • Postgres index: SET lock_timeout = '5s'; CREATE INDEX CONCURRENTLY IF NOT EXISTS ... outside a transaction (CREATE INDEX). Rollback: DROP INDEX CONCURRENTLY IF EXISTS ....
    • MySQL index: SET SESSION lock_wait_timeout = 5; ALTER TABLE t ADD INDEX ix (...), ALGORITHM=INPLACE, LOCK=NONE (online DDL). Rollback: ALTER TABLE t DROP INDEX ix.
    • Format: use the repo's migration tool format. Create the file only if the user asks.
    • Rewrite: show the before and after SQL, and where in the code it changes.
  10. Report in the format below.

Output

Query: <redacted, normalized text>   Engine: <engine version>, <primary|replica>
Plan: <estimated (EXPLAIN) | measured (EXPLAIN ANALYZE, with consent)>   Parameters: <generic plan | sample values, not shown>
Production: <calls, mean, max from pg_stat_statements / digest summary, or "not available">

Where the cost is: the 1–3 nodes that matter, quoting plan lines with constants redacted:

NodeTable / indexEstimated rowsActual rowsShare of cost or timeProblem

Findings: ordered by impact:

#SeverityFindingEvidenceRelated skillProposed migration or rewrite (rollback)How to verify
E1HIGHorders filtered by customer_id and sorted by created_at with no matching indexSeq Scan on orders, Rows Removed by Filter: 2,400,000 per call; mean 48 ms over 2.1M callspg-indexingCREATE INDEX CONCURRENTLY orders_customer_created_idx ON orders (customer_id, created_at) (rollback DROP INDEX CONCURRENTLY orders_customer_created_idx)plain EXPLAIN shows Index Scan using orders_customer_created_idx with no Sort; mean_exec_time falls after deploy

The row above is an example of the format.

After the table, give the full migration or rewrite, the write cost of the new index (one more index to maintain on every insert, and on every update of its columns), and anything the user must run themselves, such as ANALYZE. Write the report to a Markdown file in the repo only if the user asks.

When not to use

  • The user wants a health check of the whole database, not one query: use /db-skills:db-audit.
  • The query is fast alone but slow under load because of lock waits or connection queuing: the plan will look fine. Use /db-skills:db-audit (sessions and lock waits) or /db-skills:db-connection-budget.
  • The statement is an INSERT, UPDATE, DELETE or DDL and the user wants it measured: this workflow will not execute it. Measure it on a staging copy the user controls.
  • The audit role has no SELECT on the tables and the user declines the grant: EXPLAIN cannot plan the query. Ask for an explain from someone with access, or use a staging copy.
  • The tables on the connected instance are much smaller than production, as on a laptop or fresh staging: the planner will choose differently. Explain against production or a replica.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.