postgres-query-review
Created here
Use when a PostgreSQL query is slow or about to ship and should be checked against its real execution plan, with concrete index or rewrite suggestions.
SKILL.md
Review a PostgreSQL query against its real execution plan and say what to change, with the evidence for each suggestion.
Arguments
- A query (required; ask for it when missing): the SQL text, a file, or the place in the code that builds it.
- Typical parameter values (optional): without them, ask for realistic ones or take them from the data.
Flow
- Get the exact SQL the database receives. For a query built by an ORM or a query builder, obtain the generated statement (its log or its "to SQL" call) rather than guessing it.
- Read the schema of every table involved through the
dbhubMCP server: columns and types, indexes, constraints, estimated row counts and, for the filtered and joined columns, how selective they are. - Get the plan. Run
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)for a reading query, with realistic parameters. Follow the "Postgres" rules: plainEXPLAINonly for a statement that writes, and ask beforeANALYZEon a database that may be production. Without database access, say so and review from the SQL and the schema files alone, marking every conclusion as unverified. - Read the plan from the most expensive node:
- estimated rows far from actual rows (stale statistics, correlated columns, a function over a column);
- a sequential scan that reads many rows to return few;
- a nested loop over many rows, or a hash or sort that spills to disk;
- rows removed by a filter after an index scan (the index does not cover the condition);
- a sort that an index in the right order would avoid;
- high planning time, or many partitions scanned where one was meant.
- Propose changes in order of effect and cost, following the
supabase-postgres-best-practicesskill: rewrite the query first (a predicate the index can use,EXISTSinstead of a join that multiplies rows, keyset pagination instead of a largeOFFSET, fewer columns), then an index (give the exactCREATE INDEX CONCURRENTLYstatement and say what it costs on writes), then statistics or configuration. - Verify what can be verified without writing: re-run the plan for a rewritten query and compare. An index suggestion stays unverified unless the user creates it; never create it yourself.
- Check the code around the query for the same query run in a loop (one query per row of another result).
Report
- The query, the database it was measured on, and the plan's total time and buffers.
- The cause of the cost, quoting the plan lines that show it.
- Suggestions, best first: the change, the expected effect, the cost or risk, and whether it was verified, with the before and after times.
- What could not be checked, and what to measure on production-sized data.
SKILL.md
SKILL.md holds the skill's instructions; it is edited on the Instructions tab.
Frontmatter written into each target's SKILL.md.
Common
No fields set for this target.