pgguard 0.1.0 · 17 rules
Rule reference
Every rule says what it found, how dangerous it is on a table that is already live, and what to write instead. A finding on a table created in the same migration is dropped to low: an empty table has nothing to lock, rewrite or break.
All 17
worst first in a scan, in order here| Rule | Default severity | What it catches |
|---|---|---|
| PG001 | critical | ADD COLUMN NOT NULL without a DEFAULT |
| PG002 | high | CREATE INDEX without CONCURRENTLY |
| PG003 | critical | CONCURRENTLY inside a transaction block |
| PG004 | high | ALTER COLUMN TYPE rewrites the table |
| PG005 | medium | DROP COLUMN |
| PG006 | high | DROP TABLE |
| PG007 | high | RENAME breaks deployed code |
| PG008 | high | ADD FOREIGN KEY without NOT VALID |
| PG009 | medium | VALIDATE CONSTRAINT in the same transaction as NOT VALID |
| PG010 | medium | ADD CHECK constraint without NOT VALID |
| PG011 | critical | VACUUM FULL or CLUSTER |
| PG012 | high | SET NOT NULL on an existing column |
| PG013 | high | ADD PRIMARY KEY or UNIQUE without a pre-built index |
| PG014 | medium | No lock_timeout set before blocking DDL |
| PG015 | high | ADD COLUMN with a volatile DEFAULT rewrites the table |
| PG016 | medium | A data migration runs inside the migration transaction |
| PG017 | medium | AddField fills existing rows with the field type’s implicit default |
PG001 ADD COLUMN NOT NULL without a DEFAULT critical
Adding a NOT NULL column with no DEFAULT aborts on any table that already holds rows. It passes against the empty table in CI and fails the deploy in production.
PG002 CREATE INDEX without CONCURRENTLY high
A plain CREATE INDEX holds a SHARE lock for the whole build. Reads keep working; every INSERT, UPDATE and DELETE on the table blocks until it finishes.
PG003 CONCURRENTLY inside a transaction block critical
CREATE INDEX CONCURRENTLY, DROP INDEX CONCURRENTLY and REINDEX CONCURRENTLY cannot run inside a transaction block. Postgres raises an error and the migration fails.
PG004 ALTER COLUMN TYPE rewrites the table high
Changing a column type generally rewrites the whole table and rebuilds every index on it, under an ACCESS EXCLUSIVE lock that blocks reads as well as writes.
PG005 DROP COLUMN medium
The lock is brief, but any running application code that still references the column starts erroring the instant the migration commits, and the data is gone.
PG006 DROP TABLE high
Destroys the table and its data, and breaks any still-deployed code or dependent object that relies on it.
PG007 RENAME breaks deployed code high
A rename is instant and cheap for Postgres, and immediately breaks every deployed version of the application that still uses the old name.
PG008 ADD FOREIGN KEY without NOT VALID high
Adding a foreign key validates every existing row while holding locks on both tables, so writes to both block for the length of a full scan.
PG009 VALIDATE CONSTRAINT in the same transaction as NOT VALID medium
Adding a constraint NOT VALID and validating it in the same transaction blocks for exactly as long as adding it in one step. The two-step pattern only pays off across transactions.
PG010 ADD CHECK constraint without NOT VALID medium
A CHECK constraint added in one step scans the whole table under an ACCESS EXCLUSIVE lock, blocking reads and writes.
PG011 VACUUM FULL or CLUSTER critical
Both rewrite the entire table and all its indexes under an ACCESS EXCLUSIVE lock. The table is completely unavailable for the duration, and the operation needs as much free disk as the table itself.
PG012 SET NOT NULL on an existing column high
SET NOT NULL scans the whole table under an ACCESS EXCLUSIVE lock. A validated CHECK constraint lets Postgres 12+ skip that scan.
PG013 ADD PRIMARY KEY or UNIQUE without a pre-built index high
The constraint builds its backing index under an ACCESS EXCLUSIVE lock. Building the index CONCURRENTLY first and adopting it with USING INDEX avoids that.
PG014 No lock_timeout set before blocking DDL medium
DDL that cannot take its lock immediately waits, and every query that needs the table queues behind it. Without a lock_timeout, one long-running query turns a fast migration into an outage.
PG015 ADD COLUMN with a volatile DEFAULT rewrites the table high
Postgres 11+ adds a column with a constant default for free, but a volatile default such as gen_random_uuid() has to be evaluated per row, which rewrites the whole table.
PG016 A data migration runs inside the migration transaction medium
Django wraps each migration in a transaction unless the Migration class sets atomic = False, so a RunPython backfill holds its locks and its snapshot for the whole run and cannot be resumed if it fails halfway.
PG017 AddField fills existing rows with the field type’s implicit default medium
A Django field that is neither nullable nor given a default still has to put something in every existing row, so Django writes the field type’s implicit default -- "" for text, 0 for a number -- and your code then reads it as a real value.