1 /*-------------------------------------------------------------------------
4 * definitions for query plan nodes
7 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/nodes/plannodes.h,v 1.96 2007/10/11 18:05:27 tgl Exp $
12 *-------------------------------------------------------------------------
17 #include "access/sdir.h"
18 #include "nodes/bitmapset.h"
19 #include "nodes/primnodes.h"
22 /* ----------------------------------------------------------------
24 * ----------------------------------------------------------------
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.
34 typedef struct PlannedStmt
38 CmdType commandType; /* select|insert|update|delete */
40 bool canSetTag; /* do I set the command result tag? */
42 bool transientPlan; /* redo plan when TransactionXmin changes? */
44 struct Plan *planTree; /* tree of Plan nodes */
46 List *rtable; /* list of RangeTblEntry nodes */
48 /* rtable indexes of target relations for INSERT/UPDATE/DELETE */
49 List *resultRelations; /* integer list of RT indexes, or NIL */
51 Node *utilityStmt; /* non-null if this is DECLARE CURSOR */
53 IntoClause *intoClause; /* target for SELECT INTO / CREATE TABLE AS */
55 List *subplans; /* Plan trees for SubPlan expressions */
57 Bitmapset *rewindPlanIDs; /* indices of subplans that require REWIND */
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
69 List *returningLists; /* list of lists of TargetEntry, or NIL */
71 List *rowMarks; /* a list of RowMarkClause's */
73 List *relationOids; /* OIDs of relations the plan depends on */
75 int nParamExec; /* number of PARAM_EXEC Params used */
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))
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)
91 * We never actually instantiate any Plan nodes; this is just the common
92 * abstract superclass for all Plan-type nodes.
100 * estimated execution costs for plan (see costsize.c for more info)
102 Cost startup_cost; /* cost expended before fetching any tuples */
103 Cost total_cost; /* total cost (assuming all tuples fetched) */
106 * planner's estimate of result size of this plan step
108 double plan_rows; /* number of rows plan is expected to emit */
109 int plan_width; /* average row width in bytes */
112 * Common structural data for all Plan types.
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
122 * Information for management of parameter-change-driven rescanning
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.
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.
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.
143 #define innerPlan(node) (((Plan *)(node))->righttree)
144 #define outerPlan(node) (((Plan *)(node))->lefttree)
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).
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).
158 typedef struct Result
161 Node *resconstantqual;
166 * Generate the concatenation of the results of sub-plans.
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.
175 typedef struct Append
184 * Generate the intersection of the results of sub-plans.
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.
190 typedef struct BitmapAnd
198 * Generate the union of the results of sub-plans.
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.
204 typedef struct BitmapOr
218 Index scanrelid; /* relid is index into the range table */
222 * sequential scan node
225 typedef Scan SeqScan;
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.
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
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.
250 typedef struct IndexScan
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 */
262 * bitmap index scan node
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.
270 * The fields have the same meanings as for IndexScan, except we don't
271 * store a direction flag because direction is uninteresting.
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.
278 typedef struct BitmapIndexScan
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 */
289 * bitmap sequential scan node
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.
297 typedef struct BitmapHeapScan
300 List *bitmapqualorig; /* index quals, in standard expr form */
306 * tidquals is an implicitly OR'ed list of qual expressions of the form
307 * "CTID = pseudoconstant" or "CTID = ANY(pseudoconstant_array)".
310 typedef struct TidScan
313 List *tidquals; /* qual(s) involving CTID = something */
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
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.
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.
336 typedef struct SubqueryScan
340 List *subrtable; /* temporary workspace for planner */
347 typedef struct FunctionScan
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 */
360 typedef struct ValuesScan
363 List *values_lists; /* list of expression lists */
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)
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.
392 List *joinqual; /* JOIN quals (in addition to plan.qual) */
396 * nest loop join node
399 typedef struct NestLoop
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.
415 typedef struct MergeJoin
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 */
426 * hash join (probe) node
429 typedef struct HashJoin
436 * materialization node
439 typedef struct Material
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 */
459 * Used for queries with GROUP BY (but no aggregates) specified.
460 * The input must be presorted according to the grouping columns.
466 int numCols; /* number of grouping columns */
467 AttrNumber *grpColIdx; /* their indexes in the target list */
468 Oid *grpOperators; /* equality operators to compare with */
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.
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.)
485 typedef enum AggStrategy
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 */
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 */
506 typedef struct Unique
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 */
521 /* all other info is in the parent HashJoin node */
528 typedef enum SetOpCmd
531 SETOPCMD_INTERSECT_ALL,
539 SetOpCmd cmd; /* what to do */
540 int numCols; /* number of columns to check for
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 */
550 * Note: as of Postgres 8.2, the offset and count expressions are expected
551 * to yield int8, rather than int4 as before.
557 Node *limitOffset; /* OFFSET parameter, or NULL if none */
558 Node *limitCount; /* COUNT parameter, or NULL if none */
561 #endif /* PLANNODES_H */