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

opt,plpgsql: subquery hoisting rules should not reorder PL/pgSQL subroutines #120451

Merged
merged 1 commit into from Mar 20, 2024

Conversation

DrewKimball
Copy link
Collaborator

@DrewKimball DrewKimball commented Mar 14, 2024

Due to #97432, it is possible for subquery-hoisting decorrelation rules to hoist a volatile subquery from a CASE expression. This can cause a query to display side effects which were meant to be gated behind a conditional expression, or else were meant to occur in a different order. This is a problem for PL/pgSQL, which relies on expressions being executed in a certain order. While #115826 added a Barrier expression to prevent rules from changing execution order, this doesn't work for hoisting rules that traverse an entire operator subtree, instead of relying on match-and-replace patterns.

This commit makes a targeted fix for PL/pgSQL routines by preventing subquery-hoisting rules from matching if a scalar expression contains a BarrierExpr or a UDFCall with TailCall = true. Either of these conditions indicates that changing execution order would cause incorrect results.

Fixes #120439

Release note (bug fix): Fixed a bug introduced in v23.2 that could cause a PL/pgSQL routine to return incorrect results when there was at least one parameter, and an IF statement with one leak-proof branch, and one branch with side effects.

@DrewKimball DrewKimball added the backport-23.2.x Flags PRs that need to be backported to 23.2. label Mar 14, 2024
@DrewKimball DrewKimball requested a review from a team as a code owner March 14, 2024 00:29
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@DrewKimball
Copy link
Collaborator Author

This is effectively a more targeted fix to #97432, which I hope to backport. It only applies to PL/pgSQL routines, so it should make a very safe backport.

Copy link
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.

The explanation sounds good to me, but I'll defer to Marcus for approval.

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


-- commits line 20 at r1:
nit: the linked number is a PR - is this intentional?

…outines

Due to cockroachdb#97432, it is possible for subquery-hoisting decorrelation rules
to hoist a volatile subquery from a CASE expression. This can cause a
query to display side effects which were meant to be gated behind a
conditional expression, or else were meant to occur in a different order.
This is a problem for PL/pgSQL, which relies on expressions being executed
in a certain order. While cockroachdb#115826 added a `Barrier` expression to prevent
rules from changing execution order, this doesn't work for hoisting rules
that traverse an entire operator subtree, instead of relying on
match-and-replace patterns.

This commit makes a targeted fix for PL/pgSQL routines by preventing
subquery-hoisting rules from matching if a scalar expression contains a
`BarrierExpr` or a `UDFCall` with `TailCall = true`. Either of these
conditions indicates that changing execution order would cause incorrect
results.

Fixes cockroachdb#120439

Release note (bug fix): Fixed a bug introduced in v23.2 that could cause
a PL/pgSQL routine to return incorrect results when there was at least
one parameter, and an `IF` statement with one leak-proof branch, and one
branch with side effects.
Copy link
Collaborator Author

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

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


-- commits line 20 at r1:

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: the linked number is a PR - is this intentional?

Not intentional, good catch.

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: But is this PR necessary with the fix for #97432 and by marking the necessary routines as volatile?

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


pkg/sql/opt/norm/decorrelate_funcs.go line 57 at r2 (raw file):

// deriveHasUnhoistableExpr checks for expressions within the given scalar
// expression which cannot be hoisted. This is necessary beyond existing
// volatility checks because of #97432: when a subquery-hoisting rule is

You have a fix for #97432, so does that make this PR obsolete?

Copy link
Collaborator Author

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

TFTRs!

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


pkg/sql/opt/norm/decorrelate_funcs.go line 57 at r2 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

You have a fix for #97432, so does that make this PR obsolete?

I do expect that to fix this, but I want to backport this change. The other fix probably isn't backportable unless we disable it by default.

@DrewKimball
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Mar 20, 2024

Build failed:

@DrewKimball
Copy link
Collaborator Author

Looks like a flake.

bors retry

@craig
Copy link
Contributor

craig bot commented Mar 20, 2024

@craig craig bot merged commit 207f8d5 into cockroachdb:master Mar 20, 2024
17 of 19 checks passed
@DrewKimball DrewKimball deleted the unhoistable branch March 20, 2024 09:43
@mgartner
Copy link
Collaborator

I do expect that to fix this, but I want to backport this change. The other fix probably isn't backportable unless we disable it by default.

Ok. So we'll revert this change on master then?

@DrewKimball
Copy link
Collaborator Author

Ok. So we'll revert this change on master then?

We could. I think I'd rather spend some time thinking about updating our optimizer restrictions on volatility first, though. Does that sound ok?

@mgartner
Copy link
Collaborator

We could. I think I'd rather spend some time thinking about updating our optimizer restrictions on volatility first, though. Does that sound ok?

Yes, absolutely, no need to rush it. Consider creating an issue to track this potential clean-up, if you think we might forget about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.2.x Flags PRs that need to be backported to 23.2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

plpgsql: decorrelation rules can re-arrange PL/pgSQL subroutines
4 participants