1 /*-------------------------------------------------------------------------
4 * support for the POSTGRES executor module
7 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/executor/executor.h
12 *-------------------------------------------------------------------------
17 #include "executor/execdesc.h"
18 #include "nodes/parsenodes.h"
19 #include "utils/memutils.h"
23 * The "eflags" argument to ExecutorStart and the various ExecInitNode
24 * routines is a bitwise OR of the following flag bits, which tell the
25 * called plan node what to expect. Note that the flags will get modified
26 * as they are passed down the plan tree, since an upper node may require
27 * functionality in its subnode not demanded of the plan as a whole
28 * (example: MergeJoin requires mark/restore capability in its inner input),
29 * or an upper node may shield its input from some functionality requirement
30 * (example: Materialize shields its input from needing to do backward scan).
32 * EXPLAIN_ONLY indicates that the plan tree is being initialized just so
33 * EXPLAIN can print it out; it will not be run. Hence, no side-effects
34 * of startup should occur. However, error checks (such as permission checks)
35 * should be performed.
37 * REWIND indicates that the plan node should try to efficiently support
38 * rescans without parameter changes. (Nodes must support ExecReScan calls
39 * in any case, but if this flag was not given, they are at liberty to do it
40 * through complete recalculation. Note that a parameter change forces a
41 * full recalculation in any case.)
43 * BACKWARD indicates that the plan node must respect the es_direction flag.
44 * When this is not passed, the plan node will only be run forwards.
46 * MARK indicates that the plan node must support Mark/Restore calls.
47 * When this is not passed, no Mark/Restore will occur.
49 * SKIP_TRIGGERS tells ExecutorStart/ExecutorFinish to skip calling
50 * AfterTriggerBeginQuery/AfterTriggerEndQuery. This does not necessarily
51 * mean that the plan can't queue any AFTER triggers; just that the caller
52 * is responsible for there being a trigger context for them to be queued in.
54 * WITH/WITHOUT_OIDS tell the executor to emit tuples with or without space
55 * for OIDs, respectively. These are currently used only for CREATE TABLE AS.
56 * If neither is set, the plan may or may not produce tuples including OIDs.
58 #define EXEC_FLAG_EXPLAIN_ONLY 0x0001 /* EXPLAIN, no ANALYZE */
59 #define EXEC_FLAG_REWIND 0x0002 /* need efficient rescan */
60 #define EXEC_FLAG_BACKWARD 0x0004 /* need backward scan */
61 #define EXEC_FLAG_MARK 0x0008 /* need mark/restore */
62 #define EXEC_FLAG_SKIP_TRIGGERS 0x0010 /* skip AfterTrigger calls */
63 #define EXEC_FLAG_WITH_OIDS 0x0020 /* force OIDs in returned tuples */
64 #define EXEC_FLAG_WITHOUT_OIDS 0x0040 /* force no OIDs in returned tuples */
65 #define EXEC_FLAG_WITH_NO_DATA 0x0080 /* rel scannability doesn't matter */
68 /* Hook for plugins to get control in ExecutorStart() */
69 typedef void (*ExecutorStart_hook_type) (QueryDesc *queryDesc, int eflags);
70 extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook;
72 /* Hook for plugins to get control in ExecutorRun() */
73 typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
74 ScanDirection direction,
77 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;
79 /* Hook for plugins to get control in ExecutorFinish() */
80 typedef void (*ExecutorFinish_hook_type) (QueryDesc *queryDesc);
81 extern PGDLLIMPORT ExecutorFinish_hook_type ExecutorFinish_hook;
83 /* Hook for plugins to get control in ExecutorEnd() */
84 typedef void (*ExecutorEnd_hook_type) (QueryDesc *queryDesc);
85 extern PGDLLIMPORT ExecutorEnd_hook_type ExecutorEnd_hook;
87 /* Hook for plugins to get control in ExecCheckRTPerms() */
88 typedef bool (*ExecutorCheckPerms_hook_type) (List *, bool);
89 extern PGDLLIMPORT ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook;
93 * prototypes from functions in execAmi.c
95 struct Path; /* avoid including relation.h here */
97 extern void ExecReScan(PlanState *node);
98 extern void ExecMarkPos(PlanState *node);
99 extern void ExecRestrPos(PlanState *node);
100 extern bool ExecSupportsMarkRestore(struct Path *pathnode);
101 extern bool ExecSupportsBackwardScan(Plan *node);
102 extern bool ExecMaterializesOutput(NodeTag plantype);
105 * prototypes from functions in execCurrent.c
107 extern bool execCurrentOf(CurrentOfExpr *cexpr,
108 ExprContext *econtext,
110 ItemPointer current_tid);
113 * prototypes from functions in execGrouping.c
115 extern ExprState *execTuplesMatchPrepare(TupleDesc desc,
117 AttrNumber *keyColIdx,
120 extern void execTuplesHashPrepare(int numCols,
123 FmgrInfo **hashFunctions);
124 extern TupleHashTable BuildTupleHashTable(PlanState *parent,
126 int numCols, AttrNumber *keyColIdx,
128 FmgrInfo *hashfunctions,
129 long nbuckets, Size additionalsize,
130 MemoryContext tablecxt,
131 MemoryContext tempcxt, bool use_variable_hash_iv);
132 extern TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable,
133 TupleTableSlot *slot,
135 extern TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable,
136 TupleTableSlot *slot,
138 FmgrInfo *hashfunctions);
141 * prototypes from functions in execJunk.c
143 extern JunkFilter *ExecInitJunkFilter(List *targetList, bool hasoid,
144 TupleTableSlot *slot);
145 extern JunkFilter *ExecInitJunkFilterConversion(List *targetList,
146 TupleDesc cleanTupType,
147 TupleTableSlot *slot);
148 extern AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter,
149 const char *attrName);
150 extern AttrNumber ExecFindJunkAttributeInTlist(List *targetlist,
151 const char *attrName);
152 extern Datum ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno,
154 extern TupleTableSlot *ExecFilterJunk(JunkFilter *junkfilter,
155 TupleTableSlot *slot);
159 * prototypes from functions in execMain.c
161 extern void ExecutorStart(QueryDesc *queryDesc, int eflags);
162 extern void standard_ExecutorStart(QueryDesc *queryDesc, int eflags);
163 extern void ExecutorRun(QueryDesc *queryDesc,
164 ScanDirection direction, uint64 count, bool execute_once);
165 extern void standard_ExecutorRun(QueryDesc *queryDesc,
166 ScanDirection direction, uint64 count, bool execute_once);
167 extern void ExecutorFinish(QueryDesc *queryDesc);
168 extern void standard_ExecutorFinish(QueryDesc *queryDesc);
169 extern void ExecutorEnd(QueryDesc *queryDesc);
170 extern void standard_ExecutorEnd(QueryDesc *queryDesc);
171 extern void ExecutorRewind(QueryDesc *queryDesc);
172 extern bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation);
173 extern void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation);
174 extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
175 Relation resultRelationDesc,
176 Index resultRelationIndex,
177 Relation partition_root,
178 int instrument_options);
179 extern ResultRelInfo *ExecGetTriggerResultRel(EState *estate, Oid relid);
180 extern void ExecCleanUpTriggerState(EState *estate);
181 extern bool ExecContextForcesOids(PlanState *planstate, bool *hasoids);
182 extern void ExecConstraints(ResultRelInfo *resultRelInfo,
183 TupleTableSlot *slot, EState *estate);
184 extern bool ExecPartitionCheck(ResultRelInfo *resultRelInfo,
185 TupleTableSlot *slot, EState *estate, bool emitError);
186 extern void ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
187 TupleTableSlot *slot, EState *estate);
188 extern void ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
189 TupleTableSlot *slot, EState *estate);
190 extern LockTupleMode ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo);
191 extern ExecRowMark *ExecFindRowMark(EState *estate, Index rti, bool missing_ok);
192 extern ExecAuxRowMark *ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist);
193 extern TupleTableSlot *EvalPlanQual(EState *estate, EPQState *epqstate,
194 Relation relation, Index rti, int lockmode,
195 ItemPointer tid, TransactionId priorXmax);
196 extern HeapTuple EvalPlanQualFetch(EState *estate, Relation relation,
197 int lockmode, LockWaitPolicy wait_policy, ItemPointer tid,
198 TransactionId priorXmax);
199 extern void EvalPlanQualInit(EPQState *epqstate, EState *estate,
200 Plan *subplan, List *auxrowmarks, int epqParam);
201 extern void EvalPlanQualSetPlan(EPQState *epqstate,
202 Plan *subplan, List *auxrowmarks);
203 extern void EvalPlanQualSetTuple(EPQState *epqstate, Index rti,
205 extern HeapTuple EvalPlanQualGetTuple(EPQState *epqstate, Index rti);
207 #define EvalPlanQualSetSlot(epqstate, slot) ((epqstate)->origslot = (slot))
208 extern void EvalPlanQualFetchRowMarks(EPQState *epqstate);
209 extern TupleTableSlot *EvalPlanQualNext(EPQState *epqstate);
210 extern void EvalPlanQualBegin(EPQState *epqstate, EState *parentestate);
211 extern void EvalPlanQualEnd(EPQState *epqstate);
214 * functions in execProcnode.c
216 extern PlanState *ExecInitNode(Plan *node, EState *estate, int eflags);
217 extern void ExecSetExecProcNode(PlanState *node, ExecProcNodeMtd function);
218 extern Node *MultiExecProcNode(PlanState *node);
219 extern void ExecEndNode(PlanState *node);
220 extern bool ExecShutdownNode(PlanState *node);
221 extern void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node);
224 /* ----------------------------------------------------------------
227 * Execute the given node to return a(nother) tuple.
228 * ----------------------------------------------------------------
231 static inline TupleTableSlot *
232 ExecProcNode(PlanState *node)
234 if (node->chgParam != NULL) /* something changed? */
235 ExecReScan(node); /* let ReScan handle this */
237 return node->ExecProcNode(node);
242 * prototypes from functions in execExpr.c
244 extern ExprState *ExecInitExpr(Expr *node, PlanState *parent);
245 extern ExprState *ExecInitExprWithParams(Expr *node, ParamListInfo ext_params);
246 extern ExprState *ExecInitQual(List *qual, PlanState *parent);
247 extern ExprState *ExecInitCheck(List *qual, PlanState *parent);
248 extern List *ExecInitExprList(List *nodes, PlanState *parent);
249 extern ExprState *ExecBuildAggTrans(AggState *aggstate, struct AggStatePerPhaseData *phase,
250 bool doSort, bool doHash);
251 extern ExprState *ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
252 const TupleTableSlotOps *lops, const TupleTableSlotOps *rops,
254 AttrNumber *keyColIdx,
257 extern ProjectionInfo *ExecBuildProjectionInfo(List *targetList,
258 ExprContext *econtext,
259 TupleTableSlot *slot,
261 TupleDesc inputDesc);
262 extern ExprState *ExecPrepareExpr(Expr *node, EState *estate);
263 extern ExprState *ExecPrepareQual(List *qual, EState *estate);
264 extern ExprState *ExecPrepareCheck(List *qual, EState *estate);
265 extern List *ExecPrepareExprList(List *nodes, EState *estate);
270 * Evaluate expression identified by "state" in the execution context
271 * given by "econtext". *isNull is set to the is-null flag for the result,
272 * and the Datum value is the function result.
274 * The caller should already have switched into the temporary memory
275 * context econtext->ecxt_per_tuple_memory. The convenience entry point
276 * ExecEvalExprSwitchContext() is provided for callers who don't prefer to
277 * do the switch in an outer loop.
281 ExecEvalExpr(ExprState *state,
282 ExprContext *econtext,
285 return state->evalfunc(state, econtext, isNull);
290 * ExecEvalExprSwitchContext
292 * Same as ExecEvalExpr, but get into the right allocation context explicitly.
296 ExecEvalExprSwitchContext(ExprState *state,
297 ExprContext *econtext,
301 MemoryContext oldContext;
303 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
304 retDatum = state->evalfunc(state, econtext, isNull);
305 MemoryContextSwitchTo(oldContext);
313 * Projects a tuple based on projection info and stores it in the slot passed
314 * to ExecBuildProjectInfo().
316 * Note: the result is always a virtual tuple; therefore it may reference
317 * the contents of the exprContext's scan tuples and/or temporary results
318 * constructed in the exprContext. If the caller wishes the result to be
319 * valid longer than that data will be valid, he must call ExecMaterializeSlot
320 * on the result slot.
323 static inline TupleTableSlot *
324 ExecProject(ProjectionInfo *projInfo)
326 ExprContext *econtext = projInfo->pi_exprContext;
327 ExprState *state = &projInfo->pi_state;
328 TupleTableSlot *slot = state->resultslot;
332 * Clear any former contents of the result slot. This makes it safe for
333 * us to use the slot's Datum/isnull arrays as workspace.
335 ExecClearTuple(slot);
337 /* Run the expression, discarding scalar result from the last column. */
338 (void) ExecEvalExprSwitchContext(state, econtext, &isnull);
341 * Successfully formed a result row. Mark the result slot as containing a
342 * valid virtual tuple (inlined version of ExecStoreVirtualTuple()).
344 slot->tts_flags &= ~TTS_FLAG_EMPTY;
345 slot->tts_nvalid = slot->tts_tupleDescriptor->natts;
352 * ExecQual - evaluate a qual prepared with ExecInitQual (possibly via
353 * ExecPrepareQual). Returns true if qual is satisfied, else false.
355 * Note: ExecQual used to have a third argument "resultForNull". The
356 * behavior of this function now corresponds to resultForNull == false.
357 * If you want the resultForNull == true behavior, see ExecCheck.
361 ExecQual(ExprState *state, ExprContext *econtext)
366 /* short-circuit (here and in ExecInitQual) for empty restriction list */
370 /* verify that expression was compiled using ExecInitQual */
371 Assert(state->flags & EEO_FLAG_IS_QUAL);
373 ret = ExecEvalExprSwitchContext(state, econtext, &isnull);
375 /* EEOP_QUAL should never return NULL */
378 return DatumGetBool(ret);
383 * ExecQualAndReset() - evaluate qual with ExecQual() and reset expression
388 ExecQualAndReset(ExprState *state, ExprContext *econtext)
390 bool ret = ExecQual(state, econtext);
392 /* inline ResetExprContext, to avoid ordering issue in this file */
393 MemoryContextReset(econtext->ecxt_per_tuple_memory);
398 extern bool ExecCheck(ExprState *state, ExprContext *context);
401 * prototypes from functions in execSRF.c
403 extern SetExprState *ExecInitTableFunctionResult(Expr *expr,
404 ExprContext *econtext, PlanState *parent);
405 extern Tuplestorestate *ExecMakeTableFunctionResult(SetExprState *setexpr,
406 ExprContext *econtext,
407 MemoryContext argContext,
408 TupleDesc expectedDesc,
410 extern SetExprState *ExecInitFunctionResultSet(Expr *expr,
411 ExprContext *econtext, PlanState *parent);
412 extern Datum ExecMakeFunctionResultSet(SetExprState *fcache,
413 ExprContext *econtext,
414 MemoryContext argContext,
416 ExprDoneCond *isDone);
419 * prototypes from functions in execScan.c
421 typedef TupleTableSlot *(*ExecScanAccessMtd) (ScanState *node);
422 typedef bool (*ExecScanRecheckMtd) (ScanState *node, TupleTableSlot *slot);
424 extern TupleTableSlot *ExecScan(ScanState *node, ExecScanAccessMtd accessMtd,
425 ExecScanRecheckMtd recheckMtd);
426 extern void ExecAssignScanProjectionInfo(ScanState *node);
427 extern void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno);
428 extern void ExecScanReScan(ScanState *node);
431 * prototypes from functions in execTuples.c
433 extern void ExecInitResultTypeTL(PlanState *planstate);
434 extern void ExecInitResultSlot(PlanState *planstate,
435 const TupleTableSlotOps *tts_ops);
436 extern void ExecInitResultTupleSlotTL(PlanState *planstate,
437 const TupleTableSlotOps *tts_ops);
438 extern void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate,
440 const TupleTableSlotOps *tts_ops);
441 extern TupleTableSlot *ExecInitExtraTupleSlot(EState *estate,
443 const TupleTableSlotOps *tts_ops);
444 extern TupleTableSlot *ExecInitNullTupleSlot(EState *estate, TupleDesc tupType,
445 const TupleTableSlotOps *tts_ops);
446 extern TupleDesc ExecTypeFromTL(List *targetList, bool hasoid);
447 extern TupleDesc ExecCleanTypeFromTL(List *targetList, bool hasoid);
448 extern TupleDesc ExecTypeFromExprList(List *exprList);
449 extern void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList);
450 extern void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg);
452 typedef struct TupOutputState
454 TupleTableSlot *slot;
458 extern TupOutputState *begin_tup_output_tupdesc(DestReceiver *dest,
460 const TupleTableSlotOps *tts_ops);
461 extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
462 extern void do_text_output_multiline(TupOutputState *tstate, const char *txt);
463 extern void end_tup_output(TupOutputState *tstate);
466 * Write a single line of text given as a C string.
468 * Should only be used with a single-TEXT-attribute tupdesc.
470 #define do_text_output_oneline(tstate, str_to_emit) \
474 values_[0] = PointerGetDatum(cstring_to_text(str_to_emit)); \
475 isnull_[0] = false; \
476 do_tup_output(tstate, values_, isnull_); \
477 pfree(DatumGetPointer(values_[0])); \
482 * prototypes from functions in execUtils.c
484 extern EState *CreateExecutorState(void);
485 extern void FreeExecutorState(EState *estate);
486 extern ExprContext *CreateExprContext(EState *estate);
487 extern ExprContext *CreateStandaloneExprContext(void);
488 extern void FreeExprContext(ExprContext *econtext, bool isCommit);
489 extern void ReScanExprContext(ExprContext *econtext);
491 #define ResetExprContext(econtext) \
492 MemoryContextReset((econtext)->ecxt_per_tuple_memory)
494 extern ExprContext *MakePerTupleExprContext(EState *estate);
496 /* Get an EState's per-output-tuple exprcontext, making it if first use */
497 #define GetPerTupleExprContext(estate) \
498 ((estate)->es_per_tuple_exprcontext ? \
499 (estate)->es_per_tuple_exprcontext : \
500 MakePerTupleExprContext(estate))
502 #define GetPerTupleMemoryContext(estate) \
503 (GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
505 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
506 #define ResetPerTupleExprContext(estate) \
508 if ((estate)->es_per_tuple_exprcontext) \
509 ResetExprContext((estate)->es_per_tuple_exprcontext); \
512 extern void ExecAssignExprContext(EState *estate, PlanState *planstate);
513 extern TupleDesc ExecGetResultType(PlanState *planstate);
514 extern TupleTableSlot ExecGetResultSlot(PlanState *planstate);
515 extern const TupleTableSlotOps *ExecGetResultSlotOps(PlanState *planstate,
517 extern void ExecAssignProjectionInfo(PlanState *planstate,
518 TupleDesc inputDesc);
519 extern void ExecConditionalAssignProjectionInfo(PlanState *planstate,
520 TupleDesc inputDesc, Index varno);
521 extern void ExecFreeExprContext(PlanState *planstate);
522 extern void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc);
523 extern void ExecCreateScanSlotFromOuterPlan(EState *estate,
524 ScanState *scanstate,
525 const TupleTableSlotOps *tts_ops);
527 extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid);
529 extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags);
531 extern void ExecInitRangeTable(EState *estate, List *rangeTable);
533 static inline RangeTblEntry *
534 exec_rt_fetch(Index rti, EState *estate)
536 Assert(rti > 0 && rti <= estate->es_range_table_size);
537 return estate->es_range_table_array[rti - 1];
540 extern Relation ExecGetRangeTableRelation(EState *estate, Index rti);
542 extern int executor_errposition(EState *estate, int location);
544 extern void RegisterExprContextCallback(ExprContext *econtext,
545 ExprContextCallbackFunction function,
547 extern void UnregisterExprContextCallback(ExprContext *econtext,
548 ExprContextCallbackFunction function,
551 extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
553 extern Datum GetAttributeByNum(HeapTupleHeader tuple, AttrNumber attrno,
556 extern int ExecTargetListLength(List *targetlist);
557 extern int ExecCleanTargetListLength(List *targetlist);
560 * prototypes from functions in execIndexing.c
562 extern void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative);
563 extern void ExecCloseIndices(ResultRelInfo *resultRelInfo);
564 extern List *ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid,
565 EState *estate, bool noDupErr, bool *specConflict,
566 List *arbiterIndexes);
567 extern bool ExecCheckIndexConstraints(TupleTableSlot *slot, EState *estate,
568 ItemPointer conflictTid, List *arbiterIndexes);
569 extern void check_exclusion_constraint(Relation heap, Relation index,
570 IndexInfo *indexInfo,
572 Datum *values, bool *isnull,
573 EState *estate, bool newIndex);
576 * prototypes from functions in execReplication.c
578 extern bool RelationFindReplTupleByIndex(Relation rel, Oid idxoid,
579 LockTupleMode lockmode,
580 TupleTableSlot *searchslot,
581 TupleTableSlot *outslot);
582 extern bool RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode,
583 TupleTableSlot *searchslot, TupleTableSlot *outslot);
585 extern void ExecSimpleRelationInsert(EState *estate, TupleTableSlot *slot);
586 extern void ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate,
587 TupleTableSlot *searchslot, TupleTableSlot *slot);
588 extern void ExecSimpleRelationDelete(EState *estate, EPQState *epqstate,
589 TupleTableSlot *searchslot);
590 extern void CheckCmdReplicaIdentity(Relation rel, CmdType cmd);
592 extern void CheckSubscriptionRelkind(char relkind, const char *nspname,
593 const char *relname);
595 #endif /* EXECUTOR_H */