Skip to content
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

Closed
jseldess opened this issue Sep 6, 2018 · 5 comments
Closed

sql: enable adding CHECK constraints in ALTER TABLE ADD COLUMN #29639

jseldess opened this issue Sep 6, 2018 · 5 comments
Assignees
Labels
A-schema-changes A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) docs-known-limitation X-anchored-telemetry The issue number is anchored by telemetry references.

Comments

@jseldess
Copy link
Contributor

jseldess commented Sep 6, 2018

When adding a column to a table via ALTER TABLE ... ADD COLUMN, it's not currently possible to define a CHECK constraint on the new column:

[email protected]:60410/defaultdb> create table t1 (a int primary key);
CREATE TABLE

Time: 4.534737ms

[email protected]:60410/defaultdb> alter table t1 add column b int check (b > 0);
pq: adding a CHECK constraint via ALTER not supported

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.

@knz knz added C-investigation Further steps needed to qualify. C-label will change. A-schema-changes labels Sep 7, 2018
eriktrinh pushed a commit to eriktrinh/cockroach that referenced this issue Nov 12, 2018
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
@knz knz added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-sql-pgcompat Semantic compatibility with PostgreSQL X-anchored-telemetry The issue number is anchored by telemetry references. and removed C-investigation Further steps needed to qualify. C-label will change. labels Nov 22, 2018
knz added a commit to knz/cockroach that referenced this issue Nov 22, 2018
knz added a commit to knz/cockroach that referenced this issue Nov 26, 2018
knz added a commit to knz/cockroach that referenced this issue Nov 28, 2018
@knz knz changed the title sql: Not possible to add a column with a check constraint sql: enable adding CHECK constraints in ALTER TABLE ADD COLUMN Jan 3, 2019
@knz
Copy link
Contributor

knz commented Jan 3, 2019

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:

ALTER TABLE foo ADD COLUMN bar INT DEFAULT 123 CHECK (bar < 0)

Without a CHECK validation in the backfill, it would be possible for a row to be populated with the (invalid) value 123.

@thoszhang
Copy link
Contributor

#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.

@knz
Copy link
Contributor

knz commented Jan 8, 2019

Oh great! thank you!!

@knz
Copy link
Contributor

knz commented Jan 8, 2019

@lucy-zhang are you taking Erik's PR over, given he left the org?

@thoszhang
Copy link
Contributor

@knz yes, that's the plan

craig bot pushed a commit that referenced this issue Feb 22, 2019
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]>
@craig craig bot closed this as completed in #35018 Feb 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-schema-changes A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) docs-known-limitation X-anchored-telemetry The issue number is anchored by telemetry references.
Projects
None yet
Development

No branches or pull requests

3 participants