Lock & Safety Checks

pgfence maps every DDL statement to a Postgres lock mode and determines what it blocks.

Lock Mode Reference

Lock ModeBlocksSeverity
ACCESS SHARENothingSafe
ROW SHAREROW EXCLUSIVE+Low
ROW EXCLUSIVESHARE+Low
SHARE UPDATE EXCLUSIVESHARE UPDATE EXCLUSIVE+Medium
SHAREROW EXCLUSIVE+ (blocks writes)Medium
SHARE ROW EXCLUSIVEROW EXCLUSIVE+High
EXCLUSIVEROW SHARE+High
ACCESS EXCLUSIVEEverything (reads + writes)Critical

DDL Checks

#PatternLockRiskSafe Alternative
1ADD COLUMN ... NOT NULL (no DEFAULT)ACCESS EXCLUSIVEHIGHAdd nullable, backfill, SET NOT NULL
2ADD COLUMN ... DEFAULT <volatile>ACCESS EXCLUSIVEHIGHAdd without default, backfill in batches
3ADD COLUMN ... DEFAULT <constant or stable> (PG11+)ACCESS EXCLUSIVE (instant)LOWSafe on PG11+ (metadata-only)
4ADD COLUMN ... GENERATED STOREDACCESS EXCLUSIVEHIGHRegular column + trigger + backfill
5CREATE INDEX (non-concurrent)SHAREMEDIUMCREATE INDEX CONCURRENTLY
6DROP INDEX (non-concurrent)ACCESS EXCLUSIVEMEDIUMDROP INDEX CONCURRENTLY
7aALTER COLUMN TYPE (text / varchar widening)ACCESS EXCLUSIVELOWMetadata-only, no table rewrite
7bALTER COLUMN TYPE varchar(N)ACCESS EXCLUSIVEMEDIUMSafe if widening; verify with schema
7cALTER COLUMN TYPE (cross-family)ACCESS EXCLUSIVEHIGHExpand/contract pattern
8ALTER COLUMN SET NOT NULLACCESS EXCLUSIVEMEDIUMCHECK NOT VALID + VALIDATE
9ADD FOREIGN KEY (no NOT VALID)SHARE ROW EXCLUSIVEHIGHNOT VALID + VALIDATE CONSTRAINT
10ADD CHECK (no NOT VALID)ACCESS EXCLUSIVEMEDIUMNOT VALID + VALIDATE CONSTRAINT
11aADD UNIQUEACCESS EXCLUSIVEHIGHConcurrent index + USING INDEX
11bADD UNIQUE USING INDEXACCESS EXCLUSIVELOWInstant, attaches pre-built index
12ADD EXCLUDEACCESS EXCLUSIVEHIGHNo concurrent alternative; use lock_timeout
12bCREATE TABLE ... EXCLUDESHARE ROW EXCLUSIVELOWNew table only; safe to review as table creation
13DROP TABLEACCESS EXCLUSIVECRITICALSeparate release
14DROP COLUMNACCESS EXCLUSIVEHIGHRemove app references first
15TRUNCATEACCESS EXCLUSIVECRITICALBatched DELETE
16TRUNCATE ... CASCADEACCESS EXCLUSIVECRITICALExplicit per-table truncation
17RENAME COLUMNACCESS EXCLUSIVELOWInstant on PG14+
18RENAME TABLEACCESS EXCLUSIVEHIGHRename + view for backwards compat
19VACUUM FULLACCESS EXCLUSIVEHIGHUse pg_repack
20aALTER TYPE ... ADD VALUE (PG < 12)EXCLUSIVEMEDIUMCannot run inside a transaction block; run in its own migration outside one, or upgrade to PG12+
20bALTER TYPE ... ADD VALUE (PG12+)EXCLUSIVELOWSafe; instant but can't run inside tx
21aATTACH PARTITION (PG < 12)ACCESS EXCLUSIVEHIGHCreate matching CHECK constraint first
21bATTACH PARTITION (PG12+)SHARE UPDATE EXCLUSIVE on parent, ACCESS EXCLUSIVE on partitionHIGHCreate matching CHECK constraint first
22aDETACH PARTITION (non-concurrent)ACCESS EXCLUSIVEHIGHDETACH CONCURRENTLY (PG14+)
22bDETACH PARTITION CONCURRENTLYSHARE UPDATE EXCLUSIVELOWSafe on PG14+
23aREFRESH MATERIALIZED VIEWACCESS EXCLUSIVEHIGHREFRESH ... CONCURRENTLY
23bREFRESH ... CONCURRENTLYEXCLUSIVELOWBlocks writes but allows reads; requires unique index
24aREINDEX TABLESHAREHIGHREINDEX CONCURRENTLY (PG12+)
24bREINDEX INDEXACCESS EXCLUSIVEHIGHREINDEX CONCURRENTLY (PG12+)
24cREINDEX SCHEMA/DATABASEACCESS EXCLUSIVECRITICALREINDEX CONCURRENTLY (PG12+)
25CREATE TRIGGERSHARE ROW EXCLUSIVEMEDIUMUse lock_timeout to bound wait
26DROP TRIGGERACCESS EXCLUSIVEMEDIUMUse lock_timeout to bound wait
27ENABLE/DISABLE TRIGGERSHARE ROW EXCLUSIVELOWBlocks concurrent DDL only
28ADD PRIMARY KEY (no pre-built index)ACCESS EXCLUSIVEHIGHCreate index concurrently, then USING INDEX
29ADD PRIMARY KEY USING INDEXACCESS EXCLUSIVELOWInstant, attaches pre-built index
30DROP SCHEMAACCESS EXCLUSIVECRITICALRemove app references, separate release
31DROP SCHEMA CASCADEACCESS EXCLUSIVECRITICALIrreversible data loss, explicit per-table drops
32DELETE without WHEREROW EXCLUSIVEHIGHBatched DELETE with FOR UPDATE SKIP LOCKED
33VALIDATE CONSTRAINT (standalone)SHARE UPDATE EXCLUSIVELOWNon-blocking scan, safe standalone
34REFRESH ... WITH NO DATAACCESS EXCLUSIVEMEDIUMBrief lock (truncate only), no data scan
35DROP DATABASEACCESS EXCLUSIVECRITICALIrreversible, destroys entire database and all data
36ALTER DOMAIN ADD CONSTRAINTSHAREHIGHValidates against all columns using the domain, blocking writes on those tables
37CREATE DOMAIN with constraintsACCESS SHARELOWDomains with constraints have poor migration support; use table-level CHECK constraints

Idempotency Checks

pgfence flags statements that fail on re-run without an IF [NOT] EXISTS guard.

#PatternRiskSuggestion
38CREATE INDEX CONCURRENTLY without a preceding DROP INDEX CONCURRENTLY IF EXISTSLOWDrop any INVALID leftover first for retry safety. Do not add IF NOT EXISTS: it skips an invalid index
39CREATE TABLE without IF NOT EXISTSLOWAdd IF NOT EXISTS for idempotent migrations
40DROP INDEX without IF EXISTSLOWAdd IF EXISTS to prevent errors on re-run
41DROP TABLE without IF EXISTSLOWAdd IF EXISTS to prevent errors on re-run

Rule reference

Every finding links here by rule id. There are 103 rules on this page.

RuleTitleDescription
add-column-constant-default ADD COLUMN with a constant default On PostgreSQL 11 and later a constant default is stored as metadata only, so the column is added without a table rewrite.
add-column-constrained-domain ADD COLUMN using a domain with constraints Adding a column of a constrained domain validates the domain constraint against existing rows and can rewrite the table under an ACCESS EXCLUSIVE lock.
add-column-default-pre-pg11 ADD COLUMN with a default on Postgres before 11 Before PostgreSQL 11, adding a column with any DEFAULT rewrites the table under an ACCESS EXCLUSIVE lock.
add-column-identity ADD COLUMN with an identity Adding an identity column populates every existing row from a sequence, which rewrites the table under an ACCESS EXCLUSIVE lock.
add-column-inline-check ADD COLUMN with an inline CHECK An inline CHECK on a new column is validated against every existing row while the ALTER holds an ACCESS EXCLUSIVE lock. Add the column, then add the CHECK as NOT VALID and validate it separately.
add-column-inline-foreign-key ADD COLUMN with an inline foreign key An inline REFERENCES validates against the referenced table while locks are held. Add the column, then add the foreign key as NOT VALID and validate it separately.
add-column-inline-foreign-key-not-valid ADD COLUMN with an inline foreign key marked NOT VALID The foreign key skips validation, but the ALTER still takes an ACCESS EXCLUSIVE lock on the table.
add-column-inline-unique ADD COLUMN with inline UNIQUE An inline UNIQUE constraint builds a unique index while holding an ACCESS EXCLUSIVE lock. Create the index concurrently and attach it with USING INDEX.
add-column-non-constant-default ADD COLUMN with a volatile default A volatile DEFAULT (for example clock_timestamp() or random()) forces a full table rewrite under an ACCESS EXCLUSIVE lock. Add the column without a default and backfill in batches.
add-column-not-null-no-default ADD COLUMN NOT NULL without a default Adding a NOT NULL column with no DEFAULT fails on a table that already has rows, and takes an ACCESS EXCLUSIVE lock while it does. Add the column nullable, backfill, then enforce NOT NULL with a validated CHECK constraint.
add-column-stable-default ADD COLUMN with a stable default On PostgreSQL 11 and later a stable default (for example now()) is evaluated once and stored as metadata, so the column is added without a table rewrite.
add-column-stored-generated ADD COLUMN GENERATED ALWAYS AS ... STORED A stored generated column is computed for every existing row, which rewrites the table under an ACCESS EXCLUSIVE lock.
add-column-unresolved-custom-type ADD COLUMN with an unresolved custom type pgfence cannot tell whether the custom type carries constraints, so it cannot prove the ADD COLUMN is cheap. Provide a schema snapshot to resolve it.
add-constraint-check-no-not-valid ADD CHECK without NOT VALID Adding a CHECK constraint scans the whole table while holding an ACCESS EXCLUSIVE lock. Add it NOT VALID, then run VALIDATE CONSTRAINT separately.
add-constraint-check-not-valid ADD CHECK marked NOT VALID The constraint is added without scanning the table. Run VALIDATE CONSTRAINT afterwards, in a separate transaction.
add-constraint-exclude ADD EXCLUDE constraint Adding an exclusion constraint builds its index while holding an ACCESS EXCLUSIVE lock, and there is no concurrent alternative. Bound the wait with lock_timeout.
add-constraint-fk-no-not-valid ADD FOREIGN KEY without NOT VALID Adding a foreign key validates every row while holding a SHARE ROW EXCLUSIVE lock. Add it NOT VALID, then run VALIDATE CONSTRAINT separately.
add-constraint-not-null-no-not-valid ADD CONSTRAINT NOT NULL without NOT VALID On PostgreSQL 18, ADD CONSTRAINT ... NOT NULL scans the whole table under an ACCESS EXCLUSIVE lock. Add it NOT VALID, then VALIDATE CONSTRAINT separately.
add-constraint-not-null-not-valid ADD CONSTRAINT NOT NULL NOT VALID A brief ACCESS EXCLUSIVE metadata change with no scan (PostgreSQL 18). Validate it later with VALIDATE CONSTRAINT, which takes only SHARE UPDATE EXCLUSIVE.
add-constraint-unique ADD UNIQUE constraint Adding a UNIQUE constraint builds an index while holding an ACCESS EXCLUSIVE lock. Build a unique index concurrently first, then attach it with USING INDEX.
add-constraint-unique-using-index ADD UNIQUE USING INDEX Attaches a pre-built unique index. This is the safe form and only needs a brief lock.
add-constraint-unknown-type ADD CONSTRAINT of an unrecognized type pgfence does not recognize this constraint type, so it assumes the most restrictive lock.
add-pk-using-index ADD PRIMARY KEY USING INDEX Attaches a pre-built unique index as the primary key. The attach is quick but still needs an ACCESS EXCLUSIVE lock.
add-pk-without-using-index ADD PRIMARY KEY without a pre-built index Adding a primary key builds a unique index while holding an ACCESS EXCLUSIVE lock. Build the index concurrently first, then attach it with USING INDEX.
alter-column-default ALTER COLUMN SET or DROP DEFAULT A catalog-only change under a brief ACCESS EXCLUSIVE lock. Dropping the default of a NOT NULL column breaks inserts that relied on it.
alter-column-drop-expression ALTER COLUMN DROP EXPRESSION Turns a generated column into a plain column without a rewrite, under a brief ACCESS EXCLUSIVE lock. Writers must supply the value from then on.
alter-column-identity ALTER COLUMN identity change Adding, changing or dropping an identity takes a brief ACCESS EXCLUSIVE lock without a rewrite. A newly added identity starts at 1, which can collide with existing values unless restarted above the current maximum.
alter-column-set-expression ALTER COLUMN SET EXPRESSION Changing the expression of a STORED generated column rewrites the table under an ACCESS EXCLUSIVE lock (PostgreSQL 17+). Add a new column, backfill, and swap instead.
alter-column-set-not-null ALTER COLUMN SET NOT NULL Setting NOT NULL scans the table while holding an ACCESS EXCLUSIVE lock. Add a validated CHECK (col IS NOT NULL) constraint first so the scan is skipped.
alter-column-type ALTER COLUMN TYPE Most type changes rewrite the table under an ACCESS EXCLUSIVE lock. Some widening changes are metadata-only. Use expand and contract for the rest.
alter-constraint ALTER CONSTRAINT Changing a constraint (NOT ENFORCED, DEFERRABLE, NO INHERIT) takes an ACCESS EXCLUSIVE lock. Usually brief; NOT ENFORCED on a foreign key also locks the referenced table.
alter-constraint-enforced ALTER CONSTRAINT ... ENFORCED Making a NOT ENFORCED foreign key ENFORCED takes ACCESS EXCLUSIVE, recreates its triggers and scans every row while the lock is held. Drop it and re-add it NOT VALID, then VALIDATE separately.
alter-database-role-set ALTER DATABASE or ROLE ... SET Changes defaults for every new session of that database or role. Prefer SET LOCAL in the migration, or manage defaults in provisioning.
alter-enum-add-value ALTER TYPE ADD VALUE The statement takes an EXCLUSIVE lock on the type object, not on any table. On PostgreSQL 12 and later the new value is added instantly but cannot be used in the same transaction. Before PostgreSQL 12 the statement cannot run inside a transaction block.
alter-enum-no-ordering ALTER TYPE ADD VALUE without explicit ordering Without BEFORE or AFTER the new value is appended at the end of the enum ordering. Enum value positions are permanent.
alter-enum-rename-value ALTER TYPE ... RENAME VALUE Renaming an enum label takes a brief EXCLUSIVE lock on the type and breaks code or data that still uses the old label.
alter-policy ALTER POLICY Changes which rows roles can access, taking effect immediately.
alter-system ALTER SYSTEM Changes a cluster-wide setting in postgresql.auto.conf, outside the migration transaction. Manage server settings in infrastructure configuration.
attach-partition ATTACH PARTITION Validates the partition constraint by scanning the partition unless a matching CHECK constraint already exists. Add the CHECK constraint first.
autovacuum-disabled autovacuum_enabled = false Disabling autovacuum lets dead rows and transaction ID age build up until a forced anti-wraparound vacuum. Tune the scale factors instead.
ban-alter-domain-add-constraint ALTER DOMAIN ADD CONSTRAINT Validates the constraint against every column that uses the domain, blocking writes on those tables.
ban-create-domain-with-constraint CREATE DOMAIN with constraints Domains with constraints have poor migration support. Prefer table-level CHECK constraints.
cluster CLUSTER Rewrites the whole table under an ACCESS EXCLUSIVE lock. Use pg_repack for an online rewrite.
create-enum-type CREATE TYPE AS ENUM Enum values cannot be removed once added. Consider a lookup table if the set of values will change.
create-index-concurrent-if-not-exists CREATE INDEX CONCURRENTLY IF NOT EXISTS If an earlier concurrent build failed, IF NOT EXISTS silently skips the INVALID index it left behind. Drop it first with DROP INDEX CONCURRENTLY IF EXISTS, then create the index.
create-index-not-concurrent CREATE INDEX without CONCURRENTLY A regular index build blocks writes to the table for its whole duration. Use CREATE INDEX CONCURRENTLY, outside a transaction.
create-index-partitioned-parent CREATE INDEX on a partitioned table An index on a partitioned parent cannot be built CONCURRENTLY and locks every partition with SHARE. Create it ON ONLY the parent, build each partition index concurrently, then attach them.
create-policy CREATE POLICY Changes which rows roles can access, taking effect immediately.
create-table-exclude-constraint CREATE TABLE with an EXCLUDE constraint The exclusion constraint is built while the table is created, so there are no existing rows to scan. Review it as table creation.
create-table-no-primary-key CREATE TABLE without a primary key A table without a primary key or unique NOT NULL key cannot replicate UPDATE and DELETE logically and is hard to deduplicate. Declare a key.
create-trigger CREATE TRIGGER Takes a SHARE ROW EXCLUSIVE lock on the table, and the trigger then runs on every matching write.
create-unlogged-table CREATE UNLOGGED TABLE Unlogged tables are truncated after a crash and are not replicated to standbys. Use a logged table unless the data is disposable.
delete-without-where DELETE without WHERE Deletes every row in one transaction, holding row locks and generating bloat. Delete in batches.
detach-partition DETACH PARTITION Takes an ACCESS EXCLUSIVE lock on the parent table. Use DETACH PARTITION CONCURRENTLY on PostgreSQL 14 and later.
detach-partition-concurrent DETACH PARTITION CONCURRENTLY The safe form on PostgreSQL 14 and later. It cannot run inside a transaction block.
disable-rls DISABLE ROW LEVEL SECURITY Stops enforcing row policies, exposing every row to roles that could previously see only some.
drop-cascade DROP ... CASCADE CASCADE silently drops every dependent object, each under an ACCESS EXCLUSIVE lock. Drop dependents explicitly so the migration shows what it removes.
drop-column DROP COLUMN Takes an ACCESS EXCLUSIVE lock and is not reversible. Remove application references first, in an earlier release.
drop-constraint DROP CONSTRAINT Dropping a constraint takes an ACCESS EXCLUSIVE lock and removes a guarantee the application may rely on.
drop-database DROP DATABASE Irreversibly destroys the entire database.
drop-index-not-concurrent DROP INDEX without CONCURRENTLY A regular DROP INDEX takes an ACCESS EXCLUSIVE lock on the table. Use DROP INDEX CONCURRENTLY, outside a transaction.
drop-not-null ALTER COLUMN DROP NOT NULL Takes an ACCESS EXCLUSIVE lock and removes a guarantee the application may rely on.
drop-object DROP VIEW, SEQUENCE, TYPE, FUNCTION or similar Dropping a view, sequence, type, domain, extension or function breaks any application code still using it. Remove all uses first and drop in a later release.
drop-policy DROP POLICY Removing a policy can expose rows, or hide all of them if it was the only permissive policy.
drop-schema DROP SCHEMA Destroys every object in the schema. Remove application references first, in a separate release.
drop-schema-cascade DROP SCHEMA CASCADE Irreversibly drops every object in the schema and everything that depends on them. Drop objects explicitly instead.
drop-table DROP TABLE Takes an ACCESS EXCLUSIVE lock and destroys data. Drop in a separate release after removing application references.
drop-tablespace DROP TABLESPACE A cluster-level operation that fails unless the tablespace is empty. Handle it as a separate operations procedure, not a migration.
drop-trigger DROP TRIGGER Takes an ACCESS EXCLUSIVE lock on the table.
enable-disable-trigger ENABLE / DISABLE TRIGGER Takes a SHARE ROW EXCLUSIVE lock, and disabling a trigger silently stops whatever it enforced.
enable-rls ENABLE ROW LEVEL SECURITY Changes which rows every non-owner role can see, taking effect immediately. With no policies, those roles see no rows.
fk-missing-index Foreign key without a supporting index Without an index on the referencing columns, CASCADE actions and DELETEs on the referenced table fall back to sequential scans of the referencing table.
force-rls FORCE ROW LEVEL SECURITY Applies row policies to the table owner as well, which can lock out migrations and maintenance roles.
identifier-too-long Identifier longer than 63 bytes PostgreSQL silently truncates identifiers to 63 bytes, so two long names can collide and the stored name differs from the one in the migration. Shorten the name.
inherit ALTER TABLE INHERIT Changes the table hierarchy and affects what queries on the parent return.
lock-held-across-long-operation Strong lock held across a long operation A strong lock (ACCESS EXCLUSIVE or SHARE ROW EXCLUSIVE) taken early in a transaction is held until it commits, so a later backfill, index build or VALIDATE keeps the table blocked for its whole duration. Commit the DDL in its own migration before the long statement.
lock-table LOCK TABLE An explicit lock is held until the transaction ends. ACCESS EXCLUSIVE, the default mode, blocks reads and writes for the rest of the migration.
merge-alter-table Repeated ALTER TABLE that each scan or rewrite Several ALTER TABLE statements on one table that each scan or rewrite it do that work several times. Combine them into one ALTER TABLE so PostgreSQL makes one pass.
no-force-rls NO FORCE ROW LEVEL SECURITY Exempts the table owner from row policies.
no-inherit ALTER TABLE NO INHERIT Changes the table hierarchy and affects what queries on the parent return.
prefer-robust-create-index CREATE INDEX without IF NOT EXISTS Add IF NOT EXISTS so the migration can be safely re-run.
prefer-robust-create-table CREATE TABLE without IF NOT EXISTS Add IF NOT EXISTS so the migration can be safely re-run.
prefer-robust-drop-index DROP INDEX without IF EXISTS Add IF EXISTS so the migration can be safely re-run.
prefer-robust-drop-table DROP TABLE without IF EXISTS Add IF EXISTS so the migration can be safely re-run.
refresh-matview-blocking REFRESH MATERIALIZED VIEW without CONCURRENTLY Blocks reads of the materialized view while it refreshes. Use REFRESH MATERIALIZED VIEW CONCURRENTLY, which needs a unique index.
refresh-matview-concurrent REFRESH MATERIALIZED VIEW CONCURRENTLY Allows reads during the refresh. It needs a unique index on the view.
reindex-non-concurrent REINDEX without CONCURRENTLY A non-concurrent REINDEX blocks writes to the table, and reads that use the index, while it rebuilds. Use REINDEX CONCURRENTLY on PostgreSQL 12 and later.
rename-column RENAME COLUMN Takes an ACCESS EXCLUSIVE lock briefly, and breaks any application code still using the old name.
rename-object RENAME of an index, view, sequence, type, function, constraint, trigger or policy Renaming breaks code that uses the old name. Index renames take SHARE UPDATE EXCLUSIVE on PG12 and later (ACCESS EXCLUSIVE on the index before that); constraint, trigger and policy renames take ACCESS EXCLUSIVE on the table.
rename-schema RENAME SCHEMA Breaks every query that qualifies objects with the old schema name.
rename-table RENAME TABLE Takes an ACCESS EXCLUSIVE lock and breaks any application code still using the old name. Rename and add a compatibility view.
replica-identity-full REPLICA IDENTITY FULL Logs every column of old rows on update and delete, which increases WAL volume and can slow logical replication.
replica-identity-nothing REPLICA IDENTITY NOTHING With no replica identity, UPDATE and DELETE fail on a table that is part of a publication. Use the primary key (DEFAULT) or a unique NOT NULL index.
security-definer-search-path SECURITY DEFINER without a fixed search_path A SECURITY DEFINER function without SET search_path can be hijacked by objects in schemas the caller controls. Pin search_path and end it with pg_temp.
set-access-method SET ACCESS METHOD Rewrites the whole table under an ACCESS EXCLUSIVE lock.
set-logged-unlogged SET LOGGED / SET UNLOGGED Rewrites the whole table under an ACCESS EXCLUSIVE lock.
set-schema ALTER ... SET SCHEMA Moving an object to another schema breaks every query that names it with the old schema. Keep the old path working until callers have moved.
set-tablespace SET TABLESPACE Copies the relation to the new tablespace while holding an ACCESS EXCLUSIVE lock.
set-tablespace-all ALTER ... ALL IN TABLESPACE SET TABLESPACE Moves every matching relation at once, copying each under an ACCESS EXCLUSIVE lock. Move relations one at a time in a maintenance window.
truncate TRUNCATE Takes an ACCESS EXCLUSIVE lock and removes all rows. Use batched DELETE where the table must stay available.
truncate-cascade TRUNCATE CASCADE Also truncates every table with a foreign key to the target, which is rarely the full set the author intended. Truncate tables explicitly.
vacuum-full VACUUM FULL Rewrites the whole table under an ACCESS EXCLUSIVE lock. Use pg_repack for an online rewrite.
validate-constraint VALIDATE CONSTRAINT Validation scans the table without blocking reads or writes. It is the safe second step after adding a NOT VALID constraint.