All skills
timescale avatar

/postgres-database-migration

@64c743b
by Tiger Datatimescale/pg-aiguide1.9k stars
111

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 type without downtime - Add or drop indexes, constraints, or foreign keys on large tables - Understand which ALTER TABLE operations lock the table - Roll back a failed migration - Plan a zero-downtime migration strategy - Fork a database to test a migration safely **Keywords:** migration, schema change, ALTER TABLE, add column, drop column, rename column, change type, zero downtime, lock, AccessExclusiveLock, concurrent index, forking, rollback, backfill, deploy Covers: lock-level reference for every common DDL operation, safe migration patterns, fork-based testing, zero-downtime column changes, index creation, constraint addition, backfill strategies, pre/post-migration validation, and rollback planning.

Use this Skill: https://skilld.dev/gh/timescale/pg-aiguide/postgres-database-migration

This session only. Nothing lands on disk.

referencescomplete-example.md

≈523 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Complete Migration Example

End-to-end example: adding a status column with a default, a NOT NULL constraint, and an index to a large table.

Step 1: Plan and Document

Migration: Add order_status column to orders table
- Table: orders (~5M rows, 2 GB)
- Change: Add TEXT column with default 'pending', NOT NULL, partial index
- Risk: Low (no rewrite needed on PG 11+)
- Rollback: DROP COLUMN order_status
- Estimated duration: < 1 second for DDL, ~5 minutes for index build

Step 2: Test on a Fork

Fork your database using your provider's fork feature (Neon, or dump/restore).

Step 3: Run on Fork

-- Fast: non-volatile default, no rewrite (PG 11+)
ALTER TABLE orders ADD COLUMN order_status TEXT NOT NULL DEFAULT 'pending';

-- Non-blocking index
CREATE INDEX CONCURRENTLY idx_orders_active
    ON orders (order_status, created_at DESC)
    WHERE order_status NOT IN ('completed', 'cancelled');

Step 4: Validate on Fork

-- Column exists with correct type
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_name = 'orders' AND column_name = 'order_status';

-- All existing rows have the default
SELECT order_status, COUNT(*) FROM orders GROUP BY order_status;

-- Index is valid and used
EXPLAIN ANALYZE
SELECT * FROM orders WHERE order_status = 'pending' ORDER BY created_at DESC LIMIT 10;

Step 5: Apply to Production

-- Set timeouts to avoid blocking traffic
SET lock_timeout = '5s';
SET statement_timeout = '30s';

ALTER TABLE orders ADD COLUMN order_status TEXT NOT NULL DEFAULT 'pending';

RESET lock_timeout;
RESET statement_timeout;

-- Index creation is non-blocking, safe to run anytime
CREATE INDEX CONCURRENTLY idx_orders_active
    ON orders (order_status, created_at DESC)
    WHERE order_status NOT IN ('completed', 'cancelled');

-- Verify index is valid (not left in INVALID state)
SELECT indexrelid::regclass, indisvalid
FROM pg_index
WHERE indrelid = 'orders'::regclass AND NOT indisvalid;

Step 6: Clean Up

Delete the test fork if you created one.

Source: SKILL.md on GitHub

No alerts27d3 checks · Risk SAFE
  • Gen Agent Trust Hub27d

    This skill provides educational guidance and SQL templates for performing safe PostgreSQL schema migrations. It includes best practices for avoiding table locks, managing long-running queries, and using database forks for testing. No security risks were identified.

  • Socket27d

    No alerts

  • Snyk27d

    Risk: LOW · No issues

Signed by skilld at 64c743b. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 6 days ago.

Activeupdated 4 weeks ago

README badge

README badge for timescale/pg-aiguide/postgres-database-migration