]> granicus.if.org Git - postgresql/blob - src/include/executor/executor.h
Allow for parallel execution whenever ExecutorRun() is done only once.
[postgresql] / src / include / executor / executor.h
1 /*-------------------------------------------------------------------------
2  *
3  * executor.h
4  *        support for the POSTGRES executor module
5  *
6  *
7  * Portions Copyright (c) 1996-2017, 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 "catalog/partition.h"
18 #include "executor/execdesc.h"
19 #include "nodes/parsenodes.h"
20
21
22 /*
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).
31  *
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.
36  *
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.)
42  *
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.
45  *
46  * MARK indicates that the plan node must support Mark/Restore calls.
47  * When this is not passed, no Mark/Restore will occur.
48  *
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.
53  *
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.
57  */
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 */
66
67
68 /*
69  * ExecEvalExpr was formerly a function containing a switch statement;
70  * now it's just a macro invoking the function pointed to by an ExprState
71  * node.  Beware of double evaluation of the ExprState argument!
72  */
73 #define ExecEvalExpr(expr, econtext, isNull) \
74         ((*(expr)->evalfunc) (expr, econtext, isNull))
75
76
77 /* Hook for plugins to get control in ExecutorStart() */
78 typedef void (*ExecutorStart_hook_type) (QueryDesc *queryDesc, int eflags);
79 extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook;
80
81 /* Hook for plugins to get control in ExecutorRun() */
82 typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
83                                                                                                    ScanDirection direction,
84                                                                                                    uint64 count,
85                                                                                                    bool execute_once);
86 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;
87
88 /* Hook for plugins to get control in ExecutorFinish() */
89 typedef void (*ExecutorFinish_hook_type) (QueryDesc *queryDesc);
90 extern PGDLLIMPORT ExecutorFinish_hook_type ExecutorFinish_hook;
91
92 /* Hook for plugins to get control in ExecutorEnd() */
93 typedef void (*ExecutorEnd_hook_type) (QueryDesc *queryDesc);
94 extern PGDLLIMPORT ExecutorEnd_hook_type ExecutorEnd_hook;
95
96 /* Hook for plugins to get control in ExecCheckRTPerms() */
97 typedef bool (*ExecutorCheckPerms_hook_type) (List *, bool);
98 extern PGDLLIMPORT ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook;
99
100
101 /*
102  * prototypes from functions in execAmi.c
103  */
104 struct Path;                                    /* avoid including relation.h here */
105
106 extern void ExecReScan(PlanState *node);
107 extern void ExecMarkPos(PlanState *node);
108 extern void ExecRestrPos(PlanState *node);
109 extern bool ExecSupportsMarkRestore(struct Path *pathnode);
110 extern bool ExecSupportsBackwardScan(Plan *node);
111 extern bool ExecMaterializesOutput(NodeTag plantype);
112
113 /*
114  * prototypes from functions in execCurrent.c
115  */
116 extern bool execCurrentOf(CurrentOfExpr *cexpr,
117                           ExprContext *econtext,
118                           Oid table_oid,
119                           ItemPointer current_tid);
120
121 /*
122  * prototypes from functions in execGrouping.c
123  */
124 extern bool execTuplesMatch(TupleTableSlot *slot1,
125                                 TupleTableSlot *slot2,
126                                 int numCols,
127                                 AttrNumber *matchColIdx,
128                                 FmgrInfo *eqfunctions,
129                                 MemoryContext evalContext);
130 extern bool execTuplesUnequal(TupleTableSlot *slot1,
131                                   TupleTableSlot *slot2,
132                                   int numCols,
133                                   AttrNumber *matchColIdx,
134                                   FmgrInfo *eqfunctions,
135                                   MemoryContext evalContext);
136 extern FmgrInfo *execTuplesMatchPrepare(int numCols,
137                                            Oid *eqOperators);
138 extern void execTuplesHashPrepare(int numCols,
139                                           Oid *eqOperators,
140                                           FmgrInfo **eqFunctions,
141                                           FmgrInfo **hashFunctions);
142 extern TupleHashTable BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
143                                         FmgrInfo *eqfunctions,
144                                         FmgrInfo *hashfunctions,
145                                         long nbuckets, Size additionalsize,
146                                         MemoryContext tablecxt,
147                                         MemoryContext tempcxt, bool use_variable_hash_iv);
148 extern TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable,
149                                          TupleTableSlot *slot,
150                                          bool *isnew);
151 extern TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable,
152                                    TupleTableSlot *slot,
153                                    FmgrInfo *eqfunctions,
154                                    FmgrInfo *hashfunctions);
155
156 /*
157  * prototypes from functions in execJunk.c
158  */
159 extern JunkFilter *ExecInitJunkFilter(List *targetList, bool hasoid,
160                                    TupleTableSlot *slot);
161 extern JunkFilter *ExecInitJunkFilterConversion(List *targetList,
162                                                          TupleDesc cleanTupType,
163                                                          TupleTableSlot *slot);
164 extern AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter,
165                                           const char *attrName);
166 extern AttrNumber ExecFindJunkAttributeInTlist(List *targetlist,
167                                                          const char *attrName);
168 extern Datum ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno,
169                                          bool *isNull);
170 extern TupleTableSlot *ExecFilterJunk(JunkFilter *junkfilter,
171                            TupleTableSlot *slot);
172
173
174 /*
175  * prototypes from functions in execMain.c
176  */
177 extern void ExecutorStart(QueryDesc *queryDesc, int eflags);
178 extern void standard_ExecutorStart(QueryDesc *queryDesc, int eflags);
179 extern void ExecutorRun(QueryDesc *queryDesc,
180                         ScanDirection direction, uint64 count, bool execute_once);
181 extern void standard_ExecutorRun(QueryDesc *queryDesc,
182                                          ScanDirection direction, uint64 count, bool execute_once);
183 extern void ExecutorFinish(QueryDesc *queryDesc);
184 extern void standard_ExecutorFinish(QueryDesc *queryDesc);
185 extern void ExecutorEnd(QueryDesc *queryDesc);
186 extern void standard_ExecutorEnd(QueryDesc *queryDesc);
187 extern void ExecutorRewind(QueryDesc *queryDesc);
188 extern bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation);
189 extern void CheckValidResultRel(Relation resultRel, CmdType operation);
190 extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
191                                   Relation resultRelationDesc,
192                                   Index resultRelationIndex,
193                                   Relation partition_root,
194                                   int instrument_options);
195 extern ResultRelInfo *ExecGetTriggerResultRel(EState *estate, Oid relid);
196 extern bool ExecContextForcesOids(PlanState *planstate, bool *hasoids);
197 extern void ExecConstraints(ResultRelInfo *resultRelInfo,
198                                 TupleTableSlot *slot, TupleTableSlot *orig_slot,
199                                 EState *estate);
200 extern void ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
201                                          TupleTableSlot *slot, EState *estate);
202 extern LockTupleMode ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo);
203 extern ExecRowMark *ExecFindRowMark(EState *estate, Index rti, bool missing_ok);
204 extern ExecAuxRowMark *ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist);
205 extern TupleTableSlot *EvalPlanQual(EState *estate, EPQState *epqstate,
206                          Relation relation, Index rti, int lockmode,
207                          ItemPointer tid, TransactionId priorXmax);
208 extern HeapTuple EvalPlanQualFetch(EState *estate, Relation relation,
209                                   int lockmode, LockWaitPolicy wait_policy, ItemPointer tid,
210                                   TransactionId priorXmax);
211 extern void EvalPlanQualInit(EPQState *epqstate, EState *estate,
212                                  Plan *subplan, List *auxrowmarks, int epqParam);
213 extern void EvalPlanQualSetPlan(EPQState *epqstate,
214                                         Plan *subplan, List *auxrowmarks);
215 extern void EvalPlanQualSetTuple(EPQState *epqstate, Index rti,
216                                          HeapTuple tuple);
217 extern HeapTuple EvalPlanQualGetTuple(EPQState *epqstate, Index rti);
218 extern void ExecSetupPartitionTupleRouting(Relation rel,
219                                                            PartitionDispatch **pd,
220                                                            ResultRelInfo **partitions,
221                                                            TupleConversionMap ***tup_conv_maps,
222                                                            TupleTableSlot **partition_tuple_slot,
223                                                            int *num_parted, int *num_partitions);
224 extern int ExecFindPartition(ResultRelInfo *resultRelInfo,
225                                   PartitionDispatch *pd,
226                                   TupleTableSlot *slot,
227                                   EState *estate);
228
229 #define EvalPlanQualSetSlot(epqstate, slot)  ((epqstate)->origslot = (slot))
230 extern void EvalPlanQualFetchRowMarks(EPQState *epqstate);
231 extern TupleTableSlot *EvalPlanQualNext(EPQState *epqstate);
232 extern void EvalPlanQualBegin(EPQState *epqstate, EState *parentestate);
233 extern void EvalPlanQualEnd(EPQState *epqstate);
234
235 /*
236  * prototypes from functions in execProcnode.c
237  */
238 extern PlanState *ExecInitNode(Plan *node, EState *estate, int eflags);
239 extern TupleTableSlot *ExecProcNode(PlanState *node);
240 extern Node *MultiExecProcNode(PlanState *node);
241 extern void ExecEndNode(PlanState *node);
242 extern bool ExecShutdownNode(PlanState *node);
243
244 /*
245  * prototypes from functions in execQual.c
246  */
247 extern Datum GetAttributeByNum(HeapTupleHeader tuple, AttrNumber attrno,
248                                   bool *isNull);
249 extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
250                                    bool *isNull);
251 extern Tuplestorestate *ExecMakeTableFunctionResult(ExprState *funcexpr,
252                                                         ExprContext *econtext,
253                                                         MemoryContext argContext,
254                                                         TupleDesc expectedDesc,
255                                                         bool randomAccess);
256 extern Datum ExecMakeFunctionResultSet(FuncExprState *fcache,
257                                                   ExprContext *econtext,
258                                                   bool *isNull,
259                                                   ExprDoneCond *isDone);
260 extern Datum ExecEvalExprSwitchContext(ExprState *expression, ExprContext *econtext,
261                                                   bool *isNull);
262 extern ExprState *ExecInitExpr(Expr *node, PlanState *parent);
263 extern ExprState *ExecPrepareExpr(Expr *node, EState *estate);
264 extern bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull);
265 extern int      ExecTargetListLength(List *targetlist);
266 extern int      ExecCleanTargetListLength(List *targetlist);
267 extern TupleTableSlot *ExecProject(ProjectionInfo *projInfo);
268
269 /*
270  * prototypes from functions in execScan.c
271  */
272 typedef TupleTableSlot *(*ExecScanAccessMtd) (ScanState *node);
273 typedef bool (*ExecScanRecheckMtd) (ScanState *node, TupleTableSlot *slot);
274
275 extern TupleTableSlot *ExecScan(ScanState *node, ExecScanAccessMtd accessMtd,
276                  ExecScanRecheckMtd recheckMtd);
277 extern void ExecAssignScanProjectionInfo(ScanState *node);
278 extern void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno);
279 extern void ExecScanReScan(ScanState *node);
280
281 /*
282  * prototypes from functions in execTuples.c
283  */
284 extern void ExecInitResultTupleSlot(EState *estate, PlanState *planstate);
285 extern void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate);
286 extern TupleTableSlot *ExecInitExtraTupleSlot(EState *estate);
287 extern TupleTableSlot *ExecInitNullTupleSlot(EState *estate,
288                                           TupleDesc tupType);
289 extern TupleDesc ExecTypeFromTL(List *targetList, bool hasoid);
290 extern TupleDesc ExecCleanTypeFromTL(List *targetList, bool hasoid);
291 extern TupleDesc ExecTypeFromExprList(List *exprList);
292 extern void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList);
293 extern void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg);
294
295 typedef struct TupOutputState
296 {
297         TupleTableSlot *slot;
298         DestReceiver *dest;
299 } TupOutputState;
300
301 extern TupOutputState *begin_tup_output_tupdesc(DestReceiver *dest,
302                                                  TupleDesc tupdesc);
303 extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
304 extern void do_text_output_multiline(TupOutputState *tstate, const char *txt);
305 extern void end_tup_output(TupOutputState *tstate);
306
307 /*
308  * Write a single line of text given as a C string.
309  *
310  * Should only be used with a single-TEXT-attribute tupdesc.
311  */
312 #define do_text_output_oneline(tstate, str_to_emit) \
313         do { \
314                 Datum   values_[1]; \
315                 bool    isnull_[1]; \
316                 values_[0] = PointerGetDatum(cstring_to_text(str_to_emit)); \
317                 isnull_[0] = false; \
318                 do_tup_output(tstate, values_, isnull_); \
319                 pfree(DatumGetPointer(values_[0])); \
320         } while (0)
321
322
323 /*
324  * prototypes from functions in execUtils.c
325  */
326 extern EState *CreateExecutorState(void);
327 extern void FreeExecutorState(EState *estate);
328 extern ExprContext *CreateExprContext(EState *estate);
329 extern ExprContext *CreateStandaloneExprContext(void);
330 extern void FreeExprContext(ExprContext *econtext, bool isCommit);
331 extern void ReScanExprContext(ExprContext *econtext);
332
333 #define ResetExprContext(econtext) \
334         MemoryContextReset((econtext)->ecxt_per_tuple_memory)
335
336 extern ExprContext *MakePerTupleExprContext(EState *estate);
337
338 /* Get an EState's per-output-tuple exprcontext, making it if first use */
339 #define GetPerTupleExprContext(estate) \
340         ((estate)->es_per_tuple_exprcontext ? \
341          (estate)->es_per_tuple_exprcontext : \
342          MakePerTupleExprContext(estate))
343
344 #define GetPerTupleMemoryContext(estate) \
345         (GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
346
347 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
348 #define ResetPerTupleExprContext(estate) \
349         do { \
350                 if ((estate)->es_per_tuple_exprcontext) \
351                         ResetExprContext((estate)->es_per_tuple_exprcontext); \
352         } while (0)
353
354 extern void ExecAssignExprContext(EState *estate, PlanState *planstate);
355 extern void ExecAssignResultType(PlanState *planstate, TupleDesc tupDesc);
356 extern void ExecAssignResultTypeFromTL(PlanState *planstate);
357 extern TupleDesc ExecGetResultType(PlanState *planstate);
358 extern ProjectionInfo *ExecBuildProjectionInfo(List *targetList,
359                                                 ExprContext *econtext,
360                                                 TupleTableSlot *slot,
361                                                 TupleDesc inputDesc);
362 extern void ExecAssignProjectionInfo(PlanState *planstate,
363                                                  TupleDesc inputDesc);
364 extern void ExecFreeExprContext(PlanState *planstate);
365 extern void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc);
366 extern void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate);
367
368 extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid);
369
370 extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags);
371 extern void ExecCloseScanRelation(Relation scanrel);
372
373 extern void RegisterExprContextCallback(ExprContext *econtext,
374                                                         ExprContextCallbackFunction function,
375                                                         Datum arg);
376 extern void UnregisterExprContextCallback(ExprContext *econtext,
377                                                           ExprContextCallbackFunction function,
378                                                           Datum arg);
379 extern void ExecLockNonLeafAppendTables(List *partitioned_rels, EState *estate);
380
381 /*
382  * prototypes from functions in execIndexing.c
383  */
384 extern void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative);
385 extern void ExecCloseIndices(ResultRelInfo *resultRelInfo);
386 extern List *ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid,
387                                           EState *estate, bool noDupErr, bool *specConflict,
388                                           List *arbiterIndexes);
389 extern bool ExecCheckIndexConstraints(TupleTableSlot *slot, EState *estate,
390                                                   ItemPointer conflictTid, List *arbiterIndexes);
391 extern void check_exclusion_constraint(Relation heap, Relation index,
392                                                    IndexInfo *indexInfo,
393                                                    ItemPointer tupleid,
394                                                    Datum *values, bool *isnull,
395                                                    EState *estate, bool newIndex);
396
397 /*
398  * prototypes from functions in execReplication.c
399  */
400 extern bool RelationFindReplTupleByIndex(Relation rel, Oid idxoid,
401                                                          LockTupleMode lockmode,
402                                                          TupleTableSlot *searchslot,
403                                                          TupleTableSlot *outslot);
404 extern bool RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode,
405                                                  TupleTableSlot *searchslot, TupleTableSlot *outslot);
406
407 extern void ExecSimpleRelationInsert(EState *estate, TupleTableSlot *slot);
408 extern void ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate,
409                                                  TupleTableSlot *searchslot, TupleTableSlot *slot);
410 extern void ExecSimpleRelationDelete(EState *estate, EPQState *epqstate,
411                                                  TupleTableSlot *searchslot);
412 extern void CheckCmdReplicaIdentity(Relation rel, CmdType cmd);
413
414
415 #endif   /* EXECUTOR_H  */