All rules Rule BV017
note
hosted, paid — --remote

Foreign key without an index on the referencing columns

Note — this works and blocks nothing, but a performance regression is likely.

What it catches

Postgres indexes the referenced side of a foreign key (it must be unique) but never the referencing side. Without that index, every UPDATE or DELETE on the parent table sequential-scans the child table to enforce the constraint — fine in dev, a scan-per-row regression in production.

Fires on

ALTER TABLE orders ADD CONSTRAINT fk FOREIGN KEY (user_id) REFERENCES users (id) NOT VALID;

Do this instead

Index the referencing column(s) — built CONCURRENTLY in its own migration so the build never blocks writes. With the index in place, parent-side UPDATE/DELETE enforce the constraint via index lookups instead of sequential scans of the child table.

-- In its own migration file, run outside a transaction:
CREATE INDEX CONCURRENTLY idx_orders_user_id ON orders (user_id);
Catch this before it ships

This rule runs in the hosted service on Startup and above — add --remote with a team token, or use the GitHub Action: npx bolvrk check migration.sql