+ if (pinfo->argnames == NULL)
+ return NULL;
+
+ for (i = 0; i < pinfo->nargs; i++)
+ {
+ if (pinfo->argnames[i] && strcmp(pinfo->argnames[i], paramname) == 0)
+ return sql_fn_make_param(pinfo, i + 1, location);
+ }
+
+ return NULL;
+}
+
+/*
+ * Set up the per-query execution_state records for a SQL function.
+ *
+ * The input is a List of Lists of parsed and rewritten, but not planned,
+ * querytrees. The sublist structure denotes the original query boundaries.
+ */
+static List *
+init_execution_state(List *queryTree_list,
+ SQLFunctionCachePtr fcache,
+ bool lazyEvalOK)
+{
+ List *eslist = NIL;
+ execution_state *lasttages = NULL;
+ ListCell *lc1;
+
+ foreach(lc1, queryTree_list)
+ {
+ List *qtlist = castNode(List, lfirst(lc1));
+ execution_state *firstes = NULL;
+ execution_state *preves = NULL;
+ ListCell *lc2;
+
+ foreach(lc2, qtlist)
+ {
+ Query *queryTree = castNode(Query, lfirst(lc2));
+ PlannedStmt *stmt;
+ execution_state *newes;
+
+ /* Plan the query if needed */
+ if (queryTree->commandType == CMD_UTILITY)
+ {
+ /* Utility commands require no planning. */
+ stmt = makeNode(PlannedStmt);
+ stmt->commandType = CMD_UTILITY;
+ stmt->canSetTag = queryTree->canSetTag;
+ stmt->utilityStmt = queryTree->utilityStmt;
+ stmt->stmt_location = queryTree->stmt_location;
+ stmt->stmt_len = queryTree->stmt_len;
+ }
+ else
+ stmt = pg_plan_query(queryTree,
+ CURSOR_OPT_PARALLEL_OK,
+ NULL);
+
+ /*
+ * Precheck all commands for validity in a function. This should
+ * generally match the restrictions spi.c applies.
+ */
+ if (stmt->commandType == CMD_UTILITY)
+ {
+ if (IsA(stmt->utilityStmt, CopyStmt) &&
+ ((CopyStmt *) stmt->utilityStmt)->filename == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot COPY to/from client in a SQL function")));
+
+ if (IsA(stmt->utilityStmt, TransactionStmt))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ /* translator: %s is a SQL statement name */
+ errmsg("%s is not allowed in a SQL function",
+ CreateCommandTag(stmt->utilityStmt))));
+ }
+
+ if (fcache->readonly_func && !CommandIsReadOnly(stmt))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ /* translator: %s is a SQL statement name */
+ errmsg("%s is not allowed in a non-volatile function",
+ CreateCommandTag((Node *) stmt))));
+
+ if (IsInParallelMode() && !CommandIsReadOnly(stmt))
+ PreventCommandIfParallelMode(CreateCommandTag((Node *) stmt));
+
+ /* OK, build the execution_state for this query */
+ newes = (execution_state *) palloc(sizeof(execution_state));
+ if (preves)
+ preves->next = newes;
+ else
+ firstes = newes;
+
+ newes->next = NULL;
+ newes->status = F_EXEC_START;
+ newes->setsResult = false; /* might change below */
+ newes->lazyEval = false; /* might change below */
+ newes->stmt = stmt;
+ newes->qd = NULL;
+
+ if (queryTree->canSetTag)
+ lasttages = newes;
+
+ preves = newes;
+ }
+
+ eslist = lappend(eslist, firstes);
+ }
+
+ /*
+ * Mark the last canSetTag query as delivering the function result; then,
+ * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
+ * SELECT we must always run it to completion.
+ *
+ * Note: at some point we might add additional criteria for whether to use
+ * lazy eval. However, we should prefer to use it whenever the function
+ * doesn't return set, since fetching more than one row is useless in that
+ * case.
+ *
+ * Note: don't set setsResult if the function returns VOID, as evidenced
+ * by not having made a junkfilter. This ensures we'll throw away any
+ * output from a utility statement that check_sql_fn_retval deemed to not
+ * have output.
+ */
+ if (lasttages && fcache->junkFilter)
+ {
+ lasttages->setsResult = true;
+ if (lazyEvalOK &&
+ lasttages->stmt->commandType == CMD_SELECT &&
+ !lasttages->stmt->hasModifyingCTE)
+ fcache->lazyEval = lasttages->lazyEval = true;
+ }
+
+ return eslist;
+}
+
+/*
+ * Initialize the SQLFunctionCache for a SQL function
+ */