]> granicus.if.org Git - postgresql/blob - src/include/nodes/relation.h
Adjust nestloop-with-inner-indexscan plan generation so that we catch
[postgresql] / src / include / nodes / relation.h
1 /*-------------------------------------------------------------------------
2  *
3  * relation.h
4  *        Definitions for planner's internal data structures.
5  *
6  *
7  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: relation.h,v 1.81 2003/06/15 22:51:45 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef RELATION_H
15 #define RELATION_H
16
17 #include "access/sdir.h"
18 #include "nodes/bitmapset.h"
19 #include "nodes/parsenodes.h"
20
21
22 /*
23  * Relids
24  *              Set of relation identifiers (indexes into the rangetable).
25  */
26
27 typedef Bitmapset *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  * The cost estimate produced by cost_qual_eval() includes both a one-time
40  * (startup) cost, and a per-tuple cost.
41  */
42 typedef struct QualCost
43 {
44         Cost            startup;                /* one-time cost */
45         Cost            per_tuple;              /* per-evaluation cost */
46 } QualCost;
47
48 /*----------
49  * RelOptInfo
50  *              Per-relation information for planning/optimization
51  *
52  * For planning purposes, a "base rel" is either a plain relation (a table)
53  * or the output of a sub-SELECT or function that appears in the range table.
54  * In either case it is uniquely identified by an RT index.  A "joinrel"
55  * is the joining of two or more base rels.  A joinrel is identified by
56  * the set of RT indexes for its component baserels.  We create RelOptInfo
57  * nodes for each baserel and joinrel, and store them in the Query's
58  * base_rel_list and join_rel_list respectively.
59  *
60  * Note that there is only one joinrel for any given set of component
61  * baserels, no matter what order we assemble them in; so an unordered
62  * set is the right datatype to identify it with.
63  *
64  * We also have "other rels", which are like base rels in that they refer to
65  * single RT indexes; but they are not part of the join tree, and are stored
66  * in other_rel_list not base_rel_list.
67  *
68  * Currently the only kind of otherrels are those made for child relations
69  * of an inheritance scan (SELECT FROM foo*).  The parent table's RTE and
70  * corresponding baserel represent the whole result of the inheritance scan.
71  * The planner creates separate RTEs and associated RelOptInfos for each child
72  * table (including the parent table, in its capacity as a member of the
73  * inheritance set).  These RelOptInfos are physically identical to baserels,
74  * but are otherrels because they are not in the main join tree.  These added
75  * RTEs and otherrels are used to plan the scans of the individual tables in
76  * the inheritance set; then the parent baserel is given an Append plan
77  * comprising the best plans for the individual child tables.
78  *
79  * At one time we also made otherrels to represent join RTEs, for use in
80  * handling join alias Vars.  Currently this is not needed because all join
81  * alias Vars are expanded to non-aliased form during preprocess_expression.
82  *
83  * Parts of this data structure are specific to various scan and join
84  * mechanisms.  It didn't seem worth creating new node types for them.
85  *
86  *              relids - Set of base-relation identifiers; it is a base relation
87  *                              if there is just one, a join relation if more than one
88  *              rows - estimated number of tuples in the relation after restriction
89  *                         clauses have been applied (ie, output rows of a plan for it)
90  *              width - avg. number of bytes per tuple in the relation after the
91  *                              appropriate projections have been done (ie, output width)
92  *              targetlist - List of TargetEntry nodes for the attributes we need
93  *                                       to output from this relation
94  *              pathlist - List of Path nodes, one for each potentially useful
95  *                                 method of generating the relation
96  *              cheapest_startup_path - the pathlist member with lowest startup cost
97  *                                                              (regardless of its ordering)
98  *              cheapest_total_path - the pathlist member with lowest total cost
99  *                                                        (regardless of its ordering)
100  *              cheapest_unique_path - for caching cheapest path to produce unique
101  *                                                         (no duplicates) output from relation
102  *              pruneable - flag to let the planner know whether it can prune the
103  *                                      pathlist of this RelOptInfo or not.
104  *
105  * If the relation is a base relation it will have these fields set:
106  *
107  *              relid - RTE index (this is redundant with the relids field, but
108  *                              is provided for convenience of access)
109  *              rtekind - distinguishes plain relation, subquery, or function RTE
110  *              varlist - list of Vars for physical columns (only if table)
111  *              indexlist - list of IndexOptInfo nodes for relation's indexes
112  *                                      (always NIL if it's not a table)
113  *              pages - number of disk pages in relation (zero if not a table)
114  *              tuples - number of tuples in relation (not considering restrictions)
115  *              subplan - plan for subquery (NULL if it's not a subquery)
116  *
117  *              Note: for a subquery, tuples and subplan are not set immediately
118  *              upon creation of the RelOptInfo object; they are filled in when
119  *              set_base_rel_pathlist processes the object.
120  *
121  *              For otherrels that are inheritance children, these fields are filled
122  *              in just as for a baserel.
123  *
124  * The presence of the remaining fields depends on the restrictions
125  * and joins that the relation participates in:
126  *
127  *              baserestrictinfo - List of RestrictInfo nodes, containing info about
128  *                                      each qualification clause in which this relation
129  *                                      participates (only used for base rels)
130  *              baserestrictcost - Estimated cost of evaluating the baserestrictinfo
131  *                                      clauses at a single tuple (only used for base rels)
132  *              outerjoinset - For a base rel: if the rel appears within the nullable
133  *                                      side of an outer join, the set of all relids
134  *                                      participating in the highest such outer join; else NULL.
135  *                                      Otherwise, unused.
136  *              joininfo  - List of JoinInfo nodes, containing info about each join
137  *                                      clause in which this relation participates
138  *              index_outer_relids - only used for base rels; set of outer relids
139  *                                      that participate in indexable joinclauses for this rel
140  *              index_inner_paths - only used for base rels; list of InnerIndexscanInfo
141  *                                      nodes showing best indexpaths for various subsets of
142  *                                      index_outer_relids.
143  *
144  * Note: Keeping a restrictinfo list in the RelOptInfo is useful only for
145  * base rels, because for a join rel the set of clauses that are treated as
146  * restrict clauses varies depending on which sub-relations we choose to join.
147  * (For example, in a 3-base-rel join, a clause relating rels 1 and 2 must be
148  * treated as a restrictclause if we join {1} and {2 3} to make {1 2 3}; but
149  * if we join {1 2} and {3} then that clause will be a restrictclause in {1 2}
150  * and should not be processed again at the level of {1 2 3}.)  Therefore,
151  * the restrictinfo list in the join case appears in individual JoinPaths
152  * (field joinrestrictinfo), not in the parent relation.  But it's OK for
153  * the RelOptInfo to store the joininfo lists, because those are the same
154  * for a given rel no matter how we form it.
155  *
156  * We store baserestrictcost in the RelOptInfo (for base relations) because
157  * we know we will need it at least once (to price the sequential scan)
158  * and may need it multiple times to price index scans.
159  *
160  * outerjoinset is used to ensure correct placement of WHERE clauses that
161  * apply to outer-joined relations; we must not apply such WHERE clauses
162  * until after the outer join is performed.
163  *----------
164  */
165 typedef enum RelOptKind
166 {
167         RELOPT_BASEREL,
168         RELOPT_JOINREL,
169         RELOPT_OTHER_CHILD_REL
170 } RelOptKind;
171
172 typedef struct RelOptInfo
173 {
174         NodeTag         type;
175
176         RelOptKind      reloptkind;
177
178         /* all relations included in this RelOptInfo */
179         Relids          relids;                 /* set of base relids (rangetable indexes) */
180
181         /* size estimates generated by planner */
182         double          rows;                   /* estimated number of result tuples */
183         int                     width;                  /* estimated avg width of result tuples */
184
185         /* materialization information */
186         List       *targetlist;
187         List       *pathlist;           /* Path structures */
188         struct Path *cheapest_startup_path;
189         struct Path *cheapest_total_path;
190         struct Path *cheapest_unique_path;
191         bool            pruneable;
192
193         /* information about a base rel (not set for join rels!) */
194         Index           relid;
195         RTEKind         rtekind;                /* RELATION, SUBQUERY, or FUNCTION */
196         List       *varlist;
197         List       *indexlist;
198         long            pages;
199         double          tuples;
200         struct Plan *subplan;           /* if subquery */
201
202         /* used by various scans and joins: */
203         List       *baserestrictinfo;           /* RestrictInfo structures (if
204                                                                                  * base rel) */
205         QualCost        baserestrictcost;               /* cost of evaluating the above */
206         Relids          outerjoinset;   /* set of base relids */
207         List       *joininfo;           /* JoinInfo structures */
208
209         /* cached info about inner indexscan paths for relation: */
210         Relids          index_outer_relids;             /* other relids in indexable join
211                                                                                  * clauses */
212         List       *index_inner_paths;          /* InnerIndexscanInfo nodes */
213         /*
214          * Inner indexscans are not in the main pathlist because they are
215          * not usable except in specific join contexts.  We use the
216          * index_inner_paths list just to avoid recomputing the best inner
217          * indexscan repeatedly for similar outer relations.  See comments
218          * for InnerIndexscanInfo.
219          */
220 } RelOptInfo;
221
222 /*
223  * IndexOptInfo
224  *              Per-index information for planning/optimization
225  *
226  *              Prior to Postgres 7.0, RelOptInfo was used to describe both relations
227  *              and indexes, but that created confusion without actually doing anything
228  *              useful.  So now we have a separate IndexOptInfo struct for indexes.
229  *
230  *              classlist[], indexkeys[], and ordering[] have ncolumns entries.
231  *              Zeroes in the indexkeys[] array indicate index columns that are
232  *              expressions; there is one element in indexprs for each such column.
233  *
234  *              Note: for historical reasons, the classlist and ordering arrays have
235  *              an extra entry that is always zero.  Some code scans until it sees a
236  *              zero entry, rather than looking at ncolumns.
237  *
238  *              The indexprs and indpred expressions have been run through
239  *              eval_const_expressions() for ease of matching to WHERE clauses.
240  */
241
242 typedef struct IndexOptInfo
243 {
244         NodeTag         type;
245
246         Oid                     indexoid;               /* OID of the index relation */
247
248         /* statistics from pg_class */
249         long            pages;                  /* number of disk pages in index */
250         double          tuples;                 /* number of index tuples in index */
251
252         /* index descriptor information */
253         int                     ncolumns;               /* number of columns in index */
254         Oid                *classlist;          /* OIDs of operator classes for columns */
255         int                *indexkeys;          /* column numbers of index's keys, or 0 */
256         Oid                *ordering;           /* OIDs of sort operators for each column */
257         Oid                     relam;                  /* OID of the access method (in pg_am) */
258
259         RegProcedure amcostestimate;    /* OID of the access method's cost fcn */
260
261         List       *indexprs;           /* expressions for non-simple index columns */
262         List       *indpred;            /* predicate if a partial index, else NIL */
263         bool            unique;                 /* true if a unique index */
264
265         /* cached info about inner indexscan paths for index */
266         Relids          outer_relids;   /* other relids in usable join clauses */
267         List       *inner_paths;        /* List of InnerIndexscanInfo nodes */
268 } IndexOptInfo;
269
270
271 /*
272  * PathKeys
273  *
274  *      The sort ordering of a path is represented by a list of sublists of
275  *      PathKeyItem nodes.      An empty list implies no known ordering.  Otherwise
276  *      the first sublist represents the primary sort key, the second the
277  *      first secondary sort key, etc.  Each sublist contains one or more
278  *      PathKeyItem nodes, each of which can be taken as the attribute that
279  *      appears at that sort position.  (See the top of optimizer/path/pathkeys.c
280  *      for more information.)
281  */
282
283 typedef struct PathKeyItem
284 {
285         NodeTag         type;
286
287         Node       *key;                        /* the item that is ordered */
288         Oid                     sortop;                 /* the ordering operator ('<' op) */
289
290         /*
291          * key typically points to a Var node, ie a relation attribute, but it
292          * can also point to an arbitrary expression representing the value
293          * indexed by an index expression.
294          */
295 } PathKeyItem;
296
297 /*
298  * Type "Path" is used as-is for sequential-scan paths.  For other
299  * path types it is the first component of a larger struct.
300  *
301  * Note: "pathtype" is the NodeTag of the Plan node we could build from this
302  * Path.  It is partially redundant with the Path's NodeTag, but allows us
303  * to use the same Path type for multiple Plan types where there is no need
304  * to distinguish the Plan type during path processing.
305  */
306
307 typedef struct Path
308 {
309         NodeTag         type;
310
311         RelOptInfo *parent;                     /* the relation this path can build */
312
313         /* estimated execution costs for path (see costsize.c for more info) */
314         Cost            startup_cost;   /* cost expended before fetching any
315                                                                  * tuples */
316         Cost            total_cost;             /* total cost (assuming all tuples
317                                                                  * fetched) */
318
319         NodeTag         pathtype;               /* tag identifying scan/join method */
320
321         List       *pathkeys;           /* sort ordering of path's output */
322         /* pathkeys is a List of Lists of PathKeyItem nodes; see above */
323 } Path;
324
325 /*----------
326  * IndexPath represents an index scan.  Although an indexscan can only read
327  * a single relation, it can scan it more than once, potentially using a
328  * different index during each scan.  The result is the union (OR) of all the
329  * tuples matched during any scan.      (The executor is smart enough not to return
330  * the same tuple more than once, even if it is matched in multiple scans.)
331  *
332  * 'indexinfo' is a list of IndexOptInfo nodes, one per scan to be performed.
333  *
334  * 'indexqual' is a list of index qualifications, also one per scan.
335  * Each entry in 'indexqual' is a sublist of qualification expressions with
336  * implicit AND semantics across the sublist items.  Only expressions that
337  * are usable as indexquals (as determined by indxpath.c) may appear here.
338  * NOTE that the semantics of the top-level list in 'indexqual' is OR
339  * combination, while the sublists are implicitly AND combinations!
340  * Also note that indexquals lists do not contain RestrictInfo nodes,
341  * just bare clause expressions.
342  *
343  * 'indexjoinclauses' is NIL for an ordinary indexpath (one that does not
344  * use any join clauses in the index conditions).  For an innerjoin indexpath,
345  * it has the same structure as 'indexqual', but references the RestrictInfo
346  * nodes from which the indexqual was built, rather than the bare clause
347  * expressions.  (Note: there isn't necessarily a one-to-one correspondence
348  * between RestrictInfos and expressions, because of expansion of special
349  * indexable operators.)  We need this so that we can eliminate redundant
350  * join clauses when plans are built.
351  *
352  * 'indexscandir' is one of:
353  *              ForwardScanDirection: forward scan of an ordered index
354  *              BackwardScanDirection: backward scan of an ordered index
355  *              NoMovementScanDirection: scan of an unordered index, or don't care
356  * (The executor doesn't care whether it gets ForwardScanDirection or
357  * NoMovementScanDirection for an indexscan, but the planner wants to
358  * distinguish ordered from unordered indexes for building pathkeys.)
359  *
360  * 'rows' is the estimated result tuple count for the indexscan.  This
361  * is the same as path.parent->rows for a simple indexscan, but it is
362  * different for a nestloop inner scan, because the additional indexquals
363  * coming from join clauses make the scan more selective than the parent
364  * rel's restrict clauses alone would do.
365  *----------
366  */
367 typedef struct IndexPath
368 {
369         Path            path;
370         List       *indexinfo;
371         List       *indexqual;
372         List       *indexjoinclauses;
373         ScanDirection indexscandir;
374         double          rows;                   /* estimated number of result tuples */
375 } IndexPath;
376
377 /*
378  * TidPath represents a scan by TID
379  */
380 typedef struct TidPath
381 {
382         Path            path;
383         List       *tideval;            /* qual(s) involving CTID = something */
384 } TidPath;
385
386 /*
387  * AppendPath represents an Append plan, ie, successive execution of
388  * several member plans.  Currently it is only used to handle expansion
389  * of inheritance trees.
390  */
391 typedef struct AppendPath
392 {
393         Path            path;
394         List       *subpaths;           /* list of component Paths */
395 } AppendPath;
396
397 /*
398  * ResultPath represents use of a Result plan node, either to compute a
399  * variable-free targetlist or to gate execution of a subplan with a
400  * one-time (variable-free) qual condition.  Note that in the former case
401  * path.parent will be NULL; in the latter case it is copied from the subpath.
402  */
403 typedef struct ResultPath
404 {
405         Path            path;
406         Path       *subpath;
407         List       *constantqual;
408 } ResultPath;
409
410 /*
411  * MaterialPath represents use of a Material plan node, i.e., caching of
412  * the output of its subpath.  This is used when the subpath is expensive
413  * and needs to be scanned repeatedly, or when we need mark/restore ability
414  * and the subpath doesn't have it.
415  */
416 typedef struct MaterialPath
417 {
418         Path            path;
419         Path       *subpath;
420 } MaterialPath;
421
422 /*
423  * UniquePath represents elimination of distinct rows from the output of
424  * its subpath.
425  *
426  * This is unlike the other Path nodes in that it can actually generate
427  * two different plans: either hash-based or sort-based implementation.
428  * The decision is sufficiently localized that it's not worth having two
429  * separate Path node types.
430  */
431 typedef struct UniquePath
432 {
433         Path            path;
434         Path       *subpath;
435         bool            use_hash;
436         double          rows;                   /* estimated number of result tuples */
437 } UniquePath;
438
439 /*
440  * All join-type paths share these fields.
441  */
442
443 typedef struct JoinPath
444 {
445         Path            path;
446
447         JoinType        jointype;
448
449         Path       *outerjoinpath;      /* path for the outer side of the join */
450         Path       *innerjoinpath;      /* path for the inner side of the join */
451
452         List       *joinrestrictinfo;           /* RestrictInfos to apply to join */
453
454         /*
455          * See the notes for RelOptInfo to understand why joinrestrictinfo is
456          * needed in JoinPath, and can't be merged into the parent RelOptInfo.
457          */
458 } JoinPath;
459
460 /*
461  * A nested-loop path needs no special fields.
462  */
463
464 typedef JoinPath NestPath;
465
466 /*
467  * A mergejoin path has these fields.
468  *
469  * path_mergeclauses lists the clauses (in the form of RestrictInfos)
470  * that will be used in the merge.      (Before 7.0, this was a list of bare
471  * clause expressions, but we can save on list memory and cost_qual_eval
472  * work by leaving it in the form of a RestrictInfo list.)
473  *
474  * Note that the mergeclauses are a subset of the parent relation's
475  * restriction-clause list.  Any join clauses that are not mergejoinable
476  * appear only in the parent's restrict list, and must be checked by a
477  * qpqual at execution time.
478  *
479  * outersortkeys (resp. innersortkeys) is NIL if the outer path
480  * (resp. inner path) is already ordered appropriately for the
481  * mergejoin.  If it is not NIL then it is a PathKeys list describing
482  * the ordering that must be created by an explicit sort step.
483  */
484
485 typedef struct MergePath
486 {
487         JoinPath        jpath;
488         List       *path_mergeclauses;          /* join clauses to be used for
489                                                                                  * merge */
490         List       *outersortkeys;      /* keys for explicit sort, if any */
491         List       *innersortkeys;      /* keys for explicit sort, if any */
492 } MergePath;
493
494 /*
495  * A hashjoin path has these fields.
496  *
497  * The remarks above for mergeclauses apply for hashclauses as well.
498  *
499  * Hashjoin does not care what order its inputs appear in, so we have
500  * no need for sortkeys.
501  */
502
503 typedef struct HashPath
504 {
505         JoinPath        jpath;
506         List       *path_hashclauses;           /* join clauses used for hashing */
507 } HashPath;
508
509 /*
510  * Restriction clause info.
511  *
512  * We create one of these for each AND sub-clause of a restriction condition
513  * (WHERE or JOIN/ON clause).  Since the restriction clauses are logically
514  * ANDed, we can use any one of them or any subset of them to filter out
515  * tuples, without having to evaluate the rest.  The RestrictInfo node itself
516  * stores data used by the optimizer while choosing the best query plan.
517  *
518  * If a restriction clause references a single base relation, it will appear
519  * in the baserestrictinfo list of the RelOptInfo for that base rel.
520  *
521  * If a restriction clause references more than one base rel, it will
522  * appear in the JoinInfo lists of every RelOptInfo that describes a strict
523  * subset of the base rels mentioned in the clause.  The JoinInfo lists are
524  * used to drive join tree building by selecting plausible join candidates.
525  * The clause cannot actually be applied until we have built a join rel
526  * containing all the base rels it references, however.
527  *
528  * When we construct a join rel that includes all the base rels referenced
529  * in a multi-relation restriction clause, we place that clause into the
530  * joinrestrictinfo lists of paths for the join rel, if neither left nor
531  * right sub-path includes all base rels referenced in the clause.      The clause
532  * will be applied at that join level, and will not propagate any further up
533  * the join tree.  (Note: the "predicate migration" code was once intended to
534  * push restriction clauses up and down the plan tree based on evaluation
535  * costs, but it's dead code and is unlikely to be resurrected in the
536  * foreseeable future.)
537  *
538  * Note that in the presence of more than two rels, a multi-rel restriction
539  * might reach different heights in the join tree depending on the join
540  * sequence we use.  So, these clauses cannot be associated directly with
541  * the join RelOptInfo, but must be kept track of on a per-join-path basis.
542  *
543  * When dealing with outer joins we have to be very careful about pushing qual
544  * clauses up and down the tree.  An outer join's own JOIN/ON conditions must
545  * be evaluated exactly at that join node, and any quals appearing in WHERE or
546  * in a JOIN above the outer join cannot be pushed down below the outer join.
547  * Otherwise the outer join will produce wrong results because it will see the
548  * wrong sets of input rows.  All quals are stored as RestrictInfo nodes
549  * during planning, but there's a flag to indicate whether a qual has been
550  * pushed down to a lower level than its original syntactic placement in the
551  * join tree would suggest.  If an outer join prevents us from pushing a qual
552  * down to its "natural" semantic level (the level associated with just the
553  * base rels used in the qual) then the qual will appear in JoinInfo lists
554  * that reference more than just the base rels it actually uses.  By
555  * pretending that the qual references all the rels appearing in the outer
556  * join, we prevent it from being evaluated below the outer join's joinrel.
557  * When we do form the outer join's joinrel, we still need to distinguish
558  * those quals that are actually in that join's JOIN/ON condition from those
559  * that appeared higher in the tree and were pushed down to the join rel
560  * because they used no other rels.  That's what the ispusheddown flag is for;
561  * it tells us that a qual came from a point above the join of the specific
562  * set of base rels that it uses (or that the JoinInfo structures claim it
563  * uses).  A clause that originally came from WHERE will *always* have its
564  * ispusheddown flag set; a clause that came from an INNER JOIN condition,
565  * but doesn't use all the rels being joined, will also have ispusheddown set
566  * because it will get attached to some lower joinrel.
567  *
568  * In general, the referenced clause might be arbitrarily complex.      The
569  * kinds of clauses we can handle as indexscan quals, mergejoin clauses,
570  * or hashjoin clauses are fairly limited --- the code for each kind of
571  * path is responsible for identifying the restrict clauses it can use
572  * and ignoring the rest.  Clauses not implemented by an indexscan,
573  * mergejoin, or hashjoin will be placed in the plan qual or joinqual field
574  * of the finished Plan node, where they will be enforced by general-purpose
575  * qual-expression-evaluation code.  (But we are still entitled to count
576  * their selectivity when estimating the result tuple count, if we
577  * can guess what it is...)
578  */
579
580 typedef struct RestrictInfo
581 {
582         NodeTag         type;
583
584         Expr       *clause;                     /* the represented clause of WHERE or JOIN */
585
586         bool            ispusheddown;   /* TRUE if clause was pushed down in level */
587
588         /* only used if clause is an OR clause: */
589         List       *subclauseindices;           /* indexes matching subclauses */
590         /* subclauseindices is a List of Lists of IndexOptInfos */
591
592         /* cache space for costs (currently only used for join clauses) */
593         QualCost        eval_cost;              /* eval cost of clause; -1 if not yet set */
594         Selectivity this_selec;         /* selectivity; -1 if not yet set */
595
596         /*
597          * If the clause looks useful for joining --- that is, it is a binary
598          * opclause with nonoverlapping sets of relids referenced in the left
599          * and right sides --- then these two fields are set to sets of the
600          * referenced relids.  Otherwise they are both NULL.
601          */
602         Relids          left_relids;    /* relids in left side of join clause */
603         Relids          right_relids;   /* relids in right side of join clause */
604
605         /* valid if clause is mergejoinable, else InvalidOid: */
606         Oid                     mergejoinoperator;              /* copy of clause operator */
607         Oid                     left_sortop;    /* leftside sortop needed for mergejoin */
608         Oid                     right_sortop;   /* rightside sortop needed for mergejoin */
609
610         /* cache space for mergeclause processing; NIL if not yet set */
611         List       *left_pathkey;       /* canonical pathkey for left side */
612         List       *right_pathkey;      /* canonical pathkey for right side */
613
614         /* cache space for mergeclause processing; -1 if not yet set */
615         Selectivity left_mergescansel;          /* fraction of left side to scan */
616         Selectivity right_mergescansel;         /* fraction of right side to scan */
617
618         /* valid if clause is hashjoinable, else InvalidOid: */
619         Oid                     hashjoinoperator;               /* copy of clause operator */
620
621         /* cache space for hashclause processing; -1 if not yet set */
622         Selectivity left_bucketsize;    /* avg bucketsize of left side */
623         Selectivity right_bucketsize;           /* avg bucketsize of right side */
624 } RestrictInfo;
625
626 /*
627  * Join clause info.
628  *
629  * We make a list of these for each RelOptInfo, containing info about
630  * all the join clauses this RelOptInfo participates in.  (For this
631  * purpose, a "join clause" is a WHERE clause that mentions both vars
632  * belonging to this relation and vars belonging to relations not yet
633  * joined to it.)  We group these clauses according to the set of
634  * other base relations (unjoined relations) mentioned in them.
635  * There is one JoinInfo for each distinct set of unjoined_relids,
636  * and its jinfo_restrictinfo lists the clause(s) that use that set
637  * of other relations.
638  */
639
640 typedef struct JoinInfo
641 {
642         NodeTag         type;
643         Relids          unjoined_relids;        /* some rels not yet part of my RelOptInfo */
644         List       *jinfo_restrictinfo;         /* relevant RestrictInfos */
645 } JoinInfo;
646
647 /*
648  * Inner indexscan info.
649  *
650  * An inner indexscan is one that uses one or more joinclauses as index
651  * conditions (perhaps in addition to plain restriction clauses).  So it
652  * can only be used as the inner path of a nestloop join where the outer
653  * relation includes all other relids appearing in those joinclauses.
654  * The set of usable joinclauses, and thus the best inner indexscan,
655  * thus varies depending on which outer relation we consider; so we have
656  * to recompute the best such path for every join.  To avoid lots of
657  * redundant computation, we cache the results of such searches.  For
658  * each index we compute the set of possible otherrelids (all relids
659  * appearing in joinquals that could become indexquals for this index).
660  * Two outer relations whose relids have the same intersection with this
661  * set will have the same set of available joinclauses and thus the same
662  * best inner indexscan for that index.  Similarly, for each base relation,
663  * we form the union of the per-index otherrelids sets.  Two outer relations
664  * with the same intersection with that set will have the same best overall
665  * inner indexscan for the base relation.  We use lists of InnerIndexscanInfo
666  * nodes to cache the results of these searches at both the index and
667  * relation level.
668  *
669  * The search key also includes a bool showing whether the join being
670  * considered is an outer join.  Since we constrain the join order for
671  * outer joins, I believe that this bool can only have one possible value
672  * for any particular base relation; but store it anyway to avoid confusion.
673  */
674
675 typedef struct InnerIndexscanInfo
676 {
677         NodeTag         type;
678         /* The lookup key: */
679         Relids          other_relids;   /* a set of relevant other relids */
680         bool            isouterjoin;    /* true if join is outer */
681         /* Best path for this lookup key: */
682         Path       *best_innerpath;     /* best inner indexscan, or NULL if none */
683 } InnerIndexscanInfo;
684
685 /*
686  * IN clause info.
687  *
688  * When we convert top-level IN quals into join operations, we must restrict
689  * the order of joining and use special join methods at some join points.
690  * We record information about each such IN clause in an InClauseInfo struct.
691  * These structs are kept in the Query node's in_info_list.
692  */
693
694 typedef struct InClauseInfo
695 {
696         NodeTag         type;
697         Relids          lefthand;               /* base relids in lefthand expressions */
698         Relids          righthand;              /* base relids coming from the subselect */
699         List       *sub_targetlist;     /* targetlist of original RHS subquery */
700         /*
701          * Note: sub_targetlist is just a list of Vars or expressions;
702          * it does not contain TargetEntry nodes.
703          */
704 } InClauseInfo;
705
706 #endif   /* RELATION_H */