agentleFS
SkillMedevSkillMedev/database-performance/skills/migration-safety-checker/SKILL.md

migration-safety-checker

Skill0 starsChanged 39 days agoMITView on GitHub

Rewrite a schema migration into independently deployable, zero-downtime steps that never take a long-held blocking lock on a live table. Use when you are about to run an ALTER TABLE, CREATE INDEX, add a constraint, rename a column, or change a column type against a production database with live traffic. Do NOT use for greenfield table/column design with no rows yet - use database-schema instead; do NOT use for broad correctness/integrity review of a migration - use review-db instead.

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 2 are distinctive, and 5 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.

  • 5

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every step is independently deployable and individually reversible.

    line 26
  2. Very specific:

    No statement holds ACCESS EXCLUSIVE on a large write-hot table for longer than the lock_timeout.

    line 27
  3. Very specific:

    Do not ship a bare ALTER TABLE ...

    line 33
  4. Very specific:

    Do not run DDL without a lock_timeout.

    line 34
  5. Very specific:

    Do not wrap CREATE INDEX CONCURRENTLY in a transaction.

    line 35
  6. Distinctive:

    Do not add expand-contract ceremony to a small table (under roughly 10,000 rows with light write traffic) where a brief ACCESS EXCLUSIVE …

    line 36
  7. Very specific:

    Do not leave a failed concurrent index in place.

    line 37

≈ “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

Migration Safety Checker

Turn a migration that is correct in a test DB but takes a production-halting lock into a sequence of small, reversible, zero-downtime steps. The outage is rarely the DDL itself - it is the queue of fast queries that pile up behind a statement waiting for an ACCESS EXCLUSIVE lock.

Workflow

  1. Identify the lock each statement takes. In Postgres, ACCESS EXCLUSIVE (blocks reads AND writes) is taken by: changing a column type, adding a non-concurrent index, adding a column with a volatile default on old engines, and most constraint additions via direct ALTER. Flag every statement that takes a heavy lock on a large, write-hot table - as a rule of thumb, treat anything above a few million rows or with sustained write traffic as hot.
  2. Guard the lock acquisition. Set lock_timeout (2-5 seconds is a common production setting) before any DDL so a blocked statement aborts instead of stalling traffic behind it. A long ALTER waiting for its lock blocks every query that arrives after it, including fast SELECTs - that queue is the outage.
  3. Add columns nullable, then backfill, then constrain. Add the column NULLable with no volatile default (instant on modern Postgres). Backfill in batches of 1,000-10,000 rows (UPDATE ... WHERE id BETWEEN ...), committing each batch and pausing briefly between them, to avoid one giant transaction and table bloat. Add NOT NULL via a CHECK constraint with NOT VALID then VALIDATE CONSTRAINT - a weaker lock than direct SET NOT NULL.
  4. Build indexes and constraints without blocking. Use CREATE INDEX CONCURRENTLY (cannot run in a transaction - disable the migration's transaction wrapper, e.g. disable_ddl_transaction! in Rails). Add foreign keys and check constraints as NOT VALID first (fast, light lock), then VALIDATE CONSTRAINT in a separate step (scans without blocking writes). Drop with DROP INDEX CONCURRENTLY.
  5. Use expand-contract for renames and type changes. Never rename or retype a column in one deploy - running code still references the old shape. Expand: add the new column, dual-write from the app, backfill. Migrate reads to the new column. Contract: stop writing the old column, then drop it in a later deploy. Each step is independently deployable and reversible.
  6. Verify the actual lock before shipping. Confirm the rewritten statement's lock with a lock-impact migration linter (e.g. squawk) or by inspecting pg_locks while the statement runs on a staging clone - never assume from the SQL alone.

Deliverable

Produce the rewritten migration as an ordered list of independently deployable steps, each annotated with the exact statements to run, the lock it takes and why that is safe, the batch size for any backfill, and its rollback - plus the lock_timeout setting to apply before each DDL step.

Quality bar

  • Every step is independently deployable and individually reversible.
  • No statement holds ACCESS EXCLUSIVE on a large write-hot table for longer than the lock_timeout.
  • Backfills are batched and committed incrementally; no single unbounded UPDATE.
  • The output names the specific lock each statement takes and why each rewrite is safer.

Do NOT

  • Do not ship a bare ALTER TABLE ... TYPE, RENAME COLUMN, or SET NOT NULL against a hot table - expand-contract or NOT VALID/VALIDATE instead.
  • Do not run DDL without a lock_timeout; an unbounded wait is the failure mode, not the DDL.
  • Do not wrap CREATE INDEX CONCURRENTLY in a transaction; it will error.
  • Do not add expand-contract ceremony to a small table (under roughly 10,000 rows with light write traffic) where a brief ACCESS EXCLUSIVE lock is invisible - match rigor to table size and write traffic.
  • Do not leave a failed concurrent index in place; it leaves an INVALID index - drop and retry. Plain CREATE INDEX is fine inside a maintenance window.

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.