Header And Logo

PostgreSQL
| The world's most advanced open source database.

relation.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * relation.h
00004  *    Definitions for planner's internal data structures.
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/nodes/relation.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef RELATION_H
00015 #define RELATION_H
00016 
00017 #include "access/sdir.h"
00018 #include "nodes/params.h"
00019 #include "nodes/parsenodes.h"
00020 #include "storage/block.h"
00021 
00022 
00023 /*
00024  * Relids
00025  *      Set of relation identifiers (indexes into the rangetable).
00026  */
00027 typedef Bitmapset *Relids;
00028 
00029 /*
00030  * When looking for a "cheapest path", this enum specifies whether we want
00031  * cheapest startup cost or cheapest total cost.
00032  */
00033 typedef enum CostSelector
00034 {
00035     STARTUP_COST, TOTAL_COST
00036 } CostSelector;
00037 
00038 /*
00039  * The cost estimate produced by cost_qual_eval() includes both a one-time
00040  * (startup) cost, and a per-tuple cost.
00041  */
00042 typedef struct QualCost
00043 {
00044     Cost        startup;        /* one-time cost */
00045     Cost        per_tuple;      /* per-evaluation cost */
00046 } QualCost;
00047 
00048 /*
00049  * Costing aggregate function execution requires these statistics about
00050  * the aggregates to be executed by a given Agg node.  Note that transCost
00051  * includes the execution costs of the aggregates' input expressions.
00052  */
00053 typedef struct AggClauseCosts
00054 {
00055     int         numAggs;        /* total number of aggregate functions */
00056     int         numOrderedAggs; /* number that use DISTINCT or ORDER BY */
00057     QualCost    transCost;      /* total per-input-row execution costs */
00058     Cost        finalCost;      /* total costs of agg final functions */
00059     Size        transitionSpace;    /* space for pass-by-ref transition data */
00060 } AggClauseCosts;
00061 
00062 
00063 /*----------
00064  * PlannerGlobal
00065  *      Global information for planning/optimization
00066  *
00067  * PlannerGlobal holds state for an entire planner invocation; this state
00068  * is shared across all levels of sub-Queries that exist in the command being
00069  * planned.
00070  *----------
00071  */
00072 typedef struct PlannerGlobal
00073 {
00074     NodeTag     type;
00075 
00076     ParamListInfo boundParams;  /* Param values provided to planner() */
00077 
00078     List       *subplans;       /* Plans for SubPlan nodes */
00079 
00080     List       *subroots;       /* PlannerInfos for SubPlan nodes */
00081 
00082     Bitmapset  *rewindPlanIDs;  /* indices of subplans that require REWIND */
00083 
00084     List       *finalrtable;    /* "flat" rangetable for executor */
00085 
00086     List       *finalrowmarks;  /* "flat" list of PlanRowMarks */
00087 
00088     List       *resultRelations;    /* "flat" list of integer RT indexes */
00089 
00090     List       *relationOids;   /* OIDs of relations the plan depends on */
00091 
00092     List       *invalItems;     /* other dependencies, as PlanInvalItems */
00093 
00094     int         nParamExec;     /* number of PARAM_EXEC Params used */
00095 
00096     Index       lastPHId;       /* highest PlaceHolderVar ID assigned */
00097 
00098     Index       lastRowMarkId;  /* highest PlanRowMark ID assigned */
00099 
00100     bool        transientPlan;  /* redo plan when TransactionXmin changes? */
00101 } PlannerGlobal;
00102 
00103 /* macro for fetching the Plan associated with a SubPlan node */
00104 #define planner_subplan_get_plan(root, subplan) \
00105     ((Plan *) list_nth((root)->glob->subplans, (subplan)->plan_id - 1))
00106 
00107 
00108 /*----------
00109  * PlannerInfo
00110  *      Per-query information for planning/optimization
00111  *
00112  * This struct is conventionally called "root" in all the planner routines.
00113  * It holds links to all of the planner's working state, in addition to the
00114  * original Query.  Note that at present the planner extensively modifies
00115  * the passed-in Query data structure; someday that should stop.
00116  *----------
00117  */
00118 typedef struct PlannerInfo
00119 {
00120     NodeTag     type;
00121 
00122     Query      *parse;          /* the Query being planned */
00123 
00124     PlannerGlobal *glob;        /* global info for current planner run */
00125 
00126     Index       query_level;    /* 1 at the outermost Query */
00127 
00128     struct PlannerInfo *parent_root;    /* NULL at outermost Query */
00129 
00130     List       *plan_params;    /* list of PlannerParamItems, see below */
00131 
00132     /*
00133      * simple_rel_array holds pointers to "base rels" and "other rels" (see
00134      * comments for RelOptInfo for more info).  It is indexed by rangetable
00135      * index (so entry 0 is always wasted).  Entries can be NULL when an RTE
00136      * does not correspond to a base relation, such as a join RTE or an
00137      * unreferenced view RTE; or if the RelOptInfo hasn't been made yet.
00138      */
00139     struct RelOptInfo **simple_rel_array;       /* All 1-rel RelOptInfos */
00140     int         simple_rel_array_size;  /* allocated size of array */
00141 
00142     /*
00143      * simple_rte_array is the same length as simple_rel_array and holds
00144      * pointers to the associated rangetable entries.  This lets us avoid
00145      * rt_fetch(), which can be a bit slow once large inheritance sets have
00146      * been expanded.
00147      */
00148     RangeTblEntry **simple_rte_array;   /* rangetable as an array */
00149 
00150     /*
00151      * all_baserels is a Relids set of all base relids (but not "other"
00152      * relids) in the query; that is, the Relids identifier of the final join
00153      * we need to form.
00154      */
00155     Relids      all_baserels;
00156 
00157     /*
00158      * join_rel_list is a list of all join-relation RelOptInfos we have
00159      * considered in this planning run.  For small problems we just scan the
00160      * list to do lookups, but when there are many join relations we build a
00161      * hash table for faster lookups.  The hash table is present and valid
00162      * when join_rel_hash is not NULL.  Note that we still maintain the list
00163      * even when using the hash table for lookups; this simplifies life for
00164      * GEQO.
00165      */
00166     List       *join_rel_list;  /* list of join-relation RelOptInfos */
00167     struct HTAB *join_rel_hash; /* optional hashtable for join relations */
00168 
00169     /*
00170      * When doing a dynamic-programming-style join search, join_rel_level[k]
00171      * is a list of all join-relation RelOptInfos of level k, and
00172      * join_cur_level is the current level.  New join-relation RelOptInfos are
00173      * automatically added to the join_rel_level[join_cur_level] list.
00174      * join_rel_level is NULL if not in use.
00175      */
00176     List      **join_rel_level; /* lists of join-relation RelOptInfos */
00177     int         join_cur_level; /* index of list being extended */
00178 
00179     List       *init_plans;     /* init SubPlans for query */
00180 
00181     List       *cte_plan_ids;   /* per-CTE-item list of subplan IDs */
00182 
00183     List       *eq_classes;     /* list of active EquivalenceClasses */
00184 
00185     List       *canon_pathkeys; /* list of "canonical" PathKeys */
00186 
00187     List       *left_join_clauses;      /* list of RestrictInfos for
00188                                          * mergejoinable outer join clauses
00189                                          * w/nonnullable var on left */
00190 
00191     List       *right_join_clauses;     /* list of RestrictInfos for
00192                                          * mergejoinable outer join clauses
00193                                          * w/nonnullable var on right */
00194 
00195     List       *full_join_clauses;      /* list of RestrictInfos for
00196                                          * mergejoinable full join clauses */
00197 
00198     List       *join_info_list;     /* list of SpecialJoinInfos */
00199 
00200     List       *lateral_info_list;  /* list of LateralJoinInfos */
00201 
00202     List       *append_rel_list;    /* list of AppendRelInfos */
00203 
00204     List       *rowMarks;       /* list of PlanRowMarks */
00205 
00206     List       *placeholder_list;       /* list of PlaceHolderInfos */
00207 
00208     List       *query_pathkeys; /* desired pathkeys for query_planner(), and
00209                                  * actual pathkeys after planning */
00210 
00211     List       *group_pathkeys; /* groupClause pathkeys, if any */
00212     List       *window_pathkeys;    /* pathkeys of bottom window, if any */
00213     List       *distinct_pathkeys;      /* distinctClause pathkeys, if any */
00214     List       *sort_pathkeys;  /* sortClause pathkeys, if any */
00215 
00216     List       *minmax_aggs;    /* List of MinMaxAggInfos */
00217 
00218     List       *initial_rels;   /* RelOptInfos we are now trying to join */
00219 
00220     MemoryContext planner_cxt;  /* context holding PlannerInfo */
00221 
00222     double      total_table_pages;      /* # of pages in all tables of query */
00223 
00224     double      tuple_fraction; /* tuple_fraction passed to query_planner */
00225     double      limit_tuples;   /* limit_tuples passed to query_planner */
00226 
00227     bool        hasInheritedTarget;     /* true if parse->resultRelation is an
00228                                          * inheritance child rel */
00229     bool        hasJoinRTEs;    /* true if any RTEs are RTE_JOIN kind */
00230     bool        hasLateralRTEs; /* true if any RTEs are marked LATERAL */
00231     bool        hasHavingQual;  /* true if havingQual was non-null */
00232     bool        hasPseudoConstantQuals; /* true if any RestrictInfo has
00233                                          * pseudoconstant = true */
00234     bool        hasRecursion;   /* true if planning a recursive WITH item */
00235 
00236     /* These fields are used only when hasRecursion is true: */
00237     int         wt_param_id;    /* PARAM_EXEC ID for the work table */
00238     struct Plan *non_recursive_plan;    /* plan for non-recursive term */
00239 
00240     /* These fields are workspace for createplan.c */
00241     Relids      curOuterRels;   /* outer rels above current node */
00242     List       *curOuterParams; /* not-yet-assigned NestLoopParams */
00243 
00244     /* optional private data for join_search_hook, e.g., GEQO */
00245     void       *join_search_private;
00246 } PlannerInfo;
00247 
00248 
00249 /*
00250  * In places where it's known that simple_rte_array[] must have been prepared
00251  * already, we just index into it to fetch RTEs.  In code that might be
00252  * executed before or after entering query_planner(), use this macro.
00253  */
00254 #define planner_rt_fetch(rti, root) \
00255     ((root)->simple_rte_array ? (root)->simple_rte_array[rti] : \
00256      rt_fetch(rti, (root)->parse->rtable))
00257 
00258 
00259 /*----------
00260  * RelOptInfo
00261  *      Per-relation information for planning/optimization
00262  *
00263  * For planning purposes, a "base rel" is either a plain relation (a table)
00264  * or the output of a sub-SELECT or function that appears in the range table.
00265  * In either case it is uniquely identified by an RT index.  A "joinrel"
00266  * is the joining of two or more base rels.  A joinrel is identified by
00267  * the set of RT indexes for its component baserels.  We create RelOptInfo
00268  * nodes for each baserel and joinrel, and store them in the PlannerInfo's
00269  * simple_rel_array and join_rel_list respectively.
00270  *
00271  * Note that there is only one joinrel for any given set of component
00272  * baserels, no matter what order we assemble them in; so an unordered
00273  * set is the right datatype to identify it with.
00274  *
00275  * We also have "other rels", which are like base rels in that they refer to
00276  * single RT indexes; but they are not part of the join tree, and are given
00277  * a different RelOptKind to identify them.  Lastly, there is a RelOptKind
00278  * for "dead" relations, which are base rels that we have proven we don't
00279  * need to join after all.
00280  *
00281  * Currently the only kind of otherrels are those made for member relations
00282  * of an "append relation", that is an inheritance set or UNION ALL subquery.
00283  * An append relation has a parent RTE that is a base rel, which represents
00284  * the entire append relation.  The member RTEs are otherrels.  The parent
00285  * is present in the query join tree but the members are not.  The member
00286  * RTEs and otherrels are used to plan the scans of the individual tables or
00287  * subqueries of the append set; then the parent baserel is given Append
00288  * and/or MergeAppend paths comprising the best paths for the individual
00289  * member rels.  (See comments for AppendRelInfo for more information.)
00290  *
00291  * At one time we also made otherrels to represent join RTEs, for use in
00292  * handling join alias Vars.  Currently this is not needed because all join
00293  * alias Vars are expanded to non-aliased form during preprocess_expression.
00294  *
00295  * Parts of this data structure are specific to various scan and join
00296  * mechanisms.  It didn't seem worth creating new node types for them.
00297  *
00298  *      relids - Set of base-relation identifiers; it is a base relation
00299  *              if there is just one, a join relation if more than one
00300  *      rows - estimated number of tuples in the relation after restriction
00301  *             clauses have been applied (ie, output rows of a plan for it)
00302  *      width - avg. number of bytes per tuple in the relation after the
00303  *              appropriate projections have been done (ie, output width)
00304  *      consider_startup - true if there is any value in keeping paths for
00305  *                         this rel on the basis of having cheap startup cost
00306  *      reltargetlist - List of Var and PlaceHolderVar nodes for the values
00307  *                      we need to output from this relation.
00308  *                      List is in no particular order, but all rels of an
00309  *                      appendrel set must use corresponding orders.
00310  *                      NOTE: in an appendrel child relation, may contain
00311  *                      arbitrary expressions pulled up from a subquery!
00312  *      pathlist - List of Path nodes, one for each potentially useful
00313  *                 method of generating the relation
00314  *      ppilist - ParamPathInfo nodes for parameterized Paths, if any
00315  *      cheapest_startup_path - the pathlist member with lowest startup cost
00316  *          (regardless of ordering) among the unparameterized paths;
00317  *          or NULL if there is no unparameterized path
00318  *      cheapest_total_path - the pathlist member with lowest total cost
00319  *          (regardless of ordering) among the unparameterized paths;
00320  *          or if there is no unparameterized path, the path with lowest
00321  *          total cost among the paths with minimum parameterization
00322  *      cheapest_unique_path - for caching cheapest path to produce unique
00323  *          (no duplicates) output from relation; NULL if not yet requested
00324  *      cheapest_parameterized_paths - best paths for their parameterizations;
00325  *          always includes cheapest_total_path, even if that's unparameterized
00326  *
00327  * If the relation is a base relation it will have these fields set:
00328  *
00329  *      relid - RTE index (this is redundant with the relids field, but
00330  *              is provided for convenience of access)
00331  *      rtekind - distinguishes plain relation, subquery, or function RTE
00332  *      min_attr, max_attr - range of valid AttrNumbers for rel
00333  *      attr_needed - array of bitmapsets indicating the highest joinrel
00334  *              in which each attribute is needed; if bit 0 is set then
00335  *              the attribute is needed as part of final targetlist
00336  *      attr_widths - cache space for per-attribute width estimates;
00337  *                    zero means not computed yet
00338  *      lateral_vars - lateral cross-references of rel, if any (list of
00339  *                     Vars and PlaceHolderVars)
00340  *      lateral_relids - required outer rels for LATERAL, as a Relids set
00341  *                       (for child rels this can be more than lateral_vars)
00342  *      indexlist - list of IndexOptInfo nodes for relation's indexes
00343  *                  (always NIL if it's not a table)
00344  *      pages - number of disk pages in relation (zero if not a table)
00345  *      tuples - number of tuples in relation (not considering restrictions)
00346  *      allvisfrac - fraction of disk pages that are marked all-visible
00347  *      subplan - plan for subquery (NULL if it's not a subquery)
00348  *      subroot - PlannerInfo for subquery (NULL if it's not a subquery)
00349  *      subplan_params - list of PlannerParamItems to be passed to subquery
00350  *      fdwroutine - function hooks for FDW, if foreign table (else NULL)
00351  *      fdw_private - private state for FDW, if foreign table (else NULL)
00352  *
00353  *      Note: for a subquery, tuples, subplan, subroot are not set immediately
00354  *      upon creation of the RelOptInfo object; they are filled in when
00355  *      set_subquery_pathlist processes the object.  Likewise, fdwroutine
00356  *      and fdw_private are filled during initial path creation.
00357  *
00358  *      For otherrels that are appendrel members, these fields are filled
00359  *      in just as for a baserel.
00360  *
00361  * The presence of the remaining fields depends on the restrictions
00362  * and joins that the relation participates in:
00363  *
00364  *      baserestrictinfo - List of RestrictInfo nodes, containing info about
00365  *                  each non-join qualification clause in which this relation
00366  *                  participates (only used for base rels)
00367  *      baserestrictcost - Estimated cost of evaluating the baserestrictinfo
00368  *                  clauses at a single tuple (only used for base rels)
00369  *      joininfo  - List of RestrictInfo nodes, containing info about each
00370  *                  join clause in which this relation participates (but
00371  *                  note this excludes clauses that might be derivable from
00372  *                  EquivalenceClasses)
00373  *      has_eclass_joins - flag that EquivalenceClass joins are possible
00374  *
00375  * Note: Keeping a restrictinfo list in the RelOptInfo is useful only for
00376  * base rels, because for a join rel the set of clauses that are treated as
00377  * restrict clauses varies depending on which sub-relations we choose to join.
00378  * (For example, in a 3-base-rel join, a clause relating rels 1 and 2 must be
00379  * treated as a restrictclause if we join {1} and {2 3} to make {1 2 3}; but
00380  * if we join {1 2} and {3} then that clause will be a restrictclause in {1 2}
00381  * and should not be processed again at the level of {1 2 3}.)  Therefore,
00382  * the restrictinfo list in the join case appears in individual JoinPaths
00383  * (field joinrestrictinfo), not in the parent relation.  But it's OK for
00384  * the RelOptInfo to store the joininfo list, because that is the same
00385  * for a given rel no matter how we form it.
00386  *
00387  * We store baserestrictcost in the RelOptInfo (for base relations) because
00388  * we know we will need it at least once (to price the sequential scan)
00389  * and may need it multiple times to price index scans.
00390  *----------
00391  */
00392 typedef enum RelOptKind
00393 {
00394     RELOPT_BASEREL,
00395     RELOPT_JOINREL,
00396     RELOPT_OTHER_MEMBER_REL,
00397     RELOPT_DEADREL
00398 } RelOptKind;
00399 
00400 typedef struct RelOptInfo
00401 {
00402     NodeTag     type;
00403 
00404     RelOptKind  reloptkind;
00405 
00406     /* all relations included in this RelOptInfo */
00407     Relids      relids;         /* set of base relids (rangetable indexes) */
00408 
00409     /* size estimates generated by planner */
00410     double      rows;           /* estimated number of result tuples */
00411     int         width;          /* estimated avg width of result tuples */
00412 
00413     /* per-relation planner control flags */
00414     bool        consider_startup;   /* keep cheap-startup-cost paths? */
00415 
00416     /* materialization information */
00417     List       *reltargetlist;  /* Vars to be output by scan of relation */
00418     List       *pathlist;       /* Path structures */
00419     List       *ppilist;        /* ParamPathInfos used in pathlist */
00420     struct Path *cheapest_startup_path;
00421     struct Path *cheapest_total_path;
00422     struct Path *cheapest_unique_path;
00423     List       *cheapest_parameterized_paths;
00424 
00425     /* information about a base rel (not set for join rels!) */
00426     Index       relid;
00427     Oid         reltablespace;  /* containing tablespace */
00428     RTEKind     rtekind;        /* RELATION, SUBQUERY, or FUNCTION */
00429     AttrNumber  min_attr;       /* smallest attrno of rel (often <0) */
00430     AttrNumber  max_attr;       /* largest attrno of rel */
00431     Relids     *attr_needed;    /* array indexed [min_attr .. max_attr] */
00432     int32      *attr_widths;    /* array indexed [min_attr .. max_attr] */
00433     List       *lateral_vars;   /* LATERAL Vars and PHVs referenced by rel */
00434     Relids      lateral_relids; /* minimum parameterization of rel */
00435     List       *indexlist;      /* list of IndexOptInfo */
00436     BlockNumber pages;          /* size estimates derived from pg_class */
00437     double      tuples;
00438     double      allvisfrac;
00439     /* use "struct Plan" to avoid including plannodes.h here */
00440     struct Plan *subplan;       /* if subquery */
00441     PlannerInfo *subroot;       /* if subquery */
00442     List       *subplan_params; /* if subquery */
00443     /* use "struct FdwRoutine" to avoid including fdwapi.h here */
00444     struct FdwRoutine *fdwroutine;      /* if foreign table */
00445     void       *fdw_private;    /* if foreign table */
00446 
00447     /* used by various scans and joins: */
00448     List       *baserestrictinfo;       /* RestrictInfo structures (if base
00449                                          * rel) */
00450     QualCost    baserestrictcost;       /* cost of evaluating the above */
00451     List       *joininfo;       /* RestrictInfo structures for join clauses
00452                                  * involving this rel */
00453     bool        has_eclass_joins;       /* T means joininfo is incomplete */
00454 } RelOptInfo;
00455 
00456 /*
00457  * IndexOptInfo
00458  *      Per-index information for planning/optimization
00459  *
00460  *      indexkeys[], indexcollations[], opfamily[], and opcintype[]
00461  *      each have ncolumns entries.
00462  *
00463  *      sortopfamily[], reverse_sort[], and nulls_first[] likewise have
00464  *      ncolumns entries, if the index is ordered; but if it is unordered,
00465  *      those pointers are NULL.
00466  *
00467  *      Zeroes in the indexkeys[] array indicate index columns that are
00468  *      expressions; there is one element in indexprs for each such column.
00469  *
00470  *      For an ordered index, reverse_sort[] and nulls_first[] describe the
00471  *      sort ordering of a forward indexscan; we can also consider a backward
00472  *      indexscan, which will generate the reverse ordering.
00473  *
00474  *      The indexprs and indpred expressions have been run through
00475  *      prepqual.c and eval_const_expressions() for ease of matching to
00476  *      WHERE clauses. indpred is in implicit-AND form.
00477  *
00478  *      indextlist is a TargetEntry list representing the index columns.
00479  *      It provides an equivalent base-relation Var for each simple column,
00480  *      and links to the matching indexprs element for each expression column.
00481  */
00482 typedef struct IndexOptInfo
00483 {
00484     NodeTag     type;
00485 
00486     Oid         indexoid;       /* OID of the index relation */
00487     Oid         reltablespace;  /* tablespace of index (not table) */
00488     RelOptInfo *rel;            /* back-link to index's table */
00489 
00490     /* index-size statistics (from pg_class and elsewhere) */
00491     BlockNumber pages;          /* number of disk pages in index */
00492     double      tuples;         /* number of index tuples in index */
00493     int         tree_height;    /* index tree height, or -1 if unknown */
00494 
00495     /* index descriptor information */
00496     int         ncolumns;       /* number of columns in index */
00497     int        *indexkeys;      /* column numbers of index's keys, or 0 */
00498     Oid        *indexcollations;    /* OIDs of collations of index columns */
00499     Oid        *opfamily;       /* OIDs of operator families for columns */
00500     Oid        *opcintype;      /* OIDs of opclass declared input data types */
00501     Oid        *sortopfamily;   /* OIDs of btree opfamilies, if orderable */
00502     bool       *reverse_sort;   /* is sort order descending? */
00503     bool       *nulls_first;    /* do NULLs come first in the sort order? */
00504     Oid         relam;          /* OID of the access method (in pg_am) */
00505 
00506     RegProcedure amcostestimate;    /* OID of the access method's cost fcn */
00507 
00508     List       *indexprs;       /* expressions for non-simple index columns */
00509     List       *indpred;        /* predicate if a partial index, else NIL */
00510 
00511     List       *indextlist;     /* targetlist representing index columns */
00512 
00513     bool        predOK;         /* true if predicate matches query */
00514     bool        unique;         /* true if a unique index */
00515     bool        immediate;      /* is uniqueness enforced immediately? */
00516     bool        hypothetical;   /* true if index doesn't really exist */
00517     bool        canreturn;      /* can index return IndexTuples? */
00518     bool        amcanorderbyop; /* does AM support order by operator result? */
00519     bool        amoptionalkey;  /* can query omit key for the first column? */
00520     bool        amsearcharray;  /* can AM handle ScalarArrayOpExpr quals? */
00521     bool        amsearchnulls;  /* can AM search for NULL/NOT NULL entries? */
00522     bool        amhasgettuple;  /* does AM have amgettuple interface? */
00523     bool        amhasgetbitmap; /* does AM have amgetbitmap interface? */
00524 } IndexOptInfo;
00525 
00526 
00527 /*
00528  * EquivalenceClasses
00529  *
00530  * Whenever we can determine that a mergejoinable equality clause A = B is
00531  * not delayed by any outer join, we create an EquivalenceClass containing
00532  * the expressions A and B to record this knowledge.  If we later find another
00533  * equivalence B = C, we add C to the existing EquivalenceClass; this may
00534  * require merging two existing EquivalenceClasses.  At the end of the qual
00535  * distribution process, we have sets of values that are known all transitively
00536  * equal to each other, where "equal" is according to the rules of the btree
00537  * operator family(s) shown in ec_opfamilies, as well as the collation shown
00538  * by ec_collation.  (We restrict an EC to contain only equalities whose
00539  * operators belong to the same set of opfamilies.  This could probably be
00540  * relaxed, but for now it's not worth the trouble, since nearly all equality
00541  * operators belong to only one btree opclass anyway.  Similarly, we suppose
00542  * that all or none of the input datatypes are collatable, so that a single
00543  * collation value is sufficient.)
00544  *
00545  * We also use EquivalenceClasses as the base structure for PathKeys, letting
00546  * us represent knowledge about different sort orderings being equivalent.
00547  * Since every PathKey must reference an EquivalenceClass, we will end up
00548  * with single-member EquivalenceClasses whenever a sort key expression has
00549  * not been equivalenced to anything else.  It is also possible that such an
00550  * EquivalenceClass will contain a volatile expression ("ORDER BY random()"),
00551  * which is a case that can't arise otherwise since clauses containing
00552  * volatile functions are never considered mergejoinable.  We mark such
00553  * EquivalenceClasses specially to prevent them from being merged with
00554  * ordinary EquivalenceClasses.  Also, for volatile expressions we have
00555  * to be careful to match the EquivalenceClass to the correct targetlist
00556  * entry: consider SELECT random() AS a, random() AS b ... ORDER BY b,a.
00557  * So we record the SortGroupRef of the originating sort clause.
00558  *
00559  * We allow equality clauses appearing below the nullable side of an outer join
00560  * to form EquivalenceClasses, but these have a slightly different meaning:
00561  * the included values might be all NULL rather than all the same non-null
00562  * values.  See src/backend/optimizer/README for more on that point.
00563  *
00564  * NB: if ec_merged isn't NULL, this class has been merged into another, and
00565  * should be ignored in favor of using the pointed-to class.
00566  */
00567 typedef struct EquivalenceClass
00568 {
00569     NodeTag     type;
00570 
00571     List       *ec_opfamilies;  /* btree operator family OIDs */
00572     Oid         ec_collation;   /* collation, if datatypes are collatable */
00573     List       *ec_members;     /* list of EquivalenceMembers */
00574     List       *ec_sources;     /* list of generating RestrictInfos */
00575     List       *ec_derives;     /* list of derived RestrictInfos */
00576     Relids      ec_relids;      /* all relids appearing in ec_members */
00577     bool        ec_has_const;   /* any pseudoconstants in ec_members? */
00578     bool        ec_has_volatile;    /* the (sole) member is a volatile expr */
00579     bool        ec_below_outer_join;    /* equivalence applies below an OJ */
00580     bool        ec_broken;      /* failed to generate needed clauses? */
00581     Index       ec_sortref;     /* originating sortclause label, or 0 */
00582     struct EquivalenceClass *ec_merged; /* set if merged into another EC */
00583 } EquivalenceClass;
00584 
00585 /*
00586  * If an EC contains a const and isn't below-outer-join, any PathKey depending
00587  * on it must be redundant, since there's only one possible value of the key.
00588  */
00589 #define EC_MUST_BE_REDUNDANT(eclass)  \
00590     ((eclass)->ec_has_const && !(eclass)->ec_below_outer_join)
00591 
00592 /*
00593  * EquivalenceMember - one member expression of an EquivalenceClass
00594  *
00595  * em_is_child signifies that this element was built by transposing a member
00596  * for an appendrel parent relation to represent the corresponding expression
00597  * for an appendrel child.  These members are used for determining the
00598  * pathkeys of scans on the child relation and for explicitly sorting the
00599  * child when necessary to build a MergeAppend path for the whole appendrel
00600  * tree.  An em_is_child member has no impact on the properties of the EC as a
00601  * whole; in particular the EC's ec_relids field does NOT include the child
00602  * relation.  An em_is_child member should never be marked em_is_const nor
00603  * cause ec_has_const or ec_has_volatile to be set, either.  Thus, em_is_child
00604  * members are not really full-fledged members of the EC, but just reflections
00605  * or doppelgangers of real members.  Most operations on EquivalenceClasses
00606  * should ignore em_is_child members, and those that don't should test
00607  * em_relids to make sure they only consider relevant members.
00608  *
00609  * em_datatype is usually the same as exprType(em_expr), but can be
00610  * different when dealing with a binary-compatible opfamily; in particular
00611  * anyarray_ops would never work without this.  Use em_datatype when
00612  * looking up a specific btree operator to work with this expression.
00613  */
00614 typedef struct EquivalenceMember
00615 {
00616     NodeTag     type;
00617 
00618     Expr       *em_expr;        /* the expression represented */
00619     Relids      em_relids;      /* all relids appearing in em_expr */
00620     Relids      em_nullable_relids;     /* nullable by lower outer joins */
00621     bool        em_is_const;    /* expression is pseudoconstant? */
00622     bool        em_is_child;    /* derived version for a child relation? */
00623     Oid         em_datatype;    /* the "nominal type" used by the opfamily */
00624 } EquivalenceMember;
00625 
00626 /*
00627  * PathKeys
00628  *
00629  * The sort ordering of a path is represented by a list of PathKey nodes.
00630  * An empty list implies no known ordering.  Otherwise the first item
00631  * represents the primary sort key, the second the first secondary sort key,
00632  * etc.  The value being sorted is represented by linking to an
00633  * EquivalenceClass containing that value and including pk_opfamily among its
00634  * ec_opfamilies.  The EquivalenceClass tells which collation to use, too.
00635  * This is a convenient method because it makes it trivial to detect
00636  * equivalent and closely-related orderings. (See optimizer/README for more
00637  * information.)
00638  *
00639  * Note: pk_strategy is either BTLessStrategyNumber (for ASC) or
00640  * BTGreaterStrategyNumber (for DESC).  We assume that all ordering-capable
00641  * index types will use btree-compatible strategy numbers.
00642  */
00643 typedef struct PathKey
00644 {
00645     NodeTag     type;
00646 
00647     EquivalenceClass *pk_eclass;    /* the value that is ordered */
00648     Oid         pk_opfamily;    /* btree opfamily defining the ordering */
00649     int         pk_strategy;    /* sort direction (ASC or DESC) */
00650     bool        pk_nulls_first; /* do NULLs come before normal values? */
00651 } PathKey;
00652 
00653 
00654 /*
00655  * ParamPathInfo
00656  *
00657  * All parameterized paths for a given relation with given required outer rels
00658  * link to a single ParamPathInfo, which stores common information such as
00659  * the estimated rowcount for this parameterization.  We do this partly to
00660  * avoid recalculations, but mostly to ensure that the estimated rowcount
00661  * is in fact the same for every such path.
00662  *
00663  * Note: ppi_clauses is only used in ParamPathInfos for base relation paths;
00664  * in join cases it's NIL because the set of relevant clauses varies depending
00665  * on how the join is formed.  The relevant clauses will appear in each
00666  * parameterized join path's joinrestrictinfo list, instead.
00667  */
00668 typedef struct ParamPathInfo
00669 {
00670     NodeTag     type;
00671 
00672     Relids      ppi_req_outer;  /* rels supplying parameters used by path */
00673     double      ppi_rows;       /* estimated number of result tuples */
00674     List       *ppi_clauses;    /* join clauses available from outer rels */
00675 } ParamPathInfo;
00676 
00677 
00678 /*
00679  * Type "Path" is used as-is for sequential-scan paths, as well as some other
00680  * simple plan types that we don't need any extra information in the path for.
00681  * For other path types it is the first component of a larger struct.
00682  *
00683  * "pathtype" is the NodeTag of the Plan node we could build from this Path.
00684  * It is partially redundant with the Path's NodeTag, but allows us to use
00685  * the same Path type for multiple Plan types when there is no need to
00686  * distinguish the Plan type during path processing.
00687  *
00688  * "param_info", if not NULL, links to a ParamPathInfo that identifies outer
00689  * relation(s) that provide parameter values to each scan of this path.
00690  * That means this path can only be joined to those rels by means of nestloop
00691  * joins with this path on the inside.  Also note that a parameterized path
00692  * is responsible for testing all "movable" joinclauses involving this rel
00693  * and the specified outer rel(s).
00694  *
00695  * "rows" is the same as parent->rows in simple paths, but in parameterized
00696  * paths and UniquePaths it can be less than parent->rows, reflecting the
00697  * fact that we've filtered by extra join conditions or removed duplicates.
00698  *
00699  * "pathkeys" is a List of PathKey nodes (see above), describing the sort
00700  * ordering of the path's output rows.
00701  */
00702 typedef struct Path
00703 {
00704     NodeTag     type;
00705 
00706     NodeTag     pathtype;       /* tag identifying scan/join method */
00707 
00708     RelOptInfo *parent;         /* the relation this path can build */
00709     ParamPathInfo *param_info;  /* parameterization info, or NULL if none */
00710 
00711     /* estimated size/costs for path (see costsize.c for more info) */
00712     double      rows;           /* estimated number of result tuples */
00713     Cost        startup_cost;   /* cost expended before fetching any tuples */
00714     Cost        total_cost;     /* total cost (assuming all tuples fetched) */
00715 
00716     List       *pathkeys;       /* sort ordering of path's output */
00717     /* pathkeys is a List of PathKey nodes; see above */
00718 } Path;
00719 
00720 /* Macro for extracting a path's parameterization relids; beware double eval */
00721 #define PATH_REQ_OUTER(path)  \
00722     ((path)->param_info ? (path)->param_info->ppi_req_outer : (Relids) NULL)
00723 
00724 /*----------
00725  * IndexPath represents an index scan over a single index.
00726  *
00727  * This struct is used for both regular indexscans and index-only scans;
00728  * path.pathtype is T_IndexScan or T_IndexOnlyScan to show which is meant.
00729  *
00730  * 'indexinfo' is the index to be scanned.
00731  *
00732  * 'indexclauses' is a list of index qualification clauses, with implicit
00733  * AND semantics across the list.  Each clause is a RestrictInfo node from
00734  * the query's WHERE or JOIN conditions.  An empty list implies a full
00735  * index scan.
00736  *
00737  * 'indexquals' has the same structure as 'indexclauses', but it contains
00738  * the actual index qual conditions that can be used with the index.
00739  * In simple cases this is identical to 'indexclauses', but when special
00740  * indexable operators appear in 'indexclauses', they are replaced by the
00741  * derived indexscannable conditions in 'indexquals'.
00742  *
00743  * 'indexqualcols' is an integer list of index column numbers (zero-based)
00744  * of the same length as 'indexquals', showing which index column each qual
00745  * is meant to be used with.  'indexquals' is required to be ordered by
00746  * index column, so 'indexqualcols' must form a nondecreasing sequence.
00747  * (The order of multiple quals for the same index column is unspecified.)
00748  *
00749  * 'indexorderbys', if not NIL, is a list of ORDER BY expressions that have
00750  * been found to be usable as ordering operators for an amcanorderbyop index.
00751  * The list must match the path's pathkeys, ie, one expression per pathkey
00752  * in the same order.  These are not RestrictInfos, just bare expressions,
00753  * since they generally won't yield booleans.  Also, unlike the case for
00754  * quals, it's guaranteed that each expression has the index key on the left
00755  * side of the operator.
00756  *
00757  * 'indexorderbycols' is an integer list of index column numbers (zero-based)
00758  * of the same length as 'indexorderbys', showing which index column each
00759  * ORDER BY expression is meant to be used with.  (There is no restriction
00760  * on which index column each ORDER BY can be used with.)
00761  *
00762  * 'indexscandir' is one of:
00763  *      ForwardScanDirection: forward scan of an ordered index
00764  *      BackwardScanDirection: backward scan of an ordered index
00765  *      NoMovementScanDirection: scan of an unordered index, or don't care
00766  * (The executor doesn't care whether it gets ForwardScanDirection or
00767  * NoMovementScanDirection for an indexscan, but the planner wants to
00768  * distinguish ordered from unordered indexes for building pathkeys.)
00769  *
00770  * 'indextotalcost' and 'indexselectivity' are saved in the IndexPath so that
00771  * we need not recompute them when considering using the same index in a
00772  * bitmap index/heap scan (see BitmapHeapPath).  The costs of the IndexPath
00773  * itself represent the costs of an IndexScan or IndexOnlyScan plan type.
00774  *----------
00775  */
00776 typedef struct IndexPath
00777 {
00778     Path        path;
00779     IndexOptInfo *indexinfo;
00780     List       *indexclauses;
00781     List       *indexquals;
00782     List       *indexqualcols;
00783     List       *indexorderbys;
00784     List       *indexorderbycols;
00785     ScanDirection indexscandir;
00786     Cost        indextotalcost;
00787     Selectivity indexselectivity;
00788 } IndexPath;
00789 
00790 /*
00791  * BitmapHeapPath represents one or more indexscans that generate TID bitmaps
00792  * instead of directly accessing the heap, followed by AND/OR combinations
00793  * to produce a single bitmap, followed by a heap scan that uses the bitmap.
00794  * Note that the output is always considered unordered, since it will come
00795  * out in physical heap order no matter what the underlying indexes did.
00796  *
00797  * The individual indexscans are represented by IndexPath nodes, and any
00798  * logic on top of them is represented by a tree of BitmapAndPath and
00799  * BitmapOrPath nodes.  Notice that we can use the same IndexPath node both
00800  * to represent a regular (or index-only) index scan plan, and as the child
00801  * of a BitmapHeapPath that represents scanning the same index using a
00802  * BitmapIndexScan.  The startup_cost and total_cost figures of an IndexPath
00803  * always represent the costs to use it as a regular (or index-only)
00804  * IndexScan.  The costs of a BitmapIndexScan can be computed using the
00805  * IndexPath's indextotalcost and indexselectivity.
00806  */
00807 typedef struct BitmapHeapPath
00808 {
00809     Path        path;
00810     Path       *bitmapqual;     /* IndexPath, BitmapAndPath, BitmapOrPath */
00811 } BitmapHeapPath;
00812 
00813 /*
00814  * BitmapAndPath represents a BitmapAnd plan node; it can only appear as
00815  * part of the substructure of a BitmapHeapPath.  The Path structure is
00816  * a bit more heavyweight than we really need for this, but for simplicity
00817  * we make it a derivative of Path anyway.
00818  */
00819 typedef struct BitmapAndPath
00820 {
00821     Path        path;
00822     List       *bitmapquals;    /* IndexPaths and BitmapOrPaths */
00823     Selectivity bitmapselectivity;
00824 } BitmapAndPath;
00825 
00826 /*
00827  * BitmapOrPath represents a BitmapOr plan node; it can only appear as
00828  * part of the substructure of a BitmapHeapPath.  The Path structure is
00829  * a bit more heavyweight than we really need for this, but for simplicity
00830  * we make it a derivative of Path anyway.
00831  */
00832 typedef struct BitmapOrPath
00833 {
00834     Path        path;
00835     List       *bitmapquals;    /* IndexPaths and BitmapAndPaths */
00836     Selectivity bitmapselectivity;
00837 } BitmapOrPath;
00838 
00839 /*
00840  * TidPath represents a scan by TID
00841  *
00842  * tidquals is an implicitly OR'ed list of qual expressions of the form
00843  * "CTID = pseudoconstant" or "CTID = ANY(pseudoconstant_array)".
00844  * Note they are bare expressions, not RestrictInfos.
00845  */
00846 typedef struct TidPath
00847 {
00848     Path        path;
00849     List       *tidquals;       /* qual(s) involving CTID = something */
00850 } TidPath;
00851 
00852 /*
00853  * ForeignPath represents a potential scan of a foreign table
00854  *
00855  * fdw_private stores FDW private data about the scan.  While fdw_private is
00856  * not actually touched by the core code during normal operations, it's
00857  * generally a good idea to use a representation that can be dumped by
00858  * nodeToString(), so that you can examine the structure during debugging
00859  * with tools like pprint().
00860  */
00861 typedef struct ForeignPath
00862 {
00863     Path        path;
00864     List       *fdw_private;
00865 } ForeignPath;
00866 
00867 /*
00868  * AppendPath represents an Append plan, ie, successive execution of
00869  * several member plans.
00870  *
00871  * Note: it is possible for "subpaths" to contain only one, or even no,
00872  * elements.  These cases are optimized during create_append_plan.
00873  * In particular, an AppendPath with no subpaths is a "dummy" path that
00874  * is created to represent the case that a relation is provably empty.
00875  */
00876 typedef struct AppendPath
00877 {
00878     Path        path;
00879     List       *subpaths;       /* list of component Paths */
00880 } AppendPath;
00881 
00882 #define IS_DUMMY_PATH(p) \
00883     (IsA((p), AppendPath) && ((AppendPath *) (p))->subpaths == NIL)
00884 
00885 /* A relation that's been proven empty will have one path that is dummy */
00886 #define IS_DUMMY_REL(r) \
00887     ((r)->cheapest_total_path != NULL && \
00888      IS_DUMMY_PATH((r)->cheapest_total_path))
00889 
00890 /*
00891  * MergeAppendPath represents a MergeAppend plan, ie, the merging of sorted
00892  * results from several member plans to produce similarly-sorted output.
00893  */
00894 typedef struct MergeAppendPath
00895 {
00896     Path        path;
00897     List       *subpaths;       /* list of component Paths */
00898     double      limit_tuples;   /* hard limit on output tuples, or -1 */
00899 } MergeAppendPath;
00900 
00901 /*
00902  * ResultPath represents use of a Result plan node to compute a variable-free
00903  * targetlist with no underlying tables (a "SELECT expressions" query).
00904  * The query could have a WHERE clause, too, represented by "quals".
00905  *
00906  * Note that quals is a list of bare clauses, not RestrictInfos.
00907  */
00908 typedef struct ResultPath
00909 {
00910     Path        path;
00911     List       *quals;
00912 } ResultPath;
00913 
00914 /*
00915  * MaterialPath represents use of a Material plan node, i.e., caching of
00916  * the output of its subpath.  This is used when the subpath is expensive
00917  * and needs to be scanned repeatedly, or when we need mark/restore ability
00918  * and the subpath doesn't have it.
00919  */
00920 typedef struct MaterialPath
00921 {
00922     Path        path;
00923     Path       *subpath;
00924 } MaterialPath;
00925 
00926 /*
00927  * UniquePath represents elimination of distinct rows from the output of
00928  * its subpath.
00929  *
00930  * This is unlike the other Path nodes in that it can actually generate
00931  * different plans: either hash-based or sort-based implementation, or a
00932  * no-op if the input path can be proven distinct already.  The decision
00933  * is sufficiently localized that it's not worth having separate Path node
00934  * types.  (Note: in the no-op case, we could eliminate the UniquePath node
00935  * entirely and just return the subpath; but it's convenient to have a
00936  * UniquePath in the path tree to signal upper-level routines that the input
00937  * is known distinct.)
00938  */
00939 typedef enum
00940 {
00941     UNIQUE_PATH_NOOP,           /* input is known unique already */
00942     UNIQUE_PATH_HASH,           /* use hashing */
00943     UNIQUE_PATH_SORT            /* use sorting */
00944 } UniquePathMethod;
00945 
00946 typedef struct UniquePath
00947 {
00948     Path        path;
00949     Path       *subpath;
00950     UniquePathMethod umethod;
00951     List       *in_operators;   /* equality operators of the IN clause */
00952     List       *uniq_exprs;     /* expressions to be made unique */
00953 } UniquePath;
00954 
00955 /*
00956  * All join-type paths share these fields.
00957  */
00958 
00959 typedef struct JoinPath
00960 {
00961     Path        path;
00962 
00963     JoinType    jointype;
00964 
00965     Path       *outerjoinpath;  /* path for the outer side of the join */
00966     Path       *innerjoinpath;  /* path for the inner side of the join */
00967 
00968     List       *joinrestrictinfo;       /* RestrictInfos to apply to join */
00969 
00970     /*
00971      * See the notes for RelOptInfo and ParamPathInfo to understand why
00972      * joinrestrictinfo is needed in JoinPath, and can't be merged into the
00973      * parent RelOptInfo.
00974      */
00975 } JoinPath;
00976 
00977 /*
00978  * A nested-loop path needs no special fields.
00979  */
00980 
00981 typedef JoinPath NestPath;
00982 
00983 /*
00984  * A mergejoin path has these fields.
00985  *
00986  * Unlike other path types, a MergePath node doesn't represent just a single
00987  * run-time plan node: it can represent up to four.  Aside from the MergeJoin
00988  * node itself, there can be a Sort node for the outer input, a Sort node
00989  * for the inner input, and/or a Material node for the inner input.  We could
00990  * represent these nodes by separate path nodes, but considering how many
00991  * different merge paths are investigated during a complex join problem,
00992  * it seems better to avoid unnecessary palloc overhead.
00993  *
00994  * path_mergeclauses lists the clauses (in the form of RestrictInfos)
00995  * that will be used in the merge.
00996  *
00997  * Note that the mergeclauses are a subset of the parent relation's
00998  * restriction-clause list.  Any join clauses that are not mergejoinable
00999  * appear only in the parent's restrict list, and must be checked by a
01000  * qpqual at execution time.
01001  *
01002  * outersortkeys (resp. innersortkeys) is NIL if the outer path
01003  * (resp. inner path) is already ordered appropriately for the
01004  * mergejoin.  If it is not NIL then it is a PathKeys list describing
01005  * the ordering that must be created by an explicit Sort node.
01006  *
01007  * materialize_inner is TRUE if a Material node should be placed atop the
01008  * inner input.  This may appear with or without an inner Sort step.
01009  */
01010 
01011 typedef struct MergePath
01012 {
01013     JoinPath    jpath;
01014     List       *path_mergeclauses;      /* join clauses to be used for merge */
01015     List       *outersortkeys;  /* keys for explicit sort, if any */
01016     List       *innersortkeys;  /* keys for explicit sort, if any */
01017     bool        materialize_inner;      /* add Materialize to inner? */
01018 } MergePath;
01019 
01020 /*
01021  * A hashjoin path has these fields.
01022  *
01023  * The remarks above for mergeclauses apply for hashclauses as well.
01024  *
01025  * Hashjoin does not care what order its inputs appear in, so we have
01026  * no need for sortkeys.
01027  */
01028 
01029 typedef struct HashPath
01030 {
01031     JoinPath    jpath;
01032     List       *path_hashclauses;       /* join clauses used for hashing */
01033     int         num_batches;    /* number of batches expected */
01034 } HashPath;
01035 
01036 /*
01037  * Restriction clause info.
01038  *
01039  * We create one of these for each AND sub-clause of a restriction condition
01040  * (WHERE or JOIN/ON clause).  Since the restriction clauses are logically
01041  * ANDed, we can use any one of them or any subset of them to filter out
01042  * tuples, without having to evaluate the rest.  The RestrictInfo node itself
01043  * stores data used by the optimizer while choosing the best query plan.
01044  *
01045  * If a restriction clause references a single base relation, it will appear
01046  * in the baserestrictinfo list of the RelOptInfo for that base rel.
01047  *
01048  * If a restriction clause references more than one base rel, it will
01049  * appear in the joininfo list of every RelOptInfo that describes a strict
01050  * subset of the base rels mentioned in the clause.  The joininfo lists are
01051  * used to drive join tree building by selecting plausible join candidates.
01052  * The clause cannot actually be applied until we have built a join rel
01053  * containing all the base rels it references, however.
01054  *
01055  * When we construct a join rel that includes all the base rels referenced
01056  * in a multi-relation restriction clause, we place that clause into the
01057  * joinrestrictinfo lists of paths for the join rel, if neither left nor
01058  * right sub-path includes all base rels referenced in the clause.  The clause
01059  * will be applied at that join level, and will not propagate any further up
01060  * the join tree.  (Note: the "predicate migration" code was once intended to
01061  * push restriction clauses up and down the plan tree based on evaluation
01062  * costs, but it's dead code and is unlikely to be resurrected in the
01063  * foreseeable future.)
01064  *
01065  * Note that in the presence of more than two rels, a multi-rel restriction
01066  * might reach different heights in the join tree depending on the join
01067  * sequence we use.  So, these clauses cannot be associated directly with
01068  * the join RelOptInfo, but must be kept track of on a per-join-path basis.
01069  *
01070  * RestrictInfos that represent equivalence conditions (i.e., mergejoinable
01071  * equalities that are not outerjoin-delayed) are handled a bit differently.
01072  * Initially we attach them to the EquivalenceClasses that are derived from
01073  * them.  When we construct a scan or join path, we look through all the
01074  * EquivalenceClasses and generate derived RestrictInfos representing the
01075  * minimal set of conditions that need to be checked for this particular scan
01076  * or join to enforce that all members of each EquivalenceClass are in fact
01077  * equal in all rows emitted by the scan or join.
01078  *
01079  * When dealing with outer joins we have to be very careful about pushing qual
01080  * clauses up and down the tree.  An outer join's own JOIN/ON conditions must
01081  * be evaluated exactly at that join node, unless they are "degenerate"
01082  * conditions that reference only Vars from the nullable side of the join.
01083  * Quals appearing in WHERE or in a JOIN above the outer join cannot be pushed
01084  * down below the outer join, if they reference any nullable Vars.
01085  * RestrictInfo nodes contain a flag to indicate whether a qual has been
01086  * pushed down to a lower level than its original syntactic placement in the
01087  * join tree would suggest.  If an outer join prevents us from pushing a qual
01088  * down to its "natural" semantic level (the level associated with just the
01089  * base rels used in the qual) then we mark the qual with a "required_relids"
01090  * value including more than just the base rels it actually uses.  By
01091  * pretending that the qual references all the rels required to form the outer
01092  * join, we prevent it from being evaluated below the outer join's joinrel.
01093  * When we do form the outer join's joinrel, we still need to distinguish
01094  * those quals that are actually in that join's JOIN/ON condition from those
01095  * that appeared elsewhere in the tree and were pushed down to the join rel
01096  * because they used no other rels.  That's what the is_pushed_down flag is
01097  * for; it tells us that a qual is not an OUTER JOIN qual for the set of base
01098  * rels listed in required_relids.  A clause that originally came from WHERE
01099  * or an INNER JOIN condition will *always* have its is_pushed_down flag set.
01100  * It's possible for an OUTER JOIN clause to be marked is_pushed_down too,
01101  * if we decide that it can be pushed down into the nullable side of the join.
01102  * In that case it acts as a plain filter qual for wherever it gets evaluated.
01103  * (In short, is_pushed_down is only false for non-degenerate outer join
01104  * conditions.  Possibly we should rename it to reflect that meaning?)
01105  *
01106  * RestrictInfo nodes also contain an outerjoin_delayed flag, which is true
01107  * if the clause's applicability must be delayed due to any outer joins
01108  * appearing below it (ie, it has to be postponed to some join level higher
01109  * than the set of relations it actually references).
01110  *
01111  * There is also an outer_relids field, which is NULL except for outer join
01112  * clauses; for those, it is the set of relids on the outer side of the
01113  * clause's outer join.  (These are rels that the clause cannot be applied to
01114  * in parameterized scans, since pushing it into the join's outer side would
01115  * lead to wrong answers.)
01116  *
01117  * There is also a nullable_relids field, which is the set of rels the clause
01118  * references that can be forced null by some outer join below the clause.
01119  *
01120  * outerjoin_delayed = true is subtly different from nullable_relids != NULL:
01121  * a clause might reference some nullable rels and yet not be
01122  * outerjoin_delayed because it also references all the other rels of the
01123  * outer join(s). A clause that is not outerjoin_delayed can be enforced
01124  * anywhere it is computable.
01125  *
01126  * In general, the referenced clause might be arbitrarily complex.  The
01127  * kinds of clauses we can handle as indexscan quals, mergejoin clauses,
01128  * or hashjoin clauses are limited (e.g., no volatile functions).  The code
01129  * for each kind of path is responsible for identifying the restrict clauses
01130  * it can use and ignoring the rest.  Clauses not implemented by an indexscan,
01131  * mergejoin, or hashjoin will be placed in the plan qual or joinqual field
01132  * of the finished Plan node, where they will be enforced by general-purpose
01133  * qual-expression-evaluation code.  (But we are still entitled to count
01134  * their selectivity when estimating the result tuple count, if we
01135  * can guess what it is...)
01136  *
01137  * When the referenced clause is an OR clause, we generate a modified copy
01138  * in which additional RestrictInfo nodes are inserted below the top-level
01139  * OR/AND structure.  This is a convenience for OR indexscan processing:
01140  * indexquals taken from either the top level or an OR subclause will have
01141  * associated RestrictInfo nodes.
01142  *
01143  * The can_join flag is set true if the clause looks potentially useful as
01144  * a merge or hash join clause, that is if it is a binary opclause with
01145  * nonoverlapping sets of relids referenced in the left and right sides.
01146  * (Whether the operator is actually merge or hash joinable isn't checked,
01147  * however.)
01148  *
01149  * The pseudoconstant flag is set true if the clause contains no Vars of
01150  * the current query level and no volatile functions.  Such a clause can be
01151  * pulled out and used as a one-time qual in a gating Result node.  We keep
01152  * pseudoconstant clauses in the same lists as other RestrictInfos so that
01153  * the regular clause-pushing machinery can assign them to the correct join
01154  * level, but they need to be treated specially for cost and selectivity
01155  * estimates.  Note that a pseudoconstant clause can never be an indexqual
01156  * or merge or hash join clause, so it's of no interest to large parts of
01157  * the planner.
01158  *
01159  * When join clauses are generated from EquivalenceClasses, there may be
01160  * several equally valid ways to enforce join equivalence, of which we need
01161  * apply only one.  We mark clauses of this kind by setting parent_ec to
01162  * point to the generating EquivalenceClass.  Multiple clauses with the same
01163  * parent_ec in the same join are redundant.
01164  */
01165 
01166 typedef struct RestrictInfo
01167 {
01168     NodeTag     type;
01169 
01170     Expr       *clause;         /* the represented clause of WHERE or JOIN */
01171 
01172     bool        is_pushed_down; /* TRUE if clause was pushed down in level */
01173 
01174     bool        outerjoin_delayed;      /* TRUE if delayed by lower outer join */
01175 
01176     bool        can_join;       /* see comment above */
01177 
01178     bool        pseudoconstant; /* see comment above */
01179 
01180     /* The set of relids (varnos) actually referenced in the clause: */
01181     Relids      clause_relids;
01182 
01183     /* The set of relids required to evaluate the clause: */
01184     Relids      required_relids;
01185 
01186     /* If an outer-join clause, the outer-side relations, else NULL: */
01187     Relids      outer_relids;
01188 
01189     /* The relids used in the clause that are nullable by lower outer joins: */
01190     Relids      nullable_relids;
01191 
01192     /* These fields are set for any binary opclause: */
01193     Relids      left_relids;    /* relids in left side of clause */
01194     Relids      right_relids;   /* relids in right side of clause */
01195 
01196     /* This field is NULL unless clause is an OR clause: */
01197     Expr       *orclause;       /* modified clause with RestrictInfos */
01198 
01199     /* This field is NULL unless clause is potentially redundant: */
01200     EquivalenceClass *parent_ec;    /* generating EquivalenceClass */
01201 
01202     /* cache space for cost and selectivity */
01203     QualCost    eval_cost;      /* eval cost of clause; -1 if not yet set */
01204     Selectivity norm_selec;     /* selectivity for "normal" (JOIN_INNER)
01205                                  * semantics; -1 if not yet set; >1 means a
01206                                  * redundant clause */
01207     Selectivity outer_selec;    /* selectivity for outer join semantics; -1 if
01208                                  * not yet set */
01209 
01210     /* valid if clause is mergejoinable, else NIL */
01211     List       *mergeopfamilies;    /* opfamilies containing clause operator */
01212 
01213     /* cache space for mergeclause processing; NULL if not yet set */
01214     EquivalenceClass *left_ec;  /* EquivalenceClass containing lefthand */
01215     EquivalenceClass *right_ec; /* EquivalenceClass containing righthand */
01216     EquivalenceMember *left_em; /* EquivalenceMember for lefthand */
01217     EquivalenceMember *right_em;    /* EquivalenceMember for righthand */
01218     List       *scansel_cache;  /* list of MergeScanSelCache structs */
01219 
01220     /* transient workspace for use while considering a specific join path */
01221     bool        outer_is_left;  /* T = outer var on left, F = on right */
01222 
01223     /* valid if clause is hashjoinable, else InvalidOid: */
01224     Oid         hashjoinoperator;       /* copy of clause operator */
01225 
01226     /* cache space for hashclause processing; -1 if not yet set */
01227     Selectivity left_bucketsize;    /* avg bucketsize of left side */
01228     Selectivity right_bucketsize;       /* avg bucketsize of right side */
01229 } RestrictInfo;
01230 
01231 /*
01232  * Since mergejoinscansel() is a relatively expensive function, and would
01233  * otherwise be invoked many times while planning a large join tree,
01234  * we go out of our way to cache its results.  Each mergejoinable
01235  * RestrictInfo carries a list of the specific sort orderings that have
01236  * been considered for use with it, and the resulting selectivities.
01237  */
01238 typedef struct MergeScanSelCache
01239 {
01240     /* Ordering details (cache lookup key) */
01241     Oid         opfamily;       /* btree opfamily defining the ordering */
01242     Oid         collation;      /* collation for the ordering */
01243     int         strategy;       /* sort direction (ASC or DESC) */
01244     bool        nulls_first;    /* do NULLs come before normal values? */
01245     /* Results */
01246     Selectivity leftstartsel;   /* first-join fraction for clause left side */
01247     Selectivity leftendsel;     /* last-join fraction for clause left side */
01248     Selectivity rightstartsel;  /* first-join fraction for clause right side */
01249     Selectivity rightendsel;    /* last-join fraction for clause right side */
01250 } MergeScanSelCache;
01251 
01252 /*
01253  * Placeholder node for an expression to be evaluated below the top level
01254  * of a plan tree.  This is used during planning to represent the contained
01255  * expression.  At the end of the planning process it is replaced by either
01256  * the contained expression or a Var referring to a lower-level evaluation of
01257  * the contained expression.  Typically the evaluation occurs below an outer
01258  * join, and Var references above the outer join might thereby yield NULL
01259  * instead of the expression value.
01260  *
01261  * Although the planner treats this as an expression node type, it is not
01262  * recognized by the parser or executor, so we declare it here rather than
01263  * in primnodes.h.
01264  */
01265 
01266 typedef struct PlaceHolderVar
01267 {
01268     Expr        xpr;
01269     Expr       *phexpr;         /* the represented expression */
01270     Relids      phrels;         /* base relids syntactically within expr src */
01271     Index       phid;           /* ID for PHV (unique within planner run) */
01272     Index       phlevelsup;     /* > 0 if PHV belongs to outer query */
01273 } PlaceHolderVar;
01274 
01275 /*
01276  * "Special join" info.
01277  *
01278  * One-sided outer joins constrain the order of joining partially but not
01279  * completely.  We flatten such joins into the planner's top-level list of
01280  * relations to join, but record information about each outer join in a
01281  * SpecialJoinInfo struct.  These structs are kept in the PlannerInfo node's
01282  * join_info_list.
01283  *
01284  * Similarly, semijoins and antijoins created by flattening IN (subselect)
01285  * and EXISTS(subselect) clauses create partial constraints on join order.
01286  * These are likewise recorded in SpecialJoinInfo structs.
01287  *
01288  * We make SpecialJoinInfos for FULL JOINs even though there is no flexibility
01289  * of planning for them, because this simplifies make_join_rel()'s API.
01290  *
01291  * min_lefthand and min_righthand are the sets of base relids that must be
01292  * available on each side when performing the special join.  lhs_strict is
01293  * true if the special join's condition cannot succeed when the LHS variables
01294  * are all NULL (this means that an outer join can commute with upper-level
01295  * outer joins even if it appears in their RHS).  We don't bother to set
01296  * lhs_strict for FULL JOINs, however.
01297  *
01298  * It is not valid for either min_lefthand or min_righthand to be empty sets;
01299  * if they were, this would break the logic that enforces join order.
01300  *
01301  * syn_lefthand and syn_righthand are the sets of base relids that are
01302  * syntactically below this special join.  (These are needed to help compute
01303  * min_lefthand and min_righthand for higher joins.)
01304  *
01305  * delay_upper_joins is set TRUE if we detect a pushed-down clause that has
01306  * to be evaluated after this join is formed (because it references the RHS).
01307  * Any outer joins that have such a clause and this join in their RHS cannot
01308  * commute with this join, because that would leave noplace to check the
01309  * pushed-down clause.  (We don't track this for FULL JOINs, either.)
01310  *
01311  * join_quals is an implicit-AND list of the quals syntactically associated
01312  * with the join (they may or may not end up being applied at the join level).
01313  * This is just a side list and does not drive actual application of quals.
01314  * For JOIN_SEMI joins, this is cleared to NIL in create_unique_path() if
01315  * the join is found not to be suitable for a uniqueify-the-RHS plan.
01316  *
01317  * jointype is never JOIN_RIGHT; a RIGHT JOIN is handled by switching
01318  * the inputs to make it a LEFT JOIN.  So the allowed values of jointype
01319  * in a join_info_list member are only LEFT, FULL, SEMI, or ANTI.
01320  *
01321  * For purposes of join selectivity estimation, we create transient
01322  * SpecialJoinInfo structures for regular inner joins; so it is possible
01323  * to have jointype == JOIN_INNER in such a structure, even though this is
01324  * not allowed within join_info_list.  We also create transient
01325  * SpecialJoinInfos with jointype == JOIN_INNER for outer joins, since for
01326  * cost estimation purposes it is sometimes useful to know the join size under
01327  * plain innerjoin semantics.  Note that lhs_strict, delay_upper_joins, and
01328  * join_quals are not set meaningfully within such structs.
01329  */
01330 
01331 typedef struct SpecialJoinInfo
01332 {
01333     NodeTag     type;
01334     Relids      min_lefthand;   /* base relids in minimum LHS for join */
01335     Relids      min_righthand;  /* base relids in minimum RHS for join */
01336     Relids      syn_lefthand;   /* base relids syntactically within LHS */
01337     Relids      syn_righthand;  /* base relids syntactically within RHS */
01338     JoinType    jointype;       /* always INNER, LEFT, FULL, SEMI, or ANTI */
01339     bool        lhs_strict;     /* joinclause is strict for some LHS rel */
01340     bool        delay_upper_joins;      /* can't commute with upper RHS */
01341     List       *join_quals;     /* join quals, in implicit-AND list format */
01342 } SpecialJoinInfo;
01343 
01344 /*
01345  * "Lateral join" info.
01346  *
01347  * Lateral references in subqueries constrain the join order in a way that's
01348  * somewhat like outer joins, though different in detail.  We construct one or
01349  * more LateralJoinInfos for each RTE with lateral references, and add them to
01350  * the PlannerInfo node's lateral_info_list.
01351  *
01352  * lateral_rhs is the relid of a baserel with lateral references, and
01353  * lateral_lhs is a set of relids of baserels it references, all of which
01354  * must be present on the LHS to compute a parameter needed by the RHS.
01355  * Typically, lateral_lhs is a singleton, but it can include multiple rels
01356  * if the RHS references a PlaceHolderVar with a multi-rel ph_eval_at level.
01357  * We disallow joining to only part of the LHS in such cases, since that would
01358  * result in a join tree with no convenient place to compute the PHV.
01359  *
01360  * When an appendrel contains lateral references (eg "LATERAL (SELECT x.col1
01361  * UNION ALL SELECT y.col2)"), the LateralJoinInfos reference the parent
01362  * baserel not the member otherrels, since it is the parent relid that is
01363  * considered for joining purposes.
01364  */
01365 
01366 typedef struct LateralJoinInfo
01367 {
01368     NodeTag     type;
01369     Index       lateral_rhs;    /* a baserel containing lateral refs */
01370     Relids      lateral_lhs;    /* some base relids it references */
01371 } LateralJoinInfo;
01372 
01373 /*
01374  * Append-relation info.
01375  *
01376  * When we expand an inheritable table or a UNION-ALL subselect into an
01377  * "append relation" (essentially, a list of child RTEs), we build an
01378  * AppendRelInfo for each child RTE.  The list of AppendRelInfos indicates
01379  * which child RTEs must be included when expanding the parent, and each
01380  * node carries information needed to translate Vars referencing the parent
01381  * into Vars referencing that child.
01382  *
01383  * These structs are kept in the PlannerInfo node's append_rel_list.
01384  * Note that we just throw all the structs into one list, and scan the
01385  * whole list when desiring to expand any one parent.  We could have used
01386  * a more complex data structure (eg, one list per parent), but this would
01387  * be harder to update during operations such as pulling up subqueries,
01388  * and not really any easier to scan.  Considering that typical queries
01389  * will not have many different append parents, it doesn't seem worthwhile
01390  * to complicate things.
01391  *
01392  * Note: after completion of the planner prep phase, any given RTE is an
01393  * append parent having entries in append_rel_list if and only if its
01394  * "inh" flag is set.  We clear "inh" for plain tables that turn out not
01395  * to have inheritance children, and (in an abuse of the original meaning
01396  * of the flag) we set "inh" for subquery RTEs that turn out to be
01397  * flattenable UNION ALL queries.  This lets us avoid useless searches
01398  * of append_rel_list.
01399  *
01400  * Note: the data structure assumes that append-rel members are single
01401  * baserels.  This is OK for inheritance, but it prevents us from pulling
01402  * up a UNION ALL member subquery if it contains a join.  While that could
01403  * be fixed with a more complex data structure, at present there's not much
01404  * point because no improvement in the plan could result.
01405  */
01406 
01407 typedef struct AppendRelInfo
01408 {
01409     NodeTag     type;
01410 
01411     /*
01412      * These fields uniquely identify this append relationship.  There can be
01413      * (in fact, always should be) multiple AppendRelInfos for the same
01414      * parent_relid, but never more than one per child_relid, since a given
01415      * RTE cannot be a child of more than one append parent.
01416      */
01417     Index       parent_relid;   /* RT index of append parent rel */
01418     Index       child_relid;    /* RT index of append child rel */
01419 
01420     /*
01421      * For an inheritance appendrel, the parent and child are both regular
01422      * relations, and we store their rowtype OIDs here for use in translating
01423      * whole-row Vars.  For a UNION-ALL appendrel, the parent and child are
01424      * both subqueries with no named rowtype, and we store InvalidOid here.
01425      */
01426     Oid         parent_reltype; /* OID of parent's composite type */
01427     Oid         child_reltype;  /* OID of child's composite type */
01428 
01429     /*
01430      * The N'th element of this list is a Var or expression representing the
01431      * child column corresponding to the N'th column of the parent. This is
01432      * used to translate Vars referencing the parent rel into references to
01433      * the child.  A list element is NULL if it corresponds to a dropped
01434      * column of the parent (this is only possible for inheritance cases, not
01435      * UNION ALL).  The list elements are always simple Vars for inheritance
01436      * cases, but can be arbitrary expressions in UNION ALL cases.
01437      *
01438      * Notice we only store entries for user columns (attno > 0).  Whole-row
01439      * Vars are special-cased, and system columns (attno < 0) need no special
01440      * translation since their attnos are the same for all tables.
01441      *
01442      * Caution: the Vars have varlevelsup = 0.  Be careful to adjust as needed
01443      * when copying into a subquery.
01444      */
01445     List       *translated_vars;    /* Expressions in the child's Vars */
01446 
01447     /*
01448      * We store the parent table's OID here for inheritance, or InvalidOid for
01449      * UNION ALL.  This is only needed to help in generating error messages if
01450      * an attempt is made to reference a dropped parent column.
01451      */
01452     Oid         parent_reloid;  /* OID of parent relation */
01453 } AppendRelInfo;
01454 
01455 /*
01456  * For each distinct placeholder expression generated during planning, we
01457  * store a PlaceHolderInfo node in the PlannerInfo node's placeholder_list.
01458  * This stores info that is needed centrally rather than in each copy of the
01459  * PlaceHolderVar.  The phid fields identify which PlaceHolderInfo goes with
01460  * each PlaceHolderVar.  Note that phid is unique throughout a planner run,
01461  * not just within a query level --- this is so that we need not reassign ID's
01462  * when pulling a subquery into its parent.
01463  *
01464  * The idea is to evaluate the expression at (only) the ph_eval_at join level,
01465  * then allow it to bubble up like a Var until the ph_needed join level.
01466  * ph_needed has the same definition as attr_needed for a regular Var.
01467  *
01468  * ph_may_need is an initial estimate of ph_needed, formed using the
01469  * syntactic locations of references to the PHV.  We need this in order to
01470  * determine whether the PHV reference forces a join ordering constraint:
01471  * if the PHV has to be evaluated below the nullable side of an outer join,
01472  * and then used above that outer join, we must constrain join order to ensure
01473  * there's a valid place to evaluate the PHV below the join.  The final
01474  * actual ph_needed level might be lower than ph_may_need, but we can't
01475  * determine that until later on.  Fortunately this doesn't matter for what
01476  * we need ph_may_need for: if there's a PHV reference syntactically
01477  * above the outer join, it's not going to be allowed to drop below the outer
01478  * join, so we would come to the same conclusions about join order even if
01479  * we had the final ph_needed value to compare to.
01480  *
01481  * We create a PlaceHolderInfo only after determining that the PlaceHolderVar
01482  * is actually referenced in the plan tree, so that unreferenced placeholders
01483  * don't result in unnecessary constraints on join order.
01484  */
01485 
01486 typedef struct PlaceHolderInfo
01487 {
01488     NodeTag     type;
01489 
01490     Index       phid;           /* ID for PH (unique within planner run) */
01491     PlaceHolderVar *ph_var;     /* copy of PlaceHolderVar tree */
01492     Relids      ph_eval_at;     /* lowest level we can evaluate value at */
01493     Relids      ph_needed;      /* highest level the value is needed at */
01494     Relids      ph_may_need;    /* highest level it might be needed at */
01495     int32       ph_width;       /* estimated attribute width */
01496 } PlaceHolderInfo;
01497 
01498 /*
01499  * For each potentially index-optimizable MIN/MAX aggregate function,
01500  * root->minmax_aggs stores a MinMaxAggInfo describing it.
01501  */
01502 typedef struct MinMaxAggInfo
01503 {
01504     NodeTag     type;
01505 
01506     Oid         aggfnoid;       /* pg_proc Oid of the aggregate */
01507     Oid         aggsortop;      /* Oid of its sort operator */
01508     Expr       *target;         /* expression we are aggregating on */
01509     PlannerInfo *subroot;       /* modified "root" for planning the subquery */
01510     Path       *path;           /* access path for subquery */
01511     Cost        pathcost;       /* estimated cost to fetch first row */
01512     Param      *param;          /* param for subplan's output */
01513 } MinMaxAggInfo;
01514 
01515 /*
01516  * At runtime, PARAM_EXEC slots are used to pass values around from one plan
01517  * node to another.  They can be used to pass values down into subqueries (for
01518  * outer references in subqueries), or up out of subqueries (for the results
01519  * of a subplan), or from a NestLoop plan node into its inner relation (when
01520  * the inner scan is parameterized with values from the outer relation).
01521  * The planner is responsible for assigning nonconflicting PARAM_EXEC IDs to
01522  * the PARAM_EXEC Params it generates.
01523  *
01524  * Outer references are managed via root->plan_params, which is a list of
01525  * PlannerParamItems.  While planning a subquery, each parent query level's
01526  * plan_params contains the values required from it by the current subquery.
01527  * During create_plan(), we use plan_params to track values that must be
01528  * passed from outer to inner sides of NestLoop plan nodes.
01529  *
01530  * The item a PlannerParamItem represents can be one of three kinds:
01531  *
01532  * A Var: the slot represents a variable of this level that must be passed
01533  * down because subqueries have outer references to it, or must be passed
01534  * from a NestLoop node to its inner scan.  The varlevelsup value in the Var
01535  * will always be zero.
01536  *
01537  * A PlaceHolderVar: this works much like the Var case, except that the
01538  * entry is a PlaceHolderVar node with a contained expression.  The PHV
01539  * will have phlevelsup = 0, and the contained expression is adjusted
01540  * to match in level.
01541  *
01542  * An Aggref (with an expression tree representing its argument): the slot
01543  * represents an aggregate expression that is an outer reference for some
01544  * subquery.  The Aggref itself has agglevelsup = 0, and its argument tree
01545  * is adjusted to match in level.
01546  *
01547  * Note: we detect duplicate Var and PlaceHolderVar parameters and coalesce
01548  * them into one slot, but we do not bother to do that for Aggrefs.
01549  * The scope of duplicate-elimination only extends across the set of
01550  * parameters passed from one query level into a single subquery, or for
01551  * nestloop parameters across the set of nestloop parameters used in a single
01552  * query level.  So there is no possibility of a PARAM_EXEC slot being used
01553  * for conflicting purposes.
01554  *
01555  * In addition, PARAM_EXEC slots are assigned for Params representing outputs
01556  * from subplans (values that are setParam items for those subplans).  These
01557  * IDs need not be tracked via PlannerParamItems, since we do not need any
01558  * duplicate-elimination nor later processing of the represented expressions.
01559  * Instead, we just record the assignment of the slot number by incrementing
01560  * root->glob->nParamExec.
01561  */
01562 typedef struct PlannerParamItem
01563 {
01564     NodeTag     type;
01565 
01566     Node       *item;           /* the Var, PlaceHolderVar, or Aggref */
01567     int         paramId;        /* its assigned PARAM_EXEC slot number */
01568 } PlannerParamItem;
01569 
01570 /*
01571  * When making cost estimates for a SEMI or ANTI join, there are some
01572  * correction factors that are needed in both nestloop and hash joins
01573  * to account for the fact that the executor can stop scanning inner rows
01574  * as soon as it finds a match to the current outer row.  These numbers
01575  * depend only on the selected outer and inner join relations, not on the
01576  * particular paths used for them, so it's worthwhile to calculate them
01577  * just once per relation pair not once per considered path.  This struct
01578  * is filled by compute_semi_anti_join_factors and must be passed along
01579  * to the join cost estimation functions.
01580  *
01581  * outer_match_frac is the fraction of the outer tuples that are
01582  *      expected to have at least one match.
01583  * match_count is the average number of matches expected for
01584  *      outer tuples that have at least one match.
01585  */
01586 typedef struct SemiAntiJoinFactors
01587 {
01588     Selectivity outer_match_frac;
01589     Selectivity match_count;
01590 } SemiAntiJoinFactors;
01591 
01592 /*
01593  * For speed reasons, cost estimation for join paths is performed in two
01594  * phases: the first phase tries to quickly derive a lower bound for the
01595  * join cost, and then we check if that's sufficient to reject the path.
01596  * If not, we come back for a more refined cost estimate.  The first phase
01597  * fills a JoinCostWorkspace struct with its preliminary cost estimates
01598  * and possibly additional intermediate values.  The second phase takes
01599  * these values as inputs to avoid repeating work.
01600  *
01601  * (Ideally we'd declare this in cost.h, but it's also needed in pathnode.h,
01602  * so seems best to put it here.)
01603  */
01604 typedef struct JoinCostWorkspace
01605 {
01606     /* Preliminary cost estimates --- must not be larger than final ones! */
01607     Cost        startup_cost;   /* cost expended before fetching any tuples */
01608     Cost        total_cost;     /* total cost (assuming all tuples fetched) */
01609 
01610     /* Fields below here should be treated as private to costsize.c */
01611     Cost        run_cost;       /* non-startup cost components */
01612 
01613     /* private for cost_nestloop code */
01614     Cost        inner_rescan_run_cost;
01615     double      outer_matched_rows;
01616     Selectivity inner_scan_frac;
01617 
01618     /* private for cost_mergejoin code */
01619     Cost        inner_run_cost;
01620     double      outer_rows;
01621     double      inner_rows;
01622     double      outer_skip_rows;
01623     double      inner_skip_rows;
01624 
01625     /* private for cost_hashjoin code */
01626     int         numbuckets;
01627     int         numbatches;
01628 } JoinCostWorkspace;
01629 
01630 #endif   /* RELATION_H */