executor.h 15 KB

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