Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: async validation of foreign keys in ADD CONSTRAINT
Currently, validating an unvalidated foreign key constraint requires running `VALIDATE CONSTRAINT`, which executes a potentially long-running query in the user transaction. In this PR, `ADD CONSTRAINT` for foreign keys is now processed by the schema changer, with the validation for existing rows running in a separate transaction from the original user transaction. The foreign key mutation (represented by the new `FOREIGN_KEY` enum value in `ConstraintToUpdate`) is processed in the same way as check constraint mutations: the foreign key is in the mutations list while other columns and indexes with the same mutation ID are backfilled, then added to the appropriate index in the `Validating` state before being validated, and is finalized when the validation query for existing rows completes successfully. If validation fails, the transaction is rolled back, with the foreign key (and backreference) removed from the table descriptor(s) as part of the rollback. Adding foreign keys to columns or indexes being added is still not supported and is left for later work. Also unsupported is adding a foreign key constraint in the same transaction as `CREATE TABLE` that is either validated or that rolls back the entire transaction on failure. In this PR, the constraint is just left unvalidated; This needs a follow-up PR to queue a separate mutation for validating the constraint after it's been added. Release note (sql change): Foreign keys that are added to an existing table using `ALTER TABLE` will now be validated for existing rows, with improved performance compared to running `ADD CONSTRAINT` followed by `VALIDATE CONSTRAINT` previously.
- Loading branch information