1 /*-------------------------------------------------------------------------
2  *
3  * executor.h
4  *	  support for the POSTGRES executor module
5  *
6  *
7  * Portions Copyright (c) 1996-2021, 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 "fmgr.h"
19 #include "nodes/lockoptions.h"
20 #include "nodes/parsenodes.h"
21 #include "utils/memutils.h"
22 
23 
24 /*
25  * The "eflags" argument to ExecutorStart and the various ExecInitNode
26  * routines is a bitwise OR of the following flag bits, which tell the
27  * called plan node what to expect.  Note that the flags will get modified
28  * as they are passed down the plan tree, since an upper node may require
29  * functionality in its subnode not demanded of the plan as a whole
30  * (example: MergeJoin requires mark/restore capability in its inner input),
31  * or an upper node may shield its input from some functionality requirement
32  * (example: Materialize shields its input from needing to do backward scan).
33  *
34  * EXPLAIN_ONLY indicates that the plan tree is being initialized just so
35  * EXPLAIN can print it out; it will not be run.  Hence, no side-effects
36  * of startup should occur.  However, error checks (such as permission checks)
37  * should be performed.
38  *
39  * REWIND indicates that the plan node should try to efficiently support
40  * rescans without parameter changes.  (Nodes must support ExecReScan calls
41  * in any case, but if this flag was not given, they are at liberty to do it
42  * through complete recalculation.  Note that a parameter change forces a
43  * full recalculation in any case.)
44  *
45  * BACKWARD indicates that the plan node must respect the es_direction flag.
46  * When this is not passed, the plan node will only be run forwards.
47  *
48  * MARK indicates that the plan node must support Mark/Restore calls.
49  * When this is not passed, no Mark/Restore will occur.
50  *
51  * SKIP_TRIGGERS tells ExecutorStart/ExecutorFinish to skip calling
52  * AfterTriggerBeginQuery/AfterTriggerEndQuery.  This does not necessarily
53  * mean that the plan can't queue any AFTER triggers; just that the caller
54  * is responsible for there being a trigger context for them to be queued in.
55  */
56 #define EXEC_FLAG_EXPLAIN_ONLY	0x0001	/* EXPLAIN, no ANALYZE */
57 #define EXEC_FLAG_REWIND		0x0002	/* need efficient rescan */
58 #define EXEC_FLAG_BACKWARD		0x0004	/* need backward scan */
59 #define EXEC_FLAG_MARK			0x0008	/* need mark/restore */
60 #define EXEC_FLAG_SKIP_TRIGGERS 0x0010	/* skip AfterTrigger calls */
61 #define EXEC_FLAG_WITH_NO_DATA	0x0020	/* rel scannability doesn't matter */
62 
63 
64 /* Hook for plugins to get control in ExecutorStart() */
65 typedef void (*ExecutorStart_hook_type) (QueryDesc *queryDesc, int eflags);
66 extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook;
67 
68 /* Hook for plugins to get control in ExecutorRun() */
69 typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
70 									   ScanDirection direction,
71 									   uint64 count,
72 									   bool execute_once);
73 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;
74 
75 /* Hook for plugins to get control in ExecutorFinish() */
76 typedef void (*ExecutorFinish_hook_type) (QueryDesc *queryDesc);
77 extern PGDLLIMPORT ExecutorFinish_hook_type ExecutorFinish_hook;
78 
79 /* Hook for plugins to get control in ExecutorEnd() */
80 typedef void (*ExecutorEnd_hook_type) (QueryDesc *queryDesc);
81 extern PGDLLIMPORT ExecutorEnd_hook_type ExecutorEnd_hook;
82 
83 /* Hook for plugins to get control in ExecCheckRTPerms() */
84 typedef bool (*ExecutorCheckPerms_hook_type) (List *, bool);
85 extern PGDLLIMPORT ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook;
86 
87 
88 /*
89  * prototypes from functions in execAmi.c
90  */
91 struct Path;					/* avoid including pathnodes.h here */
92 
93 extern void ExecReScan(PlanState *node);
94 extern void ExecMarkPos(PlanState *node);
95 extern void ExecRestrPos(PlanState *node);
96 extern bool ExecSupportsMarkRestore(struct Path *pathnode);
97 extern bool ExecSupportsBackwardScan(Plan *node);
98 extern bool ExecMaterializesOutput(NodeTag plantype);
99 
100 /*
101  * prototypes from functions in execCurrent.c
102  */
103 extern bool execCurrentOf(CurrentOfExpr *cexpr,
104 						  ExprContext *econtext,
105 						  Oid table_oid,
106 						  ItemPointer current_tid);
107 
108 /*
109  * prototypes from functions in execGrouping.c
110  */
111 extern ExprState *execTuplesMatchPrepare(TupleDesc desc,
112 										 int numCols,
113 										 const AttrNumber *keyColIdx,
114 										 const Oid *eqOperators,
115 										 const Oid *collations,
116 										 PlanState *parent);
117 extern void execTuplesHashPrepare(int numCols,
118 								  const Oid *eqOperators,
119 								  Oid **eqFuncOids,
120 								  FmgrInfo **hashFunctions);
121 extern TupleHashTable BuildTupleHashTable(PlanState *parent,
122 										  TupleDesc inputDesc,
123 										  int numCols, AttrNumber *keyColIdx,
124 										  const Oid *eqfuncoids,
125 										  FmgrInfo *hashfunctions,
126 										  Oid *collations,
127 										  long nbuckets, Size additionalsize,
128 										  MemoryContext tablecxt,
129 										  MemoryContext tempcxt, bool use_variable_hash_iv);
130 extern TupleHashTable BuildTupleHashTableExt(PlanState *parent,
131 											 TupleDesc inputDesc,
132 											 int numCols, AttrNumber *keyColIdx,
133 											 const Oid *eqfuncoids,
134 											 FmgrInfo *hashfunctions,
135 											 Oid *collations,
136 											 long nbuckets, Size additionalsize,
137 											 MemoryContext metacxt,
138 											 MemoryContext tablecxt,
139 											 MemoryContext tempcxt, bool use_variable_hash_iv);
140 extern TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable,
141 										   TupleTableSlot *slot,
142 										   bool *isnew, uint32 *hash);
143 extern uint32 TupleHashTableHash(TupleHashTable hashtable,
144 								 TupleTableSlot *slot);
145 extern TupleHashEntry LookupTupleHashEntryHash(TupleHashTable hashtable,
146 											   TupleTableSlot *slot,
147 											   bool *isnew, uint32 hash);
148 extern TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable,
149 										 TupleTableSlot *slot,
150 										 ExprState *eqcomp,
151 										 FmgrInfo *hashfunctions);
152 extern void ResetTupleHashTable(TupleHashTable hashtable);
153 
154 /*
155  * prototypes from functions in execJunk.c
156  */
157 extern JunkFilter *ExecInitJunkFilter(List *targetList,
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 TupleTableSlot *ExecFilterJunk(JunkFilter *junkfilter,
167 									  TupleTableSlot *slot);
168 
169 /*
170  * ExecGetJunkAttribute
171  *
172  * Given a junk filter's input tuple (slot) and a junk attribute's number
173  * previously found by ExecFindJunkAttribute, extract & return the value and
174  * isNull flag of the attribute.
175  */
176 #ifndef FRONTEND
177 static inline Datum
ExecGetJunkAttribute(TupleTableSlot * slot,AttrNumber attno,bool * isNull)178 ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno, bool *isNull)
179 {
180 	Assert(attno > 0);
181 	return slot_getattr(slot, attno, isNull);
182 }
183 #endif
184 
185 /*
186  * prototypes from functions in execMain.c
187  */
188 extern void ExecutorStart(QueryDesc *queryDesc, int eflags);
189 extern void standard_ExecutorStart(QueryDesc *queryDesc, int eflags);
190 extern void ExecutorRun(QueryDesc *queryDesc,
191 						ScanDirection direction, uint64 count, bool execute_once);
192 extern void standard_ExecutorRun(QueryDesc *queryDesc,
193 								 ScanDirection direction, uint64 count, bool execute_once);
194 extern void ExecutorFinish(QueryDesc *queryDesc);
195 extern void standard_ExecutorFinish(QueryDesc *queryDesc);
196 extern void ExecutorEnd(QueryDesc *queryDesc);
197 extern void standard_ExecutorEnd(QueryDesc *queryDesc);
198 extern void ExecutorRewind(QueryDesc *queryDesc);
199 extern bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation);
200 extern void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation);
201 extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
202 							  Relation resultRelationDesc,
203 							  Index resultRelationIndex,
204 							  ResultRelInfo *partition_root_rri,
205 							  int instrument_options);
206 extern ResultRelInfo *ExecGetTriggerResultRel(EState *estate, Oid relid);
207 extern void ExecConstraints(ResultRelInfo *resultRelInfo,
208 							TupleTableSlot *slot, EState *estate);
209 extern bool ExecPartitionCheck(ResultRelInfo *resultRelInfo,
210 							   TupleTableSlot *slot, EState *estate, bool emitError);
211 extern void ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
212 										TupleTableSlot *slot, EState *estate);
213 extern void ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
214 								 TupleTableSlot *slot, EState *estate);
215 extern LockTupleMode ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo);
216 extern ExecRowMark *ExecFindRowMark(EState *estate, Index rti, bool missing_ok);
217 extern ExecAuxRowMark *ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist);
218 extern TupleTableSlot *EvalPlanQual(EPQState *epqstate, Relation relation,
219 									Index rti, TupleTableSlot *testslot);
220 extern void EvalPlanQualInit(EPQState *epqstate, EState *parentestate,
221 							 Plan *subplan, List *auxrowmarks, int epqParam);
222 extern void EvalPlanQualSetPlan(EPQState *epqstate,
223 								Plan *subplan, List *auxrowmarks);
224 extern TupleTableSlot *EvalPlanQualSlot(EPQState *epqstate,
225 										Relation relation, Index rti);
226 
227 #define EvalPlanQualSetSlot(epqstate, slot)  ((epqstate)->origslot = (slot))
228 extern bool EvalPlanQualFetchRowMark(EPQState *epqstate, Index rti, TupleTableSlot *slot);
229 extern TupleTableSlot *EvalPlanQualNext(EPQState *epqstate);
230 extern void EvalPlanQualBegin(EPQState *epqstate);
231 extern void EvalPlanQualEnd(EPQState *epqstate);
232 
233 /*
234  * functions in execProcnode.c
235  */
236 extern PlanState *ExecInitNode(Plan *node, EState *estate, int eflags);
237 extern void ExecSetExecProcNode(PlanState *node, ExecProcNodeMtd function);
238 extern Node *MultiExecProcNode(PlanState *node);
239 extern void ExecEndNode(PlanState *node);
240 extern bool ExecShutdownNode(PlanState *node);
241 extern void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node);
242 
243 
244 /* ----------------------------------------------------------------
245  *		ExecProcNode
246  *
247  *		Execute the given node to return a(nother) tuple.
248  * ----------------------------------------------------------------
249  */
250 #ifndef FRONTEND
251 static inline TupleTableSlot *
ExecProcNode(PlanState * node)252 ExecProcNode(PlanState *node)
253 {
254 	if (node->chgParam != NULL) /* something changed? */
255 		ExecReScan(node);		/* let ReScan handle this */
256 
257 	return node->ExecProcNode(node);
258 }
259 #endif
260 
261 /*
262  * prototypes from functions in execExpr.c
263  */
264 extern ExprState *ExecInitExpr(Expr *node, PlanState *parent);
265 extern ExprState *ExecInitExprWithParams(Expr *node, ParamListInfo ext_params);
266 extern ExprState *ExecInitQual(List *qual, PlanState *parent);
267 extern ExprState *ExecInitCheck(List *qual, PlanState *parent);
268 extern List *ExecInitExprList(List *nodes, PlanState *parent);
269 extern ExprState *ExecBuildAggTrans(AggState *aggstate, struct AggStatePerPhaseData *phase,
270 									bool doSort, bool doHash, bool nullcheck);
271 extern ExprState *ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
272 										 const TupleTableSlotOps *lops, const TupleTableSlotOps *rops,
273 										 int numCols,
274 										 const AttrNumber *keyColIdx,
275 										 const Oid *eqfunctions,
276 										 const Oid *collations,
277 										 PlanState *parent);
278 extern ExprState *ExecBuildParamSetEqual(TupleDesc desc,
279 										 const TupleTableSlotOps *lops,
280 										 const TupleTableSlotOps *rops,
281 										 const Oid *eqfunctions,
282 										 const Oid *collations,
283 										 const List *param_exprs,
284 										 PlanState *parent);
285 extern ProjectionInfo *ExecBuildProjectionInfo(List *targetList,
286 											   ExprContext *econtext,
287 											   TupleTableSlot *slot,
288 											   PlanState *parent,
289 											   TupleDesc inputDesc);
290 extern ProjectionInfo *ExecBuildUpdateProjection(List *targetList,
291 												 bool evalTargetList,
292 												 List *targetColnos,
293 												 TupleDesc relDesc,
294 												 ExprContext *econtext,
295 												 TupleTableSlot *slot,
296 												 PlanState *parent);
297 extern ExprState *ExecPrepareExpr(Expr *node, EState *estate);
298 extern ExprState *ExecPrepareQual(List *qual, EState *estate);
299 extern ExprState *ExecPrepareCheck(List *qual, EState *estate);
300 extern List *ExecPrepareExprList(List *nodes, EState *estate);
301 
302 /*
303  * ExecEvalExpr
304  *
305  * Evaluate expression identified by "state" in the execution context
306  * given by "econtext".  *isNull is set to the is-null flag for the result,
307  * and the Datum value is the function result.
308  *
309  * The caller should already have switched into the temporary memory
310  * context econtext->ecxt_per_tuple_memory.  The convenience entry point
311  * ExecEvalExprSwitchContext() is provided for callers who don't prefer to
312  * do the switch in an outer loop.
313  */
314 #ifndef FRONTEND
315 static inline Datum
ExecEvalExpr(ExprState * state,ExprContext * econtext,bool * isNull)316 ExecEvalExpr(ExprState *state,
317 			 ExprContext *econtext,
318 			 bool *isNull)
319 {
320 	return state->evalfunc(state, econtext, isNull);
321 }
322 #endif
323 
324 /*
325  * ExecEvalExprSwitchContext
326  *
327  * Same as ExecEvalExpr, but get into the right allocation context explicitly.
328  */
329 #ifndef FRONTEND
330 static inline Datum
ExecEvalExprSwitchContext(ExprState * state,ExprContext * econtext,bool * isNull)331 ExecEvalExprSwitchContext(ExprState *state,
332 						  ExprContext *econtext,
333 						  bool *isNull)
334 {
335 	Datum		retDatum;
336 	MemoryContext oldContext;
337 
338 	oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
339 	retDatum = state->evalfunc(state, econtext, isNull);
340 	MemoryContextSwitchTo(oldContext);
341 	return retDatum;
342 }
343 #endif
344 
345 /*
346  * ExecProject
347  *
348  * Projects a tuple based on projection info and stores it in the slot passed
349  * to ExecBuildProjectionInfo().
350  *
351  * Note: the result is always a virtual tuple; therefore it may reference
352  * the contents of the exprContext's scan tuples and/or temporary results
353  * constructed in the exprContext.  If the caller wishes the result to be
354  * valid longer than that data will be valid, he must call ExecMaterializeSlot
355  * on the result slot.
356  */
357 #ifndef FRONTEND
358 static inline TupleTableSlot *
ExecProject(ProjectionInfo * projInfo)359 ExecProject(ProjectionInfo *projInfo)
360 {
361 	ExprContext *econtext = projInfo->pi_exprContext;
362 	ExprState  *state = &projInfo->pi_state;
363 	TupleTableSlot *slot = state->resultslot;
364 	bool		isnull;
365 
366 	/*
367 	 * Clear any former contents of the result slot.  This makes it safe for
368 	 * us to use the slot's Datum/isnull arrays as workspace.
369 	 */
370 	ExecClearTuple(slot);
371 
372 	/* Run the expression, discarding scalar result from the last column. */
373 	(void) ExecEvalExprSwitchContext(state, econtext, &isnull);
374 
375 	/*
376 	 * Successfully formed a result row.  Mark the result slot as containing a
377 	 * valid virtual tuple (inlined version of ExecStoreVirtualTuple()).
378 	 */
379 	slot->tts_flags &= ~TTS_FLAG_EMPTY;
380 	slot->tts_nvalid = slot->tts_tupleDescriptor->natts;
381 
382 	return slot;
383 }
384 #endif
385 
386 /*
387  * ExecQual - evaluate a qual prepared with ExecInitQual (possibly via
388  * ExecPrepareQual).  Returns true if qual is satisfied, else false.
389  *
390  * Note: ExecQual used to have a third argument "resultForNull".  The
391  * behavior of this function now corresponds to resultForNull == false.
392  * If you want the resultForNull == true behavior, see ExecCheck.
393  */
394 #ifndef FRONTEND
395 static inline bool
ExecQual(ExprState * state,ExprContext * econtext)396 ExecQual(ExprState *state, ExprContext *econtext)
397 {
398 	Datum		ret;
399 	bool		isnull;
400 
401 	/* short-circuit (here and in ExecInitQual) for empty restriction list */
402 	if (state == NULL)
403 		return true;
404 
405 	/* verify that expression was compiled using ExecInitQual */
406 	Assert(state->flags & EEO_FLAG_IS_QUAL);
407 
408 	ret = ExecEvalExprSwitchContext(state, econtext, &isnull);
409 
410 	/* EEOP_QUAL should never return NULL */
411 	Assert(!isnull);
412 
413 	return DatumGetBool(ret);
414 }
415 #endif
416 
417 /*
418  * ExecQualAndReset() - evaluate qual with ExecQual() and reset expression
419  * context.
420  */
421 #ifndef FRONTEND
422 static inline bool
ExecQualAndReset(ExprState * state,ExprContext * econtext)423 ExecQualAndReset(ExprState *state, ExprContext *econtext)
424 {
425 	bool		ret = ExecQual(state, econtext);
426 
427 	/* inline ResetExprContext, to avoid ordering issue in this file */
428 	MemoryContextReset(econtext->ecxt_per_tuple_memory);
429 	return ret;
430 }
431 #endif
432 
433 extern bool ExecCheck(ExprState *state, ExprContext *context);
434 
435 /*
436  * prototypes from functions in execSRF.c
437  */
438 extern SetExprState *ExecInitTableFunctionResult(Expr *expr,
439 												 ExprContext *econtext, PlanState *parent);
440 extern Tuplestorestate *ExecMakeTableFunctionResult(SetExprState *setexpr,
441 													ExprContext *econtext,
442 													MemoryContext argContext,
443 													TupleDesc expectedDesc,
444 													bool randomAccess);
445 extern SetExprState *ExecInitFunctionResultSet(Expr *expr,
446 											   ExprContext *econtext, PlanState *parent);
447 extern Datum ExecMakeFunctionResultSet(SetExprState *fcache,
448 									   ExprContext *econtext,
449 									   MemoryContext argContext,
450 									   bool *isNull,
451 									   ExprDoneCond *isDone);
452 
453 /*
454  * prototypes from functions in execScan.c
455  */
456 typedef TupleTableSlot *(*ExecScanAccessMtd) (ScanState *node);
457 typedef bool (*ExecScanRecheckMtd) (ScanState *node, TupleTableSlot *slot);
458 
459 extern TupleTableSlot *ExecScan(ScanState *node, ExecScanAccessMtd accessMtd,
460 								ExecScanRecheckMtd recheckMtd);
461 extern void ExecAssignScanProjectionInfo(ScanState *node);
462 extern void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno);
463 extern void ExecScanReScan(ScanState *node);
464 
465 /*
466  * prototypes from functions in execTuples.c
467  */
468 extern void ExecInitResultTypeTL(PlanState *planstate);
469 extern void ExecInitResultSlot(PlanState *planstate,
470 							   const TupleTableSlotOps *tts_ops);
471 extern void ExecInitResultTupleSlotTL(PlanState *planstate,
472 									  const TupleTableSlotOps *tts_ops);
473 extern void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate,
474 								  TupleDesc tupleDesc,
475 								  const TupleTableSlotOps *tts_ops);
476 extern TupleTableSlot *ExecInitExtraTupleSlot(EState *estate,
477 											  TupleDesc tupledesc,
478 											  const TupleTableSlotOps *tts_ops);
479 extern TupleTableSlot *ExecInitNullTupleSlot(EState *estate, TupleDesc tupType,
480 											 const TupleTableSlotOps *tts_ops);
481 extern TupleDesc ExecTypeFromTL(List *targetList);
482 extern TupleDesc ExecCleanTypeFromTL(List *targetList);
483 extern TupleDesc ExecTypeFromExprList(List *exprList);
484 extern void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList);
485 extern void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg);
486 
487 typedef struct TupOutputState
488 {
489 	TupleTableSlot *slot;
490 	DestReceiver *dest;
491 } TupOutputState;
492 
493 extern TupOutputState *begin_tup_output_tupdesc(DestReceiver *dest,
494 												TupleDesc tupdesc,
495 												const TupleTableSlotOps *tts_ops);
496 extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
497 extern void do_text_output_multiline(TupOutputState *tstate, const char *txt);
498 extern void end_tup_output(TupOutputState *tstate);
499 
500 /*
501  * Write a single line of text given as a C string.
502  *
503  * Should only be used with a single-TEXT-attribute tupdesc.
504  */
505 #define do_text_output_oneline(tstate, str_to_emit) \
506 	do { \
507 		Datum	values_[1]; \
508 		bool	isnull_[1]; \
509 		values_[0] = PointerGetDatum(cstring_to_text(str_to_emit)); \
510 		isnull_[0] = false; \
511 		do_tup_output(tstate, values_, isnull_); \
512 		pfree(DatumGetPointer(values_[0])); \
513 	} while (0)
514 
515 
516 /*
517  * prototypes from functions in execUtils.c
518  */
519 extern EState *CreateExecutorState(void);
520 extern void FreeExecutorState(EState *estate);
521 extern ExprContext *CreateExprContext(EState *estate);
522 extern ExprContext *CreateWorkExprContext(EState *estate);
523 extern ExprContext *CreateStandaloneExprContext(void);
524 extern void FreeExprContext(ExprContext *econtext, bool isCommit);
525 extern void ReScanExprContext(ExprContext *econtext);
526 
527 #define ResetExprContext(econtext) \
528 	MemoryContextReset((econtext)->ecxt_per_tuple_memory)
529 
530 extern ExprContext *MakePerTupleExprContext(EState *estate);
531 
532 /* Get an EState's per-output-tuple exprcontext, making it if first use */
533 #define GetPerTupleExprContext(estate) \
534 	((estate)->es_per_tuple_exprcontext ? \
535 	 (estate)->es_per_tuple_exprcontext : \
536 	 MakePerTupleExprContext(estate))
537 
538 #define GetPerTupleMemoryContext(estate) \
539 	(GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
540 
541 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
542 #define ResetPerTupleExprContext(estate) \
543 	do { \
544 		if ((estate)->es_per_tuple_exprcontext) \
545 			ResetExprContext((estate)->es_per_tuple_exprcontext); \
546 	} while (0)
547 
548 extern void ExecAssignExprContext(EState *estate, PlanState *planstate);
549 extern TupleDesc ExecGetResultType(PlanState *planstate);
550 extern const TupleTableSlotOps *ExecGetResultSlotOps(PlanState *planstate,
551 													 bool *isfixed);
552 extern void ExecAssignProjectionInfo(PlanState *planstate,
553 									 TupleDesc inputDesc);
554 extern void ExecConditionalAssignProjectionInfo(PlanState *planstate,
555 												TupleDesc inputDesc, Index varno);
556 extern void ExecFreeExprContext(PlanState *planstate);
557 extern void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc);
558 extern void ExecCreateScanSlotFromOuterPlan(EState *estate,
559 											ScanState *scanstate,
560 											const TupleTableSlotOps *tts_ops);
561 
562 extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid);
563 
564 extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags);
565 
566 extern void ExecInitRangeTable(EState *estate, List *rangeTable);
567 extern void ExecCloseRangeTableRelations(EState *estate);
568 extern void ExecCloseResultRelations(EState *estate);
569 
570 static inline RangeTblEntry *
exec_rt_fetch(Index rti,EState * estate)571 exec_rt_fetch(Index rti, EState *estate)
572 {
573 	return (RangeTblEntry *) list_nth(estate->es_range_table, rti - 1);
574 }
575 
576 extern Relation ExecGetRangeTableRelation(EState *estate, Index rti);
577 extern void ExecInitResultRelation(EState *estate, ResultRelInfo *resultRelInfo,
578 								   Index rti);
579 
580 extern int	executor_errposition(EState *estate, int location);
581 
582 extern void RegisterExprContextCallback(ExprContext *econtext,
583 										ExprContextCallbackFunction function,
584 										Datum arg);
585 extern void UnregisterExprContextCallback(ExprContext *econtext,
586 										  ExprContextCallbackFunction function,
587 										  Datum arg);
588 
589 extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
590 								bool *isNull);
591 extern Datum GetAttributeByNum(HeapTupleHeader tuple, AttrNumber attrno,
592 							   bool *isNull);
593 
594 extern int	ExecTargetListLength(List *targetlist);
595 extern int	ExecCleanTargetListLength(List *targetlist);
596 
597 extern TupleTableSlot *ExecGetTriggerOldSlot(EState *estate, ResultRelInfo *relInfo);
598 extern TupleTableSlot *ExecGetTriggerNewSlot(EState *estate, ResultRelInfo *relInfo);
599 extern TupleTableSlot *ExecGetReturningSlot(EState *estate, ResultRelInfo *relInfo);
600 extern TupleConversionMap *ExecGetChildToRootMap(ResultRelInfo *resultRelInfo);
601 
602 extern Bitmapset *ExecGetInsertedCols(ResultRelInfo *relinfo, EState *estate);
603 extern Bitmapset *ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate);
604 extern Bitmapset *ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate);
605 extern Bitmapset *ExecGetAllUpdatedCols(ResultRelInfo *relinfo, EState *estate);
606 
607 /*
608  * prototypes from functions in execIndexing.c
609  */
610 extern void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative);
611 extern void ExecCloseIndices(ResultRelInfo *resultRelInfo);
612 extern List *ExecInsertIndexTuples(ResultRelInfo *resultRelInfo,
613 								   TupleTableSlot *slot, EState *estate,
614 								   bool update,
615 								   bool noDupErr,
616 								   bool *specConflict, List *arbiterIndexes);
617 extern bool ExecCheckIndexConstraints(ResultRelInfo *resultRelInfo,
618 									  TupleTableSlot *slot,
619 									  EState *estate, ItemPointer conflictTid,
620 									  List *arbiterIndexes);
621 extern void check_exclusion_constraint(Relation heap, Relation index,
622 									   IndexInfo *indexInfo,
623 									   ItemPointer tupleid,
624 									   Datum *values, bool *isnull,
625 									   EState *estate, bool newIndex);
626 
627 /*
628  * prototypes from functions in execReplication.c
629  */
630 extern bool RelationFindReplTupleByIndex(Relation rel, Oid idxoid,
631 										 LockTupleMode lockmode,
632 										 TupleTableSlot *searchslot,
633 										 TupleTableSlot *outslot);
634 extern bool RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode,
635 									 TupleTableSlot *searchslot, TupleTableSlot *outslot);
636 
637 extern void ExecSimpleRelationInsert(ResultRelInfo *resultRelInfo,
638 									 EState *estate, TupleTableSlot *slot);
639 extern void ExecSimpleRelationUpdate(ResultRelInfo *resultRelInfo,
640 									 EState *estate, EPQState *epqstate,
641 									 TupleTableSlot *searchslot, TupleTableSlot *slot);
642 extern void ExecSimpleRelationDelete(ResultRelInfo *resultRelInfo,
643 									 EState *estate, EPQState *epqstate,
644 									 TupleTableSlot *searchslot);
645 extern void CheckCmdReplicaIdentity(Relation rel, CmdType cmd);
646 
647 extern void CheckSubscriptionRelkind(char relkind, const char *nspname,
648 									 const char *relname);
649 
650 /*
651  * prototypes from functions in nodeModifyTable.c
652  */
653 extern TupleTableSlot *ExecGetUpdateNewTuple(ResultRelInfo *relinfo,
654 											 TupleTableSlot *planSlot,
655 											 TupleTableSlot *oldSlot);
656 extern ResultRelInfo *ExecLookupResultRelByOid(ModifyTableState *node,
657 											   Oid resultoid,
658 											   bool missing_ok,
659 											   bool update_cache);
660 
661 #endif							/* EXECUTOR_H  */
662