Skip to content

sql: do not re-run optbuild before collecting index recommendations#117433

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
michae2:117307
Jan 6, 2024
Merged

sql: do not re-run optbuild before collecting index recommendations#117433
craig[bot] merged 1 commit intocockroachdb:masterfrom
michae2:117307

Conversation

@michae2
Copy link
Copy Markdown
Collaborator

@michae2 michae2 commented Jan 5, 2024

In #85343 for 22.2 we added automatic collection of index recommendations for DML statements. This collection occurred after execution, and initially re-ran optbuild for the query, before doing the usual index recommendation generation steps of (1) detaching the memo, (2) optimizing with hypothetical indexes, and then (3) restoring the detached memo.

In #99081 for 23.2 we moved collection of index recommendations from after execution to between planning and execution, which simplified some things. But this revealed that some queries refer to the memo (and its metadata) during execution, and re-running optbuild can change the contents of the memo (and its metadata) from how they were after planning, especially if the original memo was cached and re-used.

I think this initial optbuild step was added to ensure we always have a root expression in the memo. This commit changes index recommendation collection to only run optbuild if the memo is empty, and otherwise use the memo that comes out of planning.

Fixes: #117307

Release note (bug fix): Fix a bug introduced in 23.2 that causes internal errors and panics when certain queries run with automatic index recommendation collection enabled.

@michae2 michae2 requested review from a team, DrewKimball and yuzefovich January 5, 2024 22:24
@michae2 michae2 requested a review from a team as a code owner January 5, 2024 22:24
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@michae2 michae2 added backport-23.2.x PAST MAINTENANCE SUPPORT: 23.2 patch releases via ER request only backport-23.2.0-rc labels Jan 5, 2024
@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Jan 5, 2024

This is broken, hold off for a bit...

@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Jan 5, 2024

Ok, should be RFAL now.

Copy link
Copy Markdown
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm: once the nil check is fixed.

Great job figuring this out! I hope we can get rid of some of these pesky memo references soon.

Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2 and @yuzefovich)


pkg/sql/instrumentation.go line 1048 at r1 (raw file):

			opc := &planner.optPlanningCtx
			f := opc.optimizer.Factory()
			if f.Memo().IsEmpty() {

I think this should probably include a nil check - ReleaseMemo leaves the memo nil, and is called in buildExecMemo.

Copy link
Copy Markdown
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Nice job tracking this down! Looks good to me, but I defer to Drew's approval.

Reviewed 3 of 4 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @michae2)


-- commits line 7 at r2:
nit: s/detatching/detaching/ and s/detatched/detached/ below.

In cockroachdb#85343 for 22.2 we added automatic collection of index
recommendations for DML statements. This collection occurred after
execution, and initially re-ran optbuild for the query, before doing the
usual index recommendation generation steps of (1) detaching the memo,
(2) optimizing with hypothetical indexes, and then (3) restoring the
detached memo.

In cockroachdb#99081 for 23.2 we moved collection of index recommendations from
after execution to between planning and execution, which simplified some
things. But this revealed that some queries refer to the memo (and its
metadata) during execution, and re-running optbuild can change the
contents of the memo (and its metadata) from how they were after
planning, especially if the original memo was cached and re-used.

I think this initial optbuild step was added to ensure we always have a
root expression in the memo. This commit changes index recommendation
collection to only run optbuild if the memo is empty, and otherwise use
the memo that comes out of planning.

Fixes: cockroachdb#117307

Release note (bug fix): Fix a bug introduced in 23.2 that causes
internal errors and panics when certain queries run with automatic index
recommendation collection enabled.
@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Jan 6, 2024

TFTRs!

bors r=DrewKimball

Copy link
Copy Markdown
Collaborator Author

@michae2 michae2 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 (and 1 stale) (waiting on @DrewKimball and @yuzefovich)


-- commits line 7 at r2:

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: s/detatching/detaching/ and s/detatched/detached/ below.

Oh! Good Catch.


pkg/sql/instrumentation.go line 1048 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

I think this should probably include a nil check - ReleaseMemo leaves the memo nil, and is called in buildExecMemo.

Good call! Thank you!

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jan 6, 2024

Build succeeded:

@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Jan 6, 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 setting reviewers, but backport branch blathers/backport-release-23.2-117433 is ready: POST https://api.github.com/repos/cockroachdb/cockroach/pulls/117453/requested_reviewers: 422 Reviews may only be requested from collaborators. One or more of the teams you specified is not a collaborator of the cockroachdb/cockroach repository. []

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


error setting reviewers, but backport branch blathers/backport-release-23.2.0-rc-117433 is ready: POST https://api.github.com/repos/cockroachdb/cockroach/pulls/117454/requested_reviewers: 422 Reviews may only be requested from collaborators. One or more of the teams you specified is not a collaborator of the cockroachdb/cockroach repository. []

Backport to branch 23.2.0-rc failed. See errors above.


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-23.2.x PAST MAINTENANCE SUPPORT: 23.2 patch releases via ER request only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

execbuilder: index out of range panic in mkFKCheckErr

4 participants