Header And Logo

PostgreSQL
| The world's most advanced open source database.

executor.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * executor.h
00004  *    support for the POSTGRES executor module
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/executor/executor.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef EXECUTOR_H
00015 #define EXECUTOR_H
00016 
00017 #include "executor/execdesc.h"
00018 #include "nodes/parsenodes.h"
00019 
00020 
00021 /*
00022  * The "eflags" argument to ExecutorStart and the various ExecInitNode
00023  * routines is a bitwise OR of the following flag bits, which tell the
00024  * called plan node what to expect.  Note that the flags will get modified
00025  * as they are passed down the plan tree, since an upper node may require
00026  * functionality in its subnode not demanded of the plan as a whole
00027  * (example: MergeJoin requires mark/restore capability in its inner input),
00028  * or an upper node may shield its input from some functionality requirement
00029  * (example: Materialize shields its input from needing to do backward scan).
00030  *
00031  * EXPLAIN_ONLY indicates that the plan tree is being initialized just so
00032  * EXPLAIN can print it out; it will not be run.  Hence, no side-effects
00033  * of startup should occur.  However, error checks (such as permission checks)
00034  * should be performed.
00035  *
00036  * REWIND indicates that the plan node should try to efficiently support
00037  * rescans without parameter changes.  (Nodes must support ExecReScan calls
00038  * in any case, but if this flag was not given, they are at liberty to do it
00039  * through complete recalculation.  Note that a parameter change forces a
00040  * full recalculation in any case.)
00041  *
00042  * BACKWARD indicates that the plan node must respect the es_direction flag.
00043  * When this is not passed, the plan node will only be run forwards.
00044  *
00045  * MARK indicates that the plan node must support Mark/Restore calls.
00046  * When this is not passed, no Mark/Restore will occur.
00047  *
00048  * SKIP_TRIGGERS tells ExecutorStart/ExecutorFinish to skip calling
00049  * AfterTriggerBeginQuery/AfterTriggerEndQuery.  This does not necessarily
00050  * mean that the plan can't queue any AFTER triggers; just that the caller
00051  * is responsible for there being a trigger context for them to be queued in.
00052  *
00053  * WITH/WITHOUT_OIDS tell the executor to emit tuples with or without space
00054  * for OIDs, respectively.  These are currently used only for CREATE TABLE AS.
00055  * If neither is set, the plan may or may not produce tuples including OIDs.
00056  */
00057 #define EXEC_FLAG_EXPLAIN_ONLY  0x0001  /* EXPLAIN, no ANALYZE */
00058 #define EXEC_FLAG_REWIND        0x0002  /* need efficient rescan */
00059 #define EXEC_FLAG_BACKWARD      0x0004  /* need backward scan */
00060 #define EXEC_FLAG_MARK          0x0008  /* need mark/restore */
00061 #define EXEC_FLAG_SKIP_TRIGGERS 0x0010  /* skip AfterTrigger calls */
00062 #define EXEC_FLAG_WITH_OIDS     0x0020  /* force OIDs in returned tuples */
00063 #define EXEC_FLAG_WITHOUT_OIDS  0x0040  /* force no OIDs in returned tuples */
00064 #define EXEC_FLAG_WITH_NO_DATA  0x0080  /* rel scannability doesn't matter */
00065 
00066 
00067 /*
00068  * ExecEvalExpr was formerly a function containing a switch statement;
00069  * now it's just a macro invoking the function pointed to by an ExprState
00070  * node.  Beware of double evaluation of the ExprState argument!
00071  */
00072 #define ExecEvalExpr(expr, econtext, isNull, isDone) \
00073     ((*(expr)->evalfunc) (expr, econtext, isNull, isDone))
00074 
00075 
00076 /* Hook for plugins to get control in ExecutorStart() */
00077 typedef void (*ExecutorStart_hook_type) (QueryDesc *queryDesc, int eflags);
00078 extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook;
00079 
00080 /* Hook for plugins to get control in ExecutorRun() */
00081 typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
00082                                                    ScanDirection direction,
00083                                                    long count);
00084 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;
00085 
00086 /* Hook for plugins to get control in ExecutorFinish() */
00087 typedef void (*ExecutorFinish_hook_type) (QueryDesc *queryDesc);
00088 extern PGDLLIMPORT ExecutorFinish_hook_type ExecutorFinish_hook;
00089 
00090 /* Hook for plugins to get control in ExecutorEnd() */
00091 typedef void (*ExecutorEnd_hook_type) (QueryDesc *queryDesc);
00092 extern PGDLLIMPORT ExecutorEnd_hook_type ExecutorEnd_hook;
00093 
00094 /* Hook for plugins to get control in ExecCheckRTPerms() */
00095 typedef bool (*ExecutorCheckPerms_hook_type) (List *, bool);
00096 extern PGDLLIMPORT ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook;
00097 
00098 
00099 /*
00100  * prototypes from functions in execAmi.c
00101  */
00102 extern void ExecReScan(PlanState *node);
00103 extern void ExecMarkPos(PlanState *node);
00104 extern void ExecRestrPos(PlanState *node);
00105 extern bool ExecSupportsMarkRestore(NodeTag plantype);
00106 extern bool ExecSupportsBackwardScan(Plan *node);
00107 extern bool ExecMaterializesOutput(NodeTag plantype);
00108 
00109 /*
00110  * prototypes from functions in execCurrent.c
00111  */
00112 extern bool execCurrentOf(CurrentOfExpr *cexpr,
00113               ExprContext *econtext,
00114               Oid table_oid,
00115               ItemPointer current_tid);
00116 
00117 /*
00118  * prototypes from functions in execGrouping.c
00119  */
00120 extern bool execTuplesMatch(TupleTableSlot *slot1,
00121                 TupleTableSlot *slot2,
00122                 int numCols,
00123                 AttrNumber *matchColIdx,
00124                 FmgrInfo *eqfunctions,
00125                 MemoryContext evalContext);
00126 extern bool execTuplesUnequal(TupleTableSlot *slot1,
00127                   TupleTableSlot *slot2,
00128                   int numCols,
00129                   AttrNumber *matchColIdx,
00130                   FmgrInfo *eqfunctions,
00131                   MemoryContext evalContext);
00132 extern FmgrInfo *execTuplesMatchPrepare(int numCols,
00133                        Oid *eqOperators);
00134 extern void execTuplesHashPrepare(int numCols,
00135                       Oid *eqOperators,
00136                       FmgrInfo **eqFunctions,
00137                       FmgrInfo **hashFunctions);
00138 extern TupleHashTable BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
00139                     FmgrInfo *eqfunctions,
00140                     FmgrInfo *hashfunctions,
00141                     long nbuckets, Size entrysize,
00142                     MemoryContext tablecxt,
00143                     MemoryContext tempcxt);
00144 extern TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable,
00145                      TupleTableSlot *slot,
00146                      bool *isnew);
00147 extern TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable,
00148                    TupleTableSlot *slot,
00149                    FmgrInfo *eqfunctions,
00150                    FmgrInfo *hashfunctions);
00151 
00152 /*
00153  * prototypes from functions in execJunk.c
00154  */
00155 extern JunkFilter *ExecInitJunkFilter(List *targetList, bool hasoid,
00156                    TupleTableSlot *slot);
00157 extern JunkFilter *ExecInitJunkFilterConversion(List *targetList,
00158                              TupleDesc cleanTupType,
00159                              TupleTableSlot *slot);
00160 extern AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter,
00161                       const char *attrName);
00162 extern AttrNumber ExecFindJunkAttributeInTlist(List *targetlist,
00163                              const char *attrName);
00164 extern Datum ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno,
00165                      bool *isNull);
00166 extern TupleTableSlot *ExecFilterJunk(JunkFilter *junkfilter,
00167                TupleTableSlot *slot);
00168 
00169 
00170 /*
00171  * prototypes from functions in execMain.c
00172  */
00173 extern void ExecutorStart(QueryDesc *queryDesc, int eflags);
00174 extern void standard_ExecutorStart(QueryDesc *queryDesc, int eflags);
00175 extern void ExecutorRun(QueryDesc *queryDesc,
00176             ScanDirection direction, long count);
00177 extern void standard_ExecutorRun(QueryDesc *queryDesc,
00178                      ScanDirection direction, long count);
00179 extern void ExecutorFinish(QueryDesc *queryDesc);
00180 extern void standard_ExecutorFinish(QueryDesc *queryDesc);
00181 extern void ExecutorEnd(QueryDesc *queryDesc);
00182 extern void standard_ExecutorEnd(QueryDesc *queryDesc);
00183 extern void ExecutorRewind(QueryDesc *queryDesc);
00184 extern bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation);
00185 extern void CheckValidResultRel(Relation resultRel, CmdType operation);
00186 extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
00187                   Relation resultRelationDesc,
00188                   Index resultRelationIndex,
00189                   int instrument_options);
00190 extern ResultRelInfo *ExecGetTriggerResultRel(EState *estate, Oid relid);
00191 extern bool ExecContextForcesOids(PlanState *planstate, bool *hasoids);
00192 extern void ExecConstraints(ResultRelInfo *resultRelInfo,
00193                 TupleTableSlot *slot, EState *estate);
00194 extern ExecRowMark *ExecFindRowMark(EState *estate, Index rti);
00195 extern ExecAuxRowMark *ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist);
00196 extern TupleTableSlot *EvalPlanQual(EState *estate, EPQState *epqstate,
00197              Relation relation, Index rti, int lockmode,
00198              ItemPointer tid, TransactionId priorXmax);
00199 extern HeapTuple EvalPlanQualFetch(EState *estate, Relation relation,
00200                   int lockmode, ItemPointer tid, TransactionId priorXmax);
00201 extern void EvalPlanQualInit(EPQState *epqstate, EState *estate,
00202                  Plan *subplan, List *auxrowmarks, int epqParam);
00203 extern void EvalPlanQualSetPlan(EPQState *epqstate,
00204                     Plan *subplan, List *auxrowmarks);
00205 extern void EvalPlanQualSetTuple(EPQState *epqstate, Index rti,
00206                      HeapTuple tuple);
00207 extern HeapTuple EvalPlanQualGetTuple(EPQState *epqstate, Index rti);
00208 
00209 #define EvalPlanQualSetSlot(epqstate, slot)  ((epqstate)->origslot = (slot))
00210 extern void EvalPlanQualFetchRowMarks(EPQState *epqstate);
00211 extern TupleTableSlot *EvalPlanQualNext(EPQState *epqstate);
00212 extern void EvalPlanQualBegin(EPQState *epqstate, EState *parentestate);
00213 extern void EvalPlanQualEnd(EPQState *epqstate);
00214 
00215 /*
00216  * prototypes from functions in execProcnode.c
00217  */
00218 extern PlanState *ExecInitNode(Plan *node, EState *estate, int eflags);
00219 extern TupleTableSlot *ExecProcNode(PlanState *node);
00220 extern Node *MultiExecProcNode(PlanState *node);
00221 extern void ExecEndNode(PlanState *node);
00222 
00223 /*
00224  * prototypes from functions in execQual.c
00225  */
00226 extern Datum GetAttributeByNum(HeapTupleHeader tuple, AttrNumber attrno,
00227                   bool *isNull);
00228 extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
00229                    bool *isNull);
00230 extern Tuplestorestate *ExecMakeTableFunctionResult(ExprState *funcexpr,
00231                             ExprContext *econtext,
00232                             TupleDesc expectedDesc,
00233                             bool randomAccess);
00234 extern Datum ExecEvalExprSwitchContext(ExprState *expression, ExprContext *econtext,
00235                           bool *isNull, ExprDoneCond *isDone);
00236 extern ExprState *ExecInitExpr(Expr *node, PlanState *parent);
00237 extern ExprState *ExecPrepareExpr(Expr *node, EState *estate);
00238 extern bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull);
00239 extern int  ExecTargetListLength(List *targetlist);
00240 extern int  ExecCleanTargetListLength(List *targetlist);
00241 extern TupleTableSlot *ExecProject(ProjectionInfo *projInfo,
00242             ExprDoneCond *isDone);
00243 
00244 /*
00245  * prototypes from functions in execScan.c
00246  */
00247 typedef TupleTableSlot *(*ExecScanAccessMtd) (ScanState *node);
00248 typedef bool (*ExecScanRecheckMtd) (ScanState *node, TupleTableSlot *slot);
00249 
00250 extern TupleTableSlot *ExecScan(ScanState *node, ExecScanAccessMtd accessMtd,
00251          ExecScanRecheckMtd recheckMtd);
00252 extern void ExecAssignScanProjectionInfo(ScanState *node);
00253 extern void ExecScanReScan(ScanState *node);
00254 
00255 /*
00256  * prototypes from functions in execTuples.c
00257  */
00258 extern void ExecInitResultTupleSlot(EState *estate, PlanState *planstate);
00259 extern void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate);
00260 extern TupleTableSlot *ExecInitExtraTupleSlot(EState *estate);
00261 extern TupleTableSlot *ExecInitNullTupleSlot(EState *estate,
00262                       TupleDesc tupType);
00263 extern TupleDesc ExecTypeFromTL(List *targetList, bool hasoid);
00264 extern TupleDesc ExecCleanTypeFromTL(List *targetList, bool hasoid);
00265 extern TupleDesc ExecTypeFromExprList(List *exprList, List *namesList);
00266 extern void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg);
00267 
00268 typedef struct TupOutputState
00269 {
00270     TupleTableSlot *slot;
00271     DestReceiver *dest;
00272 } TupOutputState;
00273 
00274 extern TupOutputState *begin_tup_output_tupdesc(DestReceiver *dest,
00275                          TupleDesc tupdesc);
00276 extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
00277 extern void do_text_output_multiline(TupOutputState *tstate, char *text);
00278 extern void end_tup_output(TupOutputState *tstate);
00279 
00280 /*
00281  * Write a single line of text given as a C string.
00282  *
00283  * Should only be used with a single-TEXT-attribute tupdesc.
00284  */
00285 #define do_text_output_oneline(tstate, str_to_emit) \
00286     do { \
00287         Datum   values_[1]; \
00288         bool    isnull_[1]; \
00289         values_[0] = PointerGetDatum(cstring_to_text(str_to_emit)); \
00290         isnull_[0] = false; \
00291         do_tup_output(tstate, values_, isnull_); \
00292         pfree(DatumGetPointer(values_[0])); \
00293     } while (0)
00294 
00295 
00296 /*
00297  * prototypes from functions in execUtils.c
00298  */
00299 extern EState *CreateExecutorState(void);
00300 extern void FreeExecutorState(EState *estate);
00301 extern ExprContext *CreateExprContext(EState *estate);
00302 extern ExprContext *CreateStandaloneExprContext(void);
00303 extern void FreeExprContext(ExprContext *econtext, bool isCommit);
00304 extern void ReScanExprContext(ExprContext *econtext);
00305 
00306 #define ResetExprContext(econtext) \
00307     MemoryContextReset((econtext)->ecxt_per_tuple_memory)
00308 
00309 extern ExprContext *MakePerTupleExprContext(EState *estate);
00310 
00311 /* Get an EState's per-output-tuple exprcontext, making it if first use */
00312 #define GetPerTupleExprContext(estate) \
00313     ((estate)->es_per_tuple_exprcontext ? \
00314      (estate)->es_per_tuple_exprcontext : \
00315      MakePerTupleExprContext(estate))
00316 
00317 #define GetPerTupleMemoryContext(estate) \
00318     (GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
00319 
00320 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
00321 #define ResetPerTupleExprContext(estate) \
00322     do { \
00323         if ((estate)->es_per_tuple_exprcontext) \
00324             ResetExprContext((estate)->es_per_tuple_exprcontext); \
00325     } while (0)
00326 
00327 extern void ExecAssignExprContext(EState *estate, PlanState *planstate);
00328 extern void ExecAssignResultType(PlanState *planstate, TupleDesc tupDesc);
00329 extern void ExecAssignResultTypeFromTL(PlanState *planstate);
00330 extern TupleDesc ExecGetResultType(PlanState *planstate);
00331 extern ProjectionInfo *ExecBuildProjectionInfo(List *targetList,
00332                         ExprContext *econtext,
00333                         TupleTableSlot *slot,
00334                         TupleDesc inputDesc);
00335 extern void ExecAssignProjectionInfo(PlanState *planstate,
00336                          TupleDesc inputDesc);
00337 extern void ExecFreeExprContext(PlanState *planstate);
00338 extern TupleDesc ExecGetScanType(ScanState *scanstate);
00339 extern void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc);
00340 extern void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate);
00341 
00342 extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid);
00343 
00344 extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags);
00345 extern void ExecCloseScanRelation(Relation scanrel);
00346 
00347 extern void ExecOpenIndices(ResultRelInfo *resultRelInfo);
00348 extern void ExecCloseIndices(ResultRelInfo *resultRelInfo);
00349 extern List *ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid,
00350                       EState *estate);
00351 extern bool check_exclusion_constraint(Relation heap, Relation index,
00352                            IndexInfo *indexInfo,
00353                            ItemPointer tupleid,
00354                            Datum *values, bool *isnull,
00355                            EState *estate,
00356                            bool newIndex, bool errorOK);
00357 
00358 extern void RegisterExprContextCallback(ExprContext *econtext,
00359                             ExprContextCallbackFunction function,
00360                             Datum arg);
00361 extern void UnregisterExprContextCallback(ExprContext *econtext,
00362                               ExprContextCallbackFunction function,
00363                               Datum arg);
00364 
00365 #endif   /* EXECUTOR_H  */