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: rearchitecture ALTER TABLE RENAME, add support for renaming constraints #35091

Merged
merged 1 commit into from Mar 7, 2019

Conversation

knz
Copy link
Contributor

@knz knz commented Feb 20, 2019

Fixes #32555. For TypeORM compat, see discussion on #22298.

Release note (sql change): This patch changes RENAME COLUMN to become
a "table command", which can be used alongside other table commands in
a single ALTER TABLE statement. This makes it possible to e.g. atomically
add a computed column based on an existing column, and rename the
columns so that the computed column "replaces" the original column.

Release note (sql change): CockroachDB now supports ALTER TABLE RENAME
CONSTRAINT for compatibility with PostgreSQL. This feature is limited
to named constraints, where the name of the constraints is preserved
in the table metadata. This currently includes CHECK, UNIQUE and
FOREIGN KEY constraints, and does not include other
constraints (DEFAULT, NULL etc) otherwise supported by PostgreSQL. For
UNIQUE constraint, only supporting indexes that are not depended on by
views can be renamed.

@knz knz added this to Triage in (DEPRECATED) SQL Front-end, Lang & Semantics via automation Feb 20, 2019
@knz knz requested review from bobvawter, BramGruneir and a team February 20, 2019 12:18
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@knz
Copy link
Contributor Author

knz commented Feb 25, 2019

ping

@dt dt requested a review from thoszhang February 25, 2019 15:37
@dt
Copy link
Member

dt commented Feb 25, 2019

just want to make sure you're sync'ed up with @lucy-zhang who's actively working on some constraint-related schema mgmt stuff right now

Copy link
Contributor

@thoszhang thoszhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll review this more thoroughly later, but I have one question for now.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @bobvawter, @BramGruneir, @dt, @knz, and @lucy-zhang)


pkg/sql/sqlbase/structured.go, line 2110 at r1 (raw file):

// RenameConstraint renames a constraint.
func (desc *MutableTableDescriptor) RenameConstraint(

I haven't tested this myself yet, but what happens if a constraint is renamed while it's being added? We now enqueue a mutation to validate check constraints when they're added, and the mutation only stores the name of the check constraint (since they don't have IDs, which is unfortunate), so when the validation runs, I think it will fail if the constraint has been renamed in the meantime.

Copy link
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @bobvawter, @BramGruneir, @dt, and @lucy-zhang)


pkg/sql/sqlbase/structured.go, line 2110 at r1 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

I haven't tested this myself yet, but what happens if a constraint is renamed while it's being added? We now enqueue a mutation to validate check constraints when they're added, and the mutation only stores the name of the check constraint (since they don't have IDs, which is unfortunate), so when the validation runs, I think it will fail if the constraint has been renamed in the meantime.

I don't have an answer for this. You're right it may break. I can add a TODO, file an issue, and add an extra error check in the constraint validation code in case "the name does not exist any more". I think the case is sufficiently rare that an error in that case is ok for now. What do you think?

(Alternatively if you have an idea of a solution, I'll take it)

Copy link
Contributor

@thoszhang thoszhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @bobvawter, @BramGruneir, @dt, and @knz)


pkg/sql/sqlbase/structured.go, line 2110 at r1 (raw file):

Previously, knz (kena) wrote…

I don't have an answer for this. You're right it may break. I can add a TODO, file an issue, and add an extra error check in the constraint validation code in case "the name does not exist any more". I think the case is sufficiently rare that an error in that case is ok for now. What do you think?

(Alternatively if you have an idea of a solution, I'll take it)

Could we disallow renaming constraints that are being added? We currently disallow deleting constraints being added: https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/alter_table.go#L466-L468

Copy link
Contributor

@thoszhang thoszhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @bobvawter, @BramGruneir, @dt, and @knz)


pkg/sql/sqlbase/structured.go, line 2110 at r1 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

Could we disallow renaming constraints that are being added? We currently disallow deleting constraints being added: https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/alter_table.go#L466-L468

This is just for check constraints. I don't think FKs (and other constraints) have this problem, since I haven't made changes to FKs yet.

Copy link
Member

@bobvawter bobvawter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll second Lucy's point about dealing with renames in the face of a long-running schema change. I think it's certainly reasonable to kick back an error message to the user saying that the proposed change conflicts with a pending schema mutation and to retry later.

Reviewed 14 of 14 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @BramGruneir, @dt, and @knz)


pkg/sql/alter_table.go, line 609 at r1 (raw file):

		case *tree.AlterTableRenameColumn:
			descChanged, err := params.p.renameColumn(params.ctx, n.tableDesc, &t.Column, &t.NewName)

Does this need a privilege check like the constraint rename below, or is that handled elsewhere in the more general alter table flow?

