8000 Prevent inlining a SQL function with multiple OUT parameters. · machack666/postgres@b0e2092 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0e2092

Browse files
committed
Prevent inlining a SQL function with multiple OUT parameters.
There were corner cases in which the planner would attempt to inline such a function, which would result in a failure at runtime due to loss of information about exactly what the result record type is. Fix by disabling inlining when the function's recorded result type is RECORD. There might be some sub-cases where inlining could still be allowed, but this is a simple and backpatchable fix, so leave refinements for another day. Per bug #5777 from Nate Carson. Back-patch to all supported branches. 8.1 happens to avoid a core-dump here, but it still does the wrong thing.
1 parent cfb6ac6 commit b0e2092

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/backend/executor/functions.c

+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,11 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
955955
* This can happen, for example, where the body of the function is
956956
* 'SELECT func2()', where func2 has the same return type as the
957957
* function that's calling it.
958+
*
959+
* XXX Note that if rettype is RECORD, the IsBinaryCoercible check
960+
* will succeed for any composite restype. For the moment we rely on
961+
* runtime type checking to catch any discrepancy, but it'd be nice to
962+
* do better at parse time.
958963
*/
959964
if (tlistlen == 1)
960965
{

src/backend/optimizer/util/clauses.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,10 @@ evaluate_function(Oid funcid, Oid result_type, List *args,
26682668
* We must also beware of changing the volatility or strictness status of
26692669
* functions by inlining them.
26702670
*
2671+
* Also, at the moment we can't inline functions returning RECORD. This
2672+
* doesn't work in the general case because it discards information such
2673+
* as OUT-parameter declarations.
2674+
*
26712675
* Returns a simplified expression if successful, or NULL if cannot
26722676
* simplify the function.
26732677
*/
@@ -2699,6 +2703,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
26992703
if (funcform->prolang != SQLlanguageId ||
27002704
funcform->prosecdef ||
27012705
funcform->proretset ||
2706+
funcform->prorettype == RECORDOID ||
27022707
funcform->pronargs != list_length(args))
27032708
return NULL;
27042709

src/test/regress/expected/rangefuncs.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,23 @@ select t.a, t, t.a from foo1(10000) t limit 1;
548548
(1 row)
549549

550550
drop function foo1(n integer);
551+
-- check handling of a SQL function with multiple OUT params (bug #5777)
552+
create or replace function foobar(out integer, out numeric) as
553+
$$ select (1, 2.1) $$ language sql;
554+
select * from foobar();
555+
column1 | column2
556+
---------+---------
557+
1 | 2.1
558+
(1 row)
559+
560+
create or replace function foobar(out integer, out numeric) as
561+
$$ select (1, 2) $$ language sql;
562+
select * from foobar(); -- fail
563+
ERROR: function return row and query-specified return row do not match
564+
DETAIL: Returned type integer at ordinal position 2, but query expects numeric.
565+
create or replace function foobar(out integer, out numeric) as
566+
$$ select (1, 2.1, 3) $$ language sql;
567+
select * from foobar(); -- fail
568+
ERROR: function return row and query-specified return row do not match
569+
DETAIL: Returned row contains 3 attributes, but query expects 2.
570+
drop function foobar();

src/test/regress/sql/rangefuncs.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,22 @@ reset work_mem;
274274
select t.a, t, t.a from foo1(10000) t limit 1;
275275

276276
drop function foo1(n integer);
277+
278+
-- check handling of a SQL function with multiple OUT params (bug #5777)
279+
280+
create or replace function foobar(out integer, out numeric) as
281+
$$ select (1, 2.1) $$ language sql;
282+
283+
select * from foobar();
284+
285+
create or replace function foobar(out integer, out numeric) as
286+
$$ select (1, 2) $$ language sql;
287+
288+
select * from foobar(); -- fail
289+
290+
create or replace function foobar(out integer, out numeric) as
291+
$$ select (1, 2.1, 3) $$ language sql;
292+
293+
select * from foobar(); -- fail
294+
295+
drop function foobar();

0 commit comments

Comments
 (0)
0