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: create partial index with inserts can fail #110216
Conversation
d21a2ee
to
62c146b
Compare
isSecondary = true | ||
} | ||
tempID := srcIdx.TemporaryIndexID | ||
newTempSpec.temporary = &scpb.TemporaryIndex{ | ||
Index: srcIdx, | ||
IsUsingSecondaryEncoding: isSecondary, | ||
EmbeddedExpr: expr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the indexSpec be marked as temporary instead of copying parts of the top level spec into the temporary section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @ecwall)
pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go
line 701 at r1 (raw file):
Previously, ecwall (Evan Wall) wrote…
Can the indexSpec be marked as temporary instead of copying parts of the top level spec into the temporary section?
So, I think you are suggesting that an indexSpec for a temporary is a copy with just a flag flipped. I think that works for some things, but then there are a bunch of rewrites that happen below to assign IDs. Also, the apply I think would need to generate a TemporryIndex element from the original/primary secondary index elements.
I think in an ideal world SecondaryIndex == TemporaryIndex in terms of content minus the encoding indicator. But we also can't poke at scpb since that's written to descriptors :(
@@ -279,6 +279,7 @@ message SecondaryIndex { | |||
message TemporaryIndex { | |||
Index embedded_index = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true]; | |||
bool is_using_secondary_encoding = 2; | |||
Expression embedded_expr = 3 [(gogoproto.nullable) = true]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add some comments around this new field
nit: if you really intend this to be embedded (via (gogoproto.embed) = true
), then consider changing the name.
# Each insert will be injected thrice per stage, so we should always, | ||
# see a count of 2. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add or modify the comments to help readers understand why a count of 2?
@@ -10,13 +10,14 @@ INSERT INTO t VALUES($stageKey + 1, 'b'); | |||
INSERT INTO t VALUES($stageKey + 2, 'c'); | |||
DELETE FROM t WHERE v = 'a' and k=$stageKey; | |||
INSERT INTO t VALUES($stageKey, 'a'); | |||
UPDATE t SET v='a' WHERE k > 0; | |||
UPDATE t SET v='a' WHERE k % 2 = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a note as to why % 2
is being used in favor of > 0
?
Previously, the temporary index created during CREATE INDEX with partial indexes did not correctly copy over the predicate. If an insert and a CREATE INDEX (for partial indexes) were run concurrently, the CREATE INDEX could fail since the temporary index would contain extra rows. Eventually, validation would fail. This patch copies the embedded expression for partial indexes to address this. Fixes: cockroachdb#110198 Release note (bug fix): CREATE INDEX for partial indexes could fail with "ERROR: duplicate key value violates unique constraint" if concurrent inserts happened simultaneously.
62c146b
to
5e8569d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 5 files at r2.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @chrisseto, @ecwall, and @Xiang-Gu)
pkg/sql/schemachanger/testdata/end_to_end/create_index/create_index.definition
line 13 at r1 (raw file):
Previously, chrisseto (Chris Seto) wrote…
Can you add a note as to why
% 2
is being used in favor of> 0
?
Done.
pkg/sql/schemachanger/testdata/end_to_end/create_index/create_index.definition
line 18 at r1 (raw file):
Previously, Xiang-Gu (Xiang Gu) wrote…
can we add or modify the comments to help readers understand why a count of 2?
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice finds!
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @chrisseto, @ecwall, and @Xiang-Gu)
@Xiang-Gu @chrisseto @rafiss @ecwall TFTR! bors r+ |
Build succeeded: |
Encountered an error creating backports. Some common things that can go wrong:
You might need to create your backport manually using the backport tool. error creating merge commit from 5e8569d to blathers/backport-release-23.1-110216: 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.1.x failed. See errors above. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
@fqazi @rafiss Any idea when this fix will be released? We have customers running into this issue duplicate key value violates unique constraint and having to disable it with what's shown below (customer is on
Once the customers do the above they are able to create the index. Are there any negative consequences to disabling the declarative schema changer in this scenario (creating indexes)? Can they safely move forward with
|
@daniel-crlabs The fix is already merged into 23.1.12 they just need to move up: #110584. Ideally they should only disable the declarative schema changer for CREATE INDEX (especially if they have larger data sets). |
Thank you, I'll let them know |
Previously, the temporary index created during CREATE INDEX with partial indexes did not correctly copy over the predicate. If an insert and a CREATE INDEX (for partial indexes) were run concurrently, the CREATE INDEX could fail since the temporary index would contain extra rows. Eventually, validation would fail. This patch copies the embedded expression for partial indexes to address this.
Fixes: #110198
Release note (bug fix): CREATE INDEX for partial indexes could fail with "ERROR: duplicate key value violates unique constraint" if concurrent inserts happened simultaneously.