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: fix statement bundle creation when memo isn't detached #92952

Merged
merged 1 commit into from
Dec 6, 2022

Conversation

cucaroach
Copy link
Contributor

@cucaroach cucaroach commented Dec 2, 2022

When a memo is deemed not resuable we don't detach it from the factory
and this causes problems later when we execute SetIndexRecommendations
which resets the optimizer context which will reset the memo. This
causes the schema.sql and opt*.txt files to be empty.

Fixes: #92920

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@cucaroach cucaroach force-pushed the fix-stmt-bundle branch 2 times, most recently from 3ec07d7 to ec0ec64 Compare December 4, 2022 02:55
@cucaroach cucaroach changed the title wip sql: fix statement bundle creation when memo isn't detached Dec 5, 2022
@cucaroach cucaroach marked this pull request as ready for review December 5, 2022 00:12
@cucaroach cucaroach requested a review from a team as a code owner December 5, 2022 00:12
Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

Great find!

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @cucaroach)


pkg/sql/plan_opt.go line 510 at r1 (raw file):

//
// The returned memo is only safe to use in one thread, during execution of the
// current statement.

Do we need this warning anymore? (not that it did much good before...)


pkg/sql/opt/norm/factory.go line 179 at r1 (raw file):

	m := f.mem
	f.mem = nil
	f.Init(f.ctx, f.evalCtx, nil /* catalog */)

Do we need to call Init here? Would it be enough to just return m and set f.mem to nil?

Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

Nice!

Reviewed 2 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @cucaroach and @rytaft)


pkg/sql/opt/norm/factory.go line 179 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Do we need to call Init here? Would it be enough to just return m and set f.mem to nil?

Seems like Init shouldn't be necessary. Looks like opc.reset() is called whenever an optimizer factory is reused, which calls the factory's Init via the optimizer's Init. Even in DetachMemo above, f.Init looks redundant - maybe it was just for additional safety?


pkg/sql/explain_bundle_test.go line 109 at r1 (raw file):

		checkBundle(t, fmt.Sprint(rows), "", func(name, contents string) error {
			if name == "opt.txt" {
				if contents == "no plan" {

nit: can you create a constant for this string that's used in both pkg/sql/explain_bundle.go and this test? That provides a little bit more safety to prevent this test from becoming a test that always passes if someone changes the "no plan" text in pkg/sql/explain_bundle.go without updating this test.

When a memo is deemed not resuable we don't detach it from the factory
and this causes problems later when we execute SetIndexRecommendations
which resets the optimizer context which will reset the memo.  This
causes the schema.sql and opt*.txt files to be empty.

Fixes: cockroachdb#92920

Release note: None
Copy link
Contributor Author

@cucaroach cucaroach 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 (waiting on @mgartner and @rytaft)


pkg/sql/plan_opt.go line 510 at r1 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Do we need this warning anymore? (not that it did much good before...)

I'm not sure.


pkg/sql/opt/norm/factory.go line 179 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Seems like Init shouldn't be necessary. Looks like opc.reset() is called whenever an optimizer factory is reused, which calls the factory's Init via the optimizer's Init. Even in DetachMemo above, f.Init looks redundant - maybe it was just for additional safety?

👍


pkg/sql/explain_bundle_test.go line 109 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

nit: can you create a constant for this string that's used in both pkg/sql/explain_bundle.go and this test? That provides a little bit more safety to prevent this test from becoming a test that always passes if someone changes the "no plan" text in pkg/sql/explain_bundle.go without updating this test.

Good idea

Copy link
Collaborator

@rytaft rytaft 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 3 of 3 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @cucaroach and @mgartner)


pkg/sql/plan_opt.go line 510 at r1 (raw file):

Previously, cucaroach (Tommy Reilly) wrote…

I'm not sure.

Ok, no harm in leaving it I guess...


pkg/sql/opt/norm/factory.go line 175 at r2 (raw file):

// ReleaseMemo is just like DetachMemo but it doesn't call Detach on the memo
// preserving any statistics information for explain purposes (distinct, null
// count etc).

nit: update comment since now it also doesn't call Init anymore

@cucaroach
Copy link
Contributor Author

bors r+

Copy link
Collaborator

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

@craig
Copy link
Contributor

craig bot commented Dec 6, 2022

Build succeeded:

@craig craig bot merged commit c79c663 into cockroachdb:master Dec 6, 2022
@cucaroach cucaroach deleted the fix-stmt-bundle branch December 6, 2022 20:10
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: some statement bundles have empty opt.* and schema.sql files
4 participants