/tsh-stack-postgresql:migrating-postgresql-schemasfrom TSH Stack: PostgreSQL
Writes and reviews PostgreSQL schema migrations that run against a live database without stopping traffic. Covers lock analysis, lock_timeout, expand-and-contract patterns, NOT VALID constraints, CONCURRENT index builds, and batched backfills.
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 migration that is correct SQL can still take production down. What decides it is the lock each statement takes, how long it holds it, and what is queued behind it. This skill makes those three things explicit before a migration ships.
PostgreSQL 14 to 18; 14 reached end of life in November 2026. Run SELECT version();
or read the version pinned in the project (Docker image, Terraform, managed-service
config) before choosing a recipe: several differ by major version, and the references
mark a recipe with the first version it works on (12+, 18+). Below 14, stop and
say the project is on an unsupported version; continue only when asked, and then
only with recipes whose mark the version satisfies.
PostgreSQL only. If the project's database is MySQL, MariaDB, SQL Server or another engine, say this skill does not apply and stop: lock modes and online DDL differ by engine.
Read the project's migration tool and its conventions first: where migrations live, how they run in CI and at deploy, and whether each one runs in a transaction. The ORM or framework skill installed for the project owns the tool's mechanics; this skill owns what the generated SQL does to a live database. A project rule (for example "every migration is reviewed by the DBA") outranks this skill.
This skill does not provision databases, tune server parameters, or run migrations against a shared or production environment.
| Severity | Rule |
|---|---|
| MUST | Name the lock every statement takes and what it blocks, from lock-levels.md, before calling a migration safe. |
| MUST | Set lock_timeout (a few seconds) on any migration that takes ACCESS EXCLUSIVE or SHARE ROW EXCLUSIVE on a table with traffic, so it fails and retries instead of blocking every query queued behind it. Not on CREATE INDEX CONCURRENTLY or VALIDATE CONSTRAINT, whose locks do not block traffic. |
| MUST | Keep every ACCESS EXCLUSIVE statement in a transaction that commits right after it. Locks are held until commit, so a scan, backfill or index build later in the same transaction turns a brief lock into an outage. |
| MUST | Build indexes on populated tables with CREATE INDEX CONCURRENTLY, outside a transaction, one statement per migration, and check for an INVALID index after a failure. |
| MUST | Add foreign keys and CHECK constraints to populated tables as NOT VALID, then VALIDATE CONSTRAINT in a separate step. |
| MUST | Rename or retype a column used by running code through expand-and-contract across deploys, never in one statement. |
| MUST | Backfill large tables in bounded batches outside the schema migration, never one UPDATE over the whole table. |
| MUST | Ship the code that stops reading a column before the migration that drops it. |
| MUST | When the migration is written with an ORM or framework that has its own skill available in this session (a NestJS, Symfony or Python stack skill, for example), load that skill too, before writing the migration file. This skill owns what the SQL does to the database; that one owns how the tool writes and runs it. Name none that is absent. |
| NEVER | Run a migration against production, staging or any shared database yourself. Hand over the SQL and the order of steps. |
| NEVER | Let a deploy retry a failed non-transactional migration automatically. The tool's state (and any invalid index) needs resolving first. |
| NEVER | Call a migration safe on a table whose size and traffic you do not know. Ask, or state the assumption in the review. |
| Reference | Load when | Covers |
|---|---|---|
| Lock levels | Any migration on a table with data, every time | Which lock each DDL takes, what it blocks, rewrites, the lock queue, lock_timeout and retries |
| Zero-downtime recipes | Adding a constraint or index, renaming, retyping or dropping a column | Step-by-step expand-and-contract recipes, version differences |
| Backfilling data | Rows must be updated or copied as part of the change | Batching, ordering, vacuum, replication lag, resumability |
| Migration tools | The migration is written with an ORM or migration runner | Turning off the per-migration transaction, what generators emit, review points per tool |
Read lock-levels.md before judging any migration, and zero-downtime-recipes.md before writing one that touches an existing column.
pg_total_relation_size), its write rate, and whether a long transaction
or replica lag is normal there. If you cannot read them, ask; a 1,000-row table
and a 200-million-row table need different migrations.lock_timeout and a retry for the statements that need it.For each statement: the lock it takes, the risk on this table, and the safe
replacement. Lead with anything that takes ACCESS EXCLUSIVE for longer than a
moment on a table with traffic. Say plainly when the migration is safe as written.
What SKILL.md sets; muted values are Claude Code's defaults
Trigger on: adding a column, index, foreign key, unique or NOT NULL constraint to an existing table, renaming a column or table, changing a column type, dropping a column, backfilling or rewriting rows in a large table, a migration that hung, timed out or caused an outage, reviewing a TypeORM, Prisma, Doctrine, Alembic, Django, Knex, Laravel, Rails or Flyway migration that targets PostgreSQL, or planning a schema change that must ship with zero downtime. How to shape the schema in the first place is designing-postgresql-schemas; a slow query is optimizing-postgresql-queries.
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./optimizing-postgresql-queriesFinds and fixes slow PostgreSQL queries from evidence: pg_stat_statements to find what actually costs time, EXPLAIN (ANALYZE, BUFFERS) read node by node, estimate-versus-actual row counts and stale statistics, choosing the index that fits the query (composite column order, partial, covering, GIN, BRIN), and the query shapes that defeat an index: N+1 loops, OFFSET pagination, functions on indexed columns, OR across columns, implicit casts. Use when an endpoint or job is slow because of the database, a query plan needs explaining, or someone proposes adding an index.Other plugins' skills for the same job