writing-performant-queries

writing-performant-queries is a skill for Claude Code from pumarogie/claude-postgres-skills. It costs 59 tokens per session (968 once invoked), scanned A, original, MIT.

A guide to finding and diagnosing slow PostgreSQL queries using database statistics and query plans. PostgreSQL is a relational database system, and a query plan shows how it will retrieve data.

In plain words
What is it for?
Ranking costly queries, inspecting plans safely with EXPLAIN, investigating indexes and statistics, and deciding what query or database change to test.
Why use it?
It prevents guessing at indexes or other fixes before identifying the actual expensive query and checking evidence from the database.

Skill for Claude Code

Written for Claude Code: shipped in a Claude Code plugin. Also seen: positional $N argument.

Part of the database-skill plugin — 6 skills shipped together

Good fit Ranking costly queries, inspecting plans safely with EXPLAIN, investigating indexes and statistics, and deciding what query or database change to test.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/pumarogie/claude-postgres-skills/writing-performant-queries
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add pumarogie/claude-postgres-skills --skill writing-performant-queries
Clone the repo
git clone --depth 1 https://github.com/pumarogie/claude-postgres-skills

Made for: Claude Code.

Or install database-skill, the plugin that ships this one along with the rest of its 6 skills.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for writing-performant-queries

README.md
[![agentmods](https://agentmods.dev/badge/skills/pumarogie/claude-postgres-skills/writing-performant-queries/github.svg)](https://agentmods.dev/skills/pumarogie/claude-postgres-skills/writing-performant-queries)
Your own site
<a href="https://agentmods.dev/skills/pumarogie/claude-postgres-skills/writing-performant-queries"><img src="https://agentmods.dev/badge/skills/pumarogie/claude-postgres-skills/writing-performant-queries/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for writing-performant-queries

Your own site · 80×15
<a href="https://agentmods.dev/skills/pumarogie/claude-postgres-skills/writing-performant-queries"><img src="https://agentmods.dev/badge/skills/pumarogie/claude-postgres-skills/writing-performant-queries.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 59 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 968 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00059 $0.00968
Opus 5 $0.00030 $0.00484
Sonnet 5 $0.00012 $0.00194
Haiku 4.5 $0.00006 $0.00097

Measured 8d ago against content hash 0d471de8a32e, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-08, from the pricing page.

Security

Grade A, and why

writing-performant-queries scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 8d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/writing-performant-queries/SKILL.md · 79 lines

How it starts

The opening of the file, as written. The whole thing — 79 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Writing Performant Queries

Required diagnostic sequence

Follow these steps in order. If the user already provides the query and plan, start at step 2. Never propose an index before identifying which query is slow and inspecting evidence from its plan.

1. Find the expensive query

Use pg_stat_statements to rank normalized SQL before tuning anything. total_exec_time finds aggregate database load; mean_exec_time finds individually slow calls. Compare a defined time window and note when statistics were reset.

SELECT queryid, calls, total_exec_time, mean_exec_time, rows,
       left(query, 200) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;

If it is not installed, add pg_stat_statements to the existing comma-separated shared_preload_libraries, restart PostgreSQL, and create the extension in each database. Do not guess from a generic “the API is slow” report.

2. Inspect that query's plan safely

Use plain EXPLAIN first; it plans but does not execute the statement. Use EXPLAIN (ANALYZE, BUFFERS) only when executing the query is safe and representative.

Warning: EXPLAIN ANALYZE executes the statement. Never run it casually on a production INSERT, UPDATE, or DELETE; it performs writes, takes locks, and can trigger side effects. Prefer staging or a safe read-only reproduction for write queries.

EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM tasks
WHERE tenant_id = $1 AND status = 'pending'
ORDER BY created_at DESC
LIMIT 50;

Read nodes from the inside out. Compare estimated rows with actual rows and inspect loops, buffer reads, sorts, and rows removed by filters. Estimated cost is not elapsed milliseconds.

3. Check statistics before changing indexes

Always check for stale or insufficient planner statistics before assuming an index is missing. A large estimated-versus-actual row mismatch is the signal. Run ANALYZE on the affected table, then inspect the plan again:

ANALYZE tasks;

Read the full file on GitHub · 79 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 8d ago First seen · 79 lines · 59 tokens per session scan A 0d471de8a32e

Subscribe to this mod's changes

writing-performant-queries is a skill published in the GitHub repository pumarogie/claude-postgres-skills (2 stars, last pushed 1mo ago), licensed MIT. It adds 59 tokens to every session and 968 once invoked, about $0.0003 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

database-optimizer

Optimizes database queries and improves performance across PostgreSQL and MySQL systems. Use when investigating slow queries, analyzing execution plans, or optimizing database performance. Invoke for index design, query rewrites, configuration tuning, partitioning strategies, lock contention resolution.

Jeffallan/claude-skills · 54 tokens

postgres-pro

Use when optimizing PostgreSQL queries, configuring replication, or implementing advanced database features. Invoke for EXPLAIN analysis, JSONB operations, extension usage, VACUUM tuning, performance monitoring.

Jeffallan/claude-skills · 41 tokens

postgres-database-migration

Use this skill for planning, testing, and safely executing PostgreSQL schema migrations — especially when working with production data or shared databases. Trigger when user asks to: Test a schema migration before applying it to production Add, remove, or rename columns safely on a live table Change a column's data…

timescale/pg-aiguide · 220 tokens

setup-timescaledb-hypertables

Use this skill when creating database schemas or tables for Timescale, TimescaleDB, TigerData, or Tiger Cloud, especially for time-series, IoT, metrics, events, or log data. Use this to improve the performance of any insert-heavy table. Trigger when user asks to: Create or design SQL schemas/tables AND…

timescale/pg-aiguide · 219 tokens

design-postgis-tables

Comprehensive PostGIS spatial table design reference covering geometry types, coordinate systems, spatial indexing, and performance patterns for location-based applications.

timescale/pg-aiguide · 31 tokens

migrate-postgres-tables-to-hypertables

Use this skill to migrate identified PostgreSQL tables to Timescale/TimescaleDB hypertables with optimal configuration and validation. Trigger when user asks to: Migrate or convert PostgreSQL tables to hypertables Execute hypertable migration with minimal downtime Plan blue-green migration for large tables Validate…

timescale/pg-aiguide · 181 tokens