138 int paramno,
int location);
140 const char *paramname,
int location);
158 bool tlist_is_modifiable,
160 bool *upper_tlist_nontrivial);
196 pinfo->
nargs = nargs = procedureStruct->pronargs;
204 procedureStruct->proargtypes.values,
205 nargs *
sizeof(
Oid));
207 for (argnum = 0; argnum < nargs; argnum++)
209 Oid argtype = argOidVect[argnum];
211 if (IsPolymorphicType(argtype))
216 (
errcode(ERRCODE_DATATYPE_MISMATCH),
217 errmsg(
"could not determine actual type of argument declared %s",
219 argOidVect[argnum] = argtype;
237 Anum_pg_proc_proargnames,
243 Anum_pg_proc_proargmodes,
252 if (n_arg_names < nargs)
283 Node *subfield = NULL;
285 const char *name2 = NULL;
335 if (strcmp(name1, pinfo->
fname) != 0)
343 else if (nnames == 2 && strcmp(name1, pinfo->
fname) == 0)
397 int paramno = pref->
number;
400 if (paramno <= 0 || paramno > pinfo->
nargs)
411 int paramno,
int location)
419 param->paramtypmod = -1;
431 return (
Node *) param;
441 const char *paramname,
int location)
472 foreach(lc1, queryTree_list)
491 stmt->canSetTag = queryTree->canSetTag;
494 stmt->stmt_len = queryTree->stmt_len;
511 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
512 errmsg(
"cannot COPY to/from client in an SQL function")));
516 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
518 errmsg(
"%s is not allowed in an SQL function",
524 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
526 errmsg(
"%s is not allowed in a non-volatile function",
532 preves->
next = newes;
543 if (queryTree->canSetTag)
549 eslist =
lappend(eslist, firstes);
593 List *queryTree_list;
623 elog(
ERROR,
"cache lookup failed for function %u", foid);
645 fcache->
returnsSet = procedureStruct->proretset;
649 (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
669 Anum_pg_proc_prosqlbody,
681 queryTree_list =
NIL;
685 List *stored_query_list;
693 foreach(lc, stored_query_list)
696 List *queryTree_sublist;
700 queryTree_list =
lappend(queryTree_list, queryTree_sublist);
705 List *raw_parsetree_list;
709 foreach(lc, raw_parsetree_list)
712 List *queryTree_sublist;
719 queryTree_list =
lappend(queryTree_list, queryTree_sublist);
759 if (rettype != VOIDOID)
893 uint64 count = (es->
lazyEval) ? 1 : 0;
932 int nargs = fcinfo->
nargs;
950 for (
int i = 0;
i < nargs;
i++)
1035 bool pushed_snapshot;
1046 sqlerrcontext.
arg = fcinfo->flinfo;
1051 if (fcinfo->flinfo->fn_retset)
1065 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1066 errmsg(
"set-valued function called in context that cannot accept a set")));
1072 randomAccess =
false;
1088 fcinfo->flinfo->fn_extra = NULL;
1116 foreach(eslc, eslist)
1162 pushed_snapshot =
false;
1178 if (!pushed_snapshot)
1181 pushed_snapshot =
true;
1193 pushed_snapshot =
true;
1227 eslc =
lnext(eslist, eslc);
1239 if (pushed_snapshot)
1242 pushed_snapshot =
false;
1265 elog(
ERROR,
"failed to fetch lazy-eval tuple");
1268 fcache, oldcontext);
1302 fcinfo->isnull =
true;
1328 fcinfo->isnull =
true;
1352 fcache, oldcontext);
1355 fcinfo->isnull =
true;
1363 fcinfo->isnull =
true;
1372 if (pushed_snapshot)
1408 int syntaxerrposition;
1414 if (fcache == NULL || fcache->
fname == NULL)
1421 if (syntaxerrposition > 0 && fcache->
src != NULL)
1450 errcontext(
"SQL function \"%s\" statement %d",
1451 fcache->
fname, query_num);
1537 foreach(lc, queryTreeLists)
1542 foreach(lc2, sublist)
1560 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1561 errmsg(
"calling procedures with output arguments is not supported in SQL functions")));
1609 bool insertDroppedCols,
1610 List **resultTargetList)
1612 bool is_tuple_result =
false;
1617 bool tlist_is_modifiable;
1620 bool upper_tlist_nontrivial =
false;
1623 if (resultTargetList)
1624 *resultTargetList =
NIL;
1630 if (rettype == VOIDOID)
1644 foreach(lc, queryTreeLists)
1649 foreach(lc2, sublist)
1675 tlist =
parse->targetList;
1677 tlist_is_modifiable = (
parse->setOperations == NULL);
1683 parse->returningList)
1685 tlist =
parse->returningList;
1687 tlist_is_modifiable =
true;
1693 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1694 errmsg(
"return type mismatch in function declared to return %s",
1696 errdetail(
"Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1717 if (fn_typtype == TYPTYPE_BASE ||
1718 fn_typtype == TYPTYPE_DOMAIN ||
1719 fn_typtype == TYPTYPE_ENUM ||
1720 fn_typtype == TYPTYPE_RANGE ||
1721 fn_typtype == TYPTYPE_MULTIRANGE)
1731 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1732 errmsg(
"return type mismatch in function declared to return %s",
1734 errdetail(
"Final statement must return exactly one column.")));
1741 tlist_is_modifiable,
1743 &upper_tlist_nontrivial))
1745 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1746 errmsg(
"return type mismatch in function declared to return %s",
1751 else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
1790 tlist_is_modifiable,
1792 &upper_tlist_nontrivial))
1795 goto tlist_coercion_finished;
1803 if (rettupdesc == NULL)
1806 if (resultTargetList)
1807 *resultTargetList = tlist;
1817 tupnatts = rettupdesc->
natts;
1833 if (colindex > tupnatts)
1835 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1836 errmsg(
"return type mismatch in function declared to return %s",
1838 errdetail(
"Final statement returns too many columns.")));
1840 if (attr->attisdropped && insertDroppedCols)
1852 upper_tlist =
lappend(upper_tlist,
1857 upper_tlist_nontrivial =
true;
1859 }
while (attr->attisdropped);
1863 attr->atttypid, attr->atttypmod,
1864 tlist_is_modifiable,
1866 &upper_tlist_nontrivial))
1868 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1869 errmsg(
"return type mismatch in function declared to return %s",
1871 errdetail(
"Final statement returns %s instead of %s at column %d.",
1878 for (colindex++; colindex <= tupnatts; colindex++)
1882 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1883 errmsg(
"return type mismatch in function declared to return %s",
1885 errdetail(
"Final statement returns too few columns.")));
1886 if (insertDroppedCols)
1898 upper_tlist =
lappend(upper_tlist,
1903 upper_tlist_nontrivial =
true;
1908 is_tuple_result =
true;
1912 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1913 errmsg(
"return type %s is not supported for SQL functions",
1916 tlist_coercion_finished:
1924 if (upper_tlist_nontrivial)
1936 newquery->querySource =
parse->querySource;
1937 newquery->canSetTag =
true;
1942 foreach(lc,
parse->targetList)
1949 makeString(tle->resname ? tle->resname :
""));
1967 lfirst(parse_cell) = newquery;
1971 if (resultTargetList)
1972 *resultTargetList = upper_tlist;
1974 return is_tuple_result;
1991 bool tlist_is_modifiable,
1993 bool *upper_tlist_nontrivial)
2011 res_type, res_typmod,
2015 if (cast_result == NULL)
2018 src_tle->
expr = (
Expr *) cast_result;
2030 res_type, res_typmod,
2034 if (cast_result == NULL)
2038 if (cast_result != (
Node *) var)
2039 *upper_tlist_nontrivial =
true;
2040 new_tle_expr = (
Expr *) cast_result;
2044 src_tle->resname,
false);
2045 *upper_tlist =
lappend(*upper_tlist, new_tle);
#define TextDatumGetCString(d)
uint32 LocalTransactionId
#define OidIsValid(objectId)
Datum datumCopy(Datum value, bool typByVal, int typLen)
elog(ERROR, "%s: %s", p2, msg)
DestReceiver * None_Receiver
DestReceiver * CreateDestReceiver(CommandDest dest)
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 * ExecInitJunkFilter(List *targetList, TupleTableSlot *slot)
TupleTableSlot * ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
JunkFilter * ExecInitJunkFilterConversion(List *targetList, TupleDesc cleanTupType, TupleTableSlot *slot)
void ExecutorEnd(QueryDesc *queryDesc)
void ExecutorFinish(QueryDesc *queryDesc)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Datum ExecFetchSlotHeapTupleDatum(TupleTableSlot *slot)
const TupleTableSlotOps TTSOpsMinimalTuple
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
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)
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)
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, bool insertDroppedCols, List **resultTargetList)
Datum fmgr_sql(PG_FUNCTION_ARGS)
static List * init_execution_state(List *queryTree_list, SQLFunctionCachePtr fcache, bool lazyEvalOK)
void check_sql_fn_statements(List *queryTreeLists)
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 void postquel_end(execution_state *es)
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)
struct execution_state execution_state
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
DestReceiver * CreateSQLFunctionDestReceiver(void)
static Node * sql_fn_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
static void sqlfunction_destroy(DestReceiver *self)
static Datum postquel_get_single_result(TupleTableSlot *slot, FunctionCallInfo fcinfo, SQLFunctionCachePtr fcache, MemoryContext resultcontext)
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)
static void init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK)
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)
SQLFunctionCache * SQLFunctionCachePtr
SQLFunctionParseInfo * SQLFunctionParseInfoPtr
#define HeapTupleIsValid(tuple)
if(TABLE==NULL||TABLE_index==NULL)
Assert(fmt[strlen(fmt) - 1] !='\n')
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)
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)
FromExpr * makeFromExpr(List *fromlist, Node *quals)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
void MemoryContextDelete(MemoryContext context)
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_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
FormData_pg_attribute * Form_pg_attribute
#define lfirst_node(type, lc)
static int list_length(const List *l)
#define linitial_node(type, l)
static ListCell * lnext(const List *l, const ListCell *c)
FormData_pg_proc * Form_pg_proc
List * pg_parse_query(const char *query_string)
List * pg_analyze_and_rewrite_withcb(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
List * pg_rewrite_query(Query *query)
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
static Pointer DatumGetPointer(Datum X)
void FreeQueryDesc(QueryDesc *qdesc)
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, 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)
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
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
SQLFunctionParseInfoPtr pinfo
TupleDesc tts_tupleDescriptor
void(* rDestroy)(DestReceiver *self)
struct execution_state * next
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
#define TupleDescAttr(tupdesc, 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)
static const char * CreateCommandName(Node *parsetree)
String * makeString(char *str)
SubTransactionId GetCurrentSubTransactionId(void)
void CommandCounterIncrement(void)
bool SubTransactionIsActive(SubTransactionId subxid)