-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sql: enable adding CHECK constraints in ALTER TABLE ADD COLUMN #29639
Comments
Add check constraints to the table descriptor immediately if any columns are currently being added. Check validation does not get turned on until all used columns are public. Related to cockroachdb#29639 cockroachdb#26508. Release note: None
… ... Release note: None
… ... Release note: None
… ... Release note: None
I have investigated this further. Although processing the descriptor change in ALTER is trivial, the column backfill logic must be extended to process CHECK constraints on newly populated values. Indeed, the following must produce an error:
Without a CHECK validation in the backfill, it would be possible for a row to be populated with the (invalid) value 123. |
#32504 is related to this - it will correctly handle validating the newly populated values in the case where the ADD COLUMN and ADD CONSTRAINT happen in the same transaction. |
Oh great! thank you!! |
@lucy-zhang are you taking Erik's PR over, given he left the org? |
@knz yes, that's the plan |
35018: sql: support adding a check with ALTER TABLE ADD COLUMN r=lucy-zhang a=lucy-zhang This PR adds support for adding a check constraint to a column in ALTER TABLE ADD COLUMN. The approach is to transform the column constraints into top-level commands in ALTER TABLE, so that, e.g., ``` ALTER TABLE t ADD COLUMN a INT CHECK (a > 0) ```` is treated like ``` ALTER TABLE t ADD COLUMN a INT, ADD CONSTRAINT check_a CHECK (a > 0) ``` similarly to how column constraints are handled in CREATE TABLE. For computed columns or columns with default values, the constraint is validated for the backfilled values before the column becomes public, which is the existing behavior when ADD COLUMN and ADD CONSTRAINT are in the same transaction. Closes #29639. Release note (sql change): Check constraints can now be added to columns that are created through ALTER TABLE ADD COLUMN. Co-authored-by: Lucy Zhang <[email protected]>
When adding a column to a table via
ALTER TABLE ... ADD COLUMN
, it's not currently possible to define aCHECK
constraint on the new column:The
ADD COLUMN
docs currently only call out that you can't add a column with a foreign key constraint. All other constraints can be defined while adding a column.I can update the docs to reflect the current behavior, but @knz suggested that it should be possible to add a column and define a check constraint on that column in a single statement.
The text was updated successfully, but these errors were encountered: