]> granicus.if.org Git - postgresql/blob - src/include/nodes/plannodes.h
Upgrade planner and executor to allow multiple hash keys for a hash join,
[postgresql] / src / include / nodes / plannodes.h
1 /*-------------------------------------------------------------------------
2  *
3  * plannodes.h
4  *        definitions for query plan nodes
5  *
6  *
7  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: plannodes.h,v 1.61 2002/11/30 00:08:22 tgl 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  *              FunctionScan                    FunctionScanState               functionstate;
36  *
37  *                (*** nodes which inherit Scan also inherit scanstate)
38  *
39  * join nodes
40  *
41  *              NestLoop                                NestLoopState                   nlstate;
42  *              MergeJoin                               MergeJoinState                  mergestate;
43  *              HashJoin                                HashJoinState                   hashjoinstate;
44  *
45  * materialize nodes
46  *
47  *              Material                                MaterialState                   matstate;
48  *              Sort                                    SortState                               sortstate;
49  *              Unique                                  UniqueState                             uniquestate;
50  *              SetOp                                   SetOpState                              setopstate;
51  *              Limit                                   LimitState                              limitstate;
52  *              Hash                                    HashState                               hashstate;
53  *
54  * ----------------------------------------------------------------
55  */
56
57
58 /* ----------------------------------------------------------------
59  *                                              node definitions
60  * ----------------------------------------------------------------
61  */
62
63 /* ----------------
64  *              Plan node
65  * ----------------
66  */
67
68 typedef struct Plan
69 {
70         NodeTag         type;
71
72         /* estimated execution costs for plan (see costsize.c for more info) */
73         Cost            startup_cost;   /* cost expended before fetching any
74                                                                  * tuples */
75         Cost            total_cost;             /* total cost (assuming all tuples
76                                                                  * fetched) */
77
78         /*
79          * planner's estimate of result size (note: LIMIT, if any, is not
80          * considered in setting plan_rows)
81          */
82         double          plan_rows;              /* number of rows plan is expected to emit */
83         int                     plan_width;             /* average row width in bytes */
84
85         /*
86          * execution state data.  Having Plan point to this, rather than the
87          * other way round, is 100% bogus.
88          */
89         EState     *state;                      /* at execution time, state's of
90                                                                  * individual nodes point to one EState
91                                                                  * for the whole top-level plan */
92
93         struct Instrumentation *instrument; /* Optional runtime stats for this
94                                                                                  * plan node */
95
96         /*
97          * Common structural data for all Plan types.  XXX chgParam is runtime
98          * data and should be in the EState, not here.
99          */
100         List       *targetlist;
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
109                                                                  * here!!! */
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
113                                                                  * subselects) */
114         List       *subPlan;            /* Other SubPlan nodes */
115
116         /*
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.
120          */
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. */
124 } Plan;
125
126 /* ----------------
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.
131  * ----------------
132  */
133 #define innerPlan(node)                 (((Plan *)(node))->righttree)
134 #define outerPlan(node)                 (((Plan *)(node))->lefttree)
135
136
137 /*
138  * ===============
139  * Top-level nodes
140  * ===============
141  */
142
143 /*
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)
148  */
149
150
151 /* ----------------
152  *       Result node -
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).
156  *
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).
160  * ----------------
161  */
162 typedef struct Result
163 {
164         Plan            plan;
165         Node       *resconstantqual;
166         ResultState *resstate;
167 } Result;
168
169 /* ----------------
170  *       Append node -
171  *              Generate the concatenation of the results of sub-plans.
172  *
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.
178  * ----------------
179  */
180 typedef struct Append
181 {
182         Plan            plan;
183         List       *appendplans;
184         bool            isTarget;
185         AppendState *appendstate;
186 } Append;
187
188 /*
189  * ==========
190  * Scan nodes
191  * ==========
192  */
193 typedef struct Scan
194 {
195         Plan            plan;
196         Index           scanrelid;              /* relid is index into the range table */
197         CommonScanState *scanstate;
198 } Scan;
199
200 /* ----------------
201  *              sequential scan node
202  * ----------------
203  */
204 typedef Scan SeqScan;
205
206 /* ----------------
207  *              index scan node
208  * ----------------
209  */
210 typedef struct IndexScan
211 {
212         Scan            scan;
213         List       *indxid;
214         List       *indxqual;
215         List       *indxqualorig;
216         ScanDirection indxorderdir;
217         IndexScanState *indxstate;
218 } IndexScan;
219
220 /* ----------------
221  *                              tid scan node
222  * ----------------
223  */
224 typedef struct TidScan
225 {
226         Scan            scan;
227         bool            needRescan;
228         List       *tideval;
229         TidScanState *tidstate;
230 } TidScan;
231
232 /* ----------------
233  *              subquery scan node
234  *
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.
239  *
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.
244  * ----------------
245  */
246 typedef struct SubqueryScan
247 {
248         Scan            scan;
249         Plan       *subplan;
250 } SubqueryScan;
251
252 /* ----------------
253  *              FunctionScan node
254  * ----------------
255  */
256 typedef struct FunctionScan
257 {
258         Scan            scan;
259         /* no other fields needed at present */
260         /* scan.scanstate actually points at a FunctionScanState node */
261 } FunctionScan;
262
263 /*
264  * ==========
265  * Join nodes
266  * ==========
267  */
268
269 /* ----------------
270  *              Join node
271  *
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)
275  *
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.
283  * ----------------
284  */
285 typedef struct Join
286 {
287         Plan            plan;
288         JoinType        jointype;
289         List       *joinqual;           /* JOIN quals (in addition to plan.qual) */
290 } Join;
291
292 /* ----------------
293  *              nest loop join node
294  * ----------------
295  */
296 typedef struct NestLoop
297 {
298         Join            join;
299         NestLoopState *nlstate;
300 } NestLoop;
301
302 /* ----------------
303  *              merge join node
304  * ----------------
305  */
306 typedef struct MergeJoin
307 {
308         Join            join;
309         List       *mergeclauses;
310         MergeJoinState *mergestate;
311 } MergeJoin;
312
313 /* ----------------
314  *              hash join (probe) node
315  * ----------------
316  */
317 typedef struct HashJoin
318 {
319         Join            join;
320         List       *hashclauses;
321         HashJoinState *hashjoinstate;
322 } HashJoin;
323
324 /* ---------------
325  *              aggregate node
326  *
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.
330  *
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.)
336  * ---------------
337  */
338 typedef enum AggStrategy
339 {
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 */
343 } AggStrategy;
344
345 typedef struct Agg
346 {
347         Plan            plan;
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 */
352         AggState   *aggstate;
353 } Agg;
354
355 /* ---------------
356  *       group node -
357  *              Used for queries with GROUP BY (but no aggregates) specified.
358  *              The input must be presorted according to the grouping columns.
359  * ---------------
360  */
361 typedef struct Group
362 {
363         Plan            plan;
364         int                     numCols;                /* number of grouping columns */
365         AttrNumber *grpColIdx;          /* their indexes in the target list */
366         GroupState *grpstate;
367 } Group;
368
369 /* ----------------
370  *              materialization node
371  * ----------------
372  */
373 typedef struct Material
374 {
375         Plan            plan;
376         MaterialState *matstate;
377 } Material;
378
379 /* ----------------
380  *              sort node
381  * ----------------
382  */
383 typedef struct Sort
384 {
385         Plan            plan;
386         int                     keycount;
387         SortState  *sortstate;
388 } Sort;
389
390 /* ----------------
391  *              unique node
392  * ----------------
393  */
394 typedef struct Unique
395 {
396         Plan            plan;
397         int                     numCols;                /* number of columns to check for
398                                                                  * uniqueness */
399         AttrNumber *uniqColIdx;         /* indexes into the target list */
400         UniqueState *uniquestate;
401 } Unique;
402
403 /* ----------------
404  *              setop node
405  * ----------------
406  */
407 typedef enum SetOpCmd
408 {
409         SETOPCMD_INTERSECT,
410         SETOPCMD_INTERSECT_ALL,
411         SETOPCMD_EXCEPT,
412         SETOPCMD_EXCEPT_ALL
413 } SetOpCmd;
414
415 typedef struct SetOp
416 {
417         Plan            plan;
418         SetOpCmd        cmd;                    /* what to do */
419         int                     numCols;                /* number of columns to check for
420                                                                  * duplicate-ness */
421         AttrNumber *dupColIdx;          /* indexes into the target list */
422         AttrNumber      flagColIdx;
423         SetOpState *setopstate;
424 } SetOp;
425
426 /* ----------------
427  *              limit node
428  * ----------------
429  */
430 typedef struct Limit
431 {
432         Plan            plan;
433         Node       *limitOffset;        /* OFFSET parameter, or NULL if none */
434         Node       *limitCount;         /* COUNT parameter, or NULL if none */
435         LimitState *limitstate;
436 } Limit;
437
438 /* ----------------
439  *              hash build node
440  * ----------------
441  */
442 typedef struct Hash
443 {
444         Plan            plan;
445         List       *hashkeys;
446         HashState  *hashstate;
447 } Hash;
448
449 #ifdef NOT_USED
450 /* -------------------
451  *              Tee node information
452  *
453  *        leftParent :                          the left parent of this node
454  *        rightParent:                          the right parent of this node
455  * -------------------
456 */
457 typedef struct Tee
458 {
459         Plan            plan;
460         Plan       *leftParent;
461         Plan       *rightParent;
462         TeeState   *teestate;
463         char       *teeTableName;       /* the name of the table to materialize
464                                                                  * the tee into */
465         List       *rtentries;          /* the range table for the plan below the
466                                                                  * Tee may be different than the parent
467                                                                  * plans */
468 }       Tee;
469 #endif
470
471 /* ---------------------
472  *              SubPlan node
473  * ---------------------
474  */
475 typedef struct SubPlan
476 {
477         NodeTag         type;
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
490                                                                  * results */
491
492         /*
493          * Remaining fields are working state for executor; not used in
494          * planning
495          */
496         bool            needShutdown;   /* TRUE = need to shutdown subplan */
497         HeapTuple       curTuple;               /* copy of most recent tuple from subplan */
498 } SubPlan;
499
500 #endif   /* PLANNODES_H */