1 /*-------------------------------------------------------------------------
4 * support for the POSTGRES executor module
7 * Portions Copyright (c) 1996-2016, 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"
22 * The "eflags" argument to ExecutorStart and the various ExecInitNode
23 * routines is a bitwise OR of the following flag bits, which tell the
24 * called plan node what to expect. Note that the flags will get modified
25 * as they are passed down the plan tree, since an upper node may require
26 * functionality in its subnode not demanded of the plan as a whole
27 * (example: MergeJoin requires mark/restore capability in its inner input),
28 * or an upper node may shield its input from some functionality requirement
29 * (example: Materialize shields its input from needing to do backward scan).
31 * EXPLAIN_ONLY indicates that the plan tree is being initialized just so
32 * EXPLAIN can print it out; it will not be run. Hence, no side-effects
33 * of startup should occur. However, error checks (such as permission checks)
34 * should be performed.
36 * REWIND indicates that the plan node should try to efficiently support
37 * rescans without parameter changes. (Nodes must support ExecReScan calls
38 * in any case, but if this flag was not given, they are at liberty to do it
39 * through complete recalculation. Note that a parameter change forces a
40 * full recalculation in any case.)
42 * BACKWARD indicates that the plan node must respect the es_direction flag.
43 * When this is not passed, the plan node will only be run forwards.
45 * MARK indicates that the plan node must support Mark/Restore calls.
46 * When this is not passed, no Mark/Restore will occur.
48 * SKIP_TRIGGERS tells ExecutorStart/ExecutorFinish to skip calling
49 * AfterTriggerBeginQuery/AfterTriggerEndQuery. This does not necessarily
50 * mean that the plan can't queue any AFTER triggers; just that the caller
51 * is responsible for there being a trigger context for them to be queued in.
53 * WITH/WITHOUT_OIDS tell the executor to emit tuples with or without space
54 * for OIDs, respectively. These are currently used only for CREATE TABLE AS.
55 * If neither is set, the plan may or may not produce tuples including OIDs.
57 #define EXEC_FLAG_EXPLAIN_ONLY 0x0001 /* EXPLAIN, no ANALYZE */
58 #define EXEC_FLAG_REWIND 0x0002 /* need efficient rescan */
59 #define EXEC_FLAG_BACKWARD 0x0004 /* need backward scan */
60 #define EXEC_FLAG_MARK 0x0008 /* need mark/restore */
61 #define EXEC_FLAG_SKIP_TRIGGERS 0x0010 /* skip AfterTrigger calls */
62 #define EXEC_FLAG_WITH_OIDS 0x0020 /* force OIDs in returned tuples */
63 #define EXEC_FLAG_WITHOUT_OIDS 0x0040 /* force no OIDs in returned tuples */
64 #define EXEC_FLAG_WITH_NO_DATA 0x0080 /* rel scannability doesn't matter */
68 * ExecEvalExpr was formerly a function containing a switch statement;
69 * now it's just a macro invoking the function pointed to by an ExprState
70 * node. Beware of double evaluation of the ExprState argument!
72 #define ExecEvalExpr(expr, econtext, isNull, isDone) \
73 ((*(expr)->evalfunc) (expr, econtext, isNull, isDone))
76 /* Hook for plugins to get control in ExecutorStart() */
77 typedef void (*ExecutorStart_hook_type) (QueryDesc *queryDesc, int eflags);
78 extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook;
80 /* Hook for plugins to get control in ExecutorRun() */
81 typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
82 ScanDirection direction,
84 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;
86 /* Hook for plugins to get control in ExecutorFinish() */
87 typedef void (*ExecutorFinish_hook_type) (QueryDesc *queryDesc);
88 extern PGDLLIMPORT ExecutorFinish_hook_type ExecutorFinish_hook;
90 /* Hook for plugins to get control in ExecutorEnd() */
91 typedef void (*ExecutorEnd_hook_type) (QueryDesc *queryDesc);
92 extern PGDLLIMPORT ExecutorEnd_hook_type ExecutorEnd_hook;
94 /* Hook for plugins to get control in ExecCheckRTPerms() */
95 typedef bool (*ExecutorCheckPerms_hook_type) (List *, bool);
96 extern PGDLLIMPORT ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook;
100 * prototypes from functions in execAmi.c
102 struct Path; /* avoid including relation.h here */
104 extern void ExecReScan(PlanState *node);
105 extern void ExecMarkPos(PlanState *node);
106 extern void ExecRestrPos(PlanState *node);
107 extern bool ExecSupportsMarkRestore(struct Path *pathnode);
108 extern bool ExecSupportsBackwardScan(Plan *node);
109 extern bool ExecMaterializesOutput(NodeTag plantype);
112 * prototypes from functions in execCurrent.c
114 extern bool execCurrentOf(CurrentOfExpr *cexpr,
115 ExprContext *econtext,
117 ItemPointer current_tid);
120 * prototypes from functions in execGrouping.c
122 extern bool execTuplesMatch(TupleTableSlot *slot1,
123 TupleTableSlot *slot2,
125 AttrNumber *matchColIdx,
126 FmgrInfo *eqfunctions,
127 MemoryContext evalContext);
128 extern bool execTuplesUnequal(TupleTableSlot *slot1,
129 TupleTableSlot *slot2,
131 AttrNumber *matchColIdx,
132 FmgrInfo *eqfunctions,
133 MemoryContext evalContext);
134 extern FmgrInfo *execTuplesMatchPrepare(int numCols,
136 extern void execTuplesHashPrepare(int numCols,
138 FmgrInfo **eqFunctions,
139 FmgrInfo **hashFunctions);
140 extern TupleHashTable BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
141 FmgrInfo *eqfunctions,
142 FmgrInfo *hashfunctions,
143 long nbuckets, Size entrysize,
144 MemoryContext tablecxt,
145 MemoryContext tempcxt);
146 extern TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable,
147 TupleTableSlot *slot,
149 extern TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable,
150 TupleTableSlot *slot,
151 FmgrInfo *eqfunctions,
152 FmgrInfo *hashfunctions);
155 * prototypes from functions in execJunk.c
157 extern JunkFilter *ExecInitJunkFilter(List *targetList, bool hasoid,
158 TupleTableSlot *slot);
159 extern JunkFilter *ExecInitJunkFilterConversion(List *targetList,
160 TupleDesc cleanTupType,
161 TupleTableSlot *slot);
162 extern AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter,
163 const char *attrName);
164 extern AttrNumber ExecFindJunkAttributeInTlist(List *targetlist,
165 const char *attrName);
166 extern Datum ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno,
168 extern TupleTableSlot *ExecFilterJunk(JunkFilter *junkfilter,
169 TupleTableSlot *slot);
173 * prototypes from functions in execMain.c
175 extern void ExecutorStart(QueryDesc *queryDesc, int eflags);
176 extern void standard_ExecutorStart(QueryDesc *queryDesc, int eflags);
177 extern void ExecutorRun(QueryDesc *queryDesc,
178 ScanDirection direction, long count);
179 extern void standard_ExecutorRun(QueryDesc *queryDesc,
180 ScanDirection direction, long count);
181 extern void ExecutorFinish(QueryDesc *queryDesc);
182 extern void standard_ExecutorFinish(QueryDesc *queryDesc);
183 extern void ExecutorEnd(QueryDesc *queryDesc);
184 extern void standard_ExecutorEnd(QueryDesc *queryDesc);
185 extern void ExecutorRewind(QueryDesc *queryDesc);
186 extern bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation);
187 extern void CheckValidResultRel(Relation resultRel, CmdType operation);
188 extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
189 Relation resultRelationDesc,
190 Index resultRelationIndex,
191 int instrument_options);
192 extern ResultRelInfo *ExecGetTriggerResultRel(EState *estate, Oid relid);
193 extern bool ExecContextForcesOids(PlanState *planstate, bool *hasoids);
194 extern void ExecConstraints(ResultRelInfo *resultRelInfo,
195 TupleTableSlot *slot, EState *estate);
196 extern void ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
197 TupleTableSlot *slot, EState *estate);
198 extern LockTupleMode ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo);
199 extern ExecRowMark *ExecFindRowMark(EState *estate, Index rti, bool missing_ok);
200 extern ExecAuxRowMark *ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist);
201 extern TupleTableSlot *EvalPlanQual(EState *estate, EPQState *epqstate,
202 Relation relation, Index rti, int lockmode,
203 ItemPointer tid, TransactionId priorXmax);
204 extern HeapTuple EvalPlanQualFetch(EState *estate, Relation relation,
205 int lockmode, LockWaitPolicy wait_policy, ItemPointer tid,
206 TransactionId priorXmax);
207 extern void EvalPlanQualInit(EPQState *epqstate, EState *estate,
208 Plan *subplan, List *auxrowmarks, int epqParam);
209 extern void EvalPlanQualSetPlan(EPQState *epqstate,
210 Plan *subplan, List *auxrowmarks);
211 extern void EvalPlanQualSetTuple(EPQState *epqstate, Index rti,
213 extern HeapTuple EvalPlanQualGetTuple(EPQState *epqstate, Index rti);
215 #define EvalPlanQualSetSlot(epqstate, slot) ((epqstate)->origslot = (slot))
216 extern void EvalPlanQualFetchRowMarks(EPQState *epqstate);
217 extern TupleTableSlot *EvalPlanQualNext(EPQState *epqstate);
218 extern void EvalPlanQualBegin(EPQState *epqstate, EState *parentestate);
219 extern void EvalPlanQualEnd(EPQState *epqstate);
222 * prototypes from functions in execProcnode.c
224 extern PlanState *ExecInitNode(Plan *node, EState *estate, int eflags);
225 extern TupleTableSlot *ExecProcNode(PlanState *node);
226 extern Node *MultiExecProcNode(PlanState *node);
227 extern void ExecEndNode(PlanState *node);
228 extern bool ExecShutdownNode(PlanState *node);
231 * prototypes from functions in execQual.c
233 extern Datum GetAttributeByNum(HeapTupleHeader tuple, AttrNumber attrno,
235 extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
237 extern Tuplestorestate *ExecMakeTableFunctionResult(ExprState *funcexpr,
238 ExprContext *econtext,
239 MemoryContext argContext,
240 TupleDesc expectedDesc,
242 extern Datum ExecEvalExprSwitchContext(ExprState *expression, ExprContext *econtext,
243 bool *isNull, ExprDoneCond *isDone);
244 extern ExprState *ExecInitExpr(Expr *node, PlanState *parent);
245 extern ExprState *ExecPrepareExpr(Expr *node, EState *estate);
246 extern bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull);
247 extern int ExecTargetListLength(List *targetlist);
248 extern int ExecCleanTargetListLength(List *targetlist);
249 extern TupleTableSlot *ExecProject(ProjectionInfo *projInfo,
250 ExprDoneCond *isDone);
253 * prototypes from functions in execScan.c
255 typedef TupleTableSlot *(*ExecScanAccessMtd) (ScanState *node);
256 typedef bool (*ExecScanRecheckMtd) (ScanState *node, TupleTableSlot *slot);
258 extern TupleTableSlot *ExecScan(ScanState *node, ExecScanAccessMtd accessMtd,
259 ExecScanRecheckMtd recheckMtd);
260 extern void ExecAssignScanProjectionInfo(ScanState *node);
261 extern void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno);
262 extern void ExecScanReScan(ScanState *node);
265 * prototypes from functions in execTuples.c
267 extern void ExecInitResultTupleSlot(EState *estate, PlanState *planstate);
268 extern void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate);
269 extern TupleTableSlot *ExecInitExtraTupleSlot(EState *estate);
270 extern TupleTableSlot *ExecInitNullTupleSlot(EState *estate,
272 extern TupleDesc ExecTypeFromTL(List *targetList, bool hasoid);
273 extern TupleDesc ExecCleanTypeFromTL(List *targetList, bool hasoid);
274 extern TupleDesc ExecTypeFromExprList(List *exprList);
275 extern void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList);
276 extern void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg);
278 typedef struct TupOutputState
280 TupleTableSlot *slot;
284 extern TupOutputState *begin_tup_output_tupdesc(DestReceiver *dest,
286 extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
287 extern void do_text_output_multiline(TupOutputState *tstate, char *text);
288 extern void end_tup_output(TupOutputState *tstate);
291 * Write a single line of text given as a C string.
293 * Should only be used with a single-TEXT-attribute tupdesc.
295 #define do_text_output_oneline(tstate, str_to_emit) \
299 values_[0] = PointerGetDatum(cstring_to_text(str_to_emit)); \
300 isnull_[0] = false; \
301 do_tup_output(tstate, values_, isnull_); \
302 pfree(DatumGetPointer(values_[0])); \
307 * prototypes from functions in execUtils.c
309 extern EState *CreateExecutorState(void);
310 extern void FreeExecutorState(EState *estate);
311 extern ExprContext *CreateExprContext(EState *estate);
312 extern ExprContext *CreateStandaloneExprContext(void);
313 extern void FreeExprContext(ExprContext *econtext, bool isCommit);
314 extern void ReScanExprContext(ExprContext *econtext);
316 #define ResetExprContext(econtext) \
317 MemoryContextReset((econtext)->ecxt_per_tuple_memory)
319 extern ExprContext *MakePerTupleExprContext(EState *estate);
321 /* Get an EState's per-output-tuple exprcontext, making it if first use */
322 #define GetPerTupleExprContext(estate) \
323 ((estate)->es_per_tuple_exprcontext ? \
324 (estate)->es_per_tuple_exprcontext : \
325 MakePerTupleExprContext(estate))
327 #define GetPerTupleMemoryContext(estate) \
328 (GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
330 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
331 #define ResetPerTupleExprContext(estate) \
333 if ((estate)->es_per_tuple_exprcontext) \
334 ResetExprContext((estate)->es_per_tuple_exprcontext); \
337 extern void ExecAssignExprContext(EState *estate, PlanState *planstate);
338 extern void ExecAssignResultType(PlanState *planstate, TupleDesc tupDesc);
339 extern void ExecAssignResultTypeFromTL(PlanState *planstate);
340 extern TupleDesc ExecGetResultType(PlanState *planstate);
341 extern ProjectionInfo *ExecBuildProjectionInfo(List *targetList,
342 ExprContext *econtext,
343 TupleTableSlot *slot,
344 TupleDesc inputDesc);
345 extern void ExecAssignProjectionInfo(PlanState *planstate,
346 TupleDesc inputDesc);
347 extern void ExecFreeExprContext(PlanState *planstate);
348 extern void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc);
349 extern void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate);
351 extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid);
353 extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags);
354 extern void ExecCloseScanRelation(Relation scanrel);
356 extern void RegisterExprContextCallback(ExprContext *econtext,
357 ExprContextCallbackFunction function,
359 extern void UnregisterExprContextCallback(ExprContext *econtext,
360 ExprContextCallbackFunction function,
364 * prototypes from functions in execIndexing.c
366 extern void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative);
367 extern void ExecCloseIndices(ResultRelInfo *resultRelInfo);
368 extern List *ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid,
369 EState *estate, bool noDupErr, bool *specConflict,
370 List *arbiterIndexes);
371 extern bool ExecCheckIndexConstraints(TupleTableSlot *slot, EState *estate,
372 ItemPointer conflictTid, List *arbiterIndexes);
373 extern void check_exclusion_constraint(Relation heap, Relation index,
374 IndexInfo *indexInfo,
376 Datum *values, bool *isnull,
377 EState *estate, bool newIndex);
380 #endif /* EXECUTOR_H */