Skip to content

Commit 225f0aa

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 c0b5fac commit 225f0aa

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/backend/executor/functions.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,11 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
11711171
* This can happen, for example, where the body of the function is
11721172
* 'SELECT func2()', where func2 has the same composite return type as
11731173
* the function that's calling it.
1174+
*
1175+
* XXX Note that if rettype is RECORD, the IsBinaryCoercible check
1176+
* will succeed for any composite restype. For the moment we rely on
1177+
* runtime type checking to catch any discrepancy, but it'd be nice to
1178+
* do better at parse time.
11741179
*/
11751180
if (tlistlen == 1)
11761181
{

src/backend/optimizer/util/clauses.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3677,6 +3677,10 @@ evaluate_function(Oid funcid, Oid result_type, int32 result_typmod, List *args,
36773677
* We must also beware of changing the volatility or strictness status of
36783678
* functions by inlining them.
36793679
*
3680+
* Also, at the moment we can't inline functions returning RECORD. This
3681+
* doesn't work in the general case because it discards information such
3682+
* as OUT-parameter declarations.
3683+
*
36803684
* Returns a simplified expression if successful, or NULL if cannot
36813685
* simplify the function.
36823686
*/
@@ -3709,6 +3713,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
37093713
if (funcform->prolang != SQLlanguageId ||
37103714
funcform->prosecdef ||
37113715
funcform->proretset ||
3716+
funcform->prorettype == RECORDOID ||
37123717
!heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
37133718
funcform->pronargs != list_length(args))
37143719
return NULL;

src/test/regress/expected/rangefuncs.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,3 +898,23 @@ select * from foobar();
898898
(2 rows)
899899

900900
drop function foobar();
901+
-- check handling of a SQL function with multiple OUT params (bug #5777)
902+
create or replace function foobar(out integer, out numeric) as
903+
$$ select (1, 2.1) $$ language sql;
904+
select * from foobar();
905+
column1 | column2
906+
---------+---------
907+
1 | 2.1
908+
(1 row)
909+
910+
create or replace function foobar(out integer, out numeric) as
911+
$$ select (1, 2) $$ language sql;
912+
select * from foobar(); -- fail
913+
ERROR: function return row and query-specified return row do not match
914+
DETAIL: Returned type integer at ordinal position 2, but query expects numeric.
915+
create or replace function foobar(out integer, out numeric) as
916+
$$ select (1, 2.1, 3) $$ language sql;
917+
select * from foobar(); -- fail
918+
ERROR: function return row and query-specified return row do not match
919+
DETAIL: Returned row contains 3 attributes, but query expects 2.
920+
drop function foobar();

src/test/regress/sql/rangefuncs.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,22 @@ select foobar();
429429
select * from foobar();
430430

431431
drop function foobar();
432+
433+
-- check handling of a SQL function with multiple OUT params (bug #5777)
434+
435+
create or replace function foobar(out integer, out numeric) as
436+
$$ select (1, 2.1) $$ language sql;
437+
438+
select * from foobar();
439+
440+
create or replace function foobar(out integer, out numeric) as
441+
$$ select (1, 2) $$ language sql;
442+
443+
select * from foobar(); -- fail
444+
445+
create or replace function foobar(out integer, out numeric) as
446+
$$ select (1, 2.1, 3) $$ language sql;
447+
448+
select * from foobar(); -- fail
449+
450+
drop function foobar();

0 commit comments

Comments
 (0)