]> granicus.if.org Git - postgresql/blob - src/include/nodes/plannodes.h
Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[postgresql] / src / include / nodes / plannodes.h
1 /*-------------------------------------------------------------------------
2  *
3  * plannodes.h
4  *        definitions for query plan nodes
5  *
6  *
7  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: plannodes.h,v 1.47 2001/01/24 19:43:26 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef PLANNODES_H
15 #define PLANNODES_H
16
17 #include "nodes/execnodes.h"
18
19 /* ----------------------------------------------------------------
20  *      Executor State types are used in the plannode structures
21  *      so we have to include their definitions too.
22  *
23  *              Node Type                               node information used by executor
24  *
25  * control nodes
26  *
27  *              Result                                  ResultState                             resstate;
28  *              Append                                  AppendState                             appendstate;
29  *
30  * scan nodes
31  *
32  *              Scan ***                                CommonScanState                 scanstate;
33  *              IndexScan                               IndexScanState                  indxstate;
34  *              SubqueryScan                    SubqueryScanState               subquerystate;
35  *
36  *                (*** nodes which inherit Scan also inherit scanstate)
37  *
38  * join nodes
39  *
40  *              NestLoop                                NestLoopState                   nlstate;
41  *              MergeJoin                               MergeJoinState                  mergestate;
42  *              HashJoin                                HashJoinState                   hashjoinstate;
43  *
44  * materialize nodes
45  *
46  *              Material                                MaterialState                   matstate;
47  *              Sort                                    SortState                               sortstate;
48  *              Unique                                  UniqueState                             uniquestate;
49  *              SetOp                                   SetOpState                              setopstate;
50  *              Limit                                   LimitState                              limitstate;
51  *              Hash                                    HashState                               hashstate;
52  *
53  * ----------------------------------------------------------------
54  */
55
56
57 /* ----------------------------------------------------------------
58  *                                              node definitions
59  * ----------------------------------------------------------------
60  */
61
62 /* ----------------
63  *              Plan node
64  * ----------------
65  */
66
67 typedef struct Plan
68 {
69         NodeTag         type;
70
71         /* estimated execution costs for plan (see costsize.c for more info) */
72         Cost            startup_cost;   /* cost expended before fetching any
73                                                                  * tuples */
74         Cost            total_cost;             /* total cost (assuming all tuples
75                                                                  * fetched) */
76
77         /*
78          * planner's estimate of result size (note: LIMIT, if any, is not
79          * considered in setting plan_rows)
80          */
81         double          plan_rows;              /* number of rows plan is expected to emit */
82         int                     plan_width;             /* average row width in bytes */
83
84         EState     *state;                      /* at execution time, state's of
85                                                                  * individual nodes point to one EState
86                                                                  * for the whole top-level plan */
87         List       *targetlist;
88         List       *qual;                       /* implicitly-ANDed qual conditions */
89         struct Plan *lefttree;
90         struct Plan *righttree;
91         List       *extParam;           /* indices of _all_ _external_ PARAM_EXEC
92                                                                  * for this plan in global
93                                                                  * es_param_exec_vals. Params from
94                                                                  * setParam from initPlan-s are not
95                                                                  * included, but their execParam-s are
96                                                                  * here!!! */
97         List       *locParam;           /* someones from setParam-s */
98         List       *chgParam;           /* list of changed ones from the above */
99         List       *initPlan;           /* Init Plan nodes (un-correlated expr
100                                                                  * subselects) */
101         List       *subPlan;            /* Other SubPlan nodes */
102
103         /*
104          * We really need in some TopPlan node to store range table and
105          * resultRelation from Query there and get rid of Query itself from
106          * Executor. Some other stuff like below could be put there, too.
107          */
108         int                     nParamExec;             /* Number of them in entire query. This is
109                                                                  * to get Executor know about how many
110                                                                  * param_exec there are in query plan. */
111 } Plan;
112
113 /* ----------------
114  *      these are are defined to avoid confusion problems with "left"
115  *      and "right" and "inner" and "outer".  The convention is that
116  *      the "left" plan is the "outer" plan and the "right" plan is
117  *      the inner plan, but these make the code more readable.
118  * ----------------
119  */
120 #define innerPlan(node)                 (((Plan *)(node))->righttree)
121 #define outerPlan(node)                 (((Plan *)(node))->lefttree)
122
123
124 /*
125  * ===============
126  * Top-level nodes
127  * ===============
128  */
129
130 /* all plan nodes "derive" from the Plan structure by having the
131    Plan structure as the first field.  This ensures that everything works
132    when nodes are cast to Plan's.  (node pointers are frequently cast to Plan*
133    when passed around generically in the executor */
134
135
136 /* ----------------
137  *       Result node -
138  *              If no outer plan, evaluate a variable-free targetlist.
139  *              If outer plan, return tuples from outer plan that satisfy
140  *              given quals (we can also do a level of projection)
141  * ----------------
142  */
143 typedef struct Result
144 {
145         Plan            plan;
146         Node       *resconstantqual;
147         ResultState *resstate;
148 } Result;
149
150 /* ----------------
151  *       Append node -
152  *              Generate the concatenation of the results of sub-plans.
153  *
154  * Append nodes are sometimes used to switch between several result relations
155  * (when the target of an UPDATE or DELETE is an inheritance set).  Such a
156  * node will have isTarget true.  The Append executor is then responsible
157  * for updating the executor state to point at the correct target relation
158  * whenever it switches subplans.
159  * ----------------
160  */
161 typedef struct Append
162 {
163         Plan            plan;
164         List       *appendplans;
165         bool            isTarget;
166         AppendState *appendstate;
167 } Append;
168
169 /*
170  * ==========
171  * Scan nodes
172  * ==========
173  */
174 typedef struct Scan
175 {
176         Plan            plan;
177         Index           scanrelid;              /* relid is index into the range table */
178         CommonScanState *scanstate;
179 } Scan;
180
181 /* ----------------
182  *              sequential scan node
183  * ----------------
184  */
185 typedef Scan SeqScan;
186
187 /* ----------------
188  *              index scan node
189  * ----------------
190  */
191 typedef struct IndexScan
192 {
193         Scan            scan;
194         List       *indxid;
195         List       *indxqual;
196         List       *indxqualorig;
197         ScanDirection indxorderdir;
198         IndexScanState *indxstate;
199 } IndexScan;
200
201 /* ----------------
202  *                              tid scan node
203  * ----------------
204  */
205 typedef struct TidScan
206 {
207         Scan            scan;
208         bool            needRescan;
209         List       *tideval;
210         TidScanState *tidstate;
211 } TidScan;
212
213 /* ----------------
214  *              subquery scan node
215  *
216  * SubqueryScan is for scanning the output of a sub-query in the range table.
217  * We need a special plan node above the sub-query's plan as a place to switch
218  * execution contexts.  Although we are not scanning a physical relation,
219  * we make this a descendant of Scan anyway for code-sharing purposes.
220  *
221  * Note: we store the sub-plan in the type-specific subplan field, not in
222  * the generic lefttree field as you might expect.  This is because we do
223  * not want plan-tree-traversal routines to recurse into the subplan without
224  * knowing that they are changing Query contexts.
225  * ----------------
226  */
227 typedef struct SubqueryScan
228 {
229         Scan            scan;
230         Plan       *subplan;
231 } SubqueryScan;
232
233 /*
234  * ==========
235  * Join nodes
236  * ==========
237  */
238
239 /* ----------------
240  *              Join node
241  *
242  * jointype:    rule for joining tuples from left and right subtrees
243  * joinqual:    qual conditions that came from JOIN/ON or JOIN/USING
244  *                              (plan.qual contains conditions that came from WHERE)
245  *
246  * When jointype is INNER, joinqual and plan.qual are semantically
247  * interchangeable.  For OUTER jointypes, the two are *not* interchangeable;
248  * only joinqual is used to determine whether a match has been found for
249  * the purpose of deciding whether to generate null-extended tuples.
250  * (But plan.qual is still applied before actually returning a tuple.)
251  * For an outer join, only joinquals are allowed to be used as the merge
252  * or hash condition of a merge or hash join.
253  * ----------------
254  */
255 typedef struct Join
256 {
257         Plan            plan;
258         JoinType        jointype;
259         List       *joinqual;           /* JOIN quals (in addition to plan.qual) */
260 } Join;
261
262 /* ----------------
263  *              nest loop join node
264  * ----------------
265  */
266 typedef struct NestLoop
267 {
268         Join            join;
269         NestLoopState *nlstate;
270 } NestLoop;
271
272 /* ----------------
273  *              merge join node
274  * ----------------
275  */
276 typedef struct MergeJoin
277 {
278         Join            join;
279         List       *mergeclauses;
280         MergeJoinState *mergestate;
281 } MergeJoin;
282
283 /* ----------------
284  *              hash join (probe) node
285  * ----------------
286  */
287 typedef struct HashJoin
288 {
289         Join            join;
290         List       *hashclauses;
291         Oid                     hashjoinop;
292         HashJoinState *hashjoinstate;
293 } HashJoin;
294
295 /* ---------------
296  *              aggregate node
297  * ---------------
298  */
299 typedef struct Agg
300 {
301         Plan            plan;
302         AggState   *aggstate;
303 } Agg;
304
305 /* ---------------
306  *       group node -
307  *              use for queries with GROUP BY specified.
308  *
309  *              If tuplePerGroup is true, one tuple (with group columns only) is
310  *              returned for each group and NULL is returned when there are no more
311  *              groups. Otherwise, all the tuples of a group are returned with a
312  *              NULL returned at the end of each group. (see nodeGroup.c for details)
313  * ---------------
314  */
315 typedef struct Group
316 {
317         Plan            plan;
318         bool            tuplePerGroup;  /* what tuples to return (see above) */
319         int                     numCols;                /* number of group columns */
320         AttrNumber *grpColIdx;          /* indexes into the target list */
321         GroupState *grpstate;
322 } Group;
323
324 /* ----------------
325  *              materialization node
326  * ----------------
327  */
328 typedef struct Material
329 {
330         Plan            plan;
331         MaterialState *matstate;
332 } Material;
333
334 /* ----------------
335  *              sort node
336  * ----------------
337  */
338 typedef struct Sort
339 {
340         Plan            plan;
341         int                     keycount;
342         SortState  *sortstate;
343 } Sort;
344
345 /* ----------------
346  *              unique node
347  * ----------------
348  */
349 typedef struct Unique
350 {
351         Plan            plan;
352         int                     numCols;                /* number of columns to check for
353                                                                  * uniqueness */
354         AttrNumber *uniqColIdx;         /* indexes into the target list */
355         UniqueState *uniquestate;
356 } Unique;
357
358 /* ----------------
359  *              setop node
360  * ----------------
361  */
362 typedef enum SetOpCmd
363 {
364         SETOPCMD_INTERSECT,
365         SETOPCMD_INTERSECT_ALL,
366         SETOPCMD_EXCEPT,
367         SETOPCMD_EXCEPT_ALL
368 } SetOpCmd;
369
370 typedef struct SetOp
371 {
372         Plan            plan;
373         SetOpCmd        cmd;                    /* what to do */
374         int                     numCols;                /* number of columns to check for
375                                                                  * duplicate-ness */
376         AttrNumber *dupColIdx;          /* indexes into the target list */
377         AttrNumber      flagColIdx;
378         SetOpState *setopstate;
379 } SetOp;
380
381 /* ----------------
382  *              limit node
383  * ----------------
384  */
385 typedef struct Limit
386 {
387         Plan            plan;
388         Node       *limitOffset;        /* OFFSET parameter, or NULL if none */
389         Node       *limitCount;         /* COUNT parameter, or NULL if none */
390         LimitState *limitstate;
391 } Limit;
392
393 /* ----------------
394  *              hash build node
395  * ----------------
396  */
397 typedef struct Hash
398 {
399         Plan            plan;
400         Node       *hashkey;
401         HashState  *hashstate;
402 } Hash;
403
404 #ifdef NOT_USED
405 /* -------------------
406  *              Tee node information
407  *
408  *        leftParent :                          the left parent of this node
409  *        rightParent:                          the right parent of this node
410  * -------------------
411 */
412 typedef struct Tee
413 {
414         Plan            plan;
415         Plan       *leftParent;
416         Plan       *rightParent;
417         TeeState   *teestate;
418         char       *teeTableName;       /* the name of the table to materialize
419                                                                  * the tee into */
420         List       *rtentries;          /* the range table for the plan below the
421                                                                  * Tee may be different than the parent
422                                                                  * plans */
423 }                       Tee;
424
425 #endif
426
427 /* ---------------------
428  *              SubPlan node
429  * ---------------------
430  */
431 typedef struct SubPlan
432 {
433         NodeTag         type;
434         Plan       *plan;                       /* subselect plan itself */
435         int                     plan_id;                /* dummy thing because of we haven't equal
436                                                                  * funcs for plan nodes... actually, we
437                                                                  * could put *plan itself somewhere else
438                                                                  * (TopPlan node ?)... */
439         List       *rtable;                     /* range table for subselect */
440         /* setParam and parParam are lists of integers (param IDs) */
441         List       *setParam;           /* non-correlated EXPR & EXISTS subqueries
442                                                                  * have to set some Params for paren Plan */
443         List       *parParam;           /* indices of corr. Vars from parent plan */
444         SubLink    *sublink;            /* SubLink node from parser; holds info
445                                                                  * about what to do with subselect's
446                                                                  * results */
447
448         /*
449          * Remaining fields are working state for executor; not used in
450          * planning
451          */
452         bool            needShutdown;   /* TRUE = need to shutdown subplan */
453         HeapTuple       curTuple;               /* copy of most recent tuple from subplan */
454 } SubPlan;
455
456 #endif   /* PLANNODES_H */