Skip to content

sql: do not rewrite UDF body statement slice while assigning placeholders #147187

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

Merged
merged 1 commit into from
May 28, 2025

Conversation

DrewKimball
Copy link
Collaborator

Previously, we accidentally modified the original slice that contains the body statements of a UDF while copying it during the placeholder assignment step. As a result, constant folding that occurred in one session could become visible in the query plan cache, causing incorrect results. This commit fixes the bug by copying the slice as well as the body statements.

This bug only applied to prepared statements, since we don't add plans with stable expressions to the plan cache outside of the prepare path.

Fixes #147186

Release note (bug fix): Fixed a bug that could cause stable expressions to be folded in cached query plans. The bug could cause stable expressions like current_setting to return the wrong result if used in a prepared statement. The bug was introduced in point releases v23.2.22, v24.1.14, v24.3.9, and v25.1.2, and the v25.2 alpha.

…ders

Previously, we accidentally modified the original slice that contains
the body statements of a UDF while copying it during the placeholder
assignment step. As a result, constant folding that occurred in one
session could become visible in the query plan cache, causing incorrect
results. This commit fixes the bug by copying the slice as well as the
body statements.

This bug only applied to prepared statements, since we don't add plans with
stable expressions to the plan cache outside of the prepare path.

Fixes cockroachdb#147186

Release note (bug fix): Fixed a bug that could cause stable expressions
to be folded in cached query plans. The bug could cause stable expressions
like `current_setting` to return the wrong result if used in a prepared
statement. The bug was introduced in point releases v23.2.22, v24.1.14,
v24.3.9, and v25.1.2, and the v25.2 alpha.
@DrewKimball DrewKimball requested a review from a team May 22, 2025 23:47
@DrewKimball DrewKimball requested a review from a team as a code owner May 22, 2025 23:47
@DrewKimball DrewKimball requested review from mgartner and removed request for a team May 22, 2025 23:47
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@DrewKimball DrewKimball added backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.3.x Flags PRs that need to be backported to 24.3 backport-25.1.x Flags PRs that need to be backported to 25.1 backport-25.2.x Flags PRs that need to be backported to 25.2 labels May 22, 2025
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: Nice catch!

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

@DrewKimball DrewKimball added backport-all Flags PRs that need to be backported to all supported release branches backport-25.2.1-rc labels May 27, 2025
@DrewKimball
Copy link
Collaborator Author

TFTR!

bors r=mgartner

@craig
Copy link
Contributor

craig bot commented May 28, 2025

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. backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.3.x Flags PRs that need to be backported to 24.3 backport-25.1.x Flags PRs that need to be backported to 25.1 backport-25.2.x Flags PRs that need to be backported to 25.2 backport-all Flags PRs that need to be backported to all supported release branches v25.3.0-prerelease
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: assigning placeholders overwrites UDF body statements
3 participants