196 int paramno,
int location);
198 const char *paramname,
int location);
225 char prokind,
bool insertDroppedCols);
228 bool tlist_is_modifiable,
230 bool *upper_tlist_nontrivial);
267 pinfo->
nargs = nargs = procedureStruct->pronargs;
275 procedureStruct->proargtypes.values,
276 nargs *
sizeof(
Oid));
278 for (argnum = 0; argnum < nargs; argnum++)
280 Oid argtype = argOidVect[argnum];
282 if (IsPolymorphicType(argtype))
287 (
errcode(ERRCODE_DATATYPE_MISMATCH),
288 errmsg(
"could not determine actual type of argument declared %s",
290 argOidVect[argnum] = argtype;
308 Anum_pg_proc_proargnames,
314 Anum_pg_proc_proargmodes,
323 if (n_arg_names < nargs)
354 Node *subfield = NULL;
356 const char *name2 = NULL;
406 if (strcmp(name1, pinfo->
fname) != 0)
414 else if (nnames == 2 && strcmp(name1, pinfo->
fname) == 0)
468 int paramno = pref->
number;
471 if (paramno <= 0 || paramno > pinfo->
nargs)
482 int paramno,
int location)
490 param->paramtypmod = -1;
502 return (
Node *) param;
512 const char *paramname,
int location)
562 if (fcache->
eslist != NULL)
589 if (func != fcache->
func)
591 if (fcache->
func != NULL)
641 fcache->
cplan = NULL;
710 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
711 errmsg(
"cannot COPY to/from client in an SQL function")));
715 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
717 errmsg(
"%s is not allowed in an SQL function",
723 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
725 errmsg(
"%s is not allowed in a non-volatile function",
731 preves->
next = newes;
779 "SQL function junkfilter",
879 List *queryTree_list;
884 Assert(qindex < func->num_queries);
974 islast ? func : NULL);
1037 comperrcontext.
arg = func;
1054 "SQL function parse trees",
1063 NameStr(procedureStruct->proname));
1084 func->
returnsSet = procedureStruct->proretset;
1088 (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
1091 func->
prokind = procedureStruct->prokind;
1123 Anum_pg_proc_prosqlbody,
1158 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1159 errmsg(
"return type mismatch in function declared to return %s",
1161 errdetail(
"Function's final statement must be SELECT or INSERT/UPDATE/DELETE/MERGE RETURNING.")));
1243 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1244 errmsg(
"cached plan must not change result type")));
1274 "SQL function execution",
1289 "SQL function execution",
1347 elog(
ERROR,
"ExecutorStart() failed unexpectedly");
1434 int nargs = fcinfo->
nargs;
1458 for (
int i = 0;
i < nargs;
i++)
1480 prm->
ptype = argtypes[
i];
1537 bool pushed_snapshot;
1543 if (fcinfo->flinfo->fn_retset)
1557 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1558 errmsg(
"set-valued function called in context that cannot accept a set")));
1564 randomAccess =
false;
1577 sqlerrcontext.
arg = fcache;
1626 pushed_snapshot =
false;
1642 if (!pushed_snapshot)
1645 pushed_snapshot =
true;
1657 pushed_snapshot =
true;
1697 if (pushed_snapshot)
1700 pushed_snapshot =
false;
1728 elog(
ERROR,
"failed to fetch lazy-eval tuple");
1764 fcinfo->isnull =
true;
1790 fcinfo->isnull =
true;
1816 fcinfo->isnull =
true;
1824 fcinfo->isnull =
true;
1833 if (pushed_snapshot)
1856 int syntaxerrposition;
1862 if (func->
fname == NULL)
1869 if (syntaxerrposition > 0 && func->
src != NULL)
1890 int syntaxerrposition;
1896 if (syntaxerrposition > 0 && fcache->
func->
src != NULL)
1908 errcontext(
"SQL function \"%s\" statement %d",
1957 fcache->
cplan = NULL;
1977 if (fcache->
func != NULL)
1982 fcache->
func = NULL;
1998 foreach(lc, queryTreeLists)
2014 foreach(lc, queryTreeList)
2032 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2033 errmsg(
"calling procedures with output arguments is not supported in SQL functions")));
2077 bool insertDroppedCols)
2079 List *queryTreeList;
2093 if (queryTreeLists !=
NIL)
2096 queryTreeList =
NIL;
2099 rettype, rettupdesc,
2100 prokind, insertDroppedCols);
2110 char prokind,
bool insertDroppedCols)
2112 bool is_tuple_result =
false;
2117 bool tlist_is_modifiable;
2120 bool upper_tlist_nontrivial =
false;
2127 if (rettype == VOIDOID)
2137 foreach(lc, queryTreeList)
2162 tlist =
parse->targetList;
2164 tlist_is_modifiable = (
parse->setOperations == NULL);
2171 parse->returningList)
2173 tlist =
parse->returningList;
2175 tlist_is_modifiable =
true;
2181 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2182 errmsg(
"return type mismatch in function declared to return %s",
2184 errdetail(
"Function's final statement must be SELECT or INSERT/UPDATE/DELETE/MERGE RETURNING.")));
2205 if (fn_typtype == TYPTYPE_BASE ||
2206 fn_typtype == TYPTYPE_DOMAIN ||
2207 fn_typtype == TYPTYPE_ENUM ||
2208 fn_typtype == TYPTYPE_RANGE ||
2209 fn_typtype == TYPTYPE_MULTIRANGE)
2219 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2220 errmsg(
"return type mismatch in function declared to return %s",
2222 errdetail(
"Final statement must return exactly one column.")));
2229 tlist_is_modifiable,
2231 &upper_tlist_nontrivial))
2233 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2234 errmsg(
"return type mismatch in function declared to return %s",
2239 else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
2277 if (tlistlen == 1 && prokind != PROKIND_PROCEDURE)
2283 tlist_is_modifiable,
2285 &upper_tlist_nontrivial))
2288 goto tlist_coercion_finished;
2296 if (rettupdesc == NULL)
2305 tupnatts = rettupdesc->
natts;
2321 if (colindex > tupnatts)
2323 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2324 errmsg(
"return type mismatch in function declared to return %s",
2326 errdetail(
"Final statement returns too many columns.")));
2328 if (attr->attisdropped && insertDroppedCols)
2340 upper_tlist =
lappend(upper_tlist,
2345 upper_tlist_nontrivial =
true;
2347 }
while (attr->attisdropped);
2351 attr->atttypid, attr->atttypmod,
2352 tlist_is_modifiable,
2354 &upper_tlist_nontrivial))
2356 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2357 errmsg(
"return type mismatch in function declared to return %s",
2359 errdetail(
"Final statement returns %s instead of %s at column %d.",
2366 for (colindex++; colindex <= tupnatts; colindex++)
2370 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2371 errmsg(
"return type mismatch in function declared to return %s",
2373 errdetail(
"Final statement returns too few columns.")));
2374 if (insertDroppedCols)
2386 upper_tlist =
lappend(upper_tlist,
2391 upper_tlist_nontrivial =
true;
2396 is_tuple_result =
true;
2400 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2401 errmsg(
"return type %s is not supported for SQL functions",
2404tlist_coercion_finished:
2412 if (upper_tlist_nontrivial)
2424 newquery->querySource =
parse->querySource;
2425 newquery->canSetTag =
true;
2430 foreach(lc,
parse->targetList)
2437 makeString(tle->resname ? tle->resname :
""));
2444 rte->eref = rte->alias =
makeAlias(
"*SELECT*", colnames);
2445 rte->lateral =
false;
2447 rte->inFromCl =
true;
2458 newquery->hasRowSecurity =
parse->hasRowSecurity;
2461 lfirst(parse_cell) = newquery;
2464 return is_tuple_result;
2481 bool tlist_is_modifiable,
2483 bool *upper_tlist_nontrivial)
2501 res_type, res_typmod,
2505 if (cast_result == NULL)
2508 src_tle->
expr = (
Expr *) cast_result;
2520 res_type, res_typmod,
2524 if (cast_result == NULL)
2528 if (cast_result != (
Node *) var)
2529 *upper_tlist_nontrivial =
true;
2530 new_tle_expr = (
Expr *) cast_result;
2534 src_tle->resname,
false);
2535 *upper_tlist =
lappend(*upper_tlist, new_tle);
2549 foreach(lc, queryTreeList)
2558 return parse->targetList;
2564 parse->returningList)
2565 return parse->returningList;
#define TextDatumGetCString(d)
#define OidIsValid(objectId)
Datum datumCopy(Datum value, bool typByVal, int typLen)
DestReceiver * CreateDestReceiver(CommandDest dest)
DestReceiver * None_Receiver
int internalerrquery(const char *query)
int internalerrposition(int cursorpos)
int errdetail(const char *fmt,...)
ErrorContextCallback * error_context_stack
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
int errposition(int cursorpos)
#define ereport(elevel,...)
JunkFilter * ExecInitJunkFilterConversion(List *targetList, TupleDesc cleanTupType, TupleTableSlot *slot)
TupleTableSlot * ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
JunkFilter * ExecInitJunkFilter(List *targetList, TupleTableSlot *slot)
bool ExecutorStart(QueryDesc *queryDesc, int eflags)
void ExecutorEnd(QueryDesc *queryDesc)
void ExecutorFinish(QueryDesc *queryDesc)
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Datum ExecFetchSlotHeapTupleDatum(TupleTableSlot *slot)
const TupleTableSlotOps TTSOpsMinimalTuple
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
int ExecCleanTargetListLength(List *targetlist)
@ SFRM_Materialize_Preferred
@ SFRM_Materialize_Random
#define EXEC_FLAG_SKIP_TRIGGERS
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
#define repalloc_array(pointer, type, count)
Oid get_call_expr_argtype(Node *expr, int argnum)
#define PG_GET_COLLATION()
int get_func_input_arg_names(Datum proargnames, Datum proargmodes, char ***arg_names)
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
CachedFunction * cached_function_compile(FunctionCallInfo fcinfo, CachedFunction *function, CachedFunctionCompileCallback ccallback, CachedFunctionDeleteCallback dcallback, Size cacheEntrySize, bool includeResultType, bool forValidator)
static void check_sql_fn_statement(List *queryTreeList)
static void sql_delete_callback(CachedFunction *cfunc)
static Datum postquel_get_single_result(TupleTableSlot *slot, FunctionCallInfo fcinfo, SQLFunctionCachePtr fcache)
Datum fmgr_sql(PG_FUNCTION_ARGS)
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, char prokind, bool insertDroppedCols)
void check_sql_fn_statements(List *queryTreeLists)
static void prepare_next_query(SQLFunctionHashEntry *func)
static List * get_sql_fn_result_tlist(List *queryTreeList)
static Node * sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo, const char *paramname, int location)
static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
static Node * sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
static bool check_sql_stmt_retval(List *queryTreeList, Oid rettype, TupleDesc rettupdesc, char prokind, bool insertDroppedCols)
static void sql_compile_callback(FunctionCallInfo fcinfo, HeapTuple procedureTuple, const CachedFunctionHashKey *hashkey, CachedFunction *cfunc, bool forValidator)
static void postquel_sub_params(SQLFunctionCachePtr fcache, FunctionCallInfo fcinfo)
static bool sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
static bool init_execution_state(SQLFunctionCachePtr fcache)
static void sql_compile_error_callback(void *arg)
DestReceiver * CreateSQLFunctionDestReceiver(void)
struct SQLFunctionCache SQLFunctionCache
static void sql_postrewrite_callback(List *querytree_list, void *arg)
static void postquel_end(execution_state *es, SQLFunctionCachePtr fcache)
struct execution_state execution_state
static void RemoveSQLFunctionCache(void *arg)
struct SQLFunctionHashEntry SQLFunctionHashEntry
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
static Node * sql_fn_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
static void sqlfunction_destroy(DestReceiver *self)
static void sql_exec_error_callback(void *arg)
static void sqlfunction_shutdown(DestReceiver *self)
static Node * sql_fn_make_param(SQLFunctionParseInfoPtr pinfo, int paramno, int location)
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info(HeapTuple procedureTuple, Node *call_expr, Oid inputCollation)
static void ShutdownSQLFunction(Datum arg)
static bool coerce_fn_result_column(TargetEntry *src_tle, Oid res_type, int32 res_typmod, bool tlist_is_modifiable, List **upper_tlist, bool *upper_tlist_nontrivial)
static SQLFunctionCache * init_sql_fcache(FunctionCallInfo fcinfo, bool lazyEvalOK)
SQLFunctionCache * SQLFunctionCachePtr
SQLFunctionParseInfo * SQLFunctionParseInfoPtr
Assert(PointerIsAligned(start, uint64))
static void * GETSTRUCT(const HeapTupleData *tuple)
if(TABLE==NULL||TABLE_index==NULL)
List * lappend(List *list, void *datum)
bool type_is_rowtype(Oid typid)
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Oid get_typcollation(Oid typid)
int16 get_typlen(Oid typid)
char get_typtype(Oid typid)
Alias * makeAlias(const char *aliasname, List *colnames)
Var * makeVarFromTargetEntry(int varno, TargetEntry *tle)
FromExpr * makeFromExpr(List *fromlist, Node *quals)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
char * MemoryContextStrdup(MemoryContext context, const char *string)
void * MemoryContextAlloc(MemoryContext context, Size size)
void MemoryContextReset(MemoryContext context)
void * MemoryContextAllocZero(MemoryContext context, Size size)
char * pstrdup(const char *in)
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
MemoryContext CacheMemoryContext
void MemoryContextDelete(MemoryContext context)
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define ALLOCSET_SMALL_SIZES
Oid exprType(const Node *expr)
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
ParamListInfo makeParamList(int numParams)
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
void assign_expr_collations(ParseState *pstate, Node *expr)
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
#define CURSOR_OPT_PARALLEL_OK
#define CURSOR_OPT_NO_SCROLL
FormData_pg_attribute * Form_pg_attribute
#define lfirst_node(type, lc)
static int list_length(const List *l)
#define linitial_node(type, l)
#define foreach_current_index(var_or_cell)
static void * list_nth(const List *list, int n)
#define list_nth_node(type, list, n)
#define llast_node(type, l)
FormData_pg_proc * Form_pg_proc
void DropCachedPlan(CachedPlanSource *plansource)
void SaveCachedPlan(CachedPlanSource *plansource)
void CompleteCachedPlan(CachedPlanSource *plansource, List *querytree_list, MemoryContext querytree_context, Oid *param_types, int num_params, ParserSetupHook parserSetup, void *parserSetupArg, int cursor_options, bool fixed_result)
CachedPlan * GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams, ResourceOwner owner, QueryEnvironment *queryEnv)
CachedPlanSource * CreateCachedPlanForQuery(Query *analyzed_parse_tree, const char *query_string, CommandTag commandTag)
void SetPostRewriteHook(CachedPlanSource *plansource, PostRewriteHook postRewrite, void *postRewriteArg)
CachedPlanSource * CreateCachedPlan(RawStmt *raw_parse_tree, const char *query_string, CommandTag commandTag)
void ReleaseCachedPlan(CachedPlan *plan, ResourceOwner owner)
List * pg_analyze_and_rewrite_withcb(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
List * pg_parse_query(const char *query_string)
List * pg_rewrite_query(Query *query)
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
void FreeQueryDesc(QueryDesc *qdesc)
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, CachedPlan *cplan, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
void * stringToNode(const char *str)
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
ResourceOwner CurrentResourceOwner
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
Snapshot GetTransactionSnapshot(void)
void PushActiveSnapshot(Snapshot snapshot)
void UpdateActiveSnapshotCommandId(void)
bool ActiveSnapshotSet(void)
void PopActiveSnapshot(void)
Snapshot GetActiveSnapshot(void)
struct ErrorContextCallback * previous
void(* callback)(void *arg)
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
TupleDesc jf_cleanTupType
TupleTableSlot * jf_resultSlot
MemoryContextCallbackFunction func
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
ParseParamRefHook p_paramref_hook
PreParseColumnRefHook p_pre_columnref_hook
PostParseColumnRefHook p_post_columnref_hook
PlannedStmt * plannedstmt
QueryEnvironment * queryEnv
SetFunctionReturnMode returnMode
Tuplestorestate * setResult
SQLFunctionHashEntry * func
execution_state * esarray
MemoryContextCallback mcb
SQLFunctionParseInfoPtr pinfo
TupleDesc tts_tupleDescriptor
void(* rStartup)(DestReceiver *self, int operation, TupleDesc typeinfo)
void(* rShutdown)(DestReceiver *self)
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
void(* rDestroy)(DestReceiver *self)
struct execution_state * next
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
void tuplestore_clear(Tuplestorestate *state)
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
void tuplestore_end(Tuplestorestate *state)
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
void ProcessUtility(PlannedStmt *pstmt, const char *queryString, bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc)
bool CommandIsReadOnly(PlannedStmt *pstmt)
CommandTag CreateCommandTag(Node *parsetree)
static const char * CreateCommandName(Node *parsetree)
String * makeString(char *str)
void CommandCounterIncrement(void)