Skip to content
Back to skills

Postgres Index Tuning

ASecurity

Reads EXPLAIN ANALYZE output for a slow Postgres query, then adds or drops indexes only when the measured timing proves a gain.

  • 3 stars
  • 0 votes
  • 0 copies
  • 6 views
  • Added November 15, 2026
ai-agentsnode

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill postgres-index-tuning --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Postgres Index Tuning?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Postgres Index Tuning
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-postgres-index-tuning/badge)](https://www.skillsdirectory.com/skills/workspace-templates-postgres-index-tuning)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: postgres-index-tuning
description: Reads EXPLAIN ANALYZE output for a slow Postgres query, then adds or drops indexes only when the measured timing proves a gain.
---

# Postgres index tuning

One change, one measurement. Never add an index you have not justified from a plan.

## Steps

1. Get the exact query with real parameter values. Run `EXPLAIN (ANALYZE, BUFFERS)` on a copy or inside a rolled-back transaction when it writes. Record total time.
2. Read the plan from the innermost node out. Find the node with the largest actual time. Compare estimated rows to actual rows: a gap of 10x or more points to stale statistics, so run `ANALYZE` on the table and re-measure first.
3. Match the problem to a fix:
   - Seq Scan with a selective filter: b-tree on the filtered columns, equality columns first, then range or sort column.
   - Sort on a large set: index that matches `ORDER BY` so the sort disappears.
   - Many rows fetched for few kept: partial index (`WHERE status = 'open'`) or a covering index with `INCLUDE`.
   - `LIKE '%x%'` or fuzzy text: `pg_trgm` GIN index. JSONB containment: GIN.
4. Create it with `CREATE INDEX CONCURRENTLY` so writes are not blocked.
5. Re-run the same `EXPLAIN`, confirm the plan uses it and the time dropped. If not, drop it.
6. Check unused indexes: `pg_stat_user_indexes` where `idx_scan = 0` for weeks. Report them, do not drop without approval.

## Rules

- Never run `CREATE INDEX` without `CONCURRENTLY` on a live table.
- Do not index low-cardinality booleans alone.
- Mention index size from `pg_relation_size`.

## Report

Query, plan before and after with timings, the index definition, its size and the write cost.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.