aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/functions.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-06-06 15:16:56 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2024-06-06 15:16:56 -0400
commit4208f44c9469fa6b105bbb1d1f7ee18b37472860 (patch)
tree2918d90439f35c88758352a35a243d25a9f785e3 /src/backend/executor/functions.c
parent78fe33742dccc23d4b8a4a1cc7a311e7be0403d1 (diff)
downloadpostgresql-4208f44c9469fa6b105bbb1d1f7ee18b37472860.tar.gz
postgresql-4208f44c9469fa6b105bbb1d1f7ee18b37472860.zip
Fix failure with SQL-procedure polymorphic output arguments in v12.
Before the v13-era commit 913bbd88d, check_sql_fn_retval fails to resolve polymorphic output types and then just throws up its hands and assumes the check will be made at runtime. I think that's true for ordinary functions returning RECORD, but it doesn't happen in CALL, potentially resulting in crashes if the actual output of the SQL procedure's SELECT doesn't match the type inferred from polymorphism. With a little bit of rearrangement, we can use get_call_result_type instead of get_func_result_type and thereby infer the correct types. I'm still unwilling to back-patch all of 913bbd88d, so if the types don't match you'll get an error rather than perhaps silently inserting a cast as v13 and later can. That's consistent with prior behavior though, so it seems fine. Prior to 70ffb27b2, you'd typically get other errors due to other shortcomings of CALL's management of polymorphism. Nonetheless, this is an independent bug. Although there is no bug in v13 and up, it seems prudent to add the test case for this to the newer branches too. It's clearly an under-tested area. Per report from Andrew Bille. Discussion: https://postgr.es/m/CAJnzarw9EeWHAQRm76dXd=7j+rgw6ERqC=nCay8jeFqTwKwhqQ@mail.gmail.com
Diffstat (limited to 'src/backend/executor/functions.c')
-rw-r--r--src/backend/executor/functions.c66
1 files changed, 50 insertions, 16 deletions
diff --git a/src/backend/executor/functions.c b/src/backend/executor/functions.c
index 2e4d5d9f454..4ec1be83e32 100644
--- a/src/backend/executor/functions.c
+++ b/src/backend/executor/functions.c
@@ -154,7 +154,7 @@ static Node *sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
static List *init_execution_state(List *queryTree_list,
SQLFunctionCachePtr fcache,
bool lazyEvalOK);
-static void init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK);
+static void init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK);
static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
static void postquel_end(execution_state *es);
@@ -166,6 +166,12 @@ static Datum postquel_get_single_result(TupleTableSlot *slot,
MemoryContext resultcontext);
static void sql_exec_error_callback(void *arg);
static void ShutdownSQLFunction(Datum arg);
+static bool check_sql_fn_retval_ext2(Oid func_id,
+ FunctionCallInfo fcinfo,
+ Oid rettype, char prokind,
+ List *queryTreeList,
+ bool *modifyTargetList,
+ JunkFilter **junkFilter);
static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
static bool sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self);
static void sqlfunction_shutdown(DestReceiver *self);
@@ -591,8 +597,9 @@ init_execution_state(List *queryTree_list,
* Initialize the SQLFunctionCache for a SQL function
*/
static void
-init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
+init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK)
{
+ FmgrInfo *finfo = fcinfo->flinfo;
Oid foid = finfo->fn_oid;
MemoryContext fcontext;
MemoryContext oldcontext;
@@ -744,12 +751,13 @@ init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
* coerce the returned rowtype to the desired form (unless the result type
* is VOID, in which case there's nothing to coerce to).
*/
- fcache->returnsTuple = check_sql_fn_retval_ext(foid,
- rettype,
- procedureStruct->prokind,
- flat_query_list,
- NULL,
- &fcache->junkFilter);
+ fcache->returnsTuple = check_sql_fn_retval_ext2(foid,
+ fcinfo,
+ rettype,
+ procedureStruct->prokind,
+ flat_query_list,
+ NULL,
+ &fcache->junkFilter);
if (fcache->returnsTuple)
{
@@ -1066,7 +1074,7 @@ fmgr_sql(PG_FUNCTION_ARGS)
if (fcache == NULL)
{
- init_sql_fcache(fcinfo->flinfo, PG_GET_COLLATION(), lazyEvalOK);
+ init_sql_fcache(fcinfo, PG_GET_COLLATION(), lazyEvalOK);
fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
}
@@ -1593,11 +1601,11 @@ check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
JunkFilter **junkFilter)
{
/* Wrapper function to preserve ABI compatibility in released branches */
- return check_sql_fn_retval_ext(func_id, rettype,
- PROKIND_FUNCTION,
- queryTreeList,
- modifyTargetList,
- junkFilter);
+ return check_sql_fn_retval_ext2(func_id, NULL, rettype,
+ PROKIND_FUNCTION,
+ queryTreeList,
+ modifyTargetList,
+ junkFilter);
}
bool
@@ -1606,6 +1614,22 @@ check_sql_fn_retval_ext(Oid func_id, Oid rettype, char prokind,
bool *modifyTargetList,
JunkFilter **junkFilter)
{
+ /* Wrapper function to preserve ABI compatibility in released branches */
+ return check_sql_fn_retval_ext2(func_id, NULL, rettype,
+ prokind,
+ queryTreeList,
+ modifyTargetList,
+ junkFilter);
+}
+
+static bool
+check_sql_fn_retval_ext2(Oid func_id,
+ FunctionCallInfo fcinfo,
+ Oid rettype, char prokind,
+ List *queryTreeList,
+ bool *modifyTargetList,
+ JunkFilter **junkFilter)
+{
Query *parse;
List **tlist_ptr;
List *tlist;
@@ -1750,6 +1774,7 @@ check_sql_fn_retval_ext(Oid func_id, Oid rettype, char prokind,
* result type, so there is no way to produce a domain-over-composite
* result except by computing it as an explicit single-column result.
*/
+ TypeFuncClass tfclass;
TupleDesc tupdesc;
int tupnatts; /* physical number of columns in tuple */
int tuplogcols; /* # of nondeleted columns in tuple */
@@ -1806,10 +1831,19 @@ check_sql_fn_retval_ext(Oid func_id, Oid rettype, char prokind,
}
/*
- * Is the rowtype fixed, or determined only at runtime? (Note we
+ * Identify the output rowtype, resolving polymorphism if possible
+ * (that is, if we were passed an fcinfo).
+ */
+ if (fcinfo)
+ tfclass = get_call_result_type(fcinfo, NULL, &tupdesc);
+ else
+ tfclass = get_func_result_type(func_id, NULL, &tupdesc);
+
+ /*
+ * Is the rowtype known, or determined only at runtime? (Note we
* cannot see TYPEFUNC_COMPOSITE_DOMAIN here.)
*/
- if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ if (tfclass != TYPEFUNC_COMPOSITE)
{
/*
* Assume we are returning the whole tuple. Crosschecking against