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: alter primary key hangs if index references exist #124132

Merged
merged 1 commit into from
May 17, 2024

Conversation

fqazi
Copy link
Collaborator

@fqazi fqazi commented May 14, 2024

Previously, ALTER PRIMARY KEY could hang if an index reference existed from either a view or function using the index selection syntax. This was becasue neither the declarative schema changer or legacy schema changer support updating these references. To address this, this patch will prevent ALTER PRIMARY KEY if such references exist to any indexes that will be recreated.

Fixes: #123017
Release note (bug fix): ALTER PRIMARY KEY could hang if the table had any indexes which were referred to by views or functions using the force index syntax.

@fqazi fqazi requested a review from a team as a code owner May 14, 2024 15:53
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@fqazi fqazi added the backport-24.1.x Flags PRs that need to be backported to 24.1. label May 14, 2024
Copy link
Contributor

@annrpom annrpom 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 @fqazi)


pkg/sql/logictest/testdata/logic_test/alter_table line 3514 at r1 (raw file):

DROP INDEX t_108974_f@t_108974_f_i_j_idx;

skipif config local-legacy-schema-changer

nit: shouid we have to distinguish the error messages between the legacy vs declarative schema changer (i'm assuming that the order of error prop. is the diff between the two ? the dsc looks like it does it in disallowDroppingPrimaryIndexReferencedInUDFOrView)

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

nice! does it need to be backported to 23.2 also?

if err != nil {
return err
}
return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we use the unimplemented.NewWithIssue helper and point at the issue you filed?

statement error pgcode 55000 pq: table \"t_108974_f\" has an index \(t_108974_f_i_j_idx\) that is still referenced by \"f_108974\"
ALTER TABLE t_108974_f ALTER PRIMARY KEY USING COLUMNS (j);

onlyif config local-legacy-schema-changer
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this mean the legacy schema changer isn't actually affected? (since it disallows the alter PK for a different reason already?)

for _, function := range functions {
for _, tableRef := range function.UsesTables {
if tableRef.TableID == idx.TableID && tableRef.IndexID == idx.IndexID {
panic(pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we use the unimplemented.NewWithIssue helper and point at the issue you filed?

Copy link
Collaborator Author

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @annrpom and @rafiss)


pkg/sql/logictest/testdata/logic_test/alter_table line 3518 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

does this mean the legacy schema changer isn't actually affected? (since it disallows the alter PK for a different reason already?)

Only in the case of functions and only if there are PK references, not secondary indexes, but views are still broken.

Previously, ALTER PRIMARY KEY could hang if an index reference existed
from either a view or function using the index selection syntax. This
was becasue neither the declarative schema changer or legacy schema
changer support updating these references. To address this, this patch
will prevent ALTER PRIMARY KEY if such references exist to any indexes
that will be recreated.

Fixes: cockroachdb#123017

Release note (bug fix): ALTER PRIMARY KEY could hang if the table had
any indexes which were referred to by views or functions using the force
index syntax.
Copy link
Collaborator Author

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @annrpom and @rafiss)


pkg/sql/alter_primary_key.go line 465 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

should we use the unimplemented.NewWithIssue helper and point at the issue you filed?

Done.


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_primary_key.go line 700 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

should we use the unimplemented.NewWithIssue helper and point at the issue you filed?

Done.


pkg/sql/logictest/testdata/logic_test/alter_table line 3514 at r1 (raw file):

Previously, annrpom (annie pompa) wrote…

nit: shouid we have to distinguish the error messages between the legacy vs declarative schema changer (i'm assuming that the order of error prop. is the diff between the two ? the dsc looks like it does it in disallowDroppingPrimaryIndexReferencedInUDFOrView)

Good point, I moved the check to later in the legacy schema changer. The declarative one can't be modified because we do it once for the comma syntax.

@fqazi
Copy link
Collaborator Author

fqazi commented May 17, 2024

@annrpom @rafiss TFTR!

bors r+

@fqazi
Copy link
Collaborator Author

fqazi commented May 17, 2024

bors r+

@fqazi fqazi added the backport-23.2.x Flags PRs that need to be backported to 23.2. label May 17, 2024
@craig craig bot merged commit 855b9cc into cockroachdb:master May 17, 2024
21 of 22 checks passed
Copy link

blathers-crl bot commented May 17, 2024

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 575e6f5 to blathers/backport-release-23.2-124132: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.2.x failed. See errors above.


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

@yuzefovich
Copy link
Member

@fqazi friendly reminder to backport this to 23.2 branch - we just saw a roachtest failure due to this #124549.

@fqazi
Copy link
Collaborator Author

fqazi commented May 22, 2024

@yuzefovich Thanks for the reminder, let me get one open now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

schemachanger: unexpected behavior of ALTER PRIMARY KEY with hard-coded index in a UDF
5 participants