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
);
80 * prototypes from functions in execCurrent.c
82 extern bool execCurrentOf(CurrentOfExpr
*cexpr
,
83 ExprContext
*econtext
,
85 ItemPointer current_tid
);
88 * prototypes from functions in execGrouping.c
90 extern bool execTuplesMatch(TupleTableSlot
*slot1
,
91 TupleTableSlot
*slot2
,
93 AttrNumber
*matchColIdx
,
94 FmgrInfo
*eqfunctions
,
95 MemoryContext evalContext
);
96 extern bool execTuplesUnequal(TupleTableSlot
*slot1
,
97 TupleTableSlot
*slot2
,
99 AttrNumber
*matchColIdx
,
100 FmgrInfo
*eqfunctions
,
101 MemoryContext evalContext
);
102 extern FmgrInfo
*execTuplesMatchPrepare(int numCols
,
104 extern void execTuplesHashPrepare(int numCols
,
106 FmgrInfo
**eqFunctions
,
107 FmgrInfo
**hashFunctions
);
108 extern TupleHashTable
BuildTupleHashTable(int numCols
, AttrNumber
*keyColIdx
,
109 FmgrInfo
*eqfunctions
,
110 FmgrInfo
*hashfunctions
,
111 int nbuckets
, Size entrysize
,
112 MemoryContext tablecxt
,
113 MemoryContext tempcxt
);
114 extern TupleHashEntry
LookupTupleHashEntry(TupleHashTable hashtable
,
115 TupleTableSlot
*slot
,
117 extern TupleHashEntry
FindTupleHashEntry(TupleHashTable hashtable
,
118 TupleTableSlot
*slot
,
119 FmgrInfo
*eqfunctions
,
120 FmgrInfo
*hashfunctions
);
123 * prototypes from functions in execJunk.c
125 extern JunkFilter
*ExecInitJunkFilter(List
*targetList
, bool hasoid
,
126 TupleTableSlot
*slot
);
127 extern JunkFilter
*ExecInitJunkFilterConversion(List
*targetList
,
128 TupleDesc cleanTupType
,
129 TupleTableSlot
*slot
);
130 extern AttrNumber
ExecFindJunkAttribute(JunkFilter
*junkfilter
,
131 const char *attrName
);
132 extern Datum
ExecGetJunkAttribute(TupleTableSlot
*slot
, AttrNumber attno
,
134 extern TupleTableSlot
*ExecFilterJunk(JunkFilter
*junkfilter
,
135 TupleTableSlot
*slot
);
136 extern HeapTuple
ExecRemoveJunk(JunkFilter
*junkfilter
, TupleTableSlot
*slot
);
140 * prototypes from functions in execMain.c
142 extern void ExecutorStart(QueryDesc
*queryDesc
, int eflags
);
143 extern TupleTableSlot
*ExecutorRun(QueryDesc
*queryDesc
,
144 ScanDirection direction
, long count
);
145 extern TupleTableSlot
*standard_ExecutorRun(QueryDesc
*queryDesc
,
146 ScanDirection direction
, long count
);
147 extern void ExecutorEnd(QueryDesc
*queryDesc
);
148 extern void ExecutorRewind(QueryDesc
*queryDesc
);
149 extern void InitResultRelInfo(ResultRelInfo
*resultRelInfo
,
150 Relation resultRelationDesc
,
151 Index resultRelationIndex
,
154 extern ResultRelInfo
*ExecGetTriggerResultRel(EState
*estate
, Oid relid
);
155 extern bool ExecContextForcesOids(PlanState
*planstate
, bool *hasoids
);
156 extern void ExecConstraints(ResultRelInfo
*resultRelInfo
,
157 TupleTableSlot
*slot
, EState
*estate
);
158 extern TupleTableSlot
*EvalPlanQual(EState
*estate
, Index rti
,
159 ItemPointer tid
, TransactionId priorXmax
);
160 extern PlanState
*ExecGetActivePlanTree(QueryDesc
*queryDesc
);
161 extern DestReceiver
*CreateIntoRelDestReceiver(void);
164 * prototypes from functions in execProcnode.c
166 extern PlanState
*ExecInitNode(Plan
*node
, EState
*estate
, int eflags
);
167 extern TupleTableSlot
*ExecProcNode(PlanState
*node
);
168 extern Node
*MultiExecProcNode(PlanState
*node
);
169 extern int ExecCountSlotsNode(Plan
*node
);
170 extern void ExecEndNode(PlanState
*node
);
173 * prototypes from functions in execQual.c
175 extern Datum
GetAttributeByNum(HeapTupleHeader tuple
, AttrNumber attrno
,
177 extern Datum
GetAttributeByName(HeapTupleHeader tuple
, const char *attname
,
179 extern void init_fcache(Oid foid
, FuncExprState
*fcache
,
180 MemoryContext fcacheCxt
);
181 extern Datum
ExecMakeFunctionResult(FuncExprState
*fcache
,
182 ExprContext
*econtext
,
184 ExprDoneCond
*isDone
);
185 extern Tuplestorestate
*ExecMakeTableFunctionResult(ExprState
*funcexpr
,
186 ExprContext
*econtext
,
187 TupleDesc expectedDesc
,
188 TupleDesc
*returnDesc
);
189 extern Datum
ExecEvalExprSwitchContext(ExprState
*expression
, ExprContext
*econtext
,
190 bool *isNull
, ExprDoneCond
*isDone
);
191 extern ExprState
*ExecInitExpr(Expr
*node
, PlanState
*parent
);
192 extern ExprState
*ExecPrepareExpr(Expr
*node
, EState
*estate
);
193 extern bool ExecQual(List
*qual
, ExprContext
*econtext
, bool resultForNull
);
194 extern int ExecTargetListLength(List
*targetlist
);
195 extern int ExecCleanTargetListLength(List
*targetlist
);
196 extern TupleTableSlot
*ExecProject(ProjectionInfo
*projInfo
,
197 ExprDoneCond
*isDone
);
200 * prototypes from functions in execScan.c
202 typedef TupleTableSlot
*(*ExecScanAccessMtd
) (ScanState
*node
);
204 extern TupleTableSlot
*ExecScan(ScanState
*node
, ExecScanAccessMtd accessMtd
);
205 extern void ExecAssignScanProjectionInfo(ScanState
*node
);
208 * prototypes from functions in execTuples.c
210 extern void ExecInitResultTupleSlot(EState
*estate
, PlanState
*planstate
);
211 extern void ExecInitScanTupleSlot(EState
*estate
, ScanState
*scanstate
);
212 extern TupleTableSlot
*ExecInitExtraTupleSlot(EState
*estate
);
213 extern TupleTableSlot
*ExecInitNullTupleSlot(EState
*estate
,
215 extern TupleDesc
ExecTypeFromTL(List
*targetList
, bool hasoid
);
216 extern TupleDesc
ExecCleanTypeFromTL(List
*targetList
, bool hasoid
);
217 extern TupleDesc
ExecTypeFromExprList(List
*exprList
);
218 extern void UpdateChangedParamSet(PlanState
*node
, Bitmapset
*newchg
);
220 typedef struct TupOutputState
222 /* use "struct" here to allow forward reference */
223 struct AttInMetadata
*metadata
;
224 TupleTableSlot
*slot
;
228 extern TupOutputState
*begin_tup_output_tupdesc(DestReceiver
*dest
,
230 extern void do_tup_output(TupOutputState
*tstate
, char **values
);
231 extern void do_text_output_multiline(TupOutputState
*tstate
, char *text
);
232 extern void end_tup_output(TupOutputState
*tstate
);
235 * Write a single line of text given as a C string.
237 * Should only be used with a single-TEXT-attribute tupdesc.
239 #define do_text_output_oneline(tstate, text_to_emit) \
242 values_[0] = (text_to_emit); \
243 do_tup_output(tstate, values_); \
248 * prototypes from functions in execUtils.c
250 extern EState
*CreateExecutorState(void);
251 extern void FreeExecutorState(EState
*estate
);
252 extern ExprContext
*CreateExprContext(EState
*estate
);
253 extern ExprContext
*CreateStandaloneExprContext(void);
254 extern void FreeExprContext(ExprContext
*econtext
);
255 extern void ReScanExprContext(ExprContext
*econtext
);
257 #define ResetExprContext(econtext) \
258 MemoryContextReset((econtext)->ecxt_per_tuple_memory)
260 extern ExprContext
*MakePerTupleExprContext(EState
*estate
);
262 /* Get an EState's per-output-tuple exprcontext, making it if first use */
263 #define GetPerTupleExprContext(estate) \
264 ((estate)->es_per_tuple_exprcontext ? \
265 (estate)->es_per_tuple_exprcontext : \
266 MakePerTupleExprContext(estate))
268 #define GetPerTupleMemoryContext(estate) \
269 (GetPerTupleExprContext(estate)->ecxt_per_tuple_memory)
271 /* Reset an EState's per-output-tuple exprcontext, if one's been created */
272 #define ResetPerTupleExprContext(estate) \
274 if ((estate)->es_per_tuple_exprcontext) \
275 ResetExprContext((estate)->es_per_tuple_exprcontext); \
278 extern void ExecAssignExprContext(EState
*estate
, PlanState
*planstate
);
279 extern void ExecAssignResultType(PlanState
*planstate
, TupleDesc tupDesc
);
280 extern void ExecAssignResultTypeFromTL(PlanState
*planstate
);
281 extern TupleDesc
ExecGetResultType(PlanState
*planstate
);
282 extern ProjectionInfo
*ExecBuildProjectionInfo(List
*targetList
,
283 ExprContext
*econtext
,
284 TupleTableSlot
*slot
,
285 TupleDesc inputDesc
);
286 extern void ExecAssignProjectionInfo(PlanState
*planstate
,
287 TupleDesc inputDesc
);
288 extern void ExecFreeExprContext(PlanState
*planstate
);
289 extern TupleDesc
ExecGetScanType(ScanState
*scanstate
);
290 extern void ExecAssignScanType(ScanState
*scanstate
, TupleDesc tupDesc
);
291 extern void ExecAssignScanTypeFromOuterPlan(ScanState
*scanstate
);
293 extern bool ExecRelationIsTargetRelation(EState
*estate
, Index scanrelid
);
295 extern Relation
ExecOpenScanRelation(EState
*estate
, Index scanrelid
);
296 extern void ExecCloseScanRelation(Relation scanrel
);
298 extern void ExecOpenIndices(ResultRelInfo
*resultRelInfo
);
299 extern void ExecCloseIndices(ResultRelInfo
*resultRelInfo
);
300 extern void ExecInsertIndexTuples(TupleTableSlot
*slot
, ItemPointer tupleid
,
301 EState
*estate
, bool is_vacuum
);
303 extern void RegisterExprContextCallback(ExprContext
*econtext
,
304 ExprContextCallbackFunction function
,
306 extern void UnregisterExprContextCallback(ExprContext
*econtext
,
307 ExprContextCallbackFunction function
,
310 #endif /* EXECUTOR_H */