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: don't store primary key col when rewriting secondary indexes during ALTER PK #115214

Conversation

Xiang-Gu
Copy link
Contributor

Fixes #114758

Release note (bug fix): Previously, if we have a table with secondary indexes that store some columns col via the STORING clause, and we alter primary key to col on this table, then we'd end up with incorrect secondary index where it continues to have the STORING clause, even though that column is already in the primary key and we don't allow creating a secondary index storing any primary key columns. This commit fixes that such that after the ALTER PK, the STORING clause is dropped on those secondary indexes.

…ing ALTER PK

Release note (bug fix): Previously, if we have a table with secondary
indexes that store some columns `col` via the `STORING` clause, and
we alter primary key to `col` on this table, then we'd end up with
incorrect secondary index where it continues to have the `STORING`
clause, even though that column is already in the primary key and we
don't allow creating a secondary index storing any primary key columns.
This commit fixes that such that after the ALTER PK, the `STORING` clause
is dropped on those secondary indexes.
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@Xiang-Gu Xiang-Gu marked this pull request as ready for review November 29, 2023 16:57
@Xiang-Gu Xiang-Gu requested a review from a team as a code owner November 29, 2023 16:57
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.

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


-- commits line 7 at r1:
Out of curiosity does that mean we store columns twice when we do inserts in both the key / value? If we aren't doing that maybe this warrants having a automated repair + validation as a separate PR (that shouldn't be back ported.

@Xiang-Gu
Copy link
Contributor Author

Out of curiosity does that mean we store columns twice when we do inserts in both the key / value?

I assume what you meant is after this fix, the secondary index will have col in only KeySuffixColumn but not in StoredColumn anymore, in which case the column is stored in key (for non-unique secondary indexes) and in value (for unique secondary indexes), but never both.

f we aren't doing that maybe this warrants having a automated repair + validation as a separate PR (that shouldn't be back ported.

I do think it's a good idea to add a validation for it (basically, we'd assert that "for any secondary index, any primary key column will either be its key column or its key suffix column", and that "for any secondary index, no stored columns should be in primary key columns".

@Xiang-Gu
Copy link
Contributor Author

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Nov 29, 2023

Build succeeded:

@craig craig bot merged commit bad0633 into cockroachdb:master Nov 29, 2023
3 checks passed
@Xiang-Gu Xiang-Gu deleted the bug/alter-pk-to-a-stored-column-by-an-secondary-index branch November 30, 2023 15:29
Xiang-Gu added a commit to Xiang-Gu/cockroach that referenced this pull request Dec 7, 2023
Previously, we encountered bugs (cockroachdb#115214) that lead to corrupted index
descriptor where it stores a primary key column. This commit added a
validation logic to ensure such corruption is caught.

Release note: None
Xiang-Gu added a commit to Xiang-Gu/cockroach that referenced this pull request Dec 12, 2023
Previously, we encountered bugs (cockroachdb#115214) that lead to corrupted index
descriptor where it stores a primary key column. This commit added a
validation logic to ensure such corruption is caught. This commit also
comes with a test to ensure the validation is silenced in a mixed
version state so that existing corruption won't disturb any SQL workload
but cluster version upgrade is indeed blocked.

Release note: None
craig bot pushed a commit that referenced this pull request Dec 13, 2023
115048: roachtest : add --use-spot option r=BabuSrithar a=BabuSrithar

This options enables use of SpotVM to run tests, If the provider does not support spotVM, it will be ignored

Epic: none

Release note: None

115558: tabledesc: validate that primary key columns are not stored in an index r=Xiang-Gu a=Xiang-Gu

Fixes #115293

Previously, we encountered bugs (#115214) that lead to corrupted index
descriptor where it stores a primary key column. This commit added a
validation logic to ensure such corruption is caught.

Release note: None

116226: kvserver: skip swap voters with non-voters under deadlock r=arulajmani a=kvoli

Liveness heartbeats may fail under deadlock builds with large test clusters, causing the test to time out waiting for suspect stores to become eligible targets.

Skip `TestReplicateQueueSwapVotersWithNonVoters` under deadlock builds.

Release note: None
Resolves: #116225

116283: bazel: upgrade `rules_go` to v0.43 r=rail,dt a=rickystewart

Also update core dependencies that must be updated for the new `rules_go` version.

Informs #99988

Epic: none
Release note: None

116285: roachtest: move admission control to storage team r=nicktrav,aadityasondhi a=kvoli

Storage now owns AC. Update the `TEAMS.yaml` so roachtest failures are filed under `T-storage`, instead of `T-kv`.

Epic: none
Release note: None

Co-authored-by: babusrithar <[email protected]>
Co-authored-by: Xiang Gu <[email protected]>
Co-authored-by: Austen McClernon <[email protected]>
Co-authored-by: Ricky Stewart <[email protected]>
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.

CRDB incorrectly rewrite secondary indexes if ALTER PK to a column that is stored in that index
3 participants