PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
executor.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * executor.h
4  * support for the POSTGRES executor module
5  *
6  *
7  * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/executor/executor.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef EXECUTOR_H
15 #define EXECUTOR_H
16 
17 #include "executor/execdesc.h"
18 #include "nodes/parsenodes.h"
19 
20 
21 /*
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).
30  *
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.
35  *
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.)
41  *
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.
44  *
45  * MARK indicates that the plan node must support Mark/Restore calls.
46  * When this is not passed, no Mark/Restore will occur.
47  *
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.
52  *
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.
56  */
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 */
65 
66 
67 /*
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!
71  */
72 #define ExecEvalExpr(expr, econtext, isNull, isDone) \
73  ((*(expr)->evalfunc) (expr, econtext, isNull, isDone))
74 
75 
76 /* Hook for plugins to get control in ExecutorStart() */
77 typedef void (*ExecutorStart_hook_type) (QueryDesc *queryDesc, int eflags);
79 
80 /* Hook for plugins to get control in ExecutorRun() */
81 typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
82  ScanDirection direction,
83  long count);
85 
86 /* Hook for plugins to get control in ExecutorFinish() */
87 typedef void (*ExecutorFinish_hook_type) (QueryDesc *queryDesc);
89 
90 /* Hook for plugins to get control in ExecutorEnd() */
91 typedef void (*ExecutorEnd_hook_type) (QueryDesc *queryDesc);
93 
94 /* Hook for plugins to get control in ExecCheckRTPerms() */
97 
98 
99 /*
100  * prototypes from functions in execAmi.c
101  */
102 extern void ExecReScan(PlanState *node);
103 extern void ExecMarkPos(PlanState *node);
104 extern void ExecRestrPos(PlanState *node);
105 extern bool ExecSupportsMarkRestore(NodeTag plantype);
106 extern bool ExecSupportsBackwardScan(Plan *node);
107 extern bool ExecMaterializesOutput(NodeTag plantype);
108 
109 /*
110  * prototypes from functions in execCurrent.c
111  */
112 extern bool execCurrentOf(CurrentOfExpr *cexpr,
113  ExprContext *econtext,
114  Oid table_oid,
115  ItemPointer current_tid);
116 
117 /*
118  * prototypes from functions in execGrouping.c
119  */
120 extern bool execTuplesMatch(TupleTableSlot *slot1,
121  TupleTableSlot *slot2,
122  int numCols,
123  AttrNumber *matchColIdx,
124  FmgrInfo *eqfunctions,
125  MemoryContext evalContext);
126 extern bool execTuplesUnequal(TupleTableSlot *slot1,
127  TupleTableSlot *slot2,
128  int numCols,
129  AttrNumber *matchColIdx,
130  FmgrInfo *eqfunctions,
131  MemoryContext evalContext);
132 extern FmgrInfo *execTuplesMatchPrepare(int numCols,
133  Oid *eqOperators);
134 extern void execTuplesHashPrepare(int numCols,
135  Oid *eqOperators,
136  FmgrInfo **eqFunctions,
137  FmgrInfo **hashFunctions);
138 extern TupleHashTable BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
139  FmgrInfo *eqfunctions,
140  FmgrInfo *hashfunctions,
141  long nbuckets, Size entrysize,
142  MemoryContext tablecxt,
143  MemoryContext tempcxt);
145  TupleTableSlot *slot,
146  bool *isnew);
148  TupleTableSlot *slot,
149  FmgrInfo *eqfunctions,
150  FmgrInfo *hashfunctions);
151 
152 /*
153  * prototypes from functions in execJunk.c
154  */
155 extern JunkFilter *ExecInitJunkFilter(List *targetList, bool hasoid,
156  TupleTableSlot *slot);
157 extern JunkFilter *ExecInitJunkFilterConversion(List *targetList,
158  TupleDesc cleanTupType,
159  TupleTableSlot *slot);
160 extern AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter,
161  const char *attrName);
162 extern AttrNumber ExecFindJunkAttributeInTlist(List *targetlist,
163  const char *attrName);
165  bool *isNull);
166 extern TupleTableSlot *ExecFilterJunk(JunkFilter *junkfilter,
167  TupleTableSlot *slot);
168 
169 
170 /*
171  * prototypes from functions in execMain.c
172  */
173 extern void ExecutorStart(QueryDesc *queryDesc, int eflags);
174 extern void standard_ExecutorStart(QueryDesc *queryDesc, int eflags);
175 extern void ExecutorRun(QueryDesc *queryDesc,
176  ScanDirection direction, long count);
177 extern void standard_ExecutorRun(QueryDesc *queryDesc,
178  ScanDirection direction, long count);
179 extern void ExecutorFinish(QueryDesc *queryDesc);
180 extern void standard_ExecutorFinish(QueryDesc *queryDesc);
181 extern void ExecutorEnd(QueryDesc *queryDesc);
182 extern void standard_ExecutorEnd(QueryDesc *queryDesc);
183 extern void ExecutorRewind(QueryDesc *queryDesc);
184 extern bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation);
185 extern void CheckValidResultRel(Relation resultRel, CmdType operation);
186 extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
187  Relation resultRelationDesc,
188  Index resultRelationIndex,
189  int instrument_options);
190 extern ResultRelInfo *ExecGetTriggerResultRel(EState *estate, Oid relid);
191 extern bool ExecContextForcesOids(PlanState *planstate, bool *hasoids);
192 extern void ExecConstraints(ResultRelInfo *resultRelInfo,
193  TupleTableSlot *slot, EState *estate);
194 extern void ExecWithCheckOptions(ResultRelInfo *resultRelInfo,
195  TupleTableSlot *slot, EState *estate);
196 extern ExecRowMark *ExecFindRowMark(EState *estate, Index rti);
197 extern ExecAuxRowMark *ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist);
198 extern TupleTableSlot *EvalPlanQual(EState *estate, EPQState *epqstate,
199  Relation relation, Index rti, int lockmode,
200  ItemPointer tid, TransactionId priorXmax);
201 extern HeapTuple EvalPlanQualFetch(EState *estate, Relation relation,
202  int lockmode, ItemPointer tid, TransactionId priorXmax);
203 extern void EvalPlanQualInit(EPQState *epqstate, EState *estate,
204  Plan *subplan, List *auxrowmarks, int epqParam);
205 extern void EvalPlanQualSetPlan(EPQState *epqstate,
206  Plan *subplan, List *auxrowmarks);
207 extern void EvalPlanQualSetTuple(EPQState *epqstate, Index rti,
208  HeapTuple tuple);
209 extern HeapTuple EvalPlanQualGetTuple(EPQState *epqstate, Index rti);
210 
211 #define EvalPlanQualSetSlot(epqstate, slot) ((epqstate)->origslot = (slot))
212 extern void EvalPlanQualFetchRowMarks(EPQState *epqstate);
213 extern TupleTableSlot *EvalPlanQualNext(EPQState *epqstate);
214 extern void EvalPlanQualBegin(EPQState *epqstate, EState *parentestate);
215 extern void EvalPlanQualEnd(EPQState *epqstate);
216 
217 /*
218  * prototypes from functions in execProcnode.c
219  */
220 extern PlanState *ExecInitNode(Plan *node, EState *estate, int eflags);
221 extern TupleTableSlot *ExecProcNode(PlanState *node);
222 extern Node *MultiExecProcNode(PlanState *node);
223 extern void ExecEndNode(PlanState *node);
224 
225 /*
226  * prototypes from functions in execQual.c
227  */
229  bool *isNull);
230 extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
231  bool *isNull);
233  ExprContext *econtext,
234  TupleDesc expectedDesc,
235  bool randomAccess);
236 extern Datum ExecEvalExprSwitchContext(ExprState *expression, ExprContext *econtext,
237  bool *isNull, ExprDoneCond *isDone);
238 extern ExprState *ExecInitExpr(Expr *node, PlanState *parent);
239 extern ExprState *ExecPrepareExpr(Expr *node, EState *estate);
240 extern bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull);
241 extern int ExecTargetListLength(List *targetlist);
242 extern int ExecCleanTargetListLength(List *targetlist);
243 extern TupleTableSlot *ExecProject(ProjectionInfo *projInfo,
244  ExprDoneCond *isDone);
245 
246 /*
247  * prototypes from functions in execScan.c
248  */
249 typedef TupleTableSlot *(*ExecScanAccessMtd) (ScanState *node);
251 
252 extern TupleTableSlot *ExecScan(ScanState *node, ExecScanAccessMtd accessMtd,
253  ExecScanRecheckMtd recheckMtd);
254 extern void ExecAssignScanProjectionInfo(ScanState *node);
255 extern void ExecScanReScan(ScanState *node);
256 
257 /*
258  * prototypes from functions in execTuples.c
259  */
260 extern void ExecInitResultTupleSlot(EState *estate, PlanState *planstate);
261 extern void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate);
264  TupleDesc tupType);
265 extern TupleDesc ExecTypeFromTL(List *targetList, bool hasoid);
266 extern TupleDesc ExecCleanTypeFromTL(List *targetList, bool hasoid);
267 extern TupleDesc ExecTypeFromExprList(List *exprList, List *namesList);
268 extern void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg);
269 
270 typedef struct TupOutputState
271 {
275 
277  TupleDesc tupdesc);
278 extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
279 extern void do_text_output_multiline(TupOutputState *tstate, char *text);
280 extern void end_tup_output(TupOutputState *tstate);
281 
282 /*
283  * Write a single line of text given as a C string.
284  *
285  * Should only be used with a single-TEXT-attribute tupdesc.
286  */
287 #define do_text_output_oneline(tstate, str_to_emit) \
288  do { \
289  Datum values_[1]; \
290  bool isnull_[1]; \
291  values_[0] = PointerGetDatum(cstring_to_text(str_to_emit)); \
292  isnull_[0] = false; \
293  do_tup_output(tstate, values_, isnull_); \
294  pfree(DatumGetPointer(values_[0])); \
295  } while (0)
296 
297 
298 /*
299  * prototypes from functions in execUtils.c
300  */
301 extern EState *CreateExecutorState(void);
302 extern void FreeExecutorState(EState *estate);
303 extern ExprContext *CreateExprContext(EState *estate);
305 extern void FreeExprContext(ExprContext *econtext, bool isCommit);
306 extern void ReScanExprContext(ExprContext *econtext);
307 
308 #define ResetExprContext(econtext) \
309  MemoryContextReset((econtext)->ecxt_per_tuple_memory)
310 
312 
313 /* Get an EState's per-output-tuple exprcontext, making it if first use */
314 #define GetPerTupleExprContext(estate) \
315  ((estate)->es_per_tuple_exprcontext ? \
316  (estate)->es_per_tuple_exprcontext : \
317  MakePerTupleExprContext(estate))
318 
319 #define GetPerTupleMemoryContext(estate) \
320  (GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
321 
322 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
323 #define ResetPerTupleExprContext(estate) \
324  do { \
325  if ((estate)->es_per_tuple_exprcontext) \
326  ResetExprContext((estate)->es_per_tuple_exprcontext); \
327  } while (0)
328 
329 extern void ExecAssignExprContext(EState *estate, PlanState *planstate);
330 extern void ExecAssignResultType(PlanState *planstate, TupleDesc tupDesc);
331 extern void ExecAssignResultTypeFromTL(PlanState *planstate);
332 extern TupleDesc ExecGetResultType(PlanState *planstate);
333 extern ProjectionInfo *ExecBuildProjectionInfo(List *targetList,
334  ExprContext *econtext,
335  TupleTableSlot *slot,
336  TupleDesc inputDesc);
337 extern void ExecAssignProjectionInfo(PlanState *planstate,
338  TupleDesc inputDesc);
339 extern void ExecFreeExprContext(PlanState *planstate);
340 extern TupleDesc ExecGetScanType(ScanState *scanstate);
341 extern void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc);
342 extern void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate);
343 
344 extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid);
345 
346 extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags);
347 extern void ExecCloseScanRelation(Relation scanrel);
348 
349 extern void ExecOpenIndices(ResultRelInfo *resultRelInfo);
350 extern void ExecCloseIndices(ResultRelInfo *resultRelInfo);
352  EState *estate);
354  IndexInfo *indexInfo,
355  ItemPointer tupleid,
356  Datum *values, bool *isnull,
357  EState *estate,
358  bool newIndex, bool errorOK);
359 
360 extern void RegisterExprContextCallback(ExprContext *econtext,
362  Datum arg);
363 extern void UnregisterExprContextCallback(ExprContext *econtext,
365  Datum arg);
366 
367 #endif /* EXECUTOR_H */