postgres-query-review
Skill1 starsChanged 56 days agoApache-2.0View on GitHub
Review PostgreSQL queries (SQL) from performance and correctness angles. Inspect execution-plan interpretation, index usability, join method/order, sort/aggregate cost, pagination, parameter binding, and anti-patterns. Use when writing/modifying SQL or diagnosing slow queries. Applies the principles of "Friendly SQL Tuning", verified against the official PostgreSQL documentation.
Files
PostgreSQL Query Review
Official PostgreSQL Performance Tips/Indexes docs + query-tuning insights.
For the underlying principles, see reference/principles.md.
Review order (proceed exactly like this)
- Understand what the query is asking (result set, cardinality, frequency: OLTP vs batch).
- Look at the execution plan.
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)— for detailed interpretation seereference/kb/execution-plan.md. - Inspect index usability —
reference/kb/index-tuning.md. - Inspect join method/order —
reference/kb/join-tuning.md. - Scan for anti-patterns —
reference/kb/sql-antipatterns.md. - Present a fix + re-measurement method together with severity.
Quick checklist (HIGH-and-above candidates)
- Is there an index on the WHERE/JOIN/ORDER BY columns? If not, full scan.
- Is the leading column not transformed?
WHERE lower(col)=,WHERE col::text=,WHERE date(ts)=,WHERE col + 0 =→ disables the index. Use an expression index or transform the constant side. - Do the types match? Column/parameter type mismatch → casting bypasses the index.
- Is
SELECT *not overused? Only needed columns → covering-index / Index-Only Scan opportunity. - Is there an unnecessary
DISTINCT/ORDER BY/UNION?UNION→UNION ALL(when no duplicates). - Is pagination using a large
OFFSET? → consider keyset (cursor) pagination. - Correlated subquery / N+1: is a query being executed repeatedly inside a loop? Use a join/batch.
- Function calls: if a function used in WHERE is
VOLATILE, indexing/caching is impossible. VerifyIMMUTABLE/STABLE. - Are you using parameter binding? String concatenation is injection + hard parsing. Always use a placeholder.
- Are you reading the entire large result? Can it be handled as a partial range via Top-N (
ORDER BY ... LIMIT)?
Execution-plan warning signs (in EXPLAIN ANALYZE)
- Estimated vs actual row-count divergence (
rows=10butactual rows=100000) → stale statistics/correlation.ANALYZEorCREATE STATISTICS. Seq Scanon a large table + a highly selective filter → missing/disabled index.Sort Method: external merge Disk→ insufficientwork_memor an unnecessary sort.Nested Loopbut the inner side iterates massively → a hash join may be better (check statistics/join key).Rows Removed by Filteris large → the index can't filter and rows are discarded from the table (access condition vs filter condition).Heap Fetchesis large (Index-Only Scan) → VACUUM needed (visibility map).
Deliverables
For each finding: problem → violated principle (principles.md §) / EXPLAIN evidence → fixed SQL → re-measurement command.
A claim that "it improved" is confirmed only by a before/after EXPLAIN ANALYZE comparison.
KB (official PostgreSQL docs — read and cite first)
From reference/kb/INDEX.md, pick and read the KB matching the task type, inspect using each KB's ## Review hooks, and
cite with the source URL.
reference/kb/execution-plan.md— how to read execution plans, per-node meaning, diagnostic queriesreference/kb/index-tuning.md— index selection/order/type, disabling patternsreference/kb/join-tuning.md— NL/Merge/Hash, subquery/semi-/anti-joinreference/kb/sql-antipatterns.md— common anti-patterns and PG prescriptions- Higher-level judgment criteria:
reference/principles.md
In this repository65 other files it gives its agents.
- agent-evalSkill
- architecture-review-workflowSkill
- backend-code-review-workflowSkill
- code-authoring-routerSkill
- code-diagram-extractSkill
- codex-agent-runtimeSkill
- design-pattern-applySkill
- diagram-stylerSkill
- document-designerSkill
- e2e-evidence-reportSkill
- e2e-report-rendererSkill
- e2e-runnerSkill
- e2e-scenario-authorSkill
- e2e-test-workflowSkill
- entitySkill
- enumSkill
- explore-dead-codeSkill
- flywaySkill
- git-flowSkill
- harness-proposeSkill
- harness-refineSkill
- java-spring-review-workflowSkill
- knowledge-auditSkill
- korean-dev-writerSkill
- kotlin-code-authoringSkill
- kotlin-spring-reviewSkill
- kotlin-spring-review-workflowSkill
- learnSkill
- mapperSkill
- markdown-publisherSkill
- mermaid-diagramsSkill
- mssql-code-authoringSkill
- nl-sql-querySkill
- notion-domain-contextSkill
- oop-design-reviewSkill
- opensearch-code-authoringSkill
- opensearch-optimizationSkill
- opensearch-query-reviewSkill
- opensearch-review-workflowSkill
- opensearch-schema-reviewSkill
- pdf-qaSkill
- playwright-e2eSkill
- postgres-code-authoringSkill
- postgres-review-workflowSkill
- postgres-risk-checkSkill
- postgres-schema-designSkill
- product-requirementsSkill
- product-ui-designSkill
- refactoring-catalogSkill
- repositorySkill
- requestSkill
- responseSkill
- retroSkill
- schema-doc-extractSkill
- security-scan-workflowSkill
- serviceSkill
- skill-updaterSkill
- start-taskSkill
- test-authoringSkill
- test-quality-refactoring-workflowSkill
- The first 60 shown.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
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.

