gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

2502 rindas
104KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * pathnodes.h
  4. * Definitions for planner's internal data structures, especially Paths.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/nodes/pathnodes.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef PATHNODES_H
  15. #define PATHNODES_H
  16. #include "access/sdir.h"
  17. #include "fmgr.h"
  18. #include "lib/stringinfo.h"
  19. #include "nodes/params.h"
  20. #include "nodes/parsenodes.h"
  21. #include "storage/block.h"
  22. /*
  23. * Relids
  24. * Set of relation identifiers (indexes into the rangetable).
  25. */
  26. typedef Bitmapset *Relids;
  27. /*
  28. * When looking for a "cheapest path", this enum specifies whether we want
  29. * cheapest startup cost or cheapest total cost.
  30. */
  31. typedef enum CostSelector
  32. {
  33. STARTUP_COST, TOTAL_COST
  34. } CostSelector;
  35. /*
  36. * The cost estimate produced by cost_qual_eval() includes both a one-time
  37. * (startup) cost, and a per-tuple cost.
  38. */
  39. typedef struct QualCost
  40. {
  41. Cost startup; /* one-time cost */
  42. Cost per_tuple; /* per-evaluation cost */
  43. } QualCost;
  44. /*
  45. * Costing aggregate function execution requires these statistics about
  46. * the aggregates to be executed by a given Agg node. Note that the costs
  47. * include the execution costs of the aggregates' argument expressions as
  48. * well as the aggregate functions themselves. Also, the fields must be
  49. * defined so that initializing the struct to zeroes with memset is correct.
  50. */
  51. typedef struct AggClauseCosts
  52. {
  53. int numAggs; /* total number of aggregate functions */
  54. int numOrderedAggs; /* number w/ DISTINCT/ORDER BY/WITHIN GROUP */
  55. bool hasNonPartial; /* does any agg not support partial mode? */
  56. bool hasNonSerial; /* is any partial agg non-serializable? */
  57. QualCost transCost; /* total per-input-row execution costs */
  58. QualCost finalCost; /* total per-aggregated-row costs */
  59. Size transitionSpace; /* space for pass-by-ref transition data */
  60. } AggClauseCosts;
  61. /*
  62. * This enum identifies the different types of "upper" (post-scan/join)
  63. * relations that we might deal with during planning.
  64. */
  65. typedef enum UpperRelationKind
  66. {
  67. UPPERREL_SETOP, /* result of UNION/INTERSECT/EXCEPT, if any */
  68. UPPERREL_PARTIAL_GROUP_AGG, /* result of partial grouping/aggregation, if
  69. * any */
  70. UPPERREL_GROUP_AGG, /* result of grouping/aggregation, if any */
  71. UPPERREL_WINDOW, /* result of window functions, if any */
  72. UPPERREL_DISTINCT, /* result of "SELECT DISTINCT", if any */
  73. UPPERREL_ORDERED, /* result of ORDER BY, if any */
  74. UPPERREL_FINAL /* result of any remaining top-level actions */
  75. /* NB: UPPERREL_FINAL must be last enum entry; it's used to size arrays */
  76. } UpperRelationKind;
  77. /*
  78. * This enum identifies which type of relation is being planned through the
  79. * inheritance planner. INHKIND_NONE indicates the inheritance planner
  80. * was not used.
  81. */
  82. typedef enum InheritanceKind
  83. {
  84. INHKIND_NONE,
  85. INHKIND_INHERITED,
  86. INHKIND_PARTITIONED
  87. } InheritanceKind;
  88. /*----------
  89. * PlannerGlobal
  90. * Global information for planning/optimization
  91. *
  92. * PlannerGlobal holds state for an entire planner invocation; this state
  93. * is shared across all levels of sub-Queries that exist in the command being
  94. * planned.
  95. *----------
  96. */
  97. typedef struct PlannerGlobal
  98. {
  99. NodeTag type;
  100. ParamListInfo boundParams; /* Param values provided to planner() */
  101. List *subplans; /* Plans for SubPlan nodes */
  102. List *subroots; /* PlannerInfos for SubPlan nodes */
  103. Bitmapset *rewindPlanIDs; /* indices of subplans that require REWIND */
  104. List *finalrtable; /* "flat" rangetable for executor */
  105. List *finalrowmarks; /* "flat" list of PlanRowMarks */
  106. List *resultRelations; /* "flat" list of integer RT indexes */
  107. List *rootResultRelations; /* "flat" list of integer RT indexes */
  108. List *relationOids; /* OIDs of relations the plan depends on */
  109. List *invalItems; /* other dependencies, as PlanInvalItems */
  110. List *paramExecTypes; /* type OIDs for PARAM_EXEC Params */
  111. Index lastPHId; /* highest PlaceHolderVar ID assigned */
  112. Index lastRowMarkId; /* highest PlanRowMark ID assigned */
  113. int lastPlanNodeId; /* highest plan node ID assigned */
  114. bool transientPlan; /* redo plan when TransactionXmin changes? */
  115. bool dependsOnRole; /* is plan specific to current role? */
  116. bool parallelModeOK; /* parallel mode potentially OK? */
  117. bool parallelModeNeeded; /* parallel mode actually required? */
  118. char maxParallelHazard; /* worst PROPARALLEL hazard level */
  119. PartitionDirectory partition_directory; /* partition descriptors */
  120. } PlannerGlobal;
  121. /* macro for fetching the Plan associated with a SubPlan node */
  122. #define planner_subplan_get_plan(root, subplan) \
  123. ((Plan *) list_nth((root)->glob->subplans, (subplan)->plan_id - 1))
  124. /*----------
  125. * PlannerInfo
  126. * Per-query information for planning/optimization
  127. *
  128. * This struct is conventionally called "root" in all the planner routines.
  129. * It holds links to all of the planner's working state, in addition to the
  130. * original Query. Note that at present the planner extensively modifies
  131. * the passed-in Query data structure; someday that should stop.
  132. *
  133. * For reasons explained in optimizer/optimizer.h, we define the typedef
  134. * either here or in that header, whichever is read first.
  135. *----------
  136. */
  137. #ifndef HAVE_PLANNERINFO_TYPEDEF
  138. typedef struct PlannerInfo PlannerInfo;
  139. #define HAVE_PLANNERINFO_TYPEDEF 1
  140. #endif
  141. struct PlannerInfo
  142. {
  143. NodeTag type;
  144. Query *parse; /* the Query being planned */
  145. PlannerGlobal *glob; /* global info for current planner run */
  146. Index query_level; /* 1 at the outermost Query */
  147. PlannerInfo *parent_root; /* NULL at outermost Query */
  148. /*
  149. * plan_params contains the expressions that this query level needs to
  150. * make available to a lower query level that is currently being planned.
  151. * outer_params contains the paramIds of PARAM_EXEC Params that outer
  152. * query levels will make available to this query level.
  153. */
  154. List *plan_params; /* list of PlannerParamItems, see below */
  155. Bitmapset *outer_params;
  156. /*
  157. * simple_rel_array holds pointers to "base rels" and "other rels" (see
  158. * comments for RelOptInfo for more info). It is indexed by rangetable
  159. * index (so entry 0 is always wasted). Entries can be NULL when an RTE
  160. * does not correspond to a base relation, such as a join RTE or an
  161. * unreferenced view RTE; or if the RelOptInfo hasn't been made yet.
  162. */
  163. struct RelOptInfo **simple_rel_array; /* All 1-rel RelOptInfos */
  164. int simple_rel_array_size; /* allocated size of array */
  165. /*
  166. * simple_rte_array is the same length as simple_rel_array and holds
  167. * pointers to the associated rangetable entries. This lets us avoid
  168. * rt_fetch(), which can be a bit slow once large inheritance sets have
  169. * been expanded.
  170. */
  171. RangeTblEntry **simple_rte_array; /* rangetable as an array */
  172. /*
  173. * append_rel_array is the same length as the above arrays, and holds
  174. * pointers to the corresponding AppendRelInfo entry indexed by
  175. * child_relid, or NULL if none. The array itself is not allocated if
  176. * append_rel_list is empty.
  177. */
  178. struct AppendRelInfo **append_rel_array;
  179. /*
  180. * all_baserels is a Relids set of all base relids (but not "other"
  181. * relids) in the query; that is, the Relids identifier of the final join
  182. * we need to form. This is computed in make_one_rel, just before we
  183. * start making Paths.
  184. */
  185. Relids all_baserels;
  186. /*
  187. * nullable_baserels is a Relids set of base relids that are nullable by
  188. * some outer join in the jointree; these are rels that are potentially
  189. * nullable below the WHERE clause, SELECT targetlist, etc. This is
  190. * computed in deconstruct_jointree.
  191. */
  192. Relids nullable_baserels;
  193. /*
  194. * join_rel_list is a list of all join-relation RelOptInfos we have
  195. * considered in this planning run. For small problems we just scan the
  196. * list to do lookups, but when there are many join relations we build a
  197. * hash table for faster lookups. The hash table is present and valid
  198. * when join_rel_hash is not NULL. Note that we still maintain the list
  199. * even when using the hash table for lookups; this simplifies life for
  200. * GEQO.
  201. */
  202. List *join_rel_list; /* list of join-relation RelOptInfos */
  203. struct HTAB *join_rel_hash; /* optional hashtable for join relations */
  204. /*
  205. * When doing a dynamic-programming-style join search, join_rel_level[k]
  206. * is a list of all join-relation RelOptInfos of level k, and
  207. * join_cur_level is the current level. New join-relation RelOptInfos are
  208. * automatically added to the join_rel_level[join_cur_level] list.
  209. * join_rel_level is NULL if not in use.
  210. */
  211. List **join_rel_level; /* lists of join-relation RelOptInfos */
  212. int join_cur_level; /* index of list being extended */
  213. List *init_plans; /* init SubPlans for query */
  214. List *cte_plan_ids; /* per-CTE-item list of subplan IDs */
  215. List *multiexpr_params; /* List of Lists of Params for MULTIEXPR
  216. * subquery outputs */
  217. List *eq_classes; /* list of active EquivalenceClasses */
  218. List *canon_pathkeys; /* list of "canonical" PathKeys */
  219. List *left_join_clauses; /* list of RestrictInfos for mergejoinable
  220. * outer join clauses w/nonnullable var on
  221. * left */
  222. List *right_join_clauses; /* list of RestrictInfos for mergejoinable
  223. * outer join clauses w/nonnullable var on
  224. * right */
  225. List *full_join_clauses; /* list of RestrictInfos for mergejoinable
  226. * full join clauses */
  227. List *join_info_list; /* list of SpecialJoinInfos */
  228. /*
  229. * Note: for AppendRelInfos describing partitions of a partitioned table,
  230. * we guarantee that partitions that come earlier in the partitioned
  231. * table's PartitionDesc will appear earlier in append_rel_list.
  232. */
  233. List *append_rel_list; /* list of AppendRelInfos */
  234. List *rowMarks; /* list of PlanRowMarks */
  235. List *placeholder_list; /* list of PlaceHolderInfos */
  236. List *fkey_list; /* list of ForeignKeyOptInfos */
  237. List *query_pathkeys; /* desired pathkeys for query_planner() */
  238. List *group_pathkeys; /* groupClause pathkeys, if any */
  239. List *window_pathkeys; /* pathkeys of bottom window, if any */
  240. List *distinct_pathkeys; /* distinctClause pathkeys, if any */
  241. List *sort_pathkeys; /* sortClause pathkeys, if any */
  242. List *part_schemes; /* Canonicalised partition schemes used in the
  243. * query. */
  244. List *initial_rels; /* RelOptInfos we are now trying to join */
  245. /* Use fetch_upper_rel() to get any particular upper rel */
  246. List *upper_rels[UPPERREL_FINAL + 1]; /* upper-rel RelOptInfos */
  247. /* Result tlists chosen by grouping_planner for upper-stage processing */
  248. struct PathTarget *upper_targets[UPPERREL_FINAL + 1];
  249. /*
  250. * The fully-processed targetlist is kept here. It differs from
  251. * parse->targetList in that (for INSERT and UPDATE) it's been reordered
  252. * to match the target table, and defaults have been filled in. Also,
  253. * additional resjunk targets may be present. preprocess_targetlist()
  254. * does most of this work, but note that more resjunk targets can get
  255. * added during appendrel expansion. (Hence, upper_targets mustn't get
  256. * set up till after that.)
  257. */
  258. List *processed_tlist;
  259. /* Fields filled during create_plan() for use in setrefs.c */
  260. AttrNumber *grouping_map; /* for GroupingFunc fixup */
  261. List *minmax_aggs; /* List of MinMaxAggInfos */
  262. MemoryContext planner_cxt; /* context holding PlannerInfo */
  263. double total_table_pages; /* # of pages in all non-dummy tables of
  264. * query */
  265. double tuple_fraction; /* tuple_fraction passed to query_planner */
  266. double limit_tuples; /* limit_tuples passed to query_planner */
  267. Index qual_security_level; /* minimum security_level for quals */
  268. /* Note: qual_security_level is zero if there are no securityQuals */
  269. InheritanceKind inhTargetKind; /* indicates if the target relation is an
  270. * inheritance child or partition or a
  271. * partitioned table */
  272. bool hasJoinRTEs; /* true if any RTEs are RTE_JOIN kind */
  273. bool hasLateralRTEs; /* true if any RTEs are marked LATERAL */
  274. bool hasHavingQual; /* true if havingQual was non-null */
  275. bool hasPseudoConstantQuals; /* true if any RestrictInfo has
  276. * pseudoconstant = true */
  277. bool hasRecursion; /* true if planning a recursive WITH item */
  278. /* These fields are used only when hasRecursion is true: */
  279. int wt_param_id; /* PARAM_EXEC ID for the work table */
  280. struct Path *non_recursive_path; /* a path for non-recursive term */
  281. /* These fields are workspace for createplan.c */
  282. Relids curOuterRels; /* outer rels above current node */
  283. List *curOuterParams; /* not-yet-assigned NestLoopParams */
  284. /* optional private data for join_search_hook, e.g., GEQO */
  285. void *join_search_private;
  286. /* Does this query modify any partition key columns? */
  287. bool partColsUpdated;
  288. };
  289. /*
  290. * In places where it's known that simple_rte_array[] must have been prepared
  291. * already, we just index into it to fetch RTEs. In code that might be
  292. * executed before or after entering query_planner(), use this macro.
  293. */
  294. #define planner_rt_fetch(rti, root) \
  295. ((root)->simple_rte_array ? (root)->simple_rte_array[rti] : \
  296. rt_fetch(rti, (root)->parse->rtable))
  297. /*
  298. * If multiple relations are partitioned the same way, all such partitions
  299. * will have a pointer to the same PartitionScheme. A list of PartitionScheme
  300. * objects is attached to the PlannerInfo. By design, the partition scheme
  301. * incorporates only the general properties of the partition method (LIST vs.
  302. * RANGE, number of partitioning columns and the type information for each)
  303. * and not the specific bounds.
  304. *
  305. * We store the opclass-declared input data types instead of the partition key
  306. * datatypes since the former rather than the latter are used to compare
  307. * partition bounds. Since partition key data types and the opclass declared
  308. * input data types are expected to be binary compatible (per ResolveOpClass),
  309. * both of those should have same byval and length properties.
  310. */
  311. typedef struct PartitionSchemeData
  312. {
  313. char strategy; /* partition strategy */
  314. int16 partnatts; /* number of partition attributes */
  315. Oid *partopfamily; /* OIDs of operator families */
  316. Oid *partopcintype; /* OIDs of opclass declared input data types */
  317. Oid *partcollation; /* OIDs of partitioning collations */
  318. /* Cached information about partition key data types. */
  319. int16 *parttyplen;
  320. bool *parttypbyval;
  321. /* Cached information about partition comparison functions. */
  322. FmgrInfo *partsupfunc;
  323. } PartitionSchemeData;
  324. typedef struct PartitionSchemeData *PartitionScheme;
  325. /*----------
  326. * RelOptInfo
  327. * Per-relation information for planning/optimization
  328. *
  329. * For planning purposes, a "base rel" is either a plain relation (a table)
  330. * or the output of a sub-SELECT or function that appears in the range table.
  331. * In either case it is uniquely identified by an RT index. A "joinrel"
  332. * is the joining of two or more base rels. A joinrel is identified by
  333. * the set of RT indexes for its component baserels. We create RelOptInfo
  334. * nodes for each baserel and joinrel, and store them in the PlannerInfo's
  335. * simple_rel_array and join_rel_list respectively.
  336. *
  337. * Note that there is only one joinrel for any given set of component
  338. * baserels, no matter what order we assemble them in; so an unordered
  339. * set is the right datatype to identify it with.
  340. *
  341. * We also have "other rels", which are like base rels in that they refer to
  342. * single RT indexes; but they are not part of the join tree, and are given
  343. * a different RelOptKind to identify them.
  344. * Currently the only kind of otherrels are those made for member relations
  345. * of an "append relation", that is an inheritance set or UNION ALL subquery.
  346. * An append relation has a parent RTE that is a base rel, which represents
  347. * the entire append relation. The member RTEs are otherrels. The parent
  348. * is present in the query join tree but the members are not. The member
  349. * RTEs and otherrels are used to plan the scans of the individual tables or
  350. * subqueries of the append set; then the parent baserel is given Append
  351. * and/or MergeAppend paths comprising the best paths for the individual
  352. * member rels. (See comments for AppendRelInfo for more information.)
  353. *
  354. * At one time we also made otherrels to represent join RTEs, for use in
  355. * handling join alias Vars. Currently this is not needed because all join
  356. * alias Vars are expanded to non-aliased form during preprocess_expression.
  357. *
  358. * We also have relations representing joins between child relations of
  359. * different partitioned tables. These relations are not added to
  360. * join_rel_level lists as they are not joined directly by the dynamic
  361. * programming algorithm.
  362. *
  363. * There is also a RelOptKind for "upper" relations, which are RelOptInfos
  364. * that describe post-scan/join processing steps, such as aggregation.
  365. * Many of the fields in these RelOptInfos are meaningless, but their Path
  366. * fields always hold Paths showing ways to do that processing step.
  367. *
  368. * Lastly, there is a RelOptKind for "dead" relations, which are base rels
  369. * that we have proven we don't need to join after all.
  370. *
  371. * Parts of this data structure are specific to various scan and join
  372. * mechanisms. It didn't seem worth creating new node types for them.
  373. *
  374. * relids - Set of base-relation identifiers; it is a base relation
  375. * if there is just one, a join relation if more than one
  376. * rows - estimated number of tuples in the relation after restriction
  377. * clauses have been applied (ie, output rows of a plan for it)
  378. * consider_startup - true if there is any value in keeping plain paths for
  379. * this rel on the basis of having cheap startup cost
  380. * consider_param_startup - the same for parameterized paths
  381. * reltarget - Default Path output tlist for this rel; normally contains
  382. * Var and PlaceHolderVar nodes for the values we need to
  383. * output from this relation.
  384. * List is in no particular order, but all rels of an
  385. * appendrel set must use corresponding orders.
  386. * NOTE: in an appendrel child relation, may contain
  387. * arbitrary expressions pulled up from a subquery!
  388. * pathlist - List of Path nodes, one for each potentially useful
  389. * method of generating the relation
  390. * ppilist - ParamPathInfo nodes for parameterized Paths, if any
  391. * cheapest_startup_path - the pathlist member with lowest startup cost
  392. * (regardless of ordering) among the unparameterized paths;
  393. * or NULL if there is no unparameterized path
  394. * cheapest_total_path - the pathlist member with lowest total cost
  395. * (regardless of ordering) among the unparameterized paths;
  396. * or if there is no unparameterized path, the path with lowest
  397. * total cost among the paths with minimum parameterization
  398. * cheapest_unique_path - for caching cheapest path to produce unique
  399. * (no duplicates) output from relation; NULL if not yet requested
  400. * cheapest_parameterized_paths - best paths for their parameterizations;
  401. * always includes cheapest_total_path, even if that's unparameterized
  402. * direct_lateral_relids - rels this rel has direct LATERAL references to
  403. * lateral_relids - required outer rels for LATERAL, as a Relids set
  404. * (includes both direct and indirect lateral references)
  405. *
  406. * If the relation is a base relation it will have these fields set:
  407. *
  408. * relid - RTE index (this is redundant with the relids field, but
  409. * is provided for convenience of access)
  410. * rtekind - copy of RTE's rtekind field
  411. * min_attr, max_attr - range of valid AttrNumbers for rel
  412. * attr_needed - array of bitmapsets indicating the highest joinrel
  413. * in which each attribute is needed; if bit 0 is set then
  414. * the attribute is needed as part of final targetlist
  415. * attr_widths - cache space for per-attribute width estimates;
  416. * zero means not computed yet
  417. * lateral_vars - lateral cross-references of rel, if any (list of
  418. * Vars and PlaceHolderVars)
  419. * lateral_referencers - relids of rels that reference this one laterally
  420. * (includes both direct and indirect lateral references)
  421. * indexlist - list of IndexOptInfo nodes for relation's indexes
  422. * (always NIL if it's not a table)
  423. * pages - number of disk pages in relation (zero if not a table)
  424. * tuples - number of tuples in relation (not considering restrictions)
  425. * allvisfrac - fraction of disk pages that are marked all-visible
  426. * subroot - PlannerInfo for subquery (NULL if it's not a subquery)
  427. * subplan_params - list of PlannerParamItems to be passed to subquery
  428. *
  429. * Note: for a subquery, tuples and subroot are not set immediately
  430. * upon creation of the RelOptInfo object; they are filled in when
  431. * set_subquery_pathlist processes the object.
  432. *
  433. * For otherrels that are appendrel members, these fields are filled
  434. * in just as for a baserel, except we don't bother with lateral_vars.
  435. *
  436. * If the relation is either a foreign table or a join of foreign tables that
  437. * all belong to the same foreign server and are assigned to the same user to
  438. * check access permissions as (cf checkAsUser), these fields will be set:
  439. *
  440. * serverid - OID of foreign server, if foreign table (else InvalidOid)
  441. * userid - OID of user to check access as (InvalidOid means current user)
  442. * useridiscurrent - we've assumed that userid equals current user
  443. * fdwroutine - function hooks for FDW, if foreign table (else NULL)
  444. * fdw_private - private state for FDW, if foreign table (else NULL)
  445. *
  446. * Two fields are used to cache knowledge acquired during the join search
  447. * about whether this rel is provably unique when being joined to given other
  448. * relation(s), ie, it can have at most one row matching any given row from
  449. * that join relation. Currently we only attempt such proofs, and thus only
  450. * populate these fields, for base rels; but someday they might be used for
  451. * join rels too:
  452. *
  453. * unique_for_rels - list of Relid sets, each one being a set of other
  454. * rels for which this one has been proven unique
  455. * non_unique_for_rels - list of Relid sets, each one being a set of
  456. * other rels for which we have tried and failed to prove
  457. * this one unique
  458. *
  459. * The presence of the following fields depends on the restrictions
  460. * and joins that the relation participates in:
  461. *
  462. * baserestrictinfo - List of RestrictInfo nodes, containing info about
  463. * each non-join qualification clause in which this relation
  464. * participates (only used for base rels)
  465. * baserestrictcost - Estimated cost of evaluating the baserestrictinfo
  466. * clauses at a single tuple (only used for base rels)
  467. * baserestrict_min_security - Smallest security_level found among
  468. * clauses in baserestrictinfo
  469. * joininfo - List of RestrictInfo nodes, containing info about each
  470. * join clause in which this relation participates (but
  471. * note this excludes clauses that might be derivable from
  472. * EquivalenceClasses)
  473. * has_eclass_joins - flag that EquivalenceClass joins are possible
  474. *
  475. * Note: Keeping a restrictinfo list in the RelOptInfo is useful only for
  476. * base rels, because for a join rel the set of clauses that are treated as
  477. * restrict clauses varies depending on which sub-relations we choose to join.
  478. * (For example, in a 3-base-rel join, a clause relating rels 1 and 2 must be
  479. * treated as a restrictclause if we join {1} and {2 3} to make {1 2 3}; but
  480. * if we join {1 2} and {3} then that clause will be a restrictclause in {1 2}
  481. * and should not be processed again at the level of {1 2 3}.) Therefore,
  482. * the restrictinfo list in the join case appears in individual JoinPaths
  483. * (field joinrestrictinfo), not in the parent relation. But it's OK for
  484. * the RelOptInfo to store the joininfo list, because that is the same
  485. * for a given rel no matter how we form it.
  486. *
  487. * We store baserestrictcost in the RelOptInfo (for base relations) because
  488. * we know we will need it at least once (to price the sequential scan)
  489. * and may need it multiple times to price index scans.
  490. *
  491. * If the relation is partitioned, these fields will be set:
  492. *
  493. * part_scheme - Partitioning scheme of the relation
  494. * nparts - Number of partitions
  495. * boundinfo - Partition bounds
  496. * partition_qual - Partition constraint if not the root
  497. * part_rels - RelOptInfos for each partition
  498. * partexprs, nullable_partexprs - Partition key expressions
  499. * partitioned_child_rels - RT indexes of unpruned partitions of
  500. * this relation that are partitioned tables
  501. * themselves, in hierarchical order
  502. *
  503. * Note: A base relation always has only one set of partition keys, but a join
  504. * relation may have as many sets of partition keys as the number of relations
  505. * being joined. partexprs and nullable_partexprs are arrays containing
  506. * part_scheme->partnatts elements each. Each of these elements is a list of
  507. * partition key expressions. For a base relation each list in partexprs
  508. * contains only one expression and nullable_partexprs is not populated. For a
  509. * join relation, partexprs and nullable_partexprs contain partition key
  510. * expressions from non-nullable and nullable relations resp. Lists at any
  511. * given position in those arrays together contain as many elements as the
  512. * number of joining relations.
  513. *----------
  514. */
  515. typedef enum RelOptKind
  516. {
  517. RELOPT_BASEREL,
  518. RELOPT_JOINREL,
  519. RELOPT_OTHER_MEMBER_REL,
  520. RELOPT_OTHER_JOINREL,
  521. RELOPT_UPPER_REL,
  522. RELOPT_OTHER_UPPER_REL,
  523. RELOPT_DEADREL
  524. } RelOptKind;
  525. /*
  526. * Is the given relation a simple relation i.e a base or "other" member
  527. * relation?
  528. */
  529. #define IS_SIMPLE_REL(rel) \
  530. ((rel)->reloptkind == RELOPT_BASEREL || \
  531. (rel)->reloptkind == RELOPT_OTHER_MEMBER_REL)
  532. /* Is the given relation a join relation? */
  533. #define IS_JOIN_REL(rel) \
  534. ((rel)->reloptkind == RELOPT_JOINREL || \
  535. (rel)->reloptkind == RELOPT_OTHER_JOINREL)
  536. /* Is the given relation an upper relation? */
  537. #define IS_UPPER_REL(rel) \
  538. ((rel)->reloptkind == RELOPT_UPPER_REL || \
  539. (rel)->reloptkind == RELOPT_OTHER_UPPER_REL)
  540. /* Is the given relation an "other" relation? */
  541. #define IS_OTHER_REL(rel) \
  542. ((rel)->reloptkind == RELOPT_OTHER_MEMBER_REL || \
  543. (rel)->reloptkind == RELOPT_OTHER_JOINREL || \
  544. (rel)->reloptkind == RELOPT_OTHER_UPPER_REL)
  545. typedef struct RelOptInfo
  546. {
  547. NodeTag type;
  548. RelOptKind reloptkind;
  549. /* all relations included in this RelOptInfo */
  550. Relids relids; /* set of base relids (rangetable indexes) */
  551. /* size estimates generated by planner */
  552. double rows; /* estimated number of result tuples */
  553. /* per-relation planner control flags */
  554. bool consider_startup; /* keep cheap-startup-cost paths? */
  555. bool consider_param_startup; /* ditto, for parameterized paths? */
  556. bool consider_parallel; /* consider parallel paths? */
  557. /* default result targetlist for Paths scanning this relation */
  558. struct PathTarget *reltarget; /* list of Vars/Exprs, cost, width */
  559. /* materialization information */
  560. List *pathlist; /* Path structures */
  561. List *ppilist; /* ParamPathInfos used in pathlist */
  562. List *partial_pathlist; /* partial Paths */
  563. struct Path *cheapest_startup_path;
  564. struct Path *cheapest_total_path;
  565. struct Path *cheapest_unique_path;
  566. List *cheapest_parameterized_paths;
  567. /* parameterization information needed for both base rels and join rels */
  568. /* (see also lateral_vars and lateral_referencers) */
  569. Relids direct_lateral_relids; /* rels directly laterally referenced */
  570. Relids lateral_relids; /* minimum parameterization of rel */
  571. /* information about a base rel (not set for join rels!) */
  572. Index relid;
  573. Oid reltablespace; /* containing tablespace */
  574. RTEKind rtekind; /* RELATION, SUBQUERY, FUNCTION, etc */
  575. AttrNumber min_attr; /* smallest attrno of rel (often <0) */
  576. AttrNumber max_attr; /* largest attrno of rel */
  577. Relids *attr_needed; /* array indexed [min_attr .. max_attr] */
  578. int32 *attr_widths; /* array indexed [min_attr .. max_attr] */
  579. List *lateral_vars; /* LATERAL Vars and PHVs referenced by rel */
  580. Relids lateral_referencers; /* rels that reference me laterally */
  581. List *indexlist; /* list of IndexOptInfo */
  582. List *statlist; /* list of StatisticExtInfo */
  583. BlockNumber pages; /* size estimates derived from pg_class */
  584. double tuples;
  585. double allvisfrac;
  586. PlannerInfo *subroot; /* if subquery */
  587. List *subplan_params; /* if subquery */
  588. int rel_parallel_workers; /* wanted number of parallel workers */
  589. /* Information about foreign tables and foreign joins */
  590. Oid serverid; /* identifies server for the table or join */
  591. Oid userid; /* identifies user to check access as */
  592. bool useridiscurrent; /* join is only valid for current user */
  593. /* use "struct FdwRoutine" to avoid including fdwapi.h here */
  594. struct FdwRoutine *fdwroutine;
  595. void *fdw_private;
  596. /* cache space for remembering if we have proven this relation unique */
  597. List *unique_for_rels; /* known unique for these other relid
  598. * set(s) */
  599. List *non_unique_for_rels; /* known not unique for these set(s) */
  600. /* used by various scans and joins: */
  601. List *baserestrictinfo; /* RestrictInfo structures (if base rel) */
  602. QualCost baserestrictcost; /* cost of evaluating the above */
  603. Index baserestrict_min_security; /* min security_level found in
  604. * baserestrictinfo */
  605. List *joininfo; /* RestrictInfo structures for join clauses
  606. * involving this rel */
  607. bool has_eclass_joins; /* T means joininfo is incomplete */
  608. /* used by partitionwise joins: */
  609. bool consider_partitionwise_join; /* consider partitionwise join
  610. * paths? (if partitioned rel) */
  611. Relids top_parent_relids; /* Relids of topmost parents (if "other"
  612. * rel) */
  613. /* used for partitioned relations */
  614. PartitionScheme part_scheme; /* Partitioning scheme. */
  615. int nparts; /* number of partitions */
  616. struct PartitionBoundInfoData *boundinfo; /* Partition bounds */
  617. List *partition_qual; /* partition constraint */
  618. struct RelOptInfo **part_rels; /* Array of RelOptInfos of partitions,
  619. * stored in the same order of bounds */
  620. List **partexprs; /* Non-nullable partition key expressions. */
  621. List **nullable_partexprs; /* Nullable partition key expressions. */
  622. List *partitioned_child_rels; /* List of RT indexes. */
  623. } RelOptInfo;
  624. /*
  625. * Is given relation partitioned?
  626. *
  627. * It's not enough to test whether rel->part_scheme is set, because it might
  628. * be that the basic partitioning properties of the input relations matched
  629. * but the partition bounds did not. Also, if we are able to prove a rel
  630. * dummy (empty), we should henceforth treat it as unpartitioned.
  631. */
  632. #define IS_PARTITIONED_REL(rel) \
  633. ((rel)->part_scheme && (rel)->boundinfo && (rel)->nparts > 0 && \
  634. (rel)->part_rels && !IS_DUMMY_REL(rel))
  635. /*
  636. * Convenience macro to make sure that a partitioned relation has all the
  637. * required members set.
  638. */
  639. #define REL_HAS_ALL_PART_PROPS(rel) \
  640. ((rel)->part_scheme && (rel)->boundinfo && (rel)->nparts > 0 && \
  641. (rel)->part_rels && (rel)->partexprs && (rel)->nullable_partexprs)
  642. /*
  643. * IndexOptInfo
  644. * Per-index information for planning/optimization
  645. *
  646. * indexkeys[], indexcollations[] each have ncolumns entries.
  647. * opfamily[], and opcintype[] each have nkeycolumns entries. They do
  648. * not contain any information about included attributes.
  649. *
  650. * sortopfamily[], reverse_sort[], and nulls_first[] have
  651. * nkeycolumns entries, if the index is ordered; but if it is unordered,
  652. * those pointers are NULL.
  653. *
  654. * Zeroes in the indexkeys[] array indicate index columns that are
  655. * expressions; there is one element in indexprs for each such column.
  656. *
  657. * For an ordered index, reverse_sort[] and nulls_first[] describe the
  658. * sort ordering of a forward indexscan; we can also consider a backward
  659. * indexscan, which will generate the reverse ordering.
  660. *
  661. * The indexprs and indpred expressions have been run through
  662. * prepqual.c and eval_const_expressions() for ease of matching to
  663. * WHERE clauses. indpred is in implicit-AND form.
  664. *
  665. * indextlist is a TargetEntry list representing the index columns.
  666. * It provides an equivalent base-relation Var for each simple column,
  667. * and links to the matching indexprs element for each expression column.
  668. *
  669. * While most of these fields are filled when the IndexOptInfo is created
  670. * (by plancat.c), indrestrictinfo and predOK are set later, in
  671. * check_index_predicates().
  672. */
  673. #ifndef HAVE_INDEXOPTINFO_TYPEDEF
  674. typedef struct IndexOptInfo IndexOptInfo;
  675. #define HAVE_INDEXOPTINFO_TYPEDEF 1
  676. #endif
  677. struct IndexOptInfo
  678. {
  679. NodeTag type;
  680. Oid indexoid; /* OID of the index relation */
  681. Oid reltablespace; /* tablespace of index (not table) */
  682. RelOptInfo *rel; /* back-link to index's table */
  683. /* index-size statistics (from pg_class and elsewhere) */
  684. BlockNumber pages; /* number of disk pages in index */
  685. double tuples; /* number of index tuples in index */
  686. int tree_height; /* index tree height, or -1 if unknown */
  687. /* index descriptor information */
  688. int ncolumns; /* number of columns in index */
  689. int nkeycolumns; /* number of key columns in index */
  690. int *indexkeys; /* column numbers of index's attributes both
  691. * key and included columns, or 0 */
  692. Oid *indexcollations; /* OIDs of collations of index columns */
  693. Oid *opfamily; /* OIDs of operator families for columns */
  694. Oid *opcintype; /* OIDs of opclass declared input data types */
  695. Oid *sortopfamily; /* OIDs of btree opfamilies, if orderable */
  696. bool *reverse_sort; /* is sort order descending? */
  697. bool *nulls_first; /* do NULLs come first in the sort order? */
  698. bool *canreturn; /* which index cols can be returned in an
  699. * index-only scan? */
  700. Oid relam; /* OID of the access method (in pg_am) */
  701. List *indexprs; /* expressions for non-simple index columns */
  702. List *indpred; /* predicate if a partial index, else NIL */
  703. List *indextlist; /* targetlist representing index columns */
  704. List *indrestrictinfo; /* parent relation's baserestrictinfo
  705. * list, less any conditions implied by
  706. * the index's predicate (unless it's a
  707. * target rel, see comments in
  708. * check_index_predicates()) */
  709. bool predOK; /* true if index predicate matches query */
  710. bool unique; /* true if a unique index */
  711. bool immediate; /* is uniqueness enforced immediately? */
  712. bool hypothetical; /* true if index doesn't really exist */
  713. /* Remaining fields are copied from the index AM's API struct: */
  714. bool amcanorderbyop; /* does AM support order by operator result? */
  715. bool amoptionalkey; /* can query omit key for the first column? */
  716. bool amsearcharray; /* can AM handle ScalarArrayOpExpr quals? */
  717. bool amsearchnulls; /* can AM search for NULL/NOT NULL entries? */
  718. bool amhasgettuple; /* does AM have amgettuple interface? */
  719. bool amhasgetbitmap; /* does AM have amgetbitmap interface? */
  720. bool amcanparallel; /* does AM support parallel scan? */
  721. /* Rather than include amapi.h here, we declare amcostestimate like this */
  722. void (*amcostestimate) (); /* AM's cost estimator */
  723. };
  724. /*
  725. * ForeignKeyOptInfo
  726. * Per-foreign-key information for planning/optimization
  727. *
  728. * The per-FK-column arrays can be fixed-size because we allow at most
  729. * INDEX_MAX_KEYS columns in a foreign key constraint. Each array has
  730. * nkeys valid entries.
  731. */
  732. typedef struct ForeignKeyOptInfo
  733. {
  734. NodeTag type;
  735. /* Basic data about the foreign key (fetched from catalogs): */
  736. Index con_relid; /* RT index of the referencing table */
  737. Index ref_relid; /* RT index of the referenced table */
  738. int nkeys; /* number of columns in the foreign key */
  739. AttrNumber conkey[INDEX_MAX_KEYS]; /* cols in referencing table */
  740. AttrNumber confkey[INDEX_MAX_KEYS]; /* cols in referenced table */
  741. Oid conpfeqop[INDEX_MAX_KEYS]; /* PK = FK operator OIDs */
  742. /* Derived info about whether FK's equality conditions match the query: */
  743. int nmatched_ec; /* # of FK cols matched by ECs */
  744. int nmatched_rcols; /* # of FK cols matched by non-EC rinfos */
  745. int nmatched_ri; /* total # of non-EC rinfos matched to FK */
  746. /* Pointer to eclass matching each column's condition, if there is one */
  747. struct EquivalenceClass *eclass[INDEX_MAX_KEYS];
  748. /* List of non-EC RestrictInfos matching each column's condition */
  749. List *rinfos[INDEX_MAX_KEYS];
  750. } ForeignKeyOptInfo;
  751. /*
  752. * StatisticExtInfo
  753. * Information about extended statistics for planning/optimization
  754. *
  755. * Each pg_statistic_ext row is represented by one or more nodes of this
  756. * type, or even zero if ANALYZE has not computed them.
  757. */
  758. typedef struct StatisticExtInfo
  759. {
  760. NodeTag type;
  761. Oid statOid; /* OID of the statistics row */
  762. RelOptInfo *rel; /* back-link to statistic's table */
  763. char kind; /* statistic kind of this entry */
  764. Bitmapset *keys; /* attnums of the columns covered */
  765. } StatisticExtInfo;
  766. /*
  767. * EquivalenceClasses
  768. *
  769. * Whenever we can determine that a mergejoinable equality clause A = B is
  770. * not delayed by any outer join, we create an EquivalenceClass containing
  771. * the expressions A and B to record this knowledge. If we later find another
  772. * equivalence B = C, we add C to the existing EquivalenceClass; this may
  773. * require merging two existing EquivalenceClasses. At the end of the qual
  774. * distribution process, we have sets of values that are known all transitively
  775. * equal to each other, where "equal" is according to the rules of the btree
  776. * operator family(s) shown in ec_opfamilies, as well as the collation shown
  777. * by ec_collation. (We restrict an EC to contain only equalities whose
  778. * operators belong to the same set of opfamilies. This could probably be
  779. * relaxed, but for now it's not worth the trouble, since nearly all equality
  780. * operators belong to only one btree opclass anyway. Similarly, we suppose
  781. * that all or none of the input datatypes are collatable, so that a single
  782. * collation value is sufficient.)
  783. *
  784. * We also use EquivalenceClasses as the base structure for PathKeys, letting
  785. * us represent knowledge about different sort orderings being equivalent.
  786. * Since every PathKey must reference an EquivalenceClass, we will end up
  787. * with single-member EquivalenceClasses whenever a sort key expression has
  788. * not been equivalenced to anything else. It is also possible that such an
  789. * EquivalenceClass will contain a volatile expression ("ORDER BY random()"),
  790. * which is a case that can't arise otherwise since clauses containing
  791. * volatile functions are never considered mergejoinable. We mark such
  792. * EquivalenceClasses specially to prevent them from being merged with
  793. * ordinary EquivalenceClasses. Also, for volatile expressions we have
  794. * to be careful to match the EquivalenceClass to the correct targetlist
  795. * entry: consider SELECT random() AS a, random() AS b ... ORDER BY b,a.
  796. * So we record the SortGroupRef of the originating sort clause.
  797. *
  798. * We allow equality clauses appearing below the nullable side of an outer join
  799. * to form EquivalenceClasses, but these have a slightly different meaning:
  800. * the included values might be all NULL rather than all the same non-null
  801. * values. See src/backend/optimizer/README for more on that point.
  802. *
  803. * NB: if ec_merged isn't NULL, this class has been merged into another, and
  804. * should be ignored in favor of using the pointed-to class.
  805. */
  806. typedef struct EquivalenceClass
  807. {
  808. NodeTag type;
  809. List *ec_opfamilies; /* btree operator family OIDs */
  810. Oid ec_collation; /* collation, if datatypes are collatable */
  811. List *ec_members; /* list of EquivalenceMembers */
  812. List *ec_sources; /* list of generating RestrictInfos */
  813. List *ec_derives; /* list of derived RestrictInfos */
  814. Relids ec_relids; /* all relids appearing in ec_members, except
  815. * for child members (see below) */
  816. bool ec_has_const; /* any pseudoconstants in ec_members? */
  817. bool ec_has_volatile; /* the (sole) member is a volatile expr */
  818. bool ec_below_outer_join; /* equivalence applies below an OJ */
  819. bool ec_broken; /* failed to generate needed clauses? */
  820. Index ec_sortref; /* originating sortclause label, or 0 */
  821. Index ec_min_security; /* minimum security_level in ec_sources */
  822. Index ec_max_security; /* maximum security_level in ec_sources */
  823. struct EquivalenceClass *ec_merged; /* set if merged into another EC */
  824. } EquivalenceClass;
  825. /*
  826. * If an EC contains a const and isn't below-outer-join, any PathKey depending
  827. * on it must be redundant, since there's only one possible value of the key.
  828. */
  829. #define EC_MUST_BE_REDUNDANT(eclass) \
  830. ((eclass)->ec_has_const && !(eclass)->ec_below_outer_join)
  831. /*
  832. * EquivalenceMember - one member expression of an EquivalenceClass
  833. *
  834. * em_is_child signifies that this element was built by transposing a member
  835. * for an appendrel parent relation to represent the corresponding expression
  836. * for an appendrel child. These members are used for determining the
  837. * pathkeys of scans on the child relation and for explicitly sorting the
  838. * child when necessary to build a MergeAppend path for the whole appendrel
  839. * tree. An em_is_child member has no impact on the properties of the EC as a
  840. * whole; in particular the EC's ec_relids field does NOT include the child
  841. * relation. An em_is_child member should never be marked em_is_const nor
  842. * cause ec_has_const or ec_has_volatile to be set, either. Thus, em_is_child
  843. * members are not really full-fledged members of the EC, but just reflections
  844. * or doppelgangers of real members. Most operations on EquivalenceClasses
  845. * should ignore em_is_child members, and those that don't should test
  846. * em_relids to make sure they only consider relevant members.
  847. *
  848. * em_datatype is usually the same as exprType(em_expr), but can be
  849. * different when dealing with a binary-compatible opfamily; in particular
  850. * anyarray_ops would never work without this. Use em_datatype when
  851. * looking up a specific btree operator to work with this expression.
  852. */
  853. typedef struct EquivalenceMember
  854. {
  855. NodeTag type;
  856. Expr *em_expr; /* the expression represented */
  857. Relids em_relids; /* all relids appearing in em_expr */
  858. Relids em_nullable_relids; /* nullable by lower outer joins */
  859. bool em_is_const; /* expression is pseudoconstant? */
  860. bool em_is_child; /* derived version for a child relation? */
  861. Oid em_datatype; /* the "nominal type" used by the opfamily */
  862. } EquivalenceMember;
  863. /*
  864. * PathKeys
  865. *
  866. * The sort ordering of a path is represented by a list of PathKey nodes.
  867. * An empty list implies no known ordering. Otherwise the first item
  868. * represents the primary sort key, the second the first secondary sort key,
  869. * etc. The value being sorted is represented by linking to an
  870. * EquivalenceClass containing that value and including pk_opfamily among its
  871. * ec_opfamilies. The EquivalenceClass tells which collation to use, too.
  872. * This is a convenient method because it makes it trivial to detect
  873. * equivalent and closely-related orderings. (See optimizer/README for more
  874. * information.)
  875. *
  876. * Note: pk_strategy is either BTLessStrategyNumber (for ASC) or
  877. * BTGreaterStrategyNumber (for DESC). We assume that all ordering-capable
  878. * index types will use btree-compatible strategy numbers.
  879. */
  880. typedef struct PathKey
  881. {
  882. NodeTag type;
  883. EquivalenceClass *pk_eclass; /* the value that is ordered */
  884. Oid pk_opfamily; /* btree opfamily defining the ordering */
  885. int pk_strategy; /* sort direction (ASC or DESC) */
  886. bool pk_nulls_first; /* do NULLs come before normal values? */
  887. } PathKey;
  888. /*
  889. * PathTarget
  890. *
  891. * This struct contains what we need to know during planning about the
  892. * targetlist (output columns) that a Path will compute. Each RelOptInfo
  893. * includes a default PathTarget, which its individual Paths may simply
  894. * reference. However, in some cases a Path may compute outputs different
  895. * from other Paths, and in that case we make a custom PathTarget for it.
  896. * For example, an indexscan might return index expressions that would
  897. * otherwise need to be explicitly calculated. (Note also that "upper"
  898. * relations generally don't have useful default PathTargets.)
  899. *
  900. * exprs contains bare expressions; they do not have TargetEntry nodes on top,
  901. * though those will appear in finished Plans.
  902. *
  903. * sortgrouprefs[] is an array of the same length as exprs, containing the
  904. * corresponding sort/group refnos, or zeroes for expressions not referenced
  905. * by sort/group clauses. If sortgrouprefs is NULL (which it generally is in
  906. * RelOptInfo.reltarget targets; only upper-level Paths contain this info),
  907. * we have not identified sort/group columns in this tlist. This allows us to
  908. * deal with sort/group refnos when needed with less expense than including
  909. * TargetEntry nodes in the exprs list.
  910. */
  911. typedef struct PathTarget
  912. {
  913. NodeTag type;
  914. List *exprs; /* list of expressions to be computed */
  915. Index *sortgrouprefs; /* corresponding sort/group refnos, or 0 */
  916. QualCost cost; /* cost of evaluating the expressions */
  917. int width; /* estimated avg width of result tuples */
  918. } PathTarget;
  919. /* Convenience macro to get a sort/group refno from a PathTarget */
  920. #define get_pathtarget_sortgroupref(target, colno) \
  921. ((target)->sortgrouprefs ? (target)->sortgrouprefs[colno] : (Index) 0)
  922. /*
  923. * ParamPathInfo
  924. *
  925. * All parameterized paths for a given relation with given required outer rels
  926. * link to a single ParamPathInfo, which stores common information such as
  927. * the estimated rowcount for this parameterization. We do this partly to
  928. * avoid recalculations, but mostly to ensure that the estimated rowcount
  929. * is in fact the same for every such path.
  930. *
  931. * Note: ppi_clauses is only used in ParamPathInfos for base relation paths;
  932. * in join cases it's NIL because the set of relevant clauses varies depending
  933. * on how the join is formed. The relevant clauses will appear in each
  934. * parameterized join path's joinrestrictinfo list, instead.
  935. */
  936. typedef struct ParamPathInfo
  937. {
  938. NodeTag type;
  939. Relids ppi_req_outer; /* rels supplying parameters used by path */
  940. double ppi_rows; /* estimated number of result tuples */
  941. List *ppi_clauses; /* join clauses available from outer rels */
  942. } ParamPathInfo;
  943. /*
  944. * Type "Path" is used as-is for sequential-scan paths, as well as some other
  945. * simple plan types that we don't need any extra information in the path for.
  946. * For other path types it is the first component of a larger struct.
  947. *
  948. * "pathtype" is the NodeTag of the Plan node we could build from this Path.
  949. * It is partially redundant with the Path's NodeTag, but allows us to use
  950. * the same Path type for multiple Plan types when there is no need to
  951. * distinguish the Plan type during path processing.
  952. *
  953. * "parent" identifies the relation this Path scans, and "pathtarget"
  954. * describes the precise set of output columns the Path would compute.
  955. * In simple cases all Paths for a given rel share the same targetlist,
  956. * which we represent by having path->pathtarget equal to parent->reltarget.
  957. *
  958. * "param_info", if not NULL, links to a ParamPathInfo that identifies outer
  959. * relation(s) that provide parameter values to each scan of this path.
  960. * That means this path can only be joined to those rels by means of nestloop
  961. * joins with this path on the inside. Also note that a parameterized path
  962. * is responsible for testing all "movable" joinclauses involving this rel
  963. * and the specified outer rel(s).
  964. *
  965. * "rows" is the same as parent->rows in simple paths, but in parameterized
  966. * paths and UniquePaths it can be less than parent->rows, reflecting the
  967. * fact that we've filtered by extra join conditions or removed duplicates.
  968. *
  969. * "pathkeys" is a List of PathKey nodes (see above), describing the sort
  970. * ordering of the path's output rows.
  971. */
  972. typedef struct Path
  973. {
  974. NodeTag type;
  975. NodeTag pathtype; /* tag identifying scan/join method */
  976. RelOptInfo *parent; /* the relation this path can build */
  977. PathTarget *pathtarget; /* list of Vars/Exprs, cost, width */
  978. ParamPathInfo *param_info; /* parameterization info, or NULL if none */
  979. bool parallel_aware; /* engage parallel-aware logic? */
  980. bool parallel_safe; /* OK to use as part of parallel plan? */
  981. int parallel_workers; /* desired # of workers; 0 = not parallel */
  982. /* estimated size/costs for path (see costsize.c for more info) */
  983. double rows; /* estimated number of result tuples */
  984. Cost startup_cost; /* cost expended before fetching any tuples */
  985. Cost total_cost; /* total cost (assuming all tuples fetched) */
  986. List *pathkeys; /* sort ordering of path's output */
  987. /* pathkeys is a List of PathKey nodes; see above */
  988. } Path;
  989. /* Macro for extracting a path's parameterization relids; beware double eval */
  990. #define PATH_REQ_OUTER(path) \
  991. ((path)->param_info ? (path)->param_info->ppi_req_outer : (Relids) NULL)
  992. /*----------
  993. * IndexPath represents an index scan over a single index.
  994. *
  995. * This struct is used for both regular indexscans and index-only scans;
  996. * path.pathtype is T_IndexScan or T_IndexOnlyScan to show which is meant.
  997. *
  998. * 'indexinfo' is the index to be scanned.
  999. *
  1000. * 'indexclauses' is a list of IndexClause nodes, each representing one
  1001. * index-checkable restriction, with implicit AND semantics across the list.
  1002. * An empty list implies a full index scan.
  1003. *
  1004. * 'indexorderbys', if not NIL, is a list of ORDER BY expressions that have
  1005. * been found to be usable as ordering operators for an amcanorderbyop index.
  1006. * The list must match the path's pathkeys, ie, one expression per pathkey
  1007. * in the same order. These are not RestrictInfos, just bare expressions,
  1008. * since they generally won't yield booleans. It's guaranteed that each
  1009. * expression has the index key on the left side of the operator.
  1010. *
  1011. * 'indexorderbycols' is an integer list of index column numbers (zero-based)
  1012. * of the same length as 'indexorderbys', showing which index column each
  1013. * ORDER BY expression is meant to be used with. (There is no restriction
  1014. * on which index column each ORDER BY can be used with.)
  1015. *
  1016. * 'indexscandir' is one of:
  1017. * ForwardScanDirection: forward scan of an ordered index
  1018. * BackwardScanDirection: backward scan of an ordered index
  1019. * NoMovementScanDirection: scan of an unordered index, or don't care
  1020. * (The executor doesn't care whether it gets ForwardScanDirection or
  1021. * NoMovementScanDirection for an indexscan, but the planner wants to
  1022. * distinguish ordered from unordered indexes for building pathkeys.)
  1023. *
  1024. * 'indextotalcost' and 'indexselectivity' are saved in the IndexPath so that
  1025. * we need not recompute them when considering using the same index in a
  1026. * bitmap index/heap scan (see BitmapHeapPath). The costs of the IndexPath
  1027. * itself represent the costs of an IndexScan or IndexOnlyScan plan type.
  1028. *----------
  1029. */
  1030. typedef struct IndexPath
  1031. {
  1032. Path path;
  1033. IndexOptInfo *indexinfo;
  1034. List *indexclauses;
  1035. List *indexorderbys;
  1036. List *indexorderbycols;
  1037. ScanDirection indexscandir;
  1038. Cost indextotalcost;
  1039. Selectivity indexselectivity;
  1040. } IndexPath;
  1041. /*
  1042. * Each IndexClause references a RestrictInfo node from the query's WHERE
  1043. * or JOIN conditions, and shows how that restriction can be applied to
  1044. * the particular index. We support both indexclauses that are directly
  1045. * usable by the index machinery, which are typically of the form
  1046. * "indexcol OP pseudoconstant", and those from which an indexable qual
  1047. * can be derived. The simplest such transformation is that a clause
  1048. * of the form "pseudoconstant OP indexcol" can be commuted to produce an
  1049. * indexable qual (the index machinery expects the indexcol to be on the
  1050. * left always). Another example is that we might be able to extract an
  1051. * indexable range condition from a LIKE condition, as in "x LIKE 'foo%bar'"
  1052. * giving rise to "x >= 'foo' AND x < 'fop'". Derivation of such lossy
  1053. * conditions is done by a planner support function attached to the
  1054. * indexclause's top-level function or operator.
  1055. *
  1056. * indexquals is a list of RestrictInfos for the directly-usable index
  1057. * conditions associated with this IndexClause. In the simplest case
  1058. * it's a one-element list whose member is iclause->rinfo. Otherwise,
  1059. * it contains one or more directly-usable indexqual conditions extracted
  1060. * from the given clause. The 'lossy' flag indicates whether the
  1061. * indexquals are semantically equivalent to the original clause, or
  1062. * represent a weaker condition.
  1063. *
  1064. * Normally, indexcol is the index of the single index column the clause
  1065. * works on, and indexcols is NIL. But if the clause is a RowCompareExpr,
  1066. * indexcol is the index of the leading column, and indexcols is a list of
  1067. * all the affected columns. (Note that indexcols matches up with the
  1068. * columns of the actual indexable RowCompareExpr in indexquals, which
  1069. * might be different from the original in rinfo.)
  1070. *
  1071. * An IndexPath's IndexClause list is required to be ordered by index
  1072. * column, i.e. the indexcol values must form a nondecreasing sequence.
  1073. * (The order of multiple clauses for the same index column is unspecified.)
  1074. */
  1075. typedef struct IndexClause
  1076. {
  1077. NodeTag type;
  1078. struct RestrictInfo *rinfo; /* original restriction or join clause */
  1079. List *indexquals; /* indexqual(s) derived from it */
  1080. bool lossy; /* are indexquals a lossy version of clause? */
  1081. AttrNumber indexcol; /* index column the clause uses (zero-based) */
  1082. List *indexcols; /* multiple index columns, if RowCompare */
  1083. } IndexClause;
  1084. /*
  1085. * BitmapHeapPath represents one or more indexscans that generate TID bitmaps
  1086. * instead of directly accessing the heap, followed by AND/OR combinations
  1087. * to produce a single bitmap, followed by a heap scan that uses the bitmap.
  1088. * Note that the output is always considered unordered, since it will come
  1089. * out in physical heap order no matter what the underlying indexes did.
  1090. *
  1091. * The individual indexscans are represented by IndexPath nodes, and any
  1092. * logic on top of them is represented by a tree of BitmapAndPath and
  1093. * BitmapOrPath nodes. Notice that we can use the same IndexPath node both
  1094. * to represent a regular (or index-only) index scan plan, and as the child
  1095. * of a BitmapHeapPath that represents scanning the same index using a
  1096. * BitmapIndexScan. The startup_cost and total_cost figures of an IndexPath
  1097. * always represent the costs to use it as a regular (or index-only)
  1098. * IndexScan. The costs of a BitmapIndexScan can be computed using the
  1099. * IndexPath's indextotalcost and indexselectivity.
  1100. */
  1101. typedef struct BitmapHeapPath
  1102. {
  1103. Path path;
  1104. Path *bitmapqual; /* IndexPath, BitmapAndPath, BitmapOrPath */
  1105. } BitmapHeapPath;
  1106. /*
  1107. * BitmapAndPath represents a BitmapAnd plan node; it can only appear as
  1108. * part of the substructure of a BitmapHeapPath. The Path structure is
  1109. * a bit more heavyweight than we really need for this, but for simplicity
  1110. * we make it a derivative of Path anyway.
  1111. */
  1112. typedef struct BitmapAndPath
  1113. {
  1114. Path path;
  1115. List *bitmapquals; /* IndexPaths and BitmapOrPaths */
  1116. Selectivity bitmapselectivity;
  1117. } BitmapAndPath;
  1118. /*
  1119. * BitmapOrPath represents a BitmapOr plan node; it can only appear as
  1120. * part of the substructure of a BitmapHeapPath. The Path structure is
  1121. * a bit more heavyweight than we really need for this, but for simplicity
  1122. * we make it a derivative of Path anyway.
  1123. */
  1124. typedef struct BitmapOrPath
  1125. {
  1126. Path path;
  1127. List *bitmapquals; /* IndexPaths and BitmapAndPaths */
  1128. Selectivity bitmapselectivity;
  1129. } BitmapOrPath;
  1130. /*
  1131. * TidPath represents a scan by TID
  1132. *
  1133. * tidquals is an implicitly OR'ed list of qual expressions of the form
  1134. * "CTID = pseudoconstant", or "CTID = ANY(pseudoconstant_array)",
  1135. * or a CurrentOfExpr for the relation.
  1136. */
  1137. typedef struct TidPath
  1138. {
  1139. Path path;
  1140. List *tidquals; /* qual(s) involving CTID = something */
  1141. } TidPath;
  1142. /*
  1143. * SubqueryScanPath represents a scan of an unflattened subquery-in-FROM
  1144. *
  1145. * Note that the subpath comes from a different planning domain; for example
  1146. * RTE indexes within it mean something different from those known to the
  1147. * SubqueryScanPath. path.parent->subroot is the planning context needed to
  1148. * interpret the subpath.
  1149. */
  1150. typedef struct SubqueryScanPath
  1151. {
  1152. Path path;
  1153. Path *subpath; /* path representing subquery execution */
  1154. } SubqueryScanPath;
  1155. /*
  1156. * ForeignPath represents a potential scan of a foreign table, foreign join
  1157. * or foreign upper-relation.
  1158. *
  1159. * fdw_private stores FDW private data about the scan. While fdw_private is
  1160. * not actually touched by the core code during normal operations, it's
  1161. * generally a good idea to use a representation that can be dumped by
  1162. * nodeToString(), so that you can examine the structure during debugging
  1163. * with tools like pprint().
  1164. */
  1165. typedef struct ForeignPath
  1166. {
  1167. Path path;
  1168. Path *fdw_outerpath;
  1169. List *fdw_private;
  1170. } ForeignPath;
  1171. /*
  1172. * CustomPath represents a table scan done by some out-of-core extension.
  1173. *
  1174. * We provide a set of hooks here - which the provider must take care to set
  1175. * up correctly - to allow extensions to supply their own methods of scanning
  1176. * a relation. For example, a provider might provide GPU acceleration, a
  1177. * cache-based scan, or some other kind of logic we haven't dreamed up yet.
  1178. *
  1179. * CustomPaths can be injected into the planning process for a relation by
  1180. * set_rel_pathlist_hook functions.
  1181. *
  1182. * Core code must avoid assuming that the CustomPath is only as large as
  1183. * the structure declared here; providers are allowed to make it the first
  1184. * element in a larger structure. (Since the planner never copies Paths,
  1185. * this doesn't add any complication.) However, for consistency with the
  1186. * FDW case, we provide a "custom_private" field in CustomPath; providers
  1187. * may prefer to use that rather than define another struct type.
  1188. */
  1189. struct CustomPathMethods;
  1190. typedef struct CustomPath
  1191. {
  1192. Path path;
  1193. uint32 flags; /* mask of CUSTOMPATH_* flags, see
  1194. * nodes/extensible.h */
  1195. List *custom_paths; /* list of child Path nodes, if any */
  1196. List *custom_private;
  1197. const struct CustomPathMethods *methods;
  1198. } CustomPath;
  1199. /*
  1200. * AppendPath represents an Append plan, ie, successive execution of
  1201. * several member plans.
  1202. *
  1203. * For partial Append, 'subpaths' contains non-partial subpaths followed by
  1204. * partial subpaths.
  1205. *
  1206. * Note: it is possible for "subpaths" to contain only one, or even no,
  1207. * elements. These cases are optimized during create_append_plan.
  1208. * In particular, an AppendPath with no subpaths is a "dummy" path that
  1209. * is created to represent the case that a relation is provably empty.
  1210. * (This is a convenient representation because it means that when we build
  1211. * an appendrel and find that all its children have been excluded, no extra
  1212. * action is needed to recognize the relation as dummy.)
  1213. */
  1214. typedef struct AppendPath
  1215. {
  1216. Path path;
  1217. /* RT indexes of non-leaf tables in a partition tree */
  1218. List *partitioned_rels;
  1219. List *subpaths; /* list of component Paths */
  1220. /* Index of first partial path in subpaths; list_length(subpaths) if none */
  1221. int first_partial_path;
  1222. double limit_tuples; /* hard limit on output tuples, or -1 */
  1223. } AppendPath;
  1224. #define IS_DUMMY_APPEND(p) \
  1225. (IsA((p), AppendPath) && ((AppendPath *) (p))->subpaths == NIL)
  1226. /*
  1227. * A relation that's been proven empty will have one path that is dummy
  1228. * (but might have projection paths on top). For historical reasons,
  1229. * this is provided as a macro that wraps is_dummy_rel().
  1230. */
  1231. #define IS_DUMMY_REL(r) is_dummy_rel(r)
  1232. extern bool is_dummy_rel(RelOptInfo *rel);
  1233. /*
  1234. * MergeAppendPath represents a MergeAppend plan, ie, the merging of sorted
  1235. * results from several member plans to produce similarly-sorted output.
  1236. */
  1237. typedef struct MergeAppendPath
  1238. {
  1239. Path path;
  1240. /* RT indexes of non-leaf tables in a partition tree */
  1241. List *partitioned_rels;
  1242. List *subpaths; /* list of component Paths */
  1243. double limit_tuples; /* hard limit on output tuples, or -1 */
  1244. } MergeAppendPath;
  1245. /*
  1246. * GroupResultPath represents use of a Result plan node to compute the
  1247. * output of a degenerate GROUP BY case, wherein we know we should produce
  1248. * exactly one row, which might then be filtered by a HAVING qual.
  1249. *
  1250. * Note that quals is a list of bare clauses, not RestrictInfos.
  1251. */
  1252. typedef struct GroupResultPath
  1253. {
  1254. Path path;
  1255. List *quals;
  1256. } GroupResultPath;
  1257. /*
  1258. * MaterialPath represents use of a Material plan node, i.e., caching of
  1259. * the output of its subpath. This is used when the subpath is expensive
  1260. * and needs to be scanned repeatedly, or when we need mark/restore ability
  1261. * and the subpath doesn't have it.
  1262. */
  1263. typedef struct MaterialPath
  1264. {
  1265. Path path;
  1266. Path *subpath;
  1267. } MaterialPath;
  1268. /*
  1269. * UniquePath represents elimination of distinct rows from the output of
  1270. * its subpath.
  1271. *
  1272. * This can represent significantly different plans: either hash-based or
  1273. * sort-based implementation, or a no-op if the input path can be proven
  1274. * distinct already. The decision is sufficiently localized that it's not
  1275. * worth having separate Path node types. (Note: in the no-op case, we could
  1276. * eliminate the UniquePath node entirely and just return the subpath; but
  1277. * it's convenient to have a UniquePath in the path tree to signal upper-level
  1278. * routines that the input is known distinct.)
  1279. */
  1280. typedef enum
  1281. {
  1282. UNIQUE_PATH_NOOP, /* input is known unique already */
  1283. UNIQUE_PATH_HASH, /* use hashing */
  1284. UNIQUE_PATH_SORT /* use sorting */
  1285. } UniquePathMethod;
  1286. typedef struct UniquePath
  1287. {
  1288. Path path;
  1289. Path *subpath;
  1290. UniquePathMethod umethod;
  1291. List *in_operators; /* equality operators of the IN clause */
  1292. List *uniq_exprs; /* expressions to be made unique */
  1293. } UniquePath;
  1294. /*
  1295. * GatherPath runs several copies of a plan in parallel and collects the
  1296. * results. The parallel leader may also execute the plan, unless the
  1297. * single_copy flag is set.
  1298. */
  1299. typedef struct GatherPath
  1300. {
  1301. Path path;
  1302. Path *subpath; /* path for each worker */
  1303. bool single_copy; /* don't execute path more than once */
  1304. int num_workers; /* number of workers sought to help */
  1305. } GatherPath;
  1306. /*
  1307. * GatherMergePath runs several copies of a plan in parallel and collects
  1308. * the results, preserving their common sort order.
  1309. */
  1310. typedef struct GatherMergePath
  1311. {
  1312. Path path;
  1313. Path *subpath; /* path for each worker */
  1314. int num_workers; /* number of workers sought to help */
  1315. } GatherMergePath;
  1316. /*
  1317. * All join-type paths share these fields.
  1318. */
  1319. typedef struct JoinPath
  1320. {
  1321. Path path;
  1322. JoinType jointype;
  1323. bool inner_unique; /* each outer tuple provably matches no more
  1324. * than one inner tuple */
  1325. Path *outerjoinpath; /* path for the outer side of the join */
  1326. Path *innerjoinpath; /* path for the inner side of the join */
  1327. List *joinrestrictinfo; /* RestrictInfos to apply to join */
  1328. /*
  1329. * See the notes for RelOptInfo and ParamPathInfo to understand why
  1330. * joinrestrictinfo is needed in JoinPath, and can't be merged into the
  1331. * parent RelOptInfo.
  1332. */
  1333. } JoinPath;
  1334. /*
  1335. * A nested-loop path needs no special fields.
  1336. */
  1337. typedef JoinPath NestPath;
  1338. /*
  1339. * A mergejoin path has these fields.
  1340. *
  1341. * Unlike other path types, a MergePath node doesn't represent just a single
  1342. * run-time plan node: it can represent up to four. Aside from the MergeJoin
  1343. * node itself, there can be a Sort node for the outer input, a Sort node
  1344. * for the inner input, and/or a Material node for the inner input. We could
  1345. * represent these nodes by separate path nodes, but considering how many
  1346. * different merge paths are investigated during a complex join problem,
  1347. * it seems better to avoid unnecessary palloc overhead.
  1348. *
  1349. * path_mergeclauses lists the clauses (in the form of RestrictInfos)
  1350. * that will be used in the merge.
  1351. *
  1352. * Note that the mergeclauses are a subset of the parent relation's
  1353. * restriction-clause list. Any join clauses that are not mergejoinable
  1354. * appear only in the parent's restrict list, and must be checked by a
  1355. * qpqual at execution time.
  1356. *
  1357. * outersortkeys (resp. innersortkeys) is NIL if the outer path
  1358. * (resp. inner path) is already ordered appropriately for the
  1359. * mergejoin. If it is not NIL then it is a PathKeys list describing
  1360. * the ordering that must be created by an explicit Sort node.
  1361. *
  1362. * skip_mark_restore is true if the executor need not do mark/restore calls.
  1363. * Mark/restore overhead is usually required, but can be skipped if we know
  1364. * that the executor need find only one match per outer tuple, and that the
  1365. * mergeclauses are sufficient to identify a match. In such cases the
  1366. * executor can immediately advance the outer relation after processing a
  1367. * match, and therefore it need never back up the inner relation.
  1368. *
  1369. * materialize_inner is true if a Material node should be placed atop the
  1370. * inner input. This may appear with or without an inner Sort step.
  1371. */
  1372. typedef struct MergePath
  1373. {
  1374. JoinPath jpath;
  1375. List *path_mergeclauses; /* join clauses to be used for merge */
  1376. List *outersortkeys; /* keys for explicit sort, if any */
  1377. List *innersortkeys; /* keys for explicit sort, if any */
  1378. bool skip_mark_restore; /* can executor skip mark/restore? */
  1379. bool materialize_inner; /* add Materialize to inner? */
  1380. } MergePath;
  1381. /*
  1382. * A hashjoin path has these fields.
  1383. *
  1384. * The remarks above for mergeclauses apply for hashclauses as well.
  1385. *
  1386. * Hashjoin does not care what order its inputs appear in, so we have
  1387. * no need for sortkeys.
  1388. */
  1389. typedef struct HashPath
  1390. {
  1391. JoinPath jpath;
  1392. List *path_hashclauses; /* join clauses used for hashing */
  1393. int num_batches; /* number of batches expected */
  1394. double inner_rows_total; /* total inner rows expected */
  1395. } HashPath;
  1396. /*
  1397. * ProjectionPath represents a projection (that is, targetlist computation)
  1398. *
  1399. * Nominally, this path node represents using a Result plan node to do a
  1400. * projection step. However, if the input plan node supports projection,
  1401. * we can just modify its output targetlist to do the required calculations
  1402. * directly, and not need a Result. In some places in the planner we can just
  1403. * jam the desired PathTarget into the input path node (and adjust its cost
  1404. * accordingly), so we don't need a ProjectionPath. But in other places
  1405. * it's necessary to not modify the input path node, so we need a separate
  1406. * ProjectionPath node, which is marked dummy to indicate that we intend to
  1407. * assign the work to the input plan node. The estimated cost for the
  1408. * ProjectionPath node will account for whether a Result will be used or not.
  1409. */
  1410. typedef struct ProjectionPath
  1411. {
  1412. Path path;
  1413. Path *subpath; /* path representing input source */
  1414. bool dummypp; /* true if no separate Result is needed */
  1415. } ProjectionPath;
  1416. /*
  1417. * ProjectSetPath represents evaluation of a targetlist that includes
  1418. * set-returning function(s), which will need to be implemented by a
  1419. * ProjectSet plan node.
  1420. */
  1421. typedef struct ProjectSetPath
  1422. {
  1423. Path path;
  1424. Path *subpath; /* path representing input source */
  1425. } ProjectSetPath;
  1426. /*
  1427. * SortPath represents an explicit sort step
  1428. *
  1429. * The sort keys are, by definition, the same as path.pathkeys.
  1430. *
  1431. * Note: the Sort plan node cannot project, so path.pathtarget must be the
  1432. * same as the input's pathtarget.
  1433. */
  1434. typedef struct SortPath
  1435. {
  1436. Path path;
  1437. Path *subpath; /* path representing input source */
  1438. } SortPath;
  1439. /*
  1440. * GroupPath represents grouping (of presorted input)
  1441. *
  1442. * groupClause represents the columns to be grouped on; the input path
  1443. * must be at least that well sorted.
  1444. *
  1445. * We can also apply a qual to the grouped rows (equivalent of HAVING)
  1446. */
  1447. typedef struct GroupPath
  1448. {
  1449. Path path;
  1450. Path *subpath; /* path representing input source */
  1451. List *groupClause; /* a list of SortGroupClause's */
  1452. List *qual; /* quals (HAVING quals), if any */
  1453. } GroupPath;
  1454. /*
  1455. * UpperUniquePath represents adjacent-duplicate removal (in presorted input)
  1456. *
  1457. * The columns to be compared are the first numkeys columns of the path's
  1458. * pathkeys. The input is presumed already sorted that way.
  1459. */
  1460. typedef struct UpperUniquePath
  1461. {
  1462. Path path;
  1463. Path *subpath; /* path representing input source */
  1464. int numkeys; /* number of pathkey columns to compare */
  1465. } UpperUniquePath;
  1466. /*
  1467. * AggPath represents generic computation of aggregate functions
  1468. *
  1469. * This may involve plain grouping (but not grouping sets), using either
  1470. * sorted or hashed grouping; for the AGG_SORTED case, the input must be
  1471. * appropriately presorted.
  1472. */
  1473. typedef struct AggPath
  1474. {
  1475. Path path;
  1476. Path *subpath; /* path representing input source */
  1477. AggStrategy aggstrategy; /* basic strategy, see nodes.h */
  1478. AggSplit aggsplit; /* agg-splitting mode, see nodes.h */
  1479. double numGroups; /* estimated number of groups in input */
  1480. List *groupClause; /* a list of SortGroupClause's */
  1481. List *qual; /* quals (HAVING quals), if any */
  1482. } AggPath;
  1483. /*
  1484. * Various annotations used for grouping sets in the planner.
  1485. */
  1486. typedef struct GroupingSetData
  1487. {
  1488. NodeTag type;
  1489. List *set; /* grouping set as list of sortgrouprefs */
  1490. double numGroups; /* est. number of result groups */
  1491. } GroupingSetData;
  1492. typedef struct RollupData
  1493. {
  1494. NodeTag type;
  1495. List *groupClause; /* applicable subset of parse->groupClause */
  1496. List *gsets; /* lists of integer indexes into groupClause */
  1497. List *gsets_data; /* list of GroupingSetData */
  1498. double numGroups; /* est. number of result groups */
  1499. bool hashable; /* can be hashed */
  1500. bool is_hashed; /* to be implemented as a hashagg */
  1501. } RollupData;
  1502. /*
  1503. * GroupingSetsPath represents a GROUPING SETS aggregation
  1504. */
  1505. typedef struct GroupingSetsPath
  1506. {
  1507. Path path;
  1508. Path *subpath; /* path representing input source */
  1509. AggStrategy aggstrategy; /* basic strategy */
  1510. List *rollups; /* list of RollupData */
  1511. List *qual; /* quals (HAVING quals), if any */
  1512. } GroupingSetsPath;
  1513. /*
  1514. * MinMaxAggPath represents computation of MIN/MAX aggregates from indexes
  1515. */
  1516. typedef struct MinMaxAggPath
  1517. {
  1518. Path path;
  1519. List *mmaggregates; /* list of MinMaxAggInfo */
  1520. List *quals; /* HAVING quals, if any */
  1521. } MinMaxAggPath;
  1522. /*
  1523. * WindowAggPath represents generic computation of window functions
  1524. */
  1525. typedef struct WindowAggPath
  1526. {
  1527. Path path;
  1528. Path *subpath; /* path representing input source */
  1529. WindowClause *winclause; /* WindowClause we'll be using */
  1530. } WindowAggPath;
  1531. /*
  1532. * SetOpPath represents a set-operation, that is INTERSECT or EXCEPT
  1533. */
  1534. typedef struct SetOpPath
  1535. {
  1536. Path path;
  1537. Path *subpath; /* path representing input source */
  1538. SetOpCmd cmd; /* what to do, see nodes.h */
  1539. SetOpStrategy strategy; /* how to do it, see nodes.h */
  1540. List *distinctList; /* SortGroupClauses identifying target cols */
  1541. AttrNumber flagColIdx; /* where is the flag column, if any */
  1542. int firstFlag; /* flag value for first input relation */
  1543. double numGroups; /* estimated number of groups in input */
  1544. } SetOpPath;
  1545. /*
  1546. * RecursiveUnionPath represents a recursive UNION node
  1547. */
  1548. typedef struct RecursiveUnionPath
  1549. {
  1550. Path path;
  1551. Path *leftpath; /* paths representing input sources */
  1552. Path *rightpath;
  1553. List *distinctList; /* SortGroupClauses identifying target cols */
  1554. int wtParam; /* ID of Param representing work table */
  1555. double numGroups; /* estimated number of groups in input */
  1556. } RecursiveUnionPath;
  1557. /*
  1558. * LockRowsPath represents acquiring row locks for SELECT FOR UPDATE/SHARE
  1559. */
  1560. typedef struct LockRowsPath
  1561. {
  1562. Path path;
  1563. Path *subpath; /* path representing input source */
  1564. List *rowMarks; /* a list of PlanRowMark's */
  1565. int epqParam; /* ID of Param for EvalPlanQual re-eval */
  1566. } LockRowsPath;
  1567. /*
  1568. * ModifyTablePath represents performing INSERT/UPDATE/DELETE modifications
  1569. *
  1570. * We represent most things that will be in the ModifyTable plan node
  1571. * literally, except we have child Path(s) not Plan(s). But analysis of the
  1572. * OnConflictExpr is deferred to createplan.c, as is collection of FDW data.
  1573. */
  1574. typedef struct ModifyTablePath
  1575. {
  1576. Path path;
  1577. CmdType operation; /* INSERT, UPDATE, or DELETE */
  1578. bool canSetTag; /* do we set the command tag/es_processed? */
  1579. Index nominalRelation; /* Parent RT index for use of EXPLAIN */
  1580. Index rootRelation; /* Root RT index, if target is partitioned */
  1581. bool partColsUpdated; /* some part key in hierarchy updated */
  1582. List *resultRelations; /* integer list of RT indexes */
  1583. List *subpaths; /* Path(s) producing source data */
  1584. List *subroots; /* per-target-table PlannerInfos */
  1585. List *withCheckOptionLists; /* per-target-table WCO lists */
  1586. List *returningLists; /* per-target-table RETURNING tlists */
  1587. List *rowMarks; /* PlanRowMarks (non-locking only) */
  1588. OnConflictExpr *onconflict; /* ON CONFLICT clause, or NULL */
  1589. int epqParam; /* ID of Param for EvalPlanQual re-eval */
  1590. } ModifyTablePath;
  1591. /*
  1592. * LimitPath represents applying LIMIT/OFFSET restrictions
  1593. */
  1594. typedef struct LimitPath
  1595. {
  1596. Path path;
  1597. Path *subpath; /* path representing input source */
  1598. Node *limitOffset; /* OFFSET parameter, or NULL if none */
  1599. Node *limitCount; /* COUNT parameter, or NULL if none */
  1600. } LimitPath;
  1601. /*
  1602. * Restriction clause info.
  1603. *
  1604. * We create one of these for each AND sub-clause of a restriction condition
  1605. * (WHERE or JOIN/ON clause). Since the restriction clauses are logically
  1606. * ANDed, we can use any one of them or any subset of them to filter out
  1607. * tuples, without having to evaluate the rest. The RestrictInfo node itself
  1608. * stores data used by the optimizer while choosing the best query plan.
  1609. *
  1610. * If a restriction clause references a single base relation, it will appear
  1611. * in the baserestrictinfo list of the RelOptInfo for that base rel.
  1612. *
  1613. * If a restriction clause references more than one base rel, it will
  1614. * appear in the joininfo list of every RelOptInfo that describes a strict
  1615. * subset of the base rels mentioned in the clause. The joininfo lists are
  1616. * used to drive join tree building by selecting plausible join candidates.
  1617. * The clause cannot actually be applied until we have built a join rel
  1618. * containing all the base rels it references, however.
  1619. *
  1620. * When we construct a join rel that includes all the base rels referenced
  1621. * in a multi-relation restriction clause, we place that clause into the
  1622. * joinrestrictinfo lists of paths for the join rel, if neither left nor
  1623. * right sub-path includes all base rels referenced in the clause. The clause
  1624. * will be applied at that join level, and will not propagate any further up
  1625. * the join tree. (Note: the "predicate migration" code was once intended to
  1626. * push restriction clauses up and down the plan tree based on evaluation
  1627. * costs, but it's dead code and is unlikely to be resurrected in the
  1628. * foreseeable future.)
  1629. *
  1630. * Note that in the presence of more than two rels, a multi-rel restriction
  1631. * might reach different heights in the join tree depending on the join
  1632. * sequence we use. So, these clauses cannot be associated directly with
  1633. * the join RelOptInfo, but must be kept track of on a per-join-path basis.
  1634. *
  1635. * RestrictInfos that represent equivalence conditions (i.e., mergejoinable
  1636. * equalities that are not outerjoin-delayed) are handled a bit differently.
  1637. * Initially we attach them to the EquivalenceClasses that are derived from
  1638. * them. When we construct a scan or join path, we look through all the
  1639. * EquivalenceClasses and generate derived RestrictInfos representing the
  1640. * minimal set of conditions that need to be checked for this particular scan
  1641. * or join to enforce that all members of each EquivalenceClass are in fact
  1642. * equal in all rows emitted by the scan or join.
  1643. *
  1644. * When dealing with outer joins we have to be very careful about pushing qual
  1645. * clauses up and down the tree. An outer join's own JOIN/ON conditions must
  1646. * be evaluated exactly at that join node, unless they are "degenerate"
  1647. * conditions that reference only Vars from the nullable side of the join.
  1648. * Quals appearing in WHERE or in a JOIN above the outer join cannot be pushed
  1649. * down below the outer join, if they reference any nullable Vars.
  1650. * RestrictInfo nodes contain a flag to indicate whether a qual has been
  1651. * pushed down to a lower level than its original syntactic placement in the
  1652. * join tree would suggest. If an outer join prevents us from pushing a qual
  1653. * down to its "natural" semantic level (the level associated with just the
  1654. * base rels used in the qual) then we mark the qual with a "required_relids"
  1655. * value including more than just the base rels it actually uses. By
  1656. * pretending that the qual references all the rels required to form the outer
  1657. * join, we prevent it from being evaluated below the outer join's joinrel.
  1658. * When we do form the outer join's joinrel, we still need to distinguish
  1659. * those quals that are actually in that join's JOIN/ON condition from those
  1660. * that appeared elsewhere in the tree and were pushed down to the join rel
  1661. * because they used no other rels. That's what the is_pushed_down flag is
  1662. * for; it tells us that a qual is not an OUTER JOIN qual for the set of base
  1663. * rels listed in required_relids. A clause that originally came from WHERE
  1664. * or an INNER JOIN condition will *always* have its is_pushed_down flag set.
  1665. * It's possible for an OUTER JOIN clause to be marked is_pushed_down too,
  1666. * if we decide that it can be pushed down into the nullable side of the join.
  1667. * In that case it acts as a plain filter qual for wherever it gets evaluated.
  1668. * (In short, is_pushed_down is only false for non-degenerate outer join
  1669. * conditions. Possibly we should rename it to reflect that meaning? But
  1670. * see also the comments for RINFO_IS_PUSHED_DOWN, below.)
  1671. *
  1672. * RestrictInfo nodes also contain an outerjoin_delayed flag, which is true
  1673. * if the clause's applicability must be delayed due to any outer joins
  1674. * appearing below it (ie, it has to be postponed to some join level higher
  1675. * than the set of relations it actually references).
  1676. *
  1677. * There is also an outer_relids field, which is NULL except for outer join
  1678. * clauses; for those, it is the set of relids on the outer side of the
  1679. * clause's outer join. (These are rels that the clause cannot be applied to
  1680. * in parameterized scans, since pushing it into the join's outer side would
  1681. * lead to wrong answers.)
  1682. *
  1683. * There is also a nullable_relids field, which is the set of rels the clause
  1684. * references that can be forced null by some outer join below the clause.
  1685. *
  1686. * outerjoin_delayed = true is subtly different from nullable_relids != NULL:
  1687. * a clause might reference some nullable rels and yet not be
  1688. * outerjoin_delayed because it also references all the other rels of the
  1689. * outer join(s). A clause that is not outerjoin_delayed can be enforced
  1690. * anywhere it is computable.
  1691. *
  1692. * To handle security-barrier conditions efficiently, we mark RestrictInfo
  1693. * nodes with a security_level field, in which higher values identify clauses
  1694. * coming from less-trusted sources. The exact semantics are that a clause
  1695. * cannot be evaluated before another clause with a lower security_level value
  1696. * unless the first clause is leakproof. As with outer-join clauses, this
  1697. * creates a reason for clauses to sometimes need to be evaluated higher in
  1698. * the join tree than their contents would suggest; and even at a single plan
  1699. * node, this rule constrains the order of application of clauses.
  1700. *
  1701. * In general, the referenced clause might be arbitrarily complex. The
  1702. * kinds of clauses we can handle as indexscan quals, mergejoin clauses,
  1703. * or hashjoin clauses are limited (e.g., no volatile functions). The code
  1704. * for each kind of path is responsible for identifying the restrict clauses
  1705. * it can use and ignoring the rest. Clauses not implemented by an indexscan,
  1706. * mergejoin, or hashjoin will be placed in the plan qual or joinqual field
  1707. * of the finished Plan node, where they will be enforced by general-purpose
  1708. * qual-expression-evaluation code. (But we are still entitled to count
  1709. * their selectivity when estimating the result tuple count, if we
  1710. * can guess what it is...)
  1711. *
  1712. * When the referenced clause is an OR clause, we generate a modified copy
  1713. * in which additional RestrictInfo nodes are inserted below the top-level
  1714. * OR/AND structure. This is a convenience for OR indexscan processing:
  1715. * indexquals taken from either the top level or an OR subclause will have
  1716. * associated RestrictInfo nodes.
  1717. *
  1718. * The can_join flag is set true if the clause looks potentially useful as
  1719. * a merge or hash join clause, that is if it is a binary opclause with
  1720. * nonoverlapping sets of relids referenced in the left and right sides.
  1721. * (Whether the operator is actually merge or hash joinable isn't checked,
  1722. * however.)
  1723. *
  1724. * The pseudoconstant flag is set true if the clause contains no Vars of
  1725. * the current query level and no volatile functions. Such a clause can be
  1726. * pulled out and used as a one-time qual in a gating Result node. We keep
  1727. * pseudoconstant clauses in the same lists as other RestrictInfos so that
  1728. * the regular clause-pushing machinery can assign them to the correct join
  1729. * level, but they need to be treated specially for cost and selectivity
  1730. * estimates. Note that a pseudoconstant clause can never be an indexqual
  1731. * or merge or hash join clause, so it's of no interest to large parts of
  1732. * the planner.
  1733. *
  1734. * When join clauses are generated from EquivalenceClasses, there may be
  1735. * several equally valid ways to enforce join equivalence, of which we need
  1736. * apply only one. We mark clauses of this kind by setting parent_ec to
  1737. * point to the generating EquivalenceClass. Multiple clauses with the same
  1738. * parent_ec in the same join are redundant.
  1739. */
  1740. typedef struct RestrictInfo
  1741. {
  1742. NodeTag type;
  1743. Expr *clause; /* the represented clause of WHERE or JOIN */
  1744. bool is_pushed_down; /* true if clause was pushed down in level */
  1745. bool outerjoin_delayed; /* true if delayed by lower outer join */
  1746. bool can_join; /* see comment above */
  1747. bool pseudoconstant; /* see comment above */
  1748. bool leakproof; /* true if known to contain no leaked Vars */
  1749. Index security_level; /* see comment above */
  1750. /* The set of relids (varnos) actually referenced in the clause: */
  1751. Relids clause_relids;
  1752. /* The set of relids required to evaluate the clause: */
  1753. Relids required_relids;
  1754. /* If an outer-join clause, the outer-side relations, else NULL: */
  1755. Relids outer_relids;
  1756. /* The relids used in the clause that are nullable by lower outer joins: */
  1757. Relids nullable_relids;
  1758. /* These fields are set for any binary opclause: */
  1759. Relids left_relids; /* relids in left side of clause */
  1760. Relids right_relids; /* relids in right side of clause */
  1761. /* This field is NULL unless clause is an OR clause: */
  1762. Expr *orclause; /* modified clause with RestrictInfos */
  1763. /* This field is NULL unless clause is potentially redundant: */
  1764. EquivalenceClass *parent_ec; /* generating EquivalenceClass */
  1765. /* cache space for cost and selectivity */
  1766. QualCost eval_cost; /* eval cost of clause; -1 if not yet set */
  1767. Selectivity norm_selec; /* selectivity for "normal" (JOIN_INNER)
  1768. * semantics; -1 if not yet set; >1 means a
  1769. * redundant clause */
  1770. Selectivity outer_selec; /* selectivity for outer join semantics; -1 if
  1771. * not yet set */
  1772. /* valid if clause is mergejoinable, else NIL */
  1773. List *mergeopfamilies; /* opfamilies containing clause operator */
  1774. /* cache space for mergeclause processing; NULL if not yet set */
  1775. EquivalenceClass *left_ec; /* EquivalenceClass containing lefthand */
  1776. EquivalenceClass *right_ec; /* EquivalenceClass containing righthand */
  1777. EquivalenceMember *left_em; /* EquivalenceMember for lefthand */
  1778. EquivalenceMember *right_em; /* EquivalenceMember for righthand */
  1779. List *scansel_cache; /* list of MergeScanSelCache structs */
  1780. /* transient workspace for use while considering a specific join path */
  1781. bool outer_is_left; /* T = outer var on left, F = on right */
  1782. /* valid if clause is hashjoinable, else InvalidOid: */
  1783. Oid hashjoinoperator; /* copy of clause operator */
  1784. /* cache space for hashclause processing; -1 if not yet set */
  1785. Selectivity left_bucketsize; /* avg bucketsize of left side */
  1786. Selectivity right_bucketsize; /* avg bucketsize of right side */
  1787. Selectivity left_mcvfreq; /* left side's most common val's freq */
  1788. Selectivity right_mcvfreq; /* right side's most common val's freq */
  1789. } RestrictInfo;
  1790. /*
  1791. * This macro embodies the correct way to test whether a RestrictInfo is
  1792. * "pushed down" to a given outer join, that is, should be treated as a filter
  1793. * clause rather than a join clause at that outer join. This is certainly so
  1794. * if is_pushed_down is true; but examining that is not sufficient anymore,
  1795. * because outer-join clauses will get pushed down to lower outer joins when
  1796. * we generate a path for the lower outer join that is parameterized by the
  1797. * LHS of the upper one. We can detect such a clause by noting that its
  1798. * required_relids exceed the scope of the join.
  1799. */
  1800. #define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids) \
  1801. ((rinfo)->is_pushed_down || \
  1802. !bms_is_subset((rinfo)->required_relids, joinrelids))
  1803. /*
  1804. * Since mergejoinscansel() is a relatively expensive function, and would
  1805. * otherwise be invoked many times while planning a large join tree,
  1806. * we go out of our way to cache its results. Each mergejoinable
  1807. * RestrictInfo carries a list of the specific sort orderings that have
  1808. * been considered for use with it, and the resulting selectivities.
  1809. */
  1810. typedef struct MergeScanSelCache
  1811. {
  1812. /* Ordering details (cache lookup key) */
  1813. Oid opfamily; /* btree opfamily defining the ordering */
  1814. Oid collation; /* collation for the ordering */
  1815. int strategy; /* sort direction (ASC or DESC) */
  1816. bool nulls_first; /* do NULLs come before normal values? */
  1817. /* Results */
  1818. Selectivity leftstartsel; /* first-join fraction for clause left side */
  1819. Selectivity leftendsel; /* last-join fraction for clause left side */
  1820. Selectivity rightstartsel; /* first-join fraction for clause right side */
  1821. Selectivity rightendsel; /* last-join fraction for clause right side */
  1822. } MergeScanSelCache;
  1823. /*
  1824. * Placeholder node for an expression to be evaluated below the top level
  1825. * of a plan tree. This is used during planning to represent the contained
  1826. * expression. At the end of the planning process it is replaced by either
  1827. * the contained expression or a Var referring to a lower-level evaluation of
  1828. * the contained expression. Typically the evaluation occurs below an outer
  1829. * join, and Var references above the outer join might thereby yield NULL
  1830. * instead of the expression value.
  1831. *
  1832. * Although the planner treats this as an expression node type, it is not
  1833. * recognized by the parser or executor, so we declare it here rather than
  1834. * in primnodes.h.
  1835. */
  1836. typedef struct PlaceHolderVar
  1837. {
  1838. Expr xpr;
  1839. Expr *phexpr; /* the represented expression */
  1840. Relids phrels; /* base relids syntactically within expr src */
  1841. Index phid; /* ID for PHV (unique within planner run) */
  1842. Index phlevelsup; /* > 0 if PHV belongs to outer query */
  1843. } PlaceHolderVar;
  1844. /*
  1845. * "Special join" info.
  1846. *
  1847. * One-sided outer joins constrain the order of joining partially but not
  1848. * completely. We flatten such joins into the planner's top-level list of
  1849. * relations to join, but record information about each outer join in a
  1850. * SpecialJoinInfo struct. These structs are kept in the PlannerInfo node's
  1851. * join_info_list.
  1852. *
  1853. * Similarly, semijoins and antijoins created by flattening IN (subselect)
  1854. * and EXISTS(subselect) clauses create partial constraints on join order.
  1855. * These are likewise recorded in SpecialJoinInfo structs.
  1856. *
  1857. * We make SpecialJoinInfos for FULL JOINs even though there is no flexibility
  1858. * of planning for them, because this simplifies make_join_rel()'s API.
  1859. *
  1860. * min_lefthand and min_righthand are the sets of base relids that must be
  1861. * available on each side when performing the special join. lhs_strict is
  1862. * true if the special join's condition cannot succeed when the LHS variables
  1863. * are all NULL (this means that an outer join can commute with upper-level
  1864. * outer joins even if it appears in their RHS). We don't bother to set
  1865. * lhs_strict for FULL JOINs, however.
  1866. *
  1867. * It is not valid for either min_lefthand or min_righthand to be empty sets;
  1868. * if they were, this would break the logic that enforces join order.
  1869. *
  1870. * syn_lefthand and syn_righthand are the sets of base relids that are
  1871. * syntactically below this special join. (These are needed to help compute
  1872. * min_lefthand and min_righthand for higher joins.)
  1873. *
  1874. * delay_upper_joins is set true if we detect a pushed-down clause that has
  1875. * to be evaluated after this join is formed (because it references the RHS).
  1876. * Any outer joins that have such a clause and this join in their RHS cannot
  1877. * commute with this join, because that would leave noplace to check the
  1878. * pushed-down clause. (We don't track this for FULL JOINs, either.)
  1879. *
  1880. * For a semijoin, we also extract the join operators and their RHS arguments
  1881. * and set semi_operators, semi_rhs_exprs, semi_can_btree, and semi_can_hash.
  1882. * This is done in support of possibly unique-ifying the RHS, so we don't
  1883. * bother unless at least one of semi_can_btree and semi_can_hash can be set
  1884. * true. (You might expect that this information would be computed during
  1885. * join planning; but it's helpful to have it available during planning of
  1886. * parameterized table scans, so we store it in the SpecialJoinInfo structs.)
  1887. *
  1888. * jointype is never JOIN_RIGHT; a RIGHT JOIN is handled by switching
  1889. * the inputs to make it a LEFT JOIN. So the allowed values of jointype
  1890. * in a join_info_list member are only LEFT, FULL, SEMI, or ANTI.
  1891. *
  1892. * For purposes of join selectivity estimation, we create transient
  1893. * SpecialJoinInfo structures for regular inner joins; so it is possible
  1894. * to have jointype == JOIN_INNER in such a structure, even though this is
  1895. * not allowed within join_info_list. We also create transient
  1896. * SpecialJoinInfos with jointype == JOIN_INNER for outer joins, since for
  1897. * cost estimation purposes it is sometimes useful to know the join size under
  1898. * plain innerjoin semantics. Note that lhs_strict, delay_upper_joins, and
  1899. * of course the semi_xxx fields are not set meaningfully within such structs.
  1900. */
  1901. #ifndef HAVE_SPECIALJOININFO_TYPEDEF
  1902. typedef struct SpecialJoinInfo SpecialJoinInfo;
  1903. #define HAVE_SPECIALJOININFO_TYPEDEF 1
  1904. #endif
  1905. struct SpecialJoinInfo
  1906. {
  1907. NodeTag type;
  1908. Relids min_lefthand; /* base relids in minimum LHS for join */
  1909. Relids min_righthand; /* base relids in minimum RHS for join */
  1910. Relids syn_lefthand; /* base relids syntactically within LHS */
  1911. Relids syn_righthand; /* base relids syntactically within RHS */
  1912. JoinType jointype; /* always INNER, LEFT, FULL, SEMI, or ANTI */
  1913. bool lhs_strict; /* joinclause is strict for some LHS rel */
  1914. bool delay_upper_joins; /* can't commute with upper RHS */
  1915. /* Remaining fields are set only for JOIN_SEMI jointype: */
  1916. bool semi_can_btree; /* true if semi_operators are all btree */
  1917. bool semi_can_hash; /* true if semi_operators are all hash */
  1918. List *semi_operators; /* OIDs of equality join operators */
  1919. List *semi_rhs_exprs; /* righthand-side expressions of these ops */
  1920. };
  1921. /*
  1922. * Append-relation info.
  1923. *
  1924. * When we expand an inheritable table or a UNION-ALL subselect into an
  1925. * "append relation" (essentially, a list of child RTEs), we build an
  1926. * AppendRelInfo for each child RTE. The list of AppendRelInfos indicates
  1927. * which child RTEs must be included when expanding the parent, and each node
  1928. * carries information needed to translate Vars referencing the parent into
  1929. * Vars referencing that child.
  1930. *
  1931. * These structs are kept in the PlannerInfo node's append_rel_list.
  1932. * Note that we just throw all the structs into one list, and scan the
  1933. * whole list when desiring to expand any one parent. We could have used
  1934. * a more complex data structure (eg, one list per parent), but this would
  1935. * be harder to update during operations such as pulling up subqueries,
  1936. * and not really any easier to scan. Considering that typical queries
  1937. * will not have many different append parents, it doesn't seem worthwhile
  1938. * to complicate things.
  1939. *
  1940. * Note: after completion of the planner prep phase, any given RTE is an
  1941. * append parent having entries in append_rel_list if and only if its
  1942. * "inh" flag is set. We clear "inh" for plain tables that turn out not
  1943. * to have inheritance children, and (in an abuse of the original meaning
  1944. * of the flag) we set "inh" for subquery RTEs that turn out to be
  1945. * flattenable UNION ALL queries. This lets us avoid useless searches
  1946. * of append_rel_list.
  1947. *
  1948. * Note: the data structure assumes that append-rel members are single
  1949. * baserels. This is OK for inheritance, but it prevents us from pulling
  1950. * up a UNION ALL member subquery if it contains a join. While that could
  1951. * be fixed with a more complex data structure, at present there's not much
  1952. * point because no improvement in the plan could result.
  1953. */
  1954. typedef struct AppendRelInfo
  1955. {
  1956. NodeTag type;
  1957. /*
  1958. * These fields uniquely identify this append relationship. There can be
  1959. * (in fact, always should be) multiple AppendRelInfos for the same
  1960. * parent_relid, but never more than one per child_relid, since a given
  1961. * RTE cannot be a child of more than one append parent.
  1962. */
  1963. Index parent_relid; /* RT index of append parent rel */
  1964. Index child_relid; /* RT index of append child rel */
  1965. /*
  1966. * For an inheritance appendrel, the parent and child are both regular
  1967. * relations, and we store their rowtype OIDs here for use in translating
  1968. * whole-row Vars. For a UNION-ALL appendrel, the parent and child are
  1969. * both subqueries with no named rowtype, and we store InvalidOid here.
  1970. */
  1971. Oid parent_reltype; /* OID of parent's composite type */
  1972. Oid child_reltype; /* OID of child's composite type */
  1973. /*
  1974. * The N'th element of this list is a Var or expression representing the
  1975. * child column corresponding to the N'th column of the parent. This is
  1976. * used to translate Vars referencing the parent rel into references to
  1977. * the child. A list element is NULL if it corresponds to a dropped
  1978. * column of the parent (this is only possible for inheritance cases, not
  1979. * UNION ALL). The list elements are always simple Vars for inheritance
  1980. * cases, but can be arbitrary expressions in UNION ALL cases.
  1981. *
  1982. * Notice we only store entries for user columns (attno > 0). Whole-row
  1983. * Vars are special-cased, and system columns (attno < 0) need no special
  1984. * translation since their attnos are the same for all tables.
  1985. *
  1986. * Caution: the Vars have varlevelsup = 0. Be careful to adjust as needed
  1987. * when copying into a subquery.
  1988. */
  1989. List *translated_vars; /* Expressions in the child's Vars */
  1990. /*
  1991. * We store the parent table's OID here for inheritance, or InvalidOid for
  1992. * UNION ALL. This is only needed to help in generating error messages if
  1993. * an attempt is made to reference a dropped parent column.
  1994. */
  1995. Oid parent_reloid; /* OID of parent relation */
  1996. } AppendRelInfo;
  1997. /*
  1998. * For each distinct placeholder expression generated during planning, we
  1999. * store a PlaceHolderInfo node in the PlannerInfo node's placeholder_list.
  2000. * This stores info that is needed centrally rather than in each copy of the
  2001. * PlaceHolderVar. The phid fields identify which PlaceHolderInfo goes with
  2002. * each PlaceHolderVar. Note that phid is unique throughout a planner run,
  2003. * not just within a query level --- this is so that we need not reassign ID's
  2004. * when pulling a subquery into its parent.
  2005. *
  2006. * The idea is to evaluate the expression at (only) the ph_eval_at join level,
  2007. * then allow it to bubble up like a Var until the ph_needed join level.
  2008. * ph_needed has the same definition as attr_needed for a regular Var.
  2009. *
  2010. * The PlaceHolderVar's expression might contain LATERAL references to vars
  2011. * coming from outside its syntactic scope. If so, those rels are *not*
  2012. * included in ph_eval_at, but they are recorded in ph_lateral.
  2013. *
  2014. * Notice that when ph_eval_at is a join rather than a single baserel, the
  2015. * PlaceHolderInfo may create constraints on join order: the ph_eval_at join
  2016. * has to be formed below any outer joins that should null the PlaceHolderVar.
  2017. *
  2018. * We create a PlaceHolderInfo only after determining that the PlaceHolderVar
  2019. * is actually referenced in the plan tree, so that unreferenced placeholders
  2020. * don't result in unnecessary constraints on join order.
  2021. */
  2022. typedef struct PlaceHolderInfo
  2023. {
  2024. NodeTag type;
  2025. Index phid; /* ID for PH (unique within planner run) */
  2026. PlaceHolderVar *ph_var; /* copy of PlaceHolderVar tree */
  2027. Relids ph_eval_at; /* lowest level we can evaluate value at */
  2028. Relids ph_lateral; /* relids of contained lateral refs, if any */
  2029. Relids ph_needed; /* highest level the value is needed at */
  2030. int32 ph_width; /* estimated attribute width */
  2031. } PlaceHolderInfo;
  2032. /*
  2033. * This struct describes one potentially index-optimizable MIN/MAX aggregate
  2034. * function. MinMaxAggPath contains a list of these, and if we accept that
  2035. * path, the list is stored into root->minmax_aggs for use during setrefs.c.
  2036. */
  2037. typedef struct MinMaxAggInfo
  2038. {
  2039. NodeTag type;
  2040. Oid aggfnoid; /* pg_proc Oid of the aggregate */
  2041. Oid aggsortop; /* Oid of its sort operator */
  2042. Expr *target; /* expression we are aggregating on */
  2043. PlannerInfo *subroot; /* modified "root" for planning the subquery */
  2044. Path *path; /* access path for subquery */
  2045. Cost pathcost; /* estimated cost to fetch first row */
  2046. Param *param; /* param for subplan's output */
  2047. } MinMaxAggInfo;
  2048. /*
  2049. * At runtime, PARAM_EXEC slots are used to pass values around from one plan
  2050. * node to another. They can be used to pass values down into subqueries (for
  2051. * outer references in subqueries), or up out of subqueries (for the results
  2052. * of a subplan), or from a NestLoop plan node into its inner relation (when
  2053. * the inner scan is parameterized with values from the outer relation).
  2054. * The planner is responsible for assigning nonconflicting PARAM_EXEC IDs to
  2055. * the PARAM_EXEC Params it generates.
  2056. *
  2057. * Outer references are managed via root->plan_params, which is a list of
  2058. * PlannerParamItems. While planning a subquery, each parent query level's
  2059. * plan_params contains the values required from it by the current subquery.
  2060. * During create_plan(), we use plan_params to track values that must be
  2061. * passed from outer to inner sides of NestLoop plan nodes.
  2062. *
  2063. * The item a PlannerParamItem represents can be one of three kinds:
  2064. *
  2065. * A Var: the slot represents a variable of this level that must be passed
  2066. * down because subqueries have outer references to it, or must be passed
  2067. * from a NestLoop node to its inner scan. The varlevelsup value in the Var
  2068. * will always be zero.
  2069. *
  2070. * A PlaceHolderVar: this works much like the Var case, except that the
  2071. * entry is a PlaceHolderVar node with a contained expression. The PHV
  2072. * will have phlevelsup = 0, and the contained expression is adjusted
  2073. * to match in level.
  2074. *
  2075. * An Aggref (with an expression tree representing its argument): the slot
  2076. * represents an aggregate expression that is an outer reference for some
  2077. * subquery. The Aggref itself has agglevelsup = 0, and its argument tree
  2078. * is adjusted to match in level.
  2079. *
  2080. * Note: we detect duplicate Var and PlaceHolderVar parameters and coalesce
  2081. * them into one slot, but we do not bother to do that for Aggrefs.
  2082. * The scope of duplicate-elimination only extends across the set of
  2083. * parameters passed from one query level into a single subquery, or for
  2084. * nestloop parameters across the set of nestloop parameters used in a single
  2085. * query level. So there is no possibility of a PARAM_EXEC slot being used
  2086. * for conflicting purposes.
  2087. *
  2088. * In addition, PARAM_EXEC slots are assigned for Params representing outputs
  2089. * from subplans (values that are setParam items for those subplans). These
  2090. * IDs need not be tracked via PlannerParamItems, since we do not need any
  2091. * duplicate-elimination nor later processing of the represented expressions.
  2092. * Instead, we just record the assignment of the slot number by appending to
  2093. * root->glob->paramExecTypes.
  2094. */
  2095. typedef struct PlannerParamItem
  2096. {
  2097. NodeTag type;
  2098. Node *item; /* the Var, PlaceHolderVar, or Aggref */
  2099. int paramId; /* its assigned PARAM_EXEC slot number */
  2100. } PlannerParamItem;
  2101. /*
  2102. * When making cost estimates for a SEMI/ANTI/inner_unique join, there are
  2103. * some correction factors that are needed in both nestloop and hash joins
  2104. * to account for the fact that the executor can stop scanning inner rows
  2105. * as soon as it finds a match to the current outer row. These numbers
  2106. * depend only on the selected outer and inner join relations, not on the
  2107. * particular paths used for them, so it's worthwhile to calculate them
  2108. * just once per relation pair not once per considered path. This struct
  2109. * is filled by compute_semi_anti_join_factors and must be passed along
  2110. * to the join cost estimation functions.
  2111. *
  2112. * outer_match_frac is the fraction of the outer tuples that are
  2113. * expected to have at least one match.
  2114. * match_count is the average number of matches expected for
  2115. * outer tuples that have at least one match.
  2116. */
  2117. typedef struct SemiAntiJoinFactors
  2118. {
  2119. Selectivity outer_match_frac;
  2120. Selectivity match_count;
  2121. } SemiAntiJoinFactors;
  2122. /*
  2123. * Struct for extra information passed to subroutines of add_paths_to_joinrel
  2124. *
  2125. * restrictlist contains all of the RestrictInfo nodes for restriction
  2126. * clauses that apply to this join
  2127. * mergeclause_list is a list of RestrictInfo nodes for available
  2128. * mergejoin clauses in this join
  2129. * inner_unique is true if each outer tuple provably matches no more
  2130. * than one inner tuple
  2131. * sjinfo is extra info about special joins for selectivity estimation
  2132. * semifactors is as shown above (only valid for SEMI/ANTI/inner_unique joins)
  2133. * param_source_rels are OK targets for parameterization of result paths
  2134. */
  2135. typedef struct JoinPathExtraData
  2136. {
  2137. List *restrictlist;
  2138. List *mergeclause_list;
  2139. bool inner_unique;
  2140. SpecialJoinInfo *sjinfo;
  2141. SemiAntiJoinFactors semifactors;
  2142. Relids param_source_rels;
  2143. } JoinPathExtraData;
  2144. /*
  2145. * Various flags indicating what kinds of grouping are possible.
  2146. *
  2147. * GROUPING_CAN_USE_SORT should be set if it's possible to perform
  2148. * sort-based implementations of grouping. When grouping sets are in use,
  2149. * this will be true if sorting is potentially usable for any of the grouping
  2150. * sets, even if it's not usable for all of them.
  2151. *
  2152. * GROUPING_CAN_USE_HASH should be set if it's possible to perform
  2153. * hash-based implementations of grouping.
  2154. *
  2155. * GROUPING_CAN_PARTIAL_AGG should be set if the aggregation is of a type
  2156. * for which we support partial aggregation (not, for example, grouping sets).
  2157. * It says nothing about parallel-safety or the availability of suitable paths.
  2158. */
  2159. #define GROUPING_CAN_USE_SORT 0x0001
  2160. #define GROUPING_CAN_USE_HASH 0x0002
  2161. #define GROUPING_CAN_PARTIAL_AGG 0x0004
  2162. /*
  2163. * What kind of partitionwise aggregation is in use?
  2164. *
  2165. * PARTITIONWISE_AGGREGATE_NONE: Not used.
  2166. *
  2167. * PARTITIONWISE_AGGREGATE_FULL: Aggregate each partition separately, and
  2168. * append the results.
  2169. *
  2170. * PARTITIONWISE_AGGREGATE_PARTIAL: Partially aggregate each partition
  2171. * separately, append the results, and then finalize aggregation.
  2172. */
  2173. typedef enum
  2174. {
  2175. PARTITIONWISE_AGGREGATE_NONE,
  2176. PARTITIONWISE_AGGREGATE_FULL,
  2177. PARTITIONWISE_AGGREGATE_PARTIAL
  2178. } PartitionwiseAggregateType;
  2179. /*
  2180. * Struct for extra information passed to subroutines of create_grouping_paths
  2181. *
  2182. * flags indicating what kinds of grouping are possible.
  2183. * partial_costs_set is true if the agg_partial_costs and agg_final_costs
  2184. * have been initialized.
  2185. * agg_partial_costs gives partial aggregation costs.
  2186. * agg_final_costs gives finalization costs.
  2187. * target_parallel_safe is true if target is parallel safe.
  2188. * havingQual gives list of quals to be applied after aggregation.
  2189. * targetList gives list of columns to be projected.
  2190. * patype is the type of partitionwise aggregation that is being performed.
  2191. */
  2192. typedef struct
  2193. {
  2194. /* Data which remains constant once set. */
  2195. int flags;
  2196. bool partial_costs_set;
  2197. AggClauseCosts agg_partial_costs;
  2198. AggClauseCosts agg_final_costs;
  2199. /* Data which may differ across partitions. */
  2200. bool target_parallel_safe;
  2201. Node *havingQual;
  2202. List *targetList;
  2203. PartitionwiseAggregateType patype;
  2204. } GroupPathExtraData;
  2205. /*
  2206. * Struct for extra information passed to subroutines of grouping_planner
  2207. *
  2208. * limit_needed is true if we actually need a Limit plan node.
  2209. * limit_tuples is an estimated bound on the number of output tuples,
  2210. * or -1 if no LIMIT or couldn't estimate.
  2211. * count_est and offset_est are the estimated values of the LIMIT and OFFSET
  2212. * expressions computed by preprocess_limit() (see comments for
  2213. * preprocess_limit() for more information).
  2214. */
  2215. typedef struct
  2216. {
  2217. bool limit_needed;
  2218. double limit_tuples;
  2219. int64 count_est;
  2220. int64 offset_est;
  2221. } FinalPathExtraData;
  2222. /*
  2223. * For speed reasons, cost estimation for join paths is performed in two
  2224. * phases: the first phase tries to quickly derive a lower bound for the
  2225. * join cost, and then we check if that's sufficient to reject the path.
  2226. * If not, we come back for a more refined cost estimate. The first phase
  2227. * fills a JoinCostWorkspace struct with its preliminary cost estimates
  2228. * and possibly additional intermediate values. The second phase takes
  2229. * these values as inputs to avoid repeating work.
  2230. *
  2231. * (Ideally we'd declare this in cost.h, but it's also needed in pathnode.h,
  2232. * so seems best to put it here.)
  2233. */
  2234. typedef struct JoinCostWorkspace
  2235. {
  2236. /* Preliminary cost estimates --- must not be larger than final ones! */
  2237. Cost startup_cost; /* cost expended before fetching any tuples */
  2238. Cost total_cost; /* total cost (assuming all tuples fetched) */
  2239. /* Fields below here should be treated as private to costsize.c */
  2240. Cost run_cost; /* non-startup cost components */
  2241. /* private for cost_nestloop code */
  2242. Cost inner_run_cost; /* also used by cost_mergejoin code */
  2243. Cost inner_rescan_run_cost;
  2244. /* private for cost_mergejoin code */
  2245. double outer_rows;
  2246. double inner_rows;
  2247. double outer_skip_rows;
  2248. double inner_skip_rows;
  2249. /* private for cost_hashjoin code */
  2250. int numbuckets;
  2251. int numbatches;
  2252. double inner_rows_total;
  2253. } JoinCostWorkspace;
  2254. #endif /* PATHNODES_H */
上海开阖软件有限公司 沪ICP备12045867号-1