Repository navigation
Description
Bug Report
IvorySQL Version
commit 7261f1c
OS Version (uname -a)
Linux 6.8.0-87-generic #88-Ubuntu SMP PREEMPT_DYNAMIC x86_64 GNU/Linux
Configuration options (config.status --config)
'--prefix=/home/ivorysql/ivorysql' '--enable-debug' '--enable-cassert' '--with-uuid=e2fs' '--with-libxml' '--with-libxslt'
Current Behavior
When a PL/iSQL function contains both nested procedures and nested functions, using a nested function's return value in an assignment expression that also references an outer scope variable causes the outer scope variable to be reset to its initial value.
Specifically, when:
- A nested procedure modifies an outer scope variable (e.g.,
v_sum) - Then a nested function is called in an expression like
v_sum := v_sum + nested_func()
The value of v_sum on the right-hand side is evaluated as 0 (its initial value), not the value set by the procedure.
Example showing the bug:
CREATE OR REPLACE FUNCTION test_bug() RETURN NUMBER AS
v_sum NUMBER := 0;
PROCEDURE add_val(p_val NUMBER) IS
BEGIN
v_sum := v_sum + p_val;
END add_val;
FUNCTION get_30(p_val NUMBER) RETURN NUMBER IS
BEGIN
RETURN p_val * 3;
END get_30;
BEGIN
add_val(10); -- v_sum should be 10
v_sum := v_sum + get_30(10); -- v_sum should be 10 + 30 = 40
RETURN v_sum; -- Returns 30 instead of 40!
END;
/
SELECT test_bug() FROM dual; -- Returns 30 (WRONG - expected 40)Expected behavior/code
The function should return 40:
- After
add_val(10),v_sumshould be 10 get_30(10)returns 30v_sum := v_sum + get_30(10)should compute10 + 30 = 40- The function should return 40
Steps to reproduce
-- Connect to IvorySQL in Oracle compatibility mode (port 1521)
-- Test case 1: Bug reproduction
CREATE OR REPLACE FUNCTION test_bug() RETURN NUMBER AS
v_sum NUMBER := 0;
PROCEDURE add_val(p_val NUMBER) IS
BEGIN
v_sum := v_sum + p_val;
END add_val;
FUNCTION get_30(p_val NUMBER) RETURN NUMBER IS
BEGIN
RETURN p_val * 3;
END get_30;
BEGIN
add_val(10);
v_sum := v_sum + get_30(10);
RETURN v_sum;
END;
/
SELECT test_bug() FROM dual; -- Returns 30 (BUG), expected 40
-- Test case 2: Workaround using separate variable
CREATE OR REPLACE FUNCTION test_workaround() RETURN NUMBER AS
v_sum NUMBER := 0;
v_temp NUMBER := 0;
PROCEDURE add_val(p_val NUMBER) IS
BEGIN
v_sum := v_sum + p_val;
END add_val;
FUNCTION get_30(p_val NUMBER) RETURN NUMBER IS
BEGIN
RETURN p_val * 3;
END get_30;
BEGIN
add_val(10);
v_temp := get_30(10); -- Assign to separate variable first
v_sum := v_sum + v_temp;
RETURN v_sum;
END;
/
SELECT test_workaround() FROM dual; -- Returns 40 (CORRECT)
-- Test case 3: Calling function before procedure works correctly
CREATE OR REPLACE FUNCTION test_order() RETURN NUMBER AS
v_sum NUMBER := 0;
FUNCTION get_30(p_val NUMBER) RETURN NUMBER IS
BEGIN
RETURN p_val * 3;
END get_30;
PROCEDURE add_val(p_val NUMBER) IS
BEGIN
v_sum := v_sum + p_val;
END add_val;
BEGIN
v_sum := v_sum + get_30(10); -- v_sum = 30
add_val(10); -- v_sum = 40
RETURN v_sum;
END;
/
SELECT test_order() FROM dual; -- Returns 40 (CORRECT)
-- Cleanup
DROP FUNCTION test_bug;
DROP FUNCTION test_workaround;
DROP FUNCTION test_order;Additional context that can be helpful for identifying the problem
Investigation findings:
-
The bug only occurs when the nested function's return value is used in an expression that also references the outer scope variable being assigned (
v_sum := v_sum + func()). -
If the function's return value is assigned to a separate variable first, then added to
v_sum, the bug does not occur. -
If the nested function is called before the nested procedure, the bug does not occur.
-
The bug occurs regardless of whether the nested procedure/function have local variables initialized from parameters.
-
The outer scope variable appears to be reset to its initial value (not just to 0, but to whatever the variable was initialized to).
Test matrix:
| Scenario | Expected | Actual | Result |
|---|---|---|---|
| Nested function only | 30 | 30 | PASS |
| Nested procedures only | 30 | 30 | PASS |
| Procedure then function (expression) | 40 | 30 | FAIL |
| Function then procedure | 40 | 40 | PASS |
| Procedure then function (separate var) | 40 | 40 | PASS |
Suspected cause:
When evaluating the right-hand side of v_sum := v_sum + get_30(10), the call to the nested function get_30() appears to trigger re-initialization of the outer scope's datum values, causing v_sum to revert to its initial value of 0 before the expression is evaluated.
This may be related to how plisql_add_initdatums() or the nested function execution context handles the parent function's local variables.
Workaround:
Use a temporary variable to capture the nested function's return value:
v_temp := nested_func();
v_sum := v_sum + v_temp;Instead of:
v_sum := v_sum + nested_func(); -- BUG: v_sum gets resetMetadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
bigplaice commentedon Dec 8, 2025
Thanks for reporting this issue! We'll look into it.
fix: resolve variable reset bug in nested functions (IvorySQL#1124)
test: add regression tests for issue IvorySQL#1124
OreoYang commentedon Dec 16, 2025
Hi Rophy,
If you're interesting take the action you can leave
/assignin the comment, and the issue will be automatically assign the issue to you.rophy commentedon Dec 16, 2025
Any checklist on what I should do to close the issue and/or PR? Would like to have a real postgresql experts to review the code.
fix: resolve variable reset bug in nested functions (IvorySQL#1124)
test: add regression tests for issue IvorySQL#1124
OreoYang commentedon Dec 18, 2025
No action is required from you. Please give us more time review your PRs 😄
OreoYang commentedon Dec 19, 2025
Narrow down the root cause
fix(plisql): copy varlena values in nested function expressions (Ivor…
test: add regression tests for issue IvorySQL#1124
fix(plisql): copy varlena values in nested function expressions (Ivor…
test: add regression tests for issue IvorySQL#1124
test: add regression tests for issue IvorySQL#1124
fix(plisql): copy varlena values in nested function expressions (Ivor…
test: add regression tests for issue IvorySQL#1124
Merge pull request #1133 from rophy/fix/1124
fix(plisql): resolve variable reset bug in nested function expressions (