You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
144347: plpgsql: fix ordinal variable references and FOR loops r=yuzefovich,rytaft a=DrewKimball
#### plpgsql: avoid redundant tuple when fetching into tuple var
Previously, a FETCH statement redundantly called `projectRecordVar`
when assigning a cursor result into a single composite-typed variable.
The resulting nested tuple was then unwrapped and assigned into the
variable.
This commit removes the extra step, and just directly assigns the FETCH
tuple into the composite-typed variable. This also fixes a bug with no
known visible effects, where we were incorrectly using the type of the
variable for the column projecting the nested tuple (it should have
been a tuple with a single element of the variable's type).
Epic: None
Release note: None
#### plpgsql: fix parameter ordinal references
Previously, variable assignment in PL/pgSQL routines did not update
the `paramOrd` field. As a result, referencing a variable via the
`$1` ordinal syntax did not always reflect updates to the variable.
In addition, it was possible to reference variables declared in blocks
within the routine body, rather than just the original routine parameters.
This commit fixes both issues by always setting `paramOrd` and setting
a `maxParamOrd` while building SQL expressions and statements, which
prevents the user from referencing block variables via ordinal syntax.
We use a `maxParamOrd` instead of simply not setting `paramOrd` for
block variables to allow the builder to internally reference any
variable with ordinal syntax.
Fixes#143887
Release note (bug fix): Fixed a bug existing since v24.1 that prevented
variable references using ordinal syntax (like `$1`) from reflecting
updates to the variable. Referencing variables declared in PL/pgSQL
blocks (instead of parameters) via ordinal syntax is now disallowed.
#### plpgsql: reference hidden variables only by ordinal
Hidden variables cannot be referenced by the user, and are used internally
to implement FOR LOOP bounds and counters. Previously, hidden variables
were referenced via metadata name. This was fragile, and in fact had
a bug because different loops used the same hidden variables names,
leading to incorrect results for nested loops.
This commit fixes the bug by tracking hidden variables by their ordinal
position among all variables in the current scope. The name of a hidden
variable is now only used for display. This ensures that a hidden variable
can always be uniquely identified, even within nested PL/pgSQL constructs.
Fixes#144321
Release note (bug fix): Fixed a bug existing since v24.3 that could cause
PL/pgSQL FOR loops to terminate early or show incorrect values for the
counter variable. The bug occurred when two or more FOR loops were nested
within one another.
Co-authored-by: Drew Kimball <[email protected]>
0 commit comments