agentleFS
tejastn10tejastn10/workbench/skills/data/postgres-indexing/SKILL.md

postgres-indexing

Skill1 starsChanged 2 months agoMITView on GitHub

Choose and verify Postgres indexes for real query shapes — index type (btree / GIN / GiST / BRIN), composite column order, partial / covering / expression indexes, reading EXPLAIN ANALYZE, adding them CONCURRENTLY, and knowing when NOT to index. Use when a query is slow, when reviewing a PR that adds a query, when asked "what index do I need", or "why is this doing a seq scan".

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Start from the query, not the table.

    line 8
  2. Distinctive:

    Every index is write amplification and bloat.

    line 46
  3. Very specific:

    After: re-run EXPLAIN ANALYZE, confirm the plan uses it and is faster.

    line 54
  4. Distinctive:

    Check for now-redundant indexes (a prefix of the new composite) and drop them.

    line 55

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Postgres indexing

Start from the query, not the table. For each slow or new query, get the plan first: EXPLAIN (ANALYZE, BUFFERS) <query>.

Reading the plan

  • Seq Scan on a large table with a selective WHERE → missing/unused index.
  • Rows estimate wildly off actual → stale stats (ANALYZE <table>), or a correlation the planner can't see (consider extended statistics).
  • Filter removing most rows after an index scan → the index doesn't cover the predicate; add the column or use a partial index.
  • Sort / Hash spilling to disk → work_mem too low, or an index could provide the order.
  • Nested Loop with high loop count → the join column isn't indexed on the inner side.

Index type

UseType
equality, range, sort, most thingsbtree (default)
@>, ?, full-text, jsonb containment, array membershipGIN
geometry, range types, exclusion constraints, nearest-neighbourGiST
huge append-only table, naturally ordered column (time), tolerate imprecisionBRIN (tiny, cheap)
dedupe / uniquenessbtree UNIQUE

Shaping the index

  • Composite column order: equality predicates first, then the range / sort column. WHERE tenant_id = ? AND created_at > ? ORDER BY created_at → (tenant_id, created_at).
  • Partial index — WHERE status = 'active' when you only ever query active rows. Smaller, faster, and it's the whole index for that query.
  • Covering index — INCLUDE (col) so the query is an index-only scan, no heap fetch.
  • Expression index — CREATE INDEX ON t (lower(email)) if you query WHERE lower(email) = ?.
  • Don't index: low-cardinality columns alone (a boolean), tiny tables, write-heavy tables where the read isn't hot, columns never in a predicate. Every index is write amplification and bloat.

Adding safely

  • CREATE INDEX CONCURRENTLY on any table with live writes — a plain CREATE INDEX takes an ACCESS EXCLUSIVE-ish lock and blocks writes.
  • Sequence it via deployment/data-migration; it can't run inside a transaction block.
  • After: re-run EXPLAIN ANALYZE, confirm the plan uses it and is faster.
  • Check for now-redundant indexes (a prefix of the new composite) and drop them.

Anti-patterns

  • ❌ Adding an index without EXPLAIN before and after.
  • ❌ Plain CREATE INDEX on a hot table.
  • ❌ One index per column instead of a composite matching the query.
  • ❌ Indexing a boolean or a tiny table.
  • ❌ Leaving redundant/duplicate indexes after adding a composite.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

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.