Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIDeepSeekDeepSeekMuseMuse+22 more

    PostgreSQL Migration Generator

    1

    An enterprise-grade AI agent skill that enables autonomous, safe, and zero-downtime PostgreSQL database migrations.

    $5

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned
    PostgreSQL Migration Generator

    PostgreSQL Migration Generator

    Example session with this skill installed

    "I am running PostgreSQL 14. I need to add a new tenant_id column to my users table (which has 25 million rows). The new column needs to have a default value of 'system', it must be NOT NULL, and it needs a new index. Please generate the migration script."

    • Read your context and instructions
    • Compiled the postgresql migration generator

    Here's a zero-downtime migration for adding tenant_id to a 25M-row users table on PostgreSQL 14.

    Why this approach: A plain ADD COLUMN ... DEFAULT 'system' NOT NULL would take an AccessExclusiveLock and, pre-PG11, rewrite the whole table. On PG11+ the default itself is metadata-only and fast — but forcing NOT NULL in the same statement still requires a full-table validation scan while holding the lock. So we split it: add the column with its default (cheap), enforce NOT NULL via a NOT VALID check + separate VALIDATE (cheap lock, expensive work done without blocking writes), then flip on NOT NULL itself (fast in PG12+, since the validated constraint lets it skip re-scanning), and finally build the index CONCURRENTLY outside any transaction.

    -- Migration: add_tenant_id_to_users
    -- Generated by PostgreSQL Migration Generator
    
    -- 1. Session safety limits to prevent database lockups
    SET statement_timeout = '10s';
    SET lock_timeout = '2s';
    
    -- 2. Schema changes
    
    -- 2a. Add the column with a constant default.
    --     PG11+ treats this as metadata-only — no table rewrite, near-instant
    --     even at 25M rows.
    ALTER TABLE users
      ADD COLUMN IF NOT EXISTS tenant_id text DEFAULT 'system';
    
    -- 2b. Enforce NOT NULL safely: add an unvalidated CHECK first.
    --     This only takes a brief metadata lock.
    ALTER TABLE users
      ADD CONSTRAINT users_tenant_id_not_null
      CHECK (tenant_id IS NOT NULL) NOT VALID;
    
    -- Note: Concurrent index creation must happen outside a transaction
    COMMIT;
    
    -- 2c. Validate the constraint. This scans the table but only takes a
    --     SHARE UPDATE EXCLUSIVE lock, so reads/writes continue normally.
    --     On 25M rows this can take a while — if lock_timeout above causes
    --     it to fail under load, rerun during lower traffic or retry with backoff.
    ALTER TABLE users
      VALIDATE CONSTRAINT users_tenant_id_not_null;
    
    -- 2d. Now SET NOT NULL. In PG12+, since a validated CHECK already
    --     guarantees no NULLs exist, this skips the full-table re-scan
    --     and is effectively metadata-only.
    ALTER TABLE users
      ALTER COLUMN tenant_id SET NOT NULL;
    
    -- 2e. The CHECK constraint is now redundant with the column's NOT NULL
    --     — drop it to keep the schema clean (cheap, metadata-only).
    ALTER TABLE users
      DROP CONSTRAINT users_tenant_id_not_null;
    
    COMMIT;
    
    -- 3. Concurrent operations
    
    -- 3a. Build the index without blocking reads/writes on the table.
    --     Must run outside a transaction block (hence the COMMIT above).
    CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_tenant_id
      ON users (tenant_id);
    

    A few things worth knowing before you run this:

    lock_timeout = '2s' means any step that can't grab its lock quickly will error out and roll back cleanly rather than blocking writers — safe to retry, but expect possible failures under heavy write load, especially on step 2c/VALIDATE.

    CREATE INDEX CONCURRENTLY can occasionally fail partway through and leave behind an invalid index. If that happens, check pg_indexes / pg_index.indisvalid, drop the invalid one, and rerun the CREATE INDEX CONCURRENTLY statement — it's idempotent thanks to IF NOT EXISTS.

    • Run each numbered section as its own migration step if your framework wraps each file in its own implicit transaction — the COMMIT; markers are there specifically so CREATE INDEX CONCURRENTLY isn't caught inside one.
    • If you hit ERROR: deadlock detected while other migrations are running concurrently, that usually means multiple migrations are touching the same tables in different orders — best avoided by running schema migrations serially.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    This skill equips multi-agent systems with Principal DBA capabilities to safely generate, validate, and execute PostgreSQL DDL scripts. It features strict constraint handling, automatic lock timeout mitigation, and self-healing error interpretation to ensure highly concurrent production databases remain stable during structural changes.

    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.

    Requires manual data backfilling for complex column type swaps. Does not support non-Postgres dialects. User must verify specific version compatibility for metadata-only defaults.

    How to use

    1. 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. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 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 2 months ago

    Permissions

    No special permissions declared or detected

    Listed2 months ago

    What's inside

    Frequently Asked Questions

    This skill automates the generation of safe, production-ready DDL scripts that avoid common pitfalls like long-held table locks or blocking transactions. It solves the risk of database outages during migrations by automatically applying lock timeouts and retry logic to every statement.

    The PostgreSQL Migration Generator is designed for any multi-agent framework (such as AutoGen, LangGraph, or CrewAI) that supports tool-calling and SQL interaction. It functions as a specialized 'DBA Agent' that can be integrated into your existing development or CI/CD pipelines.

    You receive the agent's logic configuration, a specialized toolset for schema inspection and script validation, and a comprehensive integration guide. The package includes pre-configured safety parameters to prevent destructive actions without explicit confirmation.

    Setup involves importing the skill into your agentic environment and providing read-only access for schema discovery or read-write access for execution. It is designed to work with PostgreSQL versions 12 through 16, including managed services like AWS RDS and Google Cloud SQL.

    Yes, the skill is capable of interpreting error logs from failed migration attempts and automatically generating corrective scripts. It analyzes the specific PostgreSQL error code to determine if the issue was a lock contention, syntax error, or constraint violation.

    New in Data & Databases