Copy link
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @bobvawter, @BramGruneir, @dt, and @lucy-zhang)


pkg/sql/alter_table.go, line 609 at r1 (raw file):

Previously, bobvawter (Bob Vawter) wrote…

Does this need a privilege check like the constraint rename below, or is that handled elsewhere in the more general alter table flow?

The privilege check is made inside renameColumn().


pkg/sql/sqlbase/structured.go, line 2110 at r1 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

This is just for check constraints. I don't think FKs (and other constraints) have this problem, since I haven't made changes to FKs yet.

Ok thanks I have added the check. Can you hand-hold me as to where I can put a test for this? Thanks

@knz knz force-pushed the 20190220-rename branch 2 times, most recently from 6aefcaa to ba582d0 Compare February 26, 2019 21:38
Copy link
Contributor

@thoszhang thoszhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @bobvawter, @BramGruneir, @dt, and @knz)


pkg/sql/sqlbase/structured.go, line 2110 at r1 (raw file):

Previously, knz (kena) wrote…

Ok thanks I have added the check. Can you hand-hold me as to where I can put a test for this? Thanks

We have some logic tests for the case where the disallowed schema change happens in the same transaction as creating the constraint: https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/logictest/testdata/logic_test/schema_change_in_txn#L844-L895

There is also a similar test in schema_changer_test.go where we test that a column can't be dropped if there is a check constraint being added that references it: https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/schema_changer_test.go#L1244

…traints

Release note (sql change): This patch changes RENAME COLUMN to become
a "table command", which can be used alongside other table commands in
a single ALTER TABLE statement. This makes it possible to e.g. atomically
add a computed column based on an existing column, and rename the
columns so that the computed column "replaces" the original column.

Release note (sql change): CockroachDB now supports ALTER TABLE RENAME
CONSTRAINT. Only indexes that are not depended on by views can be
renamed.
Copy link
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @bobvawter, @BramGruneir, @dt, and @lucy-zhang)


pkg/sql/sqlbase/structured.go, line 2110 at r1 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

We have some logic tests for the case where the disallowed schema change happens in the same transaction as creating the constraint: https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/logictest/testdata/logic_test/schema_change_in_txn#L844-L895

There is also a similar test in schema_changer_test.go where we test that a column can't be dropped if there is a check constraint being added that references it: https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/schema_changer_test.go#L1244

Added the test. Thanks for your guidance!

@knz
Copy link
Contributor Author

knz commented Mar 7, 2019

bors r+

craig bot pushed a commit that referenced this pull request Mar 7, 2019
35091: sql: rearchitecture ALTER TABLE RENAME, add support for renaming constraints r=knz a=knz

Fixes #32555. For TypeORM compat, see discussion on #22298.

Release note (sql change): This patch changes RENAME COLUMN to become
a "table command", which can be used alongside other table commands in
a single ALTER TABLE statement. This makes it possible to e.g. atomically
add a computed column based on an existing column, and rename the
columns so that the computed column "replaces" the original column.

Release note (sql change): CockroachDB now supports ALTER TABLE RENAME
CONSTRAINT for compatibility with PostgreSQL. This feature is limited
to *named* constraints, where the name of the constraints is preserved
in the table metadata. This currently includes CHECK, UNIQUE and
FOREIGN KEY constraints, and does not include other
constraints (DEFAULT, NULL etc) otherwise supported by PostgreSQL. For
UNIQUE constraint, only supporting indexes that are not depended on by
views can be renamed.

35121: sql: add support for pg_catalog.{current_setting,set_config} r=knz a=knz

Fixes #35108. Needed for Flowable compatibility.

Release note (sql change): The SQL built-in functions
`pg_catalog.current_setting()` and `pg_catalog.set_config()` are now
supported for compatibility with PostgreSQL. Note that only
session-scoped configuration changes remain supported (`set_config(_,
_, false)`).

35359: roachtest: backup: use AOST time slightly in the past r=mjibson a=mjibson

Avoids problems where it's sometimes in the future according to the
AOST logic.

Fixes #34817

Release note: None

35371: sql: ensure column constraints are validated after SET for UPSERT r=knz a=knz

Fixes #35040.

Release note (bug fix): CockroachDB now properly applies column width
and nullability constraints on the result of conflict resolution in
UPSERT and INSERT ON CONFLICT.

Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
Co-authored-by: Matt Jibson <matt.jibson@gmail.com>
@craig
Copy link
Contributor

craig bot commented Mar 7, 2019

Build succeeded

@craig craig bot merged commit 11a7d81 into cockroachdb:master Mar 7, 2019
@knz knz deleted the 20190220-rename branch March 7, 2019 19:57
@knz knz added the docs-todo label Mar 7, 2019

query T
SELECT species FROM users
----
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rowsort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants