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: set index recommendations after planning but before execution #99081

Merged
merged 1 commit into from
Mar 23, 2023

Conversation

yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented Mar 20, 2023

This commit moves the call to set the index recommendations to be done
right after planning was completed. Previously, it was done after the
execution, but it makes more sense to do it after planning. This also
allows us to remove the check on the txn still being open.

This required clarifying how instrumentationHelper.indexRecs is used.
Previously, it was used for two purposes:

  • for recording recommendations to be included in the
    statement_statistics system table
  • for showing when executing EXPLAIN statement.

These two usages have somewhat different requirements, so this commit
splits them out into two different slices. This also allows us to reuse
the recommendations from the latter should we choose to generate the
recommendations for the former (previously, this would result in
redundant regeneration of the recommendations).

Epic: None

Release note: None

@yuzefovich yuzefovich requested review from mgartner and a team March 20, 2023 23:46
@blathers-crl
Copy link

blathers-crl bot commented Mar 20, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

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

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@yuzefovich
Copy link
Member Author

cc @ZhouXing19

@ZhouXing19
Copy link
Collaborator

Seems that these changes made TestExecBuild_explain failed, as now there are no index recommendations: 1 and CREATE INDEX messages for EXPLAIN (TYPES) SELECT * FROM t WHERE v > 123.

@yuzefovich
Copy link
Member Author

yuzefovich commented Mar 21, 2023

This required some more adjustments than I initially made, should be fixed now.

Copy link
Collaborator

@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.

:lgtm:

Reviewed 2 of 4 files at r2.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner and @yuzefovich)


pkg/sql/instrumentation.go line 795 at r2 (raw file):

		// If the statement is an EXPLAIN, then we might have already generated
		// the index recommendations. If so, we can skip generation here.
		if len(ih.explainIndexRecs) > 0 {

Should this check for ih.explainIndexRecs != nil instead of len > 0? (In other words, is it possible that EXPLAIN will have zero index recommendations?)

@mgartner
Copy link
Collaborator

pkg/sql/instrumentation.go line 795 at r2 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Should this check for ih.explainIndexRecs != nil instead of len > 0? (In other words, is it possible that EXPLAIN will have zero index recommendations?)

Ya I think a nil check would prevent duplicate work in the case there are no recommendations.

This commit moves the call to set the index recommendations to be done
right after planning was completed. Previously, it was done after the
execution, but it makes more sense to do it after planning. This also
allows us to remove the check on the txn still being open.

This required clarifying how `instrumentationHelper.indexRecs` is used.
Previously, it was used for two purposes:
- for recording recommendations to be included in the
`statement_statistics` system table
- for showing when executing EXPLAIN statement.

These two usages have somewhat different requirements, so this commit
splits them out into two different slices. This also allows us to reuse
the recommendations from the latter should we choose to generate the
recommendations for the former (previously, this would result in
redundant regeneration of the recommendations).

Epic: None

Release note: None
Copy link
Member Author

@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.

TFTRs!

bors r+

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

@craig
Copy link
Contributor

craig bot commented Mar 23, 2023

Build succeeded:

@craig craig bot merged commit 436613f into cockroachdb:master Mar 23, 2023
@yuzefovich yuzefovich deleted the move-index-recs branch March 23, 2023 03:33
@knz
Copy link
Contributor

knz commented Apr 2, 2023

I think this should be backported?

@yuzefovich
Copy link
Member Author

Hm, I don't think this needs to be backported - it's just a nice-to-have cleanup IMO. @mgartner WDYT?

michae2 added a commit to michae2/cockroach that referenced this pull request Jan 5, 2024
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) detatching the memo,
(2) optimizing with hypothetical indexes, and then (3) restoring the
detatched 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, expecially 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 added a commit to michae2/cockroach that referenced this pull request Jan 6, 2024
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) detatching the memo,
(2) optimizing with hypothetical indexes, and then (3) restoring the
detatched 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, expecially 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 added a commit to michae2/cockroach that referenced this pull request Jan 6, 2024
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.
craig bot pushed a commit that referenced this pull request Jan 6, 2024
117433: sql: do not re-run optbuild before collecting index recommendations r=DrewKimball a=michae2

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.

Co-authored-by: Michael Erickson <[email protected]>
blathers-crl bot pushed a commit that referenced this pull request Jan 6, 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.
blathers-crl bot pushed a commit that referenced this pull request Jan 6, 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.
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.

6 participants