- Home
- Skills
- Data & Databases
- Postgres Migration Lock Check with Safe Rewrites
Works with the AI Agents you already use
Postgres Migration Lock Check with Safe Rewrites
A migration that runs in two seconds on staging can stop every write on a 40 GB production table, or queue all reads behind one ALTER that is waiting on a report query.
$12
Postgres Migration Lock Check with Safe Rewrites
Example session with this skill installed
Review the migrations in db/migrations before tonight's deploy. We're on Postgres 12 minimum, our runner only uses a transaction when the file says BEGIN, and orders (38M rows), invoices (9M rows), shipments (12M rows) and shipment_events (140M rows) take writes all day.
- Read your context and instructions
- Compiled the postgres migration lock
Migration safety review: templates/sample-migrations (001 to 005)
Assumptions: PostgreSQL 12+; runner wraps files in a transaction: no, only explicit BEGIN; busy tables: orders, invoices, shipments, shipment_events
Verdict: BLOCK
Lint: 14 high, 6 medium, 2 low, 1 suppressed (exit 1)
001, 002, 003: ship as written. They already use NOT VALID, VALIDATE in a later file, CONCURRENTLY, and timeouts.
Must fix (first 6 of 14)
1. 004:4 create-index-blocking: writes to invoices stop for the whole index build on 9M rows.
Fix: CREATE INDEX CONCURRENTLY in its own file with no BEGIN (004b).
2. 004:5 concurrently-in-transaction: Postgres rejects CONCURRENTLY inside BEGIN; the migration errors halfway.
3. 004:7 add-column-volatile-default: gen_random_uuid() rewrites all of invoices under ACCESS EXCLUSIVE.
Fix: ADD COLUMN public_id uuid; SET DEFAULT gen_random_uuid(); backfill old rows in batches;
keep NOT NULL with CHECK (public_id IS NOT NULL) NOT VALID in 004a, VALIDATE then SET NOT NULL in 004c.
4. 004:9 alter-type-rewrite: integer to bigint rewrites invoices and rebuilds every index on it.
Fix: new column amount_minor bigint, dual-write, backfill, switch reads, drop amount_cents later.
5. 004:10 set-not-null-scan: full scan of invoices with reads and writes blocked.
Fix: CHECK (customer_id IS NOT NULL) NOT VALID in 004a, VALIDATE then SET NOT NULL in 004c.
6. 004:12 add-fk-validating: writes to invoices and customers block during the scan.
Fix: NOT VALID in 004a, VALIDATE in 004c.
Should fix
- 004:4 missing-lock-timeout: add SET lock_timeout = '3s' at the top of every 004 file.
- 005:13 validate-same-transaction: remove BEGIN/COMMIT, move VALIDATE to the next file.
Safe rewrite
004a_invoices_expand.sql runner transaction on, lock_timeout 3s, statement_timeout 15s
004b_invoices_indexes.sql runner transaction OFF, lock_timeout 3s, statement_timeout 0
004c_invoices_validate.sql after backfill reports 0 rows left
004d_invoices_contract.sql after the release that stops reading memo and amount_cents
Rewrite lint: Findings: 0, Suppressed: 2, exit 0.
Deploy order
1. Release 1: 004a, then 004b.
2. App writes new and old columns; reads old.
3. Backfill job, 5,000 rows per batch by id, 200 ms pause, stop if replica lag > 10 s.
4. Release 2: 004c; app reads new columns.
5. Release 3: 004d.
Not checked
- Real row counts and current column types
- Triggers that fire during the backfill
- The body of next_batch_code() and any DO blocks
Connects securely to your tools. The creator never sees your data.
About this skill
A migration that runs in two seconds on staging can stop every write on a 40 GB production table, or queue all reads behind one ALTER that is waiting on a report query. Most of these outages come from a short list of statements, and each has a known safe pattern.
What you get
- A review method: establish Postgres version, table size, and whether your runner wraps files in a transaction, then judge each statement by the lock it takes.
- A rule table of 20+ checks: CREATE INDEX without CONCURRENTLY (and CONCURRENTLY inside a transaction), volatile ADD COLUMN defaults, type changes that rewrite, SET NOT NULL, FK and CHECK without NOT VALID, UNIQUE without USING INDEX, drops and renames of live columns, missing lock_timeout, unbatched backfills, VACUUM FULL, CLUSTER, REINDEX.
- Version facts stated plainly: constant defaults are metadata-only since PG 11, the CHECK-then-SET NOT NULL shortcut needs PG 12.
- A stdlib Python lint script: comment, string, and dollar-quote aware, file:line output, suppressions that require a reason, exit 1 on high findings.
- A worked example: five migrations, full output, the written review, and a four-file rewrite that lints clean.
What it does not do
It does not connect to your database, measure table sizes, check MySQL, or judge whether a backfill's logic is correct.
Requires Python 3.10+. No support for MySQL or SQLite. Lexer skips DDL inside dollar-quoted function bodies. Cannot verify table sizes or row counts without user input.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 3 days ago
- Software
Python 3.10+
Migrations as plain .sql files (render ORM migrations to SQL first)
Permissions
What's inside
Frequently Asked Questions
New in Data & Databases
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Data & Databases.