FormingSkillclaude-codepublished fresh until

How do I get my agent to tune a slow Postgres query safely?

Summary When an agent is tuning a slow Postgres query, rank it with pg_stat_statements, inspect plans on a database copy, compare candidate indexes with HypoPG and plain EXPLAIN, then draft migrations; hypothetical costs are not measured speedups.

Not for giving an agent safe access to a Postgres database; see postgres-database-access-for-agents

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

The option

Option
ASkill · v1.0.0supabase-postgres-best-practices skill (Supabase)
How you use itYou ask for it
What it can dowrites files
What's inside
ASkill · v1.0.0supabase-postgres-best-practices skill (Supabase)
How you use it
You ask for it
What it can do
writes files
What's inside

What this thread does

The routine

As of November 2026, use a database copy for execution plans and compare hypothetical indexes before drafting migrations. Do first: select a branch or copy, then collect slow-query statistics. Daniel Vaughan's write-up is dated 19 May 2026 and updated 9 October 2026.

1. Rank queries by total execution time, mean time and calls with pg_stat_statements. 2. On the copy, run EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) and inspect expensive nodes. PostgreSQL distinguishes estimated cost from actual runtime in its EXPLAIN guide. 3. Check for existing indexes covering the same columns. 4. Generate candidates; consider partial indexes for selective filters. 5. Compare estimated costs using HypoPG with plain EXPLAIN, without ANALYZE: hypothetical indexes cannot serve real execution. See HypoPG usage. 6. Set an explicit promotion threshold: Vaughan's flowchart uses over 50 percent, while his reusable skill uses over 30 percent. These are example policies, not measured speedups. 7. Write migrations for promising candidates, measure on the copy, and submit a pull request.

Tooling

The Postgres MCP Pro README (MIT) lists matching tools: get_top_queries, explain_query (can take hypothetical indexes), analyze_query_indexes (up to 10 queries), analyze_workload_indexes and analyze_db_health. For index tuning and comprehensive performance analysis, load the pg_stat_statements and hypopg extensions on the database; hypopg may need a system package. It has a restricted mode (read-only transactions plus an execution time limit) meant for production and an unrestricted mode for development.

Rules worth putting in AGENTS.md (from the write-up)

  • Never run EXPLAIN ANALYZE against production; use a branch or read replica.
  • Create indexes only through migration files, never ad hoc DDL.
  • Paginate any query that can return more than 100 rows.
  • Index the referencing column of every foreign key.
  • Justify a sequential scan on a large table in a comment.

Common mistakes

  • EXPLAIN ANALYZE actually executes the statement. Use it on reads, or on a throwaway copy for writes as explained in the PostgreSQL guide.
  • Trusting row estimates when statistics are stale; run ANALYZE on the copy first.
  • Adding an index for every slow query. Index maintenance adds write overhead; consider a query rewrite or a materialised view first.
  • Skipping the HypoPG check and shipping an index the planner never uses.
  • Giving a community MCP server write access to a real database.

What the sources do not show

The write-up is one person's account about Codex and gives no measured results. EconGnome in an r/dataengineering post says that in his experience agents are very poor at tuning Spark jobs because they optimise one symptom instead of the whole pipeline; that is a claim about Spark, not Postgres, but it is a fair warning to treat the agent's advice as a hypothesis you measure. Neither source provides an independent benchmark of this Postgres tuning routine.

Started from a post by @Daniel Vaughan, packaged by the editor. Original post

  1. 01 · YouPaste the line from Try it into your agent.
  2. 02 · Your agentGets the package and a trial ticket, installs it on a new branch, runs its check.
  3. 03 · YouApprove a one-line report. Nothing is posted without you.

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I get my agent to tune a slow Postgres query safely?
Summary: When an agent is tuning a slow Postgres query, rank it with `pg_stat_statements`, inspect plans on a database copy, compare candidate indexes with HypoPG and plain EXPLAIN, then draft migrations; hypothetical costs are not measured speedups.

## Options
- A · supabase-postgres-best-practices skill (Supabase) · early, no verdict yet · writes files
Copy for agent
get the whatworks thread agent-tunes-slow-postgres-query-explain-hypopg and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread agent-tunes-slow-postgres-query-explain-hypopg

How the verdict is calculated

We count trial reports at Tested and above, weighted by evidence: Verified counts ×1.5 and Platform ×2. A report that worked partly counts as half.

Under 10 trials no verdict is shown. The option asks for founding testers instead.

Ask your agent
how does whatworks decide a verdict

A · supabase-postgres-best-practices skill (Supabase)

Supabase's Postgres rules for any Postgres: about 30 one-page references on indexes, EXPLAIN ANALYZE, connection pooling, locking, RLS and schema design, loaded when the agent diagnoses slow queries or writes migrations. Does not include HypoPG or CREATE INDEX CONCURRENTLY, and recommends EXPLAIN ANALYZE without a production warning: run it on a copy.

Format
Skill
Source
github:supabase/agent-skills#skills/supabase-postgres-best-practices
Pinned
v1.0.0 · c9be0e931b79
Content hash
c9be0e931b7930f7
Audit
scan passed
Can do
writes files
Works with
claude-code
Check
Skill installed in the project; SKILL.md front matter names the skill; EXPLAIN ANALYZE rule present; Missing-index reference present
Author
@supabase
Kudos
0
Are you @supabase? Sign in with GitHub to claim it.
Ask your agent
show me the whatworks package supabase-postgres-best-practices-skill before installing

A · Files

Read from the package source at the pinned version. Shown as is; nothing here runs.

Loading files…

Ask your agent
show me the files of the whatworks package supabase-postgres-best-practices-skill
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp