]> granicus.if.org Git - postgresql/blob - src/include/nodes/plannodes.h
59425e722966f463356dca79cc25f571d568af75
[postgresql] / src / include / nodes / plannodes.h
1 /*-------------------------------------------------------------------------
2  *
3  * plannodes.h
4  *        definitions for query plan nodes
5  *
6  *
7  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/include/nodes/plannodes.h,v 1.83 2006/03/05 15:58:57 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef PLANNODES_H
15 #define PLANNODES_H
16
17 #include "access/sdir.h"
18 #include "nodes/bitmapset.h"
19 #include "nodes/primnodes.h"
20
21
22 /* ----------------------------------------------------------------
23  *                                              node definitions
24  * ----------------------------------------------------------------
25  */
26
27 /* ----------------
28  *              Plan node
29  *
30  * All plan nodes "derive" from the Plan structure by having the
31  * Plan structure as the first field.  This ensures that everything works
32  * when nodes are cast to Plan's.  (node pointers are frequently cast to Plan*
33  * when passed around generically in the executor)
34  *
35  * We never actually instantiate any Plan nodes; this is just the common
36  * abstract superclass for all Plan-type nodes.
37  * ----------------
38  */
39 typedef struct Plan
40 {
41         NodeTag         type;
42
43         /*
44          * estimated execution costs for plan (see costsize.c for more info)
45          */
46         Cost            startup_cost;   /* cost expended before fetching any tuples */
47         Cost            total_cost;             /* total cost (assuming all tuples fetched) */
48
49         /*
50          * planner's estimate of result size of this plan step
51          */
52         double          plan_rows;              /* number of rows plan is expected to emit */
53         int                     plan_width;             /* average row width in bytes */
54
55         /*
56          * Common structural data for all Plan types.
57          */
58         List       *targetlist;         /* target list to be computed at this node */
59         List       *qual;                       /* implicitly-ANDed qual conditions */
60         struct Plan *lefttree;          /* input plan tree(s) */
61         struct Plan *righttree;
62         List       *initPlan;           /* Init Plan nodes (un-correlated expr
63                                                                  * subselects) */
64
65         /*
66          * Information for management of parameter-change-driven rescanning
67          *
68          * extParam includes the paramIDs of all external PARAM_EXEC params
69          * affecting this plan node or its children.  setParam params from the
70          * node's initPlans are not included, but their extParams are.
71          *
72          * allParam includes all the extParam paramIDs, plus the IDs of local
73          * params that affect the node (i.e., the setParams of its initplans).
74          * These are _all_ the PARAM_EXEC params that affect this node.
75          */
76         Bitmapset  *extParam;
77         Bitmapset  *allParam;
78
79         /*
80          * We really need in some TopPlan node to store range table and
81          * resultRelation from Query there and get rid of Query itself from
82          * Executor. Some other stuff like below could be put there, too.
83          */
84         int                     nParamExec;             /* Number of them in entire query. This is to
85                                                                  * get Executor know about how many PARAM_EXEC
86                                                                  * there are in query plan. */
87 } Plan;
88
89 /* ----------------
90  *      these are are defined to avoid confusion problems with "left"
91  *      and "right" and "inner" and "outer".  The convention is that
92  *      the "left" plan is the "outer" plan and the "right" plan is
93  *      the inner plan, but these make the code more readable.
94  * ----------------
95  */
96 #define innerPlan(node)                 (((Plan *)(node))->righttree)
97 #define outerPlan(node)                 (((Plan *)(node))->lefttree)
98
99
100 /* ----------------
101  *       Result node -
102  *              If no outer plan, evaluate a variable-free targetlist.
103  *              If outer plan, return tuples from outer plan (after a level of
104  *              projection as shown by targetlist).
105  *
106  * If resconstantqual isn't NULL, it represents a one-time qualification
107  * test (i.e., one that doesn't depend on any variables from the outer plan,
108  * so needs to be evaluated only once).
109  * ----------------
110  */
111 typedef struct Result
112 {
113         Plan            plan;
114         Node       *resconstantqual;
115 } Result;
116
117 /* ----------------
118  *       Append node -
119  *              Generate the concatenation of the results of sub-plans.
120  *
121  * Append nodes are sometimes used to switch between several result relations
122  * (when the target of an UPDATE or DELETE is an inheritance set).      Such a
123  * node will have isTarget true.  The Append executor is then responsible
124  * for updating the executor state to point at the correct target relation
125  * whenever it switches subplans.
126  * ----------------
127  */
128 typedef struct Append
129 {
130         Plan            plan;
131         List       *appendplans;
132         bool            isTarget;
133 } Append;
134
135 /* ----------------
136  *       BitmapAnd node -
137  *              Generate the intersection of the results of sub-plans.
138  *
139  * The subplans must be of types that yield tuple bitmaps.      The targetlist
140  * and qual fields of the plan are unused and are always NIL.
141  * ----------------
142  */
143 typedef struct BitmapAnd
144 {
145         Plan            plan;
146         List       *bitmapplans;
147 } BitmapAnd;
148
149 /* ----------------
150  *       BitmapOr node -
151  *              Generate the union of the results of sub-plans.
152  *
153  * The subplans must be of types that yield tuple bitmaps.      The targetlist
154  * and qual fields of the plan are unused and are always NIL.
155  * ----------------
156  */
157 typedef struct BitmapOr
158 {
159         Plan            plan;
160         List       *bitmapplans;
161 } BitmapOr;
162
163 /*
164  * ==========
165  * Scan nodes
166  * ==========
167  */
168 typedef struct Scan
169 {
170         Plan            plan;
171         Index           scanrelid;              /* relid is index into the range table */
172 } Scan;
173
174 /* ----------------
175  *              sequential scan node
176  * ----------------
177  */
178 typedef Scan SeqScan;
179
180 /* ----------------
181  *              index scan node
182  *
183  * indexqualorig is an implicitly-ANDed list of index qual expressions, each
184  * in the same form it appeared in the query WHERE condition.  Each should
185  * be of the form (indexkey OP comparisonval) or (comparisonval OP indexkey).
186  * The indexkey is a Var or expression referencing column(s) of the index's
187  * base table.  The comparisonval might be any expression, but it won't use
188  * any columns of the base table.
189  *
190  * indexqual has the same form, but the expressions have been commuted if
191  * necessary to put the indexkeys on the left, and the indexkeys are replaced
192  * by Var nodes identifying the index columns (varattno is the index column
193  * position, not the base table's column, even though varno is for the base
194  * table).      This is a bit hokey ... would be cleaner to use a special-purpose
195  * node type that could not be mistaken for a regular Var.      But it will do
196  * for now.
197  *
198  * indexstrategy and indexsubtype are lists corresponding one-to-one with
199  * indexqual; they give information about the indexable operators that appear
200  * at the top of each indexqual.
201  * ----------------
202  */
203 typedef struct IndexScan
204 {
205         Scan            scan;
206         Oid                     indexid;                /* OID of index to scan */
207         List       *indexqual;          /* list of index quals (OpExprs) */
208         List       *indexqualorig;      /* the same in original form */
209         List       *indexstrategy;      /* integer list of strategy numbers */
210         List       *indexsubtype;       /* OID list of strategy subtypes */
211         ScanDirection indexorderdir;    /* forward or backward or don't care */
212 } IndexScan;
213
214 /* ----------------
215  *              bitmap index scan node
216  *
217  * BitmapIndexScan delivers a bitmap of potential tuple locations;
218  * it does not access the heap itself.  The bitmap is used by an
219  * ancestor BitmapHeapScan node, possibly after passing through
220  * intermediate BitmapAnd and/or BitmapOr nodes to combine it with
221  * the results of other BitmapIndexScans.
222  *
223  * The fields have the same meanings as for IndexScan, except we don't
224  * store a direction flag because direction is uninteresting.
225  *
226  * In a BitmapIndexScan plan node, the targetlist and qual fields are
227  * not used and are always NIL.  The indexqualorig field is unused at
228  * run time too, but is saved for the benefit of EXPLAIN.
229  * ----------------
230  */
231 typedef struct BitmapIndexScan
232 {
233         Scan            scan;
234         Oid                     indexid;                /* OID of index to scan */
235         List       *indexqual;          /* list of index quals (OpExprs) */
236         List       *indexqualorig;      /* the same in original form */
237         List       *indexstrategy;      /* integer list of strategy numbers */
238         List       *indexsubtype;       /* OID list of strategy subtypes */
239 } BitmapIndexScan;
240
241 /* ----------------
242  *              bitmap sequential scan node
243  *
244  * This needs a copy of the qual conditions being used by the input index
245  * scans because there are various cases where we need to recheck the quals;
246  * for example, when the bitmap is lossy about the specific rows on a page
247  * that meet the index condition.
248  * ----------------
249  */
250 typedef struct BitmapHeapScan
251 {
252         Scan            scan;
253         List       *bitmapqualorig; /* index quals, in standard expr form */
254 } BitmapHeapScan;
255
256 /* ----------------
257  *              tid scan node
258  *
259  * tidquals is an implicitly OR'ed list of qual expressions of the form
260  * "CTID = pseudoconstant" or "CTID = ANY(pseudoconstant_array)".
261  * ----------------
262  */
263 typedef struct TidScan
264 {
265         Scan            scan;
266         List       *tidquals;           /* qual(s) involving CTID = something */
267 } TidScan;
268
269 /* ----------------
270  *              subquery scan node
271  *
272  * SubqueryScan is for scanning the output of a sub-query in the range table.
273  * We need a special plan node above the sub-query's plan as a place to switch
274  * execution contexts.  Although we are not scanning a physical relation,
275  * we make this a descendant of Scan anyway for code-sharing purposes.
276  *
277  * Note: we store the sub-plan in the type-specific subplan field, not in
278  * the generic lefttree field as you might expect.      This is because we do
279  * not want plan-tree-traversal routines to recurse into the subplan without
280  * knowing that they are changing Query contexts.
281  * ----------------
282  */
283 typedef struct SubqueryScan
284 {
285         Scan            scan;
286         Plan       *subplan;
287 } SubqueryScan;
288
289 /* ----------------
290  *              FunctionScan node
291  * ----------------
292  */
293 typedef struct FunctionScan
294 {
295         Scan            scan;
296         /* no other fields needed at present */
297 } FunctionScan;
298
299 /*
300  * ==========
301  * Join nodes
302  * ==========
303  */
304
305 /* ----------------
306  *              Join node
307  *
308  * jointype:    rule for joining tuples from left and right subtrees
309  * joinqual:    qual conditions that came from JOIN/ON or JOIN/USING
310  *                              (plan.qual contains conditions that came from WHERE)
311  *
312  * When jointype is INNER, joinqual and plan.qual are semantically
313  * interchangeable.  For OUTER jointypes, the two are *not* interchangeable;
314  * only joinqual is used to determine whether a match has been found for
315  * the purpose of deciding whether to generate null-extended tuples.
316  * (But plan.qual is still applied before actually returning a tuple.)
317  * For an outer join, only joinquals are allowed to be used as the merge
318  * or hash condition of a merge or hash join.
319  * ----------------
320  */
321 typedef struct Join
322 {
323         Plan            plan;
324         JoinType        jointype;
325         List       *joinqual;           /* JOIN quals (in addition to plan.qual) */
326 } Join;
327
328 /* ----------------
329  *              nest loop join node
330  * ----------------
331  */
332 typedef struct NestLoop
333 {
334         Join            join;
335 } NestLoop;
336
337 /* ----------------
338  *              merge join node
339  * ----------------
340  */
341 typedef struct MergeJoin
342 {
343         Join            join;
344         List       *mergeclauses;
345 } MergeJoin;
346
347 /* ----------------
348  *              hash join (probe) node
349  * ----------------
350  */
351 typedef struct HashJoin
352 {
353         Join            join;
354         List       *hashclauses;
355 } HashJoin;
356
357 /* ----------------
358  *              materialization node
359  * ----------------
360  */
361 typedef struct Material
362 {
363         Plan            plan;
364 } Material;
365
366 /* ----------------
367  *              sort node
368  * ----------------
369  */
370 typedef struct Sort
371 {
372         Plan            plan;
373         int                     numCols;                /* number of sort-key columns */
374         AttrNumber *sortColIdx;         /* their indexes in the target list */
375         Oid                *sortOperators;      /* OIDs of operators to sort them by */
376 } Sort;
377
378 /* ---------------
379  *       group node -
380  *              Used for queries with GROUP BY (but no aggregates) specified.
381  *              The input must be presorted according to the grouping columns.
382  * ---------------
383  */
384 typedef struct Group
385 {
386         Plan            plan;
387         int                     numCols;                /* number of grouping columns */
388         AttrNumber *grpColIdx;          /* their indexes in the target list */
389 } Group;
390
391 /* ---------------
392  *              aggregate node
393  *
394  * An Agg node implements plain or grouped aggregation.  For grouped
395  * aggregation, we can work with presorted input or unsorted input;
396  * the latter strategy uses an internal hashtable.
397  *
398  * Notice the lack of any direct info about the aggregate functions to be
399  * computed.  They are found by scanning the node's tlist and quals during
400  * executor startup.  (It is possible that there are no aggregate functions;
401  * this could happen if they get optimized away by constant-folding, or if
402  * we are using the Agg node to implement hash-based grouping.)
403  * ---------------
404  */
405 typedef enum AggStrategy
406 {
407         AGG_PLAIN,                                      /* simple agg across all input rows */
408         AGG_SORTED,                                     /* grouped agg, input must be sorted */
409         AGG_HASHED                                      /* grouped agg, use internal hashtable */
410 } AggStrategy;
411
412 typedef struct Agg
413 {
414         Plan            plan;
415         AggStrategy aggstrategy;
416         int                     numCols;                /* number of grouping columns */
417         AttrNumber *grpColIdx;          /* their indexes in the target list */
418         long            numGroups;              /* estimated number of groups in input */
419 } Agg;
420
421 /* ----------------
422  *              unique node
423  * ----------------
424  */
425 typedef struct Unique
426 {
427         Plan            plan;
428         int                     numCols;                /* number of columns to check for uniqueness */
429         AttrNumber *uniqColIdx;         /* indexes into the target list */
430 } Unique;
431
432 /* ----------------
433  *              hash build node
434  * ----------------
435  */
436 typedef struct Hash
437 {
438         Plan            plan;
439         /* all other info is in the parent HashJoin node */
440 } Hash;
441
442 /* ----------------
443  *              setop node
444  * ----------------
445  */
446 typedef enum SetOpCmd
447 {
448         SETOPCMD_INTERSECT,
449         SETOPCMD_INTERSECT_ALL,
450         SETOPCMD_EXCEPT,
451         SETOPCMD_EXCEPT_ALL
452 } SetOpCmd;
453
454 typedef struct SetOp
455 {
456         Plan            plan;
457         SetOpCmd        cmd;                    /* what to do */
458         int                     numCols;                /* number of columns to check for
459                                                                  * duplicate-ness */
460         AttrNumber *dupColIdx;          /* indexes into the target list */
461         AttrNumber      flagColIdx;
462 } SetOp;
463
464 /* ----------------
465  *              limit node
466  * ----------------
467  */
468 typedef struct Limit
469 {
470         Plan            plan;
471         Node       *limitOffset;        /* OFFSET parameter, or NULL if none */
472         Node       *limitCount;         /* COUNT parameter, or NULL if none */
473 } Limit;
474
475 #endif   /* PLANNODES_H */