/tsh-stack-postgresql:optimizing-postgresql-queriesfrom TSH Stack: PostgreSQL
Diagnoses and fixes slow PostgreSQL queries using pg_stat_statements, EXPLAIN ANALYZE BUFFERS, and index selection (composite, partial, covering, GIN, BRIN). Covers query shapes that defeat indexes: N+1 loops, OFFSET pagination, functions on indexed columns, OR across columns, implicit casts.
Ships in TSH Stack: PostgreSQL. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
A slow query is fixed by evidence, not by adding indexes until it improves. Measure which queries cost the most time, read their actual plans, change one thing, and measure again.
PostgreSQL 14 to 18; 14 reached end of life in November 2026. Plans and features
differ by version (18 adds skip scan on multicolumn B-tree indexes and shows buffer
counts by default in EXPLAIN ANALYZE). Check SELECT version(); before reasoning
about a plan. Below 14, stop and say the project is on an unsupported version;
continue only when asked.
PostgreSQL only. If the project's database is MySQL, MariaDB, SQL Server or another engine, say this skill does not apply and stop: plan output, index types and statistics views differ by engine.
Read how the project issues queries first: ORM, query builder or raw SQL, and where query logging and metrics already exist. The ORM's own skill owns how to express the fix in that ORM; this skill owns what the database does with the SQL. An existing indexing or performance convention in the project outranks this skill.
This skill does not change server configuration or run anything that writes to a
shared or production database. EXPLAIN ANALYZE executes the statement: on
production, run it only for SELECT, and only when the person responsible agrees.
| Severity | Rule |
|---|---|
| MUST | Start from measured time: pg_stat_statements, the slow-query log, APM traces or a timed reproduction. A query that is slow once is not the one to optimise if another runs 10,000 times a minute. |
| MUST | Read the actual plan with EXPLAIN (ANALYZE, BUFFERS) on realistic data before proposing a fix. A plan on an empty development database proves nothing. |
| MUST | Compare estimated and actual rows at every node. An estimate off by 10x or more is the first thing to explain. |
| MUST | Measure again after the change and report before and after numbers. |
| MUST | Justify every new index with the query it serves and the plan that uses it, and account for its cost on writes. |
| NEVER | Run EXPLAIN ANALYZE on an INSERT, UPDATE or DELETE against a shared database. Even rolled back it takes row locks, fires triggers and advances sequences; use a copy, or plain EXPLAIN. |
| NEVER | Create an index on a production table outside a migration. Hand the index to migrating-postgresql-schemas so it ships CONCURRENTLY. |
| NEVER | Turn off planner methods (enable_seqscan = off) or add hints as a fix. They are diagnostic tools only. |
| Reference | Load when | Covers |
|---|---|---|
| Finding slow queries | No specific query is known yet, or you need to rank candidates | pg_stat_statements, the slow-query log, auto_explain, what to read first |
| Reading query plans | A plan is in front of you, every time | Node types, estimates versus actuals, buffers, loops, the usual culprits |
| Choosing indexes | An index is proposed or a scan should use one | B-tree column order, partial, covering, expression, GIN, BRIN, unused and duplicate indexes |
| Query anti-patterns | The plan is reasonable but the query shape is wrong | N+1, OFFSET pagination, functions on columns, OR, implicit casts, count(*), SELECT * |
Read reading-query-plans.md before explaining any plan.
EXPLAIN (ANALYZE, BUFFERS) (on 18, BUFFERS is included by default). If only
production has such data, use plain EXPLAIN there and say the plan is estimated.ANALYZE), a query shape that
defeats indexes (query-anti-patterns.md),
a missing or wrong index (choosing-indexes.md),
then schema changes. Each later fix costs more to ship and to keep.migrating-postgresql-schemas; a query change through the normal code path.Report the query, how often it runs, the plan node that dominates and why, the change, and the time before and after. When the database is not the bottleneck (the time is in the application or the network), say so and stop.
What SKILL.md sets; muted values are Claude Code's defaults
Trigger on: a slow endpoint, report or background job traced to a query, a query plan or EXPLAIN output pasted for explanation, a sequential scan on a large table, deciding which index to add or whether an existing one is used, N+1 queries from an ORM, deep pagination getting slower, high database CPU or I/O, pg_stat_statements or slow-query-log output, a query that was fast and became slow, or reviewing a change that adds a query to a hot path. A migration that adds the index safely is migrating-postgresql-schemas; table and column design is designing-postgresql-schemas.
Three short reads, in order
npx claudepluginhub thesoftwarehouse/agentic-collections --plugin tsh-stack-postgresql3 skills in this plugin
/designing-postgresql-schemasDesigns and reviews PostgreSQL tables at the database level, whatever ORM maps them: column types (timestamptz, numeric or integer minor units for money, text over varchar(n), identity or UUIDv7 keys, jsonb), keys and constraints that keep bad data out (NOT NULL, CHECK, foreign keys and their ON DELETE, unique and exclusion constraints), and the recurring patterns: soft delete, multi-tenancy with row-level security, audit columns, optimistic locking, status history, partitioning. Use when creating tables for a new feature, reviewing an entity or schema change, or deciding how to store a kind of data./migrating-postgresql-schemasWrites and reviews PostgreSQL schema migrations that run against a live database without stopping traffic: the lock each DDL statement takes and what it blocks, lock_timeout so a migration waits instead of queueing everyone behind it, expand-and-contract for renames and type changes, NOT VALID constraints validated separately, indexes built CONCURRENTLY outside a transaction, and batched backfills. Use when a migration adds, changes or drops a column, index or constraint on a table that already holds production data, or when reviewing a migration an ORM generated.Other plugins' skills for the same job