-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
121109: sql: make tail-call optimization work with nested routines r=DrewKimball a=DrewKimball #### sql: defer tail-call identification until execbuilding This commit changes the way routine tail-calls are handled. Before, only PL/pgSQL sub-routines were considered as tail-calls, and this was determined by a `TailCall` property that was set during optbuilding. This approach was fragile, did not work for explicit tail-calls, and did not work well with nested routine calls in general. Now, tail-calls are determined after optimization, during execbuilding. This will allow explicit (user-specified) tail calls to be optimized. It also prevents inlining rules from causing correctness bugs, since the old `TailCall` property only applied to the original calling routine. See `ExtractTailCalls` for further details. The next commit will add additional testing. Informs cockroachdb#120916 Release note: None #### sql: add tests for tail-call property This commit adds tests for the `ExtractTailCalls` function from the previous commit, and adds a `tail-call` field to `UDFCall` expressions that are in tail-call position in another routine. It also adds a regression test for cockroachdb#120916. Informs cockroachdb#120916 Release note: None #### sql: check exception handler before applying TCO This commit finishes the tail-call optimization fix begun by the previous commits, by preventing TCO when it would lose the reference to the calling routine's exception handler. PL/pgSQL sub-routines always maintain a reference to their parent's exception handler, so this isn't a problem for them. However, explicit (user-specified) nested routines do not track the calling routine's exception handler. There is no release note because this bug hasn't appeared in any release. Fixes cockroachdb#120916 Release note: None Co-authored-by: Drew Kimball <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# LogicTest: !local-mixed-23.1 !local-mixed-23.2 | ||
|
||
# Regression test for #120916 - the nested routine is not in tail-call position, | ||
# and so cannot be a target for TCO. | ||
statement ok | ||
CREATE FUNCTION f_nested(x INT) RETURNS INT AS $$ | ||
BEGIN | ||
x := x * 2; | ||
RETURN x; | ||
END | ||
$$ LANGUAGE PLpgSQL; | ||
|
||
statement ok | ||
CREATE FUNCTION f() RETURNS RECORD AS $$ | ||
DECLARE | ||
a INT := -2; | ||
BEGIN | ||
a := f_nested(a); | ||
RAISE NOTICE 'here'; | ||
RETURN (a, -a); | ||
END | ||
$$ LANGUAGE PLpgSQL; | ||
|
||
query II | ||
SELECT * FROM f() AS g(x INT, y INT); | ||
---- | ||
-4 4 | ||
|
||
# Case with an exception handler on the parent routine. This prevents TCO, | ||
# since executing the child routine in the parent's context would lose track | ||
# of the exception handler. | ||
statement ok | ||
DROP FUNCTION f; | ||
DROP FUNCTION f_nested; | ||
|
||
statement ok | ||
CREATE FUNCTION f_nested() RETURNS INT AS $$ | ||
BEGIN | ||
RETURN 1//0; | ||
END | ||
$$ LANGUAGE PLpgSQL; | ||
|
||
statement ok | ||
CREATE FUNCTION f() RETURNS INT AS $$ | ||
BEGIN | ||
RETURN f_nested(); | ||
EXCEPTION WHEN division_by_zero THEN | ||
RETURN -1; | ||
END | ||
$$ LANGUAGE PLpgSQL; | ||
|
||
query I | ||
SELECT f(); | ||
---- | ||
-1 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.