Skip to content

opt: fix SimplifyPartialIndexProjections#74431

Merged
craig[bot] merged 1 commit into
cockroachdb:masterfrom
mgartner:74385-fix-partial-index-corruption
Jan 5, 2022
Merged

opt: fix SimplifyPartialIndexProjections#74431
craig[bot] merged 1 commit into
cockroachdb:masterfrom
mgartner:74385-fix-partial-index-corruption

Conversation

@mgartner

@mgartner mgartner commented Jan 4, 2022

Copy link
Copy Markdown
Contributor

Previously, the SimplifyPartialIndexProjections rule incorrectly
simplified partial index PUT and DEL columns that were reused for
multiple partial indexes. This caused index corruption which could
result in incorrect query results.

For example, consider:

CREATE TABLE t (
  a INT,
  b INT,
  c INT,
  INDEX a_idx (a) WHERE c IS NULL,
  INDEX b_idx (b) WHERE c IS NULL
)

UPDATE t SET a = NULL

In the UPDATE, a single column will be synthesized for the PUT and DEL
columns of both partial indexes. The UPDATE does not mutate columns in
b_idx, so the synthesized column was simplified to false. However, this
caused index corruption of a_idx because a_idx contains mutating columns
and it may require writes.

The rule has been updated so that synthesized PUT and DEL columns are
only simplified to false if they are used for a single partial index.

Fixes #74385

Release note (bug fix): A bug has been fixed which caused corruption of
partial indexes, which could cause incorrect query results. The bug was
only present when two or more partial indexes in the same table had
identical WHERE clauses. This bug has been present since version
21.1.0.

@mgartner mgartner requested review from a team, rharding6373 and rytaft January 4, 2022 22:16
@mgartner mgartner requested a review from a team as a code owner January 4, 2022 22:16
@cockroach-teamcity

Copy link
Copy Markdown
Member

This change is Reviewable

@rytaft rytaft left a comment

Copy link
Copy Markdown
Collaborator

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 r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner and @rharding6373)


pkg/sql/opt/norm/mutation_funcs.go, line 111 at r1 (raw file):

}

// multiUsePartialIndexCols returns the set columns that are used as PUT or DEL

nit: set columns -> set of columns


pkg/sql/opt/norm/mutation_funcs.go, line 125 at r1 (raw file):

		delColUsedAgain := false

		for j := range mp.PartialIndexPutCols {

can't you just do:

for j := 0; j < i; j++ {

Previously, the SimplifyPartialIndexProjections rule incorrectly
simplified partial index PUT and DEL columns that were reused for
multiple partial indexes. This caused index corruption which could
result in incorrect query results.

For example, consider:

    CREATE TABLE t (
      a INT,
      b INT,
      c INT,
      INDEX a_idx (a) WHERE c IS NULL,
      INDEX b_idx (b) WHERE c IS NULL
    )

    UPDATE t SET a = NULL

In the UPDATE, a single column will be synthesized for the PUT and DEL
columns of both partial indexes. The UPDATE does not mutate columns in
b_idx, so the synthesized column was simplified to false. However, this
caused index corruption of a_idx because a_idx contains mutating columns
and it may require writes.

The rule has been updated so that synthesized PUT and DEL columns are
only simplified to false if they are used for a single partial index.

Fixes cockroachdb#74385

Release note (bug fix): A bug has been fixed which caused corruption of
partial indexes, which could cause incorrect query results. The bug was
only present when two or more partial indexes in the same table had
identical `WHERE` clauses. This bug has been present since version
21.1.0.
@mgartner mgartner force-pushed the 74385-fix-partial-index-corruption branch from 003b7f1 to a5f7f26 Compare January 5, 2022 16:32

@mgartner mgartner left a comment

Copy link
Copy Markdown
Contributor Author

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 (and 1 stale) (waiting on @rharding6373 and @rytaft)


pkg/sql/opt/norm/mutation_funcs.go, line 111 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: set columns -> set of columns

Done.


pkg/sql/opt/norm/mutation_funcs.go, line 125 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

can't you just do:

for j := 0; j < i; j++ {

Good point. Done.

@rharding6373 rharding6373 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm: Thanks for the quick fix!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @rytaft)

@mgartner

mgartner commented Jan 5, 2022

Copy link
Copy Markdown
Contributor Author

TFTRs!

bors r+

@craig

craig Bot commented Jan 5, 2022

Copy link
Copy Markdown
Contributor

Build succeeded:

@craig craig Bot merged commit ced0dd4 into cockroachdb:master Jan 5, 2022
@blathers-crl

blathers-crl Bot commented Jan 5, 2022

Copy link
Copy Markdown

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 a5f7f26 to blathers/backport-release-21.1-74431: 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 21.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@mgartner mgartner deleted the 74385-fix-partial-index-corruption branch January 5, 2022 20:46
@RaduBerinde

Copy link
Copy Markdown
Member

What an amazing find! Awesome job tracking this down!

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: partial index corruption

5 participants