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