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/schemachanger: Prevent Temporary Disappearance of Altered Columns #134411

Merged
merged 1 commit into from
Nov 12, 2024

Conversation

spilchen
Copy link
Contributor

@spilchen spilchen commented Nov 6, 2024

Previously, during an ALTER COLUMN ... TYPE operation, the column being modified could briefly disappear, leading to "column does not exist" errors for any queries attempting to access it. This issue arose from the incorrect ordering of dependency rules, which has now been addressed.

The changes to the dependency rules during the column type alteration are as follows:

  • The name of the new column is not set until we are ready to make it public. Previously, the column name was assigned too early in the pre-commit phase, which necessitated additional shadow transient column name logic. This new approach simplifies the process and eliminates the need for that extra rename logic.
  • The ColumnName and Column elements for both the old and new columns are swapped within the same stage.

I have also maintained the existing rules for cases where the column type is not being altered by utilizing the IsAlterColumnTypeOp and IsNotAlterColumnTypeOp predicates.

I updated existing tests to query the column during each stage of the alter. This allowed me to reproduce the issue.

Epic: CRDB-25314
Closes #133996
Release note: none

@spilchen spilchen self-assigned this Nov 6, 2024
@spilchen spilchen requested a review from a team as a code owner November 6, 2024 14:32
Copy link

blathers-crl bot commented Nov 6, 2024

Your pull request contains more than 1000 changes. It is strongly encouraged to split big PRs into smaller chunks.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Please add the deps command to: pkg/sql/schemachanger/scplan/testdata/alter_table_alter_column_type so that we can confirm which rules are firing as well.

Reviewed 57 of 57 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @spilchen)


pkg/sql/schemachanger/scplan/internal/rules/current/dep_drop_column.go line 230 at r1 (raw file):

	// This rule is similar to the previous one but applies specifically to ALTER COLUMN ... TYPE operations.
	// It uses SameStagePrecedence to enable the swapping of dropped and added columns within the same stage.

Just as a sanity what happens to the plan without SameStagePrecedence?


pkg/sql/schemachanger/scplan/internal/rules/current/dep_add_column.go line 21 at r1 (raw file):

	registerDepRule(
		"column existence precedes column dependents",

Nit: could we reduce duplication here some how by having a helper function?

registerDepRule(
"column existence precedes column dependents",
append(ColumnExistencePrecedes(isColumnDependentExcept),
IsNotAlterColumnTypeOp("table-id", "col-id"))

@spilchen spilchen force-pushed the issue-133996/final-rev-rebase branch from 59a59b3 to f0cb08b Compare November 12, 2024 13:52
Copy link
Contributor Author

@spilchen spilchen left a comment

Choose a reason for hiding this comment

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

Thanks for taking a look.

Please add the deps command to: pkg/sql/schemachanger/scplan/testdata/alter_table_alter_column_type so that we can confirm which rules are firing as well.

I did have to update pkg/sql/schemachanger/scplan/testdata/alter_table_alter_column_type. You can see that I had to make changes to it because the column name is being set in a later stage.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @fqazi)


pkg/sql/schemachanger/scplan/internal/rules/current/dep_add_column.go line 21 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Nit: could we reduce duplication here some how by having a helper function?

registerDepRule(
"column existence precedes column dependents",
append(ColumnExistencePrecedes(isColumnDependentExcept),
IsNotAlterColumnTypeOp("table-id", "col-id"))

Done


pkg/sql/schemachanger/scplan/internal/rules/current/dep_drop_column.go line 230 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Just as a sanity what happens to the plan without SameStagePrecedence?

Without this change, the old column transitions out of the public state (i.e., becomes invisible to end users) in the previous stage, but the new column has not yet been made public. This causes any query for the column during that stage to fail with column "<col-name>" does not exist.

Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 3 of 3 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @spilchen)

@spilchen
Copy link
Contributor Author

TFTR @fqazi

bors r+

craig bot pushed a commit that referenced this pull request Nov 12, 2024
134411: sql/schemachanger: Prevent Temporary Disappearance of Altered Columns r=spilchen a=spilchen

Previously, during an ALTER COLUMN ... TYPE operation, the column being modified could briefly disappear, leading to "column does not exist" errors for any queries attempting to access it. This issue arose from the incorrect ordering of dependency rules, which has now been addressed.

The changes to the dependency rules during the column type alteration are as follows:
- The name of the new column is not set until we are ready to make it public. Previously, the column name was assigned too early in the pre-commit phase, which necessitated additional shadow transient column name logic. This new approach simplifies the process and eliminates the need for that extra rename logic.
- The ColumnName and Column elements for both the old and new columns are swapped within the same stage.

I have also maintained the existing rules for cases where the column type is not being altered by utilizing the IsAlterColumnTypeOp and IsNotAlterColumnTypeOp predicates.

I updated existing tests to query the column during each stage of the alter. This allowed me to reproduce the issue.

Epic: CRDB-25314
Closes #133996
Release note: none

Co-authored-by: Matt Spilchen <matt.spilchen@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented Nov 12, 2024

Build failed:

Previously, during an ALTER COLUMN ... TYPE operation, the column being
modified could briefly disappear, leading to "column does not exist" errors for
any queries attempting to access it. This issue arose from the incorrect
ordering of dependency rules, which has now been addressed.

The changes to the dependency rules during the column type alteration are as
follows:
- The name of the new column is not set until we are ready to make it public.
Previously, the column name was assigned too early in the pre-commit phase,
which necessitated additional shadow transient column name logic. This new
approach simplifies the process and eliminates the need for that extra rename
logic.
- The ColumnName and Column elements for both the old and new columns are
swapped within the same stage.

I have also maintained the existing rules for cases where the column type is
not being altered by utilizing the IsAlterColumnTypeOp and IsNotAlterColumnTypeOp
predicates.

I updated existing tests to query the column during each stage of the alter.
This allowed me to reproduce the issue.

Epic: CRDB-25314
Closes cockroachdb#133996
Release note: none
@spilchen spilchen force-pushed the issue-133996/final-rev-rebase branch from f0cb08b to 88a9c8e Compare November 12, 2024 17:55
@spilchen
Copy link
Contributor Author

bors r+

@craig craig bot merged commit c1a452d into cockroachdb:master Nov 12, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql/schemachanger: column can be invisible temporarily when altering the column type
3 participants