# pgfence pgfence is a Postgres migration safety CLI written in TypeScript. It analyzes SQL migration files (and migrations emitted by TypeORM, Prisma, Knex, Drizzle, and Sequelize) before they hit production, and reports the exact Postgres lock mode each DDL statement acquires, the risk level (optionally adjusted by real table size), and a safe expand/contract rewrite recipe when a footgun is detected. It is the first Node-native tool in a space otherwise covered by Ruby (strong_migrations), Rust (Eugene, Squawk), and Go (pgroll). ## What it does - Maps every DDL statement to its Postgres lock mode (ACCESS SHARE through ACCESS EXCLUSIVE) and what that lock blocks (reads, writes, or both). - Assigns a risk level (LOW, MEDIUM, HIGH, CRITICAL) and upgrades it when supplied with table stats from `pg_stat_user_tables`. - Emits safe rewrite recipes: the exact expand, backfill, and contract SQL to run instead of the dangerous one-shot statement. - Extracts SQL from raw `.sql` files and from TypeORM, Prisma, Knex, Drizzle, and Sequelize migration sources. - Outputs as a terminal table, JSON, SARIF, GitLab Code Quality, or GitHub PR comment markdown. ## Quickstart ```bash npx @flvmnt/pgfence analyze migrations/*.sql ``` Global install: ```bash npm install -g @flvmnt/pgfence pgfence analyze migrations/*.sql ``` CI mode (exit 1 on HIGH+ risk): ```bash pgfence analyze --ci --max-risk medium migrations/*.sql ``` Explain a single statement: ```bash pgfence explain "ALTER TABLE users ADD COLUMN last_seen_at TIMESTAMPTZ NOT NULL DEFAULT now()" ``` ## Key concepts - **Lock mode**: every Postgres DDL acquires one of eight named locks. ACCESS EXCLUSIVE is the worst; it blocks every reader and writer on the table. - **Risk level**: pgfence's verdict on whether the statement is safe to ship as written. Adjusted upward when the target table is large. - **Safe rewrite (expand/contract)**: split a dangerous one-shot DDL into nullable expand, out-of-band batched backfill, and a contract step that uses `NOT VALID` plus `VALIDATE CONSTRAINT` to avoid long ACCESS EXCLUSIVE locks. - **Policy checks**: every migration should `SET lock_timeout`, `SET statement_timeout`, `SET idle_in_transaction_session_timeout`, and `SET application_name`. pgfence warns when these are missing. - **Trust contract**: pgfence never silently passes dangerous SQL. Statements it cannot statically analyze surface as UNKNOWN with a coverage line in every report. ## Authoritative guides - Lock mode reference: https://pgfence.com/lock-modes/ - Postgres lock mode cheat sheet: https://pgfence.com/blog/postgres-lock-mode-cheat-sheet/ - Zero-downtime expand and contract migrations: https://pgfence.com/blog/expand-contract-zero-downtime-migrations/ - Diagnose a stuck ALTER TABLE: https://pgfence.com/blog/postgres-alter-table-hangs/ - SET NOT NULL without a long scan: https://pgfence.com/blog/set-not-null-without-a-table-scan/ - REINDEX and DROP INDEX locks: https://pgfence.com/blog/reindex-and-drop-index-locks/ - TRUNCATE CASCADE blast radius: https://pgfence.com/blog/truncate-cascade-postgres-blast-radius/ ## Source - Repository: https://github.com/flvmnt/pgfence - Website: https://pgfence.com - npm: https://www.npmjs.com/package/@flvmnt/pgfence - Extended rules and recipes: https://pgfence.com/llms-full.txt ## License MIT