add-unique-constraint
errorADD UNIQUE constraint inline
Why it's unsafe
Adding a UNIQUE constraint inline builds its underlying index while holding ACCESS EXCLUSIVE on the table for the whole build.
Safe rewrite
Build the index first with CREATE UNIQUE INDEX CONCURRENTLY, then attach it: ALTER TABLE ... ADD CONSTRAINT ... UNIQUE USING INDEX idx (a brief lock).
Example
Try it in the playground →Unsafe
ALTER TABLE users ADD CONSTRAINT uq_users_email UNIQUE (email);Safe
CREATE UNIQUE INDEX CONCURRENTLY uq_users_email ON users (email);
ALTER TABLE users ADD CONSTRAINT uq_users_email UNIQUE USING INDEX uq_users_email;