Adding a foreign key constraint to a twenty-million-row Postgres table will lock writes while it scans every existing row unless you append NOT VALID to the ALTER TABLE statement. You can enforce the constraint on new writes immediately and run VALIDATE CONSTRAINT right after without blocking traffic.
commit abf8e5f