1 /*-------------------------------------------------------------------------
2  *
3  * executor.h
4  *	  support for the POSTGRES executor module
5  *
6  *
7  * Portions Copyright (c) 1996-2016, 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);
78 extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook;
79 
80 /* Hook for plugins to get control in ExecutorRun() */
81 typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
82 												   ScanDirection direction,
83 												   uint64 count);
84 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;
85 
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;
89 
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;
93 
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;
97 
98 
99 /*
100  * prototypes from functions in execAmi.c
101  */
102 struct Path;					/* avoid including relation.h here */
103 
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);
110 
111 /*
112  * prototypes from functions in execCurrent.c
113  */
114 extern bool execCurrentOf(CurrentOfExpr *cexpr,
115 			  ExprContext *econtext,
116 			  Oid table_oid,
117 			  ItemPointer current_tid);
118 
119 /*
120  * prototypes from functions in execGrouping.c
121  */
122 extern bool execTuplesMatch(TupleTableSlot *slot1,
123 				TupleTableSlot *slot2,
124 				int numCols,
125 				AttrNumber *matchColIdx,
126 				FmgrInfo *eqfunctions,
127 				MemoryContext evalContext);
128 extern bool execTuplesUnequal(TupleTableSlot *slot1,
129 				  TupleTableSlot *slot2,
130 				  int numCols,
131 				  AttrNumber *matchColIdx,
132 				  FmgrInfo *eqfunctions,
133 				  MemoryContext evalContext);
134 extern FmgrInfo *execTuplesMatchPrepare(int numCols,
135 					   Oid *eqOperators);
136 extern void execTuplesHashPrepare(int numCols,
137 					  Oid *eqOperators,
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,
148 					 bool *isnew);
149 extern TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable,
150 				   TupleTableSlot *slot,
151 				   FmgrInfo *eqfunctions,
152 				   FmgrInfo *hashfunctions);
153 
154 /*
155  * prototypes from functions in execJunk.c
156  */
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,
167 					 bool *isNull);
168 extern TupleTableSlot *ExecFilterJunk(JunkFilter *junkfilter,
169 			   TupleTableSlot *slot);
170 
171 
172 /*
173  * prototypes from functions in execMain.c
174  */
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, uint64 count);
179 extern void standard_ExecutorRun(QueryDesc *queryDesc,
180 					 ScanDirection direction, uint64 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,
212 					 HeapTuple tuple);
213 extern HeapTuple EvalPlanQualGetTuple(EPQState *epqstate, Index rti);
214 
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);
220 
221 /*
222  * prototypes from functions in execProcnode.c
223  */
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);
229 
230 /*
231  * prototypes from functions in execQual.c
232  */
233 extern Datum GetAttributeByNum(HeapTupleHeader tuple, AttrNumber attrno,
234 				  bool *isNull);
235 extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
236 				   bool *isNull);
237 extern Tuplestorestate *ExecMakeTableFunctionResult(ExprState *funcexpr,
238 							ExprContext *econtext,
239 							MemoryContext argContext,
240 							TupleDesc expectedDesc,
241 							bool randomAccess);
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);
251 
252 /*
253  * prototypes from functions in execScan.c
254  */
255 typedef TupleTableSlot *(*ExecScanAccessMtd) (ScanState *node);
256 typedef bool (*ExecScanRecheckMtd) (ScanState *node, TupleTableSlot *slot);
257 
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);
263 
264 /*
265  * prototypes from functions in execTuples.c
266  */
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,
271 					  TupleDesc tupType);
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);
277 
278 typedef struct TupOutputState
279 {
280 	TupleTableSlot *slot;
281 	DestReceiver *dest;
282 } TupOutputState;
283 
284 extern TupOutputState *begin_tup_output_tupdesc(DestReceiver *dest,
285 						 TupleDesc tupdesc);
286 extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
287 extern void do_text_output_multiline(TupOutputState *tstate, const char *txt);
288 extern void end_tup_output(TupOutputState *tstate);
289 
290 /*
291  * Write a single line of text given as a C string.
292  *
293  * Should only be used with a single-TEXT-attribute tupdesc.
294  */
295 #define do_text_output_oneline(tstate, str_to_emit) \
296 	do { \
297 		Datum	values_[1]; \
298 		bool	isnull_[1]; \
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])); \
303 	} while (0)
304 
305 
306 /*
307  * prototypes from functions in execUtils.c
308  */
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);
315 
316 #define ResetExprContext(econtext) \
317 	MemoryContextReset((econtext)->ecxt_per_tuple_memory)
318 
319 extern ExprContext *MakePerTupleExprContext(EState *estate);
320 
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))
326 
327 #define GetPerTupleMemoryContext(estate) \
328 	(GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
329 
330 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
331 #define ResetPerTupleExprContext(estate) \
332 	do { \
333 		if ((estate)->es_per_tuple_exprcontext) \
334 			ResetExprContext((estate)->es_per_tuple_exprcontext); \
335 	} while (0)
336 
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);
350 
351 extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid);
352 
353 extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags);
354 extern void ExecCloseScanRelation(Relation scanrel);
355 
356 extern void RegisterExprContextCallback(ExprContext *econtext,
357 							ExprContextCallbackFunction function,
358 							Datum arg);
359 extern void UnregisterExprContextCallback(ExprContext *econtext,
360 							  ExprContextCallbackFunction function,
361 							  Datum arg);
362 
363 /*
364  * prototypes from functions in execIndexing.c
365  */
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,
375 						   ItemPointer tupleid,
376 						   Datum *values, bool *isnull,
377 						   EState *estate, bool newIndex);
378 
379 
380 #endif   /* EXECUTOR_H  */
381