|
| 1 | +--- |
| 2 | +id: adding-foreign-key-constraint |
| 3 | +title: adding-foreign-key-constraint |
| 4 | +--- |
| 5 | + |
| 6 | +A foreign key constraint should be added with `NOT VALID`. |
| 7 | + |
| 8 | +Adding a foreign key constraint requires a table scan and a `SHARE ROW EXCLUSIVE` lock on both tables, which blocks writes. |
| 9 | + |
| 10 | +Adding the constraint as `NOT VALID` in one transaction and then using |
| 11 | +`VALIDATE` in another transaction will allow writes when adding the |
| 12 | +constraint. |
| 13 | + |
| 14 | +## problem |
| 15 | + |
| 16 | +Adding a foreign key constraint requires a table scan and a `SHARE ROW EXCLUSIVE` lock on both tables, which blocks writes to each table. |
| 17 | + |
| 18 | +This means no writes will be allowed to either table while the table you're altering is scanned to validate the constraint. |
| 19 | + |
| 20 | +## solution |
| 21 | + |
| 22 | +To prevent blocking writes to tables, add the constraint as `NOT VALID` in one transaction, then `VALIDATE CONSTRAINT` in another. |
| 23 | + |
| 24 | +While `NOT VALID` prevents row updates while running, it commits instantly if it can get a lock (see ["Safety requirements"](./safe_migrations.md#safety-requirements)). `VALIDATE CONSTRAINT` allows row updates while it scans |
| 25 | +the table. |
| 26 | + |
| 27 | +### adding constraint to existing table |
| 28 | + |
| 29 | +Instead of: |
| 30 | + |
| 31 | +```sql |
| 32 | +ALTER TABLE "email" ADD CONSTRAINT "fk_user" |
| 33 | + FOREIGN KEY ("user_id") REFERENCES "user" ("id"); |
| 34 | +``` |
| 35 | + |
| 36 | +Use: |
| 37 | + |
| 38 | +```sql |
| 39 | +ALTER TABLE "email" ADD CONSTRAINT "fk_user" |
| 40 | + FOREIGN KEY ("user_id") REFERENCES "user" ("id") NOT VALID; |
| 41 | +ALTER TABLE "email" VALIDATE CONSTRAINT "fk_user"; |
| 42 | +``` |
| 43 | + |
| 44 | +Add the foreign key constraint as `NOT VALID` to prevent locking the `"email"` and `"user"` tables. |
| 45 | + |
| 46 | +Run `VALIDATE CONSTRAINT` to scan the `"email"` table in the background while reads and writes continue. |
| 47 | + |
| 48 | +### adding constraint to new table |
| 49 | + |
| 50 | +Instead of: |
| 51 | + |
| 52 | +```sql |
| 53 | +CREATE TABLE email ( |
| 54 | + id BIGINT GENERATED ALWAYS AS IDENTITY, |
| 55 | + user_id BIGINT, |
| 56 | + email TEXT, |
| 57 | + PRIMARY KEY(id), |
| 58 | + CONSTRAINT fk_user |
| 59 | + FOREIGN KEY ("user_id") |
| 60 | + REFERENCES "user" ("id") |
| 61 | +); |
| 62 | +``` |
| 63 | + |
| 64 | +Use: |
| 65 | + |
| 66 | +```sql |
| 67 | +CREATE TABLE email ( |
| 68 | + id BIGINT GENERATED ALWAYS AS IDENTITY, |
| 69 | + user_id BIGINT, |
| 70 | + email TEXT, |
| 71 | + PRIMARY KEY(id) |
| 72 | +); |
| 73 | + |
| 74 | +ALTER TABLE "email" ADD CONSTRAINT "fk_user" |
| 75 | + FOREIGN KEY ("user_id") REFERENCES "user" ("id") NOT VALID; |
| 76 | +ALTER TABLE "email" VALIDATE CONSTRAINT "fk_user"; |
| 77 | +``` |
| 78 | + |
| 79 | +Create the table, add the foreign key constraint as `NOT VALID`, then `VALIDATE` the constraint. |
0 commit comments