]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Prevent inlining a SQL function with multiple OUT parameters.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 1 Dec 2010 05:53:44 +0000 (00:53 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 1 Dec 2010 05:53:44 +0000 (00:53 -0500)
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.

src/backend/executor/functions.c
src/backend/optimizer/util/clauses.c
src/test/regress/expected/rangefuncs.out
src/test/regress/sql/rangefuncs.sql

index 84398e8a9602f1f55edfffdda216079dab231baf..f974febeee5effe932fab9aa7dcea1acf14a30e8 100644 (file)
@@ -949,6 +949,11 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
                 * This can happen, for example, where the body of the function is
                 * 'SELECT func2()', where func2 has the same return type as the
                 * function that's calling it.
+                *
+                * XXX Note that if rettype is RECORD, the IsBinaryCoercible check
+                * will succeed for any composite restype.  For the moment we rely on
+                * runtime type checking to catch any discrepancy, but it'd be nice to
+                * do better at parse time.
                 */
                if (tlistlen == 1)
                {
index 7029ee1738ddbbb19d96f19db1590a78cee5717a..bc5df1f6f97f506e3d98c5e0be99f0c67d1872bb 100644 (file)
@@ -2289,6 +2289,10 @@ evaluate_function(Oid funcid, Oid result_type, List *args,
  * We must also beware of changing the volatility or strictness status of
  * functions by inlining them.
  *
+ * Also, at the moment we can't inline functions returning RECORD.  This
+ * doesn't work in the general case because it discards information such
+ * as OUT-parameter declarations.
+ *
  * Returns a simplified expression if successful, or NULL if cannot
  * simplify the function.
  */
@@ -2320,6 +2324,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
        if (funcform->prolang != SQLlanguageId ||
                funcform->prosecdef ||
                funcform->proretset ||
+               funcform->prorettype == RECORDOID ||
                funcform->pronargs != list_length(args))
                return NULL;
 
index 49fc121f04f72c3819a206e6dda4a90cb9263546..a043bc1d338ef2777f04c01e9822f761adadba83 100644 (file)
@@ -528,3 +528,23 @@ CREATE FUNCTION bad (f1 int, out f2 anyelement, out f3 anyarray)
 AS 'select $1, array[$1,$1]' LANGUAGE sql;
 ERROR:  cannot determine result data type
 DETAIL:  A function returning "anyarray" or "anyelement" must have at least one argument of either type.
+-- check handling of a SQL function with multiple OUT params (bug #5777)
+create or replace function foobar(out integer, out numeric) as
+$$ select (1, 2.1) $$ language sql;
+select * from foobar();
+ column1 | column2 
+---------+---------
+       1 |     2.1
+(1 row)
+
+create or replace function foobar(out integer, out numeric) as
+$$ select (1, 2) $$ language sql;
+select * from foobar();  -- fail
+ERROR:  function return row and query-specified return row do not match
+DETAIL:  Returned type integer at ordinal position 2, but query expects numeric.
+create or replace function foobar(out integer, out numeric) as
+$$ select (1, 2.1, 3) $$ language sql;
+select * from foobar();  -- fail
+ERROR:  function return row and query-specified return row do not match
+DETAIL:  Returned row contains 3 attributes, but query expects 2.
+drop function foobar();
index 50495897acd2adba59cee5f9cf86abb9a52af44e..06bbd1b537d17e144d5912506d53593668d64e99 100644 (file)
@@ -261,3 +261,22 @@ DROP FUNCTION dup(anyelement);
 -- fails, no way to deduce outputs
 CREATE FUNCTION bad (f1 int, out f2 anyelement, out f3 anyarray)
 AS 'select $1, array[$1,$1]' LANGUAGE sql;
+
+-- check handling of a SQL function with multiple OUT params (bug #5777)
+
+create or replace function foobar(out integer, out numeric) as
+$$ select (1, 2.1) $$ language sql;
+
+select * from foobar();
+
+create or replace function foobar(out integer, out numeric) as
+$$ select (1, 2) $$ language sql;
+
+select * from foobar();  -- fail
+
+create or replace function foobar(out integer, out numeric) as
+$$ select (1, 2.1, 3) $$ language sql;
+
+select * from foobar();  -- fail
+
+drop function foobar();