1 /*-------------------------------------------------------------------------
4 * Routines to create the desired plan for processing a query.
5 * Planning is complete, we just need to convert the selected
8 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/backend/optimizer/plan/createplan.c
15 *-------------------------------------------------------------------------
22 #include "access/sysattr.h"
23 #include "catalog/pg_class.h"
24 #include "foreign/fdwapi.h"
25 #include "miscadmin.h"
26 #include "nodes/extensible.h"
27 #include "nodes/makefuncs.h"
28 #include "nodes/nodeFuncs.h"
29 #include "optimizer/clauses.h"
30 #include "optimizer/cost.h"
31 #include "optimizer/optimizer.h"
32 #include "optimizer/paramassign.h"
33 #include "optimizer/paths.h"
34 #include "optimizer/placeholder.h"
35 #include "optimizer/plancat.h"
36 #include "optimizer/planmain.h"
37 #include "optimizer/restrictinfo.h"
38 #include "optimizer/subselect.h"
39 #include "optimizer/tlist.h"
40 #include "parser/parse_clause.h"
41 #include "parser/parsetree.h"
42 #include "partitioning/partprune.h"
43 #include "utils/lsyscache.h"
47 * Flag bits that can appear in the flags argument of create_plan_recurse().
48 * These can be OR-ed together.
50 * CP_EXACT_TLIST specifies that the generated plan node must return exactly
51 * the tlist specified by the path's pathtarget (this overrides both
52 * CP_SMALL_TLIST and CP_LABEL_TLIST, if those are set). Otherwise, the
53 * plan node is allowed to return just the Vars and PlaceHolderVars needed
54 * to evaluate the pathtarget.
56 * CP_SMALL_TLIST specifies that a narrower tlist is preferred. This is
57 * passed down by parent nodes such as Sort and Hash, which will have to
58 * store the returned tuples.
60 * CP_LABEL_TLIST specifies that the plan node must return columns matching
61 * any sortgrouprefs specified in its pathtarget, with appropriate
62 * ressortgroupref labels. This is passed down by parent nodes such as Sort
63 * and Group, which need these values to be available in their inputs.
65 * CP_IGNORE_TLIST specifies that the caller plans to replace the targetlist,
66 * and therefore it doesn't matter a bit what target list gets generated.
68 #define CP_EXACT_TLIST 0x0001 /* Plan must return specified tlist */
69 #define CP_SMALL_TLIST 0x0002 /* Prefer narrower tlists */
70 #define CP_LABEL_TLIST 0x0004 /* tlist must contain sortgrouprefs */
71 #define CP_IGNORE_TLIST 0x0008 /* caller will replace tlist */
74 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path,
76 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path,
78 static List *build_path_tlist(PlannerInfo *root, Path *path);
79 static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags);
80 static List *get_gating_quals(PlannerInfo *root, List *quals);
81 static Plan *create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
83 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
84 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
85 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
86 static Result *create_group_result_plan(PlannerInfo *root,
87 GroupResultPath *best_path);
88 static ProjectSet *create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path);
89 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path,
91 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path,
93 static Gather *create_gather_plan(PlannerInfo *root, GatherPath *best_path);
94 static Plan *create_projection_plan(PlannerInfo *root,
95 ProjectionPath *best_path,
97 static Plan *inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe);
98 static Sort *create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags);
99 static Group *create_group_plan(PlannerInfo *root, GroupPath *best_path);
100 static Unique *create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path,
102 static Agg *create_agg_plan(PlannerInfo *root, AggPath *best_path);
103 static Plan *create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path);
104 static Result *create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path);
105 static WindowAgg *create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path);
106 static SetOp *create_setop_plan(PlannerInfo *root, SetOpPath *best_path,
108 static RecursiveUnion *create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path);
109 static LockRows *create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
111 static ModifyTable *create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path);
112 static Limit *create_limit_plan(PlannerInfo *root, LimitPath *best_path,
114 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
115 List *tlist, List *scan_clauses);
116 static SampleScan *create_samplescan_plan(PlannerInfo *root, Path *best_path,
117 List *tlist, List *scan_clauses);
118 static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
119 List *tlist, List *scan_clauses, bool indexonly);
120 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
121 BitmapHeapPath *best_path,
122 List *tlist, List *scan_clauses);
123 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
124 List **qual, List **indexqual, List **indexECs);
125 static void bitmap_subplan_mark_shared(Plan *plan);
126 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
127 List *tlist, List *scan_clauses);
128 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root,
129 SubqueryScanPath *best_path,
130 List *tlist, List *scan_clauses);
131 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
132 List *tlist, List *scan_clauses);
133 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
134 List *tlist, List *scan_clauses);
135 static TableFuncScan *create_tablefuncscan_plan(PlannerInfo *root, Path *best_path,
136 List *tlist, List *scan_clauses);
137 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
138 List *tlist, List *scan_clauses);
139 static NamedTuplestoreScan *create_namedtuplestorescan_plan(PlannerInfo *root,
140 Path *best_path, List *tlist, List *scan_clauses);
141 static Result *create_resultscan_plan(PlannerInfo *root, Path *best_path,
142 List *tlist, List *scan_clauses);
143 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
144 List *tlist, List *scan_clauses);
145 static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
146 List *tlist, List *scan_clauses);
147 static CustomScan *create_customscan_plan(PlannerInfo *root,
148 CustomPath *best_path,
149 List *tlist, List *scan_clauses);
150 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path);
151 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path);
152 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path);
153 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
154 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
155 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path);
156 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
157 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
158 static List *get_switched_clauses(List *clauses, Relids outerrelids);
159 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
160 static void copy_generic_path_info(Plan *dest, Path *src);
161 static void copy_plan_costsize(Plan *dest, Plan *src);
162 static void label_sort_with_costsize(PlannerInfo *root, Sort *plan,
163 double limit_tuples);
164 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
165 static SampleScan *make_samplescan(List *qptlist, List *qpqual, Index scanrelid,
166 TableSampleClause *tsc);
167 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
168 Oid indexid, List *indexqual, List *indexqualorig,
169 List *indexorderby, List *indexorderbyorig,
170 List *indexorderbyops,
171 ScanDirection indexscandir);
172 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
173 Index scanrelid, Oid indexid,
174 List *indexqual, List *indexorderby,
176 ScanDirection indexscandir);
177 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
179 List *indexqualorig);
180 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
183 List *bitmapqualorig,
185 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
187 static SubqueryScan *make_subqueryscan(List *qptlist,
191 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
192 Index scanrelid, List *functions, bool funcordinality);
193 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
194 Index scanrelid, List *values_lists);
195 static TableFuncScan *make_tablefuncscan(List *qptlist, List *qpqual,
196 Index scanrelid, TableFunc *tablefunc);
197 static CteScan *make_ctescan(List *qptlist, List *qpqual,
198 Index scanrelid, int ctePlanId, int cteParam);
199 static NamedTuplestoreScan *make_namedtuplestorescan(List *qptlist, List *qpqual,
200 Index scanrelid, char *enrname);
201 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
202 Index scanrelid, int wtParam);
203 static Append *make_append(List *appendplans, int first_partial_plan,
204 List *tlist, PartitionPruneInfo *partpruneinfo);
205 static RecursiveUnion *make_recursive_union(List *tlist,
211 static BitmapAnd *make_bitmap_and(List *bitmapplans);
212 static BitmapOr *make_bitmap_or(List *bitmapplans);
213 static NestLoop *make_nestloop(List *tlist,
214 List *joinclauses, List *otherclauses, List *nestParams,
215 Plan *lefttree, Plan *righttree,
216 JoinType jointype, bool inner_unique);
217 static HashJoin *make_hashjoin(List *tlist,
218 List *joinclauses, List *otherclauses,
220 Plan *lefttree, Plan *righttree,
221 JoinType jointype, bool inner_unique);
222 static Hash *make_hash(Plan *lefttree,
224 AttrNumber skewColumn,
226 static MergeJoin *make_mergejoin(List *tlist,
227 List *joinclauses, List *otherclauses,
230 Oid *mergecollations,
231 int *mergestrategies,
232 bool *mergenullsfirst,
233 Plan *lefttree, Plan *righttree,
234 JoinType jointype, bool inner_unique,
235 bool skip_mark_restore);
236 static Sort *make_sort(Plan *lefttree, int numCols,
237 AttrNumber *sortColIdx, Oid *sortOperators,
238 Oid *collations, bool *nullsFirst);
239 static Plan *prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
241 const AttrNumber *reqColIdx,
242 bool adjust_tlist_in_place,
244 AttrNumber **p_sortColIdx,
245 Oid **p_sortOperators,
247 bool **p_nullsFirst);
248 static EquivalenceMember *find_ec_member_for_tle(EquivalenceClass *ec,
251 static Sort *make_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
253 static Sort *make_sort_from_groupcols(List *groupcls,
254 AttrNumber *grpColIdx,
256 static Material *make_material(Plan *lefttree);
257 static WindowAgg *make_windowagg(List *tlist, Index winref,
258 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
259 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
260 int frameOptions, Node *startOffset, Node *endOffset,
261 Oid startInRangeFunc, Oid endInRangeFunc,
262 Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst,
264 static Group *make_group(List *tlist, List *qual, int numGroupCols,
265 AttrNumber *grpColIdx, Oid *grpOperators,
267 static Unique *make_unique_from_sortclauses(Plan *lefttree, List *distinctList);
268 static Unique *make_unique_from_pathkeys(Plan *lefttree,
269 List *pathkeys, int numCols);
270 static Gather *make_gather(List *qptlist, List *qpqual,
271 int nworkers, int rescan_param, bool single_copy, Plan *subplan);
272 static SetOp *make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
273 List *distinctList, AttrNumber flagColIdx, int firstFlag,
275 static LockRows *make_lockrows(Plan *lefttree, List *rowMarks, int epqParam);
276 static Result *make_result(List *tlist, Node *resconstantqual, Plan *subplan);
277 static ProjectSet *make_project_set(List *tlist, Plan *subplan);
278 static ModifyTable *make_modifytable(PlannerInfo *root,
279 CmdType operation, bool canSetTag,
280 Index nominalRelation, Index rootRelation,
281 bool partColsUpdated,
282 List *resultRelations, List *subplans, List *subroots,
283 List *withCheckOptionLists, List *returningLists,
284 List *rowMarks, OnConflictExpr *onconflict, int epqParam);
285 static GatherMerge *create_gather_merge_plan(PlannerInfo *root,
286 GatherMergePath *best_path);
291 * Creates the access plan for a query by recursively processing the
292 * desired tree of pathnodes, starting at the node 'best_path'. For
293 * every pathnode found, we create a corresponding plan node containing
294 * appropriate id, target list, and qualification information.
296 * The tlists and quals in the plan tree are still in planner format,
297 * ie, Vars still correspond to the parser's numbering. This will be
298 * fixed later by setrefs.c.
300 * best_path is the best access path
302 * Returns a Plan tree.
305 create_plan(PlannerInfo *root, Path *best_path)
309 /* plan_params should not be in use in current query level */
310 Assert(root->plan_params == NIL);
312 /* Initialize this module's workspace in PlannerInfo */
313 root->curOuterRels = NULL;
314 root->curOuterParams = NIL;
316 /* Recursively process the path tree, demanding the correct tlist result */
317 plan = create_plan_recurse(root, best_path, CP_EXACT_TLIST);
320 * Make sure the topmost plan node's targetlist exposes the original
321 * column names and other decorative info. Targetlists generated within
322 * the planner don't bother with that stuff, but we must have it on the
323 * top-level tlist seen at execution time. However, ModifyTable plan
324 * nodes don't have a tlist matching the querytree targetlist.
326 if (!IsA(plan, ModifyTable))
327 apply_tlist_labeling(plan->targetlist, root->processed_tlist);
330 * Attach any initPlans created in this query level to the topmost plan
331 * node. (In principle the initplans could go in any plan node at or
332 * above where they're referenced, but there seems no reason to put them
333 * any lower than the topmost node for the query level. Also, see
334 * comments for SS_finalize_plan before you try to change this.)
336 SS_attach_initplans(root, plan);
338 /* Check we successfully assigned all NestLoopParams to plan nodes */
339 if (root->curOuterParams != NIL)
340 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
343 * Reset plan_params to ensure param IDs used for nestloop params are not
346 root->plan_params = NIL;
352 * create_plan_recurse
353 * Recursive guts of create_plan().
356 create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
360 /* Guard against stack overflow due to overly complex plans */
363 switch (best_path->pathtype)
368 case T_IndexOnlyScan:
369 case T_BitmapHeapScan:
373 case T_TableFuncScan:
376 case T_WorkTableScan:
377 case T_NamedTuplestoreScan:
380 plan = create_scan_plan(root, best_path, flags);
385 plan = create_join_plan(root,
386 (JoinPath *) best_path);
389 plan = create_append_plan(root,
390 (AppendPath *) best_path);
393 plan = create_merge_append_plan(root,
394 (MergeAppendPath *) best_path);
397 if (IsA(best_path, ProjectionPath))
399 plan = create_projection_plan(root,
400 (ProjectionPath *) best_path,
403 else if (IsA(best_path, MinMaxAggPath))
405 plan = (Plan *) create_minmaxagg_plan(root,
406 (MinMaxAggPath *) best_path);
408 else if (IsA(best_path, GroupResultPath))
410 plan = (Plan *) create_group_result_plan(root,
411 (GroupResultPath *) best_path);
415 /* Simple RTE_RESULT base relation */
416 Assert(IsA(best_path, Path));
417 plan = create_scan_plan(root, best_path, flags);
421 plan = (Plan *) create_project_set_plan(root,
422 (ProjectSetPath *) best_path);
425 plan = (Plan *) create_material_plan(root,
426 (MaterialPath *) best_path,
430 if (IsA(best_path, UpperUniquePath))
432 plan = (Plan *) create_upper_unique_plan(root,
433 (UpperUniquePath *) best_path,
438 Assert(IsA(best_path, UniquePath));
439 plan = create_unique_plan(root,
440 (UniquePath *) best_path,
445 plan = (Plan *) create_gather_plan(root,
446 (GatherPath *) best_path);
449 plan = (Plan *) create_sort_plan(root,
450 (SortPath *) best_path,
454 plan = (Plan *) create_group_plan(root,
455 (GroupPath *) best_path);
458 if (IsA(best_path, GroupingSetsPath))
459 plan = create_groupingsets_plan(root,
460 (GroupingSetsPath *) best_path);
463 Assert(IsA(best_path, AggPath));
464 plan = (Plan *) create_agg_plan(root,
465 (AggPath *) best_path);
469 plan = (Plan *) create_windowagg_plan(root,
470 (WindowAggPath *) best_path);
473 plan = (Plan *) create_setop_plan(root,
474 (SetOpPath *) best_path,
477 case T_RecursiveUnion:
478 plan = (Plan *) create_recursiveunion_plan(root,
479 (RecursiveUnionPath *) best_path);
482 plan = (Plan *) create_lockrows_plan(root,
483 (LockRowsPath *) best_path,
487 plan = (Plan *) create_modifytable_plan(root,
488 (ModifyTablePath *) best_path);
491 plan = (Plan *) create_limit_plan(root,
492 (LimitPath *) best_path,
496 plan = (Plan *) create_gather_merge_plan(root,
497 (GatherMergePath *) best_path);
500 elog(ERROR, "unrecognized node type: %d",
501 (int) best_path->pathtype);
502 plan = NULL; /* keep compiler quiet */
511 * Create a scan plan for the parent relation of 'best_path'.
514 create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
516 RelOptInfo *rel = best_path->parent;
518 List *gating_clauses;
523 * Extract the relevant restriction clauses from the parent relation. The
524 * executor must apply all these restrictions during the scan, except for
525 * pseudoconstants which we'll take care of below.
527 * If this is a plain indexscan or index-only scan, we need not consider
528 * restriction clauses that are implied by the index's predicate, so use
529 * indrestrictinfo not baserestrictinfo. Note that we can't do that for
530 * bitmap indexscans, since there's not necessarily a single index
531 * involved; but it doesn't matter since create_bitmap_scan_plan() will be
532 * able to get rid of such clauses anyway via predicate proof.
534 switch (best_path->pathtype)
537 case T_IndexOnlyScan:
538 scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
541 scan_clauses = rel->baserestrictinfo;
546 * If this is a parameterized scan, we also need to enforce all the join
547 * clauses available from the outer relation(s).
549 * For paranoia's sake, don't modify the stored baserestrictinfo list.
551 if (best_path->param_info)
552 scan_clauses = list_concat(list_copy(scan_clauses),
553 best_path->param_info->ppi_clauses);
556 * Detect whether we have any pseudoconstant quals to deal with. Then, if
557 * we'll need a gating Result node, it will be able to project, so there
558 * are no requirements on the child's tlist.
560 gating_clauses = get_gating_quals(root, scan_clauses);
565 * For table scans, rather than using the relation targetlist (which is
566 * only those Vars actually needed by the query), we prefer to generate a
567 * tlist containing all Vars in order. This will allow the executor to
568 * optimize away projection of the table tuples, if possible.
570 * But if the caller is going to ignore our tlist anyway, then don't
571 * bother generating one at all. We use an exact equality test here, so
572 * that this only applies when CP_IGNORE_TLIST is the only flag set.
574 if (flags == CP_IGNORE_TLIST)
578 else if (use_physical_tlist(root, best_path, flags))
580 if (best_path->pathtype == T_IndexOnlyScan)
582 /* For index-only scan, the preferred tlist is the index's */
583 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
586 * Transfer sortgroupref data to the replacement tlist, if
587 * requested (use_physical_tlist checked that this will work).
589 if (flags & CP_LABEL_TLIST)
590 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
594 tlist = build_physical_tlist(root, rel);
597 /* Failed because of dropped cols, so use regular method */
598 tlist = build_path_tlist(root, best_path);
602 /* As above, transfer sortgroupref data to replacement tlist */
603 if (flags & CP_LABEL_TLIST)
604 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
610 tlist = build_path_tlist(root, best_path);
613 switch (best_path->pathtype)
616 plan = (Plan *) create_seqscan_plan(root,
623 plan = (Plan *) create_samplescan_plan(root,
630 plan = (Plan *) create_indexscan_plan(root,
631 (IndexPath *) best_path,
637 case T_IndexOnlyScan:
638 plan = (Plan *) create_indexscan_plan(root,
639 (IndexPath *) best_path,
645 case T_BitmapHeapScan:
646 plan = (Plan *) create_bitmap_scan_plan(root,
647 (BitmapHeapPath *) best_path,
653 plan = (Plan *) create_tidscan_plan(root,
654 (TidPath *) best_path,
660 plan = (Plan *) create_subqueryscan_plan(root,
661 (SubqueryScanPath *) best_path,
667 plan = (Plan *) create_functionscan_plan(root,
673 case T_TableFuncScan:
674 plan = (Plan *) create_tablefuncscan_plan(root,
681 plan = (Plan *) create_valuesscan_plan(root,
688 plan = (Plan *) create_ctescan_plan(root,
694 case T_NamedTuplestoreScan:
695 plan = (Plan *) create_namedtuplestorescan_plan(root,
702 plan = (Plan *) create_resultscan_plan(root,
708 case T_WorkTableScan:
709 plan = (Plan *) create_worktablescan_plan(root,
716 plan = (Plan *) create_foreignscan_plan(root,
717 (ForeignPath *) best_path,
723 plan = (Plan *) create_customscan_plan(root,
724 (CustomPath *) best_path,
730 elog(ERROR, "unrecognized node type: %d",
731 (int) best_path->pathtype);
732 plan = NULL; /* keep compiler quiet */
737 * If there are any pseudoconstant clauses attached to this node, insert a
738 * gating Result node that evaluates the pseudoconstants as one-time
742 plan = create_gating_plan(root, best_path, plan, gating_clauses);
748 * Build a target list (ie, a list of TargetEntry) for the Path's output.
750 * This is almost just make_tlist_from_pathtarget(), but we also have to
751 * deal with replacing nestloop params.
754 build_path_tlist(PlannerInfo *root, Path *path)
757 Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
761 foreach(v, path->pathtarget->exprs)
763 Node *node = (Node *) lfirst(v);
767 * If it's a parameterized path, there might be lateral references in
768 * the tlist, which need to be replaced with Params. There's no need
769 * to remake the TargetEntry nodes, so apply this to each list item
772 if (path->param_info)
773 node = replace_nestloop_params(root, node);
775 tle = makeTargetEntry((Expr *) node,
780 tle->ressortgroupref = sortgrouprefs[resno - 1];
782 tlist = lappend(tlist, tle);
790 * Decide whether to use a tlist matching relation structure,
791 * rather than only those Vars actually referenced.
794 use_physical_tlist(PlannerInfo *root, Path *path, int flags)
796 RelOptInfo *rel = path->parent;
801 * Forget it if either exact tlist or small tlist is demanded.
803 if (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST))
807 * We can do this for real relation scans, subquery scans, function scans,
808 * tablefunc scans, values scans, and CTE scans (but not for, eg, joins).
810 if (rel->rtekind != RTE_RELATION &&
811 rel->rtekind != RTE_SUBQUERY &&
812 rel->rtekind != RTE_FUNCTION &&
813 rel->rtekind != RTE_TABLEFUNC &&
814 rel->rtekind != RTE_VALUES &&
815 rel->rtekind != RTE_CTE)
819 * Can't do it with inheritance cases either (mainly because Append
820 * doesn't project; this test may be unnecessary now that
821 * create_append_plan instructs its children to return an exact tlist).
823 if (rel->reloptkind != RELOPT_BASEREL)
827 * Also, don't do it to a CustomPath; the premise that we're extracting
828 * columns from a simple physical tuple is unlikely to hold for those.
829 * (When it does make sense, the custom path creator can set up the path's
830 * pathtarget that way.)
832 if (IsA(path, CustomPath))
836 * If a bitmap scan's tlist is empty, keep it as-is. This may allow the
837 * executor to skip heap page fetches, and in any case, the benefit of
838 * using a physical tlist instead would be minimal.
840 if (IsA(path, BitmapHeapPath) &&
841 path->pathtarget->exprs == NIL)
845 * Can't do it if any system columns or whole-row Vars are requested.
846 * (This could possibly be fixed but would take some fragile assumptions
847 * in setrefs.c, I think.)
849 for (i = rel->min_attr; i <= 0; i++)
851 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
856 * Can't do it if the rel is required to emit any placeholder expressions,
859 foreach(lc, root->placeholder_list)
861 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
863 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
864 bms_is_subset(phinfo->ph_eval_at, rel->relids))
869 * Also, can't do it if CP_LABEL_TLIST is specified and path is requested
870 * to emit any sort/group columns that are not simple Vars. (If they are
871 * simple Vars, they should appear in the physical tlist, and
872 * apply_pathtarget_labeling_to_tlist will take care of getting them
873 * labeled again.) We also have to check that no two sort/group columns
874 * are the same Var, else that element of the physical tlist would need
875 * conflicting ressortgroupref labels.
877 if ((flags & CP_LABEL_TLIST) && path->pathtarget->sortgrouprefs)
879 Bitmapset *sortgroupatts = NULL;
882 foreach(lc, path->pathtarget->exprs)
884 Expr *expr = (Expr *) lfirst(lc);
886 if (path->pathtarget->sortgrouprefs[i])
888 if (expr && IsA(expr, Var))
890 int attno = ((Var *) expr)->varattno;
892 attno -= FirstLowInvalidHeapAttributeNumber;
893 if (bms_is_member(attno, sortgroupatts))
895 sortgroupatts = bms_add_member(sortgroupatts, attno);
909 * See if there are pseudoconstant quals in a node's quals list
911 * If the node's quals list includes any pseudoconstant quals,
912 * return just those quals.
915 get_gating_quals(PlannerInfo *root, List *quals)
917 /* No need to look if we know there are no pseudoconstants */
918 if (!root->hasPseudoConstantQuals)
921 /* Sort into desirable execution order while still in RestrictInfo form */
922 quals = order_qual_clauses(root, quals);
924 /* Pull out any pseudoconstant quals from the RestrictInfo list */
925 return extract_actual_clauses(quals, true);
930 * Deal with pseudoconstant qual clauses
932 * Add a gating Result node atop the already-built plan.
935 create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
941 Assert(gating_quals);
944 * We might have a trivial Result plan already. Stacking one Result atop
945 * another is silly, so if that applies, just discard the input plan.
946 * (We're assuming its targetlist is uninteresting; it should be either
947 * the same as the result of build_path_tlist, or a simplified version.)
950 if (IsA(plan, Result))
952 Result *rplan = (Result *) plan;
954 if (rplan->plan.lefttree == NULL &&
955 rplan->resconstantqual == NULL)
960 * Since we need a Result node anyway, always return the path's requested
961 * tlist; that's never a wrong choice, even if the parent node didn't ask
962 * for CP_EXACT_TLIST.
964 gplan = (Plan *) make_result(build_path_tlist(root, path),
965 (Node *) gating_quals,
969 * Notice that we don't change cost or size estimates when doing gating.
970 * The costs of qual eval were already included in the subplan's cost.
971 * Leaving the size alone amounts to assuming that the gating qual will
972 * succeed, which is the conservative estimate for planning upper queries.
973 * We certainly don't want to assume the output size is zero (unless the
974 * gating qual is actually constant FALSE, and that case is dealt with in
975 * clausesel.c). Interpolating between the two cases is silly, because it
976 * doesn't reflect what will really happen at runtime, and besides which
977 * in most cases we have only a very bad idea of the probability of the
978 * gating qual being true.
980 copy_plan_costsize(gplan, plan);
982 /* Gating quals could be unsafe, so better use the Path's safety flag */
983 gplan->parallel_safe = path->parallel_safe;
990 * Create a join plan for 'best_path' and (recursively) plans for its
991 * inner and outer paths.
994 create_join_plan(PlannerInfo *root, JoinPath *best_path)
997 List *gating_clauses;
999 switch (best_path->path.pathtype)
1002 plan = (Plan *) create_mergejoin_plan(root,
1003 (MergePath *) best_path);
1006 plan = (Plan *) create_hashjoin_plan(root,
1007 (HashPath *) best_path);
1010 plan = (Plan *) create_nestloop_plan(root,
1011 (NestPath *) best_path);
1014 elog(ERROR, "unrecognized node type: %d",
1015 (int) best_path->path.pathtype);
1016 plan = NULL; /* keep compiler quiet */
1021 * If there are any pseudoconstant clauses attached to this node, insert a
1022 * gating Result node that evaluates the pseudoconstants as one-time
1025 gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1027 plan = create_gating_plan(root, (Path *) best_path, plan,
1033 * * Expensive function pullups may have pulled local predicates * into
1034 * this path node. Put them in the qpqual of the plan node. * JMH,
1037 if (get_loc_restrictinfo(best_path) != NIL)
1038 set_qpqual((Plan) plan,
1039 list_concat(get_qpqual((Plan) plan),
1040 get_actual_clauses(get_loc_restrictinfo(best_path))));
1047 * create_append_plan
1048 * Create an Append plan for 'best_path' and (recursively) plans
1051 * Returns a Plan node.
1054 create_append_plan(PlannerInfo *root, AppendPath *best_path)
1057 List *tlist = build_path_tlist(root, &best_path->path);
1058 List *subplans = NIL;
1060 RelOptInfo *rel = best_path->path.parent;
1061 PartitionPruneInfo *partpruneinfo = NULL;
1064 * The subpaths list could be empty, if every child was proven empty by
1065 * constraint exclusion. In that case generate a dummy plan that returns
1068 * Note that an AppendPath with no members is also generated in certain
1069 * cases where there was no appending construct at all, but we know the
1070 * relation is empty (see set_dummy_rel_pathlist).
1072 if (best_path->subpaths == NIL)
1074 /* Generate a Result plan with constant-FALSE gating qual */
1077 plan = (Plan *) make_result(tlist,
1078 (Node *) list_make1(makeBoolConst(false,
1082 copy_generic_path_info(plan, (Path *) best_path);
1087 /* Build the plan for each child */
1088 foreach(subpaths, best_path->subpaths)
1090 Path *subpath = (Path *) lfirst(subpaths);
1093 /* Must insist that all children return the same tlist */
1094 subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1096 subplans = lappend(subplans, subplan);
1100 * If any quals exist, they may be useful to perform further partition
1101 * pruning during execution. Gather information needed by the executor to
1102 * do partition pruning.
1104 if (enable_partition_pruning &&
1105 rel->reloptkind == RELOPT_BASEREL &&
1106 best_path->partitioned_rels != NIL)
1110 prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1112 if (best_path->path.param_info)
1114 List *prmquals = best_path->path.param_info->ppi_clauses;
1116 prmquals = extract_actual_clauses(prmquals, false);
1117 prmquals = (List *) replace_nestloop_params(root,
1120 prunequal = list_concat(prunequal, prmquals);
1123 if (prunequal != NIL)
1125 make_partition_pruneinfo(root, rel,
1126 best_path->subpaths,
1127 best_path->partitioned_rels,
1132 * XXX ideally, if there's just one child, we'd not bother to generate an
1133 * Append node but just return the single child. At the moment this does
1134 * not work because the varno of the child scan plan won't match the
1135 * parent-rel Vars it'll be asked to emit.
1138 plan = make_append(subplans, best_path->first_partial_path,
1139 tlist, partpruneinfo);
1141 copy_generic_path_info(&plan->plan, (Path *) best_path);
1143 return (Plan *) plan;
1147 * create_merge_append_plan
1148 * Create a MergeAppend plan for 'best_path' and (recursively) plans
1151 * Returns a Plan node.
1154 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
1156 MergeAppend *node = makeNode(MergeAppend);
1157 Plan *plan = &node->plan;
1158 List *tlist = build_path_tlist(root, &best_path->path);
1159 List *pathkeys = best_path->path.pathkeys;
1160 List *subplans = NIL;
1162 RelOptInfo *rel = best_path->path.parent;
1163 PartitionPruneInfo *partpruneinfo = NULL;
1166 * We don't have the actual creation of the MergeAppend node split out
1167 * into a separate make_xxx function. This is because we want to run
1168 * prepare_sort_from_pathkeys on it before we do so on the individual
1169 * child plans, to make cross-checking the sort info easier.
1171 copy_generic_path_info(plan, (Path *) best_path);
1172 plan->targetlist = tlist;
1174 plan->lefttree = NULL;
1175 plan->righttree = NULL;
1177 /* Compute sort column info, and adjust MergeAppend's tlist as needed */
1178 (void) prepare_sort_from_pathkeys(plan, pathkeys,
1179 best_path->path.parent->relids,
1184 &node->sortOperators,
1189 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1190 * even to subplans that don't need an explicit sort, to make sure they
1191 * are returning the same sort key columns the MergeAppend expects.
1193 foreach(subpaths, best_path->subpaths)
1195 Path *subpath = (Path *) lfirst(subpaths);
1198 AttrNumber *sortColIdx;
1203 /* Build the child plan */
1204 /* Must insist that all children return the same tlist */
1205 subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1207 /* Compute sort column info, and adjust subplan's tlist as needed */
1208 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1209 subpath->parent->relids,
1219 * Check that we got the same sort key information. We just Assert
1220 * that the sortops match, since those depend only on the pathkeys;
1221 * but it seems like a good idea to check the sort column numbers
1222 * explicitly, to ensure the tlists really do match up.
1224 Assert(numsortkeys == node->numCols);
1225 if (memcmp(sortColIdx, node->sortColIdx,
1226 numsortkeys * sizeof(AttrNumber)) != 0)
1227 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1228 Assert(memcmp(sortOperators, node->sortOperators,
1229 numsortkeys * sizeof(Oid)) == 0);
1230 Assert(memcmp(collations, node->collations,
1231 numsortkeys * sizeof(Oid)) == 0);
1232 Assert(memcmp(nullsFirst, node->nullsFirst,
1233 numsortkeys * sizeof(bool)) == 0);
1235 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1236 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1238 Sort *sort = make_sort(subplan, numsortkeys,
1239 sortColIdx, sortOperators,
1240 collations, nullsFirst);
1242 label_sort_with_costsize(root, sort, best_path->limit_tuples);
1243 subplan = (Plan *) sort;
1246 subplans = lappend(subplans, subplan);
1250 * If any quals exist, they may be useful to perform further partition
1251 * pruning during execution. Gather information needed by the executor to
1252 * do partition pruning.
1254 if (enable_partition_pruning &&
1255 rel->reloptkind == RELOPT_BASEREL &&
1256 best_path->partitioned_rels != NIL)
1260 prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1262 if (best_path->path.param_info)
1265 List *prmquals = best_path->path.param_info->ppi_clauses;
1267 prmquals = extract_actual_clauses(prmquals, false);
1268 prmquals = (List *) replace_nestloop_params(root,
1271 prunequal = list_concat(prunequal, prmquals);
1274 if (prunequal != NIL)
1275 partpruneinfo = make_partition_pruneinfo(root, rel,
1276 best_path->subpaths,
1277 best_path->partitioned_rels,
1281 node->mergeplans = subplans;
1282 node->part_prune_info = partpruneinfo;
1284 return (Plan *) node;
1288 * create_group_result_plan
1289 * Create a Result plan for 'best_path'.
1290 * This is only used for degenerate grouping cases.
1292 * Returns a Plan node.
1295 create_group_result_plan(PlannerInfo *root, GroupResultPath *best_path)
1301 tlist = build_path_tlist(root, &best_path->path);
1303 /* best_path->quals is just bare clauses */
1304 quals = order_qual_clauses(root, best_path->quals);
1306 plan = make_result(tlist, (Node *) quals, NULL);
1308 copy_generic_path_info(&plan->plan, (Path *) best_path);
1314 * create_project_set_plan
1315 * Create a ProjectSet plan for 'best_path'.
1317 * Returns a Plan node.
1320 create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
1326 /* Since we intend to project, we don't need to constrain child tlist */
1327 subplan = create_plan_recurse(root, best_path->subpath, 0);
1329 tlist = build_path_tlist(root, &best_path->path);
1331 plan = make_project_set(tlist, subplan);
1333 copy_generic_path_info(&plan->plan, (Path *) best_path);
1339 * create_material_plan
1340 * Create a Material plan for 'best_path' and (recursively) plans
1343 * Returns a Plan node.
1346 create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
1352 * We don't want any excess columns in the materialized tuples, so request
1353 * a smaller tlist. Otherwise, since Material doesn't project, tlist
1354 * requirements pass through.
1356 subplan = create_plan_recurse(root, best_path->subpath,
1357 flags | CP_SMALL_TLIST);
1359 plan = make_material(subplan);
1361 copy_generic_path_info(&plan->plan, (Path *) best_path);
1367 * create_unique_plan
1368 * Create a Unique plan for 'best_path' and (recursively) plans
1371 * Returns a Plan node.
1374 create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
1384 AttrNumber *groupColIdx;
1388 /* Unique doesn't project, so tlist requirements pass through */
1389 subplan = create_plan_recurse(root, best_path->subpath, flags);
1391 /* Done if we don't need to do any actual unique-ifying */
1392 if (best_path->umethod == UNIQUE_PATH_NOOP)
1396 * As constructed, the subplan has a "flat" tlist containing just the Vars
1397 * needed here and at upper levels. The values we are supposed to
1398 * unique-ify may be expressions in these variables. We have to add any
1399 * such expressions to the subplan's tlist.
1401 * The subplan may have a "physical" tlist if it is a simple scan plan. If
1402 * we're going to sort, this should be reduced to the regular tlist, so
1403 * that we don't sort more data than we need to. For hashing, the tlist
1404 * should be left as-is if we don't need to add any expressions; but if we
1405 * do have to add expressions, then a projection step will be needed at
1406 * runtime anyway, so we may as well remove unneeded items. Therefore
1407 * newtlist starts from build_path_tlist() not just a copy of the
1408 * subplan's tlist; and we don't install it into the subplan unless we are
1409 * sorting or stuff has to be added.
1411 in_operators = best_path->in_operators;
1412 uniq_exprs = best_path->uniq_exprs;
1414 /* initialize modified subplan tlist as just the "required" vars */
1415 newtlist = build_path_tlist(root, &best_path->path);
1416 nextresno = list_length(newtlist) + 1;
1419 foreach(l, uniq_exprs)
1421 Expr *uniqexpr = lfirst(l);
1424 tle = tlist_member(uniqexpr, newtlist);
1427 tle = makeTargetEntry((Expr *) uniqexpr,
1431 newtlist = lappend(newtlist, tle);
1437 /* Use change_plan_targetlist in case we need to insert a Result node */
1438 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
1439 subplan = change_plan_targetlist(subplan, newtlist,
1440 best_path->path.parallel_safe);
1443 * Build control information showing which subplan output columns are to
1444 * be examined by the grouping step. Unfortunately we can't merge this
1445 * with the previous loop, since we didn't then know which version of the
1446 * subplan tlist we'd end up using.
1448 newtlist = subplan->targetlist;
1449 numGroupCols = list_length(uniq_exprs);
1450 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
1453 foreach(l, uniq_exprs)
1455 Expr *uniqexpr = lfirst(l);
1458 tle = tlist_member(uniqexpr, newtlist);
1459 if (!tle) /* shouldn't happen */
1460 elog(ERROR, "failed to find unique expression in subplan tlist");
1461 groupColIdx[groupColPos++] = tle->resno;
1464 if (best_path->umethod == UNIQUE_PATH_HASH)
1466 Oid *groupOperators;
1469 * Get the hashable equality operators for the Agg node to use.
1470 * Normally these are the same as the IN clause operators, but if
1471 * those are cross-type operators then the equality operators are the
1472 * ones for the IN clause operators' RHS datatype.
1474 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
1476 foreach(l, in_operators)
1478 Oid in_oper = lfirst_oid(l);
1481 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
1482 elog(ERROR, "could not find compatible hash operator for operator %u",
1484 groupOperators[groupColPos++] = eq_oper;
1488 * Since the Agg node is going to project anyway, we can give it the
1489 * minimum output tlist, without any stuff we might have added to the
1492 plan = (Plan *) make_agg(build_path_tlist(root, &best_path->path),
1501 best_path->path.rows,
1506 List *sortList = NIL;
1509 /* Create an ORDER BY list to sort the input compatibly */
1511 foreach(l, in_operators)
1513 Oid in_oper = lfirst_oid(l);
1517 SortGroupClause *sortcl;
1519 sortop = get_ordering_op_for_equality_op(in_oper, false);
1520 if (!OidIsValid(sortop)) /* shouldn't happen */
1521 elog(ERROR, "could not find ordering operator for equality operator %u",
1525 * The Unique node will need equality operators. Normally these
1526 * are the same as the IN clause operators, but if those are
1527 * cross-type operators then the equality operators are the ones
1528 * for the IN clause operators' RHS datatype.
1530 eqop = get_equality_op_for_ordering_op(sortop, NULL);
1531 if (!OidIsValid(eqop)) /* shouldn't happen */
1532 elog(ERROR, "could not find equality operator for ordering operator %u",
1535 tle = get_tle_by_resno(subplan->targetlist,
1536 groupColIdx[groupColPos]);
1537 Assert(tle != NULL);
1539 sortcl = makeNode(SortGroupClause);
1540 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
1541 subplan->targetlist);
1542 sortcl->eqop = eqop;
1543 sortcl->sortop = sortop;
1544 sortcl->nulls_first = false;
1545 sortcl->hashable = false; /* no need to make this accurate */
1546 sortList = lappend(sortList, sortcl);
1549 sort = make_sort_from_sortclauses(sortList, subplan);
1550 label_sort_with_costsize(root, sort, -1.0);
1551 plan = (Plan *) make_unique_from_sortclauses((Plan *) sort, sortList);
1554 /* Copy cost data from Path to Plan */
1555 copy_generic_path_info(plan, &best_path->path);
1561 * create_gather_plan
1563 * Create a Gather plan for 'best_path' and (recursively) plans
1567 create_gather_plan(PlannerInfo *root, GatherPath *best_path)
1569 Gather *gather_plan;
1574 * Although the Gather node can project, we prefer to push down such work
1575 * to its child node, so demand an exact tlist from the child.
1577 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1579 tlist = build_path_tlist(root, &best_path->path);
1581 gather_plan = make_gather(tlist,
1583 best_path->num_workers,
1584 assign_special_exec_param(root),
1585 best_path->single_copy,
1588 copy_generic_path_info(&gather_plan->plan, &best_path->path);
1590 /* use parallel mode for parallel plans. */
1591 root->glob->parallelModeNeeded = true;
1597 * create_gather_merge_plan
1599 * Create a Gather Merge plan for 'best_path' and (recursively)
1600 * plans for its subpaths.
1602 static GatherMerge *
1603 create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
1605 GatherMerge *gm_plan;
1607 List *pathkeys = best_path->path.pathkeys;
1608 List *tlist = build_path_tlist(root, &best_path->path);
1610 /* As with Gather, it's best to project away columns in the workers. */
1611 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1613 /* Create a shell for a GatherMerge plan. */
1614 gm_plan = makeNode(GatherMerge);
1615 gm_plan->plan.targetlist = tlist;
1616 gm_plan->num_workers = best_path->num_workers;
1617 copy_generic_path_info(&gm_plan->plan, &best_path->path);
1619 /* Assign the rescan Param. */
1620 gm_plan->rescan_param = assign_special_exec_param(root);
1622 /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1623 Assert(pathkeys != NIL);
1625 /* Compute sort column info, and adjust subplan's tlist as needed */
1626 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1627 best_path->subpath->parent->relids,
1628 gm_plan->sortColIdx,
1631 &gm_plan->sortColIdx,
1632 &gm_plan->sortOperators,
1633 &gm_plan->collations,
1634 &gm_plan->nullsFirst);
1637 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1638 if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1639 subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1640 gm_plan->sortColIdx,
1641 gm_plan->sortOperators,
1642 gm_plan->collations,
1643 gm_plan->nullsFirst);
1645 /* Now insert the subplan under GatherMerge. */
1646 gm_plan->plan.lefttree = subplan;
1648 /* use parallel mode for parallel plans. */
1649 root->glob->parallelModeNeeded = true;
1655 * create_projection_plan
1657 * Create a plan tree to do a projection step and (recursively) plans
1658 * for its subpaths. We may need a Result node for the projection,
1659 * but sometimes we can just let the subplan do the work.
1662 create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
1667 bool needs_result_node = false;
1670 * Convert our subpath to a Plan and determine whether we need a Result
1673 * In most cases where we don't need to project, creation_projection_path
1674 * will have set dummypp, but not always. First, some createplan.c
1675 * routines change the tlists of their nodes. (An example is that
1676 * create_merge_append_plan might add resjunk sort columns to a
1677 * MergeAppend.) Second, create_projection_path has no way of knowing
1678 * what path node will be placed on top of the projection path and
1679 * therefore can't predict whether it will require an exact tlist. For
1680 * both of these reasons, we have to recheck here.
1682 if (use_physical_tlist(root, &best_path->path, flags))
1685 * Our caller doesn't really care what tlist we return, so we don't
1686 * actually need to project. However, we may still need to ensure
1687 * proper sortgroupref labels, if the caller cares about those.
1689 subplan = create_plan_recurse(root, best_path->subpath, 0);
1690 tlist = subplan->targetlist;
1691 if (flags & CP_LABEL_TLIST)
1692 apply_pathtarget_labeling_to_tlist(tlist,
1693 best_path->path.pathtarget);
1695 else if (is_projection_capable_path(best_path->subpath))
1698 * Our caller requires that we return the exact tlist, but no separate
1699 * result node is needed because the subpath is projection-capable.
1700 * Tell create_plan_recurse that we're going to ignore the tlist it
1703 subplan = create_plan_recurse(root, best_path->subpath,
1705 tlist = build_path_tlist(root, &best_path->path);
1710 * It looks like we need a result node, unless by good fortune the
1711 * requested tlist is exactly the one the child wants to produce.
1713 subplan = create_plan_recurse(root, best_path->subpath, 0);
1714 tlist = build_path_tlist(root, &best_path->path);
1715 needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
1719 * If we make a different decision about whether to include a Result node
1720 * than create_projection_path did, we'll have made slightly wrong cost
1721 * estimates; but label the plan with the cost estimates we actually used,
1722 * not "corrected" ones. (XXX this could be cleaned up if we moved more
1723 * of the sortcolumn setup logic into Path creation, but that would add
1724 * expense to creating Paths we might end up not using.)
1726 if (!needs_result_node)
1728 /* Don't need a separate Result, just assign tlist to subplan */
1730 plan->targetlist = tlist;
1732 /* Label plan with the estimated costs we actually used */
1733 plan->startup_cost = best_path->path.startup_cost;
1734 plan->total_cost = best_path->path.total_cost;
1735 plan->plan_rows = best_path->path.rows;
1736 plan->plan_width = best_path->path.pathtarget->width;
1737 plan->parallel_safe = best_path->path.parallel_safe;
1738 /* ... but don't change subplan's parallel_aware flag */
1742 /* We need a Result node */
1743 plan = (Plan *) make_result(tlist, NULL, subplan);
1745 copy_generic_path_info(plan, (Path *) best_path);
1752 * inject_projection_plan
1753 * Insert a Result node to do a projection step.
1755 * This is used in a few places where we decide on-the-fly that we need a
1756 * projection step as part of the tree generated for some Path node.
1757 * We should try to get rid of this in favor of doing it more honestly.
1759 * One reason it's ugly is we have to be told the right parallel_safe marking
1760 * to apply (since the tlist might be unsafe even if the child plan is safe).
1763 inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
1767 plan = (Plan *) make_result(tlist, NULL, subplan);
1770 * In principle, we should charge tlist eval cost plus cpu_per_tuple per
1771 * row for the Result node. But the former has probably been factored in
1772 * already and the latter was not accounted for during Path construction,
1773 * so being formally correct might just make the EXPLAIN output look less
1774 * consistent not more so. Hence, just copy the subplan's cost.
1776 copy_plan_costsize(plan, subplan);
1777 plan->parallel_safe = parallel_safe;
1783 * change_plan_targetlist
1784 * Externally available wrapper for inject_projection_plan.
1786 * This is meant for use by FDW plan-generation functions, which might
1787 * want to adjust the tlist computed by some subplan tree. In general,
1788 * a Result node is needed to compute the new tlist, but we can optimize
1791 * In most cases, tlist_parallel_safe can just be passed as the parallel_safe
1792 * flag of the FDW's own Path node.
1795 change_plan_targetlist(Plan *subplan, List *tlist, bool tlist_parallel_safe)
1798 * If the top plan node can't do projections and its existing target list
1799 * isn't already what we need, we need to add a Result node to help it
1802 if (!is_projection_capable_plan(subplan) &&
1803 !tlist_same_exprs(tlist, subplan->targetlist))
1804 subplan = inject_projection_plan(subplan, tlist,
1805 subplan->parallel_safe &&
1806 tlist_parallel_safe);
1809 /* Else we can just replace the plan node's tlist */
1810 subplan->targetlist = tlist;
1811 subplan->parallel_safe &= tlist_parallel_safe;
1819 * Create a Sort plan for 'best_path' and (recursively) plans
1823 create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
1829 * We don't want any excess columns in the sorted tuples, so request a
1830 * smaller tlist. Otherwise, since Sort doesn't project, tlist
1831 * requirements pass through.
1833 subplan = create_plan_recurse(root, best_path->subpath,
1834 flags | CP_SMALL_TLIST);
1837 * make_sort_from_pathkeys() indirectly calls find_ec_member_for_tle(),
1838 * which will ignore any child EC members that don't belong to the given
1839 * relids. Thus, if this sort path is based on a child relation, we must
1842 plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys,
1843 IS_OTHER_REL(best_path->subpath->parent) ?
1844 best_path->path.parent->relids : NULL);
1846 copy_generic_path_info(&plan->plan, (Path *) best_path);
1854 * Create a Group plan for 'best_path' and (recursively) plans
1858 create_group_plan(PlannerInfo *root, GroupPath *best_path)
1866 * Group can project, so no need to be terribly picky about child tlist,
1867 * but we do need grouping columns to be available
1869 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1871 tlist = build_path_tlist(root, &best_path->path);
1873 quals = order_qual_clauses(root, best_path->qual);
1875 plan = make_group(tlist,
1877 list_length(best_path->groupClause),
1878 extract_grouping_cols(best_path->groupClause,
1879 subplan->targetlist),
1880 extract_grouping_ops(best_path->groupClause),
1883 copy_generic_path_info(&plan->plan, (Path *) best_path);
1889 * create_upper_unique_plan
1891 * Create a Unique plan for 'best_path' and (recursively) plans
1895 create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
1901 * Unique doesn't project, so tlist requirements pass through; moreover we
1902 * need grouping columns to be labeled.
1904 subplan = create_plan_recurse(root, best_path->subpath,
1905 flags | CP_LABEL_TLIST);
1907 plan = make_unique_from_pathkeys(subplan,
1908 best_path->path.pathkeys,
1909 best_path->numkeys);
1911 copy_generic_path_info(&plan->plan, (Path *) best_path);
1919 * Create an Agg plan for 'best_path' and (recursively) plans
1923 create_agg_plan(PlannerInfo *root, AggPath *best_path)
1931 * Agg can project, so no need to be terribly picky about child tlist, but
1932 * we do need grouping columns to be available
1934 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1936 tlist = build_path_tlist(root, &best_path->path);
1938 quals = order_qual_clauses(root, best_path->qual);
1940 plan = make_agg(tlist, quals,
1941 best_path->aggstrategy,
1942 best_path->aggsplit,
1943 list_length(best_path->groupClause),
1944 extract_grouping_cols(best_path->groupClause,
1945 subplan->targetlist),
1946 extract_grouping_ops(best_path->groupClause),
1949 best_path->numGroups,
1952 copy_generic_path_info(&plan->plan, (Path *) best_path);
1958 * Given a groupclause for a collection of grouping sets, produce the
1959 * corresponding groupColIdx.
1961 * root->grouping_map maps the tleSortGroupRef to the actual column position in
1962 * the input tuple. So we get the ref from the entries in the groupclause and
1963 * look them up there.
1966 remap_groupColIdx(PlannerInfo *root, List *groupClause)
1968 AttrNumber *grouping_map = root->grouping_map;
1969 AttrNumber *new_grpColIdx;
1973 Assert(grouping_map);
1975 new_grpColIdx = palloc0(sizeof(AttrNumber) * list_length(groupClause));
1978 foreach(lc, groupClause)
1980 SortGroupClause *clause = lfirst(lc);
1982 new_grpColIdx[i++] = grouping_map[clause->tleSortGroupRef];
1985 return new_grpColIdx;
1989 * create_groupingsets_plan
1990 * Create a plan for 'best_path' and (recursively) plans
1993 * What we emit is an Agg plan with some vestigial Agg and Sort nodes
1994 * hanging off the side. The top Agg implements the last grouping set
1995 * specified in the GroupingSetsPath, and any additional grouping sets
1996 * each give rise to a subsidiary Agg and Sort node in the top Agg's
1997 * "chain" list. These nodes don't participate in the plan directly,
1998 * but they are a convenient way to represent the required data for
2001 * Returns a Plan node.
2004 create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
2008 List *rollups = best_path->rollups;
2009 AttrNumber *grouping_map;
2014 /* Shouldn't get here without grouping sets */
2015 Assert(root->parse->groupingSets);
2016 Assert(rollups != NIL);
2019 * Agg can project, so no need to be terribly picky about child tlist, but
2020 * we do need grouping columns to be available
2022 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2025 * Compute the mapping from tleSortGroupRef to column index in the child's
2026 * tlist. First, identify max SortGroupRef in groupClause, for array
2030 foreach(lc, root->parse->groupClause)
2032 SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2034 if (gc->tleSortGroupRef > maxref)
2035 maxref = gc->tleSortGroupRef;
2038 grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2040 /* Now look up the column numbers in the child's tlist */
2041 foreach(lc, root->parse->groupClause)
2043 SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2044 TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
2046 grouping_map[gc->tleSortGroupRef] = tle->resno;
2050 * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2051 * in GroupingFunc nodes. Save it for setrefs.c to use.
2053 * This doesn't work if we're in an inheritance subtree (see notes in
2054 * create_modifytable_plan). Fortunately we can't be because there would
2055 * never be grouping in an UPDATE/DELETE; but let's Assert that.
2057 Assert(root->inhTargetKind == INHKIND_NONE);
2058 Assert(root->grouping_map == NULL);
2059 root->grouping_map = grouping_map;
2062 * Generate the side nodes that describe the other sort and group
2063 * operations besides the top one. Note that we don't worry about putting
2064 * accurate cost estimates in the side nodes; only the topmost Agg node's
2065 * costs will be shown by EXPLAIN.
2068 if (list_length(rollups) > 1)
2070 ListCell *lc2 = lnext(list_head(rollups));
2071 bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2073 for_each_cell(lc, lc2)
2075 RollupData *rollup = lfirst(lc);
2076 AttrNumber *new_grpColIdx;
2077 Plan *sort_plan = NULL;
2081 new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2083 if (!rollup->is_hashed && !is_first_sort)
2085 sort_plan = (Plan *)
2086 make_sort_from_groupcols(rollup->groupClause,
2091 if (!rollup->is_hashed)
2092 is_first_sort = false;
2094 if (rollup->is_hashed)
2096 else if (list_length(linitial(rollup->gsets)) == 0)
2101 agg_plan = (Plan *) make_agg(NIL,
2105 list_length((List *) linitial(rollup->gsets)),
2107 extract_grouping_ops(rollup->groupClause),
2114 * Remove stuff we don't need to avoid bloating debug output.
2118 sort_plan->targetlist = NIL;
2119 sort_plan->lefttree = NULL;
2122 chain = lappend(chain, agg_plan);
2127 * Now make the real Agg node
2130 RollupData *rollup = linitial(rollups);
2131 AttrNumber *top_grpColIdx;
2134 top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2136 numGroupCols = list_length((List *) linitial(rollup->gsets));
2138 plan = make_agg(build_path_tlist(root, &best_path->path),
2140 best_path->aggstrategy,
2144 extract_grouping_ops(rollup->groupClause),
2150 /* Copy cost data from Path to Plan */
2151 copy_generic_path_info(&plan->plan, &best_path->path);
2154 return (Plan *) plan;
2158 * create_minmaxagg_plan
2160 * Create a Result plan for 'best_path' and (recursively) plans
2164 create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
2170 /* Prepare an InitPlan for each aggregate's subquery. */
2171 foreach(lc, best_path->mmaggregates)
2173 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2174 PlannerInfo *subroot = mminfo->subroot;
2175 Query *subparse = subroot->parse;
2179 * Generate the plan for the subquery. We already have a Path, but we
2180 * have to convert it to a Plan and attach a LIMIT node above it.
2181 * Since we are entering a different planner context (subroot),
2182 * recurse to create_plan not create_plan_recurse.
2184 plan = create_plan(subroot, mminfo->path);
2186 plan = (Plan *) make_limit(plan,
2187 subparse->limitOffset,
2188 subparse->limitCount);
2190 /* Must apply correct cost/width data to Limit node */
2191 plan->startup_cost = mminfo->path->startup_cost;
2192 plan->total_cost = mminfo->pathcost;
2193 plan->plan_rows = 1;
2194 plan->plan_width = mminfo->path->pathtarget->width;
2195 plan->parallel_aware = false;
2196 plan->parallel_safe = mminfo->path->parallel_safe;
2198 /* Convert the plan into an InitPlan in the outer query. */
2199 SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2202 /* Generate the output plan --- basically just a Result */
2203 tlist = build_path_tlist(root, &best_path->path);
2205 plan = make_result(tlist, (Node *) best_path->quals, NULL);
2207 copy_generic_path_info(&plan->plan, (Path *) best_path);
2210 * During setrefs.c, we'll need to replace references to the Agg nodes
2211 * with InitPlan output params. (We can't just do that locally in the
2212 * MinMaxAgg node, because path nodes above here may have Agg references
2213 * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2215 * This doesn't work if we're in an inheritance subtree (see notes in
2216 * create_modifytable_plan). Fortunately we can't be because there would
2217 * never be aggregates in an UPDATE/DELETE; but let's Assert that.
2219 Assert(root->inhTargetKind == INHKIND_NONE);
2220 Assert(root->minmax_aggs == NIL);
2221 root->minmax_aggs = best_path->mmaggregates;
2227 * create_windowagg_plan
2229 * Create a WindowAgg plan for 'best_path' and (recursively) plans
2233 create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
2236 WindowClause *wc = best_path->winclause;
2237 int numPart = list_length(wc->partitionClause);
2238 int numOrder = list_length(wc->orderClause);
2242 AttrNumber *partColIdx;
2245 AttrNumber *ordColIdx;
2250 * WindowAgg can project, so no need to be terribly picky about child
2251 * tlist, but we do need grouping columns to be available
2253 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2255 tlist = build_path_tlist(root, &best_path->path);
2258 * Convert SortGroupClause lists into arrays of attr indexes and equality
2259 * operators, as wanted by executor. (Note: in principle, it's possible
2260 * to drop some of the sort columns, if they were proved redundant by
2261 * pathkey logic. However, it doesn't seem worth going out of our way to
2262 * optimize such cases. In any case, we must *not* remove the ordering
2263 * column for RANGE OFFSET cases, as the executor needs that for in_range
2264 * tests even if it's known to be equal to some partitioning column.)
2266 partColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numPart);
2267 partOperators = (Oid *) palloc(sizeof(Oid) * numPart);
2270 foreach(lc, wc->partitionClause)
2272 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2273 TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
2275 Assert(OidIsValid(sgc->eqop));
2276 partColIdx[partNumCols] = tle->resno;
2277 partOperators[partNumCols] = sgc->eqop;
2281 ordColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numOrder);
2282 ordOperators = (Oid *) palloc(sizeof(Oid) * numOrder);
2285 foreach(lc, wc->orderClause)
2287 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2288 TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
2290 Assert(OidIsValid(sgc->eqop));
2291 ordColIdx[ordNumCols] = tle->resno;
2292 ordOperators[ordNumCols] = sgc->eqop;
2296 /* And finally we can make the WindowAgg node */
2297 plan = make_windowagg(tlist,
2308 wc->startInRangeFunc,
2312 wc->inRangeNullsFirst,
2315 copy_generic_path_info(&plan->plan, (Path *) best_path);
2323 * Create a SetOp plan for 'best_path' and (recursively) plans
2327 create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
2334 * SetOp doesn't project, so tlist requirements pass through; moreover we
2335 * need grouping columns to be labeled.
2337 subplan = create_plan_recurse(root, best_path->subpath,
2338 flags | CP_LABEL_TLIST);
2340 /* Convert numGroups to long int --- but 'ware overflow! */
2341 numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2343 plan = make_setop(best_path->cmd,
2344 best_path->strategy,
2346 best_path->distinctList,
2347 best_path->flagColIdx,
2348 best_path->firstFlag,
2351 copy_generic_path_info(&plan->plan, (Path *) best_path);
2357 * create_recursiveunion_plan
2359 * Create a RecursiveUnion plan for 'best_path' and (recursively) plans
2362 static RecursiveUnion *
2363 create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
2365 RecursiveUnion *plan;
2371 /* Need both children to produce same tlist, so force it */
2372 leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2373 rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2375 tlist = build_path_tlist(root, &best_path->path);
2377 /* Convert numGroups to long int --- but 'ware overflow! */
2378 numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2380 plan = make_recursive_union(tlist,
2384 best_path->distinctList,
2387 copy_generic_path_info(&plan->plan, (Path *) best_path);
2393 * create_lockrows_plan
2395 * Create a LockRows plan for 'best_path' and (recursively) plans
2399 create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
2405 /* LockRows doesn't project, so tlist requirements pass through */
2406 subplan = create_plan_recurse(root, best_path->subpath, flags);
2408 plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2410 copy_generic_path_info(&plan->plan, (Path *) best_path);
2416 * create_modifytable_plan
2417 * Create a ModifyTable plan for 'best_path'.
2419 * Returns a Plan node.
2421 static ModifyTable *
2422 create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
2425 List *subplans = NIL;
2429 /* Build the plan for each input path */
2430 forboth(subpaths, best_path->subpaths,
2431 subroots, best_path->subroots)
2433 Path *subpath = (Path *) lfirst(subpaths);
2434 PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2438 * In an inherited UPDATE/DELETE, reference the per-child modified
2439 * subroot while creating Plans from Paths for the child rel. This is
2440 * a kluge, but otherwise it's too hard to ensure that Plan creation
2441 * functions (particularly in FDWs) don't depend on the contents of
2442 * "root" matching what they saw at Path creation time. The main
2443 * downside is that creation functions for Plans that might appear
2444 * below a ModifyTable cannot expect to modify the contents of "root"
2445 * and have it "stick" for subsequent processing such as setrefs.c.
2446 * That's not great, but it seems better than the alternative.
2448 subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2450 /* Transfer resname/resjunk labeling, too, to keep executor happy */
2451 apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2453 subplans = lappend(subplans, subplan);
2456 plan = make_modifytable(root,
2457 best_path->operation,
2458 best_path->canSetTag,
2459 best_path->nominalRelation,
2460 best_path->rootRelation,
2461 best_path->partColsUpdated,
2462 best_path->resultRelations,
2464 best_path->subroots,
2465 best_path->withCheckOptionLists,
2466 best_path->returningLists,
2467 best_path->rowMarks,
2468 best_path->onconflict,
2469 best_path->epqParam);
2471 copy_generic_path_info(&plan->plan, &best_path->path);
2479 * Create a Limit plan for 'best_path' and (recursively) plans
2483 create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
2488 /* Limit doesn't project, so tlist requirements pass through */
2489 subplan = create_plan_recurse(root, best_path->subpath, flags);
2491 plan = make_limit(subplan,
2492 best_path->limitOffset,
2493 best_path->limitCount);
2495 copy_generic_path_info(&plan->plan, (Path *) best_path);
2501 /*****************************************************************************
2503 * BASE-RELATION SCAN METHODS
2505 *****************************************************************************/
2509 * create_seqscan_plan
2510 * Returns a seqscan plan for the base relation scanned by 'best_path'
2511 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2514 create_seqscan_plan(PlannerInfo *root, Path *best_path,
2515 List *tlist, List *scan_clauses)
2518 Index scan_relid = best_path->parent->relid;
2520 /* it should be a base rel... */
2521 Assert(scan_relid > 0);
2522 Assert(best_path->parent->rtekind == RTE_RELATION);
2524 /* Sort clauses into best execution order */
2525 scan_clauses = order_qual_clauses(root, scan_clauses);
2527 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2528 scan_clauses = extract_actual_clauses(scan_clauses, false);
2530 /* Replace any outer-relation variables with nestloop params */
2531 if (best_path->param_info)
2533 scan_clauses = (List *)
2534 replace_nestloop_params(root, (Node *) scan_clauses);
2537 scan_plan = make_seqscan(tlist,
2541 copy_generic_path_info(&scan_plan->plan, best_path);
2547 * create_samplescan_plan
2548 * Returns a samplescan plan for the base relation scanned by 'best_path'
2549 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2552 create_samplescan_plan(PlannerInfo *root, Path *best_path,
2553 List *tlist, List *scan_clauses)
2555 SampleScan *scan_plan;
2556 Index scan_relid = best_path->parent->relid;
2558 TableSampleClause *tsc;
2560 /* it should be a base rel with a tablesample clause... */
2561 Assert(scan_relid > 0);
2562 rte = planner_rt_fetch(scan_relid, root);
2563 Assert(rte->rtekind == RTE_RELATION);
2564 tsc = rte->tablesample;
2565 Assert(tsc != NULL);
2567 /* Sort clauses into best execution order */
2568 scan_clauses = order_qual_clauses(root, scan_clauses);
2570 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2571 scan_clauses = extract_actual_clauses(scan_clauses, false);
2573 /* Replace any outer-relation variables with nestloop params */
2574 if (best_path->param_info)
2576 scan_clauses = (List *)
2577 replace_nestloop_params(root, (Node *) scan_clauses);
2578 tsc = (TableSampleClause *)
2579 replace_nestloop_params(root, (Node *) tsc);
2582 scan_plan = make_samplescan(tlist,
2587 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2593 * create_indexscan_plan
2594 * Returns an indexscan plan for the base relation scanned by 'best_path'
2595 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2597 * We use this for both plain IndexScans and IndexOnlyScans, because the
2598 * qual preprocessing work is the same for both. Note that the caller tells
2599 * us which to build --- we don't look at best_path->path.pathtype, because
2600 * create_bitmap_subplan needs to be able to override the prior decision.
2603 create_indexscan_plan(PlannerInfo *root,
2604 IndexPath *best_path,
2610 List *indexquals = best_path->indexquals;
2611 List *indexorderbys = best_path->indexorderbys;
2612 Index baserelid = best_path->path.parent->relid;
2613 Oid indexoid = best_path->indexinfo->indexoid;
2615 List *stripped_indexquals;
2616 List *fixed_indexquals;
2617 List *fixed_indexorderbys;
2618 List *indexorderbyops = NIL;
2621 /* it should be a base rel... */
2622 Assert(baserelid > 0);
2623 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2626 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
2627 * executor as indexqualorig
2629 stripped_indexquals = get_actual_clauses(indexquals);
2632 * The executor needs a copy with the indexkey on the left of each clause
2633 * and with index Vars substituted for table ones.
2635 fixed_indexquals = fix_indexqual_references(root, best_path);
2638 * Likewise fix up index attr references in the ORDER BY expressions.
2640 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2643 * The qpqual list must contain all restrictions not automatically handled
2644 * by the index, other than pseudoconstant clauses which will be handled
2645 * by a separate gating plan node. All the predicates in the indexquals
2646 * will be checked (either by the index itself, or by nodeIndexscan.c),
2647 * but if there are any "special" operators involved then they must be
2648 * included in qpqual. The upshot is that qpqual must contain
2649 * scan_clauses minus whatever appears in indexquals.
2651 * In normal cases simple pointer equality checks will be enough to spot
2652 * duplicate RestrictInfos, so we try that first.
2654 * Another common case is that a scan_clauses entry is generated from the
2655 * same EquivalenceClass as some indexqual, and is therefore redundant
2656 * with it, though not equal. (This happens when indxpath.c prefers a
2657 * different derived equality than what generate_join_implied_equalities
2658 * picked for a parameterized scan's ppi_clauses.)
2660 * In some situations (particularly with OR'd index conditions) we may
2661 * have scan_clauses that are not equal to, but are logically implied by,
2662 * the index quals; so we also try a predicate_implied_by() check to see
2663 * if we can discard quals that way. (predicate_implied_by assumes its
2664 * first input contains only immutable functions, so we have to check
2667 * Note: if you change this bit of code you should also look at
2668 * extract_nonindex_conditions() in costsize.c.
2671 foreach(l, scan_clauses)
2673 RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2675 if (rinfo->pseudoconstant)
2676 continue; /* we may drop pseudoconstants here */
2677 if (list_member_ptr(indexquals, rinfo))
2678 continue; /* simple duplicate */
2679 if (is_redundant_derived_clause(rinfo, indexquals))
2680 continue; /* derived from same EquivalenceClass */
2681 if (!contain_mutable_functions((Node *) rinfo->clause) &&
2682 predicate_implied_by(list_make1(rinfo->clause), indexquals, false))
2683 continue; /* provably implied by indexquals */
2684 qpqual = lappend(qpqual, rinfo);
2687 /* Sort clauses into best execution order */
2688 qpqual = order_qual_clauses(root, qpqual);
2690 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2691 qpqual = extract_actual_clauses(qpqual, false);
2694 * We have to replace any outer-relation variables with nestloop params in
2695 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2696 * annoying to have to do this separately from the processing in
2697 * fix_indexqual_references --- rethink this when generalizing the inner
2698 * indexscan support. But note we can't really do this earlier because
2699 * it'd break the comparisons to predicates above ... (or would it? Those
2700 * wouldn't have outer refs)
2702 if (best_path->path.param_info)
2704 stripped_indexquals = (List *)
2705 replace_nestloop_params(root, (Node *) stripped_indexquals);
2707 replace_nestloop_params(root, (Node *) qpqual);
2708 indexorderbys = (List *)
2709 replace_nestloop_params(root, (Node *) indexorderbys);
2713 * If there are ORDER BY expressions, look up the sort operators for their
2718 ListCell *pathkeyCell,
2722 * PathKey contains OID of the btree opfamily we're sorting by, but
2723 * that's not quite enough because we need the expression's datatype
2724 * to look up the sort operator in the operator family.
2726 Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2727 forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2729 PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2730 Node *expr = (Node *) lfirst(exprCell);
2731 Oid exprtype = exprType(expr);
2734 /* Get sort operator from opfamily */
2735 sortop = get_opfamily_member(pathkey->pk_opfamily,
2738 pathkey->pk_strategy);
2739 if (!OidIsValid(sortop))
2740 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2741 pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
2742 indexorderbyops = lappend_oid(indexorderbyops, sortop);
2746 /* Finally ready to build the plan node */
2748 scan_plan = (Scan *) make_indexonlyscan(tlist,
2753 fixed_indexorderbys,
2754 best_path->indexinfo->indextlist,
2755 best_path->indexscandir);
2757 scan_plan = (Scan *) make_indexscan(tlist,
2762 stripped_indexquals,
2763 fixed_indexorderbys,
2766 best_path->indexscandir);
2768 copy_generic_path_info(&scan_plan->plan, &best_path->path);
2774 * create_bitmap_scan_plan
2775 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
2776 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2778 static BitmapHeapScan *
2779 create_bitmap_scan_plan(PlannerInfo *root,
2780 BitmapHeapPath *best_path,
2784 Index baserelid = best_path->path.parent->relid;
2785 Plan *bitmapqualplan;
2786 List *bitmapqualorig;
2791 BitmapHeapScan *scan_plan;
2793 /* it should be a base rel... */
2794 Assert(baserelid > 0);
2795 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2797 /* Process the bitmapqual tree into a Plan tree and qual lists */
2798 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
2799 &bitmapqualorig, &indexquals,
2802 if (best_path->path.parallel_aware)
2803 bitmap_subplan_mark_shared(bitmapqualplan);
2806 * The qpqual list must contain all restrictions not automatically handled
2807 * by the index, other than pseudoconstant clauses which will be handled
2808 * by a separate gating plan node. All the predicates in the indexquals
2809 * will be checked (either by the index itself, or by
2810 * nodeBitmapHeapscan.c), but if there are any "special" operators
2811 * involved then they must be added to qpqual. The upshot is that qpqual
2812 * must contain scan_clauses minus whatever appears in indexquals.
2814 * This loop is similar to the comparable code in create_indexscan_plan(),
2815 * but with some differences because it has to compare the scan clauses to
2816 * stripped (no RestrictInfos) indexquals. See comments there for more
2819 * In normal cases simple equal() checks will be enough to spot duplicate
2820 * clauses, so we try that first. We next see if the scan clause is
2821 * redundant with any top-level indexqual by virtue of being generated
2822 * from the same EC. After that, try predicate_implied_by().
2824 * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
2825 * useful for getting rid of qpquals that are implied by index predicates,
2826 * because the predicate conditions are included in the "indexquals"
2827 * returned by create_bitmap_subplan(). Bitmap scans have to do it that
2828 * way because predicate conditions need to be rechecked if the scan
2829 * becomes lossy, so they have to be included in bitmapqualorig.
2832 foreach(l, scan_clauses)
2834 RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2835 Node *clause = (Node *) rinfo->clause;
2837 if (rinfo->pseudoconstant)
2838 continue; /* we may drop pseudoconstants here */
2839 if (list_member(indexquals, clause))
2840 continue; /* simple duplicate */
2841 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
2842 continue; /* derived from same EquivalenceClass */
2843 if (!contain_mutable_functions(clause) &&
2844 predicate_implied_by(list_make1(clause), indexquals, false))
2845 continue; /* provably implied by indexquals */
2846 qpqual = lappend(qpqual, rinfo);
2849 /* Sort clauses into best execution order */
2850 qpqual = order_qual_clauses(root, qpqual);
2852 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2853 qpqual = extract_actual_clauses(qpqual, false);
2856 * When dealing with special operators, we will at this point have
2857 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
2858 * 'em from bitmapqualorig, since there's no point in making the tests
2861 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
2864 * We have to replace any outer-relation variables with nestloop params in
2865 * the qpqual and bitmapqualorig expressions. (This was already done for
2866 * expressions attached to plan nodes in the bitmapqualplan tree.)
2868 if (best_path->path.param_info)
2871 replace_nestloop_params(root, (Node *) qpqual);
2872 bitmapqualorig = (List *)
2873 replace_nestloop_params(root, (Node *) bitmapqualorig);
2876 /* Finally ready to build the plan node */
2877 scan_plan = make_bitmap_heapscan(tlist,
2883 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2889 * Given a bitmapqual tree, generate the Plan tree that implements it
2891 * As byproducts, we also return in *qual and *indexqual the qual lists
2892 * (in implicit-AND form, without RestrictInfos) describing the original index
2893 * conditions and the generated indexqual conditions. (These are the same in
2894 * simple cases, but when special index operators are involved, the former
2895 * list includes the special conditions while the latter includes the actual
2896 * indexable conditions derived from them.) Both lists include partial-index
2897 * predicates, because we have to recheck predicates as well as index
2898 * conditions if the bitmap scan becomes lossy.
2900 * In addition, we return a list of EquivalenceClass pointers for all the
2901 * top-level indexquals that were possibly-redundantly derived from ECs.
2902 * This allows removal of scan_clauses that are redundant with such quals.
2903 * (We do not attempt to detect such redundancies for quals that are within
2904 * OR subtrees. This could be done in a less hacky way if we returned the
2905 * indexquals in RestrictInfo form, but that would be slower and still pretty
2906 * messy, since we'd have to build new RestrictInfos in many cases.)
2909 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
2910 List **qual, List **indexqual, List **indexECs)
2914 if (IsA(bitmapqual, BitmapAndPath))
2916 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
2917 List *subplans = NIL;
2918 List *subquals = NIL;
2919 List *subindexquals = NIL;
2920 List *subindexECs = NIL;
2924 * There may well be redundant quals among the subplans, since a
2925 * top-level WHERE qual might have gotten used to form several
2926 * different index quals. We don't try exceedingly hard to eliminate
2927 * redundancies, but we do eliminate obvious duplicates by using
2928 * list_concat_unique.
2930 foreach(l, apath->bitmapquals)
2937 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2938 &subqual, &subindexqual,
2940 subplans = lappend(subplans, subplan);
2941 subquals = list_concat_unique(subquals, subqual);
2942 subindexquals = list_concat_unique(subindexquals, subindexqual);
2943 /* Duplicates in indexECs aren't worth getting rid of */
2944 subindexECs = list_concat(subindexECs, subindexEC);
2946 plan = (Plan *) make_bitmap_and(subplans);
2947 plan->startup_cost = apath->path.startup_cost;
2948 plan->total_cost = apath->path.total_cost;
2950 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
2951 plan->plan_width = 0; /* meaningless */
2952 plan->parallel_aware = false;
2953 plan->parallel_safe = apath->path.parallel_safe;
2955 *indexqual = subindexquals;
2956 *indexECs = subindexECs;
2958 else if (IsA(bitmapqual, BitmapOrPath))
2960 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
2961 List *subplans = NIL;
2962 List *subquals = NIL;
2963 List *subindexquals = NIL;
2964 bool const_true_subqual = false;
2965 bool const_true_subindexqual = false;
2969 * Here, we only detect qual-free subplans. A qual-free subplan would
2970 * cause us to generate "... OR true ..." which we may as well reduce
2971 * to just "true". We do not try to eliminate redundant subclauses
2972 * because (a) it's not as likely as in the AND case, and (b) we might
2973 * well be working with hundreds or even thousands of OR conditions,
2974 * perhaps from a long IN list. The performance of list_append_unique
2975 * would be unacceptable.
2977 foreach(l, opath->bitmapquals)
2984 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2985 &subqual, &subindexqual,
2987 subplans = lappend(subplans, subplan);
2989 const_true_subqual = true;
2990 else if (!const_true_subqual)
2991 subquals = lappend(subquals,
2992 make_ands_explicit(subqual));
2993 if (subindexqual == NIL)
2994 const_true_subindexqual = true;
2995 else if (!const_true_subindexqual)
2996 subindexquals = lappend(subindexquals,
2997 make_ands_explicit(subindexqual));
3001 * In the presence of ScalarArrayOpExpr quals, we might have built
3002 * BitmapOrPaths with just one subpath; don't add an OR step.
3004 if (list_length(subplans) == 1)
3006 plan = (Plan *) linitial(subplans);
3010 plan = (Plan *) make_bitmap_or(subplans);
3011 plan->startup_cost = opath->path.startup_cost;
3012 plan->total_cost = opath->path.total_cost;
3014 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3015 plan->plan_width = 0; /* meaningless */
3016 plan->parallel_aware = false;
3017 plan->parallel_safe = opath->path.parallel_safe;
3021 * If there were constant-TRUE subquals, the OR reduces to constant
3022 * TRUE. Also, avoid generating one-element ORs, which could happen
3023 * due to redundancy elimination or ScalarArrayOpExpr quals.
3025 if (const_true_subqual)
3027 else if (list_length(subquals) <= 1)
3030 *qual = list_make1(make_orclause(subquals));
3031 if (const_true_subindexqual)
3033 else if (list_length(subindexquals) <= 1)
3034 *indexqual = subindexquals;
3036 *indexqual = list_make1(make_orclause(subindexquals));
3039 else if (IsA(bitmapqual, IndexPath))
3041 IndexPath *ipath = (IndexPath *) bitmapqual;
3046 /* Use the regular indexscan plan build machinery... */
3047 iscan = castNode(IndexScan,
3048 create_indexscan_plan(root, ipath,
3050 /* then convert to a bitmap indexscan */
3051 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
3054 iscan->indexqualorig);
3055 /* and set its cost/width fields appropriately */
3056 plan->startup_cost = 0.0;
3057 plan->total_cost = ipath->indextotalcost;
3059 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3060 plan->plan_width = 0; /* meaningless */
3061 plan->parallel_aware = false;
3062 plan->parallel_safe = ipath->path.parallel_safe;
3063 *qual = get_actual_clauses(ipath->indexclauses);
3064 *indexqual = get_actual_clauses(ipath->indexquals);
3065 foreach(l, ipath->indexinfo->indpred)
3067 Expr *pred = (Expr *) lfirst(l);
3070 * We know that the index predicate must have been implied by the
3071 * query condition as a whole, but it may or may not be implied by
3072 * the conditions that got pushed into the bitmapqual. Avoid
3073 * generating redundant conditions.
3075 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses,
3078 *qual = lappend(*qual, pred);
3079 *indexqual = lappend(*indexqual, pred);
3083 foreach(l, ipath->indexquals)
3085 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
3087 if (rinfo->parent_ec)
3088 subindexECs = lappend(subindexECs, rinfo->parent_ec);
3090 *indexECs = subindexECs;
3094 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3095 plan = NULL; /* keep compiler quiet */
3102 * create_tidscan_plan
3103 * Returns a tidscan plan for the base relation scanned by 'best_path'
3104 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3107 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
3108 List *tlist, List *scan_clauses)
3111 Index scan_relid = best_path->path.parent->relid;
3112 List *tidquals = best_path->tidquals;
3114 /* it should be a base rel... */
3115 Assert(scan_relid > 0);
3116 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3119 * The qpqual list must contain all restrictions not enforced by the
3120 * tidquals list. Since tidquals has OR semantics, we have to be careful
3121 * about matching it up to scan_clauses. It's convenient to handle the
3122 * single-tidqual case separately from the multiple-tidqual case. In the
3123 * single-tidqual case, we look through the scan_clauses while they are
3124 * still in RestrictInfo form, and drop any that are redundant with the
3127 * In normal cases simple pointer equality checks will be enough to spot
3128 * duplicate RestrictInfos, so we try that first.
3130 * Another common case is that a scan_clauses entry is generated from the
3131 * same EquivalenceClass as some tidqual, and is therefore redundant with
3132 * it, though not equal.
3134 * Unlike indexpaths, we don't bother with predicate_implied_by(); the
3135 * number of cases where it could win are pretty small.
3137 if (list_length(tidquals) == 1)
3142 foreach(l, scan_clauses)
3144 RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3146 if (rinfo->pseudoconstant)
3147 continue; /* we may drop pseudoconstants here */
3148 if (list_member_ptr(tidquals, rinfo))
3149 continue; /* simple duplicate */
3150 if (is_redundant_derived_clause(rinfo, tidquals))
3151 continue; /* derived from same EquivalenceClass */
3152 qpqual = lappend(qpqual, rinfo);
3154 scan_clauses = qpqual;
3157 /* Sort clauses into best execution order */
3158 scan_clauses = order_qual_clauses(root, scan_clauses);
3160 /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3161 tidquals = extract_actual_clauses(tidquals, false);
3162 scan_clauses = extract_actual_clauses(scan_clauses, false);
3165 * If we have multiple tidquals, it's more convenient to remove duplicate
3166 * scan_clauses after stripping the RestrictInfos. In this situation,
3167 * because the tidquals represent OR sub-clauses, they could not have come
3168 * from EquivalenceClasses so we don't have to worry about matching up
3169 * non-identical clauses. On the other hand, because tidpath.c will have
3170 * extracted those sub-clauses from some OR clause and built its own list,
3171 * we will certainly not have pointer equality to any scan clause. So
3172 * convert the tidquals list to an explicit OR clause and see if we can
3173 * match it via equal() to any scan clause.
3175 if (list_length(tidquals) > 1)
3176 scan_clauses = list_difference(scan_clauses,
3177 list_make1(make_orclause(tidquals)));
3179 /* Replace any outer-relation variables with nestloop params */
3180 if (best_path->path.param_info)
3183 replace_nestloop_params(root, (Node *) tidquals);
3184 scan_clauses = (List *)
3185 replace_nestloop_params(root, (Node *) scan_clauses);
3188 scan_plan = make_tidscan(tlist,
3193 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3199 * create_subqueryscan_plan
3200 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
3201 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3203 static SubqueryScan *
3204 create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path,
3205 List *tlist, List *scan_clauses)
3207 SubqueryScan *scan_plan;
3208 RelOptInfo *rel = best_path->path.parent;
3209 Index scan_relid = rel->relid;
3212 /* it should be a subquery base rel... */
3213 Assert(scan_relid > 0);
3214 Assert(rel->rtekind == RTE_SUBQUERY);
3217 * Recursively create Plan from Path for subquery. Since we are entering
3218 * a different planner context (subroot), recurse to create_plan not
3219 * create_plan_recurse.
3221 subplan = create_plan(rel->subroot, best_path->subpath);
3223 /* Sort clauses into best execution order */
3224 scan_clauses = order_qual_clauses(root, scan_clauses);
3226 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3227 scan_clauses = extract_actual_clauses(scan_clauses, false);
3229 /* Replace any outer-relation variables with nestloop params */
3230 if (best_path->path.param_info)
3232 scan_clauses = (List *)
3233 replace_nestloop_params(root, (Node *) scan_clauses);
3234 process_subquery_nestloop_params(root,
3235 rel->subplan_params);
3238 scan_plan = make_subqueryscan(tlist,
3243 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3249 * create_functionscan_plan
3250 * Returns a functionscan plan for the base relation scanned by 'best_path'
3251 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3253 static FunctionScan *
3254 create_functionscan_plan(PlannerInfo *root, Path *best_path,
3255 List *tlist, List *scan_clauses)
3257 FunctionScan *scan_plan;
3258 Index scan_relid = best_path->parent->relid;
3262 /* it should be a function base rel... */
3263 Assert(scan_relid > 0);
3264 rte = planner_rt_fetch(scan_relid, root);
3265 Assert(rte->rtekind == RTE_FUNCTION);
3266 functions = rte->functions;
3268 /* Sort clauses into best execution order */
3269 scan_clauses = order_qual_clauses(root, scan_clauses);
3271 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3272 scan_clauses = extract_actual_clauses(scan_clauses, false);
3274 /* Replace any outer-relation variables with nestloop params */
3275 if (best_path->param_info)
3277 scan_clauses = (List *)
3278 replace_nestloop_params(root, (Node *) scan_clauses);
3279 /* The function expressions could contain nestloop params, too */
3280 functions = (List *) replace_nestloop_params(root, (Node *) functions);
3283 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3284 functions, rte->funcordinality);
3286 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3292 * create_tablefuncscan_plan
3293 * Returns a tablefuncscan plan for the base relation scanned by 'best_path'
3294 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3296 static TableFuncScan *
3297 create_tablefuncscan_plan(PlannerInfo *root, Path *best_path,
3298 List *tlist, List *scan_clauses)
3300 TableFuncScan *scan_plan;
3301 Index scan_relid = best_path->parent->relid;
3303 TableFunc *tablefunc;
3305 /* it should be a function base rel... */
3306 Assert(scan_relid > 0);
3307 rte = planner_rt_fetch(scan_relid, root);
3308 Assert(rte->rtekind == RTE_TABLEFUNC);
3309 tablefunc = rte->tablefunc;
3311 /* Sort clauses into best execution order */
3312 scan_clauses = order_qual_clauses(root, scan_clauses);
3314 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3315 scan_clauses = extract_actual_clauses(scan_clauses, false);
3317 /* Replace any outer-relation variables with nestloop params */
3318 if (best_path->param_info)
3320 scan_clauses = (List *)
3321 replace_nestloop_params(root, (Node *) scan_clauses);
3322 /* The function expressions could contain nestloop params, too */
3323 tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
3326 scan_plan = make_tablefuncscan(tlist, scan_clauses, scan_relid,
3329 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3335 * create_valuesscan_plan
3336 * Returns a valuesscan plan for the base relation scanned by 'best_path'
3337 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3340 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
3341 List *tlist, List *scan_clauses)
3343 ValuesScan *scan_plan;
3344 Index scan_relid = best_path->parent->relid;
3348 /* it should be a values base rel... */
3349 Assert(scan_relid > 0);
3350 rte = planner_rt_fetch(scan_relid, root);
3351 Assert(rte->rtekind == RTE_VALUES);
3352 values_lists = rte->values_lists;
3354 /* Sort clauses into best execution order */
3355 scan_clauses = order_qual_clauses(root, scan_clauses);
3357 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3358 scan_clauses = extract_actual_clauses(scan_clauses, false);
3360 /* Replace any outer-relation variables with nestloop params */
3361 if (best_path->param_info)
3363 scan_clauses = (List *)
3364 replace_nestloop_params(root, (Node *) scan_clauses);
3365 /* The values lists could contain nestloop params, too */
3366 values_lists = (List *)
3367 replace_nestloop_params(root, (Node *) values_lists);
3370 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
3373 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3379 * create_ctescan_plan
3380 * Returns a ctescan plan for the base relation scanned by 'best_path'
3381 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3384 create_ctescan_plan(PlannerInfo *root, Path *best_path,
3385 List *tlist, List *scan_clauses)
3388 Index scan_relid = best_path->parent->relid;
3390 SubPlan *ctesplan = NULL;
3393 PlannerInfo *cteroot;
3398 Assert(scan_relid > 0);
3399 rte = planner_rt_fetch(scan_relid, root);
3400 Assert(rte->rtekind == RTE_CTE);
3401 Assert(!rte->self_reference);
3404 * Find the referenced CTE, and locate the SubPlan previously made for it.
3406 levelsup = rte->ctelevelsup;
3408 while (levelsup-- > 0)
3410 cteroot = cteroot->parent_root;
3411 if (!cteroot) /* shouldn't happen */
3412 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3416 * Note: cte_plan_ids can be shorter than cteList, if we are still working
3417 * on planning the CTEs (ie, this is a side-reference from another CTE).
3418 * So we mustn't use forboth here.
3421 foreach(lc, cteroot->parse->cteList)
3423 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3425 if (strcmp(cte->ctename, rte->ctename) == 0)
3429 if (lc == NULL) /* shouldn't happen */
3430 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3431 if (ndx >= list_length(cteroot->cte_plan_ids))
3432 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3433 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3434 Assert(plan_id > 0);
3435 foreach(lc, cteroot->init_plans)
3437 ctesplan = (SubPlan *) lfirst(lc);
3438 if (ctesplan->plan_id == plan_id)
3441 if (lc == NULL) /* shouldn't happen */
3442 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3445 * We need the CTE param ID, which is the sole member of the SubPlan's
3448 cte_param_id = linitial_int(ctesplan->setParam);
3450 /* Sort clauses into best execution order */
3451 scan_clauses = order_qual_clauses(root, scan_clauses);
3453 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3454 scan_clauses = extract_actual_clauses(scan_clauses, false);
3456 /* Replace any outer-relation variables with nestloop params */
3457 if (best_path->param_info)
3459 scan_clauses = (List *)
3460 replace_nestloop_params(root, (Node *) scan_clauses);
3463 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3464 plan_id, cte_param_id);
3466 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3472 * create_namedtuplestorescan_plan
3473 * Returns a tuplestorescan plan for the base relation scanned by
3474 * 'best_path' with restriction clauses 'scan_clauses' and targetlist
3477 static NamedTuplestoreScan *
3478 create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path,
3479 List *tlist, List *scan_clauses)
3481 NamedTuplestoreScan *scan_plan;
3482 Index scan_relid = best_path->parent->relid;
3485 Assert(scan_relid > 0);
3486 rte = planner_rt_fetch(scan_relid, root);
3487 Assert(rte->rtekind == RTE_NAMEDTUPLESTORE);
3489 /* Sort clauses into best execution order */
3490 scan_clauses = order_qual_clauses(root, scan_clauses);
3492 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3493 scan_clauses = extract_actual_clauses(scan_clauses, false);
3495 /* Replace any outer-relation variables with nestloop params */
3496 if (best_path->param_info)
3498 scan_clauses = (List *)
3499 replace_nestloop_params(root, (Node *) scan_clauses);
3502 scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3505 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3511 * create_resultscan_plan
3512 * Returns a Result plan for the RTE_RESULT base relation scanned by
3513 * 'best_path' with restriction clauses 'scan_clauses' and targetlist
3517 create_resultscan_plan(PlannerInfo *root, Path *best_path,
3518 List *tlist, List *scan_clauses)
3521 Index scan_relid = best_path->parent->relid;
3522 RangeTblEntry *rte PG_USED_FOR_ASSERTS_ONLY;
3524 Assert(scan_relid > 0);
3525 rte = planner_rt_fetch(scan_relid, root);
3526 Assert(rte->rtekind == RTE_RESULT);
3528 /* Sort clauses into best execution order */
3529 scan_clauses = order_qual_clauses(root, scan_clauses);
3531 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3532 scan_clauses = extract_actual_clauses(scan_clauses, false);
3534 /* Replace any outer-relation variables with nestloop params */
3535 if (best_path->param_info)
3537 scan_clauses = (List *)
3538 replace_nestloop_params(root, (Node *) scan_clauses);
3541 scan_plan = make_result(tlist, (Node *) scan_clauses, NULL);
3543 copy_generic_path_info(&scan_plan->plan, best_path);
3549 * create_worktablescan_plan
3550 * Returns a worktablescan plan for the base relation scanned by 'best_path'
3551 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3553 static WorkTableScan *
3554 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
3555 List *tlist, List *scan_clauses)
3557 WorkTableScan *scan_plan;
3558 Index scan_relid = best_path->parent->relid;
3561 PlannerInfo *cteroot;
3563 Assert(scan_relid > 0);
3564 rte = planner_rt_fetch(scan_relid, root);
3565 Assert(rte->rtekind == RTE_CTE);
3566 Assert(rte->self_reference);
3569 * We need to find the worktable param ID, which is in the plan level
3570 * that's processing the recursive UNION, which is one level *below* where
3571 * the CTE comes from.
3573 levelsup = rte->ctelevelsup;
3574 if (levelsup == 0) /* shouldn't happen */
3575 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3578 while (levelsup-- > 0)
3580 cteroot = cteroot->parent_root;
3581 if (!cteroot) /* shouldn't happen */
3582 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3584 if (cteroot->wt_param_id < 0) /* shouldn't happen */
3585 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
3587 /* Sort clauses into best execution order */
3588 scan_clauses = order_qual_clauses(root, scan_clauses);
3590 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3591 scan_clauses = extract_actual_clauses(scan_clauses, false);
3593 /* Replace any outer-relation variables with nestloop params */
3594 if (best_path->param_info)
3596 scan_clauses = (List *)
3597 replace_nestloop_params(root, (Node *) scan_clauses);
3600 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
3601 cteroot->wt_param_id);
3603 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3609 * create_foreignscan_plan
3610 * Returns a foreignscan plan for the relation scanned by 'best_path'
3611 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3613 static ForeignScan *
3614 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
3615 List *tlist, List *scan_clauses)
3617 ForeignScan *scan_plan;
3618 RelOptInfo *rel = best_path->path.parent;
3619 Index scan_relid = rel->relid;
3620 Oid rel_oid = InvalidOid;
3621 Plan *outer_plan = NULL;
3623 Assert(rel->fdwroutine != NULL);
3625 /* transform the child path if any */
3626 if (best_path->fdw_outerpath)
3627 outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3631 * If we're scanning a base relation, fetch its OID. (Irrelevant if
3632 * scanning a join relation.)
3638 Assert(rel->rtekind == RTE_RELATION);
3639 rte = planner_rt_fetch(scan_relid, root);
3640 Assert(rte->rtekind == RTE_RELATION);
3641 rel_oid = rte->relid;
3645 * Sort clauses into best execution order. We do this first since the FDW
3646 * might have more info than we do and wish to adjust the ordering.
3648 scan_clauses = order_qual_clauses(root, scan_clauses);
3651 * Let the FDW perform its processing on the restriction clauses and
3652 * generate the plan node. Note that the FDW might remove restriction
3653 * clauses that it intends to execute remotely, or even add more (if it
3654 * has selected some join clauses for remote use but also wants them
3655 * rechecked locally).
3657 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
3659 tlist, scan_clauses,
3662 /* Copy cost data from Path to Plan; no need to make FDW do this */
3663 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3665 /* Copy foreign server OID; likewise, no need to make FDW do this */
3666 scan_plan->fs_server = rel->serverid;
3669 * Likewise, copy the relids that are represented by this foreign scan. An
3670 * upper rel doesn't have relids set, but it covers all the base relations
3671 * participating in the underlying scan, so use root's all_baserels.
3673 if (rel->reloptkind == RELOPT_UPPER_REL)
3674 scan_plan->fs_relids = root->all_baserels;
3676 scan_plan->fs_relids = best_path->path.parent->relids;
3679 * If this is a foreign join, and to make it valid to push down we had to
3680 * assume that the current user is the same as some user explicitly named
3681 * in the query, mark the finished plan as depending on the current user.
3683 if (rel->useridiscurrent)
3684 root->glob->dependsOnRole = true;
3687 * Replace any outer-relation variables with nestloop params in the qual,
3688 * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
3689 * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
3690 * fdw_recheck_quals could have come from join clauses, so doing this
3691 * beforehand on the scan_clauses wouldn't work.) We assume
3692 * fdw_scan_tlist contains no such variables.
3694 if (best_path->path.param_info)
3696 scan_plan->scan.plan.qual = (List *)
3697 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
3698 scan_plan->fdw_exprs = (List *)
3699 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
3700 scan_plan->fdw_recheck_quals = (List *)
3701 replace_nestloop_params(root,
3702 (Node *) scan_plan->fdw_recheck_quals);
3706 * If rel is a base relation, detect whether any system columns are
3707 * requested from the rel. (If rel is a join relation, rel->relid will be
3708 * 0, but there can be no Var with relid 0 in the rel's targetlist or the
3709 * restriction clauses, so we skip this in that case. Note that any such
3710 * columns in base relations that were joined are assumed to be contained
3711 * in fdw_scan_tlist.) This is a bit of a kluge and might go away
3712 * someday, so we intentionally leave it out of the API presented to FDWs.
3714 scan_plan->fsSystemCol = false;
3717 Bitmapset *attrs_used = NULL;
3722 * First, examine all the attributes needed for joins or final output.
3723 * Note: we must look at rel's targetlist, not the attr_needed data,
3724 * because attr_needed isn't computed for inheritance child rels.
3726 pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
3728 /* Add all the attributes used by restriction clauses. */
3729 foreach(lc, rel->baserestrictinfo)
3731 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3733 pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
3736 /* Now, are any system columns requested from rel? */
3737 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
3739 if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
3741 scan_plan->fsSystemCol = true;
3746 bms_free(attrs_used);
3753 * create_custom_plan
3755 * Transform a CustomPath into a Plan.
3758 create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
3759 List *tlist, List *scan_clauses)
3762 RelOptInfo *rel = best_path->path.parent;
3763 List *custom_plans = NIL;
3766 /* Recursively transform child paths. */
3767 foreach(lc, best_path->custom_paths)
3769 Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3772 custom_plans = lappend(custom_plans, plan);
3776 * Sort clauses into the best execution order, although custom-scan
3777 * provider can reorder them again.
3779 scan_clauses = order_qual_clauses(root, scan_clauses);
3782 * Invoke custom plan provider to create the Plan node represented by the
3785 cplan = castNode(CustomScan,
3786 best_path->methods->PlanCustomPath(root,
3794 * Copy cost data from Path to Plan; no need to make custom-plan providers
3797 copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3799 /* Likewise, copy the relids that are represented by this custom scan */
3800 cplan->custom_relids = best_path->path.parent->relids;
3803 * Replace any outer-relation variables with nestloop params in the qual
3804 * and custom_exprs expressions. We do this last so that the custom-plan
3805 * provider doesn't have to be involved. (Note that parts of custom_exprs
3806 * could have come from join clauses, so doing this beforehand on the
3807 * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3810 if (best_path->path.param_info)
3812 cplan->scan.plan.qual = (List *)
3813 replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3814 cplan->custom_exprs = (List *)
3815 replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3822 /*****************************************************************************
3826 *****************************************************************************/
3829 create_nestloop_plan(PlannerInfo *root,
3830 NestPath *best_path)
3832 NestLoop *join_plan;
3835 List *tlist = build_path_tlist(root, &best_path->path);
3836 List *joinrestrictclauses = best_path->joinrestrictinfo;
3841 Relids saveOuterRels = root->curOuterRels;
3843 /* NestLoop can project, so no need to be picky about child tlists */
3844 outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3846 /* For a nestloop, include outer relids in curOuterRels for inner side */
3847 root->curOuterRels = bms_union(root->curOuterRels,
3848 best_path->outerjoinpath->parent->relids);
3850 inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3852 /* Restore curOuterRels */
3853 bms_free(root->curOuterRels);
3854 root->curOuterRels = saveOuterRels;
3856 /* Sort join qual clauses into best execution order */
3857 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3859 /* Get the join qual clauses (in plain expression form) */
3860 /* Any pseudoconstant clauses are ignored here */
3861 if (IS_OUTER_JOIN(best_path->jointype))
3863 extract_actual_join_clauses(joinrestrictclauses,
3864 best_path->path.parent->relids,
3865 &joinclauses, &otherclauses);
3869 /* We can treat all clauses alike for an inner join */
3870 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3874 /* Replace any outer-relation variables with nestloop params */
3875 if (best_path->path.param_info)
3877 joinclauses = (List *)
3878 replace_nestloop_params(root, (Node *) joinclauses);
3879 otherclauses = (List *)
3880 replace_nestloop_params(root, (Node *) otherclauses);
3884 * Identify any nestloop parameters that should be supplied by this join
3885 * node, and remove them from root->curOuterParams.
3887 outerrelids = best_path->outerjoinpath->parent->relids;
3888 nestParams = identify_current_nestloop_params(root, outerrelids);
3890 join_plan = make_nestloop(tlist,
3896 best_path->jointype,
3897 best_path->inner_unique);
3899 copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3905 create_mergejoin_plan(PlannerInfo *root,
3906 MergePath *best_path)
3908 MergeJoin *join_plan;
3911 List *tlist = build_path_tlist(root, &best_path->jpath.path);
3915 List *outerpathkeys;
3916 List *innerpathkeys;
3919 Oid *mergecollations;
3920 int *mergestrategies;
3921 bool *mergenullsfirst;
3923 EquivalenceClass *opeclass;
3928 Path *outer_path = best_path->jpath.outerjoinpath;
3929 Path *inner_path = best_path->jpath.innerjoinpath;
3932 * MergeJoin can project, so we don't have to demand exact tlists from the
3933 * inputs. However, if we're intending to sort an input's result, it's
3934 * best to request a small tlist so we aren't sorting more data than
3937 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
3938 (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3940 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
3941 (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3943 /* Sort join qual clauses into best execution order */
3944 /* NB: do NOT reorder the mergeclauses */
3945 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
3947 /* Get the join qual clauses (in plain expression form) */
3948 /* Any pseudoconstant clauses are ignored here */
3949 if (IS_OUTER_JOIN(best_path->jpath.jointype))
3951 extract_actual_join_clauses(joinclauses,
3952 best_path->jpath.path.parent->relids,
3953 &joinclauses, &otherclauses);
3957 /* We can treat all clauses alike for an inner join */
3958 joinclauses = extract_actual_clauses(joinclauses, false);
3963 * Remove the mergeclauses from the list of join qual clauses, leaving the
3964 * list of quals that must be checked as qpquals.
3966 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
3967 joinclauses = list_difference(joinclauses, mergeclauses);
3970 * Replace any outer-relation variables with nestloop params. There
3971 * should not be any in the mergeclauses.
3973 if (best_path->jpath.path.param_info)
3975 joinclauses = (List *)
3976 replace_nestloop_params(root, (Node *) joinclauses);
3977 otherclauses = (List *)
3978 replace_nestloop_params(root, (Node *) otherclauses);
3982 * Rearrange mergeclauses, if needed, so that the outer variable is always
3983 * on the left; mark the mergeclause restrictinfos with correct
3984 * outer_is_left status.
3986 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
3987 best_path->jpath.outerjoinpath->parent->relids);
3990 * Create explicit sort nodes for the outer and inner paths if necessary.
3992 if (best_path->outersortkeys)
3994 Relids outer_relids = outer_path->parent->relids;
3995 Sort *sort = make_sort_from_pathkeys(outer_plan,
3996 best_path->outersortkeys,
3999 label_sort_with_costsize(root, sort, -1.0);
4000 outer_plan = (Plan *) sort;
4001 outerpathkeys = best_path->outersortkeys;
4004 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
4006 if (best_path->innersortkeys)
4008 Relids inner_relids = inner_path->parent->relids;
4009 Sort *sort = make_sort_from_pathkeys(inner_plan,
4010 best_path->innersortkeys,
4013 label_sort_with_costsize(root, sort, -1.0);
4014 inner_plan = (Plan *) sort;
4015 innerpathkeys = best_path->innersortkeys;
4018 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
4021 * If specified, add a materialize node to shield the inner plan from the
4022 * need to handle mark/restore.
4024 if (best_path->materialize_inner)
4026 Plan *matplan = (Plan *) make_material(inner_plan);
4029 * We assume the materialize will not spill to disk, and therefore
4030 * charge just cpu_operator_cost per tuple. (Keep this estimate in
4031 * sync with final_cost_mergejoin.)
4033 copy_plan_costsize(matplan, inner_plan);
4034 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4036 inner_plan = matplan;
4040 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4041 * executor. The information is in the pathkeys for the two inputs, but
4042 * we need to be careful about the possibility of mergeclauses sharing a
4043 * pathkey, as well as the possibility that the inner pathkeys are not in
4044 * an order matching the mergeclauses.
4046 nClauses = list_length(mergeclauses);
4047 Assert(nClauses == list_length(best_path->path_mergeclauses));
4048 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4049 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4050 mergestrategies = (int *) palloc(nClauses * sizeof(int));
4051 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4055 lop = list_head(outerpathkeys);
4056 lip = list_head(innerpathkeys);
4058 foreach(lc, best_path->path_mergeclauses)
4060 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
4061 EquivalenceClass *oeclass;
4062 EquivalenceClass *ieclass;
4063 PathKey *ipathkey = NULL;
4064 EquivalenceClass *ipeclass = NULL;
4065 bool first_inner_match = false;
4067 /* fetch outer/inner eclass from mergeclause */
4068 if (rinfo->outer_is_left)
4070 oeclass = rinfo->left_ec;
4071 ieclass = rinfo->right_ec;
4075 oeclass = rinfo->right_ec;
4076 ieclass = rinfo->left_ec;
4078 Assert(oeclass != NULL);
4079 Assert(ieclass != NULL);
4082 * We must identify the pathkey elements associated with this clause
4083 * by matching the eclasses (which should give a unique match, since
4084 * the pathkey lists should be canonical). In typical cases the merge
4085 * clauses are one-to-one with the pathkeys, but when dealing with
4086 * partially redundant query conditions, things are more complicated.
4088 * lop and lip reference the first as-yet-unmatched pathkey elements.
4089 * If they're NULL then all pathkey elements have been matched.
4091 * The ordering of the outer pathkeys should match the mergeclauses,
4092 * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4093 * could be more than one mergeclause for the same outer pathkey, but
4094 * no pathkey may be entirely skipped over.
4096 if (oeclass != opeclass) /* multiple matches are not interesting */
4098 /* doesn't match the current opathkey, so must match the next */
4100 elog(ERROR, "outer pathkeys do not match mergeclauses");
4101 opathkey = (PathKey *) lfirst(lop);
4102 opeclass = opathkey->pk_eclass;
4104 if (oeclass != opeclass)
4105 elog(ERROR, "outer pathkeys do not match mergeclauses");
4109 * The inner pathkeys likewise should not have skipped-over keys, but
4110 * it's possible for a mergeclause to reference some earlier inner
4111 * pathkey if we had redundant pathkeys. For example we might have
4112 * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4113 * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4114 * mechanism drops the second sort by x as redundant, and this code
4117 * It's also possible for the implied inner-rel ordering to be like
4118 * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4119 * redundant; but this means that the sort ordering of a redundant
4120 * inner pathkey should not be considered significant. So we must
4121 * detect whether this is the first clause matching an inner pathkey.
4125 ipathkey = (PathKey *) lfirst(lip);
4126 ipeclass = ipathkey->pk_eclass;
4127 if (ieclass == ipeclass)
4129 /* successful first match to this inner pathkey */
4131 first_inner_match = true;
4134 if (!first_inner_match)
4136 /* redundant clause ... must match something before lip */
4139 foreach(l2, innerpathkeys)
4143 ipathkey = (PathKey *) lfirst(l2);
4144 ipeclass = ipathkey->pk_eclass;
4145 if (ieclass == ipeclass)
4148 if (ieclass != ipeclass)
4149 elog(ERROR, "inner pathkeys do not match mergeclauses");
4153 * The pathkeys should always match each other as to opfamily and
4154 * collation (which affect equality), but if we're considering a
4155 * redundant inner pathkey, its sort ordering might not match. In
4156 * such cases we may ignore the inner pathkey's sort ordering and use
4157 * the outer's. (In effect, we're lying to the executor about the
4158 * sort direction of this inner column, but it does not matter since
4159 * the run-time row comparisons would only reach this column when
4160 * there's equality for the earlier column containing the same eclass.
4161 * There could be only one value in this column for the range of inner
4162 * rows having a given value in the earlier column, so it does not
4163 * matter which way we imagine this column to be ordered.) But a
4164 * non-redundant inner pathkey had better match outer's ordering too.
4166 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4167 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4168 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4169 if (first_inner_match &&
4170 (opathkey->pk_strategy != ipathkey->pk_strategy ||
4171 opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4172 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4174 /* OK, save info for executor */
4175 mergefamilies[i] = opathkey->pk_opfamily;
4176 mergecollations[i] = opathkey->pk_eclass->ec_collation;
4177 mergestrategies[i] = opathkey->pk_strategy;
4178 mergenullsfirst[i] = opathkey->pk_nulls_first;
4183 * Note: it is not an error if we have additional pathkey elements (i.e.,
4184 * lop or lip isn't NULL here). The input paths might be better-sorted
4185 * than we need for the current mergejoin.
4189 * Now we can build the mergejoin node.
4191 join_plan = make_mergejoin(tlist,
4201 best_path->jpath.jointype,
4202 best_path->jpath.inner_unique,
4203 best_path->skip_mark_restore);
4205 /* Costs of sort and material steps are included in path cost already */
4206 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4212 create_hashjoin_plan(PlannerInfo *root,
4213 HashPath *best_path)
4215 HashJoin *join_plan;
4219 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4223 Oid skewTable = InvalidOid;
4224 AttrNumber skewColumn = InvalidAttrNumber;
4225 bool skewInherit = false;
4228 * HashJoin can project, so we don't have to demand exact tlists from the
4229 * inputs. However, it's best to request a small tlist from the inner
4230 * side, so that we aren't storing more data than necessary. Likewise, if
4231 * we anticipate batching, request a small tlist from the outer side so
4232 * that we don't put extra data in the outer batch files.
4234 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4235 (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4237 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4240 /* Sort join qual clauses into best execution order */
4241 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4242 /* There's no point in sorting the hash clauses ... */
4244 /* Get the join qual clauses (in plain expression form) */
4245 /* Any pseudoconstant clauses are ignored here */
4246 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4248 extract_actual_join_clauses(joinclauses,
4249 best_path->jpath.path.parent->relids,
4250 &joinclauses, &otherclauses);
4254 /* We can treat all clauses alike for an inner join */
4255 joinclauses = extract_actual_clauses(joinclauses, false);
4260 * Remove the hashclauses from the list of join qual clauses, leaving the
4261 * list of quals that must be checked as qpquals.
4263 hashclauses = get_actual_clauses(best_path->path_hashclauses);
4264 joinclauses = list_difference(joinclauses, hashclauses);
4267 * Replace any outer-relation variables with nestloop params. There
4268 * should not be any in the hashclauses.
4270 if (best_path->jpath.path.param_info)
4272 joinclauses = (List *)
4273 replace_nestloop_params(root, (Node *) joinclauses);
4274 otherclauses = (List *)
4275 replace_nestloop_params(root, (Node *) otherclauses);
4279 * Rearrange hashclauses, if needed, so that the outer variable is always
4282 hashclauses = get_switched_clauses(best_path->path_hashclauses,
4283 best_path->jpath.outerjoinpath->parent->relids);
4286 * If there is a single join clause and we can identify the outer variable
4287 * as a simple column reference, supply its identity for possible use in
4288 * skew optimization. (Note: in principle we could do skew optimization
4289 * with multiple join clauses, but we'd have to be able to determine the
4290 * most common combinations of outer values, which we don't currently have
4291 * enough stats for.)
4293 if (list_length(hashclauses) == 1)
4295 OpExpr *clause = (OpExpr *) linitial(hashclauses);
4298 Assert(is_opclause(clause));
4299 node = (Node *) linitial(clause->args);
4300 if (IsA(node, RelabelType))
4301 node = (Node *) ((RelabelType *) node)->arg;
4304 Var *var = (Var *) node;
4307 rte = root->simple_rte_array[var->varno];
4308 if (rte->rtekind == RTE_RELATION)
4310 skewTable = rte->relid;
4311 skewColumn = var->varattno;
4312 skewInherit = rte->inh;
4318 * Build the hash node and hash join node.
4320 hash_plan = make_hash(inner_plan,
4326 * Set Hash node's startup & total costs equal to total cost of input
4327 * plan; this only affects EXPLAIN display not decisions.
4329 copy_plan_costsize(&hash_plan->plan, inner_plan);
4330 hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4333 * If parallel-aware, the executor will also need an estimate of the total
4334 * number of rows expected from all participants so that it can size the
4335 * shared hash table.
4337 if (best_path->jpath.path.parallel_aware)
4339 hash_plan->plan.parallel_aware = true;
4340 hash_plan->rows_total = best_path->inner_rows_total;
4343 join_plan = make_hashjoin(tlist,
4349 best_path->jpath.jointype,
4350 best_path->jpath.inner_unique);
4352 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4358 /*****************************************************************************
4360 * SUPPORTING ROUTINES
4362 *****************************************************************************/
4365 * replace_nestloop_params
4366 * Replace outer-relation Vars and PlaceHolderVars in the given expression
4367 * with nestloop Params
4369 * All Vars and PlaceHolderVars belonging to the relation(s) identified by
4370 * root->curOuterRels are replaced by Params, and entries are added to
4371 * root->curOuterParams if not already present.
4374 replace_nestloop_params(PlannerInfo *root, Node *expr)
4376 /* No setup needed for tree walk, so away we go */
4377 return replace_nestloop_params_mutator(expr, root);
4381 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
4387 Var *var = (Var *) node;
4389 /* Upper-level Vars should be long gone at this point */
4390 Assert(var->varlevelsup == 0);
4391 /* If not to be replaced, we can just return the Var unmodified */
4392 if (!bms_is_member(var->varno, root->curOuterRels))
4394 /* Replace the Var with a nestloop Param */
4395 return (Node *) replace_nestloop_param_var(root, var);
4397 if (IsA(node, PlaceHolderVar))
4399 PlaceHolderVar *phv = (PlaceHolderVar *) node;
4401 /* Upper-level PlaceHolderVars should be long gone at this point */
4402 Assert(phv->phlevelsup == 0);
4405 * Check whether we need to replace the PHV. We use bms_overlap as a
4406 * cheap/quick test to see if the PHV might be evaluated in the outer
4407 * rels, and then grab its PlaceHolderInfo to tell for sure.
4409 if (!bms_overlap(phv->phrels, root->curOuterRels) ||
4410 !bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
4411 root->curOuterRels))
4414 * We can't replace the whole PHV, but we might still need to
4415 * replace Vars or PHVs within its expression, in case it ends up
4416 * actually getting evaluated here. (It might get evaluated in
4417 * this plan node, or some child node; in the latter case we don't
4418 * really need to process the expression here, but we haven't got
4419 * enough info to tell if that's the case.) Flat-copy the PHV
4420 * node and then recurse on its expression.
4422 * Note that after doing this, we might have different
4423 * representations of the contents of the same PHV in different
4424 * parts of the plan tree. This is OK because equal() will just
4425 * match on phid/phlevelsup, so setrefs.c will still recognize an
4426 * upper-level reference to a lower-level copy of the same PHV.
4428 PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
4430 memcpy(newphv, phv, sizeof(PlaceHolderVar));
4431 newphv->phexpr = (Expr *)
4432 replace_nestloop_params_mutator((Node *) phv->phexpr,
4434 return (Node *) newphv;
4436 /* Replace the PlaceHolderVar with a nestloop Param */
4437 return (Node *) replace_nestloop_param_placeholdervar(root, phv);
4439 return expression_tree_mutator(node,
4440 replace_nestloop_params_mutator,
4445 * fix_indexqual_references
4446 * Adjust indexqual clauses to the form the executor's indexqual
4449 * We have four tasks here:
4450 * * Remove RestrictInfo nodes from the input clauses.
4451 * * Replace any outer-relation Var or PHV nodes with nestloop Params.
4452 * (XXX eventually, that responsibility should go elsewhere?)
4453 * * Index keys must be represented by Var nodes with varattno set to the
4454 * index's attribute number, not the attribute number in the original rel.
4455 * * If the index key is on the right, commute the clause to put it on the
4458 * The result is a modified copy of the path's indexquals list --- the
4459 * original is not changed. Note also that the copy shares no substructure
4460 * with the original; this is needed in case there is a subplan in it (we need
4461 * two separate copies of the subplan tree, or things will go awry).
4464 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
4466 IndexOptInfo *index = index_path->indexinfo;
4467 List *fixed_indexquals;
4471 fixed_indexquals = NIL;
4473 forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
4475 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lcc);
4476 int indexcol = lfirst_int(lci);
4480 * Replace any outer-relation variables with nestloop params.
4482 * This also makes a copy of the clause, so it's safe to modify it
4485 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
4487 if (IsA(clause, OpExpr))
4489 OpExpr *op = (OpExpr *) clause;
4491 if (list_length(op->args) != 2)
4492 elog(ERROR, "indexqual clause is not binary opclause");
4495 * Check to see if the indexkey is on the right; if so, commute
4496 * the clause. The indexkey should be the side that refers to
4497 * (only) the base relation.
4499 if (!bms_equal(rinfo->left_relids, index->rel->relids))
4503 * Now replace the indexkey expression with an index Var.
4505 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
4509 else if (IsA(clause, RowCompareExpr))
4511 RowCompareExpr *rc = (RowCompareExpr *) clause;
4519 * Re-discover which index columns are used in the rowcompare.
4521 newrc = adjust_rowcompare_for_index(rc,
4528 * Trouble if adjust_rowcompare_for_index thought the
4529 * RowCompareExpr didn't match the index as-is; the clause should
4530 * have gone through that routine already.
4532 if (newrc != (Expr *) rc)
4533 elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
4536 * Check to see if the indexkey is on the right; if so, commute
4540 CommuteRowCompareExpr(rc);
4543 * Now replace the indexkey expressions with index Vars.
4545 Assert(list_length(rc->largs) == list_length(indexcolnos));
4546 forboth(lca, rc->largs, lcai, indexcolnos)
4548 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
4553 else if (IsA(clause, ScalarArrayOpExpr))
4555 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
4557 /* Never need to commute... */
4559 /* Replace the indexkey expression with an index Var. */
4560 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
4564 else if (IsA(clause, NullTest))
4566 NullTest *nt = (NullTest *) clause;
4568 /* Replace the indexkey expression with an index Var. */
4569 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
4574 elog(ERROR, "unsupported indexqual type: %d",
4575 (int) nodeTag(clause));
4577 fixed_indexquals = lappend(fixed_indexquals, clause);
4580 return fixed_indexquals;
4584 * fix_indexorderby_references
4585 * Adjust indexorderby clauses to the form the executor's index
4588 * This is a simplified version of fix_indexqual_references. The input does
4589 * not have RestrictInfo nodes, and we assume that indxpath.c already
4590 * commuted the clauses to put the index keys on the left. Also, we don't
4591 * bother to support any cases except simple OpExprs, since nothing else
4592 * is allowed for ordering operators.
4595 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
4597 IndexOptInfo *index = index_path->indexinfo;
4598 List *fixed_indexorderbys;
4602 fixed_indexorderbys = NIL;
4604 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
4606 Node *clause = (Node *) lfirst(lcc);
4607 int indexcol = lfirst_int(lci);
4610 * Replace any outer-relation variables with nestloop params.
4612 * This also makes a copy of the clause, so it's safe to modify it
4615 clause = replace_nestloop_params(root, clause);
4617 if (IsA(clause, OpExpr))
4619 OpExpr *op = (OpExpr *) clause;
4621 if (list_length(op->args) != 2)
4622 elog(ERROR, "indexorderby clause is not binary opclause");
4625 * Now replace the indexkey expression with an index Var.
4627 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
4632 elog(ERROR, "unsupported indexorderby type: %d",
4633 (int) nodeTag(clause));
4635 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
4638 return fixed_indexorderbys;
4642 * fix_indexqual_operand
4643 * Convert an indexqual expression to a Var referencing the index column.
4645 * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
4646 * equal to the index's attribute number (index column position).
4648 * Most of the code here is just for sanity cross-checking that the given
4649 * expression actually matches the index column it's claimed to.
4652 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
4656 ListCell *indexpr_item;
4659 * Remove any binary-compatible relabeling of the indexkey
4661 if (IsA(node, RelabelType))
4662 node = (Node *) ((RelabelType *) node)->arg;
4664 Assert(indexcol >= 0 && indexcol < index->ncolumns);
4666 if (index->indexkeys[indexcol] != 0)
4668 /* It's a simple index column */
4669 if (IsA(node, Var) &&
4670 ((Var *) node)->varno == index->rel->relid &&
4671 ((Var *) node)->varattno == index->indexkeys[indexcol])
4673 result = (Var *) copyObject(node);
4674 result->varno = INDEX_VAR;
4675 result->varattno = indexcol + 1;
4676 return (Node *) result;
4679 elog(ERROR, "index key does not match expected index column");
4682 /* It's an index expression, so find and cross-check the expression */
4683 indexpr_item = list_head(index->indexprs);
4684 for (pos = 0; pos < index->ncolumns; pos++)
4686 if (index->indexkeys[pos] == 0)
4688 if (indexpr_item == NULL)
4689 elog(ERROR, "too few entries in indexprs list");
4690 if (pos == indexcol)
4694 indexkey = (Node *) lfirst(indexpr_item);
4695 if (indexkey && IsA(indexkey, RelabelType))
4696 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4697 if (equal(node, indexkey))
4699 result = makeVar(INDEX_VAR, indexcol + 1,
4700 exprType(lfirst(indexpr_item)), -1,
4701 exprCollation(lfirst(indexpr_item)),
4703 return (Node *) result;
4706 elog(ERROR, "index key does not match expected index column");
4708 indexpr_item = lnext(indexpr_item);
4713 elog(ERROR, "index key does not match expected index column");
4714 return NULL; /* keep compiler quiet */
4718 * get_switched_clauses
4719 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
4720 * extract the bare clauses, and rearrange the elements within the
4721 * clauses, if needed, so the outer join variable is on the left and
4722 * the inner is on the right. The original clause data structure is not
4723 * touched; a modified list is returned. We do, however, set the transient
4724 * outer_is_left field in each RestrictInfo to show which side was which.
4727 get_switched_clauses(List *clauses, Relids outerrelids)
4734 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
4735 OpExpr *clause = (OpExpr *) restrictinfo->clause;
4737 Assert(is_opclause(clause));
4738 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
4741 * Duplicate just enough of the structure to allow commuting the
4742 * clause without changing the original list. Could use
4743 * copyObject, but a complete deep copy is overkill.
4745 OpExpr *temp = makeNode(OpExpr);
4747 temp->opno = clause->opno;
4748 temp->opfuncid = InvalidOid;
4749 temp->opresulttype = clause->opresulttype;
4750 temp->opretset = clause->opretset;
4751 temp->opcollid = clause->opcollid;
4752 temp->inputcollid = clause->inputcollid;
4753 temp->args = list_copy(clause->args);
4754 temp->location = clause->location;
4755 /* Commute it --- note this modifies the temp node in-place. */
4756 CommuteOpExpr(temp);
4757 t_list = lappend(t_list, temp);
4758 restrictinfo->outer_is_left = false;
4762 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
4763 t_list = lappend(t_list, clause);
4764 restrictinfo->outer_is_left = true;
4771 * order_qual_clauses
4772 * Given a list of qual clauses that will all be evaluated at the same
4773 * plan node, sort the list into the order we want to check the quals
4776 * When security barrier quals are used in the query, we may have quals with
4777 * different security levels in the list. Quals of lower security_level
4778 * must go before quals of higher security_level, except that we can grant
4779 * exceptions to move up quals that are leakproof. When security level
4780 * doesn't force the decision, we prefer to order clauses by estimated
4781 * execution cost, cheapest first.
4783 * Ideally the order should be driven by a combination of execution cost and
4784 * selectivity, but it's not immediately clear how to account for both,
4785 * and given the uncertainty of the estimates the reliability of the decisions
4786 * would be doubtful anyway. So we just order by security level then
4787 * estimated per-tuple cost, being careful not to change the order when
4788 * (as is often the case) the estimates are identical.
4790 * Although this will work on either bare clauses or RestrictInfos, it's
4791 * much faster to apply it to RestrictInfos, since it can re-use cost
4792 * information that is cached in RestrictInfos. XXX in the bare-clause
4793 * case, we are also not able to apply security considerations. That is
4794 * all right for the moment, because the bare-clause case doesn't occur
4795 * anywhere that barrier quals could be present, but it would be better to
4798 * Note: some callers pass lists that contain entries that will later be
4799 * removed; this is the easiest way to let this routine see RestrictInfos
4800 * instead of bare clauses. This is another reason why trying to consider
4801 * selectivity in the ordering would likely do the wrong thing.
4804 order_qual_clauses(PlannerInfo *root, List *clauses)
4810 Index security_level;
4812 int nitems = list_length(clauses);
4818 /* No need to work hard for 0 or 1 clause */
4823 * Collect the items and costs into an array. This is to avoid repeated
4824 * cost_qual_eval work if the inputs aren't RestrictInfos.
4826 items = (QualItem *) palloc(nitems * sizeof(QualItem));
4828 foreach(lc, clauses)
4830 Node *clause = (Node *) lfirst(lc);
4833 cost_qual_eval_node(&qcost, clause, root);
4834 items[i].clause = clause;
4835 items[i].cost = qcost.per_tuple;
4836 if (IsA(clause, RestrictInfo))
4838 RestrictInfo *rinfo = (RestrictInfo *) clause;
4841 * If a clause is leakproof, it doesn't have to be constrained by
4842 * its nominal security level. If it's also reasonably cheap
4843 * (here defined as 10X cpu_operator_cost), pretend it has
4844 * security_level 0, which will allow it to go in front of
4845 * more-expensive quals of lower security levels. Of course, that
4846 * will also force it to go in front of cheaper quals of its own
4847 * security level, which is not so great, but we can alleviate
4848 * that risk by applying the cost limit cutoff.
4850 if (rinfo->leakproof && items[i].cost < 10 * cpu_operator_cost)
4851 items[i].security_level = 0;
4853 items[i].security_level = rinfo->security_level;
4856 items[i].security_level = 0;
4861 * Sort. We don't use qsort() because it's not guaranteed stable for
4862 * equal keys. The expected number of entries is small enough that a
4863 * simple insertion sort should be good enough.
4865 for (i = 1; i < nitems; i++)
4867 QualItem newitem = items[i];
4870 /* insert newitem into the already-sorted subarray */
4871 for (j = i; j > 0; j--)
4873 QualItem *olditem = &items[j - 1];
4875 if (newitem.security_level > olditem->security_level ||
4876 (newitem.security_level == olditem->security_level &&
4877 newitem.cost >= olditem->cost))
4879 items[j] = *olditem;
4884 /* Convert back to a list */
4886 for (i = 0; i < nitems; i++)
4887 result = lappend(result, items[i].clause);
4893 * Copy cost and size info from a Path node to the Plan node created from it.
4894 * The executor usually won't use this info, but it's needed by EXPLAIN.
4895 * Also copy the parallel-related flags, which the executor *will* use.
4898 copy_generic_path_info(Plan *dest, Path *src)
4900 dest->startup_cost = src->startup_cost;
4901 dest->total_cost = src->total_cost;
4902 dest->plan_rows = src->rows;
4903 dest->plan_width = src->pathtarget->width;
4904 dest->parallel_aware = src->parallel_aware;
4905 dest->parallel_safe = src->parallel_safe;
4909 * Copy cost and size info from a lower plan node to an inserted node.
4910 * (Most callers alter the info after copying it.)
4913 copy_plan_costsize(Plan *dest, Plan *src)
4915 dest->startup_cost = src->startup_cost;
4916 dest->total_cost = src->total_cost;
4917 dest->plan_rows = src->plan_rows;
4918 dest->plan_width = src->plan_width;
4919 /* Assume the inserted node is not parallel-aware. */
4920 dest->parallel_aware = false;
4921 /* Assume the inserted node is parallel-safe, if child plan is. */
4922 dest->parallel_safe = src->parallel_safe;
4926 * Some places in this file build Sort nodes that don't have a directly
4927 * corresponding Path node. The cost of the sort is, or should have been,
4928 * included in the cost of the Path node we're working from, but since it's
4929 * not split out, we have to re-figure it using cost_sort(). This is just
4930 * to label the Sort node nicely for EXPLAIN.
4932 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
4935 label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
4937 Plan *lefttree = plan->plan.lefttree;
4938 Path sort_path; /* dummy for result of cost_sort */
4940 cost_sort(&sort_path, root, NIL,
4941 lefttree->total_cost,
4942 lefttree->plan_rows,
4943 lefttree->plan_width,
4947 plan->plan.startup_cost = sort_path.startup_cost;
4948 plan->plan.total_cost = sort_path.total_cost;
4949 plan->plan.plan_rows = lefttree->plan_rows;
4950 plan->plan.plan_width = lefttree->plan_width;
4951 plan->plan.parallel_aware = false;
4952 plan->plan.parallel_safe = lefttree->parallel_safe;
4956 * bitmap_subplan_mark_shared
4957 * Set isshared flag in bitmap subplan so that it will be created in
4961 bitmap_subplan_mark_shared(Plan *plan)
4963 if (IsA(plan, BitmapAnd))
4964 bitmap_subplan_mark_shared(
4965 linitial(((BitmapAnd *) plan)->bitmapplans));
4966 else if (IsA(plan, BitmapOr))
4968 ((BitmapOr *) plan)->isshared = true;
4969 bitmap_subplan_mark_shared(
4970 linitial(((BitmapOr *) plan)->bitmapplans));
4972 else if (IsA(plan, BitmapIndexScan))
4973 ((BitmapIndexScan *) plan)->isshared = true;
4975 elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
4978 /*****************************************************************************
4980 * PLAN NODE BUILDING ROUTINES
4982 * In general, these functions are not passed the original Path and therefore
4983 * leave it to the caller to fill in the cost/width fields from the Path,
4984 * typically by calling copy_generic_path_info(). This convention is
4985 * somewhat historical, but it does support a few places above where we build
4986 * a plan node without having an exactly corresponding Path node. Under no
4987 * circumstances should one of these functions do its own cost calculations,
4988 * as that would be redundant with calculations done while building Paths.
4990 *****************************************************************************/
4993 make_seqscan(List *qptlist,
4997 SeqScan *node = makeNode(SeqScan);
4998 Plan *plan = &node->plan;
5000 plan->targetlist = qptlist;
5001 plan->qual = qpqual;
5002 plan->lefttree = NULL;
5003 plan->righttree = NULL;
5004 node->scanrelid = scanrelid;
5010 make_samplescan(List *qptlist,
5013 TableSampleClause *tsc)
5015 SampleScan *node = makeNode(SampleScan);
5016 Plan *plan = &node->scan.plan;
5018 plan->targetlist = qptlist;
5019 plan->qual = qpqual;
5020 plan->lefttree = NULL;
5021 plan->righttree = NULL;
5022 node->scan.scanrelid = scanrelid;
5023 node->tablesample = tsc;
5029 make_indexscan(List *qptlist,
5034 List *indexqualorig,
5036 List *indexorderbyorig,
5037 List *indexorderbyops,
5038 ScanDirection indexscandir)
5040 IndexScan *node = makeNode(IndexScan);
5041 Plan *plan = &node->scan.plan;
5043 plan->targetlist = qptlist;
5044 plan->qual = qpqual;
5045 plan->lefttree = NULL;
5046 plan->righttree = NULL;
5047 node->scan.scanrelid = scanrelid;
5048 node->indexid = indexid;
5049 node->indexqual = indexqual;
5050 node->indexqualorig = indexqualorig;
5051 node->indexorderby = indexorderby;
5052 node->indexorderbyorig = indexorderbyorig;
5053 node->indexorderbyops = indexorderbyops;
5054 node->indexorderdir = indexscandir;
5059 static IndexOnlyScan *
5060 make_indexonlyscan(List *qptlist,
5067 ScanDirection indexscandir)
5069 IndexOnlyScan *node = makeNode(IndexOnlyScan);
5070 Plan *plan = &node->scan.plan;
5072 plan->targetlist = qptlist;
5073 plan->qual = qpqual;
5074 plan->lefttree = NULL;
5075 plan->righttree = NULL;
5076 node->scan.scanrelid = scanrelid;
5077 node->indexid = indexid;
5078 node->indexqual = indexqual;
5079 node->indexorderby = indexorderby;
5080 node->indextlist = indextlist;
5081 node->indexorderdir = indexscandir;
5086 static BitmapIndexScan *
5087 make_bitmap_indexscan(Index scanrelid,
5090 List *indexqualorig)
5092 BitmapIndexScan *node = makeNode(BitmapIndexScan);
5093 Plan *plan = &node->scan.plan;
5095 plan->targetlist = NIL; /* not used */
5096 plan->qual = NIL; /* not used */
5097 plan->lefttree = NULL;
5098 plan->righttree = NULL;
5099 node->scan.scanrelid = scanrelid;
5100 node->indexid = indexid;
5101 node->indexqual = indexqual;
5102 node->indexqualorig = indexqualorig;
5107 static BitmapHeapScan *
5108 make_bitmap_heapscan(List *qptlist,
5111 List *bitmapqualorig,
5114 BitmapHeapScan *node = makeNode(BitmapHeapScan);
5115 Plan *plan = &node->scan.plan;
5117 plan->targetlist = qptlist;
5118 plan->qual = qpqual;
5119 plan->lefttree = lefttree;
5120 plan->righttree = NULL;
5121 node->scan.scanrelid = scanrelid;
5122 node->bitmapqualorig = bitmapqualorig;
5128 make_tidscan(List *qptlist,
5133 TidScan *node = makeNode(TidScan);
5134 Plan *plan = &node->scan.plan;
5136 plan->targetlist = qptlist;
5137 plan->qual = qpqual;
5138 plan->lefttree = NULL;
5139 plan->righttree = NULL;
5140 node->scan.scanrelid = scanrelid;
5141 node->tidquals = tidquals;
5146 static SubqueryScan *
5147 make_subqueryscan(List *qptlist,
5152 SubqueryScan *node = makeNode(SubqueryScan);
5153 Plan *plan = &node->scan.plan;
5155 plan->targetlist = qptlist;
5156 plan->qual = qpqual;
5157 plan->lefttree = NULL;
5158 plan->righttree = NULL;
5159 node->scan.scanrelid = scanrelid;
5160 node->subplan = subplan;
5165 static FunctionScan *
5166 make_functionscan(List *qptlist,
5170 bool funcordinality)
5172 FunctionScan *node = makeNode(FunctionScan);
5173 Plan *plan = &node->scan.plan;
5175 plan->targetlist = qptlist;
5176 plan->qual = qpqual;
5177 plan->lefttree = NULL;
5178 plan->righttree = NULL;
5179 node->scan.scanrelid = scanrelid;
5180 node->functions = functions;
5181 node->funcordinality = funcordinality;
5186 static TableFuncScan *
5187 make_tablefuncscan(List *qptlist,
5190 TableFunc *tablefunc)
5192 TableFuncScan *node = makeNode(TableFuncScan);
5193 Plan *plan = &node->scan.plan;
5195 plan->targetlist = qptlist;
5196 plan->qual = qpqual;
5197 plan->lefttree = NULL;
5198 plan->righttree = NULL;
5199 node->scan.scanrelid = scanrelid;
5200 node->tablefunc = tablefunc;
5206 make_valuesscan(List *qptlist,
5211 ValuesScan *node = makeNode(ValuesScan);
5212 Plan *plan = &node->scan.plan;
5214 plan->targetlist = qptlist;
5215 plan->qual = qpqual;
5216 plan->lefttree = NULL;
5217 plan->righttree = NULL;
5218 node->scan.scanrelid = scanrelid;
5219 node->values_lists = values_lists;
5225 make_ctescan(List *qptlist,
5231 CteScan *node = makeNode(CteScan);
5232 Plan *plan = &node->scan.plan;
5234 plan->targetlist = qptlist;
5235 plan->qual = qpqual;
5236 plan->lefttree = NULL;
5237 plan->righttree = NULL;
5238 node->scan.scanrelid = scanrelid;
5239 node->ctePlanId = ctePlanId;
5240 node->cteParam = cteParam;
5245 static NamedTuplestoreScan *
5246 make_namedtuplestorescan(List *qptlist,
5251 NamedTuplestoreScan *node = makeNode(NamedTuplestoreScan);
5252 Plan *plan = &node->scan.plan;
5254 /* cost should be inserted by caller */
5255 plan->targetlist = qptlist;
5256 plan->qual = qpqual;
5257 plan->lefttree = NULL;
5258 plan->righttree = NULL;
5259 node->scan.scanrelid = scanrelid;
5260 node->enrname = enrname;
5265 static WorkTableScan *
5266 make_worktablescan(List *qptlist,
5271 WorkTableScan *node = makeNode(WorkTableScan);
5272 Plan *plan = &node->scan.plan;
5274 plan->targetlist = qptlist;
5275 plan->qual = qpqual;
5276 plan->lefttree = NULL;
5277 plan->righttree = NULL;
5278 node->scan.scanrelid = scanrelid;
5279 node->wtParam = wtParam;
5285 make_foreignscan(List *qptlist,
5290 List *fdw_scan_tlist,
5291 List *fdw_recheck_quals,
5294 ForeignScan *node = makeNode(ForeignScan);
5295 Plan *plan = &node->scan.plan;
5297 /* cost will be filled in by create_foreignscan_plan */
5298 plan->targetlist = qptlist;
5299 plan->qual = qpqual;
5300 plan->lefttree = outer_plan;
5301 plan->righttree = NULL;
5302 node->scan.scanrelid = scanrelid;
5303 node->operation = CMD_SELECT;
5304 /* fs_server will be filled in by create_foreignscan_plan */
5305 node->fs_server = InvalidOid;
5306 node->fdw_exprs = fdw_exprs;
5307 node->fdw_private = fdw_private;
5308 node->fdw_scan_tlist = fdw_scan_tlist;
5309 node->fdw_recheck_quals = fdw_recheck_quals;
5310 /* fs_relids will be filled in by create_foreignscan_plan */
5311 node->fs_relids = NULL;
5312 /* fsSystemCol will be filled in by create_foreignscan_plan */
5313 node->fsSystemCol = false;
5319 make_append(List *appendplans, int first_partial_plan,
5320 List *tlist, PartitionPruneInfo *partpruneinfo)
5322 Append *node = makeNode(Append);
5323 Plan *plan = &node->plan;
5325 plan->targetlist = tlist;
5327 plan->lefttree = NULL;
5328 plan->righttree = NULL;
5329 node->appendplans = appendplans;
5330 node->first_partial_plan = first_partial_plan;
5331 node->part_prune_info = partpruneinfo;
5335 static RecursiveUnion *
5336 make_recursive_union(List *tlist,
5343 RecursiveUnion *node = makeNode(RecursiveUnion);
5344 Plan *plan = &node->plan;
5345 int numCols = list_length(distinctList);
5347 plan->targetlist = tlist;
5349 plan->lefttree = lefttree;
5350 plan->righttree = righttree;
5351 node->wtParam = wtParam;
5354 * convert SortGroupClause list into arrays of attr indexes and equality
5355 * operators, as wanted by executor
5357 node->numCols = numCols;
5361 AttrNumber *dupColIdx;
5365 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
5366 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
5368 foreach(slitem, distinctList)
5370 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
5371 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
5374 dupColIdx[keyno] = tle->resno;
5375 dupOperators[keyno] = sortcl->eqop;
5376 Assert(OidIsValid(dupOperators[keyno]));
5379 node->dupColIdx = dupColIdx;
5380 node->dupOperators = dupOperators;
5382 node->numGroups = numGroups;
5388 make_bitmap_and(List *bitmapplans)
5390 BitmapAnd *node = makeNode(BitmapAnd);
5391 Plan *plan = &node->plan;
5393 plan->targetlist = NIL;
5395 plan->lefttree = NULL;
5396 plan->righttree = NULL;
5397 node->bitmapplans = bitmapplans;
5403 make_bitmap_or(List *bitmapplans)
5405 BitmapOr *node = makeNode(BitmapOr);
5406 Plan *plan = &node->plan;
5408 plan->targetlist = NIL;
5410 plan->lefttree = NULL;
5411 plan->righttree = NULL;
5412 node->bitmapplans = bitmapplans;
5418 make_nestloop(List *tlist,
5427 NestLoop *node = makeNode(NestLoop);
5428 Plan *plan = &node->join.plan;
5430 plan->targetlist = tlist;
5431 plan->qual = otherclauses;
5432 plan->lefttree = lefttree;
5433 plan->righttree = righttree;
5434 node->join.jointype = jointype;
5435 node->join.inner_unique = inner_unique;
5436 node->join.joinqual = joinclauses;
5437 node->nestParams = nestParams;
5443 make_hashjoin(List *tlist,
5452 HashJoin *node = makeNode(HashJoin);
5453 Plan *plan = &node->join.plan;
5455 plan->targetlist = tlist;
5456 plan->qual = otherclauses;
5457 plan->lefttree = lefttree;
5458 plan->righttree = righttree;
5459 node->hashclauses = hashclauses;
5460 node->join.jointype = jointype;
5461 node->join.inner_unique = inner_unique;
5462 node->join.joinqual = joinclauses;
5468 make_hash(Plan *lefttree,
5470 AttrNumber skewColumn,
5473 Hash *node = makeNode(Hash);
5474 Plan *plan = &node->plan;
5476 plan->targetlist = lefttree->targetlist;
5478 plan->lefttree = lefttree;
5479 plan->righttree = NULL;
5481 node->skewTable = skewTable;
5482 node->skewColumn = skewColumn;
5483 node->skewInherit = skewInherit;
5489 make_mergejoin(List *tlist,
5494 Oid *mergecollations,
5495 int *mergestrategies,
5496 bool *mergenullsfirst,
5501 bool skip_mark_restore)
5503 MergeJoin *node = makeNode(MergeJoin);
5504 Plan *plan = &node->join.plan;
5506 plan->targetlist = tlist;
5507 plan->qual = otherclauses;
5508 plan->lefttree = lefttree;
5509 plan->righttree = righttree;
5510 node->skip_mark_restore = skip_mark_restore;
5511 node->mergeclauses = mergeclauses;
5512 node->mergeFamilies = mergefamilies;
5513 node->mergeCollations = mergecollations;
5514 node->mergeStrategies = mergestrategies;
5515 node->mergeNullsFirst = mergenullsfirst;
5516 node->join.jointype = jointype;
5517 node->join.inner_unique = inner_unique;
5518 node->join.joinqual = joinclauses;
5524 * make_sort --- basic routine to build a Sort plan node
5526 * Caller must have built the sortColIdx, sortOperators, collations, and
5527 * nullsFirst arrays already.
5530 make_sort(Plan *lefttree, int numCols,
5531 AttrNumber *sortColIdx, Oid *sortOperators,
5532 Oid *collations, bool *nullsFirst)
5534 Sort *node = makeNode(Sort);
5535 Plan *plan = &node->plan;
5537 plan->targetlist = lefttree->targetlist;
5539 plan->lefttree = lefttree;
5540 plan->righttree = NULL;
5541 node->numCols = numCols;
5542 node->sortColIdx = sortColIdx;
5543 node->sortOperators = sortOperators;
5544 node->collations = collations;
5545 node->nullsFirst = nullsFirst;
5551 * prepare_sort_from_pathkeys
5552 * Prepare to sort according to given pathkeys
5554 * This is used to set up for Sort, MergeAppend, and Gather Merge nodes. It
5555 * calculates the executor's representation of the sort key information, and
5556 * adjusts the plan targetlist if needed to add resjunk sort columns.
5559 * 'lefttree' is the plan node which yields input tuples
5560 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
5561 * 'relids' identifies the child relation being sorted, if any
5562 * 'reqColIdx' is NULL or an array of required sort key column numbers
5563 * 'adjust_tlist_in_place' is true if lefttree must be modified in-place
5565 * We must convert the pathkey information into arrays of sort key column
5566 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
5567 * which is the representation the executor wants. These are returned into
5568 * the output parameters *p_numsortkeys etc.
5570 * When looking for matches to an EquivalenceClass's members, we will only
5571 * consider child EC members if they belong to given 'relids'. This protects
5572 * against possible incorrect matches to child expressions that contain no
5575 * If reqColIdx isn't NULL then it contains sort key column numbers that
5576 * we should match. This is used when making child plans for a MergeAppend;
5577 * it's an error if we can't match the columns.
5579 * If the pathkeys include expressions that aren't simple Vars, we will
5580 * usually need to add resjunk items to the input plan's targetlist to
5581 * compute these expressions, since a Sort or MergeAppend node itself won't
5582 * do any such calculations. If the input plan type isn't one that can do
5583 * projections, this means adding a Result node just to do the projection.
5584 * However, the caller can pass adjust_tlist_in_place = true to force the
5585 * lefttree tlist to be modified in-place regardless of whether the node type
5586 * can project --- we use this for fixing the tlist of MergeAppend itself.
5588 * Returns the node which is to be the input to the Sort (either lefttree,
5589 * or a Result stacked atop lefttree).
5592 prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
5594 const AttrNumber *reqColIdx,
5595 bool adjust_tlist_in_place,
5597 AttrNumber **p_sortColIdx,
5598 Oid **p_sortOperators,
5600 bool **p_nullsFirst)
5602 List *tlist = lefttree->targetlist;
5605 AttrNumber *sortColIdx;
5611 * We will need at most list_length(pathkeys) sort columns; possibly less
5613 numsortkeys = list_length(pathkeys);
5614 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5615 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5616 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5617 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5621 foreach(i, pathkeys)
5623 PathKey *pathkey = (PathKey *) lfirst(i);
5624 EquivalenceClass *ec = pathkey->pk_eclass;
5625 EquivalenceMember *em;
5626 TargetEntry *tle = NULL;
5627 Oid pk_datatype = InvalidOid;
5631 if (ec->ec_has_volatile)
5634 * If the pathkey's EquivalenceClass is volatile, then it must
5635 * have come from an ORDER BY clause, and we have to match it to
5636 * that same targetlist entry.
5638 if (ec->ec_sortref == 0) /* can't happen */
5639 elog(ERROR, "volatile EquivalenceClass has no sortref");
5640 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
5642 Assert(list_length(ec->ec_members) == 1);
5643 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
5645 else if (reqColIdx != NULL)
5648 * If we are given a sort column number to match, only consider
5649 * the single TLE at that position. It's possible that there is
5650 * no such TLE, in which case fall through and generate a resjunk
5651 * targetentry (we assume this must have happened in the parent
5652 * plan as well). If there is a TLE but it doesn't match the
5653 * pathkey's EC, we do the same, which is probably the wrong thing
5654 * but we'll leave it to caller to complain about the mismatch.
5656 tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
5659 em = find_ec_member_for_tle(ec, tle, relids);
5662 /* found expr at right place in tlist */
5663 pk_datatype = em->em_datatype;
5672 * Otherwise, we can sort by any non-constant expression listed in
5673 * the pathkey's EquivalenceClass. For now, we take the first
5674 * tlist item found in the EC. If there's no match, we'll generate
5675 * a resjunk entry using the first EC member that is an expression
5676 * in the input's vars. (The non-const restriction only matters
5677 * if the EC is below_outer_join; but if it isn't, it won't
5678 * contain consts anyway, else we'd have discarded the pathkey as
5681 * XXX if we have a choice, is there any way of figuring out which
5682 * might be cheapest to execute? (For example, int4lt is likely
5683 * much cheaper to execute than numericlt, but both might appear
5684 * in the same equivalence class...) Not clear that we ever will
5685 * have an interesting choice in practice, so it may not matter.
5689 tle = (TargetEntry *) lfirst(j);
5690 em = find_ec_member_for_tle(ec, tle, relids);
5693 /* found expr already in tlist */
5694 pk_datatype = em->em_datatype;
5704 * No matching tlist item; look for a computable expression. Note
5705 * that we treat Aggrefs as if they were variables; this is
5706 * necessary when attempting to sort the output from an Agg node
5707 * for use in a WindowFunc (since grouping_planner will have
5708 * treated the Aggrefs as variables, too). Likewise, if we find a
5709 * WindowFunc in a sort expression, treat it as a variable.
5711 Expr *sortexpr = NULL;
5713 foreach(j, ec->ec_members)
5715 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
5720 * We shouldn't be trying to sort by an equivalence class that
5721 * contains a constant, so no need to consider such cases any
5724 if (em->em_is_const)
5728 * Ignore child members unless they belong to the rel being
5731 if (em->em_is_child &&
5732 !bms_is_subset(em->em_relids, relids))
5735 sortexpr = em->em_expr;
5736 exprvars = pull_var_clause((Node *) sortexpr,
5737 PVC_INCLUDE_AGGREGATES |
5738 PVC_INCLUDE_WINDOWFUNCS |
5739 PVC_INCLUDE_PLACEHOLDERS);
5740 foreach(k, exprvars)
5742 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
5745 list_free(exprvars);
5748 pk_datatype = em->em_datatype;
5749 break; /* found usable expression */
5753 elog(ERROR, "could not find pathkey item to sort");
5756 * Do we need to insert a Result node?
5758 if (!adjust_tlist_in_place &&
5759 !is_projection_capable_plan(lefttree))
5761 /* copy needed so we don't modify input's tlist below */
5762 tlist = copyObject(tlist);
5763 lefttree = inject_projection_plan(lefttree, tlist,
5764 lefttree->parallel_safe);
5767 /* Don't bother testing is_projection_capable_plan again */
5768 adjust_tlist_in_place = true;
5771 * Add resjunk entry to input's tlist
5773 tle = makeTargetEntry(sortexpr,
5774 list_length(tlist) + 1,
5777 tlist = lappend(tlist, tle);
5778 lefttree->targetlist = tlist; /* just in case NIL before */
5782 * Look up the correct sort operator from the PathKey's slightly
5783 * abstracted representation.
5785 sortop = get_opfamily_member(pathkey->pk_opfamily,
5788 pathkey->pk_strategy);
5789 if (!OidIsValid(sortop)) /* should not happen */
5790 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
5791 pathkey->pk_strategy, pk_datatype, pk_datatype,
5792 pathkey->pk_opfamily);
5794 /* Add the column to the sort arrays */
5795 sortColIdx[numsortkeys] = tle->resno;
5796 sortOperators[numsortkeys] = sortop;
5797 collations[numsortkeys] = ec->ec_collation;
5798 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
5802 /* Return results */
5803 *p_numsortkeys = numsortkeys;
5804 *p_sortColIdx = sortColIdx;
5805 *p_sortOperators = sortOperators;
5806 *p_collations = collations;
5807 *p_nullsFirst = nullsFirst;
5813 * find_ec_member_for_tle
5814 * Locate an EquivalenceClass member matching the given TLE, if any
5816 * Child EC members are ignored unless they belong to given 'relids'.
5818 static EquivalenceMember *
5819 find_ec_member_for_tle(EquivalenceClass *ec,
5826 /* We ignore binary-compatible relabeling on both ends */
5828 while (tlexpr && IsA(tlexpr, RelabelType))
5829 tlexpr = ((RelabelType *) tlexpr)->arg;
5831 foreach(lc, ec->ec_members)
5833 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
5837 * We shouldn't be trying to sort by an equivalence class that
5838 * contains a constant, so no need to consider such cases any further.
5840 if (em->em_is_const)
5844 * Ignore child members unless they belong to the rel being sorted.
5846 if (em->em_is_child &&
5847 !bms_is_subset(em->em_relids, relids))
5850 /* Match if same expression (after stripping relabel) */
5851 emexpr = em->em_expr;
5852 while (emexpr && IsA(emexpr, RelabelType))
5853 emexpr = ((RelabelType *) emexpr)->arg;
5855 if (equal(emexpr, tlexpr))
5863 * make_sort_from_pathkeys
5864 * Create sort plan to sort according to given pathkeys
5866 * 'lefttree' is the node which yields input tuples
5867 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
5868 * 'relids' is the set of relations required by prepare_sort_from_pathkeys()
5871 make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
5874 AttrNumber *sortColIdx;
5879 /* Compute sort column info, and adjust lefttree as needed */
5880 lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
5890 /* Now build the Sort node */
5891 return make_sort(lefttree, numsortkeys,
5892 sortColIdx, sortOperators,
5893 collations, nullsFirst);
5897 * make_sort_from_sortclauses
5898 * Create sort plan to sort according to given sortclauses
5900 * 'sortcls' is a list of SortGroupClauses
5901 * 'lefttree' is the node which yields input tuples
5904 make_sort_from_sortclauses(List *sortcls, Plan *lefttree)
5906 List *sub_tlist = lefttree->targetlist;
5909 AttrNumber *sortColIdx;
5914 /* Convert list-ish representation to arrays wanted by executor */
5915 numsortkeys = list_length(sortcls);
5916 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5917 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5918 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5919 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5924 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
5925 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
5927 sortColIdx[numsortkeys] = tle->resno;
5928 sortOperators[numsortkeys] = sortcl->sortop;
5929 collations[numsortkeys] = exprCollation((Node *) tle->expr);
5930 nullsFirst[numsortkeys] = sortcl->nulls_first;
5934 return make_sort(lefttree, numsortkeys,
5935 sortColIdx, sortOperators,
5936 collations, nullsFirst);
5940 * make_sort_from_groupcols
5941 * Create sort plan to sort based on grouping columns
5943 * 'groupcls' is the list of SortGroupClauses
5944 * 'grpColIdx' gives the column numbers to use
5946 * This might look like it could be merged with make_sort_from_sortclauses,
5947 * but presently we *must* use the grpColIdx[] array to locate sort columns,
5948 * because the child plan's tlist is not marked with ressortgroupref info
5949 * appropriate to the grouping node. So, only the sort ordering info
5950 * is used from the SortGroupClause entries.
5953 make_sort_from_groupcols(List *groupcls,
5954 AttrNumber *grpColIdx,
5957 List *sub_tlist = lefttree->targetlist;
5960 AttrNumber *sortColIdx;
5965 /* Convert list-ish representation to arrays wanted by executor */
5966 numsortkeys = list_length(groupcls);
5967 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5968 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5969 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5970 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5973 foreach(l, groupcls)
5975 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
5976 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
5979 elog(ERROR, "could not retrieve tle for sort-from-groupcols");
5981 sortColIdx[numsortkeys] = tle->resno;
5982 sortOperators[numsortkeys] = grpcl->sortop;
5983 collations[numsortkeys] = exprCollation((Node *) tle->expr);
5984 nullsFirst[numsortkeys] = grpcl->nulls_first;
5988 return make_sort(lefttree, numsortkeys,
5989 sortColIdx, sortOperators,
5990 collations, nullsFirst);
5994 make_material(Plan *lefttree)
5996 Material *node = makeNode(Material);
5997 Plan *plan = &node->plan;
5999 plan->targetlist = lefttree->targetlist;
6001 plan->lefttree = lefttree;
6002 plan->righttree = NULL;
6008 * materialize_finished_plan: stick a Material node atop a completed plan
6010 * There are a couple of places where we want to attach a Material node
6011 * after completion of create_plan(), without any MaterialPath path.
6012 * Those places should probably be refactored someday to do this on the
6013 * Path representation, but it's not worth the trouble yet.
6016 materialize_finished_plan(Plan *subplan)
6019 Path matpath; /* dummy for result of cost_material */
6021 matplan = (Plan *) make_material(subplan);
6024 * XXX horrid kluge: if there are any initPlans attached to the subplan,
6025 * move them up to the Material node, which is now effectively the top
6026 * plan node in its query level. This prevents failure in
6027 * SS_finalize_plan(), which see for comments. We don't bother adjusting
6028 * the subplan's cost estimate for this.
6030 matplan->initPlan = subplan->initPlan;
6031 subplan->initPlan = NIL;
6034 cost_material(&matpath,
6035 subplan->startup_cost,
6036 subplan->total_cost,
6038 subplan->plan_width);
6039 matplan->startup_cost = matpath.startup_cost;
6040 matplan->total_cost = matpath.total_cost;
6041 matplan->plan_rows = subplan->plan_rows;
6042 matplan->plan_width = subplan->plan_width;
6043 matplan->parallel_aware = false;
6044 matplan->parallel_safe = subplan->parallel_safe;
6050 make_agg(List *tlist, List *qual,
6051 AggStrategy aggstrategy, AggSplit aggsplit,
6052 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
6053 List *groupingSets, List *chain,
6054 double dNumGroups, Plan *lefttree)
6056 Agg *node = makeNode(Agg);
6057 Plan *plan = &node->plan;
6060 /* Reduce to long, but 'ware overflow! */
6061 numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
6063 node->aggstrategy = aggstrategy;
6064 node->aggsplit = aggsplit;
6065 node->numCols = numGroupCols;
6066 node->grpColIdx = grpColIdx;
6067 node->grpOperators = grpOperators;
6068 node->numGroups = numGroups;
6069 node->aggParams = NULL; /* SS_finalize_plan() will fill this */
6070 node->groupingSets = groupingSets;
6071 node->chain = chain;
6074 plan->targetlist = tlist;
6075 plan->lefttree = lefttree;
6076 plan->righttree = NULL;
6082 make_windowagg(List *tlist, Index winref,
6083 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
6084 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
6085 int frameOptions, Node *startOffset, Node *endOffset,
6086 Oid startInRangeFunc, Oid endInRangeFunc,
6087 Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst,
6090 WindowAgg *node = makeNode(WindowAgg);
6091 Plan *plan = &node->plan;
6093 node->winref = winref;
6094 node->partNumCols = partNumCols;
6095 node->partColIdx = partColIdx;
6096 node->partOperators = partOperators;
6097 node->ordNumCols = ordNumCols;
6098 node->ordColIdx = ordColIdx;
6099 node->ordOperators = ordOperators;
6100 node->frameOptions = frameOptions;
6101 node->startOffset = startOffset;
6102 node->endOffset = endOffset;
6103 node->startInRangeFunc = startInRangeFunc;
6104 node->endInRangeFunc = endInRangeFunc;
6105 node->inRangeColl = inRangeColl;
6106 node->inRangeAsc = inRangeAsc;
6107 node->inRangeNullsFirst = inRangeNullsFirst;
6109 plan->targetlist = tlist;
6110 plan->lefttree = lefttree;
6111 plan->righttree = NULL;
6112 /* WindowAgg nodes never have a qual clause */
6119 make_group(List *tlist,
6122 AttrNumber *grpColIdx,
6126 Group *node = makeNode(Group);
6127 Plan *plan = &node->plan;
6129 node->numCols = numGroupCols;
6130 node->grpColIdx = grpColIdx;
6131 node->grpOperators = grpOperators;
6134 plan->targetlist = tlist;
6135 plan->lefttree = lefttree;
6136 plan->righttree = NULL;
6142 * distinctList is a list of SortGroupClauses, identifying the targetlist items
6143 * that should be considered by the Unique filter. The input path must
6144 * already be sorted accordingly.
6147 make_unique_from_sortclauses(Plan *lefttree, List *distinctList)
6149 Unique *node = makeNode(Unique);
6150 Plan *plan = &node->plan;
6151 int numCols = list_length(distinctList);
6153 AttrNumber *uniqColIdx;
6157 plan->targetlist = lefttree->targetlist;
6159 plan->lefttree = lefttree;
6160 plan->righttree = NULL;
6163 * convert SortGroupClause list into arrays of attr indexes and equality
6164 * operators, as wanted by executor
6166 Assert(numCols > 0);
6167 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6168 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6170 foreach(slitem, distinctList)
6172 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
6173 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
6175 uniqColIdx[keyno] = tle->resno;
6176 uniqOperators[keyno] = sortcl->eqop;
6177 Assert(OidIsValid(uniqOperators[keyno]));
6181 node->numCols = numCols;
6182 node->uniqColIdx = uniqColIdx;
6183 node->uniqOperators = uniqOperators;
6189 * as above, but use pathkeys to identify the sort columns and semantics
6192 make_unique_from_pathkeys(Plan *lefttree, List *pathkeys, int numCols)
6194 Unique *node = makeNode(Unique);
6195 Plan *plan = &node->plan;
6197 AttrNumber *uniqColIdx;
6201 plan->targetlist = lefttree->targetlist;
6203 plan->lefttree = lefttree;
6204 plan->righttree = NULL;
6207 * Convert pathkeys list into arrays of attr indexes and equality
6208 * operators, as wanted by executor. This has a lot in common with
6209 * prepare_sort_from_pathkeys ... maybe unify sometime?
6211 Assert(numCols >= 0 && numCols <= list_length(pathkeys));
6212 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6213 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6215 foreach(lc, pathkeys)
6217 PathKey *pathkey = (PathKey *) lfirst(lc);
6218 EquivalenceClass *ec = pathkey->pk_eclass;
6219 EquivalenceMember *em;
6220 TargetEntry *tle = NULL;
6221 Oid pk_datatype = InvalidOid;
6225 /* Ignore pathkeys beyond the specified number of columns */
6226 if (keyno >= numCols)
6229 if (ec->ec_has_volatile)
6232 * If the pathkey's EquivalenceClass is volatile, then it must
6233 * have come from an ORDER BY clause, and we have to match it to
6234 * that same targetlist entry.
6236 if (ec->ec_sortref == 0) /* can't happen */
6237 elog(ERROR, "volatile EquivalenceClass has no sortref");
6238 tle = get_sortgroupref_tle(ec->ec_sortref, plan->targetlist);
6240 Assert(list_length(ec->ec_members) == 1);
6241 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
6246 * Otherwise, we can use any non-constant expression listed in the
6247 * pathkey's EquivalenceClass. For now, we take the first tlist
6248 * item found in the EC.
6250 foreach(j, plan->targetlist)
6252 tle = (TargetEntry *) lfirst(j);
6253 em = find_ec_member_for_tle(ec, tle, NULL);
6256 /* found expr already in tlist */
6257 pk_datatype = em->em_datatype;
6265 elog(ERROR, "could not find pathkey item to sort");
6268 * Look up the correct equality operator from the PathKey's slightly
6269 * abstracted representation.
6271 eqop = get_opfamily_member(pathkey->pk_opfamily,
6274 BTEqualStrategyNumber);
6275 if (!OidIsValid(eqop)) /* should not happen */
6276 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
6277 BTEqualStrategyNumber, pk_datatype, pk_datatype,
6278 pathkey->pk_opfamily);
6280 uniqColIdx[keyno] = tle->resno;
6281 uniqOperators[keyno] = eqop;
6286 node->numCols = numCols;
6287 node->uniqColIdx = uniqColIdx;
6288 node->uniqOperators = uniqOperators;
6294 make_gather(List *qptlist,
6301 Gather *node = makeNode(Gather);
6302 Plan *plan = &node->plan;
6304 plan->targetlist = qptlist;
6305 plan->qual = qpqual;
6306 plan->lefttree = subplan;
6307 plan->righttree = NULL;
6308 node->num_workers = nworkers;
6309 node->rescan_param = rescan_param;
6310 node->single_copy = single_copy;
6311 node->invisible = false;
6312 node->initParam = NULL;
6318 * distinctList is a list of SortGroupClauses, identifying the targetlist
6319 * items that should be considered by the SetOp filter. The input path must
6320 * already be sorted accordingly.
6323 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
6324 List *distinctList, AttrNumber flagColIdx, int firstFlag,
6327 SetOp *node = makeNode(SetOp);
6328 Plan *plan = &node->plan;
6329 int numCols = list_length(distinctList);
6331 AttrNumber *dupColIdx;
6335 plan->targetlist = lefttree->targetlist;
6337 plan->lefttree = lefttree;
6338 plan->righttree = NULL;
6341 * convert SortGroupClause list into arrays of attr indexes and equality
6342 * operators, as wanted by executor
6344 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6345 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6347 foreach(slitem, distinctList)
6349 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
6350 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
6352 dupColIdx[keyno] = tle->resno;
6353 dupOperators[keyno] = sortcl->eqop;
6354 Assert(OidIsValid(dupOperators[keyno]));
6359 node->strategy = strategy;
6360 node->numCols = numCols;
6361 node->dupColIdx = dupColIdx;
6362 node->dupOperators = dupOperators;
6363 node->flagColIdx = flagColIdx;
6364 node->firstFlag = firstFlag;
6365 node->numGroups = numGroups;
6372 * Build a LockRows plan node
6375 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
6377 LockRows *node = makeNode(LockRows);
6378 Plan *plan = &node->plan;
6380 plan->targetlist = lefttree->targetlist;
6382 plan->lefttree = lefttree;
6383 plan->righttree = NULL;
6385 node->rowMarks = rowMarks;
6386 node->epqParam = epqParam;
6393 * Build a Limit plan node
6396 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
6398 Limit *node = makeNode(Limit);
6399 Plan *plan = &node->plan;
6401 plan->targetlist = lefttree->targetlist;
6403 plan->lefttree = lefttree;
6404 plan->righttree = NULL;
6406 node->limitOffset = limitOffset;
6407 node->limitCount = limitCount;
6414 * Build a Result plan node
6417 make_result(List *tlist,
6418 Node *resconstantqual,
6421 Result *node = makeNode(Result);
6422 Plan *plan = &node->plan;
6424 plan->targetlist = tlist;
6426 plan->lefttree = subplan;
6427 plan->righttree = NULL;
6428 node->resconstantqual = resconstantqual;
6435 * Build a ProjectSet plan node
6438 make_project_set(List *tlist,
6441 ProjectSet *node = makeNode(ProjectSet);
6442 Plan *plan = &node->plan;
6444 plan->targetlist = tlist;
6446 plan->lefttree = subplan;
6447 plan->righttree = NULL;
6454 * Build a ModifyTable plan node
6456 static ModifyTable *
6457 make_modifytable(PlannerInfo *root,
6458 CmdType operation, bool canSetTag,
6459 Index nominalRelation, Index rootRelation,
6460 bool partColsUpdated,
6461 List *resultRelations, List *subplans, List *subroots,
6462 List *withCheckOptionLists, List *returningLists,
6463 List *rowMarks, OnConflictExpr *onconflict, int epqParam)
6465 ModifyTable *node = makeNode(ModifyTable);
6466 List *fdw_private_list;
6467 Bitmapset *direct_modify_plans;
6472 Assert(list_length(resultRelations) == list_length(subplans));
6473 Assert(list_length(resultRelations) == list_length(subroots));
6474 Assert(withCheckOptionLists == NIL ||
6475 list_length(resultRelations) == list_length(withCheckOptionLists));
6476 Assert(returningLists == NIL ||
6477 list_length(resultRelations) == list_length(returningLists));
6479 node->plan.lefttree = NULL;
6480 node->plan.righttree = NULL;
6481 node->plan.qual = NIL;
6482 /* setrefs.c will fill in the targetlist, if needed */
6483 node->plan.targetlist = NIL;
6485 node->operation = operation;
6486 node->canSetTag = canSetTag;
6487 node->nominalRelation = nominalRelation;
6488 node->rootRelation = rootRelation;
6489 node->partColsUpdated = partColsUpdated;
6490 node->resultRelations = resultRelations;
6491 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
6492 node->rootResultRelIndex = -1; /* will be set correctly in setrefs.c */
6493 node->plans = subplans;
6496 node->onConflictAction = ONCONFLICT_NONE;
6497 node->onConflictSet = NIL;
6498 node->onConflictWhere = NULL;
6499 node->arbiterIndexes = NIL;
6500 node->exclRelRTI = 0;
6501 node->exclRelTlist = NIL;
6505 node->onConflictAction = onconflict->action;
6506 node->onConflictSet = onconflict->onConflictSet;
6507 node->onConflictWhere = onconflict->onConflictWhere;
6510 * If a set of unique index inference elements was provided (an
6511 * INSERT...ON CONFLICT "inference specification"), then infer
6512 * appropriate unique indexes (or throw an error if none are
6515 node->arbiterIndexes = infer_arbiter_indexes(root);
6517 node->exclRelRTI = onconflict->exclRelIndex;
6518 node->exclRelTlist = onconflict->exclRelTlist;
6520 node->withCheckOptionLists = withCheckOptionLists;
6521 node->returningLists = returningLists;
6522 node->rowMarks = rowMarks;
6523 node->epqParam = epqParam;
6526 * For each result relation that is a foreign table, allow the FDW to
6527 * construct private plan data, and accumulate it all into a list.
6529 fdw_private_list = NIL;
6530 direct_modify_plans = NULL;
6532 forboth(lc, resultRelations, lc2, subroots)
6534 Index rti = lfirst_int(lc);
6535 PlannerInfo *subroot = lfirst_node(PlannerInfo, lc2);
6536 FdwRoutine *fdwroutine;
6541 * If possible, we want to get the FdwRoutine from our RelOptInfo for
6542 * the table. But sometimes we don't have a RelOptInfo and must get
6543 * it the hard way. (In INSERT, the target relation is not scanned,
6544 * so it's not a baserel; and there are also corner cases for
6545 * updatable views where the target rel isn't a baserel.)
6547 if (rti < subroot->simple_rel_array_size &&
6548 subroot->simple_rel_array[rti] != NULL)
6550 RelOptInfo *resultRel = subroot->simple_rel_array[rti];
6552 fdwroutine = resultRel->fdwroutine;
6556 RangeTblEntry *rte = planner_rt_fetch(rti, subroot);
6558 Assert(rte->rtekind == RTE_RELATION);
6559 if (rte->relkind == RELKIND_FOREIGN_TABLE)
6560 fdwroutine = GetFdwRoutineByRelId(rte->relid);
6566 * Try to modify the foreign table directly if (1) the FDW provides
6567 * callback functions needed for that, (2) there are no row-level
6568 * triggers on the foreign table, and (3) there are no WITH CHECK
6569 * OPTIONs from parent views.
6571 direct_modify = false;
6572 if (fdwroutine != NULL &&
6573 fdwroutine->PlanDirectModify != NULL &&
6574 fdwroutine->BeginDirectModify != NULL &&
6575 fdwroutine->IterateDirectModify != NULL &&
6576 fdwroutine->EndDirectModify != NULL &&
6577 withCheckOptionLists == NIL &&
6578 !has_row_triggers(subroot, rti, operation))
6579 direct_modify = fdwroutine->PlanDirectModify(subroot, node, rti, i);
6581 direct_modify_plans = bms_add_member(direct_modify_plans, i);
6583 if (!direct_modify &&
6584 fdwroutine != NULL &&
6585 fdwroutine->PlanForeignModify != NULL)
6586 fdw_private = fdwroutine->PlanForeignModify(subroot, node, rti, i);
6589 fdw_private_list = lappend(fdw_private_list, fdw_private);
6592 node->fdwPrivLists = fdw_private_list;
6593 node->fdwDirectModifyPlans = direct_modify_plans;
6599 * is_projection_capable_path
6600 * Check whether a given Path node is able to do projection.
6603 is_projection_capable_path(Path *path)
6605 /* Most plan types can project, so just list the ones that can't */
6606 switch (path->pathtype)
6617 case T_RecursiveUnion:
6622 * Append can't project, but if it's being used to represent a
6623 * dummy path, claim that it can project. This prevents us from
6624 * converting a rel from dummy to non-dummy status by applying a
6625 * projection to its dummy path.
6627 return IS_DUMMY_PATH(path);
6631 * Although ProjectSet certainly projects, say "no" because we
6632 * don't want the planner to randomly replace its tlist with
6633 * something else; the SRFs have to stay at top level. This might
6634 * get relaxed later.
6644 * is_projection_capable_plan
6645 * Check whether a given Plan node is able to do projection.
6648 is_projection_capable_plan(Plan *plan)
6650 /* Most plan types can project, so just list the ones that can't */
6651 switch (nodeTag(plan))
6663 case T_RecursiveUnion:
6668 * Although ProjectSet certainly projects, say "no" because we
6669 * don't want the planner to randomly replace its tlist with
6670 * something else; the SRFs have to stay at top level. This might
6671 * get relaxed later.