1 /*-------------------------------------------------------------------------
4 * support for the POSTGRES executor module
7 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 *-------------------------------------------------------------------------
17 #include "executor/execdesc.h"
18 #include "nodes/parsenodes.h"
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).
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 (such as creating a SELECT INTO target table).
34 * However, error checks (such as permission checks) should be performed.
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.)
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.
45 * MARK indicates that the plan node must support Mark/Restore calls.
46 * When this is not passed, no Mark/Restore will occur.
48 #define EXEC_FLAG_EXPLAIN_ONLY 0x0001 /* EXPLAIN, no ANALYZE */
49 #define EXEC_FLAG_REWIND 0x0002 /* need efficient rescan */
50 #define EXEC_FLAG_BACKWARD 0x0004 /* need backward scan */
51 #define EXEC_FLAG_MARK 0x0008 /* need mark/restore */
55 * ExecEvalExpr was formerly a function containing a switch statement;
56 * now it's just a macro invoking the function pointed to by an ExprState
57 * node. Beware of double evaluation of the ExprState argument!
59 #define ExecEvalExpr(expr, econtext, isNull, isDone) \
60 ((*(expr)->evalfunc) (expr, econtext, isNull, isDone))
63 /* Hook for plugins to get control in ExecutorStart() */
64 typedef void (*ExecutorStart_hook_type
) (QueryDesc
*queryDesc
, int eflags
);
65 extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook
;
67 /* Hook for plugins to get control in ExecutorRun() */
68 typedef void (*ExecutorRun_hook_type
) (QueryDesc
*queryDesc
,
69 ScanDirection direction
,
71 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook
;
73 /* Hook for plugins to get control in ExecutorEnd() */
74 typedef void (*ExecutorEnd_hook_type
) (QueryDesc
*queryDesc
);
75 extern PGDLLIMPORT ExecutorEnd_hook_type ExecutorEnd_hook
;
79 * prototypes from functions in execAmi.c
81 extern void ExecReScan(PlanState
*node
, ExprContext
*exprCtxt
);
82 extern void ExecMarkPos(PlanState
*node
);
83 extern void ExecRestrPos(PlanState
*node
);
84 extern bool ExecSupportsMarkRestore(NodeTag plantype
);
85 extern bool ExecSupportsBackwardScan(Plan
*node
);
88 * prototypes from functions in execCurrent.c
90 extern bool execCurrentOf(CurrentOfExpr
*cexpr
,
91 ExprContext
*econtext
,
93 ItemPointer current_tid
);
96 * prototypes from functions in execGrouping.c
98 extern bool execTuplesMatch(TupleTableSlot
*slot1
,
99 TupleTableSlot
*slot2
,
101 AttrNumber
*matchColIdx
,
102 FmgrInfo
*eqfunctions
,
103 MemoryContext evalContext
);
104 extern bool execTuplesUnequal(TupleTableSlot
*slot1
,
105 TupleTableSlot
*slot2
,
107 AttrNumber
*matchColIdx
,
108 FmgrInfo
*eqfunctions
,
109 MemoryContext evalContext
);
110 extern FmgrInfo
*execTuplesMatchPrepare(int numCols
,
112 extern void execTuplesHashPrepare(int numCols
,
114 FmgrInfo
**eqFunctions
,
115 FmgrInfo
**hashFunctions
);
116 extern TupleHashTable
BuildTupleHashTable(int numCols
, AttrNumber
*keyColIdx
,
117 FmgrInfo
*eqfunctions
,
118 FmgrInfo
*hashfunctions
,
119 int nbuckets
, Size entrysize
,
120 MemoryContext tablecxt
,
121 MemoryContext tempcxt
);
122 extern TupleHashEntry
LookupTupleHashEntry(TupleHashTable hashtable
,
123 TupleTableSlot
*slot
,
125 extern TupleHashEntry
FindTupleHashEntry(TupleHashTable hashtable
,
126 TupleTableSlot
*slot
,
127 FmgrInfo
*eqfunctions
,
128 FmgrInfo
*hashfunctions
);
131 * prototypes from functions in execJunk.c
133 extern JunkFilter
*ExecInitJunkFilter(List
*targetList
, bool hasoid
,
134 TupleTableSlot
*slot
);
135 extern JunkFilter
*ExecInitJunkFilterConversion(List
*targetList
,
136 TupleDesc cleanTupType
,
137 TupleTableSlot
*slot
);
138 extern AttrNumber
ExecFindJunkAttribute(JunkFilter
*junkfilter
,
139 const char *attrName
);
140 extern Datum
ExecGetJunkAttribute(TupleTableSlot
*slot
, AttrNumber attno
,
142 extern TupleTableSlot
*ExecFilterJunk(JunkFilter
*junkfilter
,
143 TupleTableSlot
*slot
);
144 extern HeapTuple
ExecRemoveJunk(JunkFilter
*junkfilter
, TupleTableSlot
*slot
);
148 * prototypes from functions in execMain.c
150 extern void ExecutorStart(QueryDesc
*queryDesc
, int eflags
);
151 extern void standard_ExecutorStart(QueryDesc
*queryDesc
, int eflags
);
152 extern void ExecutorRun(QueryDesc
*queryDesc
,
153 ScanDirection direction
, long count
);
154 extern void standard_ExecutorRun(QueryDesc
*queryDesc
,
155 ScanDirection direction
, long count
);
156 extern void ExecutorEnd(QueryDesc
*queryDesc
);
157 extern void standard_ExecutorEnd(QueryDesc
*queryDesc
);
158 extern void ExecutorRewind(QueryDesc
*queryDesc
);
159 extern void InitResultRelInfo(ResultRelInfo
*resultRelInfo
,
160 Relation resultRelationDesc
,
161 Index resultRelationIndex
,
164 extern ResultRelInfo
*ExecGetTriggerResultRel(EState
*estate
, Oid relid
);
165 extern bool ExecContextForcesOids(PlanState
*planstate
, bool *hasoids
);
166 extern void ExecConstraints(ResultRelInfo
*resultRelInfo
,
167 TupleTableSlot
*slot
, EState
*estate
);
168 extern TupleTableSlot
*EvalPlanQual(EState
*estate
, Index rti
,
169 ItemPointer tid
, TransactionId priorXmax
);
170 extern PlanState
*ExecGetActivePlanTree(QueryDesc
*queryDesc
);
171 extern DestReceiver
*CreateIntoRelDestReceiver(void);
174 * prototypes from functions in execProcnode.c
176 extern PlanState
*ExecInitNode(Plan
*node
, EState
*estate
, int eflags
);
177 extern TupleTableSlot
*ExecProcNode(PlanState
*node
);
178 extern Node
*MultiExecProcNode(PlanState
*node
);
179 extern int ExecCountSlotsNode(Plan
*node
);
180 extern void ExecEndNode(PlanState
*node
);
183 * prototypes from functions in execQual.c
185 extern Datum
GetAttributeByNum(HeapTupleHeader tuple
, AttrNumber attrno
,
187 extern Datum
GetAttributeByName(HeapTupleHeader tuple
, const char *attname
,
189 extern Tuplestorestate
*ExecMakeTableFunctionResult(ExprState
*funcexpr
,
190 ExprContext
*econtext
,
191 TupleDesc expectedDesc
,
193 extern Datum
ExecEvalExprSwitchContext(ExprState
*expression
, ExprContext
*econtext
,
194 bool *isNull
, ExprDoneCond
*isDone
);
195 extern ExprState
*ExecInitExpr(Expr
*node
, PlanState
*parent
);
196 extern ExprState
*ExecPrepareExpr(Expr
*node
, EState
*estate
);
197 extern bool ExecQual(List
*qual
, ExprContext
*econtext
, bool resultForNull
);
198 extern int ExecTargetListLength(List
*targetlist
);
199 extern int ExecCleanTargetListLength(List
*targetlist
);
200 extern TupleTableSlot
*ExecProject(ProjectionInfo
*projInfo
,
201 ExprDoneCond
*isDone
);
204 * prototypes from functions in execScan.c
206 typedef TupleTableSlot
*(*ExecScanAccessMtd
) (ScanState
*node
);
208 extern TupleTableSlot
*ExecScan(ScanState
*node
, ExecScanAccessMtd accessMtd
);
209 extern void ExecAssignScanProjectionInfo(ScanState
*node
);
212 * prototypes from functions in execTuples.c
214 extern void ExecInitResultTupleSlot(EState
*estate
, PlanState
*planstate
);
215 extern void ExecInitScanTupleSlot(EState
*estate
, ScanState
*scanstate
);
216 extern TupleTableSlot
*ExecInitExtraTupleSlot(EState
*estate
);
217 extern TupleTableSlot
*ExecInitNullTupleSlot(EState
*estate
,
219 extern TupleDesc
ExecTypeFromTL(List
*targetList
, bool hasoid
);
220 extern TupleDesc
ExecCleanTypeFromTL(List
*targetList
, bool hasoid
);
221 extern TupleDesc
ExecTypeFromExprList(List
*exprList
);
222 extern void UpdateChangedParamSet(PlanState
*node
, Bitmapset
*newchg
);
224 typedef struct TupOutputState
226 /* use "struct" here to allow forward reference */
227 struct AttInMetadata
*metadata
;
228 TupleTableSlot
*slot
;
232 extern TupOutputState
*begin_tup_output_tupdesc(DestReceiver
*dest
,
234 extern void do_tup_output(TupOutputState
*tstate
, char **values
);
235 extern void do_text_output_multiline(TupOutputState
*tstate
, char *text
);
236 extern void end_tup_output(TupOutputState
*tstate
);
239 * Write a single line of text given as a C string.
241 * Should only be used with a single-TEXT-attribute tupdesc.
243 #define do_text_output_oneline(tstate, text_to_emit) \
246 values_[0] = (text_to_emit); \
247 do_tup_output(tstate, values_); \
252 * prototypes from functions in execUtils.c
254 extern EState
*CreateExecutorState(void);
255 extern void FreeExecutorState(EState
*estate
);
256 extern ExprContext
*CreateExprContext(EState
*estate
);
257 extern ExprContext
*CreateStandaloneExprContext(void);
258 extern void FreeExprContext(ExprContext
*econtext
);
259 extern void ReScanExprContext(ExprContext
*econtext
);
261 #define ResetExprContext(econtext) \
262 MemoryContextReset((econtext)->ecxt_per_tuple_memory)
264 extern ExprContext
*MakePerTupleExprContext(EState
*estate
);
266 /* Get an EState's per-output-tuple exprcontext, making it if first use */
267 #define GetPerTupleExprContext(estate) \
268 ((estate)->es_per_tuple_exprcontext ? \
269 (estate)->es_per_tuple_exprcontext : \
270 MakePerTupleExprContext(estate))
272 #define GetPerTupleMemoryContext(estate) \
273 (GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
275 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
276 #define ResetPerTupleExprContext(estate) \
278 if ((estate)->es_per_tuple_exprcontext) \
279 ResetExprContext((estate)->es_per_tuple_exprcontext); \
282 extern void ExecAssignExprContext(EState
*estate
, PlanState
*planstate
);
283 extern void ExecAssignResultType(PlanState
*planstate
, TupleDesc tupDesc
);
284 extern void ExecAssignResultTypeFromTL(PlanState
*planstate
);
285 extern TupleDesc
ExecGetResultType(PlanState
*planstate
);
286 extern ProjectionInfo
*ExecBuildProjectionInfo(List
*targetList
,
287 ExprContext
*econtext
,
288 TupleTableSlot
*slot
,
289 TupleDesc inputDesc
);
290 extern void ExecAssignProjectionInfo(PlanState
*planstate
,
291 TupleDesc inputDesc
);
292 extern void ExecFreeExprContext(PlanState
*planstate
);
293 extern TupleDesc
ExecGetScanType(ScanState
*scanstate
);
294 extern void ExecAssignScanType(ScanState
*scanstate
, TupleDesc tupDesc
);
295 extern void ExecAssignScanTypeFromOuterPlan(ScanState
*scanstate
);
297 extern bool ExecRelationIsTargetRelation(EState
*estate
, Index scanrelid
);
299 extern Relation
ExecOpenScanRelation(EState
*estate
, Index scanrelid
);
300 extern void ExecCloseScanRelation(Relation scanrel
);
302 extern void ExecOpenIndices(ResultRelInfo
*resultRelInfo
);
303 extern void ExecCloseIndices(ResultRelInfo
*resultRelInfo
);
304 extern void ExecInsertIndexTuples(TupleTableSlot
*slot
, ItemPointer tupleid
,
305 EState
*estate
, bool is_vacuum
);
307 extern void RegisterExprContextCallback(ExprContext
*econtext
,
308 ExprContextCallbackFunction function
,
310 extern void UnregisterExprContextCallback(ExprContext
*econtext
,
311 ExprContextCallbackFunction function
,
314 #endif /* EXECUTOR_H */