release-20.2: opt: fix FK cascades to child tables with partial indexes #57325
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport 1/1 commits from #57100.
/cc @cockroachdb/release
Previously, the optimizer was not synthesizing partial index DEL columns
for FK cascading updates and deletes. As a result, a cascading
UPDATE
could corrupt a child table's partial index, ultimately resulting in
incorrect query results. A cascading
DELETE
would not corruptpartial indexes, but unnecessary
DEL
operations would be issued onthe partial index.
The optbuilder has been refactored so that these columns are correctly
projected. There are now three functions for projecting PUT columns, DEL
columns, and both PUT and DEL columns, each ensuring that the input
scopes are non-nil. These three functions are called from principal
functions in the optbuilder where CHECK constraint columns are also
projected, like
mutationBuilder.buildUpdate
. In theory this shouldmake it harder in the future to omit these necessary projections.
Additionally, the execution engine was unable to handle extraneous
columns that can be added as input to FK cascading updates. These
extraneous columns would be incorrectly interpreted as synthesized
partial index columns. This commit works around this issue by slicing
the source values with an upper bound in
updateNode.processSourceRow
.The longer term fix is to not produce these columns (see issue #57097).
Fixes #57085
Fixes #57084
Release justification: This is a critical bug fix to a new feature,
partial indexes.
Release note (bug fix): A bug has been fixed that caused errors or
corrupted partial indexes of child tables in foreign key relationships
with cascading
UPDATE
s andDELETE
s. The corrupt partial indexescould result in incorrect query results. Any partial indexes on child
tables of foreign key relationships with
ON DELETE CASCADE
orON UPDATE CASCADE
actions may be corrupt and should be dropped andre-created. This bug was introduce in version 20.2.