Revert commit 66c0185a3 and follow-on patches.
[pgsql.git] / src / include / nodes / pathnodes.h
blob6ec81637c1d552aa2d3483802f138b92fafd9807
1 /*-------------------------------------------------------------------------
3 * pathnodes.h
4 * Definitions for planner's internal data structures, especially Paths.
6 * We don't support copying RelOptInfo, IndexOptInfo, or Path nodes.
7 * There are some subsidiary structs that are useful to copy, though.
9 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/include/nodes/pathnodes.h
14 *-------------------------------------------------------------------------
16 #ifndef PATHNODES_H
17 #define PATHNODES_H
19 #include "access/sdir.h"
20 #include "lib/stringinfo.h"
21 #include "nodes/params.h"
22 #include "nodes/parsenodes.h"
23 #include "storage/block.h"
27 * Relids
28 * Set of relation identifiers (indexes into the rangetable).
30 typedef Bitmapset *Relids;
33 * When looking for a "cheapest path", this enum specifies whether we want
34 * cheapest startup cost or cheapest total cost.
36 typedef enum CostSelector
38 STARTUP_COST, TOTAL_COST
39 } CostSelector;
42 * The cost estimate produced by cost_qual_eval() includes both a one-time
43 * (startup) cost, and a per-tuple cost.
45 typedef struct QualCost
47 Cost startup; /* one-time cost */
48 Cost per_tuple; /* per-evaluation cost */
49 } QualCost;
52 * Costing aggregate function execution requires these statistics about
53 * the aggregates to be executed by a given Agg node. Note that the costs
54 * include the execution costs of the aggregates' argument expressions as
55 * well as the aggregate functions themselves. Also, the fields must be
56 * defined so that initializing the struct to zeroes with memset is correct.
58 typedef struct AggClauseCosts
60 QualCost transCost; /* total per-input-row execution costs */
61 QualCost finalCost; /* total per-aggregated-row costs */
62 Size transitionSpace; /* space for pass-by-ref transition data */
63 } AggClauseCosts;
66 * This enum identifies the different types of "upper" (post-scan/join)
67 * relations that we might deal with during planning.
69 typedef enum UpperRelationKind
71 UPPERREL_SETOP, /* result of UNION/INTERSECT/EXCEPT, if any */
72 UPPERREL_PARTIAL_GROUP_AGG, /* result of partial grouping/aggregation, if
73 * any */
74 UPPERREL_GROUP_AGG, /* result of grouping/aggregation, if any */
75 UPPERREL_WINDOW, /* result of window functions, if any */
76 UPPERREL_PARTIAL_DISTINCT, /* result of partial "SELECT DISTINCT", if any */
77 UPPERREL_DISTINCT, /* result of "SELECT DISTINCT", if any */
78 UPPERREL_ORDERED, /* result of ORDER BY, if any */
79 UPPERREL_FINAL, /* result of any remaining top-level actions */
80 /* NB: UPPERREL_FINAL must be last enum entry; it's used to size arrays */
81 } UpperRelationKind;
83 /*----------
84 * PlannerGlobal
85 * Global information for planning/optimization
87 * PlannerGlobal holds state for an entire planner invocation; this state
88 * is shared across all levels of sub-Queries that exist in the command being
89 * planned.
91 * Not all fields are printed. (In some cases, there is no print support for
92 * the field type; in others, doing so would lead to infinite recursion.)
93 *----------
95 typedef struct PlannerGlobal
97 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
99 NodeTag type;
101 /* Param values provided to planner() */
102 ParamListInfo boundParams pg_node_attr(read_write_ignore);
104 /* Plans for SubPlan nodes */
105 List *subplans;
107 /* Paths from which the SubPlan Plans were made */
108 List *subpaths;
110 /* PlannerInfos for SubPlan nodes */
111 List *subroots pg_node_attr(read_write_ignore);
113 /* indices of subplans that require REWIND */
114 Bitmapset *rewindPlanIDs;
116 /* "flat" rangetable for executor */
117 List *finalrtable;
119 /* "flat" list of RTEPermissionInfos */
120 List *finalrteperminfos;
122 /* "flat" list of PlanRowMarks */
123 List *finalrowmarks;
125 /* "flat" list of integer RT indexes */
126 List *resultRelations;
128 /* "flat" list of AppendRelInfos */
129 List *appendRelations;
131 /* OIDs of relations the plan depends on */
132 List *relationOids;
134 /* other dependencies, as PlanInvalItems */
135 List *invalItems;
137 /* type OIDs for PARAM_EXEC Params */
138 List *paramExecTypes;
140 /* highest PlaceHolderVar ID assigned */
141 Index lastPHId;
143 /* highest PlanRowMark ID assigned */
144 Index lastRowMarkId;
146 /* highest plan node ID assigned */
147 int lastPlanNodeId;
149 /* redo plan when TransactionXmin changes? */
150 bool transientPlan;
152 /* is plan specific to current role? */
153 bool dependsOnRole;
155 /* parallel mode potentially OK? */
156 bool parallelModeOK;
158 /* parallel mode actually required? */
159 bool parallelModeNeeded;
161 /* worst PROPARALLEL hazard level */
162 char maxParallelHazard;
164 /* partition descriptors */
165 PartitionDirectory partition_directory pg_node_attr(read_write_ignore);
166 } PlannerGlobal;
168 /* macro for fetching the Plan associated with a SubPlan node */
169 #define planner_subplan_get_plan(root, subplan) \
170 ((Plan *) list_nth((root)->glob->subplans, (subplan)->plan_id - 1))
173 /*----------
174 * PlannerInfo
175 * Per-query information for planning/optimization
177 * This struct is conventionally called "root" in all the planner routines.
178 * It holds links to all of the planner's working state, in addition to the
179 * original Query. Note that at present the planner extensively modifies
180 * the passed-in Query data structure; someday that should stop.
182 * For reasons explained in optimizer/optimizer.h, we define the typedef
183 * either here or in that header, whichever is read first.
185 * Not all fields are printed. (In some cases, there is no print support for
186 * the field type; in others, doing so would lead to infinite recursion or
187 * bloat dump output more than seems useful.)
188 *----------
190 #ifndef HAVE_PLANNERINFO_TYPEDEF
191 typedef struct PlannerInfo PlannerInfo;
192 #define HAVE_PLANNERINFO_TYPEDEF 1
193 #endif
195 struct PlannerInfo
197 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
199 NodeTag type;
201 /* the Query being planned */
202 Query *parse;
204 /* global info for current planner run */
205 PlannerGlobal *glob;
207 /* 1 at the outermost Query */
208 Index query_level;
210 /* NULL at outermost Query */
211 PlannerInfo *parent_root pg_node_attr(read_write_ignore);
214 * plan_params contains the expressions that this query level needs to
215 * make available to a lower query level that is currently being planned.
216 * outer_params contains the paramIds of PARAM_EXEC Params that outer
217 * query levels will make available to this query level.
219 /* list of PlannerParamItems, see below */
220 List *plan_params;
221 Bitmapset *outer_params;
224 * simple_rel_array holds pointers to "base rels" and "other rels" (see
225 * comments for RelOptInfo for more info). It is indexed by rangetable
226 * index (so entry 0 is always wasted). Entries can be NULL when an RTE
227 * does not correspond to a base relation, such as a join RTE or an
228 * unreferenced view RTE; or if the RelOptInfo hasn't been made yet.
230 struct RelOptInfo **simple_rel_array pg_node_attr(array_size(simple_rel_array_size));
231 /* allocated size of array */
232 int simple_rel_array_size;
235 * simple_rte_array is the same length as simple_rel_array and holds
236 * pointers to the associated rangetable entries. Using this is a shade
237 * faster than using rt_fetch(), mostly due to fewer indirections. (Not
238 * printed because it'd be redundant with parse->rtable.)
240 RangeTblEntry **simple_rte_array pg_node_attr(read_write_ignore);
243 * append_rel_array is the same length as the above arrays, and holds
244 * pointers to the corresponding AppendRelInfo entry indexed by
245 * child_relid, or NULL if the rel is not an appendrel child. The array
246 * itself is not allocated if append_rel_list is empty. (Not printed
247 * because it'd be redundant with append_rel_list.)
249 struct AppendRelInfo **append_rel_array pg_node_attr(read_write_ignore);
252 * all_baserels is a Relids set of all base relids (but not joins or
253 * "other" rels) in the query. This is computed in deconstruct_jointree.
255 Relids all_baserels;
258 * outer_join_rels is a Relids set of all outer-join relids in the query.
259 * This is computed in deconstruct_jointree.
261 Relids outer_join_rels;
264 * all_query_rels is a Relids set of all base relids and outer join relids
265 * (but not "other" relids) in the query. This is the Relids identifier
266 * of the final join we need to form. This is computed in
267 * deconstruct_jointree.
269 Relids all_query_rels;
272 * join_rel_list is a list of all join-relation RelOptInfos we have
273 * considered in this planning run. For small problems we just scan the
274 * list to do lookups, but when there are many join relations we build a
275 * hash table for faster lookups. The hash table is present and valid
276 * when join_rel_hash is not NULL. Note that we still maintain the list
277 * even when using the hash table for lookups; this simplifies life for
278 * GEQO.
280 List *join_rel_list;
281 struct HTAB *join_rel_hash pg_node_attr(read_write_ignore);
284 * When doing a dynamic-programming-style join search, join_rel_level[k]
285 * is a list of all join-relation RelOptInfos of level k, and
286 * join_cur_level is the current level. New join-relation RelOptInfos are
287 * automatically added to the join_rel_level[join_cur_level] list.
288 * join_rel_level is NULL if not in use.
290 * Note: we've already printed all baserel and joinrel RelOptInfos above,
291 * so we don't dump join_rel_level or other lists of RelOptInfos.
293 /* lists of join-relation RelOptInfos */
294 List **join_rel_level pg_node_attr(read_write_ignore);
295 /* index of list being extended */
296 int join_cur_level;
298 /* init SubPlans for query */
299 List *init_plans;
302 * per-CTE-item list of subplan IDs (or -1 if no subplan was made for that
303 * CTE)
305 List *cte_plan_ids;
307 /* List of Lists of Params for MULTIEXPR subquery outputs */
308 List *multiexpr_params;
310 /* list of JoinDomains used in the query (higher ones first) */
311 List *join_domains;
313 /* list of active EquivalenceClasses */
314 List *eq_classes;
316 /* set true once ECs are canonical */
317 bool ec_merging_done;
319 /* list of "canonical" PathKeys */
320 List *canon_pathkeys;
323 * list of OuterJoinClauseInfos for mergejoinable outer join clauses
324 * w/nonnullable var on left
326 List *left_join_clauses;
329 * list of OuterJoinClauseInfos for mergejoinable outer join clauses
330 * w/nonnullable var on right
332 List *right_join_clauses;
335 * list of OuterJoinClauseInfos for mergejoinable full join clauses
337 List *full_join_clauses;
339 /* list of SpecialJoinInfos */
340 List *join_info_list;
342 /* counter for assigning RestrictInfo serial numbers */
343 int last_rinfo_serial;
346 * all_result_relids is empty for SELECT, otherwise it contains at least
347 * parse->resultRelation. For UPDATE/DELETE/MERGE across an inheritance
348 * or partitioning tree, the result rel's child relids are added. When
349 * using multi-level partitioning, intermediate partitioned rels are
350 * included. leaf_result_relids is similar except that only actual result
351 * tables, not partitioned tables, are included in it.
353 /* set of all result relids */
354 Relids all_result_relids;
355 /* set of all leaf relids */
356 Relids leaf_result_relids;
359 * list of AppendRelInfos
361 * Note: for AppendRelInfos describing partitions of a partitioned table,
362 * we guarantee that partitions that come earlier in the partitioned
363 * table's PartitionDesc will appear earlier in append_rel_list.
365 List *append_rel_list;
367 /* list of RowIdentityVarInfos */
368 List *row_identity_vars;
370 /* list of PlanRowMarks */
371 List *rowMarks;
373 /* list of PlaceHolderInfos */
374 List *placeholder_list;
376 /* array of PlaceHolderInfos indexed by phid */
377 struct PlaceHolderInfo **placeholder_array pg_node_attr(read_write_ignore, array_size(placeholder_array_size));
378 /* allocated size of array */
379 int placeholder_array_size pg_node_attr(read_write_ignore);
381 /* list of ForeignKeyOptInfos */
382 List *fkey_list;
384 /* desired pathkeys for query_planner() */
385 List *query_pathkeys;
387 /* groupClause pathkeys, if any */
388 List *group_pathkeys;
391 * The number of elements in the group_pathkeys list which belong to the
392 * GROUP BY clause. Additional ones belong to ORDER BY / DISTINCT
393 * aggregates.
395 int num_groupby_pathkeys;
397 /* pathkeys of bottom window, if any */
398 List *window_pathkeys;
399 /* distinctClause pathkeys, if any */
400 List *distinct_pathkeys;
401 /* sortClause pathkeys, if any */
402 List *sort_pathkeys;
404 /* Canonicalised partition schemes used in the query. */
405 List *part_schemes pg_node_attr(read_write_ignore);
407 /* RelOptInfos we are now trying to join */
408 List *initial_rels pg_node_attr(read_write_ignore);
411 * Upper-rel RelOptInfos. Use fetch_upper_rel() to get any particular
412 * upper rel.
414 List *upper_rels[UPPERREL_FINAL + 1] pg_node_attr(read_write_ignore);
416 /* Result tlists chosen by grouping_planner for upper-stage processing */
417 struct PathTarget *upper_targets[UPPERREL_FINAL + 1] pg_node_attr(read_write_ignore);
420 * The fully-processed groupClause is kept here. It differs from
421 * parse->groupClause in that we remove any items that we can prove
422 * redundant, so that only the columns named here actually need to be
423 * compared to determine grouping. Note that it's possible for *all* the
424 * items to be proven redundant, implying that there is only one group
425 * containing all the query's rows. Hence, if you want to check whether
426 * GROUP BY was specified, test for nonempty parse->groupClause, not for
427 * nonempty processed_groupClause.
429 * Currently, when grouping sets are specified we do not attempt to
430 * optimize the groupClause, so that processed_groupClause will be
431 * identical to parse->groupClause.
433 List *processed_groupClause;
436 * The fully-processed distinctClause is kept here. It differs from
437 * parse->distinctClause in that we remove any items that we can prove
438 * redundant, so that only the columns named here actually need to be
439 * compared to determine uniqueness. Note that it's possible for *all*
440 * the items to be proven redundant, implying that there should be only
441 * one output row. Hence, if you want to check whether DISTINCT was
442 * specified, test for nonempty parse->distinctClause, not for nonempty
443 * processed_distinctClause.
445 List *processed_distinctClause;
448 * The fully-processed targetlist is kept here. It differs from
449 * parse->targetList in that (for INSERT) it's been reordered to match the
450 * target table, and defaults have been filled in. Also, additional
451 * resjunk targets may be present. preprocess_targetlist() does most of
452 * that work, but note that more resjunk targets can get added during
453 * appendrel expansion. (Hence, upper_targets mustn't get set up till
454 * after that.)
456 List *processed_tlist;
459 * For UPDATE, this list contains the target table's attribute numbers to
460 * which the first N entries of processed_tlist are to be assigned. (Any
461 * additional entries in processed_tlist must be resjunk.) DO NOT use the
462 * resnos in processed_tlist to identify the UPDATE target columns.
464 List *update_colnos;
467 * Fields filled during create_plan() for use in setrefs.c
469 /* for GroupingFunc fixup (can't print: array length not known here) */
470 AttrNumber *grouping_map pg_node_attr(read_write_ignore);
471 /* List of MinMaxAggInfos */
472 List *minmax_aggs;
474 /* context holding PlannerInfo */
475 MemoryContext planner_cxt pg_node_attr(read_write_ignore);
477 /* # of pages in all non-dummy tables of query */
478 Cardinality total_table_pages;
480 /* tuple_fraction passed to query_planner */
481 Selectivity tuple_fraction;
482 /* limit_tuples passed to query_planner */
483 Cardinality limit_tuples;
486 * Minimum security_level for quals. Note: qual_security_level is zero if
487 * there are no securityQuals.
489 Index qual_security_level;
491 /* true if any RTEs are RTE_JOIN kind */
492 bool hasJoinRTEs;
493 /* true if any RTEs are marked LATERAL */
494 bool hasLateralRTEs;
495 /* true if havingQual was non-null */
496 bool hasHavingQual;
497 /* true if any RestrictInfo has pseudoconstant = true */
498 bool hasPseudoConstantQuals;
499 /* true if we've made any of those */
500 bool hasAlternativeSubPlans;
501 /* true once we're no longer allowed to add PlaceHolderInfos */
502 bool placeholdersFrozen;
503 /* true if planning a recursive WITH item */
504 bool hasRecursion;
507 * Information about aggregates. Filled by preprocess_aggrefs().
509 /* AggInfo structs */
510 List *agginfos;
511 /* AggTransInfo structs */
512 List *aggtransinfos;
513 /* number of aggs with DISTINCT/ORDER BY/WITHIN GROUP */
514 int numOrderedAggs;
515 /* does any agg not support partial mode? */
516 bool hasNonPartialAggs;
517 /* is any partial agg non-serializable? */
518 bool hasNonSerialAggs;
521 * These fields are used only when hasRecursion is true:
523 /* PARAM_EXEC ID for the work table */
524 int wt_param_id;
525 /* a path for non-recursive term */
526 struct Path *non_recursive_path;
529 * These fields are workspace for createplan.c
531 /* outer rels above current node */
532 Relids curOuterRels;
533 /* not-yet-assigned NestLoopParams */
534 List *curOuterParams;
537 * These fields are workspace for setrefs.c. Each is an array
538 * corresponding to glob->subplans. (We could probably teach
539 * gen_node_support.pl how to determine the array length, but it doesn't
540 * seem worth the trouble, so just mark them read_write_ignore.)
542 bool *isAltSubplan pg_node_attr(read_write_ignore);
543 bool *isUsedSubplan pg_node_attr(read_write_ignore);
545 /* optional private data for join_search_hook, e.g., GEQO */
546 void *join_search_private pg_node_attr(read_write_ignore);
548 /* Does this query modify any partition key columns? */
549 bool partColsUpdated;
554 * In places where it's known that simple_rte_array[] must have been prepared
555 * already, we just index into it to fetch RTEs. In code that might be
556 * executed before or after entering query_planner(), use this macro.
558 #define planner_rt_fetch(rti, root) \
559 ((root)->simple_rte_array ? (root)->simple_rte_array[rti] : \
560 rt_fetch(rti, (root)->parse->rtable))
563 * If multiple relations are partitioned the same way, all such partitions
564 * will have a pointer to the same PartitionScheme. A list of PartitionScheme
565 * objects is attached to the PlannerInfo. By design, the partition scheme
566 * incorporates only the general properties of the partition method (LIST vs.
567 * RANGE, number of partitioning columns and the type information for each)
568 * and not the specific bounds.
570 * We store the opclass-declared input data types instead of the partition key
571 * datatypes since the former rather than the latter are used to compare
572 * partition bounds. Since partition key data types and the opclass declared
573 * input data types are expected to be binary compatible (per ResolveOpClass),
574 * both of those should have same byval and length properties.
576 typedef struct PartitionSchemeData
578 char strategy; /* partition strategy */
579 int16 partnatts; /* number of partition attributes */
580 Oid *partopfamily; /* OIDs of operator families */
581 Oid *partopcintype; /* OIDs of opclass declared input data types */
582 Oid *partcollation; /* OIDs of partitioning collations */
584 /* Cached information about partition key data types. */
585 int16 *parttyplen;
586 bool *parttypbyval;
588 /* Cached information about partition comparison functions. */
589 struct FmgrInfo *partsupfunc;
590 } PartitionSchemeData;
592 typedef struct PartitionSchemeData *PartitionScheme;
594 /*----------
595 * RelOptInfo
596 * Per-relation information for planning/optimization
598 * For planning purposes, a "base rel" is either a plain relation (a table)
599 * or the output of a sub-SELECT or function that appears in the range table.
600 * In either case it is uniquely identified by an RT index. A "joinrel"
601 * is the joining of two or more base rels. A joinrel is identified by
602 * the set of RT indexes for its component baserels, along with RT indexes
603 * for any outer joins it has computed. We create RelOptInfo nodes for each
604 * baserel and joinrel, and store them in the PlannerInfo's simple_rel_array
605 * and join_rel_list respectively.
607 * Note that there is only one joinrel for any given set of component
608 * baserels, no matter what order we assemble them in; so an unordered
609 * set is the right datatype to identify it with.
611 * We also have "other rels", which are like base rels in that they refer to
612 * single RT indexes; but they are not part of the join tree, and are given
613 * a different RelOptKind to identify them.
614 * Currently the only kind of otherrels are those made for member relations
615 * of an "append relation", that is an inheritance set or UNION ALL subquery.
616 * An append relation has a parent RTE that is a base rel, which represents
617 * the entire append relation. The member RTEs are otherrels. The parent
618 * is present in the query join tree but the members are not. The member
619 * RTEs and otherrels are used to plan the scans of the individual tables or
620 * subqueries of the append set; then the parent baserel is given Append
621 * and/or MergeAppend paths comprising the best paths for the individual
622 * member rels. (See comments for AppendRelInfo for more information.)
624 * At one time we also made otherrels to represent join RTEs, for use in
625 * handling join alias Vars. Currently this is not needed because all join
626 * alias Vars are expanded to non-aliased form during preprocess_expression.
628 * We also have relations representing joins between child relations of
629 * different partitioned tables. These relations are not added to
630 * join_rel_level lists as they are not joined directly by the dynamic
631 * programming algorithm.
633 * There is also a RelOptKind for "upper" relations, which are RelOptInfos
634 * that describe post-scan/join processing steps, such as aggregation.
635 * Many of the fields in these RelOptInfos are meaningless, but their Path
636 * fields always hold Paths showing ways to do that processing step.
638 * Parts of this data structure are specific to various scan and join
639 * mechanisms. It didn't seem worth creating new node types for them.
641 * relids - Set of relation identifiers (RT indexes). This is a base
642 * relation if there is just one, a join relation if more;
643 * in the join case, RT indexes of any outer joins formed
644 * at or below this join are included along with baserels
645 * rows - estimated number of tuples in the relation after restriction
646 * clauses have been applied (ie, output rows of a plan for it)
647 * consider_startup - true if there is any value in keeping plain paths for
648 * this rel on the basis of having cheap startup cost
649 * consider_param_startup - the same for parameterized paths
650 * reltarget - Default Path output tlist for this rel; normally contains
651 * Var and PlaceHolderVar nodes for the values we need to
652 * output from this relation.
653 * List is in no particular order, but all rels of an
654 * appendrel set must use corresponding orders.
655 * NOTE: in an appendrel child relation, may contain
656 * arbitrary expressions pulled up from a subquery!
657 * pathlist - List of Path nodes, one for each potentially useful
658 * method of generating the relation
659 * ppilist - ParamPathInfo nodes for parameterized Paths, if any
660 * cheapest_startup_path - the pathlist member with lowest startup cost
661 * (regardless of ordering) among the unparameterized paths;
662 * or NULL if there is no unparameterized path
663 * cheapest_total_path - the pathlist member with lowest total cost
664 * (regardless of ordering) among the unparameterized paths;
665 * or if there is no unparameterized path, the path with lowest
666 * total cost among the paths with minimum parameterization
667 * cheapest_unique_path - for caching cheapest path to produce unique
668 * (no duplicates) output from relation; NULL if not yet requested
669 * cheapest_parameterized_paths - best paths for their parameterizations;
670 * always includes cheapest_total_path, even if that's unparameterized
671 * direct_lateral_relids - rels this rel has direct LATERAL references to
672 * lateral_relids - required outer rels for LATERAL, as a Relids set
673 * (includes both direct and indirect lateral references)
675 * If the relation is a base relation it will have these fields set:
677 * relid - RTE index (this is redundant with the relids field, but
678 * is provided for convenience of access)
679 * rtekind - copy of RTE's rtekind field
680 * min_attr, max_attr - range of valid AttrNumbers for rel
681 * attr_needed - array of bitmapsets indicating the highest joinrel
682 * in which each attribute is needed; if bit 0 is set then
683 * the attribute is needed as part of final targetlist
684 * attr_widths - cache space for per-attribute width estimates;
685 * zero means not computed yet
686 * nulling_relids - relids of outer joins that can null this rel
687 * lateral_vars - lateral cross-references of rel, if any (list of
688 * Vars and PlaceHolderVars)
689 * lateral_referencers - relids of rels that reference this one laterally
690 * (includes both direct and indirect lateral references)
691 * indexlist - list of IndexOptInfo nodes for relation's indexes
692 * (always NIL if it's not a table or partitioned table)
693 * pages - number of disk pages in relation (zero if not a table)
694 * tuples - number of tuples in relation (not considering restrictions)
695 * allvisfrac - fraction of disk pages that are marked all-visible
696 * eclass_indexes - EquivalenceClasses that mention this rel (filled
697 * only after EC merging is complete)
698 * subroot - PlannerInfo for subquery (NULL if it's not a subquery)
699 * subplan_params - list of PlannerParamItems to be passed to subquery
701 * Note: for a subquery, tuples and subroot are not set immediately
702 * upon creation of the RelOptInfo object; they are filled in when
703 * set_subquery_pathlist processes the object.
705 * For otherrels that are appendrel members, these fields are filled
706 * in just as for a baserel, except we don't bother with lateral_vars.
708 * If the relation is either a foreign table or a join of foreign tables that
709 * all belong to the same foreign server and are assigned to the same user to
710 * check access permissions as (cf checkAsUser), these fields will be set:
712 * serverid - OID of foreign server, if foreign table (else InvalidOid)
713 * userid - OID of user to check access as (InvalidOid means current user)
714 * useridiscurrent - we've assumed that userid equals current user
715 * fdwroutine - function hooks for FDW, if foreign table (else NULL)
716 * fdw_private - private state for FDW, if foreign table (else NULL)
718 * Two fields are used to cache knowledge acquired during the join search
719 * about whether this rel is provably unique when being joined to given other
720 * relation(s), ie, it can have at most one row matching any given row from
721 * that join relation. Currently we only attempt such proofs, and thus only
722 * populate these fields, for base rels; but someday they might be used for
723 * join rels too:
725 * unique_for_rels - list of Relid sets, each one being a set of other
726 * rels for which this one has been proven unique
727 * non_unique_for_rels - list of Relid sets, each one being a set of
728 * other rels for which we have tried and failed to prove
729 * this one unique
731 * The presence of the following fields depends on the restrictions
732 * and joins that the relation participates in:
734 * baserestrictinfo - List of RestrictInfo nodes, containing info about
735 * each non-join qualification clause in which this relation
736 * participates (only used for base rels)
737 * baserestrictcost - Estimated cost of evaluating the baserestrictinfo
738 * clauses at a single tuple (only used for base rels)
739 * baserestrict_min_security - Smallest security_level found among
740 * clauses in baserestrictinfo
741 * joininfo - List of RestrictInfo nodes, containing info about each
742 * join clause in which this relation participates (but
743 * note this excludes clauses that might be derivable from
744 * EquivalenceClasses)
745 * has_eclass_joins - flag that EquivalenceClass joins are possible
747 * Note: Keeping a restrictinfo list in the RelOptInfo is useful only for
748 * base rels, because for a join rel the set of clauses that are treated as
749 * restrict clauses varies depending on which sub-relations we choose to join.
750 * (For example, in a 3-base-rel join, a clause relating rels 1 and 2 must be
751 * treated as a restrictclause if we join {1} and {2 3} to make {1 2 3}; but
752 * if we join {1 2} and {3} then that clause will be a restrictclause in {1 2}
753 * and should not be processed again at the level of {1 2 3}.) Therefore,
754 * the restrictinfo list in the join case appears in individual JoinPaths
755 * (field joinrestrictinfo), not in the parent relation. But it's OK for
756 * the RelOptInfo to store the joininfo list, because that is the same
757 * for a given rel no matter how we form it.
759 * We store baserestrictcost in the RelOptInfo (for base relations) because
760 * we know we will need it at least once (to price the sequential scan)
761 * and may need it multiple times to price index scans.
763 * A join relation is considered to be partitioned if it is formed from a
764 * join of two relations that are partitioned, have matching partitioning
765 * schemes, and are joined on an equijoin of the partitioning columns.
766 * Under those conditions we can consider the join relation to be partitioned
767 * by either relation's partitioning keys, though some care is needed if
768 * either relation can be forced to null by outer-joining. For example, an
769 * outer join like (A LEFT JOIN B ON A.a = B.b) may produce rows with B.b
770 * NULL. These rows may not fit the partitioning conditions imposed on B.
771 * Hence, strictly speaking, the join is not partitioned by B.b and thus
772 * partition keys of an outer join should include partition key expressions
773 * from the non-nullable side only. However, if a subsequent join uses
774 * strict comparison operators (and all commonly-used equijoin operators are
775 * strict), the presence of nulls doesn't cause a problem: such rows couldn't
776 * match anything on the other side and thus they don't create a need to do
777 * any cross-partition sub-joins. Hence we can treat such values as still
778 * partitioning the join output for the purpose of additional partitionwise
779 * joining, so long as a strict join operator is used by the next join.
781 * If the relation is partitioned, these fields will be set:
783 * part_scheme - Partitioning scheme of the relation
784 * nparts - Number of partitions
785 * boundinfo - Partition bounds
786 * partbounds_merged - true if partition bounds are merged ones
787 * partition_qual - Partition constraint if not the root
788 * part_rels - RelOptInfos for each partition
789 * all_partrels - Relids set of all partition relids
790 * partexprs, nullable_partexprs - Partition key expressions
792 * The partexprs and nullable_partexprs arrays each contain
793 * part_scheme->partnatts elements. Each of the elements is a list of
794 * partition key expressions. For partitioned base relations, there is one
795 * expression in each partexprs element, and nullable_partexprs is empty.
796 * For partitioned join relations, each base relation within the join
797 * contributes one partition key expression per partitioning column;
798 * that expression goes in the partexprs[i] list if the base relation
799 * is not nullable by this join or any lower outer join, or in the
800 * nullable_partexprs[i] list if the base relation is nullable.
801 * Furthermore, FULL JOINs add extra nullable_partexprs expressions
802 * corresponding to COALESCE expressions of the left and right join columns,
803 * to simplify matching join clauses to those lists.
805 * Not all fields are printed. (In some cases, there is no print support for
806 * the field type.)
807 *----------
810 /* Bitmask of flags supported by table AMs */
811 #define AMFLAG_HAS_TID_RANGE (1 << 0)
813 typedef enum RelOptKind
815 RELOPT_BASEREL,
816 RELOPT_JOINREL,
817 RELOPT_OTHER_MEMBER_REL,
818 RELOPT_OTHER_JOINREL,
819 RELOPT_UPPER_REL,
820 RELOPT_OTHER_UPPER_REL,
821 } RelOptKind;
824 * Is the given relation a simple relation i.e a base or "other" member
825 * relation?
827 #define IS_SIMPLE_REL(rel) \
828 ((rel)->reloptkind == RELOPT_BASEREL || \
829 (rel)->reloptkind == RELOPT_OTHER_MEMBER_REL)
831 /* Is the given relation a join relation? */
832 #define IS_JOIN_REL(rel) \
833 ((rel)->reloptkind == RELOPT_JOINREL || \
834 (rel)->reloptkind == RELOPT_OTHER_JOINREL)
836 /* Is the given relation an upper relation? */
837 #define IS_UPPER_REL(rel) \
838 ((rel)->reloptkind == RELOPT_UPPER_REL || \
839 (rel)->reloptkind == RELOPT_OTHER_UPPER_REL)
841 /* Is the given relation an "other" relation? */
842 #define IS_OTHER_REL(rel) \
843 ((rel)->reloptkind == RELOPT_OTHER_MEMBER_REL || \
844 (rel)->reloptkind == RELOPT_OTHER_JOINREL || \
845 (rel)->reloptkind == RELOPT_OTHER_UPPER_REL)
847 typedef struct RelOptInfo
849 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
851 NodeTag type;
853 RelOptKind reloptkind;
856 * all relations included in this RelOptInfo; set of base + OJ relids
857 * (rangetable indexes)
859 Relids relids;
862 * size estimates generated by planner
864 /* estimated number of result tuples */
865 Cardinality rows;
868 * per-relation planner control flags
870 /* keep cheap-startup-cost paths? */
871 bool consider_startup;
872 /* ditto, for parameterized paths? */
873 bool consider_param_startup;
874 /* consider parallel paths? */
875 bool consider_parallel;
878 * default result targetlist for Paths scanning this relation; list of
879 * Vars/Exprs, cost, width
881 struct PathTarget *reltarget;
884 * materialization information
886 List *pathlist; /* Path structures */
887 List *ppilist; /* ParamPathInfos used in pathlist */
888 List *partial_pathlist; /* partial Paths */
889 struct Path *cheapest_startup_path;
890 struct Path *cheapest_total_path;
891 struct Path *cheapest_unique_path;
892 List *cheapest_parameterized_paths;
895 * parameterization information needed for both base rels and join rels
896 * (see also lateral_vars and lateral_referencers)
898 /* rels directly laterally referenced */
899 Relids direct_lateral_relids;
900 /* minimum parameterization of rel */
901 Relids lateral_relids;
904 * information about a base rel (not set for join rels!)
906 Index relid;
907 /* containing tablespace */
908 Oid reltablespace;
909 /* RELATION, SUBQUERY, FUNCTION, etc */
910 RTEKind rtekind;
911 /* smallest attrno of rel (often <0) */
912 AttrNumber min_attr;
913 /* largest attrno of rel */
914 AttrNumber max_attr;
915 /* array indexed [min_attr .. max_attr] */
916 Relids *attr_needed pg_node_attr(read_write_ignore);
917 /* array indexed [min_attr .. max_attr] */
918 int32 *attr_widths pg_node_attr(read_write_ignore);
921 * Zero-based set containing attnums of NOT NULL columns. Not populated
922 * for rels corresponding to non-partitioned inh==true RTEs.
924 Bitmapset *notnullattnums;
925 /* relids of outer joins that can null this baserel */
926 Relids nulling_relids;
927 /* LATERAL Vars and PHVs referenced by rel */
928 List *lateral_vars;
929 /* rels that reference this baserel laterally */
930 Relids lateral_referencers;
931 /* list of IndexOptInfo */
932 List *indexlist;
933 /* list of StatisticExtInfo */
934 List *statlist;
935 /* size estimates derived from pg_class */
936 BlockNumber pages;
937 Cardinality tuples;
938 double allvisfrac;
939 /* indexes in PlannerInfo's eq_classes list of ECs that mention this rel */
940 Bitmapset *eclass_indexes;
941 PlannerInfo *subroot; /* if subquery */
942 List *subplan_params; /* if subquery */
943 /* wanted number of parallel workers */
944 int rel_parallel_workers;
945 /* Bitmask of optional features supported by the table AM */
946 uint32 amflags;
949 * Information about foreign tables and foreign joins
951 /* identifies server for the table or join */
952 Oid serverid;
953 /* identifies user to check access as; 0 means to check as current user */
954 Oid userid;
955 /* join is only valid for current user */
956 bool useridiscurrent;
957 /* use "struct FdwRoutine" to avoid including fdwapi.h here */
958 struct FdwRoutine *fdwroutine pg_node_attr(read_write_ignore);
959 void *fdw_private pg_node_attr(read_write_ignore);
962 * cache space for remembering if we have proven this relation unique
964 /* known unique for these other relid set(s) */
965 List *unique_for_rels;
966 /* known not unique for these set(s) */
967 List *non_unique_for_rels;
970 * used by various scans and joins:
972 /* RestrictInfo structures (if base rel) */
973 List *baserestrictinfo;
974 /* cost of evaluating the above */
975 QualCost baserestrictcost;
976 /* min security_level found in baserestrictinfo */
977 Index baserestrict_min_security;
978 /* RestrictInfo structures for join clauses involving this rel */
979 List *joininfo;
980 /* T means joininfo is incomplete */
981 bool has_eclass_joins;
984 * used by partitionwise joins:
986 /* consider partitionwise join paths? (if partitioned rel) */
987 bool consider_partitionwise_join;
990 * inheritance links, if this is an otherrel (otherwise NULL):
992 /* Immediate parent relation (dumping it would be too verbose) */
993 struct RelOptInfo *parent pg_node_attr(read_write_ignore);
994 /* Topmost parent relation (dumping it would be too verbose) */
995 struct RelOptInfo *top_parent pg_node_attr(read_write_ignore);
996 /* Relids of topmost parent (redundant, but handy) */
997 Relids top_parent_relids;
1000 * used for partitioned relations:
1002 /* Partitioning scheme */
1003 PartitionScheme part_scheme pg_node_attr(read_write_ignore);
1006 * Number of partitions; -1 if not yet set; in case of a join relation 0
1007 * means it's considered unpartitioned
1009 int nparts;
1010 /* Partition bounds */
1011 struct PartitionBoundInfoData *boundinfo pg_node_attr(read_write_ignore);
1012 /* True if partition bounds were created by partition_bounds_merge() */
1013 bool partbounds_merged;
1014 /* Partition constraint, if not the root */
1015 List *partition_qual;
1018 * Array of RelOptInfos of partitions, stored in the same order as bounds
1019 * (don't print, too bulky and duplicative)
1021 struct RelOptInfo **part_rels pg_node_attr(read_write_ignore);
1024 * Bitmap with members acting as indexes into the part_rels[] array to
1025 * indicate which partitions survived partition pruning.
1027 Bitmapset *live_parts;
1028 /* Relids set of all partition relids */
1029 Relids all_partrels;
1032 * These arrays are of length partkey->partnatts, which we don't have at
1033 * hand, so don't try to print
1036 /* Non-nullable partition key expressions */
1037 List **partexprs pg_node_attr(read_write_ignore);
1038 /* Nullable partition key expressions */
1039 List **nullable_partexprs pg_node_attr(read_write_ignore);
1040 } RelOptInfo;
1043 * Is given relation partitioned?
1045 * It's not enough to test whether rel->part_scheme is set, because it might
1046 * be that the basic partitioning properties of the input relations matched
1047 * but the partition bounds did not. Also, if we are able to prove a rel
1048 * dummy (empty), we should henceforth treat it as unpartitioned.
1050 #define IS_PARTITIONED_REL(rel) \
1051 ((rel)->part_scheme && (rel)->boundinfo && (rel)->nparts > 0 && \
1052 (rel)->part_rels && !IS_DUMMY_REL(rel))
1055 * Convenience macro to make sure that a partitioned relation has all the
1056 * required members set.
1058 #define REL_HAS_ALL_PART_PROPS(rel) \
1059 ((rel)->part_scheme && (rel)->boundinfo && (rel)->nparts > 0 && \
1060 (rel)->part_rels && (rel)->partexprs && (rel)->nullable_partexprs)
1063 * IndexOptInfo
1064 * Per-index information for planning/optimization
1066 * indexkeys[], indexcollations[] each have ncolumns entries.
1067 * opfamily[], and opcintype[] each have nkeycolumns entries. They do
1068 * not contain any information about included attributes.
1070 * sortopfamily[], reverse_sort[], and nulls_first[] have
1071 * nkeycolumns entries, if the index is ordered; but if it is unordered,
1072 * those pointers are NULL.
1074 * Zeroes in the indexkeys[] array indicate index columns that are
1075 * expressions; there is one element in indexprs for each such column.
1077 * For an ordered index, reverse_sort[] and nulls_first[] describe the
1078 * sort ordering of a forward indexscan; we can also consider a backward
1079 * indexscan, which will generate the reverse ordering.
1081 * The indexprs and indpred expressions have been run through
1082 * prepqual.c and eval_const_expressions() for ease of matching to
1083 * WHERE clauses. indpred is in implicit-AND form.
1085 * indextlist is a TargetEntry list representing the index columns.
1086 * It provides an equivalent base-relation Var for each simple column,
1087 * and links to the matching indexprs element for each expression column.
1089 * While most of these fields are filled when the IndexOptInfo is created
1090 * (by plancat.c), indrestrictinfo and predOK are set later, in
1091 * check_index_predicates().
1093 #ifndef HAVE_INDEXOPTINFO_TYPEDEF
1094 typedef struct IndexOptInfo IndexOptInfo;
1095 #define HAVE_INDEXOPTINFO_TYPEDEF 1
1096 #endif
1098 struct IndexOptInfo
1100 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
1102 NodeTag type;
1104 /* OID of the index relation */
1105 Oid indexoid;
1106 /* tablespace of index (not table) */
1107 Oid reltablespace;
1108 /* back-link to index's table; don't print, else infinite recursion */
1109 RelOptInfo *rel pg_node_attr(read_write_ignore);
1112 * index-size statistics (from pg_class and elsewhere)
1114 /* number of disk pages in index */
1115 BlockNumber pages;
1116 /* number of index tuples in index */
1117 Cardinality tuples;
1118 /* index tree height, or -1 if unknown */
1119 int tree_height;
1122 * index descriptor information
1124 /* number of columns in index */
1125 int ncolumns;
1126 /* number of key columns in index */
1127 int nkeycolumns;
1130 * table column numbers of index's columns (both key and included
1131 * columns), or 0 for expression columns
1133 int *indexkeys pg_node_attr(array_size(ncolumns));
1134 /* OIDs of collations of index columns */
1135 Oid *indexcollations pg_node_attr(array_size(nkeycolumns));
1136 /* OIDs of operator families for columns */
1137 Oid *opfamily pg_node_attr(array_size(nkeycolumns));
1138 /* OIDs of opclass declared input data types */
1139 Oid *opcintype pg_node_attr(array_size(nkeycolumns));
1140 /* OIDs of btree opfamilies, if orderable. NULL if partitioned index */
1141 Oid *sortopfamily pg_node_attr(array_size(nkeycolumns));
1142 /* is sort order descending? or NULL if partitioned index */
1143 bool *reverse_sort pg_node_attr(array_size(nkeycolumns));
1144 /* do NULLs come first in the sort order? or NULL if partitioned index */
1145 bool *nulls_first pg_node_attr(array_size(nkeycolumns));
1146 /* opclass-specific options for columns */
1147 bytea **opclassoptions pg_node_attr(read_write_ignore);
1148 /* which index cols can be returned in an index-only scan? */
1149 bool *canreturn pg_node_attr(array_size(ncolumns));
1150 /* OID of the access method (in pg_am) */
1151 Oid relam;
1154 * expressions for non-simple index columns; redundant to print since we
1155 * print indextlist
1157 List *indexprs pg_node_attr(read_write_ignore);
1158 /* predicate if a partial index, else NIL */
1159 List *indpred;
1161 /* targetlist representing index columns */
1162 List *indextlist;
1165 * parent relation's baserestrictinfo list, less any conditions implied by
1166 * the index's predicate (unless it's a target rel, see comments in
1167 * check_index_predicates())
1169 List *indrestrictinfo;
1171 /* true if index predicate matches query */
1172 bool predOK;
1173 /* true if a unique index */
1174 bool unique;
1175 /* is uniqueness enforced immediately? */
1176 bool immediate;
1177 /* true if index doesn't really exist */
1178 bool hypothetical;
1181 * Remaining fields are copied from the index AM's API struct
1182 * (IndexAmRoutine). These fields are not set for partitioned indexes.
1184 bool amcanorderbyop;
1185 bool amoptionalkey;
1186 bool amsearcharray;
1187 bool amsearchnulls;
1188 /* does AM have amgettuple interface? */
1189 bool amhasgettuple;
1190 /* does AM have amgetbitmap interface? */
1191 bool amhasgetbitmap;
1192 bool amcanparallel;
1193 /* does AM have ammarkpos interface? */
1194 bool amcanmarkpos;
1195 /* AM's cost estimator */
1196 /* Rather than include amapi.h here, we declare amcostestimate like this */
1197 void (*amcostestimate) () pg_node_attr(read_write_ignore);
1201 * ForeignKeyOptInfo
1202 * Per-foreign-key information for planning/optimization
1204 * The per-FK-column arrays can be fixed-size because we allow at most
1205 * INDEX_MAX_KEYS columns in a foreign key constraint. Each array has
1206 * nkeys valid entries.
1208 typedef struct ForeignKeyOptInfo
1210 pg_node_attr(custom_read_write, no_copy_equal, no_read, no_query_jumble)
1212 NodeTag type;
1215 * Basic data about the foreign key (fetched from catalogs):
1218 /* RT index of the referencing table */
1219 Index con_relid;
1220 /* RT index of the referenced table */
1221 Index ref_relid;
1222 /* number of columns in the foreign key */
1223 int nkeys;
1224 /* cols in referencing table */
1225 AttrNumber conkey[INDEX_MAX_KEYS] pg_node_attr(array_size(nkeys));
1226 /* cols in referenced table */
1227 AttrNumber confkey[INDEX_MAX_KEYS] pg_node_attr(array_size(nkeys));
1228 /* PK = FK operator OIDs */
1229 Oid conpfeqop[INDEX_MAX_KEYS] pg_node_attr(array_size(nkeys));
1232 * Derived info about whether FK's equality conditions match the query:
1235 /* # of FK cols matched by ECs */
1236 int nmatched_ec;
1237 /* # of these ECs that are ec_has_const */
1238 int nconst_ec;
1239 /* # of FK cols matched by non-EC rinfos */
1240 int nmatched_rcols;
1241 /* total # of non-EC rinfos matched to FK */
1242 int nmatched_ri;
1243 /* Pointer to eclass matching each column's condition, if there is one */
1244 struct EquivalenceClass *eclass[INDEX_MAX_KEYS];
1245 /* Pointer to eclass member for the referencing Var, if there is one */
1246 struct EquivalenceMember *fk_eclass_member[INDEX_MAX_KEYS];
1247 /* List of non-EC RestrictInfos matching each column's condition */
1248 List *rinfos[INDEX_MAX_KEYS];
1249 } ForeignKeyOptInfo;
1252 * StatisticExtInfo
1253 * Information about extended statistics for planning/optimization
1255 * Each pg_statistic_ext row is represented by one or more nodes of this
1256 * type, or even zero if ANALYZE has not computed them.
1258 typedef struct StatisticExtInfo
1260 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
1262 NodeTag type;
1264 /* OID of the statistics row */
1265 Oid statOid;
1267 /* includes child relations */
1268 bool inherit;
1270 /* back-link to statistic's table; don't print, else infinite recursion */
1271 RelOptInfo *rel pg_node_attr(read_write_ignore);
1273 /* statistics kind of this entry */
1274 char kind;
1276 /* attnums of the columns covered */
1277 Bitmapset *keys;
1279 /* expressions */
1280 List *exprs;
1281 } StatisticExtInfo;
1284 * JoinDomains
1286 * A "join domain" defines the scope of applicability of deductions made via
1287 * the EquivalenceClass mechanism. Roughly speaking, a join domain is a set
1288 * of base+OJ relations that are inner-joined together. More precisely, it is
1289 * the set of relations at which equalities deduced from an EquivalenceClass
1290 * can be enforced or should be expected to hold. The topmost JoinDomain
1291 * covers the whole query (so its jd_relids should equal all_query_rels).
1292 * An outer join creates a new JoinDomain that includes all base+OJ relids
1293 * within its nullable side, but (by convention) not the OJ's own relid.
1294 * A FULL join creates two new JoinDomains, one for each side.
1296 * Notice that a rel that is below outer join(s) will thus appear to belong
1297 * to multiple join domains. However, any of its Vars that appear in
1298 * EquivalenceClasses belonging to higher join domains will have nullingrel
1299 * bits preventing them from being evaluated at the rel's scan level, so that
1300 * we will not be able to derive enforceable-at-the-rel-scan-level clauses
1301 * from such ECs. We define the join domain relid sets this way so that
1302 * domains can be said to be "higher" or "lower" when one domain relid set
1303 * includes another.
1305 * The JoinDomains for a query are computed in deconstruct_jointree.
1306 * We do not copy JoinDomain structs once made, so they can be compared
1307 * for equality by simple pointer equality.
1309 typedef struct JoinDomain
1311 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
1313 NodeTag type;
1315 Relids jd_relids; /* all relids contained within the domain */
1316 } JoinDomain;
1319 * EquivalenceClasses
1321 * Whenever we identify a mergejoinable equality clause A = B that is
1322 * not an outer-join clause, we create an EquivalenceClass containing
1323 * the expressions A and B to record this knowledge. If we later find another
1324 * equivalence B = C, we add C to the existing EquivalenceClass; this may
1325 * require merging two existing EquivalenceClasses. At the end of the qual
1326 * distribution process, we have sets of values that are known all transitively
1327 * equal to each other, where "equal" is according to the rules of the btree
1328 * operator family(s) shown in ec_opfamilies, as well as the collation shown
1329 * by ec_collation. (We restrict an EC to contain only equalities whose
1330 * operators belong to the same set of opfamilies. This could probably be
1331 * relaxed, but for now it's not worth the trouble, since nearly all equality
1332 * operators belong to only one btree opclass anyway. Similarly, we suppose
1333 * that all or none of the input datatypes are collatable, so that a single
1334 * collation value is sufficient.)
1336 * Strictly speaking, deductions from an EquivalenceClass hold only within
1337 * a "join domain", that is a set of relations that are innerjoined together
1338 * (see JoinDomain above). For the most part we don't need to account for
1339 * this explicitly, because equality clauses from different join domains
1340 * will contain Vars that are not equal() because they have different
1341 * nullingrel sets, and thus we will never falsely merge ECs from different
1342 * join domains. But Var-free (pseudoconstant) expressions lack that safety
1343 * feature. We handle that by marking "const" EC members with the JoinDomain
1344 * of the clause they came from; two nominally-equal const members will be
1345 * considered different if they came from different JoinDomains. This ensures
1346 * no false EquivalenceClass merges will occur.
1348 * We also use EquivalenceClasses as the base structure for PathKeys, letting
1349 * us represent knowledge about different sort orderings being equivalent.
1350 * Since every PathKey must reference an EquivalenceClass, we will end up
1351 * with single-member EquivalenceClasses whenever a sort key expression has
1352 * not been equivalenced to anything else. It is also possible that such an
1353 * EquivalenceClass will contain a volatile expression ("ORDER BY random()"),
1354 * which is a case that can't arise otherwise since clauses containing
1355 * volatile functions are never considered mergejoinable. We mark such
1356 * EquivalenceClasses specially to prevent them from being merged with
1357 * ordinary EquivalenceClasses. Also, for volatile expressions we have
1358 * to be careful to match the EquivalenceClass to the correct targetlist
1359 * entry: consider SELECT random() AS a, random() AS b ... ORDER BY b,a.
1360 * So we record the SortGroupRef of the originating sort clause.
1362 * NB: if ec_merged isn't NULL, this class has been merged into another, and
1363 * should be ignored in favor of using the pointed-to class.
1365 * NB: EquivalenceClasses are never copied after creation. Therefore,
1366 * copyObject() copies pointers to them as pointers, and equal() compares
1367 * pointers to EquivalenceClasses via pointer equality. This is implemented
1368 * by putting copy_as_scalar and equal_as_scalar attributes on fields that
1369 * are pointers to EquivalenceClasses. The same goes for EquivalenceMembers.
1371 typedef struct EquivalenceClass
1373 pg_node_attr(custom_read_write, no_copy_equal, no_read, no_query_jumble)
1375 NodeTag type;
1377 List *ec_opfamilies; /* btree operator family OIDs */
1378 Oid ec_collation; /* collation, if datatypes are collatable */
1379 List *ec_members; /* list of EquivalenceMembers */
1380 List *ec_sources; /* list of generating RestrictInfos */
1381 List *ec_derives; /* list of derived RestrictInfos */
1382 Relids ec_relids; /* all relids appearing in ec_members, except
1383 * for child members (see below) */
1384 bool ec_has_const; /* any pseudoconstants in ec_members? */
1385 bool ec_has_volatile; /* the (sole) member is a volatile expr */
1386 bool ec_broken; /* failed to generate needed clauses? */
1387 Index ec_sortref; /* originating sortclause label, or 0 */
1388 Index ec_min_security; /* minimum security_level in ec_sources */
1389 Index ec_max_security; /* maximum security_level in ec_sources */
1390 struct EquivalenceClass *ec_merged; /* set if merged into another EC */
1391 } EquivalenceClass;
1394 * If an EC contains a constant, any PathKey depending on it must be
1395 * redundant, since there's only one possible value of the key.
1397 #define EC_MUST_BE_REDUNDANT(eclass) \
1398 ((eclass)->ec_has_const)
1401 * EquivalenceMember - one member expression of an EquivalenceClass
1403 * em_is_child signifies that this element was built by transposing a member
1404 * for an appendrel parent relation to represent the corresponding expression
1405 * for an appendrel child. These members are used for determining the
1406 * pathkeys of scans on the child relation and for explicitly sorting the
1407 * child when necessary to build a MergeAppend path for the whole appendrel
1408 * tree. An em_is_child member has no impact on the properties of the EC as a
1409 * whole; in particular the EC's ec_relids field does NOT include the child
1410 * relation. An em_is_child member should never be marked em_is_const nor
1411 * cause ec_has_const or ec_has_volatile to be set, either. Thus, em_is_child
1412 * members are not really full-fledged members of the EC, but just reflections
1413 * or doppelgangers of real members. Most operations on EquivalenceClasses
1414 * should ignore em_is_child members, and those that don't should test
1415 * em_relids to make sure they only consider relevant members.
1417 * em_datatype is usually the same as exprType(em_expr), but can be
1418 * different when dealing with a binary-compatible opfamily; in particular
1419 * anyarray_ops would never work without this. Use em_datatype when
1420 * looking up a specific btree operator to work with this expression.
1422 typedef struct EquivalenceMember
1424 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
1426 NodeTag type;
1428 Expr *em_expr; /* the expression represented */
1429 Relids em_relids; /* all relids appearing in em_expr */
1430 bool em_is_const; /* expression is pseudoconstant? */
1431 bool em_is_child; /* derived version for a child relation? */
1432 Oid em_datatype; /* the "nominal type" used by the opfamily */
1433 JoinDomain *em_jdomain; /* join domain containing the source clause */
1434 /* if em_is_child is true, this links to corresponding EM for top parent */
1435 struct EquivalenceMember *em_parent pg_node_attr(read_write_ignore);
1436 } EquivalenceMember;
1439 * PathKeys
1441 * The sort ordering of a path is represented by a list of PathKey nodes.
1442 * An empty list implies no known ordering. Otherwise the first item
1443 * represents the primary sort key, the second the first secondary sort key,
1444 * etc. The value being sorted is represented by linking to an
1445 * EquivalenceClass containing that value and including pk_opfamily among its
1446 * ec_opfamilies. The EquivalenceClass tells which collation to use, too.
1447 * This is a convenient method because it makes it trivial to detect
1448 * equivalent and closely-related orderings. (See optimizer/README for more
1449 * information.)
1451 * Note: pk_strategy is either BTLessStrategyNumber (for ASC) or
1452 * BTGreaterStrategyNumber (for DESC). We assume that all ordering-capable
1453 * index types will use btree-compatible strategy numbers.
1455 typedef struct PathKey
1457 pg_node_attr(no_read, no_query_jumble)
1459 NodeTag type;
1461 /* the value that is ordered */
1462 EquivalenceClass *pk_eclass pg_node_attr(copy_as_scalar, equal_as_scalar);
1463 Oid pk_opfamily; /* btree opfamily defining the ordering */
1464 int pk_strategy; /* sort direction (ASC or DESC) */
1465 bool pk_nulls_first; /* do NULLs come before normal values? */
1466 } PathKey;
1469 * Combines the information about pathkeys and the associated clauses.
1471 typedef struct PathKeyInfo
1473 NodeTag type;
1474 List *pathkeys;
1475 List *clauses;
1476 } PathKeyInfo;
1479 * VolatileFunctionStatus -- allows nodes to cache their
1480 * contain_volatile_functions properties. VOLATILITY_UNKNOWN means not yet
1481 * determined.
1483 typedef enum VolatileFunctionStatus
1485 VOLATILITY_UNKNOWN = 0,
1486 VOLATILITY_VOLATILE,
1487 VOLATILITY_NOVOLATILE,
1488 } VolatileFunctionStatus;
1491 * PathTarget
1493 * This struct contains what we need to know during planning about the
1494 * targetlist (output columns) that a Path will compute. Each RelOptInfo
1495 * includes a default PathTarget, which its individual Paths may simply
1496 * reference. However, in some cases a Path may compute outputs different
1497 * from other Paths, and in that case we make a custom PathTarget for it.
1498 * For example, an indexscan might return index expressions that would
1499 * otherwise need to be explicitly calculated. (Note also that "upper"
1500 * relations generally don't have useful default PathTargets.)
1502 * exprs contains bare expressions; they do not have TargetEntry nodes on top,
1503 * though those will appear in finished Plans.
1505 * sortgrouprefs[] is an array of the same length as exprs, containing the
1506 * corresponding sort/group refnos, or zeroes for expressions not referenced
1507 * by sort/group clauses. If sortgrouprefs is NULL (which it generally is in
1508 * RelOptInfo.reltarget targets; only upper-level Paths contain this info),
1509 * we have not identified sort/group columns in this tlist. This allows us to
1510 * deal with sort/group refnos when needed with less expense than including
1511 * TargetEntry nodes in the exprs list.
1513 typedef struct PathTarget
1515 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
1517 NodeTag type;
1519 /* list of expressions to be computed */
1520 List *exprs;
1522 /* corresponding sort/group refnos, or 0 */
1523 Index *sortgrouprefs pg_node_attr(array_size(exprs));
1525 /* cost of evaluating the expressions */
1526 QualCost cost;
1528 /* estimated avg width of result tuples */
1529 int width;
1531 /* indicates if exprs contain any volatile functions */
1532 VolatileFunctionStatus has_volatile_expr;
1533 } PathTarget;
1535 /* Convenience macro to get a sort/group refno from a PathTarget */
1536 #define get_pathtarget_sortgroupref(target, colno) \
1537 ((target)->sortgrouprefs ? (target)->sortgrouprefs[colno] : (Index) 0)
1541 * ParamPathInfo
1543 * All parameterized paths for a given relation with given required outer rels
1544 * link to a single ParamPathInfo, which stores common information such as
1545 * the estimated rowcount for this parameterization. We do this partly to
1546 * avoid recalculations, but mostly to ensure that the estimated rowcount
1547 * is in fact the same for every such path.
1549 * Note: ppi_clauses is only used in ParamPathInfos for base relation paths;
1550 * in join cases it's NIL because the set of relevant clauses varies depending
1551 * on how the join is formed. The relevant clauses will appear in each
1552 * parameterized join path's joinrestrictinfo list, instead. ParamPathInfos
1553 * for append relations don't bother with this, either.
1555 * ppi_serials is the set of rinfo_serial numbers for quals that are enforced
1556 * by this path. As with ppi_clauses, it's only maintained for baserels.
1557 * (We could construct it on-the-fly from ppi_clauses, but it seems better
1558 * to materialize a copy.)
1560 typedef struct ParamPathInfo
1562 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
1564 NodeTag type;
1566 Relids ppi_req_outer; /* rels supplying parameters used by path */
1567 Cardinality ppi_rows; /* estimated number of result tuples */
1568 List *ppi_clauses; /* join clauses available from outer rels */
1569 Bitmapset *ppi_serials; /* set of rinfo_serial for enforced quals */
1570 } ParamPathInfo;
1574 * Type "Path" is used as-is for sequential-scan paths, as well as some other
1575 * simple plan types that we don't need any extra information in the path for.
1576 * For other path types it is the first component of a larger struct.
1578 * "pathtype" is the NodeTag of the Plan node we could build from this Path.
1579 * It is partially redundant with the Path's NodeTag, but allows us to use
1580 * the same Path type for multiple Plan types when there is no need to
1581 * distinguish the Plan type during path processing.
1583 * "parent" identifies the relation this Path scans, and "pathtarget"
1584 * describes the precise set of output columns the Path would compute.
1585 * In simple cases all Paths for a given rel share the same targetlist,
1586 * which we represent by having path->pathtarget equal to parent->reltarget.
1588 * "param_info", if not NULL, links to a ParamPathInfo that identifies outer
1589 * relation(s) that provide parameter values to each scan of this path.
1590 * That means this path can only be joined to those rels by means of nestloop
1591 * joins with this path on the inside. Also note that a parameterized path
1592 * is responsible for testing all "movable" joinclauses involving this rel
1593 * and the specified outer rel(s).
1595 * "rows" is the same as parent->rows in simple paths, but in parameterized
1596 * paths and UniquePaths it can be less than parent->rows, reflecting the
1597 * fact that we've filtered by extra join conditions or removed duplicates.
1599 * "pathkeys" is a List of PathKey nodes (see above), describing the sort
1600 * ordering of the path's output rows.
1602 * We do not support copying Path trees, mainly because the circular linkages
1603 * between RelOptInfo and Path nodes can't be handled easily in a simple
1604 * depth-first traversal. We also don't have read support at the moment.
1606 typedef struct Path
1608 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
1610 NodeTag type;
1612 /* tag identifying scan/join method */
1613 NodeTag pathtype;
1616 * the relation this path can build
1618 * We do NOT print the parent, else we'd be in infinite recursion. We can
1619 * print the parent's relids for identification purposes, though.
1621 RelOptInfo *parent pg_node_attr(write_only_relids);
1624 * list of Vars/Exprs, cost, width
1626 * We print the pathtarget only if it's not the default one for the rel.
1628 PathTarget *pathtarget pg_node_attr(write_only_nondefault_pathtarget);
1631 * parameterization info, or NULL if none
1633 * We do not print the whole of param_info, since it's printed via
1634 * RelOptInfo; it's sufficient and less cluttering to print just the
1635 * required outer relids.
1637 ParamPathInfo *param_info pg_node_attr(write_only_req_outer);
1639 /* engage parallel-aware logic? */
1640 bool parallel_aware;
1641 /* OK to use as part of parallel plan? */
1642 bool parallel_safe;
1643 /* desired # of workers; 0 = not parallel */
1644 int parallel_workers;
1646 /* estimated size/costs for path (see costsize.c for more info) */
1647 Cardinality rows; /* estimated number of result tuples */
1648 Cost startup_cost; /* cost expended before fetching any tuples */
1649 Cost total_cost; /* total cost (assuming all tuples fetched) */
1651 /* sort ordering of path's output; a List of PathKey nodes; see above */
1652 List *pathkeys;
1653 } Path;
1655 /* Macro for extracting a path's parameterization relids; beware double eval */
1656 #define PATH_REQ_OUTER(path) \
1657 ((path)->param_info ? (path)->param_info->ppi_req_outer : (Relids) NULL)
1659 /*----------
1660 * IndexPath represents an index scan over a single index.
1662 * This struct is used for both regular indexscans and index-only scans;
1663 * path.pathtype is T_IndexScan or T_IndexOnlyScan to show which is meant.
1665 * 'indexinfo' is the index to be scanned.
1667 * 'indexclauses' is a list of IndexClause nodes, each representing one
1668 * index-checkable restriction, with implicit AND semantics across the list.
1669 * An empty list implies a full index scan.
1671 * 'indexorderbys', if not NIL, is a list of ORDER BY expressions that have
1672 * been found to be usable as ordering operators for an amcanorderbyop index.
1673 * The list must match the path's pathkeys, ie, one expression per pathkey
1674 * in the same order. These are not RestrictInfos, just bare expressions,
1675 * since they generally won't yield booleans. It's guaranteed that each
1676 * expression has the index key on the left side of the operator.
1678 * 'indexorderbycols' is an integer list of index column numbers (zero-based)
1679 * of the same length as 'indexorderbys', showing which index column each
1680 * ORDER BY expression is meant to be used with. (There is no restriction
1681 * on which index column each ORDER BY can be used with.)
1683 * 'indexscandir' is one of:
1684 * ForwardScanDirection: forward scan of an index
1685 * BackwardScanDirection: backward scan of an ordered index
1686 * Unordered indexes will always have an indexscandir of ForwardScanDirection.
1688 * 'indextotalcost' and 'indexselectivity' are saved in the IndexPath so that
1689 * we need not recompute them when considering using the same index in a
1690 * bitmap index/heap scan (see BitmapHeapPath). The costs of the IndexPath
1691 * itself represent the costs of an IndexScan or IndexOnlyScan plan type.
1692 *----------
1694 typedef struct IndexPath
1696 Path path;
1697 IndexOptInfo *indexinfo;
1698 List *indexclauses;
1699 List *indexorderbys;
1700 List *indexorderbycols;
1701 ScanDirection indexscandir;
1702 Cost indextotalcost;
1703 Selectivity indexselectivity;
1704 } IndexPath;
1707 * Each IndexClause references a RestrictInfo node from the query's WHERE
1708 * or JOIN conditions, and shows how that restriction can be applied to
1709 * the particular index. We support both indexclauses that are directly
1710 * usable by the index machinery, which are typically of the form
1711 * "indexcol OP pseudoconstant", and those from which an indexable qual
1712 * can be derived. The simplest such transformation is that a clause
1713 * of the form "pseudoconstant OP indexcol" can be commuted to produce an
1714 * indexable qual (the index machinery expects the indexcol to be on the
1715 * left always). Another example is that we might be able to extract an
1716 * indexable range condition from a LIKE condition, as in "x LIKE 'foo%bar'"
1717 * giving rise to "x >= 'foo' AND x < 'fop'". Derivation of such lossy
1718 * conditions is done by a planner support function attached to the
1719 * indexclause's top-level function or operator.
1721 * indexquals is a list of RestrictInfos for the directly-usable index
1722 * conditions associated with this IndexClause. In the simplest case
1723 * it's a one-element list whose member is iclause->rinfo. Otherwise,
1724 * it contains one or more directly-usable indexqual conditions extracted
1725 * from the given clause. The 'lossy' flag indicates whether the
1726 * indexquals are semantically equivalent to the original clause, or
1727 * represent a weaker condition.
1729 * Normally, indexcol is the index of the single index column the clause
1730 * works on, and indexcols is NIL. But if the clause is a RowCompareExpr,
1731 * indexcol is the index of the leading column, and indexcols is a list of
1732 * all the affected columns. (Note that indexcols matches up with the
1733 * columns of the actual indexable RowCompareExpr in indexquals, which
1734 * might be different from the original in rinfo.)
1736 * An IndexPath's IndexClause list is required to be ordered by index
1737 * column, i.e. the indexcol values must form a nondecreasing sequence.
1738 * (The order of multiple clauses for the same index column is unspecified.)
1740 typedef struct IndexClause
1742 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
1744 NodeTag type;
1745 struct RestrictInfo *rinfo; /* original restriction or join clause */
1746 List *indexquals; /* indexqual(s) derived from it */
1747 bool lossy; /* are indexquals a lossy version of clause? */
1748 AttrNumber indexcol; /* index column the clause uses (zero-based) */
1749 List *indexcols; /* multiple index columns, if RowCompare */
1750 } IndexClause;
1753 * BitmapHeapPath represents one or more indexscans that generate TID bitmaps
1754 * instead of directly accessing the heap, followed by AND/OR combinations
1755 * to produce a single bitmap, followed by a heap scan that uses the bitmap.
1756 * Note that the output is always considered unordered, since it will come
1757 * out in physical heap order no matter what the underlying indexes did.
1759 * The individual indexscans are represented by IndexPath nodes, and any
1760 * logic on top of them is represented by a tree of BitmapAndPath and
1761 * BitmapOrPath nodes. Notice that we can use the same IndexPath node both
1762 * to represent a regular (or index-only) index scan plan, and as the child
1763 * of a BitmapHeapPath that represents scanning the same index using a
1764 * BitmapIndexScan. The startup_cost and total_cost figures of an IndexPath
1765 * always represent the costs to use it as a regular (or index-only)
1766 * IndexScan. The costs of a BitmapIndexScan can be computed using the
1767 * IndexPath's indextotalcost and indexselectivity.
1769 typedef struct BitmapHeapPath
1771 Path path;
1772 Path *bitmapqual; /* IndexPath, BitmapAndPath, BitmapOrPath */
1773 } BitmapHeapPath;
1776 * BitmapAndPath represents a BitmapAnd plan node; it can only appear as
1777 * part of the substructure of a BitmapHeapPath. The Path structure is
1778 * a bit more heavyweight than we really need for this, but for simplicity
1779 * we make it a derivative of Path anyway.
1781 typedef struct BitmapAndPath
1783 Path path;
1784 List *bitmapquals; /* IndexPaths and BitmapOrPaths */
1785 Selectivity bitmapselectivity;
1786 } BitmapAndPath;
1789 * BitmapOrPath represents a BitmapOr plan node; it can only appear as
1790 * part of the substructure of a BitmapHeapPath. The Path structure is
1791 * a bit more heavyweight than we really need for this, but for simplicity
1792 * we make it a derivative of Path anyway.
1794 typedef struct BitmapOrPath
1796 Path path;
1797 List *bitmapquals; /* IndexPaths and BitmapAndPaths */
1798 Selectivity bitmapselectivity;
1799 } BitmapOrPath;
1802 * TidPath represents a scan by TID
1804 * tidquals is an implicitly OR'ed list of qual expressions of the form
1805 * "CTID = pseudoconstant", or "CTID = ANY(pseudoconstant_array)",
1806 * or a CurrentOfExpr for the relation.
1808 typedef struct TidPath
1810 Path path;
1811 List *tidquals; /* qual(s) involving CTID = something */
1812 } TidPath;
1815 * TidRangePath represents a scan by a contiguous range of TIDs
1817 * tidrangequals is an implicitly AND'ed list of qual expressions of the form
1818 * "CTID relop pseudoconstant", where relop is one of >,>=,<,<=.
1820 typedef struct TidRangePath
1822 Path path;
1823 List *tidrangequals;
1824 } TidRangePath;
1827 * SubqueryScanPath represents a scan of an unflattened subquery-in-FROM
1829 * Note that the subpath comes from a different planning domain; for example
1830 * RTE indexes within it mean something different from those known to the
1831 * SubqueryScanPath. path.parent->subroot is the planning context needed to
1832 * interpret the subpath.
1834 typedef struct SubqueryScanPath
1836 Path path;
1837 Path *subpath; /* path representing subquery execution */
1838 } SubqueryScanPath;
1841 * ForeignPath represents a potential scan of a foreign table, foreign join
1842 * or foreign upper-relation.
1844 * In the case of a foreign join, fdw_restrictinfo stores the RestrictInfos to
1845 * apply to the join, which are used by createplan.c to get pseudoconstant
1846 * clauses evaluated as one-time quals in a gating Result plan node.
1848 * fdw_private stores FDW private data about the scan. While fdw_private is
1849 * not actually touched by the core code during normal operations, it's
1850 * generally a good idea to use a representation that can be dumped by
1851 * nodeToString(), so that you can examine the structure during debugging
1852 * with tools like pprint().
1854 typedef struct ForeignPath
1856 Path path;
1857 Path *fdw_outerpath;
1858 List *fdw_restrictinfo;
1859 List *fdw_private;
1860 } ForeignPath;
1863 * CustomPath represents a table scan or a table join done by some out-of-core
1864 * extension.
1866 * We provide a set of hooks here - which the provider must take care to set
1867 * up correctly - to allow extensions to supply their own methods of scanning
1868 * a relation or join relations. For example, a provider might provide GPU
1869 * acceleration, a cache-based scan, or some other kind of logic we haven't
1870 * dreamed up yet.
1872 * CustomPaths can be injected into the planning process for a base or join
1873 * relation by set_rel_pathlist_hook or set_join_pathlist_hook functions,
1874 * respectively.
1876 * In the case of a table join, custom_restrictinfo stores the RestrictInfos
1877 * to apply to the join, which are used by createplan.c to get pseudoconstant
1878 * clauses evaluated as one-time quals in a gating Result plan node.
1880 * Core code must avoid assuming that the CustomPath is only as large as
1881 * the structure declared here; providers are allowed to make it the first
1882 * element in a larger structure. (Since the planner never copies Paths,
1883 * this doesn't add any complication.) However, for consistency with the
1884 * FDW case, we provide a "custom_private" field in CustomPath; providers
1885 * may prefer to use that rather than define another struct type.
1888 struct CustomPathMethods;
1890 typedef struct CustomPath
1892 Path path;
1893 uint32 flags; /* mask of CUSTOMPATH_* flags, see
1894 * nodes/extensible.h */
1895 List *custom_paths; /* list of child Path nodes, if any */
1896 List *custom_restrictinfo;
1897 List *custom_private;
1898 const struct CustomPathMethods *methods;
1899 } CustomPath;
1902 * AppendPath represents an Append plan, ie, successive execution of
1903 * several member plans.
1905 * For partial Append, 'subpaths' contains non-partial subpaths followed by
1906 * partial subpaths.
1908 * Note: it is possible for "subpaths" to contain only one, or even no,
1909 * elements. These cases are optimized during create_append_plan.
1910 * In particular, an AppendPath with no subpaths is a "dummy" path that
1911 * is created to represent the case that a relation is provably empty.
1912 * (This is a convenient representation because it means that when we build
1913 * an appendrel and find that all its children have been excluded, no extra
1914 * action is needed to recognize the relation as dummy.)
1916 typedef struct AppendPath
1918 Path path;
1919 List *subpaths; /* list of component Paths */
1920 /* Index of first partial path in subpaths; list_length(subpaths) if none */
1921 int first_partial_path;
1922 Cardinality limit_tuples; /* hard limit on output tuples, or -1 */
1923 } AppendPath;
1925 #define IS_DUMMY_APPEND(p) \
1926 (IsA((p), AppendPath) && ((AppendPath *) (p))->subpaths == NIL)
1929 * A relation that's been proven empty will have one path that is dummy
1930 * (but might have projection paths on top). For historical reasons,
1931 * this is provided as a macro that wraps is_dummy_rel().
1933 #define IS_DUMMY_REL(r) is_dummy_rel(r)
1934 extern bool is_dummy_rel(RelOptInfo *rel);
1937 * MergeAppendPath represents a MergeAppend plan, ie, the merging of sorted
1938 * results from several member plans to produce similarly-sorted output.
1940 typedef struct MergeAppendPath
1942 Path path;
1943 List *subpaths; /* list of component Paths */
1944 Cardinality limit_tuples; /* hard limit on output tuples, or -1 */
1945 } MergeAppendPath;
1948 * GroupResultPath represents use of a Result plan node to compute the
1949 * output of a degenerate GROUP BY case, wherein we know we should produce
1950 * exactly one row, which might then be filtered by a HAVING qual.
1952 * Note that quals is a list of bare clauses, not RestrictInfos.
1954 typedef struct GroupResultPath
1956 Path path;
1957 List *quals;
1958 } GroupResultPath;
1961 * MaterialPath represents use of a Material plan node, i.e., caching of
1962 * the output of its subpath. This is used when the subpath is expensive
1963 * and needs to be scanned repeatedly, or when we need mark/restore ability
1964 * and the subpath doesn't have it.
1966 typedef struct MaterialPath
1968 Path path;
1969 Path *subpath;
1970 } MaterialPath;
1973 * MemoizePath represents a Memoize plan node, i.e., a cache that caches
1974 * tuples from parameterized paths to save the underlying node from having to
1975 * be rescanned for parameter values which are already cached.
1977 typedef struct MemoizePath
1979 Path path;
1980 Path *subpath; /* outerpath to cache tuples from */
1981 List *hash_operators; /* OIDs of hash equality ops for cache keys */
1982 List *param_exprs; /* expressions that are cache keys */
1983 bool singlerow; /* true if the cache entry is to be marked as
1984 * complete after caching the first record. */
1985 bool binary_mode; /* true when cache key should be compared bit
1986 * by bit, false when using hash equality ops */
1987 Cardinality calls; /* expected number of rescans */
1988 uint32 est_entries; /* The maximum number of entries that the
1989 * planner expects will fit in the cache, or 0
1990 * if unknown */
1991 } MemoizePath;
1994 * UniquePath represents elimination of distinct rows from the output of
1995 * its subpath.
1997 * This can represent significantly different plans: either hash-based or
1998 * sort-based implementation, or a no-op if the input path can be proven
1999 * distinct already. The decision is sufficiently localized that it's not
2000 * worth having separate Path node types. (Note: in the no-op case, we could
2001 * eliminate the UniquePath node entirely and just return the subpath; but
2002 * it's convenient to have a UniquePath in the path tree to signal upper-level
2003 * routines that the input is known distinct.)
2005 typedef enum UniquePathMethod
2007 UNIQUE_PATH_NOOP, /* input is known unique already */
2008 UNIQUE_PATH_HASH, /* use hashing */
2009 UNIQUE_PATH_SORT, /* use sorting */
2010 } UniquePathMethod;
2012 typedef struct UniquePath
2014 Path path;
2015 Path *subpath;
2016 UniquePathMethod umethod;
2017 List *in_operators; /* equality operators of the IN clause */
2018 List *uniq_exprs; /* expressions to be made unique */
2019 } UniquePath;
2022 * GatherPath runs several copies of a plan in parallel and collects the
2023 * results. The parallel leader may also execute the plan, unless the
2024 * single_copy flag is set.
2026 typedef struct GatherPath
2028 Path path;
2029 Path *subpath; /* path for each worker */
2030 bool single_copy; /* don't execute path more than once */
2031 int num_workers; /* number of workers sought to help */
2032 } GatherPath;
2035 * GatherMergePath runs several copies of a plan in parallel and collects
2036 * the results, preserving their common sort order.
2038 typedef struct GatherMergePath
2040 Path path;
2041 Path *subpath; /* path for each worker */
2042 int num_workers; /* number of workers sought to help */
2043 } GatherMergePath;
2047 * All join-type paths share these fields.
2050 typedef struct JoinPath
2052 pg_node_attr(abstract)
2054 Path path;
2056 JoinType jointype;
2058 bool inner_unique; /* each outer tuple provably matches no more
2059 * than one inner tuple */
2061 Path *outerjoinpath; /* path for the outer side of the join */
2062 Path *innerjoinpath; /* path for the inner side of the join */
2064 List *joinrestrictinfo; /* RestrictInfos to apply to join */
2067 * See the notes for RelOptInfo and ParamPathInfo to understand why
2068 * joinrestrictinfo is needed in JoinPath, and can't be merged into the
2069 * parent RelOptInfo.
2071 } JoinPath;
2074 * A nested-loop path needs no special fields.
2077 typedef struct NestPath
2079 JoinPath jpath;
2080 } NestPath;
2083 * A mergejoin path has these fields.
2085 * Unlike other path types, a MergePath node doesn't represent just a single
2086 * run-time plan node: it can represent up to four. Aside from the MergeJoin
2087 * node itself, there can be a Sort node for the outer input, a Sort node
2088 * for the inner input, and/or a Material node for the inner input. We could
2089 * represent these nodes by separate path nodes, but considering how many
2090 * different merge paths are investigated during a complex join problem,
2091 * it seems better to avoid unnecessary palloc overhead.
2093 * path_mergeclauses lists the clauses (in the form of RestrictInfos)
2094 * that will be used in the merge.
2096 * Note that the mergeclauses are a subset of the parent relation's
2097 * restriction-clause list. Any join clauses that are not mergejoinable
2098 * appear only in the parent's restrict list, and must be checked by a
2099 * qpqual at execution time.
2101 * outersortkeys (resp. innersortkeys) is NIL if the outer path
2102 * (resp. inner path) is already ordered appropriately for the
2103 * mergejoin. If it is not NIL then it is a PathKeys list describing
2104 * the ordering that must be created by an explicit Sort node.
2106 * skip_mark_restore is true if the executor need not do mark/restore calls.
2107 * Mark/restore overhead is usually required, but can be skipped if we know
2108 * that the executor need find only one match per outer tuple, and that the
2109 * mergeclauses are sufficient to identify a match. In such cases the
2110 * executor can immediately advance the outer relation after processing a
2111 * match, and therefore it need never back up the inner relation.
2113 * materialize_inner is true if a Material node should be placed atop the
2114 * inner input. This may appear with or without an inner Sort step.
2117 typedef struct MergePath
2119 JoinPath jpath;
2120 List *path_mergeclauses; /* join clauses to be used for merge */
2121 List *outersortkeys; /* keys for explicit sort, if any */
2122 List *innersortkeys; /* keys for explicit sort, if any */
2123 bool skip_mark_restore; /* can executor skip mark/restore? */
2124 bool materialize_inner; /* add Materialize to inner? */
2125 } MergePath;
2128 * A hashjoin path has these fields.
2130 * The remarks above for mergeclauses apply for hashclauses as well.
2132 * Hashjoin does not care what order its inputs appear in, so we have
2133 * no need for sortkeys.
2136 typedef struct HashPath
2138 JoinPath jpath;
2139 List *path_hashclauses; /* join clauses used for hashing */
2140 int num_batches; /* number of batches expected */
2141 Cardinality inner_rows_total; /* total inner rows expected */
2142 } HashPath;
2145 * ProjectionPath represents a projection (that is, targetlist computation)
2147 * Nominally, this path node represents using a Result plan node to do a
2148 * projection step. However, if the input plan node supports projection,
2149 * we can just modify its output targetlist to do the required calculations
2150 * directly, and not need a Result. In some places in the planner we can just
2151 * jam the desired PathTarget into the input path node (and adjust its cost
2152 * accordingly), so we don't need a ProjectionPath. But in other places
2153 * it's necessary to not modify the input path node, so we need a separate
2154 * ProjectionPath node, which is marked dummy to indicate that we intend to
2155 * assign the work to the input plan node. The estimated cost for the
2156 * ProjectionPath node will account for whether a Result will be used or not.
2158 typedef struct ProjectionPath
2160 Path path;
2161 Path *subpath; /* path representing input source */
2162 bool dummypp; /* true if no separate Result is needed */
2163 } ProjectionPath;
2166 * ProjectSetPath represents evaluation of a targetlist that includes
2167 * set-returning function(s), which will need to be implemented by a
2168 * ProjectSet plan node.
2170 typedef struct ProjectSetPath
2172 Path path;
2173 Path *subpath; /* path representing input source */
2174 } ProjectSetPath;
2177 * SortPath represents an explicit sort step
2179 * The sort keys are, by definition, the same as path.pathkeys.
2181 * Note: the Sort plan node cannot project, so path.pathtarget must be the
2182 * same as the input's pathtarget.
2184 typedef struct SortPath
2186 Path path;
2187 Path *subpath; /* path representing input source */
2188 } SortPath;
2191 * IncrementalSortPath represents an incremental sort step
2193 * This is like a regular sort, except some leading key columns are assumed
2194 * to be ordered already.
2196 typedef struct IncrementalSortPath
2198 SortPath spath;
2199 int nPresortedCols; /* number of presorted columns */
2200 } IncrementalSortPath;
2203 * GroupPath represents grouping (of presorted input)
2205 * groupClause represents the columns to be grouped on; the input path
2206 * must be at least that well sorted.
2208 * We can also apply a qual to the grouped rows (equivalent of HAVING)
2210 typedef struct GroupPath
2212 Path path;
2213 Path *subpath; /* path representing input source */
2214 List *groupClause; /* a list of SortGroupClause's */
2215 List *qual; /* quals (HAVING quals), if any */
2216 } GroupPath;
2219 * UpperUniquePath represents adjacent-duplicate removal (in presorted input)
2221 * The columns to be compared are the first numkeys columns of the path's
2222 * pathkeys. The input is presumed already sorted that way.
2224 typedef struct UpperUniquePath
2226 Path path;
2227 Path *subpath; /* path representing input source */
2228 int numkeys; /* number of pathkey columns to compare */
2229 } UpperUniquePath;
2232 * AggPath represents generic computation of aggregate functions
2234 * This may involve plain grouping (but not grouping sets), using either
2235 * sorted or hashed grouping; for the AGG_SORTED case, the input must be
2236 * appropriately presorted.
2238 typedef struct AggPath
2240 Path path;
2241 Path *subpath; /* path representing input source */
2242 AggStrategy aggstrategy; /* basic strategy, see nodes.h */
2243 AggSplit aggsplit; /* agg-splitting mode, see nodes.h */
2244 Cardinality numGroups; /* estimated number of groups in input */
2245 uint64 transitionSpace; /* for pass-by-ref transition data */
2246 List *groupClause; /* a list of SortGroupClause's */
2247 List *qual; /* quals (HAVING quals), if any */
2248 } AggPath;
2251 * Various annotations used for grouping sets in the planner.
2254 typedef struct GroupingSetData
2256 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
2258 NodeTag type;
2259 List *set; /* grouping set as list of sortgrouprefs */
2260 Cardinality numGroups; /* est. number of result groups */
2261 } GroupingSetData;
2263 typedef struct RollupData
2265 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
2267 NodeTag type;
2268 List *groupClause; /* applicable subset of parse->groupClause */
2269 List *gsets; /* lists of integer indexes into groupClause */
2270 List *gsets_data; /* list of GroupingSetData */
2271 Cardinality numGroups; /* est. number of result groups */
2272 bool hashable; /* can be hashed */
2273 bool is_hashed; /* to be implemented as a hashagg */
2274 } RollupData;
2277 * GroupingSetsPath represents a GROUPING SETS aggregation
2280 typedef struct GroupingSetsPath
2282 Path path;
2283 Path *subpath; /* path representing input source */
2284 AggStrategy aggstrategy; /* basic strategy */
2285 List *rollups; /* list of RollupData */
2286 List *qual; /* quals (HAVING quals), if any */
2287 uint64 transitionSpace; /* for pass-by-ref transition data */
2288 } GroupingSetsPath;
2291 * MinMaxAggPath represents computation of MIN/MAX aggregates from indexes
2293 typedef struct MinMaxAggPath
2295 Path path;
2296 List *mmaggregates; /* list of MinMaxAggInfo */
2297 List *quals; /* HAVING quals, if any */
2298 } MinMaxAggPath;
2301 * WindowAggPath represents generic computation of window functions
2303 typedef struct WindowAggPath
2305 Path path;
2306 Path *subpath; /* path representing input source */
2307 WindowClause *winclause; /* WindowClause we'll be using */
2308 List *qual; /* lower-level WindowAgg runconditions */
2309 List *runCondition; /* OpExpr List to short-circuit execution */
2310 bool topwindow; /* false for all apart from the WindowAgg
2311 * that's closest to the root of the plan */
2312 } WindowAggPath;
2315 * SetOpPath represents a set-operation, that is INTERSECT or EXCEPT
2317 typedef struct SetOpPath
2319 Path path;
2320 Path *subpath; /* path representing input source */
2321 SetOpCmd cmd; /* what to do, see nodes.h */
2322 SetOpStrategy strategy; /* how to do it, see nodes.h */
2323 List *distinctList; /* SortGroupClauses identifying target cols */
2324 AttrNumber flagColIdx; /* where is the flag column, if any */
2325 int firstFlag; /* flag value for first input relation */
2326 Cardinality numGroups; /* estimated number of groups in input */
2327 } SetOpPath;
2330 * RecursiveUnionPath represents a recursive UNION node
2332 typedef struct RecursiveUnionPath
2334 Path path;
2335 Path *leftpath; /* paths representing input sources */
2336 Path *rightpath;
2337 List *distinctList; /* SortGroupClauses identifying target cols */
2338 int wtParam; /* ID of Param representing work table */
2339 Cardinality numGroups; /* estimated number of groups in input */
2340 } RecursiveUnionPath;
2343 * LockRowsPath represents acquiring row locks for SELECT FOR UPDATE/SHARE
2345 typedef struct LockRowsPath
2347 Path path;
2348 Path *subpath; /* path representing input source */
2349 List *rowMarks; /* a list of PlanRowMark's */
2350 int epqParam; /* ID of Param for EvalPlanQual re-eval */
2351 } LockRowsPath;
2354 * ModifyTablePath represents performing INSERT/UPDATE/DELETE/MERGE
2356 * We represent most things that will be in the ModifyTable plan node
2357 * literally, except we have a child Path not Plan. But analysis of the
2358 * OnConflictExpr is deferred to createplan.c, as is collection of FDW data.
2360 typedef struct ModifyTablePath
2362 Path path;
2363 Path *subpath; /* Path producing source data */
2364 CmdType operation; /* INSERT, UPDATE, DELETE, or MERGE */
2365 bool canSetTag; /* do we set the command tag/es_processed? */
2366 Index nominalRelation; /* Parent RT index for use of EXPLAIN */
2367 Index rootRelation; /* Root RT index, if partitioned/inherited */
2368 bool partColsUpdated; /* some part key in hierarchy updated? */
2369 List *resultRelations; /* integer list of RT indexes */
2370 List *updateColnosLists; /* per-target-table update_colnos lists */
2371 List *withCheckOptionLists; /* per-target-table WCO lists */
2372 List *returningLists; /* per-target-table RETURNING tlists */
2373 List *rowMarks; /* PlanRowMarks (non-locking only) */
2374 OnConflictExpr *onconflict; /* ON CONFLICT clause, or NULL */
2375 int epqParam; /* ID of Param for EvalPlanQual re-eval */
2376 List *mergeActionLists; /* per-target-table lists of actions for
2377 * MERGE */
2378 List *mergeJoinConditions; /* per-target-table join conditions
2379 * for MERGE */
2380 } ModifyTablePath;
2383 * LimitPath represents applying LIMIT/OFFSET restrictions
2385 typedef struct LimitPath
2387 Path path;
2388 Path *subpath; /* path representing input source */
2389 Node *limitOffset; /* OFFSET parameter, or NULL if none */
2390 Node *limitCount; /* COUNT parameter, or NULL if none */
2391 LimitOption limitOption; /* FETCH FIRST with ties or exact number */
2392 } LimitPath;
2396 * Restriction clause info.
2398 * We create one of these for each AND sub-clause of a restriction condition
2399 * (WHERE or JOIN/ON clause). Since the restriction clauses are logically
2400 * ANDed, we can use any one of them or any subset of them to filter out
2401 * tuples, without having to evaluate the rest. The RestrictInfo node itself
2402 * stores data used by the optimizer while choosing the best query plan.
2404 * If a restriction clause references a single base relation, it will appear
2405 * in the baserestrictinfo list of the RelOptInfo for that base rel.
2407 * If a restriction clause references more than one base+OJ relation, it will
2408 * appear in the joininfo list of every RelOptInfo that describes a strict
2409 * subset of the relations mentioned in the clause. The joininfo lists are
2410 * used to drive join tree building by selecting plausible join candidates.
2411 * The clause cannot actually be applied until we have built a join rel
2412 * containing all the relations it references, however.
2414 * When we construct a join rel that includes all the relations referenced
2415 * in a multi-relation restriction clause, we place that clause into the
2416 * joinrestrictinfo lists of paths for the join rel, if neither left nor
2417 * right sub-path includes all relations referenced in the clause. The clause
2418 * will be applied at that join level, and will not propagate any further up
2419 * the join tree. (Note: the "predicate migration" code was once intended to
2420 * push restriction clauses up and down the plan tree based on evaluation
2421 * costs, but it's dead code and is unlikely to be resurrected in the
2422 * foreseeable future.)
2424 * Note that in the presence of more than two rels, a multi-rel restriction
2425 * might reach different heights in the join tree depending on the join
2426 * sequence we use. So, these clauses cannot be associated directly with
2427 * the join RelOptInfo, but must be kept track of on a per-join-path basis.
2429 * RestrictInfos that represent equivalence conditions (i.e., mergejoinable
2430 * equalities that are not outerjoin-delayed) are handled a bit differently.
2431 * Initially we attach them to the EquivalenceClasses that are derived from
2432 * them. When we construct a scan or join path, we look through all the
2433 * EquivalenceClasses and generate derived RestrictInfos representing the
2434 * minimal set of conditions that need to be checked for this particular scan
2435 * or join to enforce that all members of each EquivalenceClass are in fact
2436 * equal in all rows emitted by the scan or join.
2438 * The clause_relids field lists the base plus outer-join RT indexes that
2439 * actually appear in the clause. required_relids lists the minimum set of
2440 * relids needed to evaluate the clause; while this is often equal to
2441 * clause_relids, it can be more. We will add relids to required_relids when
2442 * we need to force an outer join ON clause to be evaluated exactly at the
2443 * level of the outer join, which is true except when it is a "degenerate"
2444 * condition that references only Vars from the nullable side of the join.
2446 * RestrictInfo nodes contain a flag to indicate whether a qual has been
2447 * pushed down to a lower level than its original syntactic placement in the
2448 * join tree would suggest. If an outer join prevents us from pushing a qual
2449 * down to its "natural" semantic level (the level associated with just the
2450 * base rels used in the qual) then we mark the qual with a "required_relids"
2451 * value including more than just the base rels it actually uses. By
2452 * pretending that the qual references all the rels required to form the outer
2453 * join, we prevent it from being evaluated below the outer join's joinrel.
2454 * When we do form the outer join's joinrel, we still need to distinguish
2455 * those quals that are actually in that join's JOIN/ON condition from those
2456 * that appeared elsewhere in the tree and were pushed down to the join rel
2457 * because they used no other rels. That's what the is_pushed_down flag is
2458 * for; it tells us that a qual is not an OUTER JOIN qual for the set of base
2459 * rels listed in required_relids. A clause that originally came from WHERE
2460 * or an INNER JOIN condition will *always* have its is_pushed_down flag set.
2461 * It's possible for an OUTER JOIN clause to be marked is_pushed_down too,
2462 * if we decide that it can be pushed down into the nullable side of the join.
2463 * In that case it acts as a plain filter qual for wherever it gets evaluated.
2464 * (In short, is_pushed_down is only false for non-degenerate outer join
2465 * conditions. Possibly we should rename it to reflect that meaning? But
2466 * see also the comments for RINFO_IS_PUSHED_DOWN, below.)
2468 * There is also an incompatible_relids field, which is a set of outer-join
2469 * relids above which we cannot evaluate the clause (because they might null
2470 * Vars it uses that should not be nulled yet). In principle this could be
2471 * filled in any RestrictInfo as the set of OJ relids that appear above the
2472 * clause and null Vars that it uses. In practice we only bother to populate
2473 * it for "clone" clauses, as it's currently only needed to prevent multiple
2474 * clones of the same clause from being accepted for evaluation at the same
2475 * join level.
2477 * There is also an outer_relids field, which is NULL except for outer join
2478 * clauses; for those, it is the set of relids on the outer side of the
2479 * clause's outer join. (These are rels that the clause cannot be applied to
2480 * in parameterized scans, since pushing it into the join's outer side would
2481 * lead to wrong answers.)
2483 * To handle security-barrier conditions efficiently, we mark RestrictInfo
2484 * nodes with a security_level field, in which higher values identify clauses
2485 * coming from less-trusted sources. The exact semantics are that a clause
2486 * cannot be evaluated before another clause with a lower security_level value
2487 * unless the first clause is leakproof. As with outer-join clauses, this
2488 * creates a reason for clauses to sometimes need to be evaluated higher in
2489 * the join tree than their contents would suggest; and even at a single plan
2490 * node, this rule constrains the order of application of clauses.
2492 * In general, the referenced clause might be arbitrarily complex. The
2493 * kinds of clauses we can handle as indexscan quals, mergejoin clauses,
2494 * or hashjoin clauses are limited (e.g., no volatile functions). The code
2495 * for each kind of path is responsible for identifying the restrict clauses
2496 * it can use and ignoring the rest. Clauses not implemented by an indexscan,
2497 * mergejoin, or hashjoin will be placed in the plan qual or joinqual field
2498 * of the finished Plan node, where they will be enforced by general-purpose
2499 * qual-expression-evaluation code. (But we are still entitled to count
2500 * their selectivity when estimating the result tuple count, if we
2501 * can guess what it is...)
2503 * When the referenced clause is an OR clause, we generate a modified copy
2504 * in which additional RestrictInfo nodes are inserted below the top-level
2505 * OR/AND structure. This is a convenience for OR indexscan processing:
2506 * indexquals taken from either the top level or an OR subclause will have
2507 * associated RestrictInfo nodes.
2509 * The can_join flag is set true if the clause looks potentially useful as
2510 * a merge or hash join clause, that is if it is a binary opclause with
2511 * nonoverlapping sets of relids referenced in the left and right sides.
2512 * (Whether the operator is actually merge or hash joinable isn't checked,
2513 * however.)
2515 * The pseudoconstant flag is set true if the clause contains no Vars of
2516 * the current query level and no volatile functions. Such a clause can be
2517 * pulled out and used as a one-time qual in a gating Result node. We keep
2518 * pseudoconstant clauses in the same lists as other RestrictInfos so that
2519 * the regular clause-pushing machinery can assign them to the correct join
2520 * level, but they need to be treated specially for cost and selectivity
2521 * estimates. Note that a pseudoconstant clause can never be an indexqual
2522 * or merge or hash join clause, so it's of no interest to large parts of
2523 * the planner.
2525 * When we generate multiple versions of a clause so as to have versions
2526 * that will work after commuting some left joins per outer join identity 3,
2527 * we mark the one with the fewest nullingrels bits with has_clone = true,
2528 * and the rest with is_clone = true. This allows proper filtering of
2529 * these redundant clauses, so that we apply only one version of them.
2531 * When join clauses are generated from EquivalenceClasses, there may be
2532 * several equally valid ways to enforce join equivalence, of which we need
2533 * apply only one. We mark clauses of this kind by setting parent_ec to
2534 * point to the generating EquivalenceClass. Multiple clauses with the same
2535 * parent_ec in the same join are redundant.
2537 * Most fields are ignored for equality, since they may not be set yet, and
2538 * should be derivable from the clause anyway.
2540 * parent_ec, left_ec, right_ec are not printed, lest it lead to infinite
2541 * recursion in plan tree dump.
2544 typedef struct RestrictInfo
2546 pg_node_attr(no_read, no_query_jumble)
2548 NodeTag type;
2550 /* the represented clause of WHERE or JOIN */
2551 Expr *clause;
2553 /* true if clause was pushed down in level */
2554 bool is_pushed_down;
2556 /* see comment above */
2557 bool can_join pg_node_attr(equal_ignore);
2559 /* see comment above */
2560 bool pseudoconstant pg_node_attr(equal_ignore);
2562 /* see comment above */
2563 bool has_clone;
2564 bool is_clone;
2566 /* true if known to contain no leaked Vars */
2567 bool leakproof pg_node_attr(equal_ignore);
2569 /* indicates if clause contains any volatile functions */
2570 VolatileFunctionStatus has_volatile pg_node_attr(equal_ignore);
2572 /* see comment above */
2573 Index security_level;
2575 /* number of base rels in clause_relids */
2576 int num_base_rels pg_node_attr(equal_ignore);
2578 /* The relids (varnos+varnullingrels) actually referenced in the clause: */
2579 Relids clause_relids pg_node_attr(equal_ignore);
2581 /* The set of relids required to evaluate the clause: */
2582 Relids required_relids;
2584 /* Relids above which we cannot evaluate the clause (see comment above) */
2585 Relids incompatible_relids;
2587 /* If an outer-join clause, the outer-side relations, else NULL: */
2588 Relids outer_relids;
2591 * Relids in the left/right side of the clause. These fields are set for
2592 * any binary opclause.
2594 Relids left_relids pg_node_attr(equal_ignore);
2595 Relids right_relids pg_node_attr(equal_ignore);
2598 * Modified clause with RestrictInfos. This field is NULL unless clause
2599 * is an OR clause.
2601 Expr *orclause pg_node_attr(equal_ignore);
2603 /*----------
2604 * Serial number of this RestrictInfo. This is unique within the current
2605 * PlannerInfo context, with a few critical exceptions:
2606 * 1. When we generate multiple clones of the same qual condition to
2607 * cope with outer join identity 3, all the clones get the same serial
2608 * number. This reflects that we only want to apply one of them in any
2609 * given plan.
2610 * 2. If we manufacture a commuted version of a qual to use as an index
2611 * condition, it copies the original's rinfo_serial, since it is in
2612 * practice the same condition.
2613 * 3. If we reduce a qual to constant-FALSE, the new constant-FALSE qual
2614 * copies the original's rinfo_serial, since it is in practice the same
2615 * condition.
2616 * 4. RestrictInfos made for a child relation copy their parent's
2617 * rinfo_serial. Likewise, when an EquivalenceClass makes a derived
2618 * equality clause for a child relation, it copies the rinfo_serial of
2619 * the matching equality clause for the parent. This allows detection
2620 * of redundant pushed-down equality clauses.
2621 *----------
2623 int rinfo_serial;
2626 * Generating EquivalenceClass. This field is NULL unless clause is
2627 * potentially redundant.
2629 EquivalenceClass *parent_ec pg_node_attr(copy_as_scalar, equal_ignore, read_write_ignore);
2632 * cache space for cost and selectivity
2635 /* eval cost of clause; -1 if not yet set */
2636 QualCost eval_cost pg_node_attr(equal_ignore);
2638 /* selectivity for "normal" (JOIN_INNER) semantics; -1 if not yet set */
2639 Selectivity norm_selec pg_node_attr(equal_ignore);
2640 /* selectivity for outer join semantics; -1 if not yet set */
2641 Selectivity outer_selec pg_node_attr(equal_ignore);
2644 * opfamilies containing clause operator; valid if clause is
2645 * mergejoinable, else NIL
2647 List *mergeopfamilies pg_node_attr(equal_ignore);
2650 * cache space for mergeclause processing; NULL if not yet set
2653 /* EquivalenceClass containing lefthand */
2654 EquivalenceClass *left_ec pg_node_attr(copy_as_scalar, equal_ignore, read_write_ignore);
2655 /* EquivalenceClass containing righthand */
2656 EquivalenceClass *right_ec pg_node_attr(copy_as_scalar, equal_ignore, read_write_ignore);
2657 /* EquivalenceMember for lefthand */
2658 EquivalenceMember *left_em pg_node_attr(copy_as_scalar, equal_ignore);
2659 /* EquivalenceMember for righthand */
2660 EquivalenceMember *right_em pg_node_attr(copy_as_scalar, equal_ignore);
2663 * List of MergeScanSelCache structs. Those aren't Nodes, so hard to
2664 * copy; instead replace with NIL. That has the effect that copying will
2665 * just reset the cache. Likewise, can't compare or print them.
2667 List *scansel_cache pg_node_attr(copy_as(NIL), equal_ignore, read_write_ignore);
2670 * transient workspace for use while considering a specific join path; T =
2671 * outer var on left, F = on right
2673 bool outer_is_left pg_node_attr(equal_ignore);
2676 * copy of clause operator; valid if clause is hashjoinable, else
2677 * InvalidOid
2679 Oid hashjoinoperator pg_node_attr(equal_ignore);
2682 * cache space for hashclause processing; -1 if not yet set
2684 /* avg bucketsize of left side */
2685 Selectivity left_bucketsize pg_node_attr(equal_ignore);
2686 /* avg bucketsize of right side */
2687 Selectivity right_bucketsize pg_node_attr(equal_ignore);
2688 /* left side's most common val's freq */
2689 Selectivity left_mcvfreq pg_node_attr(equal_ignore);
2690 /* right side's most common val's freq */
2691 Selectivity right_mcvfreq pg_node_attr(equal_ignore);
2693 /* hash equality operators used for memoize nodes, else InvalidOid */
2694 Oid left_hasheqoperator pg_node_attr(equal_ignore);
2695 Oid right_hasheqoperator pg_node_attr(equal_ignore);
2696 } RestrictInfo;
2699 * This macro embodies the correct way to test whether a RestrictInfo is
2700 * "pushed down" to a given outer join, that is, should be treated as a filter
2701 * clause rather than a join clause at that outer join. This is certainly so
2702 * if is_pushed_down is true; but examining that is not sufficient anymore,
2703 * because outer-join clauses will get pushed down to lower outer joins when
2704 * we generate a path for the lower outer join that is parameterized by the
2705 * LHS of the upper one. We can detect such a clause by noting that its
2706 * required_relids exceed the scope of the join.
2708 #define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids) \
2709 ((rinfo)->is_pushed_down || \
2710 !bms_is_subset((rinfo)->required_relids, joinrelids))
2713 * Since mergejoinscansel() is a relatively expensive function, and would
2714 * otherwise be invoked many times while planning a large join tree,
2715 * we go out of our way to cache its results. Each mergejoinable
2716 * RestrictInfo carries a list of the specific sort orderings that have
2717 * been considered for use with it, and the resulting selectivities.
2719 typedef struct MergeScanSelCache
2721 /* Ordering details (cache lookup key) */
2722 Oid opfamily; /* btree opfamily defining the ordering */
2723 Oid collation; /* collation for the ordering */
2724 int strategy; /* sort direction (ASC or DESC) */
2725 bool nulls_first; /* do NULLs come before normal values? */
2726 /* Results */
2727 Selectivity leftstartsel; /* first-join fraction for clause left side */
2728 Selectivity leftendsel; /* last-join fraction for clause left side */
2729 Selectivity rightstartsel; /* first-join fraction for clause right side */
2730 Selectivity rightendsel; /* last-join fraction for clause right side */
2731 } MergeScanSelCache;
2734 * Placeholder node for an expression to be evaluated below the top level
2735 * of a plan tree. This is used during planning to represent the contained
2736 * expression. At the end of the planning process it is replaced by either
2737 * the contained expression or a Var referring to a lower-level evaluation of
2738 * the contained expression. Generally the evaluation occurs below an outer
2739 * join, and Var references above the outer join might thereby yield NULL
2740 * instead of the expression value.
2742 * phrels and phlevelsup correspond to the varno/varlevelsup fields of a
2743 * plain Var, except that phrels has to be a relid set since the evaluation
2744 * level of a PlaceHolderVar might be a join rather than a base relation.
2745 * Likewise, phnullingrels corresponds to varnullingrels.
2747 * Although the planner treats this as an expression node type, it is not
2748 * recognized by the parser or executor, so we declare it here rather than
2749 * in primnodes.h.
2751 * We intentionally do not compare phexpr. Two PlaceHolderVars with the
2752 * same ID and levelsup should be considered equal even if the contained
2753 * expressions have managed to mutate to different states. This will
2754 * happen during final plan construction when there are nested PHVs, since
2755 * the inner PHV will get replaced by a Param in some copies of the outer
2756 * PHV. Another way in which it can happen is that initplan sublinks
2757 * could get replaced by differently-numbered Params when sublink folding
2758 * is done. (The end result of such a situation would be some
2759 * unreferenced initplans, which is annoying but not really a problem.)
2760 * On the same reasoning, there is no need to examine phrels. But we do
2761 * need to compare phnullingrels, as that represents effects that are
2762 * external to the original value of the PHV.
2765 typedef struct PlaceHolderVar
2767 pg_node_attr(no_query_jumble)
2769 Expr xpr;
2771 /* the represented expression */
2772 Expr *phexpr pg_node_attr(equal_ignore);
2774 /* base+OJ relids syntactically within expr src */
2775 Relids phrels pg_node_attr(equal_ignore);
2777 /* RT indexes of outer joins that can null PHV's value */
2778 Relids phnullingrels;
2780 /* ID for PHV (unique within planner run) */
2781 Index phid;
2783 /* > 0 if PHV belongs to outer query */
2784 Index phlevelsup;
2785 } PlaceHolderVar;
2788 * "Special join" info.
2790 * One-sided outer joins constrain the order of joining partially but not
2791 * completely. We flatten such joins into the planner's top-level list of
2792 * relations to join, but record information about each outer join in a
2793 * SpecialJoinInfo struct. These structs are kept in the PlannerInfo node's
2794 * join_info_list.
2796 * Similarly, semijoins and antijoins created by flattening IN (subselect)
2797 * and EXISTS(subselect) clauses create partial constraints on join order.
2798 * These are likewise recorded in SpecialJoinInfo structs.
2800 * We make SpecialJoinInfos for FULL JOINs even though there is no flexibility
2801 * of planning for them, because this simplifies make_join_rel()'s API.
2803 * min_lefthand and min_righthand are the sets of base+OJ relids that must be
2804 * available on each side when performing the special join.
2805 * It is not valid for either min_lefthand or min_righthand to be empty sets;
2806 * if they were, this would break the logic that enforces join order.
2808 * syn_lefthand and syn_righthand are the sets of base+OJ relids that are
2809 * syntactically below this special join. (These are needed to help compute
2810 * min_lefthand and min_righthand for higher joins.)
2812 * jointype is never JOIN_RIGHT; a RIGHT JOIN is handled by switching
2813 * the inputs to make it a LEFT JOIN. It's never JOIN_RIGHT_ANTI either.
2814 * So the allowed values of jointype in a join_info_list member are only
2815 * LEFT, FULL, SEMI, or ANTI.
2817 * ojrelid is the RT index of the join RTE representing this outer join,
2818 * if there is one. It is zero when jointype is INNER or SEMI, and can be
2819 * zero for jointype ANTI (if the join was transformed from a SEMI join).
2820 * One use for this field is that when constructing the output targetlist of a
2821 * join relation that implements this OJ, we add ojrelid to the varnullingrels
2822 * and phnullingrels fields of nullable (RHS) output columns, so that the
2823 * output Vars and PlaceHolderVars correctly reflect the nulling that has
2824 * potentially happened to them.
2826 * commute_above_l is filled with the relids of syntactically-higher outer
2827 * joins that have been found to commute with this one per outer join identity
2828 * 3 (see optimizer/README), when this join is in the LHS of the upper join
2829 * (so, this is the lower join in the first form of the identity).
2831 * commute_above_r is filled with the relids of syntactically-higher outer
2832 * joins that have been found to commute with this one per outer join identity
2833 * 3, when this join is in the RHS of the upper join (so, this is the lower
2834 * join in the second form of the identity).
2836 * commute_below_l is filled with the relids of syntactically-lower outer
2837 * joins that have been found to commute with this one per outer join identity
2838 * 3 and are in the LHS of this join (so, this is the upper join in the first
2839 * form of the identity).
2841 * commute_below_r is filled with the relids of syntactically-lower outer
2842 * joins that have been found to commute with this one per outer join identity
2843 * 3 and are in the RHS of this join (so, this is the upper join in the second
2844 * form of the identity).
2846 * lhs_strict is true if the special join's condition cannot succeed when the
2847 * LHS variables are all NULL (this means that an outer join can commute with
2848 * upper-level outer joins even if it appears in their RHS). We don't bother
2849 * to set lhs_strict for FULL JOINs, however.
2851 * For a semijoin, we also extract the join operators and their RHS arguments
2852 * and set semi_operators, semi_rhs_exprs, semi_can_btree, and semi_can_hash.
2853 * This is done in support of possibly unique-ifying the RHS, so we don't
2854 * bother unless at least one of semi_can_btree and semi_can_hash can be set
2855 * true. (You might expect that this information would be computed during
2856 * join planning; but it's helpful to have it available during planning of
2857 * parameterized table scans, so we store it in the SpecialJoinInfo structs.)
2859 * For purposes of join selectivity estimation, we create transient
2860 * SpecialJoinInfo structures for regular inner joins; so it is possible
2861 * to have jointype == JOIN_INNER in such a structure, even though this is
2862 * not allowed within join_info_list. We also create transient
2863 * SpecialJoinInfos with jointype == JOIN_INNER for outer joins, since for
2864 * cost estimation purposes it is sometimes useful to know the join size under
2865 * plain innerjoin semantics. Note that lhs_strict and the semi_xxx fields
2866 * are not set meaningfully within such structs.
2868 * We also create transient SpecialJoinInfos for child joins during
2869 * partitionwise join planning, which are also not present in join_info_list.
2871 #ifndef HAVE_SPECIALJOININFO_TYPEDEF
2872 typedef struct SpecialJoinInfo SpecialJoinInfo;
2873 #define HAVE_SPECIALJOININFO_TYPEDEF 1
2874 #endif
2876 struct SpecialJoinInfo
2878 pg_node_attr(no_read, no_query_jumble)
2880 NodeTag type;
2881 Relids min_lefthand; /* base+OJ relids in minimum LHS for join */
2882 Relids min_righthand; /* base+OJ relids in minimum RHS for join */
2883 Relids syn_lefthand; /* base+OJ relids syntactically within LHS */
2884 Relids syn_righthand; /* base+OJ relids syntactically within RHS */
2885 JoinType jointype; /* always INNER, LEFT, FULL, SEMI, or ANTI */
2886 Index ojrelid; /* outer join's RT index; 0 if none */
2887 Relids commute_above_l; /* commuting OJs above this one, if LHS */
2888 Relids commute_above_r; /* commuting OJs above this one, if RHS */
2889 Relids commute_below_l; /* commuting OJs in this one's LHS */
2890 Relids commute_below_r; /* commuting OJs in this one's RHS */
2891 bool lhs_strict; /* joinclause is strict for some LHS rel */
2892 /* Remaining fields are set only for JOIN_SEMI jointype: */
2893 bool semi_can_btree; /* true if semi_operators are all btree */
2894 bool semi_can_hash; /* true if semi_operators are all hash */
2895 List *semi_operators; /* OIDs of equality join operators */
2896 List *semi_rhs_exprs; /* righthand-side expressions of these ops */
2900 * Transient outer-join clause info.
2902 * We set aside every outer join ON clause that looks mergejoinable,
2903 * and process it specially at the end of qual distribution.
2905 typedef struct OuterJoinClauseInfo
2907 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
2909 NodeTag type;
2910 RestrictInfo *rinfo; /* a mergejoinable outer-join clause */
2911 SpecialJoinInfo *sjinfo; /* the outer join's SpecialJoinInfo */
2912 } OuterJoinClauseInfo;
2915 * Append-relation info.
2917 * When we expand an inheritable table or a UNION-ALL subselect into an
2918 * "append relation" (essentially, a list of child RTEs), we build an
2919 * AppendRelInfo for each child RTE. The list of AppendRelInfos indicates
2920 * which child RTEs must be included when expanding the parent, and each node
2921 * carries information needed to translate between columns of the parent and
2922 * columns of the child.
2924 * These structs are kept in the PlannerInfo node's append_rel_list, with
2925 * append_rel_array[] providing a convenient lookup method for the struct
2926 * associated with a particular child relid (there can be only one, though
2927 * parent rels may have many entries in append_rel_list).
2929 * Note: after completion of the planner prep phase, any given RTE is an
2930 * append parent having entries in append_rel_list if and only if its
2931 * "inh" flag is set. We clear "inh" for plain tables that turn out not
2932 * to have inheritance children, and (in an abuse of the original meaning
2933 * of the flag) we set "inh" for subquery RTEs that turn out to be
2934 * flattenable UNION ALL queries. This lets us avoid useless searches
2935 * of append_rel_list.
2937 * Note: the data structure assumes that append-rel members are single
2938 * baserels. This is OK for inheritance, but it prevents us from pulling
2939 * up a UNION ALL member subquery if it contains a join. While that could
2940 * be fixed with a more complex data structure, at present there's not much
2941 * point because no improvement in the plan could result.
2944 typedef struct AppendRelInfo
2946 pg_node_attr(no_query_jumble)
2948 NodeTag type;
2951 * These fields uniquely identify this append relationship. There can be
2952 * (in fact, always should be) multiple AppendRelInfos for the same
2953 * parent_relid, but never more than one per child_relid, since a given
2954 * RTE cannot be a child of more than one append parent.
2956 Index parent_relid; /* RT index of append parent rel */
2957 Index child_relid; /* RT index of append child rel */
2960 * For an inheritance appendrel, the parent and child are both regular
2961 * relations, and we store their rowtype OIDs here for use in translating
2962 * whole-row Vars. For a UNION-ALL appendrel, the parent and child are
2963 * both subqueries with no named rowtype, and we store InvalidOid here.
2965 Oid parent_reltype; /* OID of parent's composite type */
2966 Oid child_reltype; /* OID of child's composite type */
2969 * The N'th element of this list is a Var or expression representing the
2970 * child column corresponding to the N'th column of the parent. This is
2971 * used to translate Vars referencing the parent rel into references to
2972 * the child. A list element is NULL if it corresponds to a dropped
2973 * column of the parent (this is only possible for inheritance cases, not
2974 * UNION ALL). The list elements are always simple Vars for inheritance
2975 * cases, but can be arbitrary expressions in UNION ALL cases.
2977 * Notice we only store entries for user columns (attno > 0). Whole-row
2978 * Vars are special-cased, and system columns (attno < 0) need no special
2979 * translation since their attnos are the same for all tables.
2981 * Caution: the Vars have varlevelsup = 0. Be careful to adjust as needed
2982 * when copying into a subquery.
2984 List *translated_vars; /* Expressions in the child's Vars */
2987 * This array simplifies translations in the reverse direction, from
2988 * child's column numbers to parent's. The entry at [ccolno - 1] is the
2989 * 1-based parent column number for child column ccolno, or zero if that
2990 * child column is dropped or doesn't exist in the parent.
2992 int num_child_cols; /* length of array */
2993 AttrNumber *parent_colnos pg_node_attr(array_size(num_child_cols));
2996 * We store the parent table's OID here for inheritance, or InvalidOid for
2997 * UNION ALL. This is only needed to help in generating error messages if
2998 * an attempt is made to reference a dropped parent column.
3000 Oid parent_reloid; /* OID of parent relation */
3001 } AppendRelInfo;
3004 * Information about a row-identity "resjunk" column in UPDATE/DELETE/MERGE.
3006 * In partitioned UPDATE/DELETE/MERGE it's important for child partitions to
3007 * share row-identity columns whenever possible, so as not to chew up too many
3008 * targetlist columns. We use these structs to track which identity columns
3009 * have been requested. In the finished plan, each of these will give rise
3010 * to one resjunk entry in the targetlist of the ModifyTable's subplan node.
3012 * All the Vars stored in RowIdentityVarInfos must have varno ROWID_VAR, for
3013 * convenience of detecting duplicate requests. We'll replace that, in the
3014 * final plan, with the varno of the generating rel.
3016 * Outside this list, a Var with varno ROWID_VAR and varattno k is a reference
3017 * to the k-th element of the row_identity_vars list (k counting from 1).
3018 * We add such a reference to root->processed_tlist when creating the entry,
3019 * and it propagates into the plan tree from there.
3021 typedef struct RowIdentityVarInfo
3023 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
3025 NodeTag type;
3027 Var *rowidvar; /* Var to be evaluated (but varno=ROWID_VAR) */
3028 int32 rowidwidth; /* estimated average width */
3029 char *rowidname; /* name of the resjunk column */
3030 Relids rowidrels; /* RTE indexes of target rels using this */
3031 } RowIdentityVarInfo;
3034 * For each distinct placeholder expression generated during planning, we
3035 * store a PlaceHolderInfo node in the PlannerInfo node's placeholder_list.
3036 * This stores info that is needed centrally rather than in each copy of the
3037 * PlaceHolderVar. The phid fields identify which PlaceHolderInfo goes with
3038 * each PlaceHolderVar. Note that phid is unique throughout a planner run,
3039 * not just within a query level --- this is so that we need not reassign ID's
3040 * when pulling a subquery into its parent.
3042 * The idea is to evaluate the expression at (only) the ph_eval_at join level,
3043 * then allow it to bubble up like a Var until the ph_needed join level.
3044 * ph_needed has the same definition as attr_needed for a regular Var.
3046 * The PlaceHolderVar's expression might contain LATERAL references to vars
3047 * coming from outside its syntactic scope. If so, those rels are *not*
3048 * included in ph_eval_at, but they are recorded in ph_lateral.
3050 * Notice that when ph_eval_at is a join rather than a single baserel, the
3051 * PlaceHolderInfo may create constraints on join order: the ph_eval_at join
3052 * has to be formed below any outer joins that should null the PlaceHolderVar.
3054 * We create a PlaceHolderInfo only after determining that the PlaceHolderVar
3055 * is actually referenced in the plan tree, so that unreferenced placeholders
3056 * don't result in unnecessary constraints on join order.
3059 typedef struct PlaceHolderInfo
3061 pg_node_attr(no_read, no_query_jumble)
3063 NodeTag type;
3065 /* ID for PH (unique within planner run) */
3066 Index phid;
3069 * copy of PlaceHolderVar tree (should be redundant for comparison, could
3070 * be ignored)
3072 PlaceHolderVar *ph_var;
3074 /* lowest level we can evaluate value at */
3075 Relids ph_eval_at;
3077 /* relids of contained lateral refs, if any */
3078 Relids ph_lateral;
3080 /* highest level the value is needed at */
3081 Relids ph_needed;
3083 /* estimated attribute width */
3084 int32 ph_width;
3085 } PlaceHolderInfo;
3088 * This struct describes one potentially index-optimizable MIN/MAX aggregate
3089 * function. MinMaxAggPath contains a list of these, and if we accept that
3090 * path, the list is stored into root->minmax_aggs for use during setrefs.c.
3092 typedef struct MinMaxAggInfo
3094 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
3096 NodeTag type;
3098 /* pg_proc Oid of the aggregate */
3099 Oid aggfnoid;
3101 /* Oid of its sort operator */
3102 Oid aggsortop;
3104 /* expression we are aggregating on */
3105 Expr *target;
3108 * modified "root" for planning the subquery; not printed, too large, not
3109 * interesting enough
3111 PlannerInfo *subroot pg_node_attr(read_write_ignore);
3113 /* access path for subquery */
3114 Path *path;
3116 /* estimated cost to fetch first row */
3117 Cost pathcost;
3119 /* param for subplan's output */
3120 Param *param;
3121 } MinMaxAggInfo;
3124 * At runtime, PARAM_EXEC slots are used to pass values around from one plan
3125 * node to another. They can be used to pass values down into subqueries (for
3126 * outer references in subqueries), or up out of subqueries (for the results
3127 * of a subplan), or from a NestLoop plan node into its inner relation (when
3128 * the inner scan is parameterized with values from the outer relation).
3129 * The planner is responsible for assigning nonconflicting PARAM_EXEC IDs to
3130 * the PARAM_EXEC Params it generates.
3132 * Outer references are managed via root->plan_params, which is a list of
3133 * PlannerParamItems. While planning a subquery, each parent query level's
3134 * plan_params contains the values required from it by the current subquery.
3135 * During create_plan(), we use plan_params to track values that must be
3136 * passed from outer to inner sides of NestLoop plan nodes.
3138 * The item a PlannerParamItem represents can be one of three kinds:
3140 * A Var: the slot represents a variable of this level that must be passed
3141 * down because subqueries have outer references to it, or must be passed
3142 * from a NestLoop node to its inner scan. The varlevelsup value in the Var
3143 * will always be zero.
3145 * A PlaceHolderVar: this works much like the Var case, except that the
3146 * entry is a PlaceHolderVar node with a contained expression. The PHV
3147 * will have phlevelsup = 0, and the contained expression is adjusted
3148 * to match in level.
3150 * An Aggref (with an expression tree representing its argument): the slot
3151 * represents an aggregate expression that is an outer reference for some
3152 * subquery. The Aggref itself has agglevelsup = 0, and its argument tree
3153 * is adjusted to match in level.
3155 * Note: we detect duplicate Var and PlaceHolderVar parameters and coalesce
3156 * them into one slot, but we do not bother to do that for Aggrefs.
3157 * The scope of duplicate-elimination only extends across the set of
3158 * parameters passed from one query level into a single subquery, or for
3159 * nestloop parameters across the set of nestloop parameters used in a single
3160 * query level. So there is no possibility of a PARAM_EXEC slot being used
3161 * for conflicting purposes.
3163 * In addition, PARAM_EXEC slots are assigned for Params representing outputs
3164 * from subplans (values that are setParam items for those subplans). These
3165 * IDs need not be tracked via PlannerParamItems, since we do not need any
3166 * duplicate-elimination nor later processing of the represented expressions.
3167 * Instead, we just record the assignment of the slot number by appending to
3168 * root->glob->paramExecTypes.
3170 typedef struct PlannerParamItem
3172 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
3174 NodeTag type;
3176 Node *item; /* the Var, PlaceHolderVar, or Aggref */
3177 int paramId; /* its assigned PARAM_EXEC slot number */
3178 } PlannerParamItem;
3181 * When making cost estimates for a SEMI/ANTI/inner_unique join, there are
3182 * some correction factors that are needed in both nestloop and hash joins
3183 * to account for the fact that the executor can stop scanning inner rows
3184 * as soon as it finds a match to the current outer row. These numbers
3185 * depend only on the selected outer and inner join relations, not on the
3186 * particular paths used for them, so it's worthwhile to calculate them
3187 * just once per relation pair not once per considered path. This struct
3188 * is filled by compute_semi_anti_join_factors and must be passed along
3189 * to the join cost estimation functions.
3191 * outer_match_frac is the fraction of the outer tuples that are
3192 * expected to have at least one match.
3193 * match_count is the average number of matches expected for
3194 * outer tuples that have at least one match.
3196 typedef struct SemiAntiJoinFactors
3198 Selectivity outer_match_frac;
3199 Selectivity match_count;
3200 } SemiAntiJoinFactors;
3203 * Struct for extra information passed to subroutines of add_paths_to_joinrel
3205 * restrictlist contains all of the RestrictInfo nodes for restriction
3206 * clauses that apply to this join
3207 * mergeclause_list is a list of RestrictInfo nodes for available
3208 * mergejoin clauses in this join
3209 * inner_unique is true if each outer tuple provably matches no more
3210 * than one inner tuple
3211 * sjinfo is extra info about special joins for selectivity estimation
3212 * semifactors is as shown above (only valid for SEMI/ANTI/inner_unique joins)
3213 * param_source_rels are OK targets for parameterization of result paths
3215 typedef struct JoinPathExtraData
3217 List *restrictlist;
3218 List *mergeclause_list;
3219 bool inner_unique;
3220 SpecialJoinInfo *sjinfo;
3221 SemiAntiJoinFactors semifactors;
3222 Relids param_source_rels;
3223 } JoinPathExtraData;
3226 * Various flags indicating what kinds of grouping are possible.
3228 * GROUPING_CAN_USE_SORT should be set if it's possible to perform
3229 * sort-based implementations of grouping. When grouping sets are in use,
3230 * this will be true if sorting is potentially usable for any of the grouping
3231 * sets, even if it's not usable for all of them.
3233 * GROUPING_CAN_USE_HASH should be set if it's possible to perform
3234 * hash-based implementations of grouping.
3236 * GROUPING_CAN_PARTIAL_AGG should be set if the aggregation is of a type
3237 * for which we support partial aggregation (not, for example, grouping sets).
3238 * It says nothing about parallel-safety or the availability of suitable paths.
3240 #define GROUPING_CAN_USE_SORT 0x0001
3241 #define GROUPING_CAN_USE_HASH 0x0002
3242 #define GROUPING_CAN_PARTIAL_AGG 0x0004
3245 * What kind of partitionwise aggregation is in use?
3247 * PARTITIONWISE_AGGREGATE_NONE: Not used.
3249 * PARTITIONWISE_AGGREGATE_FULL: Aggregate each partition separately, and
3250 * append the results.
3252 * PARTITIONWISE_AGGREGATE_PARTIAL: Partially aggregate each partition
3253 * separately, append the results, and then finalize aggregation.
3255 typedef enum
3257 PARTITIONWISE_AGGREGATE_NONE,
3258 PARTITIONWISE_AGGREGATE_FULL,
3259 PARTITIONWISE_AGGREGATE_PARTIAL,
3260 } PartitionwiseAggregateType;
3263 * Struct for extra information passed to subroutines of create_grouping_paths
3265 * flags indicating what kinds of grouping are possible.
3266 * partial_costs_set is true if the agg_partial_costs and agg_final_costs
3267 * have been initialized.
3268 * agg_partial_costs gives partial aggregation costs.
3269 * agg_final_costs gives finalization costs.
3270 * target_parallel_safe is true if target is parallel safe.
3271 * havingQual gives list of quals to be applied after aggregation.
3272 * targetList gives list of columns to be projected.
3273 * patype is the type of partitionwise aggregation that is being performed.
3275 typedef struct
3277 /* Data which remains constant once set. */
3278 int flags;
3279 bool partial_costs_set;
3280 AggClauseCosts agg_partial_costs;
3281 AggClauseCosts agg_final_costs;
3283 /* Data which may differ across partitions. */
3284 bool target_parallel_safe;
3285 Node *havingQual;
3286 List *targetList;
3287 PartitionwiseAggregateType patype;
3288 } GroupPathExtraData;
3291 * Struct for extra information passed to subroutines of grouping_planner
3293 * limit_needed is true if we actually need a Limit plan node.
3294 * limit_tuples is an estimated bound on the number of output tuples,
3295 * or -1 if no LIMIT or couldn't estimate.
3296 * count_est and offset_est are the estimated values of the LIMIT and OFFSET
3297 * expressions computed by preprocess_limit() (see comments for
3298 * preprocess_limit() for more information).
3300 typedef struct
3302 bool limit_needed;
3303 Cardinality limit_tuples;
3304 int64 count_est;
3305 int64 offset_est;
3306 } FinalPathExtraData;
3309 * For speed reasons, cost estimation for join paths is performed in two
3310 * phases: the first phase tries to quickly derive a lower bound for the
3311 * join cost, and then we check if that's sufficient to reject the path.
3312 * If not, we come back for a more refined cost estimate. The first phase
3313 * fills a JoinCostWorkspace struct with its preliminary cost estimates
3314 * and possibly additional intermediate values. The second phase takes
3315 * these values as inputs to avoid repeating work.
3317 * (Ideally we'd declare this in cost.h, but it's also needed in pathnode.h,
3318 * so seems best to put it here.)
3320 typedef struct JoinCostWorkspace
3322 /* Preliminary cost estimates --- must not be larger than final ones! */
3323 Cost startup_cost; /* cost expended before fetching any tuples */
3324 Cost total_cost; /* total cost (assuming all tuples fetched) */
3326 /* Fields below here should be treated as private to costsize.c */
3327 Cost run_cost; /* non-startup cost components */
3329 /* private for cost_nestloop code */
3330 Cost inner_run_cost; /* also used by cost_mergejoin code */
3331 Cost inner_rescan_run_cost;
3333 /* private for cost_mergejoin code */
3334 Cardinality outer_rows;
3335 Cardinality inner_rows;
3336 Cardinality outer_skip_rows;
3337 Cardinality inner_skip_rows;
3339 /* private for cost_hashjoin code */
3340 int numbuckets;
3341 int numbatches;
3342 Cardinality inner_rows_total;
3343 } JoinCostWorkspace;
3346 * AggInfo holds information about an aggregate that needs to be computed.
3347 * Multiple Aggrefs in a query can refer to the same AggInfo by having the
3348 * same 'aggno' value, so that the aggregate is computed only once.
3350 typedef struct AggInfo
3352 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
3354 NodeTag type;
3357 * List of Aggref exprs that this state value is for.
3359 * There will always be at least one, but there can be multiple identical
3360 * Aggref's sharing the same per-agg.
3362 List *aggrefs;
3364 /* Transition state number for this aggregate */
3365 int transno;
3368 * "shareable" is false if this agg cannot share state values with other
3369 * aggregates because the final function is read-write.
3371 bool shareable;
3373 /* Oid of the final function, or InvalidOid if none */
3374 Oid finalfn_oid;
3375 } AggInfo;
3378 * AggTransInfo holds information about transition state that is used by one
3379 * or more aggregates in the query. Multiple aggregates can share the same
3380 * transition state, if they have the same inputs and the same transition
3381 * function. Aggrefs that share the same transition info have the same
3382 * 'aggtransno' value.
3384 typedef struct AggTransInfo
3386 pg_node_attr(no_copy_equal, no_read, no_query_jumble)
3388 NodeTag type;
3390 /* Inputs for this transition state */
3391 List *args;
3392 Expr *aggfilter;
3394 /* Oid of the state transition function */
3395 Oid transfn_oid;
3397 /* Oid of the serialization function, or InvalidOid if none */
3398 Oid serialfn_oid;
3400 /* Oid of the deserialization function, or InvalidOid if none */
3401 Oid deserialfn_oid;
3403 /* Oid of the combine function, or InvalidOid if none */
3404 Oid combinefn_oid;
3406 /* Oid of state value's datatype */
3407 Oid aggtranstype;
3409 /* Additional data about transtype */
3410 int32 aggtranstypmod;
3411 int transtypeLen;
3412 bool transtypeByVal;
3414 /* Space-consumption estimate */
3415 int32 aggtransspace;
3417 /* Initial value from pg_aggregate entry */
3418 Datum initValue pg_node_attr(read_write_ignore);
3419 bool initValueIsNull;
3420 } AggTransInfo;
3422 #endif /* PATHNODES_H */