PostgreSQL
Tunes, designs, and operates PostgreSQL: slow queries, indexes, schemas, migrations, vacuum, locks, replication, backups. Use when writing Postgres SQL or…
by clawic★ 23 GitHub stars
Install PostgreSQL in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
User preferences and memory live in ~/Clawic/data/pg/ (see setup.md on first use, memory-template.md for the file format). If you have data at an old location (~/pg/ or ~/clawic/pg/), move it to ~/Clawic/data/pg/, and say in one line that you moved it and from where.
When To Use
- A query is slow, a plan needs interpretation, or an index is being ignored
- Designing a schema: types, keys, constraints, partitions, JSONB vs columns
- Running DDL, a migration, or a backfill against a live production table
- Operating Postgres: connections, timeouts, vacuum, bloat, locks, replication, backups, upgrades
- A production incident: disk full, wraparound warnings, connection storm, runaway query, suspected corruption
- Building queue, upsert, search, or vector patterns that Postgres solves natively
- Not for cross-engine SQL portability or ORM-level modeling (see Related Skills)
Quick Reference
| Situation | Play |
|---|---|
| Slow query, cause unknown | Slow-Query Triage below, in order — never tune from query text alone |
| Plan shows a seq scan you did not expect | Not automatically the bug: a scan beats an index past roughly 5-15% of the table (→ slow-queries.md) |
| WHERE on a function of a column | Expression index matching the query text exactly (→ Indexing Essentials) |
| Status/flag column, minority of rows queried | Partial index WHERE status = 'active' |
Substring/fuzzy search (LIKE '%x%', ILIKE, typos) |
pg_trgm GIN index; a B-tree cannot serve a leading wildcard (→ search.md) |
| Word/phrase search, ranking | Stored generated tsvector + GIN, queried via websearch_to_tsquery (→ search.md) |
| Job queue, retries, or an event outbox in Postgres | SELECT ... FOR UPDATE SKIP LOCKED — no external broker needed (→ queues.md) |
| Insert-or-update | INSERT ... ON CONFLICT ... DO UPDATE + RETURNING |
| First/top-N rows per group | DISTINCT ON for first-row; LATERAL + matching index for top-N at scale |
| Deep pagination | Keyset (WHERE (created_at, id) < (?, ?)), never a large OFFSET |
| DDL on a busy table | SET lock_timeout first, then the online variant (→ Safe DDL on Busy Tables) |
| Query hangs with no CPU burn | It is blocked: pg_blocking_pids() names the holder (→ locks.md) |
| Table grows on disk while row count is flat | Bloat: find the oldest transaction or replication slot first (→ vacuum-bloat.md) |
| An error code or message to decode | Error Codes below; full catalog in errors.md |
| Slow test suite, or a migration that needs rehearsing on real volume | Transaction-per-test and template databases; rehearse on a restored copy (→ test-databases.md) |
| Anything else surprising | EXPLAIN (ANALYZE, BUFFERS) before and after every change; if it is live and stuck, check pg_stat_activity for a blocker before touching anything |
Depth on demand, by phase:
- Diagnose —
slow-queries.mdplan reading and misestimates ·locks.mdblocked queries and deadlocks ·errors.mdSQLSTATE to cause ·monitoring.mdwhat to watch and alert on ·incidents.mddisk full, wraparound, runaway query, corruption - Design —
schema-design.mdtypes, keys, constraints ·indexing.mdindex types and when each wins ·partitioning.mdtables too big for one heap ·json.mdJSONB modeling and indexing ·search.mdtext, fuzzy, and vector search ·queues.mdjob queues, retries, transactional outbox - Change —
migrations.mdonline DDL, backfills, expand/contract ·bulk-load.mdloading millions of rows ·upgrades.mdmajor versions, OS/glibc, extensions ·functions-triggers.mdplpgsql, triggers, LISTEN/NOTIFY ·test-databases.mdfast test isolation, anonymized copies, CI, rehearsals - Operate —
connections.mdpooling and PgBouncer ·vacuum-bloat.mdautovacuum tuning and reclaim ·tuning.mdpostgresql.conf memory, WAL, planner ·backup-restore.mddumps, PITR, restore drills ·replication.mdreplicas, lag, failover ·security.mdroles, grants, RLS, pg_hba ·managed-postgres.mdRDS, Aurora, Cloud SQL, Neon, Supabase ·psql.mdclient workflow and scripting ·extensions.mdwhich extension solves this
Core Rules
- Tune from a plan, never from query text.
EXPLAIN (ANALYZE, BUFFERS); plain EXPLAIN prints estimates and the estimate is usually the bug. Check: estimated vs actual rows per node — off by more than 10x means a statistics problem, not a missing index. - Index every foreign key column you join or cascade on. Postgres indexes primary keys and unique constraints automatically, never FK columns. A parent
DELETEof 10k rows against an unindexed 50M-row child is 10k sequential scans. - Composite index order: equality columns first, then one range or sort column.
(tenant_id, created_at)answersWHERE tenant_id = ? ORDER BY created_at DESC LIMIT 20from the first 20 index entries;(created_at, tenant_id)walks every row in date order filtering as it goes, and gets slower as the tenant gets s
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
- _meta.json
- backup-restore.md
- bulk-load.md
- connections.md
- errors.md
- extensions.md
- functions-triggers.md
- incidents.md
- indexing.md
- json.md
- locks.md
- managed-postgres.md
- memory-template.md
- migrations.md
- monitoring.md
- partitioning.md
- psql.md
- queues.md
- replication.md
- schema-design.md
- search.md
- security.md
- setup.md
- slow-queries.md
- test-databases.md
- tuning.md
- upgrades.md
- vacuum-bloat.md
FAQ
What does the PostgreSQL skill do?
Tunes, designs, and operates PostgreSQL: slow queries, indexes, schemas, migrations, vacuum, locks, replication, backups. Use when writing Postgres SQL or psql, designing tables and indexes, reading an EXPLAIN plan, running DDL or a migration against a live table, or when a query suddenly got slow, a table keeps growing while rows stay flat, autovacuum cannot keep up, "too many clients already" appears, a replica lags, deadlocks and lock waits pile up, pg_wal fills the disk, or an upgrade, a restore, or a partitioning plan is on the table. Covers connection pooling and PgBouncer, full-text, trigram and pgvector search, JSONB, roles and row-level security, PITR, extensions, and managed Postgres (RDS, Aurora, Cloud SQL, Neon, Supabase). Not for cross-engine SQL portability or ORM-level modeling.
How do I install the PostgreSQL skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the PostgreSQL skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
doc-coauthoring
AnthropicGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
academy-guide
AnthropicStop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
internal-comms
AnthropicA set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
skill-development
AnthropicThis skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…