writing-safe-migrations

writing-safe-migrations is a skill for Claude Code from pumarogie/claude-postgres-skills. It costs 62 tokens per session (1,571 once invoked), scanned A, original, MIT.

A guide for changing PostgreSQL database structures safely, especially when tables are large or serving live traffic. PostgreSQL is a database system; a migration is a change to its tables, columns, indexes, or constraints.

In plain words
What is it for?
Use it when adding indexes or columns, changing types, adding constraints, backfilling many rows, or planning a rollout with little or no downtime.
Why use it?
It helps prevent migrations from blocking reads and writes or creating a queue of database operations waiting for a lock.

Skill for Claude Code

Written for Claude Code: shipped in a Claude Code plugin.

Part of the database-skill plugin — 6 skills shipped together

Good fit Use it when adding indexes or columns, changing types, adding constraints, backfilling many rows, or planning a rollout with little or no downtime.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/pumarogie/claude-postgres-skills/writing-safe-migrations
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add pumarogie/claude-postgres-skills --skill writing-safe-migrations
Clone the repo
git clone --depth 1 https://github.com/pumarogie/claude-postgres-skills

Made for: Claude Code.

Or install database-skill, the plugin that ships this one along with the rest of its 6 skills.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for writing-safe-migrations

README.md
[![agentmods](https://agentmods.dev/badge/skills/pumarogie/claude-postgres-skills/writing-safe-migrations.svg)](https://agentmods.dev/skills/pumarogie/claude-postgres-skills/writing-safe-migrations)
Your own site
<a href="https://agentmods.dev/skills/pumarogie/claude-postgres-skills/writing-safe-migrations"><img src="https://agentmods.dev/badge/skills/pumarogie/claude-postgres-skills/writing-safe-migrations.svg" alt="Measured on agentmods" height="20"></a>
Per session 62 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,571 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00062 $0.01571
Opus 5 $0.00031 $0.00785
Sonnet 5 $0.00012 $0.00314
Haiku 4.5 $0.00006 $0.00157

Measured 8d ago against content hash 8379f711c69e, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-08, from the pricing page.

Security

Grade A, and why

writing-safe-migrations scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 8d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/writing-safe-migrations/SKILL.md · 126 lines

How it starts

The opening of the file, as written. The whole thing — 126 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Writing Safe Migrations

Overview

Two questions before any migration runs against a live table:

  1. Does this rewrite the table? A rewrite holds ACCESS EXCLUSIVE for the whole rewrite — every read and write blocks.
  2. Can it wait for its lock without taking the table down? A statement waiting for ACCESS EXCLUSIVE queues ahead of every query that arrives after it. One long-running SELECT plus one unguarded ALTER TABLE stalls the entire table, even for statements the migration itself would never have blocked.

Question 2 causes more outages than question 1, and lock_timeout is the whole fix.

When to Use

  • Adding an index to an existing large table.
  • ALTER TABLE, adding or dropping columns, changing types, adding constraints.
  • Backfilling a column across many rows.
  • Any migration on a table with meaningful traffic.

Always set a lock timeout

Never issue DDL against a live table without bounding the wait:

SET lock_timeout = '5s';        -- fail instead of building a lock queue
SET statement_timeout = '0';    -- but let a long index build finish
ALTER TABLE tasks ADD COLUMN priority int;

If the lock isn't acquired in 5s the statement errors — retry it later. That is the correct outcome: a failed migration is recoverable, a stalled table is an incident.

lock_timeout only bounds acquiring a lock, not holding one. It will not save you from a rewrite that takes ten minutes once it starts — check the lock table for that.

Find what's blocking you:

SELECT pid, pg_blocking_pids(pid), wait_event_type, left(query, 80) AS query
FROM pg_stat_activity
WHERE cardinality(pg_blocking_pids(pid)) > 0;

Quick Reference

Operation Danger Safe way
CREATE INDEX Blocks writes for the whole build CREATE INDEX CONCURRENTLY, outside a transaction
Add check / FK constraint Validation scan blocks writes NOT VALID, then VALIDATE CONSTRAINT
SET NOT NULL Full scan blocks reads and writes Add an equivalent CHECK (col IS NOT NULL) NOT VALID, validate, then SET NOT NULL
ALTER COLUMN TYPE Usually a full rewrite New column + backfill + swap (expand-and-contract)
Backfill in one UPDATE Long transaction, bloat, blocked autovacuum Batch in separate transactions
Dropping columns Hard to roll back Keep migrations additive; expand-and-contract

Read the full file on GitHub · 126 lines

Files

What ships with it

1 file beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 8d ago First seen · 126 lines · 62 tokens per session scan A 8379f711c69e

Subscribe to this mod's changes

writing-safe-migrations is a skill published in the GitHub repository pumarogie/claude-postgres-skills (2 stars, last pushed 1mo ago), licensed MIT. It adds 62 tokens to every session and 1,571 once invoked, about $0.0003 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

database-optimizer

Optimizes database queries and improves performance across PostgreSQL and MySQL systems. Use when investigating slow queries, analyzing execution plans, or optimizing database performance. Invoke for index design, query rewrites, configuration tuning, partitioning strategies, lock contention resolution.

Jeffallan/claude-skills · 54 tokens

postgres-pro

Use when optimizing PostgreSQL queries, configuring replication, or implementing advanced database features. Invoke for EXPLAIN analysis, JSONB operations, extension usage, VACUUM tuning, performance monitoring.

Jeffallan/claude-skills · 41 tokens

postgres-database-migration

Use this skill for planning, testing, and safely executing PostgreSQL schema migrations — especially when working with production data or shared databases. Trigger when user asks to: Test a schema migration before applying it to production Add, remove, or rename columns safely on a live table Change a column's data…

timescale/pg-aiguide · 220 tokens

setup-timescaledb-hypertables

Use this skill when creating database schemas or tables for Timescale, TimescaleDB, TigerData, or Tiger Cloud, especially for time-series, IoT, metrics, events, or log data. Use this to improve the performance of any insert-heavy table. Trigger when user asks to: Create or design SQL schemas/tables AND…

timescale/pg-aiguide · 219 tokens

design-postgis-tables

Comprehensive PostGIS spatial table design reference covering geometry types, coordinate systems, spatial indexing, and performance patterns for location-based applications.

timescale/pg-aiguide · 31 tokens

migrate-postgres-tables-to-hypertables

Use this skill to migrate identified PostgreSQL tables to Timescale/TimescaleDB hypertables with optimal configuration and validation. Trigger when user asks to: Migrate or convert PostgreSQL tables to hypertables Execute hypertable migration with minimal downtime Plan blue-green migration for large tables Validate…

timescale/pg-aiguide · 181 tokens