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

Skip to content

Commit 6bd3753

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 61f8618 commit 6bd3753

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
@@ -1168,6 +1168,11 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
11681168
* This can happen, for example, where the body of the function is
11691169
* 'SELECT func2()', where func2 has the same composite return type as
11701170
* the function that's calling it.
1171+
*
1172+
* XXX Note that if rettype is RECORD, the IsBinaryCoercible check
1173+
* will succeed for any composite restype. For the moment we rely on
1174+
* runtime type checking to catch any discrepancy, but it'd be nice to
1175+
* do better at parse time.
11711176
*/
11721177
if (tlistlen == 1)
11731178
{

src/backend/optimizer/util/clauses.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,6 +3493,10 @@ evaluate_function(Oid funcid, Oid result_type, int32 result_typmod, List *args,
34933493
* We must also beware of changing the volatility or strictness status of
34943494
* functions by inlining them.
34953495
*
3496+
* Also, at the moment we can't inline functions returning RECORD. This
3497+
* doesn't work in the general case because it discards information such
3498+
* as OUT-parameter declarations.
3499+
*
34963500
* Returns a simplified expression if successful, or NULL if cannot
34973501
* simplify the function.
34983502
*/
@@ -3525,6 +3529,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
35253529
if (funcform->prolang != SQLlanguageId ||
35263530
funcform->prosecdef ||
35273531
funcform->proretset ||
3532+
funcform->prorettype == RECORDOID ||
35283533
!heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
35293534
funcform->pronargs != list_length(args))
35303535
return NULL;

src/test/regress/expected/rangefuncs.out

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

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

src/test/regress/sql/rangefuncs.sql

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

425425
drop function foobar();
426+
427+
-- check handling of a SQL function with multiple OUT params (bug #5777)
428+
429+
create or replace function foobar(out integer, out numeric) as
430+
$$ select (1, 2.1) $$ language sql;
431+
432+
select * from foobar();
433+
434+
create or replace function foobar(out integer, out numeric) as
435+
$$ select (1, 2) $$ language sql;
436+
437+
select * from foobar(); -- fail
438+
439+
create or replace function foobar(out integer, out numeric) as
440+
$$ select (1, 2.1, 3) $$ language sql;
441+
442+
select * from foobar(); -- fail
443+
444+
drop function foobar();

0 commit comments

Comments
 (0)
0