1 /*-------------------------------------------------------------------------
4 * Definitions for planner's internal data structures.
7 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: relation.h,v 1.73 2002/12/05 15:50:39 tgl Exp $
12 *-------------------------------------------------------------------------
17 #include "access/sdir.h"
18 #include "nodes/parsenodes.h"
22 * List of relation identifiers (indexes into the rangetable).
24 * Note: these are lists of integers, not Nodes.
30 * When looking for a "cheapest path", this enum specifies whether we want
31 * cheapest startup cost or cheapest total cost.
33 typedef enum CostSelector
35 STARTUP_COST, TOTAL_COST
40 * Per-relation information for planning/optimization
42 * For planning purposes, a "base rel" is either a plain relation (a table)
43 * or the output of a sub-SELECT that appears in the range table.
44 * In either case it is uniquely identified by an RT index. A "joinrel"
45 * is the joining of two or more base rels. A joinrel is identified by
46 * the set of RT indexes for its component baserels. We create RelOptInfo
47 * nodes for each baserel and joinrel, and store them in the Query's
48 * base_rel_list and join_rel_list respectively.
50 * Note that there is only one joinrel for any given set of component
51 * baserels, no matter what order we assemble them in; so an unordered
52 * set is the right datatype to identify it with.
54 * We also have "other rels", which are like base rels in that they refer to
55 * single RT indexes; but they are not part of the join tree, and are stored
56 * in other_rel_list not base_rel_list. An otherrel is created for each
57 * join RTE as an aid in processing Vars that refer to the join's outputs,
58 * but it serves no other purpose in planning. It is important not to
59 * confuse this otherrel with the joinrel that represents the matching set
62 * A second category of otherrels are those made for child relations of an
63 * inheritance scan (SELECT FROM foo*). The parent table's RTE and
64 * corresponding baserel represent the whole result of the inheritance scan.
65 * The planner creates separate RTEs and associated RelOptInfos for each child
66 * table (including the parent table, in its capacity as a member of the
67 * inheritance set). These RelOptInfos are physically identical to baserels,
68 * but are otherrels because they are not in the main join tree. These added
69 * RTEs and otherrels are used to plan the scans of the individual tables in
70 * the inheritance set; then the parent baserel is given an Append plan
71 * comprising the best plans for the individual child tables.
73 * Parts of this data structure are specific to various scan and join
74 * mechanisms. It didn't seem worth creating new node types for them.
76 * relids - List of base-relation identifiers; it is a base relation
77 * if there is just one, a join relation if more than one
78 * rows - estimated number of tuples in the relation after restriction
79 * clauses have been applied (ie, output rows of a plan for it)
80 * width - avg. number of bytes per tuple in the relation after the
81 * appropriate projections have been done (ie, output width)
82 * targetlist - List of TargetEntry nodes for the attributes we need
83 * to output from this relation
84 * pathlist - List of Path nodes, one for each potentially useful
85 * method of generating the relation
86 * cheapest_startup_path - the pathlist member with lowest startup cost
87 * (regardless of its ordering)
88 * cheapest_total_path - the pathlist member with lowest total cost
89 * (regardless of its ordering)
90 * pruneable - flag to let the planner know whether it can prune the
91 * pathlist of this RelOptInfo or not.
93 * If the relation is a base relation it will have these fields set:
95 * rtekind - distinguishes plain relation, subquery, or function RTE
96 * indexlist - list of IndexOptInfo nodes for relation's indexes
97 * (always NIL if it's not a table)
98 * pages - number of disk pages in relation (zero if not a table)
99 * tuples - number of tuples in relation (not considering restrictions)
100 * subplan - plan for subquery (NULL if it's not a subquery)
102 * Note: for a subquery, tuples and subplan are not set immediately
103 * upon creation of the RelOptInfo object; they are filled in when
104 * set_base_rel_pathlist processes the object.
106 * For otherrels that are inheritance children, these fields are filled
107 * in just as for a baserel. In otherrels for join RTEs, these fields
108 * are empty --- the only useful field of a join otherrel is its
111 * If the relation is a join relation it will have these fields set:
113 * joinrti - RT index of corresponding JOIN RTE, if any; 0 if none
114 * joinrteids - List of RT indexes of JOIN RTEs included in this join
115 * (including joinrti)
117 * The presence of the remaining fields depends on the restrictions
118 * and joins that the relation participates in:
120 * baserestrictinfo - List of RestrictInfo nodes, containing info about
121 * each qualification clause in which this relation
122 * participates (only used for base rels)
123 * baserestrictcost - Estimated cost of evaluating the baserestrictinfo
124 * clauses at a single tuple (only used for base rels)
125 * outerjoinset - For a base rel: if the rel appears within the nullable
126 * side of an outer join, the list of all relids
127 * participating in the highest such outer join; else NIL.
128 * For a join otherrel: the list of all baserel relids
129 * syntactically within the join. Otherwise, unused.
130 * joininfo - List of JoinInfo nodes, containing info about each join
131 * clause in which this relation participates
132 * index_outer_relids - only used for base rels; list of outer relids
133 * that participate in indexable joinclauses for this rel
134 * index_inner_paths - only used for base rels; list of InnerIndexscanInfo
135 * nodes showing best indexpaths for various subsets of
136 * index_outer_relids.
138 * Note: Keeping a restrictinfo list in the RelOptInfo is useful only for
139 * base rels, because for a join rel the set of clauses that are treated as
140 * restrict clauses varies depending on which sub-relations we choose to join.
141 * (For example, in a 3-base-rel join, a clause relating rels 1 and 2 must be
142 * treated as a restrictclause if we join {1} and {2 3} to make {1 2 3}; but
143 * if we join {1 2} and {3} then that clause will be a restrictclause in {1 2}
144 * and should not be processed again at the level of {1 2 3}.) Therefore,
145 * the restrictinfo list in the join case appears in individual JoinPaths
146 * (field joinrestrictinfo), not in the parent relation. But it's OK for
147 * the RelOptInfo to store the joininfo lists, because those are the same
148 * for a given rel no matter how we form it.
150 * We store baserestrictcost in the RelOptInfo (for base relations) because
151 * we know we will need it at least once (to price the sequential scan)
152 * and may need it multiple times to price index scans.
154 * outerjoinset is used to ensure correct placement of WHERE clauses that
155 * apply to outer-joined relations; we must not apply such WHERE clauses
156 * until after the outer join is performed.
159 typedef enum RelOptKind
163 RELOPT_OTHER_JOIN_REL,
164 RELOPT_OTHER_CHILD_REL
167 typedef struct RelOptInfo
171 RelOptKind reloptkind;
173 /* all relations included in this RelOptInfo */
174 Relids relids; /* integer list of base relids (rangetable
177 /* size estimates generated by planner */
178 double rows; /* estimated number of result tuples */
179 int width; /* estimated avg width of result tuples */
181 /* materialization information */
183 List *pathlist; /* Path structures */
184 struct Path *cheapest_startup_path;
185 struct Path *cheapest_total_path;
188 /* information about a base rel (not set for join rels!) */
189 RTEKind rtekind; /* RELATION, SUBQUERY, or FUNCTION */
193 struct Plan *subplan; /* if subquery */
195 /* information about a join rel (not set for base rels!) */
199 /* used by various scans and joins: */
200 List *baserestrictinfo; /* RestrictInfo structures (if
202 Cost baserestrictcost; /* cost of evaluating the above */
203 Relids outerjoinset; /* integer list of base relids */
204 List *joininfo; /* JoinInfo structures */
206 /* cached info about inner indexscan paths for relation: */
207 Relids index_outer_relids; /* other relids in indexable join
209 List *index_inner_paths; /* InnerIndexscanInfo nodes */
211 * Inner indexscans are not in the main pathlist because they are
212 * not usable except in specific join contexts. We use the
213 * index_inner_paths list just to avoid recomputing the best inner
214 * indexscan repeatedly for similar outer relations. See comments
215 * for InnerIndexscanInfo.
221 * Per-index information for planning/optimization
223 * Prior to Postgres 7.0, RelOptInfo was used to describe both relations
224 * and indexes, but that created confusion without actually doing anything
225 * useful. So now we have a separate IndexOptInfo struct for indexes.
227 * ncolumns and nkeys are the same except for a functional index,
228 * wherein ncolumns is 1 (the single function output) while nkeys
229 * is the number of table columns passed to the function. classlist[]
230 * and ordering[] have ncolumns entries, while indexkeys[] has nkeys
233 * Note: for historical reasons, the arrays classlist, indexkeys and
234 * ordering have an extra entry that is always zero. Some code scans
235 * until it sees a zero rather than looking at ncolumns or nkeys.
238 typedef struct IndexOptInfo
242 Oid indexoid; /* OID of the index relation */
244 /* statistics from pg_class */
245 long pages; /* number of disk pages in index */
246 double tuples; /* number of index tuples in index */
248 /* index descriptor information */
249 int ncolumns; /* number of columns in index */
250 int nkeys; /* number of keys used by index */
251 Oid *classlist; /* OIDs of operator classes for columns */
252 int *indexkeys; /* column numbers of index's keys */
253 Oid *ordering; /* OIDs of sort operators for each column */
254 Oid relam; /* OID of the access method (in pg_am) */
256 RegProcedure amcostestimate; /* OID of the access method's cost fcn */
258 Oid indproc; /* OID of func if functional index, else 0 */
259 List *indpred; /* predicate if a partial index, else NIL */
260 bool unique; /* true if a unique index */
262 /* cached info about inner indexscan paths for index */
263 Relids outer_relids; /* other relids in usable join clauses */
264 List *inner_paths; /* List of InnerIndexscanInfo nodes */
269 * A Var is considered to belong to a relation if it's either from one
270 * of the actual base rels making up the relation, or it's a join alias
271 * var that is included in the relation.
273 #define VARISRELMEMBER(varno,rel) (intMember((varno), (rel)->relids) || \
274 intMember((varno), (rel)->joinrteids))
280 * The sort ordering of a path is represented by a list of sublists of
281 * PathKeyItem nodes. An empty list implies no known ordering. Otherwise
282 * the first sublist represents the primary sort key, the second the
283 * first secondary sort key, etc. Each sublist contains one or more
284 * PathKeyItem nodes, each of which can be taken as the attribute that
285 * appears at that sort position. (See the top of optimizer/path/pathkeys.c
286 * for more information.)
289 typedef struct PathKeyItem
293 Node *key; /* the item that is ordered */
294 Oid sortop; /* the ordering operator ('<' op) */
297 * key typically points to a Var node, ie a relation attribute, but it
298 * can also point to a Func clause representing the value indexed by a
299 * functional index. Someday we might allow arbitrary expressions as
300 * path keys, so don't assume more than you must.
305 * Type "Path" is used as-is for sequential-scan paths. For other
306 * path types it is the first component of a larger struct.
308 * Note: "pathtype" is the NodeTag of the Plan node we could build from this
309 * Path. It is partially redundant with the Path's NodeTag, but allows us
310 * to use the same Path type for multiple Plan types where there is no need
311 * to distinguish the Plan type during path processing.
318 RelOptInfo *parent; /* the relation this path can build */
320 /* estimated execution costs for path (see costsize.c for more info) */
321 Cost startup_cost; /* cost expended before fetching any
323 Cost total_cost; /* total cost (assuming all tuples
326 NodeTag pathtype; /* tag identifying scan/join method */
328 List *pathkeys; /* sort ordering of path's output */
329 /* pathkeys is a List of Lists of PathKeyItem nodes; see above */
333 * IndexPath represents an index scan. Although an indexscan can only read
334 * a single relation, it can scan it more than once, potentially using a
335 * different index during each scan. The result is the union (OR) of all the
336 * tuples matched during any scan. (The executor is smart enough not to return
337 * the same tuple more than once, even if it is matched in multiple scans.)
339 * 'indexinfo' is a list of IndexOptInfo nodes, one per scan to be performed.
341 * 'indexqual' is a list of index qualifications, also one per scan.
342 * Each entry in 'indexqual' is a sublist of qualification expressions with
343 * implicit AND semantics across the sublist items. Only expressions that
344 * are usable as indexquals (as determined by indxpath.c) may appear here.
345 * NOTE that the semantics of the top-level list in 'indexqual' is OR
346 * combination, while the sublists are implicitly AND combinations!
347 * Also note that indexquals lists do not contain RestrictInfo nodes,
348 * just bare clause expressions.
350 * 'indexscandir' is one of:
351 * ForwardScanDirection: forward scan of an ordered index
352 * BackwardScanDirection: backward scan of an ordered index
353 * NoMovementScanDirection: scan of an unordered index, or don't care
354 * (The executor doesn't care whether it gets ForwardScanDirection or
355 * NoMovementScanDirection for an indexscan, but the planner wants to
356 * distinguish ordered from unordered indexes for building pathkeys.)
358 * 'rows' is the estimated result tuple count for the indexscan. This
359 * is the same as path.parent->rows for a simple indexscan, but it is
360 * different for a nestloop inner scan, because the additional indexquals
361 * coming from join clauses make the scan more selective than the parent
362 * rel's restrict clauses alone would do.
365 typedef struct IndexPath
370 ScanDirection indexscandir;
371 double rows; /* estimated number of result tuples */
375 * TidPath represents a scan by TID
377 typedef struct TidPath
380 List *tideval; /* qual(s) involving CTID = something */
384 * AppendPath represents an Append plan, ie, successive execution of
385 * several member plans. Currently it is only used to handle expansion
386 * of inheritance trees.
388 typedef struct AppendPath
391 List *subpaths; /* list of component Paths */
395 * ResultPath represents use of a Result plan node, either to compute a
396 * variable-free targetlist or to gate execution of a subplan with a
397 * one-time (variable-free) qual condition. Note that in the former case
398 * path.parent will be NULL; in the latter case it is copied from the subpath.
400 typedef struct ResultPath
408 * MaterialPath represents use of a Material plan node, i.e., caching of
409 * the output of its subpath. This is used when the subpath is expensive
410 * and needs to be scanned repeatedly, or when we need mark/restore ability
411 * and the subpath doesn't have it.
413 typedef struct MaterialPath
420 * All join-type paths share these fields.
423 typedef struct JoinPath
429 Path *outerjoinpath; /* path for the outer side of the join */
430 Path *innerjoinpath; /* path for the inner side of the join */
432 List *joinrestrictinfo; /* RestrictInfos to apply to join */
435 * See the notes for RelOptInfo to understand why joinrestrictinfo is
436 * needed in JoinPath, and can't be merged into the parent RelOptInfo.
441 * A nested-loop path needs no special fields.
444 typedef JoinPath NestPath;
447 * A mergejoin path has these fields.
449 * path_mergeclauses lists the clauses (in the form of RestrictInfos)
450 * that will be used in the merge. (Before 7.0, this was a list of bare
451 * clause expressions, but we can save on list memory and cost_qual_eval
452 * work by leaving it in the form of a RestrictInfo list.)
454 * Note that the mergeclauses are a subset of the parent relation's
455 * restriction-clause list. Any join clauses that are not mergejoinable
456 * appear only in the parent's restrict list, and must be checked by a
457 * qpqual at execution time.
459 * outersortkeys (resp. innersortkeys) is NIL if the outer path
460 * (resp. inner path) is already ordered appropriately for the
461 * mergejoin. If it is not NIL then it is a PathKeys list describing
462 * the ordering that must be created by an explicit sort step.
465 typedef struct MergePath
468 List *path_mergeclauses; /* join clauses to be used for
470 List *outersortkeys; /* keys for explicit sort, if any */
471 List *innersortkeys; /* keys for explicit sort, if any */
475 * A hashjoin path has these fields.
477 * The remarks above for mergeclauses apply for hashclauses as well.
479 * Hashjoin does not care what order its inputs appear in, so we have
480 * no need for sortkeys.
483 typedef struct HashPath
486 List *path_hashclauses; /* join clauses used for hashing */
490 * Restriction clause info.
492 * We create one of these for each AND sub-clause of a restriction condition
493 * (WHERE or JOIN/ON clause). Since the restriction clauses are logically
494 * ANDed, we can use any one of them or any subset of them to filter out
495 * tuples, without having to evaluate the rest. The RestrictInfo node itself
496 * stores data used by the optimizer while choosing the best query plan.
498 * If a restriction clause references a single base relation, it will appear
499 * in the baserestrictinfo list of the RelOptInfo for that base rel.
501 * If a restriction clause references more than one base rel, it will
502 * appear in the JoinInfo lists of every RelOptInfo that describes a strict
503 * subset of the base rels mentioned in the clause. The JoinInfo lists are
504 * used to drive join tree building by selecting plausible join candidates.
505 * The clause cannot actually be applied until we have built a join rel
506 * containing all the base rels it references, however.
508 * When we construct a join rel that includes all the base rels referenced
509 * in a multi-relation restriction clause, we place that clause into the
510 * joinrestrictinfo lists of paths for the join rel, if neither left nor
511 * right sub-path includes all base rels referenced in the clause. The clause
512 * will be applied at that join level, and will not propagate any further up
513 * the join tree. (Note: the "predicate migration" code was once intended to
514 * push restriction clauses up and down the plan tree based on evaluation
515 * costs, but it's dead code and is unlikely to be resurrected in the
516 * foreseeable future.)
518 * Note that in the presence of more than two rels, a multi-rel restriction
519 * might reach different heights in the join tree depending on the join
520 * sequence we use. So, these clauses cannot be associated directly with
521 * the join RelOptInfo, but must be kept track of on a per-join-path basis.
523 * When dealing with outer joins we have to be very careful about pushing qual
524 * clauses up and down the tree. An outer join's own JOIN/ON conditions must
525 * be evaluated exactly at that join node, and any quals appearing in WHERE or
526 * in a JOIN above the outer join cannot be pushed down below the outer join.
527 * Otherwise the outer join will produce wrong results because it will see the
528 * wrong sets of input rows. All quals are stored as RestrictInfo nodes
529 * during planning, but there's a flag to indicate whether a qual has been
530 * pushed down to a lower level than its original syntactic placement in the
531 * join tree would suggest. If an outer join prevents us from pushing a qual
532 * down to its "natural" semantic level (the level associated with just the
533 * base rels used in the qual) then the qual will appear in JoinInfo lists
534 * that reference more than just the base rels it actually uses. By
535 * pretending that the qual references all the rels appearing in the outer
536 * join, we prevent it from being evaluated below the outer join's joinrel.
537 * When we do form the outer join's joinrel, we still need to distinguish
538 * those quals that are actually in that join's JOIN/ON condition from those
539 * that appeared higher in the tree and were pushed down to the join rel
540 * because they used no other rels. That's what the ispusheddown flag is for;
541 * it tells us that a qual came from a point above the join of the specific
542 * set of base rels that it uses (or that the JoinInfo structures claim it
543 * uses). A clause that originally came from WHERE will *always* have its
544 * ispusheddown flag set; a clause that came from an INNER JOIN condition,
545 * but doesn't use all the rels being joined, will also have ispusheddown set
546 * because it will get attached to some lower joinrel.
548 * In general, the referenced clause might be arbitrarily complex. The
549 * kinds of clauses we can handle as indexscan quals, mergejoin clauses,
550 * or hashjoin clauses are fairly limited --- the code for each kind of
551 * path is responsible for identifying the restrict clauses it can use
552 * and ignoring the rest. Clauses not implemented by an indexscan,
553 * mergejoin, or hashjoin will be placed in the plan qual or joinqual field
554 * of the finished Plan node, where they will be enforced by general-purpose
555 * qual-expression-evaluation code. (But we are still entitled to count
556 * their selectivity when estimating the result tuple count, if we
557 * can guess what it is...)
560 typedef struct RestrictInfo
564 Expr *clause; /* the represented clause of WHERE or JOIN */
566 bool ispusheddown; /* TRUE if clause was pushed down in level */
568 /* only used if clause is an OR clause: */
569 List *subclauseindices; /* indexes matching subclauses */
570 /* subclauseindices is a List of Lists of IndexOptInfos */
572 /* cache space for costs (currently only used for join clauses) */
573 Cost eval_cost; /* eval cost of clause; -1 if not yet set */
574 Selectivity this_selec; /* selectivity; -1 if not yet set */
576 /* valid if clause is mergejoinable, else InvalidOid: */
577 Oid mergejoinoperator; /* copy of clause operator */
578 Oid left_sortop; /* leftside sortop needed for mergejoin */
579 Oid right_sortop; /* rightside sortop needed for mergejoin */
581 /* cache space for mergeclause processing; NIL if not yet set */
582 List *left_pathkey; /* canonical pathkey for left side */
583 List *right_pathkey; /* canonical pathkey for right side */
585 /* cache space for mergeclause processing; -1 if not yet set */
586 Selectivity left_mergescansel; /* fraction of left side to scan */
587 Selectivity right_mergescansel; /* fraction of right side to scan */
589 /* valid if clause is hashjoinable, else InvalidOid: */
590 Oid hashjoinoperator; /* copy of clause operator */
592 /* cache space for hashclause processing; -1 if not yet set */
593 Selectivity left_bucketsize; /* avg bucketsize of left side */
594 Selectivity right_bucketsize; /* avg bucketsize of right side */
600 * We make a list of these for each RelOptInfo, containing info about
601 * all the join clauses this RelOptInfo participates in. (For this
602 * purpose, a "join clause" is a WHERE clause that mentions both vars
603 * belonging to this relation and vars belonging to relations not yet
604 * joined to it.) We group these clauses according to the set of
605 * other base relations (unjoined relations) mentioned in them.
606 * There is one JoinInfo for each distinct set of unjoined_relids,
607 * and its jinfo_restrictinfo lists the clause(s) that use that set
608 * of other relations.
611 typedef struct JoinInfo
614 Relids unjoined_relids; /* some rels not yet part of my RelOptInfo */
615 List *jinfo_restrictinfo; /* relevant RestrictInfos */
619 * Inner indexscan info.
621 * An inner indexscan is one that uses one or more joinclauses as index
622 * conditions (perhaps in addition to plain restriction clauses). So it
623 * can only be used as the inner path of a nestloop join where the outer
624 * relation includes all other relids appearing in those joinclauses.
625 * The set of usable joinclauses, and thus the best inner indexscan,
626 * thus varies depending on which outer relation we consider; so we have
627 * to recompute the best such path for every join. To avoid lots of
628 * redundant computation, we cache the results of such searches. For
629 * each index we compute the set of possible otherrelids (all relids
630 * appearing in joinquals that could become indexquals for this index).
631 * Two outer relations whose relids have the same intersection with this
632 * set will have the same set of available joinclauses and thus the same
633 * best inner indexscan for that index. Similarly, for each base relation,
634 * we form the union of the per-index otherrelids sets. Two outer relations
635 * with the same intersection with that set will have the same best overall
636 * inner indexscan for the base relation. We use lists of InnerIndexscanInfo
637 * nodes to cache the results of these searches at both the index and
640 * The search key also includes a bool showing whether the join being
641 * considered is an outer join. Since we constrain the join order for
642 * outer joins, I believe that this bool can only have one possible value
643 * for any particular base relation; but store it anyway to avoid confusion.
646 typedef struct InnerIndexscanInfo
649 /* The lookup key: */
650 Relids other_relids; /* a set of relevant other relids */
651 bool isouterjoin; /* true if join is outer */
652 /* Best path for this lookup key: */
653 Path *best_innerpath; /* best inner indexscan, or NULL if none */
654 } InnerIndexscanInfo;
656 #endif /* RELATION_H */