1 /*-------------------------------------------------------------------------
4 * definitions for query plan nodes
7 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: plannodes.h,v 1.61 2002/11/30 00:08:22 tgl Exp $
12 *-------------------------------------------------------------------------
17 #include "nodes/execnodes.h"
19 /* ----------------------------------------------------------------
20 * Executor State types are used in the plannode structures
21 * so we have to include their definitions too.
23 * Node Type node information used by executor
27 * Result ResultState resstate;
28 * Append AppendState appendstate;
32 * Scan *** CommonScanState scanstate;
33 * IndexScan IndexScanState indxstate;
34 * SubqueryScan SubqueryScanState subquerystate;
35 * FunctionScan FunctionScanState functionstate;
37 * (*** nodes which inherit Scan also inherit scanstate)
41 * NestLoop NestLoopState nlstate;
42 * MergeJoin MergeJoinState mergestate;
43 * HashJoin HashJoinState hashjoinstate;
47 * Material MaterialState matstate;
48 * Sort SortState sortstate;
49 * Unique UniqueState uniquestate;
50 * SetOp SetOpState setopstate;
51 * Limit LimitState limitstate;
52 * Hash HashState hashstate;
54 * ----------------------------------------------------------------
58 /* ----------------------------------------------------------------
60 * ----------------------------------------------------------------
72 /* estimated execution costs for plan (see costsize.c for more info) */
73 Cost startup_cost; /* cost expended before fetching any
75 Cost total_cost; /* total cost (assuming all tuples
79 * planner's estimate of result size (note: LIMIT, if any, is not
80 * considered in setting plan_rows)
82 double plan_rows; /* number of rows plan is expected to emit */
83 int plan_width; /* average row width in bytes */
86 * execution state data. Having Plan point to this, rather than the
87 * other way round, is 100% bogus.
89 EState *state; /* at execution time, state's of
90 * individual nodes point to one EState
91 * for the whole top-level plan */
93 struct Instrumentation *instrument; /* Optional runtime stats for this
97 * Common structural data for all Plan types. XXX chgParam is runtime
98 * data and should be in the EState, not here.
101 List *qual; /* implicitly-ANDed qual conditions */
102 struct Plan *lefttree;
103 struct Plan *righttree;
104 List *extParam; /* indices of _all_ _external_ PARAM_EXEC
105 * for this plan in global
106 * es_param_exec_vals. Params from
107 * setParam from initPlan-s are not
108 * included, but their execParam-s are
110 List *locParam; /* someones from setParam-s */
111 List *chgParam; /* list of changed ones from the above */
112 List *initPlan; /* Init Plan nodes (un-correlated expr
114 List *subPlan; /* Other SubPlan nodes */
117 * We really need in some TopPlan node to store range table and
118 * resultRelation from Query there and get rid of Query itself from
119 * Executor. Some other stuff like below could be put there, too.
121 int nParamExec; /* Number of them in entire query. This is
122 * to get Executor know about how many
123 * param_exec there are in query plan. */
127 * these are are defined to avoid confusion problems with "left"
128 * and "right" and "inner" and "outer". The convention is that
129 * the "left" plan is the "outer" plan and the "right" plan is
130 * the inner plan, but these make the code more readable.
133 #define innerPlan(node) (((Plan *)(node))->righttree)
134 #define outerPlan(node) (((Plan *)(node))->lefttree)
144 * all plan nodes "derive" from the Plan structure by having the
145 * Plan structure as the first field. This ensures that everything works
146 * when nodes are cast to Plan's. (node pointers are frequently cast to Plan*
147 * when passed around generically in the executor)
153 * If no outer plan, evaluate a variable-free targetlist.
154 * If outer plan, return tuples from outer plan (after a level of
155 * projection as shown by targetlist).
157 * If resconstantqual isn't NULL, it represents a one-time qualification
158 * test (i.e., one that doesn't depend on any variables from the outer plan,
159 * so needs to be evaluated only once).
162 typedef struct Result
165 Node *resconstantqual;
166 ResultState *resstate;
171 * Generate the concatenation of the results of sub-plans.
173 * Append nodes are sometimes used to switch between several result relations
174 * (when the target of an UPDATE or DELETE is an inheritance set). Such a
175 * node will have isTarget true. The Append executor is then responsible
176 * for updating the executor state to point at the correct target relation
177 * whenever it switches subplans.
180 typedef struct Append
185 AppendState *appendstate;
196 Index scanrelid; /* relid is index into the range table */
197 CommonScanState *scanstate;
201 * sequential scan node
204 typedef Scan SeqScan;
210 typedef struct IndexScan
216 ScanDirection indxorderdir;
217 IndexScanState *indxstate;
224 typedef struct TidScan
229 TidScanState *tidstate;
235 * SubqueryScan is for scanning the output of a sub-query in the range table.
236 * We need a special plan node above the sub-query's plan as a place to switch
237 * execution contexts. Although we are not scanning a physical relation,
238 * we make this a descendant of Scan anyway for code-sharing purposes.
240 * Note: we store the sub-plan in the type-specific subplan field, not in
241 * the generic lefttree field as you might expect. This is because we do
242 * not want plan-tree-traversal routines to recurse into the subplan without
243 * knowing that they are changing Query contexts.
246 typedef struct SubqueryScan
256 typedef struct FunctionScan
259 /* no other fields needed at present */
260 /* scan.scanstate actually points at a FunctionScanState node */
272 * jointype: rule for joining tuples from left and right subtrees
273 * joinqual: qual conditions that came from JOIN/ON or JOIN/USING
274 * (plan.qual contains conditions that came from WHERE)
276 * When jointype is INNER, joinqual and plan.qual are semantically
277 * interchangeable. For OUTER jointypes, the two are *not* interchangeable;
278 * only joinqual is used to determine whether a match has been found for
279 * the purpose of deciding whether to generate null-extended tuples.
280 * (But plan.qual is still applied before actually returning a tuple.)
281 * For an outer join, only joinquals are allowed to be used as the merge
282 * or hash condition of a merge or hash join.
289 List *joinqual; /* JOIN quals (in addition to plan.qual) */
293 * nest loop join node
296 typedef struct NestLoop
299 NestLoopState *nlstate;
306 typedef struct MergeJoin
310 MergeJoinState *mergestate;
314 * hash join (probe) node
317 typedef struct HashJoin
321 HashJoinState *hashjoinstate;
327 * An Agg node implements plain or grouped aggregation. For grouped
328 * aggregation, we can work with presorted input or unsorted input;
329 * the latter strategy uses an internal hashtable.
331 * Notice the lack of any direct info about the aggregate functions to be
332 * computed. They are found by scanning the node's tlist and quals during
333 * executor startup. (It is possible that there are no aggregate functions;
334 * this could happen if they get optimized away by constant-folding, or if
335 * we are using the Agg node to implement hash-based grouping.)
338 typedef enum AggStrategy
340 AGG_PLAIN, /* simple agg across all input rows */
341 AGG_SORTED, /* grouped agg, input must be sorted */
342 AGG_HASHED /* grouped agg, use internal hashtable */
348 AggStrategy aggstrategy;
349 int numCols; /* number of grouping columns */
350 AttrNumber *grpColIdx; /* their indexes in the target list */
351 long numGroups; /* estimated number of groups in input */
357 * Used for queries with GROUP BY (but no aggregates) specified.
358 * The input must be presorted according to the grouping columns.
364 int numCols; /* number of grouping columns */
365 AttrNumber *grpColIdx; /* their indexes in the target list */
366 GroupState *grpstate;
370 * materialization node
373 typedef struct Material
376 MaterialState *matstate;
387 SortState *sortstate;
394 typedef struct Unique
397 int numCols; /* number of columns to check for
399 AttrNumber *uniqColIdx; /* indexes into the target list */
400 UniqueState *uniquestate;
407 typedef enum SetOpCmd
410 SETOPCMD_INTERSECT_ALL,
418 SetOpCmd cmd; /* what to do */
419 int numCols; /* number of columns to check for
421 AttrNumber *dupColIdx; /* indexes into the target list */
422 AttrNumber flagColIdx;
423 SetOpState *setopstate;
433 Node *limitOffset; /* OFFSET parameter, or NULL if none */
434 Node *limitCount; /* COUNT parameter, or NULL if none */
435 LimitState *limitstate;
446 HashState *hashstate;
450 /* -------------------
451 * Tee node information
453 * leftParent : the left parent of this node
454 * rightParent: the right parent of this node
455 * -------------------
463 char *teeTableName; /* the name of the table to materialize
465 List *rtentries; /* the range table for the plan below the
466 * Tee may be different than the parent
471 /* ---------------------
473 * ---------------------
475 typedef struct SubPlan
478 Plan *plan; /* subselect plan itself */
479 int plan_id; /* dummy thing because of we haven't equal
480 * funcs for plan nodes... actually, we
481 * could put *plan itself somewhere else
482 * (TopPlan node ?)... */
483 List *rtable; /* range table for subselect */
484 /* setParam and parParam are lists of integers (param IDs) */
485 List *setParam; /* non-correlated EXPR & EXISTS subqueries
486 * have to set some Params for paren Plan */
487 List *parParam; /* indices of corr. Vars from parent plan */
488 SubLink *sublink; /* SubLink node from parser; holds info
489 * about what to do with subselect's
493 * Remaining fields are working state for executor; not used in
496 bool needShutdown; /* TRUE = need to shutdown subplan */
497 HeapTuple curTuple; /* copy of most recent tuple from subplan */
500 #endif /* PLANNODES_H */