CLI Reference

This page reflects pgfence 0.9.0. Run pgfence <command> --help against your installed version when scripting a release gate.

Commands

CommandPurpose
analyze <files...>Statically analyze migrations for lock risk, policy violations, safe rewrites, and coverage gaps.
trace <files...>Compare static predictions with locks observed in a disposable Docker Postgres container.
explain [sql...]Explain one SQL statement from an argument or stdin. Supports CLI and JSON output.
snapshotRead schema metadata from a database and write a local snapshot for later analysis.
initInstall a local git hook or scaffold the Prisma GitHub Actions workflow.
telemetry [action]Show, enable, disable, or reset anonymous usage telemetry.
lspStart the bundled Language Server Protocol server over stdio.

Analyze

bash
pgfence analyze [options] <files...>
OptionValues and behavior
--formatsql, typeorm, prisma, knex, drizzle, sequelize, kysely, or auto. Default: auto.
--outputcli, json, github, sarif, or gitlab. Default: cli.
--ciFail when the configured risk gate or a policy error blocks the run.
--max-riskHighest allowed risk in CI. Default: high.
--unknownwarn or block for unanalyzable SQL. Default: warn.
--min-pg-versionMinimum PostgreSQL version assumed by version-sensitive checks. Default: 14.
--db-urlRead live table statistics for size-aware risk scoring.
--stats-fileUse a local pgfence-stats.json instead of a direct database connection.
--snapshotUse schema snapshot metadata for type and index ownership analysis.
--pluginLoad one or more custom rule modules.
--disable-rulesDisable specific rule ids.
--enable-rulesRun only the listed rule ids.
--no-lock-timeoutDisable the lock_timeout requirement.
--no-statement-timeoutDisable the statement_timeout requirement.
--max-lock-timeoutMaximum allowed lock timeout in milliseconds. Default: 5000.
--max-statement-timeoutMaximum allowed statement timeout in milliseconds. Default: 600000.
--config <path>Use this .toml or .json config file instead of discovering one. A missing file is an error. See Configuration.
--baseline <path>Suppress findings recorded in this baseline file, report new ones, and list stale entries. Overrides the baseline config key. Cannot be combined with --write-baseline.
--write-baseline <path>Record every current finding to this baseline file and exit 0, without CI gating. Cannot be combined with --changed-since, because a baseline written from a subset of files would make the rest look new on the next full run.
--changed-since <ref>Analyze only the given files that changed against a git ref (the merge base with HEAD, plus the working tree and untracked files). A file git cannot vouch for is analyzed, never skipped. Needs a git repository and, in CI, enough history to reach the ref.
--assume-in-transactionAssume the migration runner wraps each migration in a transaction. Same as assume-in-transaction = true in config.
--prisma-version <version>Prisma version of the project, such as 7.3.0 or ^7.3.0. Prisma before 7.4 runs each migration.sql in one transaction. When omitted, pgfence reads the nearest package.json or the installed package, and assumes no transaction when the version is unknown.
--no-cloud-hintHide the hosted-check hint on a blocked CI run without changing the report or exit code.

Adoption options

--write-baseline, --baseline and --changed-since exist so a team can adopt pgfence on a codebase that already has findings. Whatever they hide is counted in the report, never dropped silently, and a statement pgfence could not analyze stays UNKNOWN in the coverage line. If every given file is excluded by config exclude patterns, or none changed since the ref, pgfence says so on stderr and exits 0.

bash
# Record today's findings once, from a full run
pgfence analyze --write-baseline pgfence-baseline.json migrations/*.sql

# Fail only on findings that are not in the baseline
pgfence analyze --ci --baseline pgfence-baseline.json migrations/*.sql

# Analyze only migrations changed since the base branch
pgfence analyze --ci --changed-since origin/main migrations/*.sql

See Configuration for the baseline file and CI/CD for a workflow.

Fix options

--fix edits raw SQL files in place, but only for the documented mechanical allowlist. It can add concurrent index syntax and missing timeout settings. It refuses unsafe guesses and refuses concurrent index changes inside an explicit transaction.

--fix --split also creates sibling SQL files for supported multi-step recipes. It never edits the original migration for those recipes, and generated backfill templates remain commented out.

bash
pgfence analyze --fix migrations/*.sql
pgfence analyze --fix --split migrations/*.sql

Trace

trace accepts the analysis, policy, output, plugin, snapshot, --config, --prisma-version and CI options above, except for database statistics, fix options, --baseline, --write-baseline, --changed-since and --assume-in-transaction. It applies the config's per-rule severity overrides, but not exclude or baseline, and says so in its output. It also accepts:

  • --pg-version <version>: Docker PostgreSQL version, default 17.
  • --docker-image <image>: custom image that overrides --pg-version.

Other command options

bash
# Explain one statement
pgfence explain --min-pg-version 14 --output json "ALTER TABLE users DROP COLUMN legacy"

# Generate a schema snapshot
pgfence snapshot --db-url postgres://readonly@replica:5432/app --output pgfence-snapshot.json

# Install hooks or scaffold a Prisma workflow
pgfence init
pgfence init --prisma-github-action

# Inspect or change telemetry
pgfence telemetry status
pgfence telemetry enable
pgfence telemetry disable
pgfence telemetry reset

Exit codes

CodeMeaning
0The command completed and the configured gate passed.
1A CI risk threshold, policy error, blocked unknown statement, trace mismatch, or command error failed the run.
2The run analyzed zero SQL statements, or a bin-like launch could not be confirmed safely.

A file with no statements is reported as [NO STATEMENTS]. If the entire run contains no statements, analyze and trace exit 2. Dynamic SQL that was found but could not be analyzed is reported separately as [UNANALYZABLE].

Configuration files and environment variables are documented in Configuration. Reporter schemas and coverage fields are documented in Output Formats.