Skip to content

MG13 — ADD PRIMARY KEY/UNIQUE builds its index under a lock

Group: Migration safety · Auto-fixable: no

ADD PRIMARY KEY or ADD UNIQUE builds the backing index while holding an exclusive lock. Build a unique index CONCURRENTLY, then attach it with ADD CONSTRAINT … USING INDEX, which only needs a brief lock.

bad: ALTER TABLE t ADD CONSTRAINT u UNIQUE (email);
good: CREATE UNIQUE INDEX CONCURRENTLY u ON t (email);
ALTER TABLE t ADD CONSTRAINT u UNIQUE USING INDEX u;

Disable this rule in banshee.toml:

[lint]
exclude = ["MG13"]

Or silence one line with -- noqa: MG13. See Linting.