]> granicus.if.org Git - postgresql/blob - src/include/nodes/relation.h
Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[postgresql] / src / include / nodes / relation.h
1 /*-------------------------------------------------------------------------
2  *
3  * relation.h
4  *        Definitions for internal planner 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: relation.h,v 1.53 2001/01/24 19:43:26 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef RELATION_H
15 #define RELATION_H
16
17 #include "access/sdir.h"
18 #include "nodes/parsenodes.h"
19
20 /*
21  * Relids
22  *              List of relation identifiers (indexes into the rangetable).
23  *
24  *              Note: these are lists of integers, not Nodes.
25  */
26
27 typedef List *Relids;
28
29 /*
30  * When looking for a "cheapest path", this enum specifies whether we want
31  * cheapest startup cost or cheapest total cost.
32  */
33 typedef enum CostSelector
34 {
35         STARTUP_COST, TOTAL_COST
36 } CostSelector;
37
38 /*----------
39  * RelOptInfo
40  *              Per-relation information for planning/optimization
41  *
42  *              For planning purposes, a "base rel" is either a plain relation (a
43  *              table) 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.
47  *
48  *              Note that there is only one joinrel for any given set of component
49  *              baserels, no matter what order we assemble them in; so an unordered
50  *              set is the right datatype to identify it with.
51  *
52  *              Parts of this data structure are specific to various scan and join
53  *              mechanisms.  It didn't seem worth creating new node types for them.
54  *
55  *              relids - List of base-relation identifiers; it is a base relation
56  *                              if there is just one, a join relation if more than one
57  *              rows - estimated number of tuples in the relation after restriction
58  *                         clauses have been applied (ie, output rows of a plan for it)
59  *              width - avg. number of bytes per tuple in the relation after the
60  *                              appropriate projections have been done (ie, output width)
61  *              targetlist - List of TargetEntry nodes for the attributes we need
62  *                                       to output from this relation
63  *              pathlist - List of Path nodes, one for each potentially useful
64  *                                 method of generating the relation
65  *              cheapest_startup_path - the pathlist member with lowest startup cost
66  *                                                              (regardless of its ordering)
67  *              cheapest_total_path - the pathlist member with lowest total cost
68  *                                                        (regardless of its ordering)
69  *              pruneable - flag to let the planner know whether it can prune the
70  *                                      pathlist of this RelOptInfo or not.
71  *
72  *       * If the relation is a base relation it will have these fields set:
73  *
74  *              issubquery - true if baserel is a subquery RTE rather than a table
75  *              indexed - true if the relation has secondary indices (always false
76  *                                if it's a subquery)
77  *              pages - number of disk pages in relation (zero if a subquery)
78  *              tuples - number of tuples in relation (not considering restrictions)
79  *              subplan - plan for subquery (NULL if it's a plain table)
80  *
81  *              Note: for a subquery, tuples and subplan are not set immediately
82  *              upon creation of the RelOptInfo object; they are filled in when
83  *              set_base_rel_pathlist processes the object.
84  *
85  *              Note: if a base relation is the root of an inheritance tree
86  *              (SELECT FROM foo*) it is still considered a base rel.  We will
87  *              generate a list of candidate Paths for accessing that table itself,
88  *              and also generate baserel RelOptInfo nodes for each child table,
89  *              with their own candidate Path lists.  Then, an AppendPath is built
90  *              from the cheapest Path for each of these tables, and set to be the
91  *              only available Path for the inheritance baserel.
92  *
93  *       * The presence of the remaining fields depends on the restrictions
94  *              and joins that the relation participates in:
95  *
96  *              baserestrictinfo - List of RestrictInfo nodes, containing info about
97  *                                      each qualification clause in which this relation
98  *                                      participates (only used for base rels)
99  *              baserestrictcost - Estimated cost of evaluating the baserestrictinfo
100  *                                      clauses at a single tuple (only used for base rels)
101  *              outerjoinset - If the rel appears within the nullable side of an outer
102  *                                      join, the list of all relids participating in the highest
103  *                                      such outer join; else NIL (only used for base rels)
104  *              joininfo  - List of JoinInfo nodes, containing info about each join
105  *                                      clause in which this relation participates
106  *              innerjoin - List of Path nodes that represent indices that may be used
107  *                                      as inner paths of nestloop joins. This field is non-null
108  *                                      only for base rels, since join rels have no indices.
109  *
110  * Note: Keeping a restrictinfo list in the RelOptInfo is useful only for
111  * base rels, because for a join rel the set of clauses that are treated as
112  * restrict clauses varies depending on which sub-relations we choose to join.
113  * (For example, in a 3-base-rel join, a clause relating rels 1 and 2 must be
114  * treated as a restrictclause if we join {1} and {2 3} to make {1 2 3}; but
115  * if we join {1 2} and {3} then that clause will be a restrictclause in {1 2}
116  * and should not be processed again at the level of {1 2 3}.)  Therefore,
117  * the restrictinfo list in the join case appears in individual JoinPaths
118  * (field joinrestrictinfo), not in the parent relation.  But it's OK for
119  * the RelOptInfo to store the joininfo lists, because those are the same
120  * for a given rel no matter how we form it.
121  *
122  * We store baserestrictcost in the RelOptInfo (for base relations) because
123  * we know we will need it at least once (to price the sequential scan)
124  * and may need it multiple times to price index scans.
125  *
126  * outerjoinset is used to ensure correct placement of WHERE clauses that
127  * apply to outer-joined relations; we must not apply such WHERE clauses
128  * until after the outer join is performed.
129  *----------
130  */
131
132 typedef struct RelOptInfo
133 {
134         NodeTag         type;
135
136         /* all relations included in this RelOptInfo */
137         Relids          relids;                 /* integer list of base relids (RT
138                                                                  * indexes) */
139
140         /* size estimates generated by planner */
141         double          rows;                   /* estimated number of result tuples */
142         int                     width;                  /* estimated avg width of result tuples */
143
144         /* materialization information */
145         List       *targetlist;
146         List       *pathlist;           /* Path structures */
147         struct Path *cheapest_startup_path;
148         struct Path *cheapest_total_path;
149         bool            pruneable;
150
151         /* information about a base rel (not set for join rels!) */
152         bool            issubquery;
153         bool            indexed;
154         long            pages;
155         double          tuples;
156         struct Plan *subplan;
157
158         /* used by various scans and joins: */
159         List       *baserestrictinfo;           /* RestrictInfo structures (if
160                                                                                  * base rel) */
161         Cost            baserestrictcost;               /* cost of evaluating the above */
162         Relids          outerjoinset;                   /* integer list of base relids */
163         List       *joininfo;           /* JoinInfo structures */
164         List       *innerjoin;          /* potential indexscans for nestloop joins */
165
166         /*
167          * innerjoin indexscans are not in the main pathlist because they are
168          * not usable except in specific join contexts; we have to test before
169          * seeing whether they can be used.
170          */
171 } RelOptInfo;
172
173 /*
174  * IndexOptInfo
175  *              Per-index information for planning/optimization
176  *
177  *              Prior to Postgres 7.0, RelOptInfo was used to describe both relations
178  *              and indexes, but that created confusion without actually doing anything
179  *              useful.  So now we have a separate IndexOptInfo struct for indexes.
180  *
181  *              indexoid - OID of the index relation itself
182  *              pages - number of disk pages in index
183  *              tuples - number of index tuples in index
184  *              classlist - List of PG_AMOPCLASS OIDs for the index
185  *              indexkeys - List of base-relation attribute numbers that are index keys
186  *              ordering - List of PG_OPERATOR OIDs which order the indexscan result
187  *              relam     - the OID of the pg_am of the index
188  *              amcostestimate - OID of the relam's cost estimator
189  *              indproc   - OID of the function if a functional index, else 0
190  *              indpred   - index predicate if a partial index, else NULL
191  *              lossy     - true if index is lossy (may return non-matching tuples)
192  *
193  *              NB. the last element of the arrays classlist, indexkeys and ordering
194  *                      is always 0.
195  */
196
197 typedef struct IndexOptInfo
198 {
199         NodeTag         type;
200
201         Oid                     indexoid;               /* OID of the index relation */
202
203         /* statistics from pg_class */
204         long            pages;
205         double          tuples;
206
207         /* index descriptor information */
208         Oid                *classlist;          /* classes of AM operators */
209         int                *indexkeys;          /* keys over which we're indexing */
210         Oid                *ordering;           /* OIDs of sort operators for each key */
211         Oid                     relam;                  /* OID of the access method (in pg_am) */
212
213         RegProcedure amcostestimate;/* OID of the access method's cost fcn */
214
215         Oid                     indproc;                /* if a functional index */
216         List       *indpred;            /* if a partial index */
217         bool            lossy;                  /* if a lossy index */
218 } IndexOptInfo;
219
220 /*
221  * PathKeys
222  *
223  *      The sort ordering of a path is represented by a list of sublists of
224  *      PathKeyItem nodes.      An empty list implies no known ordering.  Otherwise
225  *      the first sublist represents the primary sort key, the second the
226  *      first secondary sort key, etc.  Each sublist contains one or more
227  *      PathKeyItem nodes, each of which can be taken as the attribute that
228  *      appears at that sort position.  (See the top of optimizer/path/pathkeys.c
229  *      for more information.)
230  */
231
232 typedef struct PathKeyItem
233 {
234         NodeTag         type;
235
236         Node       *key;                        /* the item that is ordered */
237         Oid                     sortop;                 /* the ordering operator ('<' op) */
238
239         /*
240          * key typically points to a Var node, ie a relation attribute, but it
241          * can also point to a Func clause representing the value indexed by a
242          * functional index.  Someday we might allow arbitrary expressions as
243          * path keys, so don't assume more than you must.
244          */
245 } PathKeyItem;
246
247 /*
248  * Type "Path" is used as-is for sequential-scan paths.  For other
249  * path types it is the first component of a larger struct.
250  */
251
252 typedef struct Path
253 {
254         NodeTag         type;
255
256         RelOptInfo *parent;                     /* the relation this path can build */
257
258         /* estimated execution costs for path (see costsize.c for more info) */
259         Cost            startup_cost;   /* cost expended before fetching any
260                                                                  * tuples */
261         Cost            total_cost;             /* total cost (assuming all tuples
262                                                                  * fetched) */
263
264         NodeTag         pathtype;               /* tag identifying scan/join method */
265         /* XXX why is pathtype separate from the NodeTag? */
266
267         List       *pathkeys;           /* sort ordering of path's output */
268         /* pathkeys is a List of Lists of PathKeyItem nodes; see above */
269 } Path;
270
271 /*----------
272  * IndexPath represents an index scan.  Although an indexscan can only read
273  * a single relation, it can scan it more than once, potentially using a
274  * different index during each scan.  The result is the union (OR) of all the
275  * tuples matched during any scan.      (The executor is smart enough not to return
276  * the same tuple more than once, even if it is matched in multiple scans.)
277  *
278  * 'indexid' is a list of index relation OIDs, one per scan to be performed.
279  *
280  * 'indexqual' is a list of index qualifications, also one per scan.
281  * Each entry in 'indexqual' is a sublist of qualification expressions with
282  * implicit AND semantics across the sublist items.  Only expressions that
283  * are usable as indexquals (as determined by indxpath.c) may appear here.
284  * NOTE that the semantics of the top-level list in 'indexqual' is OR
285  * combination, while the sublists are implicitly AND combinations!
286  * Also note that indexquals lists do not contain RestrictInfo nodes,
287  * just bare clause expressions.
288  *
289  * 'indexscandir' is one of:
290  *              ForwardScanDirection: forward scan of an ordered index
291  *              BackwardScanDirection: backward scan of an ordered index
292  *              NoMovementScanDirection: scan of an unordered index, or don't care
293  * (The executor doesn't care whether it gets ForwardScanDirection or
294  * NoMovementScanDirection for an indexscan, but the planner wants to
295  * distinguish ordered from unordered indexes for building pathkeys.)
296  *
297  * 'joinrelids' is only used in IndexPaths that are constructed for use
298  * as the inner path of a nestloop join.  These paths have indexquals
299  * that refer to values of other rels, so those other rels must be
300  * included in the outer joinrel in order to make a usable join.
301  *
302  * 'alljoinquals' is also used only for inner paths of nestloop joins.
303  * This flag is TRUE iff all the indexquals came from non-pushed-down
304  * JOIN/ON conditions, which means the path is safe to use for an outer join.
305  *
306  * 'rows' is the estimated result tuple count for the indexscan.  This
307  * is the same as path.parent->rows for a simple indexscan, but it is
308  * different for a nestloop inner path, because the additional indexquals
309  * coming from join clauses make the scan more selective than the parent
310  * rel's restrict clauses alone would do.
311  *----------
312  */
313 typedef struct IndexPath
314 {
315         Path            path;
316         List       *indexid;
317         List       *indexqual;
318         ScanDirection indexscandir;
319         Relids          joinrelids;             /* other rels mentioned in indexqual */
320         bool            alljoinquals;   /* all indexquals derived from JOIN conds? */
321         double          rows;                   /* estimated number of result tuples */
322 } IndexPath;
323
324 /*
325  * TidPath represents a scan by TID
326  */
327 typedef struct TidPath
328 {
329         Path            path;
330         List       *tideval;
331         Relids          unjoined_relids;/* some rels not yet part of my Path */
332 } TidPath;
333
334 /*
335  * AppendPath represents an Append plan, ie, successive execution of
336  * several member plans.  Currently it is only used to handle expansion
337  * of inheritance trees.
338  */
339 typedef struct AppendPath
340 {
341         Path            path;
342         List       *subpaths;           /* list of component Paths */
343 } AppendPath;
344
345 /*
346  * All join-type paths share these fields.
347  */
348
349 typedef struct JoinPath
350 {
351         Path            path;
352
353         JoinType        jointype;
354
355         Path       *outerjoinpath;      /* path for the outer side of the join */
356         Path       *innerjoinpath;      /* path for the inner side of the join */
357
358         List       *joinrestrictinfo;           /* RestrictInfos to apply to join */
359
360         /*
361          * See the notes for RelOptInfo to understand why joinrestrictinfo is
362          * needed in JoinPath, and can't be merged into the parent RelOptInfo.
363          */
364 } JoinPath;
365
366 /*
367  * A nested-loop path needs no special fields.
368  */
369
370 typedef JoinPath NestPath;
371
372 /*
373  * A mergejoin path has these fields.
374  *
375  * path_mergeclauses lists the clauses (in the form of RestrictInfos)
376  * that will be used in the merge.      (Before 7.0, this was a list of bare
377  * clause expressions, but we can save on list memory and cost_qual_eval
378  * work by leaving it in the form of a RestrictInfo list.)
379  *
380  * Note that the mergeclauses are a subset of the parent relation's
381  * restriction-clause list.  Any join clauses that are not mergejoinable
382  * appear only in the parent's restrict list, and must be checked by a
383  * qpqual at execution time.
384  *
385  * outersortkeys (resp. innersortkeys) is NIL if the outer path
386  * (resp. inner path) is already ordered appropriately for the
387  * mergejoin.  If it is not NIL then it is a PathKeys list describing
388  * the ordering that must be created by an explicit sort step.
389  */
390
391 typedef struct MergePath
392 {
393         JoinPath        jpath;
394         List       *path_mergeclauses;          /* join clauses to be used for
395                                                                                  * merge */
396         List       *outersortkeys;      /* keys for explicit sort, if any */
397         List       *innersortkeys;      /* keys for explicit sort, if any */
398 } MergePath;
399
400 /*
401  * A hashjoin path has these fields.
402  *
403  * The remarks above for mergeclauses apply for hashclauses as well.
404  * (But note that path_hashclauses will always be a one-element list,
405  * since we only hash on one hashable clause.)
406  *
407  * Hashjoin does not care what order its inputs appear in, so we have
408  * no need for sortkeys.
409  */
410
411 typedef struct HashPath
412 {
413         JoinPath        jpath;
414         List       *path_hashclauses;           /* join clauses used for hashing */
415 } HashPath;
416
417 /*
418  * Restriction clause info.
419  *
420  * We create one of these for each AND sub-clause of a restriction condition
421  * (WHERE or JOIN/ON clause).  Since the restriction clauses are logically
422  * ANDed, we can use any one of them or any subset of them to filter out
423  * tuples, without having to evaluate the rest.  The RestrictInfo node itself
424  * stores data used by the optimizer while choosing the best query plan.
425  *
426  * If a restriction clause references a single base relation, it will appear
427  * in the baserestrictinfo list of the RelOptInfo for that base rel.
428  *
429  * If a restriction clause references more than one base rel, it will
430  * appear in the JoinInfo lists of every RelOptInfo that describes a strict
431  * subset of the base rels mentioned in the clause.  The JoinInfo lists are
432  * used to drive join tree building by selecting plausible join candidates.
433  * The clause cannot actually be applied until we have built a join rel
434  * containing all the base rels it references, however.
435  *
436  * When we construct a join rel that includes all the base rels referenced
437  * in a multi-relation restriction clause, we place that clause into the
438  * joinrestrictinfo lists of paths for the join rel, if neither left nor
439  * right sub-path includes all base rels referenced in the clause.  The clause
440  * will be applied at that join level, and will not propagate any further up
441  * the join tree.  (Note: the "predicate migration" code was once intended to
442  * push restriction clauses up and down the plan tree based on evaluation
443  * costs, but it's dead code and is unlikely to be resurrected in the
444  * foreseeable future.)
445  *
446  * Note that in the presence of more than two rels, a multi-rel restriction
447  * might reach different heights in the join tree depending on the join
448  * sequence we use.  So, these clauses cannot be associated directly with
449  * the join RelOptInfo, but must be kept track of on a per-join-path basis.
450  *
451  * When dealing with outer joins we have to be very careful about pushing qual
452  * clauses up and down the tree.  An outer join's own JOIN/ON conditions must
453  * be evaluated exactly at that join node, and any quals appearing in WHERE or
454  * in a JOIN above the outer join cannot be pushed down below the outer join.
455  * Otherwise the outer join will produce wrong results because it will see the
456  * wrong sets of input rows.  All quals are stored as RestrictInfo nodes
457  * during planning, but there's a flag to indicate whether a qual has been
458  * pushed down to a lower level than its original syntactic placement in the
459  * join tree would suggest.  If an outer join prevents us from pushing a qual
460  * down to its "natural" semantic level (the level associated with just the
461  * base rels used in the qual) then the qual will appear in JoinInfo lists
462  * that reference more than just the base rels it actually uses.  By
463  * pretending that the qual references all the rels appearing in the outer
464  * join, we prevent it from being evaluated below the outer join's joinrel.
465  * When we do form the outer join's joinrel, we still need to distinguish
466  * those quals that are actually in that join's JOIN/ON condition from those
467  * that appeared higher in the tree and were pushed down to the join rel
468  * because they used no other rels.  That's what the ispusheddown flag is for;
469  * it tells us that a qual came from a point above the join of the specific
470  * set of base rels that it uses (or that the JoinInfo structures claim it
471  * uses).  A clause that originally came from WHERE will *always* have its
472  * ispusheddown flag set; a clause that came from an INNER JOIN condition,
473  * but doesn't use all the rels being joined, will also have ispusheddown set
474  * because it will get attached to some lower joinrel.
475  *
476  * In general, the referenced clause might be arbitrarily complex.      The
477  * kinds of clauses we can handle as indexscan quals, mergejoin clauses,
478  * or hashjoin clauses are fairly limited --- the code for each kind of
479  * path is responsible for identifying the restrict clauses it can use
480  * and ignoring the rest.  Clauses not implemented by an indexscan,
481  * mergejoin, or hashjoin will be placed in the plan qual or joinqual field
482  * of the final Plan node, where they will be enforced by general-purpose
483  * qual-expression-evaluation code.  (But we are still entitled to count
484  * their selectivity when estimating the result tuple count, if we
485  * can guess what it is...)
486  */
487
488 typedef struct RestrictInfo
489 {
490         NodeTag         type;
491
492         Expr       *clause;                     /* the represented clause of WHERE or JOIN */
493
494         Cost            eval_cost;              /* eval cost of clause; -1 if not yet set */
495
496         bool            ispusheddown;   /* TRUE if clause was pushed down in level */
497
498         /* only used if clause is an OR clause: */
499         List       *subclauseindices;           /* indexes matching subclauses */
500         /* subclauseindices is a List of Lists of IndexOptInfos */
501
502         /* valid if clause is mergejoinable, else InvalidOid: */
503         Oid                     mergejoinoperator;              /* copy of clause operator */
504         Oid                     left_sortop;    /* leftside sortop needed for mergejoin */
505         Oid                     right_sortop;   /* rightside sortop needed for mergejoin */
506
507         /* cache space for mergeclause processing; NIL if not yet set */
508         List       *left_pathkey;       /* canonical pathkey for left side */
509         List       *right_pathkey;      /* canonical pathkey for right side */
510
511         /* valid if clause is hashjoinable, else InvalidOid: */
512         Oid                     hashjoinoperator;               /* copy of clause operator */
513
514         /* cache space for hashclause processing; -1 if not yet set */
515         Selectivity     left_dispersion;        /* dispersion of left side */
516         Selectivity     right_dispersion;       /* dispersion of right side */
517 } RestrictInfo;
518
519 /*
520  * Join clause info.
521  *
522  * We make a list of these for each RelOptInfo, containing info about
523  * all the join clauses this RelOptInfo participates in.  (For this
524  * purpose, a "join clause" is a WHERE clause that mentions both vars
525  * belonging to this relation and vars belonging to relations not yet
526  * joined to it.)  We group these clauses according to the set of
527  * other base relations (unjoined relations) mentioned in them.
528  * There is one JoinInfo for each distinct set of unjoined_relids,
529  * and its jinfo_restrictinfo lists the clause(s) that use that set
530  * of other relations.
531  */
532
533 typedef struct JoinInfo
534 {
535         NodeTag         type;
536         Relids          unjoined_relids; /* some rels not yet part of my RelOptInfo */
537         List       *jinfo_restrictinfo;         /* relevant RestrictInfos */
538 } JoinInfo;
539
540 /*
541  *      Stream:
542  *      A stream represents a root-to-leaf path in a plan tree (i.e. a tree of
543  *      JoinPaths and Paths).  The stream includes pointers to all Path nodes,
544  *      as well as to any clauses that reside above Path nodes. This structure
545  *      is used to make Path nodes and clauses look similar, so that Predicate
546  *      Migration can run.
547  *
548  *      XXX currently, Predicate Migration is dead code, and so is this node type.
549  *      Probably should remove support for it.
550  *
551  *      pathptr -- pointer to the current path node
552  *      cinfo -- if NULL, this stream node referes to the path node.
553  *                        Otherwise this is a pointer to the current clause.
554  *      clausetype -- whether cinfo is in loc_restrictinfo or pathinfo in the
555  *                        path node (XXX this is now used only by dead code, which is
556  *                        good because the distinction no longer exists...)
557  *      upstream -- linked list pointer upwards
558  *      downstream -- ditto, downwards
559  *      groupup -- whether or not this node is in a group with the node upstream
560  *      groupcost -- total cost of the group that node is in
561  *      groupsel -- total selectivity of the group that node is in
562  */
563 typedef struct Stream *StreamPtr;
564
565 typedef struct Stream
566 {
567         NodeTag         type;
568         Path       *pathptr;
569         RestrictInfo *cinfo;
570         int                *clausetype;
571         StreamPtr       upstream;
572         StreamPtr       downstream;
573         bool            groupup;
574         Cost            groupcost;
575         Selectivity groupsel;
576 } Stream;
577
578 #endif   /* RELATION_H */