pg-query-rewrite

pg-query-rewrite is a skill for Claude Code, Codex from isdaniel/pgtuner_mcp. It costs 67 tokens per session (3,097 once invoked), scanned A, original, Apache-2.0.

A guide to rewriting PostgreSQL database queries so they can run faster. PostgreSQL is a database system, and query rewriting changes how a request is expressed without changing its intended result.

In plain words
What is it for?
Use it to analyze execution plans and consider alternatives such as JOINs, UNIONs, EXISTS checks, and selecting only needed columns.
Why use it?
It helps investigate slow queries when adding indexes is not enough, including problems caused by subqueries, common table expressions, OR conditions, and inefficient pagination.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit Use it to analyze execution plans and consider alternatives such as JOINs…

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/isdaniel/pgtuner_mcp/pg-query-rewrite
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 isdaniel/pgtuner_mcp --skill pg-query-rewrite
Clone the repo
git clone --depth 1 https://github.com/isdaniel/pgtuner_mcp

Made for: Claude Code, Codex.

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 pg-query-rewrite

README.md
[![agentmods](https://agentmods.dev/badge/skills/isdaniel/pgtuner_mcp/pg-query-rewrite.svg)](https://agentmods.dev/skills/isdaniel/pgtuner_mcp/pg-query-rewrite)
Your own site
<a href="https://agentmods.dev/skills/isdaniel/pgtuner_mcp/pg-query-rewrite"><img src="https://agentmods.dev/badge/skills/isdaniel/pgtuner_mcp/pg-query-rewrite.svg" alt="Measured on agentmods" height="20"></a>
Per session 67 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 3,097 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.00067 $0.03097
Opus 5 $0.00034 $0.01548
Sonnet 5 $0.00013 $0.00619
Haiku 4.5 $0.00007 $0.00310

Measured 7d ago against content hash 7955c48bf087, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-07, from the pricing page.

Security

Grade A, and why

pg-query-rewrite 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 7d 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/pg-query-rewrite/SKILL.md · 371 lines

How it starts

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

PostgreSQL Query Rewrite Patterns

This skill guides the agent through analyzing SQL queries and applying common restructuring patterns to improve performance, using pgtuner-mcp execution plan analysis tools.

When to Use This Skill

Use this skill when the user:

  • Has a specific slow query and wants it rewritten for better performance
  • Asks "how can I make this query faster?"
  • Reports that adding indexes didn't help (query structure is the issue)
  • Has queries with subqueries, CTEs, OR conditions, or NOT IN that could be improved
  • Asks about query optimization patterns or best practices
  • Has queries with SELECT * or inefficient pagination

MCP Resources Available

  • pgtuner://docs/tools -- Reference for analyze_query parameters and options
  • pgtuner://query/{query_hash}/stats -- Get statistics for a specific query by its queryid
  • pgtuner://table/{schema}/{table_name}/indexes -- Check available indexes on involved tables

This skill is closely related to the query_tuning MCP Prompt, which provides a structured query optimization workflow.

Prerequisites

  • The pgtuner-mcp server must be connected with a valid DATABASE_URI
  • Required: pg_stat_statements extension (for identifying slow queries to rewrite)
  • The user should provide the specific SQL query to optimize

Agent Decision Tree

Start: User provides a slow query
  |
  +--> Step 1: Analyze the execution plan
  |
  +--> Look at the plan for these patterns:
       |
       +--> SubPlan / Correlated Subquery?
       |    --> Apply Pattern 1: Subquery to JOIN
       |
       +--> CTE Scan with many rows materialized?
       |    --> Apply Pattern 2: CTE Materialization Control
       |
       +--> BitmapOr / multiple OR conditions?
       |    --> Apply Pattern 3: OR to UNION ALL
       |
       +--> NOT IN with possible NULLs?
       |    --> Apply Pattern 4: NOT IN to NOT EXISTS
       |
       +--> Seq Scan on query with OFFSET + LIMIT?
       |    --> Apply Pattern 5: Keyset Pagination
       |
       +--> Many columns fetched but few used?
       |    --> Apply Pattern 6: Eliminate SELECT *
       |
       +--> Sort on non-indexed column?
       |    --> Recommend index (redirect to pg-index-optimization)
       |
       +--> No obvious rewrite opportunity?
            --> Check statistics freshness (ANALYZE table)
            --> Check index recommendations
            --> Consider configuration tuning (work_mem, etc.)

Read the full file on GitHub · 371 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. 7d ago First seen · 371 lines · 67 tokens per session scan A 7955c48bf087

Subscribe to this mod's changes

pg-query-rewrite is a skill published in the GitHub repository isdaniel/pgtuner_mcp (29 stars, last pushed 3mo ago), licensed Apache-2.0. It adds 67 tokens to every session and 3,097 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-30.