aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/functioncmds.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-05-14 20:19:20 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2024-05-14 20:19:20 -0400
commitf535f350c1f9b5a9c4f10583992576c3af505275 (patch)
tree13a96330dda0c161b4cbe710ad9be1e0ca1a2092 /src/backend/commands/functioncmds.c
parent245f1cec59d23032d2eab7019e5d836143383452 (diff)
downloadpostgresql-f535f350c1f9b5a9c4f10583992576c3af505275.tar.gz
postgresql-f535f350c1f9b5a9c4f10583992576c3af505275.zip
Fix handling of polymorphic output arguments for procedures.
Most of the infrastructure for procedure arguments was already okay with polymorphic output arguments, but it turns out that CallStmtResultDesc() was a few bricks shy of a load here. It thought all it needed to do was call build_function_result_tupdesc_t, but that function specifically disclaims responsibility for resolving polymorphic arguments. Failing to handle that doesn't seem to be a problem for CALL in plpgsql, but CALL from plain SQL would get errors like "cannot display a value of type anyelement", or even crash outright. In v14 and later we can simply examine the exposed types of the CallStmt.outargs nodes to get the right type OIDs. But it's a lot more complicated to fix in v12/v13, because those versions don't have CallStmt.outargs, nor do they do expand_function_arguments until ExecuteCallStmt runs. We have to duplicatively run expand_function_arguments, and then re-determine which elements of the args list are output arguments. Per bug #18463 from Drew Kimball. Back-patch to all supported versions, since it's busted in all of them. Discussion: https://postgr.es/m/18463-f8cd77e12564d8a2@postgresql.org
Diffstat (limited to 'src/backend/commands/functioncmds.c')
-rw-r--r--src/backend/commands/functioncmds.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c
index 9cf3fe8275e..6593fd7d811 100644
--- a/src/backend/commands/functioncmds.c
+++ b/src/backend/commands/functioncmds.c
@@ -52,6 +52,7 @@
#include "executor/functions.h"
#include "funcapi.h"
#include "miscadmin.h"
+#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "parser/analyze.h"
#include "parser/parse_coerce.h"
@@ -2364,5 +2365,32 @@ CallStmtResultDesc(CallStmt *stmt)
ReleaseSysCache(tuple);
+ /*
+ * The result of build_function_result_tupdesc_t has the right column
+ * names, but it just has the declared output argument types, which is the
+ * wrong thing in polymorphic cases. Get the correct types by examining
+ * stmt->outargs. We intentionally keep the atttypmod as -1 and the
+ * attcollation as the type's default, since that's always the appropriate
+ * thing for function outputs; there's no point in considering any
+ * additional info available from outargs. Note that tupdesc is null if
+ * there are no outargs.
+ */
+ if (tupdesc)
+ {
+ Assert(tupdesc->natts == list_length(stmt->outargs));
+ for (int i = 0; i < tupdesc->natts; i++)
+ {
+ Form_pg_attribute att = TupleDescAttr(tupdesc, i);
+ Node *outarg = (Node *) list_nth(stmt->outargs, i);
+
+ TupleDescInitEntry(tupdesc,
+ i + 1,
+ NameStr(att->attname),
+ exprType(outarg),
+ -1,
+ 0);
+ }
+ }
+
return tupdesc;
}