]> granicus.if.org Git - postgresql/blob - src/include/nodes/plannodes.h
Fix the plan-invalidation mechanism to treat regclass constants that refer to
[postgresql] / src / include / nodes / plannodes.h
1 /*-------------------------------------------------------------------------
2  *
3  * plannodes.h
4  *        definitions for query plan nodes
5  *
6  *
7  * Portions Copyright (c) 1996-2007, 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.96 2007/10/11 18:05:27 tgl 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  *              PlannedStmt node
29  *
30  * The output of the planner is a Plan tree headed by a PlannedStmt node.
31  * PlannedStmt holds the "one time" information needed by the executor.
32  * ----------------
33  */
34 typedef struct PlannedStmt
35 {
36         NodeTag         type;
37
38         CmdType         commandType;    /* select|insert|update|delete */
39
40         bool            canSetTag;              /* do I set the command result tag? */
41
42         bool            transientPlan;  /* redo plan when TransactionXmin changes? */
43
44         struct Plan *planTree;          /* tree of Plan nodes */
45
46         List       *rtable;                     /* list of RangeTblEntry nodes */
47
48         /* rtable indexes of target relations for INSERT/UPDATE/DELETE */
49         List       *resultRelations;    /* integer list of RT indexes, or NIL */
50
51         Node       *utilityStmt;        /* non-null if this is DECLARE CURSOR */
52
53         IntoClause *intoClause;         /* target for SELECT INTO / CREATE TABLE AS */
54
55         List       *subplans;           /* Plan trees for SubPlan expressions */
56
57         Bitmapset  *rewindPlanIDs;      /* indices of subplans that require REWIND */
58
59         /*
60          * If the query has a returningList then the planner will store a list of
61          * processed targetlists (one per result relation) here.  We must have a
62          * separate RETURNING targetlist for each result rel because column
63          * numbers may vary within an inheritance tree.  In the targetlists, Vars
64          * referencing the result relation will have their original varno and
65          * varattno, while Vars referencing other rels will be converted to have
66          * varno OUTER and varattno referencing a resjunk entry in the top plan
67          * node's targetlist.
68          */
69         List       *returningLists; /* list of lists of TargetEntry, or NIL */
70
71         List       *rowMarks;           /* a list of RowMarkClause's */
72
73         List       *relationOids;       /* OIDs of relations the plan depends on */
74
75         int                     nParamExec;             /* number of PARAM_EXEC Params used */
76 } PlannedStmt;
77
78 /* macro for fetching the Plan associated with a SubPlan node */
79 #define exec_subplan_get_plan(plannedstmt, subplan) \
80         ((Plan *) list_nth((plannedstmt)->subplans, (subplan)->plan_id - 1))
81
82
83 /* ----------------
84  *              Plan node
85  *
86  * All plan nodes "derive" from the Plan structure by having the
87  * Plan structure as the first field.  This ensures that everything works
88  * when nodes are cast to Plan's.  (node pointers are frequently cast to Plan*
89  * when passed around generically in the executor)
90  *
91  * We never actually instantiate any Plan nodes; this is just the common
92  * abstract superclass for all Plan-type nodes.
93  * ----------------
94  */
95 typedef struct Plan
96 {
97         NodeTag         type;
98
99         /*
100          * estimated execution costs for plan (see costsize.c for more info)
101          */
102         Cost            startup_cost;   /* cost expended before fetching any tuples */
103         Cost            total_cost;             /* total cost (assuming all tuples fetched) */
104
105         /*
106          * planner's estimate of result size of this plan step
107          */
108         double          plan_rows;              /* number of rows plan is expected to emit */
109         int                     plan_width;             /* average row width in bytes */
110
111         /*
112          * Common structural data for all Plan types.
113          */
114         List       *targetlist;         /* target list to be computed at this node */
115         List       *qual;                       /* implicitly-ANDed qual conditions */
116         struct Plan *lefttree;          /* input plan tree(s) */
117         struct Plan *righttree;
118         List       *initPlan;           /* Init Plan nodes (un-correlated expr
119                                                                  * subselects) */
120
121         /*
122          * Information for management of parameter-change-driven rescanning
123          *
124          * extParam includes the paramIDs of all external PARAM_EXEC params
125          * affecting this plan node or its children.  setParam params from the
126          * node's initPlans are not included, but their extParams are.
127          *
128          * allParam includes all the extParam paramIDs, plus the IDs of local
129          * params that affect the node (i.e., the setParams of its initplans).
130          * These are _all_ the PARAM_EXEC params that affect this node.
131          */
132         Bitmapset  *extParam;
133         Bitmapset  *allParam;
134 } Plan;
135
136 /* ----------------
137  *      these are are defined to avoid confusion problems with "left"
138  *      and "right" and "inner" and "outer".  The convention is that
139  *      the "left" plan is the "outer" plan and the "right" plan is
140  *      the inner plan, but these make the code more readable.
141  * ----------------
142  */
143 #define innerPlan(node)                 (((Plan *)(node))->righttree)
144 #define outerPlan(node)                 (((Plan *)(node))->lefttree)
145
146
147 /* ----------------
148  *       Result node -
149  *              If no outer plan, evaluate a variable-free targetlist.
150  *              If outer plan, return tuples from outer plan (after a level of
151  *              projection as shown by targetlist).
152  *
153  * If resconstantqual isn't NULL, it represents a one-time qualification
154  * test (i.e., one that doesn't depend on any variables from the outer plan,
155  * so needs to be evaluated only once).
156  * ----------------
157  */
158 typedef struct Result
159 {
160         Plan            plan;
161         Node       *resconstantqual;
162 } Result;
163
164 /* ----------------
165  *       Append node -
166  *              Generate the concatenation of the results of sub-plans.
167  *
168  * Append nodes are sometimes used to switch between several result relations
169  * (when the target of an UPDATE or DELETE is an inheritance set).      Such a
170  * node will have isTarget true.  The Append executor is then responsible
171  * for updating the executor state to point at the correct target relation
172  * whenever it switches subplans.
173  * ----------------
174  */
175 typedef struct Append
176 {
177         Plan            plan;
178         List       *appendplans;
179         bool            isTarget;
180 } Append;
181
182 /* ----------------
183  *       BitmapAnd node -
184  *              Generate the intersection of the results of sub-plans.
185  *
186  * The subplans must be of types that yield tuple bitmaps.      The targetlist
187  * and qual fields of the plan are unused and are always NIL.
188  * ----------------
189  */
190 typedef struct BitmapAnd
191 {
192         Plan            plan;
193         List       *bitmapplans;
194 } BitmapAnd;
195
196 /* ----------------
197  *       BitmapOr node -
198  *              Generate the union of the results of sub-plans.
199  *
200  * The subplans must be of types that yield tuple bitmaps.      The targetlist
201  * and qual fields of the plan are unused and are always NIL.
202  * ----------------
203  */
204 typedef struct BitmapOr
205 {
206         Plan            plan;
207         List       *bitmapplans;
208 } BitmapOr;
209
210 /*
211  * ==========
212  * Scan nodes
213  * ==========
214  */
215 typedef struct Scan
216 {
217         Plan            plan;
218         Index           scanrelid;              /* relid is index into the range table */
219 } Scan;
220
221 /* ----------------
222  *              sequential scan node
223  * ----------------
224  */
225 typedef Scan SeqScan;
226
227 /* ----------------
228  *              index scan node
229  *
230  * indexqualorig is an implicitly-ANDed list of index qual expressions, each
231  * in the same form it appeared in the query WHERE condition.  Each should
232  * be of the form (indexkey OP comparisonval) or (comparisonval OP indexkey).
233  * The indexkey is a Var or expression referencing column(s) of the index's
234  * base table.  The comparisonval might be any expression, but it won't use
235  * any columns of the base table.
236  *
237  * indexqual has the same form, but the expressions have been commuted if
238  * necessary to put the indexkeys on the left, and the indexkeys are replaced
239  * by Var nodes identifying the index columns (varattno is the index column
240  * position, not the base table's column, even though varno is for the base
241  * table).      This is a bit hokey ... would be cleaner to use a special-purpose
242  * node type that could not be mistaken for a regular Var.      But it will do
243  * for now.
244  *
245  * indexstrategy and indexsubtype are lists corresponding one-to-one with
246  * indexqual; they give information about the indexable operators that appear
247  * at the top of each indexqual.
248  * ----------------
249  */
250 typedef struct IndexScan
251 {
252         Scan            scan;
253         Oid                     indexid;                /* OID of index to scan */
254         List       *indexqual;          /* list of index quals (OpExprs) */
255         List       *indexqualorig;      /* the same in original form */
256         List       *indexstrategy;      /* integer list of strategy numbers */
257         List       *indexsubtype;       /* OID list of strategy subtypes */
258         ScanDirection indexorderdir;    /* forward or backward or don't care */
259 } IndexScan;
260
261 /* ----------------
262  *              bitmap index scan node
263  *
264  * BitmapIndexScan delivers a bitmap of potential tuple locations;
265  * it does not access the heap itself.  The bitmap is used by an
266  * ancestor BitmapHeapScan node, possibly after passing through
267  * intermediate BitmapAnd and/or BitmapOr nodes to combine it with
268  * the results of other BitmapIndexScans.
269  *
270  * The fields have the same meanings as for IndexScan, except we don't
271  * store a direction flag because direction is uninteresting.
272  *
273  * In a BitmapIndexScan plan node, the targetlist and qual fields are
274  * not used and are always NIL.  The indexqualorig field is unused at
275  * run time too, but is saved for the benefit of EXPLAIN.
276  * ----------------
277  */
278 typedef struct BitmapIndexScan
279 {
280         Scan            scan;
281         Oid                     indexid;                /* OID of index to scan */
282         List       *indexqual;          /* list of index quals (OpExprs) */
283         List       *indexqualorig;      /* the same in original form */
284         List       *indexstrategy;      /* integer list of strategy numbers */
285         List       *indexsubtype;       /* OID list of strategy subtypes */
286 } BitmapIndexScan;
287
288 /* ----------------
289  *              bitmap sequential scan node
290  *
291  * This needs a copy of the qual conditions being used by the input index
292  * scans because there are various cases where we need to recheck the quals;
293  * for example, when the bitmap is lossy about the specific rows on a page
294  * that meet the index condition.
295  * ----------------
296  */
297 typedef struct BitmapHeapScan
298 {
299         Scan            scan;
300         List       *bitmapqualorig; /* index quals, in standard expr form */
301 } BitmapHeapScan;
302
303 /* ----------------
304  *              tid scan node
305  *
306  * tidquals is an implicitly OR'ed list of qual expressions of the form
307  * "CTID = pseudoconstant" or "CTID = ANY(pseudoconstant_array)".
308  * ----------------
309  */
310 typedef struct TidScan
311 {
312         Scan            scan;
313         List       *tidquals;           /* qual(s) involving CTID = something */
314 } TidScan;
315
316 /* ----------------
317  *              subquery scan node
318  *
319  * SubqueryScan is for scanning the output of a sub-query in the range table.
320  * We often need an extra plan node above the sub-query's plan to perform
321  * expression evaluations (which we can't push into the sub-query without
322  * risking changing its semantics).  Although we are not scanning a physical
323  * relation, we make this a descendant of Scan anyway for code-sharing
324  * purposes.
325  *
326  * Note: we store the sub-plan in the type-specific subplan field, not in
327  * the generic lefttree field as you might expect.      This is because we do
328  * not want plan-tree-traversal routines to recurse into the subplan without
329  * knowing that they are changing Query contexts.
330  *
331  * Note: subrtable is used just to carry the subquery rangetable from
332  * createplan.c to setrefs.c; it should always be NIL by the time the
333  * executor sees the plan.
334  * ----------------
335  */
336 typedef struct SubqueryScan
337 {
338         Scan            scan;
339         Plan       *subplan;
340         List       *subrtable;          /* temporary workspace for planner */
341 } SubqueryScan;
342
343 /* ----------------
344  *              FunctionScan node
345  * ----------------
346  */
347 typedef struct FunctionScan
348 {
349         Scan            scan;
350         Node       *funcexpr;           /* expression tree for func call */
351         List       *funccolnames;       /* output column names (string Value nodes) */
352         List       *funccoltypes;       /* OID list of column type OIDs */
353         List       *funccoltypmods; /* integer list of column typmods */
354 } FunctionScan;
355
356 /* ----------------
357  *              ValuesScan node
358  * ----------------
359  */
360 typedef struct ValuesScan
361 {
362         Scan            scan;
363         List       *values_lists;       /* list of expression lists */
364 } ValuesScan;
365
366 /*
367  * ==========
368  * Join nodes
369  * ==========
370  */
371
372 /* ----------------
373  *              Join node
374  *
375  * jointype:    rule for joining tuples from left and right subtrees
376  * joinqual:    qual conditions that came from JOIN/ON or JOIN/USING
377  *                              (plan.qual contains conditions that came from WHERE)
378  *
379  * When jointype is INNER, joinqual and plan.qual are semantically
380  * interchangeable.  For OUTER jointypes, the two are *not* interchangeable;
381  * only joinqual is used to determine whether a match has been found for
382  * the purpose of deciding whether to generate null-extended tuples.
383  * (But plan.qual is still applied before actually returning a tuple.)
384  * For an outer join, only joinquals are allowed to be used as the merge
385  * or hash condition of a merge or hash join.
386  * ----------------
387  */
388 typedef struct Join
389 {
390         Plan            plan;
391         JoinType        jointype;
392         List       *joinqual;           /* JOIN quals (in addition to plan.qual) */
393 } Join;
394
395 /* ----------------
396  *              nest loop join node
397  * ----------------
398  */
399 typedef struct NestLoop
400 {
401         Join            join;
402 } NestLoop;
403
404 /* ----------------
405  *              merge join node
406  *
407  * The expected ordering of each mergeable column is described by a btree
408  * opfamily OID, a direction (BTLessStrategyNumber or BTGreaterStrategyNumber)
409  * and a nulls-first flag.  Note that the two sides of each mergeclause may
410  * be of different datatypes, but they are ordered the same way according to
411  * the common opfamily.  The operator in each mergeclause must be an equality
412  * operator of the indicated opfamily.
413  * ----------------
414  */
415 typedef struct MergeJoin
416 {
417         Join            join;
418         List       *mergeclauses;               /* mergeclauses as expression trees */
419         /* these are arrays, but have the same length as the mergeclauses list: */
420         Oid                *mergeFamilies;              /* per-clause OIDs of btree opfamilies */
421         int                *mergeStrategies;    /* per-clause ordering (ASC or DESC) */
422         bool       *mergeNullsFirst;    /* per-clause nulls ordering */
423 } MergeJoin;
424
425 /* ----------------
426  *              hash join (probe) node
427  * ----------------
428  */
429 typedef struct HashJoin
430 {
431         Join            join;
432         List       *hashclauses;
433 } HashJoin;
434
435 /* ----------------
436  *              materialization node
437  * ----------------
438  */
439 typedef struct Material
440 {
441         Plan            plan;
442 } Material;
443
444 /* ----------------
445  *              sort node
446  * ----------------
447  */
448 typedef struct Sort
449 {
450         Plan            plan;
451         int                     numCols;                /* number of sort-key columns */
452         AttrNumber *sortColIdx;         /* their indexes in the target list */
453         Oid                *sortOperators;      /* OIDs of operators to sort them by */
454         bool       *nullsFirst;         /* NULLS FIRST/LAST directions */
455 } Sort;
456
457 /* ---------------
458  *       group node -
459  *              Used for queries with GROUP BY (but no aggregates) specified.
460  *              The input must be presorted according to the grouping columns.
461  * ---------------
462  */
463 typedef struct Group
464 {
465         Plan            plan;
466         int                     numCols;                /* number of grouping columns */
467         AttrNumber *grpColIdx;          /* their indexes in the target list */
468         Oid                *grpOperators;       /* equality operators to compare with */
469 } Group;
470
471 /* ---------------
472  *              aggregate node
473  *
474  * An Agg node implements plain or grouped aggregation.  For grouped
475  * aggregation, we can work with presorted input or unsorted input;
476  * the latter strategy uses an internal hashtable.
477  *
478  * Notice the lack of any direct info about the aggregate functions to be
479  * computed.  They are found by scanning the node's tlist and quals during
480  * executor startup.  (It is possible that there are no aggregate functions;
481  * this could happen if they get optimized away by constant-folding, or if
482  * we are using the Agg node to implement hash-based grouping.)
483  * ---------------
484  */
485 typedef enum AggStrategy
486 {
487         AGG_PLAIN,                                      /* simple agg across all input rows */
488         AGG_SORTED,                                     /* grouped agg, input must be sorted */
489         AGG_HASHED                                      /* grouped agg, use internal hashtable */
490 } AggStrategy;
491
492 typedef struct Agg
493 {
494         Plan            plan;
495         AggStrategy aggstrategy;
496         int                     numCols;                /* number of grouping columns */
497         AttrNumber *grpColIdx;          /* their indexes in the target list */
498         Oid                *grpOperators;       /* equality operators to compare with */
499         long            numGroups;              /* estimated number of groups in input */
500 } Agg;
501
502 /* ----------------
503  *              unique node
504  * ----------------
505  */
506 typedef struct Unique
507 {
508         Plan            plan;
509         int                     numCols;                /* number of columns to check for uniqueness */
510         AttrNumber *uniqColIdx;         /* their indexes in the target list */
511         Oid                *uniqOperators;      /* equality operators to compare with */
512 } Unique;
513
514 /* ----------------
515  *              hash build node
516  * ----------------
517  */
518 typedef struct Hash
519 {
520         Plan            plan;
521         /* all other info is in the parent HashJoin node */
522 } Hash;
523
524 /* ----------------
525  *              setop node
526  * ----------------
527  */
528 typedef enum SetOpCmd
529 {
530         SETOPCMD_INTERSECT,
531         SETOPCMD_INTERSECT_ALL,
532         SETOPCMD_EXCEPT,
533         SETOPCMD_EXCEPT_ALL
534 } SetOpCmd;
535
536 typedef struct SetOp
537 {
538         Plan            plan;
539         SetOpCmd        cmd;                    /* what to do */
540         int                     numCols;                /* number of columns to check for
541                                                                  * duplicate-ness */
542         AttrNumber *dupColIdx;          /* their indexes in the target list */
543         Oid                *dupOperators;       /* equality operators to compare with */
544         AttrNumber      flagColIdx;             /* where is the flag column, if any */
545 } SetOp;
546
547 /* ----------------
548  *              limit node
549  *
550  * Note: as of Postgres 8.2, the offset and count expressions are expected
551  * to yield int8, rather than int4 as before.
552  * ----------------
553  */
554 typedef struct Limit
555 {
556         Plan            plan;
557         Node       *limitOffset;        /* OFFSET parameter, or NULL if none */
558         Node       *limitCount;         /* COUNT parameter, or NULL if none */
559 } Limit;
560
561 #endif   /* PLANNODES_H */