Skip to content

Commit 8a6eb2e

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 35a3def commit 8a6eb2e

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/backend/executor/functions.c

+5
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,11 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
949949
* This can happen, for example, where the body of the function is
950950
* 'SELECT func2()', where func2 has the same return type as the
951951
* function that's calling it.
952+
*
953+
* XXX Note that if rettype is RECORD, the IsBinaryCoercible check
954+
* will succeed for any composite restype. For the moment we rely on
955+
* runtime type checking to catch any discrepancy, but it'd be nice to
956+
* do better at parse time.
952957
*/
953958
if (tlistlen == 1)
954959
{

src/backend/optimizer/util/clauses.c

+5
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,10 @@ evaluate_function(Oid funcid, Oid result_type, List *args,
22892289
* We must also beware of changing the volatility or strictness status of
22902290
* functions by inlining them.
22912291
*
2292+
* Also, at the moment we can't inline functions returning RECORD. This
2293+
* doesn't work in the general case because it discards information such
2294+
* as OUT-parameter declarations.
2295+
*
22922296
* Returns a simplified expression if successful, or NULL if cannot
22932297
* simplify the function.
22942298
*/
@@ -2320,6 +2324,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
23202324
if (funcform->prolang != SQLlanguageId ||
23212325
funcform->prosecdef ||
23222326
funcform->proretset ||
2327+
funcform->prorettype == RECORDOID ||
23232328
funcform->pronargs != list_length(args))
23242329
return NULL;
23252330

src/test/regress/expected/rangefuncs.out

+20
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,23 @@ CREATE FUNCTION bad (f1 int, out f2 anyelement, out f3 anyarray)
528528
AS 'select $1, array[$1,$1]' LANGUAGE sql;
529529
ERROR: cannot determine result data type
530530
DETAIL: A function returning "anyarray" or "anyelement" must have at least one argument of either type.
531+
-- check handling of a SQL function with multiple OUT params (bug #5777)
532+
create or replace function foobar(out integer, out numeric) as
533+
$$ select (1, 2.1) $$ language sql;
534+
select * from foobar();
535+
column1 | column2
536+
---------+---------
537+
1 | 2.1
538+
(1 row)
539+
540+
create or replace function foobar(out integer, out numeric) as
541+
$$ select (1, 2) $$ language sql;
542+
select * from foobar(); -- fail
543+
ERROR: function return row and query-specified return row do not match
544+
DETAIL: Returned type integer at ordinal position 2, but query expects numeric.
545+
create or replace function foobar(out integer, out numeric) as
546+
$$ select (1, 2.1, 3) $$ language sql;
547+
select * from foobar(); -- fail
548+
ERROR: function return row and query-specified return row do not match
549+
DETAIL: Returned row contains 3 attributes, but query expects 2.
550+
drop function foobar();

src/test/regress/sql/rangefuncs.sql

+19
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,22 @@ DROP FUNCTION dup(anyelement);
261261
-- fails, no way to deduce outputs
262262
CREATE FUNCTION bad (f1 int, out f2 anyelement, out f3 anyarray)
263263
AS 'select $1, array[$1,$1]' LANGUAGE sql;
264+
265+
-- check handling of a SQL function with multiple OUT params (bug #5777)
266+
267+
create or replace function foobar(out integer, out numeric) as
268+
$$ select (1, 2.1) $$ language sql;
269+
270+
select * from foobar();
271+
272+
create or replace function foobar(out integer, out numeric) as
273+
$$ select (1, 2) $$ language sql;
274+
275+
select * from foobar(); -- fail
276+
277+
create or replace function foobar(out integer, out numeric) as
278+
$$ select (1, 2.1, 3) $$ language sql;
279+
280+
select * from foobar(); -- fail
281+
282+
drop function foobar();

0 commit comments

Comments
 (0)