Repository navigation
[2.1.0] Don't fold interpolated initializers into DECLARE; reject constants that read locals - #21
Open
OffgridwithJD wants to merge 1 commit into
Open
OffgridwithJD wants to merge 1 commit into
OffgridwithJD wants to merge 1 commit into
Conversation
…hat read locals
A local's first top-level assignment of a string literal is folded into
DECLARE as its initializer, which runs when the function starts. An
interpolating string was folded too, so it read its variables before the
statements above it ran: in plxjs, plxpython3, plxruby and plxphp,
'v = "v0"; v = "v2"; s = "got #{v}"' returned "got v0". Such an assignment
now runs in place; a plain literal is still folded.
A constant has the same timing by design (plpgsql CONSTANT), so a constant
computed from a local read the local's initial value. A constant's
initializer may now use only literals, parameters and earlier constants;
reading another local is rejected with an explanation.
Reported by the plxcsharp front end's review, which worked around the
first case by parenthesizing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two silent miscompiles. In both, generated code computes a value at function start that the source computes later. The plxcsharp review found the first one.
An interpolated string as a local's first value read stale values
A local's first top-level assignment of a string literal is folded into
DECLAREas its initializer. That initializer runs when the function starts. An interpolating string was folded too, so it read its variables before the statements above it had run:This affected plxjs (
`${v}`), plxpython3 (f"{v}"), plxruby ("#{v}") and plxphp ("$v"). Calls made inside an interpolation also ran early, before any statements above them. Such an assignment now runs in place. A plain literal is still folded.A constant computed from a local read its initial value
A constant becomes a plpgsql
CONSTANT, which is also computed at function start. Sov = 1; v = 2; c = v + 1 #:: int constgavec = 2. A constant may now use only literals, parameters and earlier constants. Reading another local is rejected atCREATE FUNCTION:The check scans the constant's generated SQL. It skips string literals, quoted identifiers, field names after
.and type names after::, and reports the first non-constant local it finds. The per-dialect chapters now document the rule.Compatibility
Code-only, so no
ALTER EXTENSIONis needed.Testing
make installcheck: all 13 suites pass on PG 18.4. CI covers 13 to 18.make differentialcheck: 0 unexplained.plx_features: the four dialects returngot v2; a constant built from a parameter and an earlier constant still folds, and so does a plain literal (listing shown).plx_errors: a constant that reads a local, in plxruby and plxjs, is rejected.🤖 Generated with Claude Code