1 /*-------------------------------------------------------------------------
4 * support for the POSTGRES executor module
7 * Portions Copyright (c) 1996-2008, 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 ExecutorRun() */
64 typedef TupleTableSlot
*(*ExecutorRun_hook_type
) (QueryDesc
*queryDesc
,
65 ScanDirection direction
,
67 extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook
;
71 * prototypes from functions in execAmi.c
73 extern void ExecReScan(PlanState
*node
, ExprContext
*exprCtxt
);
74 extern void ExecMarkPos(PlanState
*node
);
75 extern void ExecRestrPos(PlanState
*node
);
76 extern bool ExecSupportsMarkRestore(NodeTag plantype
);
77 extern bool ExecSupportsBackwardScan(Plan
*node
);
78 extern bool ExecMayReturnRawTuples(PlanState
*node
);
81 * prototypes from functions in execCurrent.c
83 extern bool execCurrentOf(CurrentOfExpr
*cexpr
,
84 ExprContext
*econtext
,
86 ItemPointer current_tid
);
89 * prototypes from functions in execGrouping.c
91 extern bool execTuplesMatch(TupleTableSlot
*slot1
,
92 TupleTableSlot
*slot2
,
94 AttrNumber
*matchColIdx
,
95 FmgrInfo
*eqfunctions
,
96 MemoryContext evalContext
);
97 extern bool execTuplesUnequal(TupleTableSlot
*slot1
,
98 TupleTableSlot
*slot2
,
100 AttrNumber
*matchColIdx
,
101 FmgrInfo
*eqfunctions
,
102 MemoryContext evalContext
);
103 extern FmgrInfo
*execTuplesMatchPrepare(int numCols
,
105 extern void execTuplesHashPrepare(int numCols
,
107 FmgrInfo
**eqFunctions
,
108 FmgrInfo
**hashFunctions
);
109 extern TupleHashTable
BuildTupleHashTable(int numCols
, AttrNumber
*keyColIdx
,
110 FmgrInfo
*eqfunctions
,
111 FmgrInfo
*hashfunctions
,
112 int nbuckets
, Size entrysize
,
113 MemoryContext tablecxt
,
114 MemoryContext tempcxt
);
115 extern TupleHashEntry
LookupTupleHashEntry(TupleHashTable hashtable
,
116 TupleTableSlot
*slot
,
118 extern TupleHashEntry
FindTupleHashEntry(TupleHashTable hashtable
,
119 TupleTableSlot
*slot
,
120 FmgrInfo
*eqfunctions
,
121 FmgrInfo
*hashfunctions
);
124 * prototypes from functions in execJunk.c
126 extern JunkFilter
*ExecInitJunkFilter(List
*targetList
, bool hasoid
,
127 TupleTableSlot
*slot
);
128 extern JunkFilter
*ExecInitJunkFilterConversion(List
*targetList
,
129 TupleDesc cleanTupType
,
130 TupleTableSlot
*slot
);
131 extern AttrNumber
ExecFindJunkAttribute(JunkFilter
*junkfilter
,
132 const char *attrName
);
133 extern Datum
ExecGetJunkAttribute(TupleTableSlot
*slot
, AttrNumber attno
,
135 extern TupleTableSlot
*ExecFilterJunk(JunkFilter
*junkfilter
,
136 TupleTableSlot
*slot
);
137 extern HeapTuple
ExecRemoveJunk(JunkFilter
*junkfilter
, TupleTableSlot
*slot
);
141 * prototypes from functions in execMain.c
143 extern void ExecutorStart(QueryDesc
*queryDesc
, int eflags
);
144 extern TupleTableSlot
*ExecutorRun(QueryDesc
*queryDesc
,
145 ScanDirection direction
, long count
);
146 extern TupleTableSlot
*standard_ExecutorRun(QueryDesc
*queryDesc
,
147 ScanDirection direction
, long count
);
148 extern void ExecutorEnd(QueryDesc
*queryDesc
);
149 extern void ExecutorRewind(QueryDesc
*queryDesc
);
150 extern void InitResultRelInfo(ResultRelInfo
*resultRelInfo
,
151 Relation resultRelationDesc
,
152 Index resultRelationIndex
,
155 extern ResultRelInfo
*ExecGetTriggerResultRel(EState
*estate
, Oid relid
);
156 extern bool ExecContextForcesOids(PlanState
*planstate
, bool *hasoids
);
157 extern void ExecConstraints(ResultRelInfo
*resultRelInfo
,
158 TupleTableSlot
*slot
, EState
*estate
);
159 extern TupleTableSlot
*EvalPlanQual(EState
*estate
, Index rti
,
160 ItemPointer tid
, TransactionId priorXmax
);
161 extern PlanState
*ExecGetActivePlanTree(QueryDesc
*queryDesc
);
162 extern DestReceiver
*CreateIntoRelDestReceiver(void);
165 * prototypes from functions in execProcnode.c
167 extern PlanState
*ExecInitNode(Plan
*node
, EState
*estate
, int eflags
);
168 extern TupleTableSlot
*ExecProcNode(PlanState
*node
);
169 extern Node
*MultiExecProcNode(PlanState
*node
);
170 extern int ExecCountSlotsNode(Plan
*node
);
171 extern void ExecEndNode(PlanState
*node
);
174 * prototypes from functions in execQual.c
176 extern Datum
GetAttributeByNum(HeapTupleHeader tuple
, AttrNumber attrno
,
178 extern Datum
GetAttributeByName(HeapTupleHeader tuple
, const char *attname
,
180 extern void init_fcache(Oid foid
, FuncExprState
*fcache
,
181 MemoryContext fcacheCxt
);
182 extern Datum
ExecMakeFunctionResult(FuncExprState
*fcache
,
183 ExprContext
*econtext
,
185 ExprDoneCond
*isDone
);
186 extern Tuplestorestate
*ExecMakeTableFunctionResult(ExprState
*funcexpr
,
187 ExprContext
*econtext
,
188 TupleDesc expectedDesc
,
189 TupleDesc
*returnDesc
);
190 extern Datum
ExecEvalExprSwitchContext(ExprState
*expression
, ExprContext
*econtext
,
191 bool *isNull
, ExprDoneCond
*isDone
);
192 extern ExprState
*ExecInitExpr(Expr
*node
, PlanState
*parent
);
193 extern ExprState
*ExecPrepareExpr(Expr
*node
, EState
*estate
);
194 extern bool ExecQual(List
*qual
, ExprContext
*econtext
, bool resultForNull
);
195 extern int ExecTargetListLength(List
*targetlist
);
196 extern int ExecCleanTargetListLength(List
*targetlist
);
197 extern TupleTableSlot
*ExecProject(ProjectionInfo
*projInfo
,
198 ExprDoneCond
*isDone
);
201 * prototypes from functions in execScan.c
203 typedef TupleTableSlot
*(*ExecScanAccessMtd
) (ScanState
*node
);
205 extern TupleTableSlot
*ExecScan(ScanState
*node
, ExecScanAccessMtd accessMtd
);
206 extern void ExecAssignScanProjectionInfo(ScanState
*node
);
209 * prototypes from functions in execTuples.c
211 extern void ExecInitResultTupleSlot(EState
*estate
, PlanState
*planstate
);
212 extern void ExecInitScanTupleSlot(EState
*estate
, ScanState
*scanstate
);
213 extern TupleTableSlot
*ExecInitExtraTupleSlot(EState
*estate
);
214 extern TupleTableSlot
*ExecInitNullTupleSlot(EState
*estate
,
216 extern TupleDesc
ExecTypeFromTL(List
*targetList
, bool hasoid
);
217 extern TupleDesc
ExecCleanTypeFromTL(List
*targetList
, bool hasoid
);
218 extern TupleDesc
ExecTypeFromExprList(List
*exprList
);
219 extern void UpdateChangedParamSet(PlanState
*node
, Bitmapset
*newchg
);
221 typedef struct TupOutputState
223 /* use "struct" here to allow forward reference */
224 struct AttInMetadata
*metadata
;
225 TupleTableSlot
*slot
;
229 extern TupOutputState
*begin_tup_output_tupdesc(DestReceiver
*dest
,
231 extern void do_tup_output(TupOutputState
*tstate
, char **values
);
232 extern void do_text_output_multiline(TupOutputState
*tstate
, char *text
);
233 extern void end_tup_output(TupOutputState
*tstate
);
236 * Write a single line of text given as a C string.
238 * Should only be used with a single-TEXT-attribute tupdesc.
240 #define do_text_output_oneline(tstate, text_to_emit) \
243 values_[0] = (text_to_emit); \
244 do_tup_output(tstate, values_); \
249 * prototypes from functions in execUtils.c
251 extern EState
*CreateExecutorState(void);
252 extern void FreeExecutorState(EState
*estate
);
253 extern ExprContext
*CreateExprContext(EState
*estate
);
254 extern ExprContext
*CreateStandaloneExprContext(void);
255 extern void FreeExprContext(ExprContext
*econtext
);
256 extern void ReScanExprContext(ExprContext
*econtext
);
258 #define ResetExprContext(econtext) \
259 MemoryContextReset((econtext)->ecxt_per_tuple_memory)
261 extern ExprContext
*MakePerTupleExprContext(EState
*estate
);
263 /* Get an EState's per-output-tuple exprcontext, making it if first use */
264 #define GetPerTupleExprContext(estate) \
265 ((estate)->es_per_tuple_exprcontext ? \
266 (estate)->es_per_tuple_exprcontext : \
267 MakePerTupleExprContext(estate))
269 #define GetPerTupleMemoryContext(estate) \
270 (GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
272 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
273 #define ResetPerTupleExprContext(estate) \
275 if ((estate)->es_per_tuple_exprcontext) \
276 ResetExprContext((estate)->es_per_tuple_exprcontext); \
279 extern void ExecAssignExprContext(EState
*estate
, PlanState
*planstate
);
280 extern void ExecAssignResultType(PlanState
*planstate
, TupleDesc tupDesc
);
281 extern void ExecAssignResultTypeFromTL(PlanState
*planstate
);
282 extern TupleDesc
ExecGetResultType(PlanState
*planstate
);
283 extern ProjectionInfo
*ExecBuildProjectionInfo(List
*targetList
,
284 ExprContext
*econtext
,
285 TupleTableSlot
*slot
,
286 TupleDesc inputDesc
);
287 extern void ExecAssignProjectionInfo(PlanState
*planstate
,
288 TupleDesc inputDesc
);
289 extern void ExecFreeExprContext(PlanState
*planstate
);
290 extern TupleDesc
ExecGetScanType(ScanState
*scanstate
);
291 extern void ExecAssignScanType(ScanState
*scanstate
, TupleDesc tupDesc
);
292 extern void ExecAssignScanTypeFromOuterPlan(ScanState
*scanstate
);
294 extern bool ExecRelationIsTargetRelation(EState
*estate
, Index scanrelid
);
296 extern Relation
ExecOpenScanRelation(EState
*estate
, Index scanrelid
);
297 extern void ExecCloseScanRelation(Relation scanrel
);
299 extern void ExecOpenIndices(ResultRelInfo
*resultRelInfo
);
300 extern void ExecCloseIndices(ResultRelInfo
*resultRelInfo
);
301 extern void ExecInsertIndexTuples(TupleTableSlot
*slot
, ItemPointer tupleid
,
302 EState
*estate
, bool is_vacuum
);
304 extern void RegisterExprContextCallback(ExprContext
*econtext
,
305 ExprContextCallbackFunction function
,
307 extern void UnregisterExprContextCallback(ExprContext
*econtext
,
308 ExprContextCallbackFunction function
,
311 #endif /* EXECUTOR_H */