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 void fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
156 List **stripped_indexquals_p,
157 List **fixed_indexquals_p);
158 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
159 static Node *fix_indexqual_clause(PlannerInfo *root,
160 IndexOptInfo *index, int indexcol,
161 Node *clause, List *indexcolnos);
162 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
163 static List *get_switched_clauses(List *clauses, Relids outerrelids);
164 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
165 static void copy_generic_path_info(Plan *dest, Path *src);
166 static void copy_plan_costsize(Plan *dest, Plan *src);
167 static void label_sort_with_costsize(PlannerInfo *root, Sort *plan,
168 double limit_tuples);
169 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
170 static SampleScan *make_samplescan(List *qptlist, List *qpqual, Index scanrelid,
171 TableSampleClause *tsc);
172 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
173 Oid indexid, List *indexqual, List *indexqualorig,
174 List *indexorderby, List *indexorderbyorig,
175 List *indexorderbyops,
176 ScanDirection indexscandir);
177 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
178 Index scanrelid, Oid indexid,
179 List *indexqual, List *indexorderby,
181 ScanDirection indexscandir);
182 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
184 List *indexqualorig);
185 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
188 List *bitmapqualorig,
190 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
192 static SubqueryScan *make_subqueryscan(List *qptlist,
196 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
197 Index scanrelid, List *functions, bool funcordinality);
198 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
199 Index scanrelid, List *values_lists);
200 static TableFuncScan *make_tablefuncscan(List *qptlist, List *qpqual,
201 Index scanrelid, TableFunc *tablefunc);
202 static CteScan *make_ctescan(List *qptlist, List *qpqual,
203 Index scanrelid, int ctePlanId, int cteParam);
204 static NamedTuplestoreScan *make_namedtuplestorescan(List *qptlist, List *qpqual,
205 Index scanrelid, char *enrname);
206 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
207 Index scanrelid, int wtParam);
208 static RecursiveUnion *make_recursive_union(List *tlist,
214 static BitmapAnd *make_bitmap_and(List *bitmapplans);
215 static BitmapOr *make_bitmap_or(List *bitmapplans);
216 static NestLoop *make_nestloop(List *tlist,
217 List *joinclauses, List *otherclauses, List *nestParams,
218 Plan *lefttree, Plan *righttree,
219 JoinType jointype, bool inner_unique);
220 static HashJoin *make_hashjoin(List *tlist,
221 List *joinclauses, List *otherclauses,
223 Plan *lefttree, Plan *righttree,
224 JoinType jointype, bool inner_unique);
225 static Hash *make_hash(Plan *lefttree,
227 AttrNumber skewColumn,
229 static MergeJoin *make_mergejoin(List *tlist,
230 List *joinclauses, List *otherclauses,
233 Oid *mergecollations,
234 int *mergestrategies,
235 bool *mergenullsfirst,
236 Plan *lefttree, Plan *righttree,
237 JoinType jointype, bool inner_unique,
238 bool skip_mark_restore);
239 static Sort *make_sort(Plan *lefttree, int numCols,
240 AttrNumber *sortColIdx, Oid *sortOperators,
241 Oid *collations, bool *nullsFirst);
242 static Plan *prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
244 const AttrNumber *reqColIdx,
245 bool adjust_tlist_in_place,
247 AttrNumber **p_sortColIdx,
248 Oid **p_sortOperators,
250 bool **p_nullsFirst);
251 static EquivalenceMember *find_ec_member_for_tle(EquivalenceClass *ec,
254 static Sort *make_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
256 static Sort *make_sort_from_groupcols(List *groupcls,
257 AttrNumber *grpColIdx,
259 static Material *make_material(Plan *lefttree);
260 static WindowAgg *make_windowagg(List *tlist, Index winref,
261 int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations,
262 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations,
263 int frameOptions, Node *startOffset, Node *endOffset,
264 Oid startInRangeFunc, Oid endInRangeFunc,
265 Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst,
267 static Group *make_group(List *tlist, List *qual, int numGroupCols,
268 AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations,
270 static Unique *make_unique_from_sortclauses(Plan *lefttree, List *distinctList);
271 static Unique *make_unique_from_pathkeys(Plan *lefttree,
272 List *pathkeys, int numCols);
273 static Gather *make_gather(List *qptlist, List *qpqual,
274 int nworkers, int rescan_param, bool single_copy, Plan *subplan);
275 static SetOp *make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
276 List *distinctList, AttrNumber flagColIdx, int firstFlag,
278 static LockRows *make_lockrows(Plan *lefttree, List *rowMarks, int epqParam);
279 static Result *make_result(List *tlist, Node *resconstantqual, Plan *subplan);
280 static ProjectSet *make_project_set(List *tlist, Plan *subplan);
281 static ModifyTable *make_modifytable(PlannerInfo *root,
282 CmdType operation, bool canSetTag,
283 Index nominalRelation, Index rootRelation,
284 bool partColsUpdated,
285 List *resultRelations, List *subplans, List *subroots,
286 List *withCheckOptionLists, List *returningLists,
287 List *rowMarks, OnConflictExpr *onconflict, int epqParam);
288 static GatherMerge *create_gather_merge_plan(PlannerInfo *root,
289 GatherMergePath *best_path);
294 * Creates the access plan for a query by recursively processing the
295 * desired tree of pathnodes, starting at the node 'best_path'. For
296 * every pathnode found, we create a corresponding plan node containing
297 * appropriate id, target list, and qualification information.
299 * The tlists and quals in the plan tree are still in planner format,
300 * ie, Vars still correspond to the parser's numbering. This will be
301 * fixed later by setrefs.c.
303 * best_path is the best access path
305 * Returns a Plan tree.
308 create_plan(PlannerInfo *root, Path *best_path)
312 /* plan_params should not be in use in current query level */
313 Assert(root->plan_params == NIL);
315 /* Initialize this module's workspace in PlannerInfo */
316 root->curOuterRels = NULL;
317 root->curOuterParams = NIL;
319 /* Recursively process the path tree, demanding the correct tlist result */
320 plan = create_plan_recurse(root, best_path, CP_EXACT_TLIST);
323 * Make sure the topmost plan node's targetlist exposes the original
324 * column names and other decorative info. Targetlists generated within
325 * the planner don't bother with that stuff, but we must have it on the
326 * top-level tlist seen at execution time. However, ModifyTable plan
327 * nodes don't have a tlist matching the querytree targetlist.
329 if (!IsA(plan, ModifyTable))
330 apply_tlist_labeling(plan->targetlist, root->processed_tlist);
333 * Attach any initPlans created in this query level to the topmost plan
334 * node. (In principle the initplans could go in any plan node at or
335 * above where they're referenced, but there seems no reason to put them
336 * any lower than the topmost node for the query level. Also, see
337 * comments for SS_finalize_plan before you try to change this.)
339 SS_attach_initplans(root, plan);
341 /* Check we successfully assigned all NestLoopParams to plan nodes */
342 if (root->curOuterParams != NIL)
343 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
346 * Reset plan_params to ensure param IDs used for nestloop params are not
349 root->plan_params = NIL;
355 * create_plan_recurse
356 * Recursive guts of create_plan().
359 create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
363 /* Guard against stack overflow due to overly complex plans */
366 switch (best_path->pathtype)
371 case T_IndexOnlyScan:
372 case T_BitmapHeapScan:
376 case T_TableFuncScan:
379 case T_WorkTableScan:
380 case T_NamedTuplestoreScan:
383 plan = create_scan_plan(root, best_path, flags);
388 plan = create_join_plan(root,
389 (JoinPath *) best_path);
392 plan = create_append_plan(root,
393 (AppendPath *) best_path);
396 plan = create_merge_append_plan(root,
397 (MergeAppendPath *) best_path);
400 if (IsA(best_path, ProjectionPath))
402 plan = create_projection_plan(root,
403 (ProjectionPath *) best_path,
406 else if (IsA(best_path, MinMaxAggPath))
408 plan = (Plan *) create_minmaxagg_plan(root,
409 (MinMaxAggPath *) best_path);
411 else if (IsA(best_path, GroupResultPath))
413 plan = (Plan *) create_group_result_plan(root,
414 (GroupResultPath *) best_path);
418 /* Simple RTE_RESULT base relation */
419 Assert(IsA(best_path, Path));
420 plan = create_scan_plan(root, best_path, flags);
424 plan = (Plan *) create_project_set_plan(root,
425 (ProjectSetPath *) best_path);
428 plan = (Plan *) create_material_plan(root,
429 (MaterialPath *) best_path,
433 if (IsA(best_path, UpperUniquePath))
435 plan = (Plan *) create_upper_unique_plan(root,
436 (UpperUniquePath *) best_path,
441 Assert(IsA(best_path, UniquePath));
442 plan = create_unique_plan(root,
443 (UniquePath *) best_path,
448 plan = (Plan *) create_gather_plan(root,
449 (GatherPath *) best_path);
452 plan = (Plan *) create_sort_plan(root,
453 (SortPath *) best_path,
457 plan = (Plan *) create_group_plan(root,
458 (GroupPath *) best_path);
461 if (IsA(best_path, GroupingSetsPath))
462 plan = create_groupingsets_plan(root,
463 (GroupingSetsPath *) best_path);
466 Assert(IsA(best_path, AggPath));
467 plan = (Plan *) create_agg_plan(root,
468 (AggPath *) best_path);
472 plan = (Plan *) create_windowagg_plan(root,
473 (WindowAggPath *) best_path);
476 plan = (Plan *) create_setop_plan(root,
477 (SetOpPath *) best_path,
480 case T_RecursiveUnion:
481 plan = (Plan *) create_recursiveunion_plan(root,
482 (RecursiveUnionPath *) best_path);
485 plan = (Plan *) create_lockrows_plan(root,
486 (LockRowsPath *) best_path,
490 plan = (Plan *) create_modifytable_plan(root,
491 (ModifyTablePath *) best_path);
494 plan = (Plan *) create_limit_plan(root,
495 (LimitPath *) best_path,
499 plan = (Plan *) create_gather_merge_plan(root,
500 (GatherMergePath *) best_path);
503 elog(ERROR, "unrecognized node type: %d",
504 (int) best_path->pathtype);
505 plan = NULL; /* keep compiler quiet */
514 * Create a scan plan for the parent relation of 'best_path'.
517 create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
519 RelOptInfo *rel = best_path->parent;
521 List *gating_clauses;
526 * Extract the relevant restriction clauses from the parent relation. The
527 * executor must apply all these restrictions during the scan, except for
528 * pseudoconstants which we'll take care of below.
530 * If this is a plain indexscan or index-only scan, we need not consider
531 * restriction clauses that are implied by the index's predicate, so use
532 * indrestrictinfo not baserestrictinfo. Note that we can't do that for
533 * bitmap indexscans, since there's not necessarily a single index
534 * involved; but it doesn't matter since create_bitmap_scan_plan() will be
535 * able to get rid of such clauses anyway via predicate proof.
537 switch (best_path->pathtype)
540 case T_IndexOnlyScan:
541 scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
544 scan_clauses = rel->baserestrictinfo;
549 * If this is a parameterized scan, we also need to enforce all the join
550 * clauses available from the outer relation(s).
552 * For paranoia's sake, don't modify the stored baserestrictinfo list.
554 if (best_path->param_info)
555 scan_clauses = list_concat(list_copy(scan_clauses),
556 best_path->param_info->ppi_clauses);
559 * Detect whether we have any pseudoconstant quals to deal with. Then, if
560 * we'll need a gating Result node, it will be able to project, so there
561 * are no requirements on the child's tlist.
563 gating_clauses = get_gating_quals(root, scan_clauses);
568 * For table scans, rather than using the relation targetlist (which is
569 * only those Vars actually needed by the query), we prefer to generate a
570 * tlist containing all Vars in order. This will allow the executor to
571 * optimize away projection of the table tuples, if possible.
573 * But if the caller is going to ignore our tlist anyway, then don't
574 * bother generating one at all. We use an exact equality test here, so
575 * that this only applies when CP_IGNORE_TLIST is the only flag set.
577 if (flags == CP_IGNORE_TLIST)
581 else if (use_physical_tlist(root, best_path, flags))
583 if (best_path->pathtype == T_IndexOnlyScan)
585 /* For index-only scan, the preferred tlist is the index's */
586 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
589 * Transfer sortgroupref data to the replacement tlist, if
590 * requested (use_physical_tlist checked that this will work).
592 if (flags & CP_LABEL_TLIST)
593 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
597 tlist = build_physical_tlist(root, rel);
600 /* Failed because of dropped cols, so use regular method */
601 tlist = build_path_tlist(root, best_path);
605 /* As above, transfer sortgroupref data to replacement tlist */
606 if (flags & CP_LABEL_TLIST)
607 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
613 tlist = build_path_tlist(root, best_path);
616 switch (best_path->pathtype)
619 plan = (Plan *) create_seqscan_plan(root,
626 plan = (Plan *) create_samplescan_plan(root,
633 plan = (Plan *) create_indexscan_plan(root,
634 (IndexPath *) best_path,
640 case T_IndexOnlyScan:
641 plan = (Plan *) create_indexscan_plan(root,
642 (IndexPath *) best_path,
648 case T_BitmapHeapScan:
649 plan = (Plan *) create_bitmap_scan_plan(root,
650 (BitmapHeapPath *) best_path,
656 plan = (Plan *) create_tidscan_plan(root,
657 (TidPath *) best_path,
663 plan = (Plan *) create_subqueryscan_plan(root,
664 (SubqueryScanPath *) best_path,
670 plan = (Plan *) create_functionscan_plan(root,
676 case T_TableFuncScan:
677 plan = (Plan *) create_tablefuncscan_plan(root,
684 plan = (Plan *) create_valuesscan_plan(root,
691 plan = (Plan *) create_ctescan_plan(root,
697 case T_NamedTuplestoreScan:
698 plan = (Plan *) create_namedtuplestorescan_plan(root,
705 plan = (Plan *) create_resultscan_plan(root,
711 case T_WorkTableScan:
712 plan = (Plan *) create_worktablescan_plan(root,
719 plan = (Plan *) create_foreignscan_plan(root,
720 (ForeignPath *) best_path,
726 plan = (Plan *) create_customscan_plan(root,
727 (CustomPath *) best_path,
733 elog(ERROR, "unrecognized node type: %d",
734 (int) best_path->pathtype);
735 plan = NULL; /* keep compiler quiet */
740 * If there are any pseudoconstant clauses attached to this node, insert a
741 * gating Result node that evaluates the pseudoconstants as one-time
745 plan = create_gating_plan(root, best_path, plan, gating_clauses);
751 * Build a target list (ie, a list of TargetEntry) for the Path's output.
753 * This is almost just make_tlist_from_pathtarget(), but we also have to
754 * deal with replacing nestloop params.
757 build_path_tlist(PlannerInfo *root, Path *path)
760 Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
764 foreach(v, path->pathtarget->exprs)
766 Node *node = (Node *) lfirst(v);
770 * If it's a parameterized path, there might be lateral references in
771 * the tlist, which need to be replaced with Params. There's no need
772 * to remake the TargetEntry nodes, so apply this to each list item
775 if (path->param_info)
776 node = replace_nestloop_params(root, node);
778 tle = makeTargetEntry((Expr *) node,
783 tle->ressortgroupref = sortgrouprefs[resno - 1];
785 tlist = lappend(tlist, tle);
793 * Decide whether to use a tlist matching relation structure,
794 * rather than only those Vars actually referenced.
797 use_physical_tlist(PlannerInfo *root, Path *path, int flags)
799 RelOptInfo *rel = path->parent;
804 * Forget it if either exact tlist or small tlist is demanded.
806 if (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST))
810 * We can do this for real relation scans, subquery scans, function scans,
811 * tablefunc scans, values scans, and CTE scans (but not for, eg, joins).
813 if (rel->rtekind != RTE_RELATION &&
814 rel->rtekind != RTE_SUBQUERY &&
815 rel->rtekind != RTE_FUNCTION &&
816 rel->rtekind != RTE_TABLEFUNC &&
817 rel->rtekind != RTE_VALUES &&
818 rel->rtekind != RTE_CTE)
822 * Can't do it with inheritance cases either (mainly because Append
823 * doesn't project; this test may be unnecessary now that
824 * create_append_plan instructs its children to return an exact tlist).
826 if (rel->reloptkind != RELOPT_BASEREL)
830 * Also, don't do it to a CustomPath; the premise that we're extracting
831 * columns from a simple physical tuple is unlikely to hold for those.
832 * (When it does make sense, the custom path creator can set up the path's
833 * pathtarget that way.)
835 if (IsA(path, CustomPath))
839 * If a bitmap scan's tlist is empty, keep it as-is. This may allow the
840 * executor to skip heap page fetches, and in any case, the benefit of
841 * using a physical tlist instead would be minimal.
843 if (IsA(path, BitmapHeapPath) &&
844 path->pathtarget->exprs == NIL)
848 * Can't do it if any system columns or whole-row Vars are requested.
849 * (This could possibly be fixed but would take some fragile assumptions
850 * in setrefs.c, I think.)
852 for (i = rel->min_attr; i <= 0; i++)
854 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
859 * Can't do it if the rel is required to emit any placeholder expressions,
862 foreach(lc, root->placeholder_list)
864 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
866 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
867 bms_is_subset(phinfo->ph_eval_at, rel->relids))
872 * Also, can't do it if CP_LABEL_TLIST is specified and path is requested
873 * to emit any sort/group columns that are not simple Vars. (If they are
874 * simple Vars, they should appear in the physical tlist, and
875 * apply_pathtarget_labeling_to_tlist will take care of getting them
876 * labeled again.) We also have to check that no two sort/group columns
877 * are the same Var, else that element of the physical tlist would need
878 * conflicting ressortgroupref labels.
880 if ((flags & CP_LABEL_TLIST) && path->pathtarget->sortgrouprefs)
882 Bitmapset *sortgroupatts = NULL;
885 foreach(lc, path->pathtarget->exprs)
887 Expr *expr = (Expr *) lfirst(lc);
889 if (path->pathtarget->sortgrouprefs[i])
891 if (expr && IsA(expr, Var))
893 int attno = ((Var *) expr)->varattno;
895 attno -= FirstLowInvalidHeapAttributeNumber;
896 if (bms_is_member(attno, sortgroupatts))
898 sortgroupatts = bms_add_member(sortgroupatts, attno);
912 * See if there are pseudoconstant quals in a node's quals list
914 * If the node's quals list includes any pseudoconstant quals,
915 * return just those quals.
918 get_gating_quals(PlannerInfo *root, List *quals)
920 /* No need to look if we know there are no pseudoconstants */
921 if (!root->hasPseudoConstantQuals)
924 /* Sort into desirable execution order while still in RestrictInfo form */
925 quals = order_qual_clauses(root, quals);
927 /* Pull out any pseudoconstant quals from the RestrictInfo list */
928 return extract_actual_clauses(quals, true);
933 * Deal with pseudoconstant qual clauses
935 * Add a gating Result node atop the already-built plan.
938 create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
944 Assert(gating_quals);
947 * We might have a trivial Result plan already. Stacking one Result atop
948 * another is silly, so if that applies, just discard the input plan.
949 * (We're assuming its targetlist is uninteresting; it should be either
950 * the same as the result of build_path_tlist, or a simplified version.)
953 if (IsA(plan, Result))
955 Result *rplan = (Result *) plan;
957 if (rplan->plan.lefttree == NULL &&
958 rplan->resconstantqual == NULL)
963 * Since we need a Result node anyway, always return the path's requested
964 * tlist; that's never a wrong choice, even if the parent node didn't ask
965 * for CP_EXACT_TLIST.
967 gplan = (Plan *) make_result(build_path_tlist(root, path),
968 (Node *) gating_quals,
972 * Notice that we don't change cost or size estimates when doing gating.
973 * The costs of qual eval were already included in the subplan's cost.
974 * Leaving the size alone amounts to assuming that the gating qual will
975 * succeed, which is the conservative estimate for planning upper queries.
976 * We certainly don't want to assume the output size is zero (unless the
977 * gating qual is actually constant FALSE, and that case is dealt with in
978 * clausesel.c). Interpolating between the two cases is silly, because it
979 * doesn't reflect what will really happen at runtime, and besides which
980 * in most cases we have only a very bad idea of the probability of the
981 * gating qual being true.
983 copy_plan_costsize(gplan, plan);
985 /* Gating quals could be unsafe, so better use the Path's safety flag */
986 gplan->parallel_safe = path->parallel_safe;
993 * Create a join plan for 'best_path' and (recursively) plans for its
994 * inner and outer paths.
997 create_join_plan(PlannerInfo *root, JoinPath *best_path)
1000 List *gating_clauses;
1002 switch (best_path->path.pathtype)
1005 plan = (Plan *) create_mergejoin_plan(root,
1006 (MergePath *) best_path);
1009 plan = (Plan *) create_hashjoin_plan(root,
1010 (HashPath *) best_path);
1013 plan = (Plan *) create_nestloop_plan(root,
1014 (NestPath *) best_path);
1017 elog(ERROR, "unrecognized node type: %d",
1018 (int) best_path->path.pathtype);
1019 plan = NULL; /* keep compiler quiet */
1024 * If there are any pseudoconstant clauses attached to this node, insert a
1025 * gating Result node that evaluates the pseudoconstants as one-time
1028 gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1030 plan = create_gating_plan(root, (Path *) best_path, plan,
1036 * * Expensive function pullups may have pulled local predicates * into
1037 * this path node. Put them in the qpqual of the plan node. * JMH,
1040 if (get_loc_restrictinfo(best_path) != NIL)
1041 set_qpqual((Plan) plan,
1042 list_concat(get_qpqual((Plan) plan),
1043 get_actual_clauses(get_loc_restrictinfo(best_path))));
1050 * create_append_plan
1051 * Create an Append plan for 'best_path' and (recursively) plans
1054 * Returns a Plan node.
1057 create_append_plan(PlannerInfo *root, AppendPath *best_path)
1060 List *tlist = build_path_tlist(root, &best_path->path);
1061 List *pathkeys = best_path->path.pathkeys;
1062 List *subplans = NIL;
1064 RelOptInfo *rel = best_path->path.parent;
1065 PartitionPruneInfo *partpruneinfo = NULL;
1066 int nodenumsortkeys = 0;
1067 AttrNumber *nodeSortColIdx = NULL;
1068 Oid *nodeSortOperators = NULL;
1069 Oid *nodeCollations = NULL;
1070 bool *nodeNullsFirst = NULL;
1073 * The subpaths list could be empty, if every child was proven empty by
1074 * constraint exclusion. In that case generate a dummy plan that returns
1077 * Note that an AppendPath with no members is also generated in certain
1078 * cases where there was no appending construct at all, but we know the
1079 * relation is empty (see set_dummy_rel_pathlist and mark_dummy_rel).
1081 if (best_path->subpaths == NIL)
1083 /* Generate a Result plan with constant-FALSE gating qual */
1086 plan = (Plan *) make_result(tlist,
1087 (Node *) list_make1(makeBoolConst(false,
1091 copy_generic_path_info(plan, (Path *) best_path);
1097 * Otherwise build an Append plan. Note that if there's just one child,
1098 * the Append is pretty useless; but we wait till setrefs.c to get rid of
1099 * it. Doing so here doesn't work because the varno of the child scan
1100 * plan won't match the parent-rel Vars it'll be asked to emit.
1102 * We don't have the actual creation of the Append node split out into a
1103 * separate make_xxx function. This is because we want to run
1104 * prepare_sort_from_pathkeys on it before we do so on the individual
1105 * child plans, to make cross-checking the sort info easier.
1107 plan = makeNode(Append);
1108 plan->plan.targetlist = tlist;
1109 plan->plan.qual = NIL;
1110 plan->plan.lefttree = NULL;
1111 plan->plan.righttree = NULL;
1113 if (pathkeys != NIL)
1115 /* Compute sort column info, and adjust the Append's tlist as needed */
1116 (void) prepare_sort_from_pathkeys((Plan *) plan, pathkeys,
1117 best_path->path.parent->relids,
1127 /* Build the plan for each child */
1128 foreach(subpaths, best_path->subpaths)
1130 Path *subpath = (Path *) lfirst(subpaths);
1133 /* Must insist that all children return the same tlist */
1134 subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1137 * For ordered Appends, we must insert a Sort node if subplan isn't
1138 * sufficiently ordered.
1140 if (pathkeys != NIL)
1143 AttrNumber *sortColIdx;
1149 * Compute sort column info, and adjust subplan's tlist as needed.
1150 * We must apply prepare_sort_from_pathkeys even to subplans that
1151 * don't need an explicit sort, to make sure they are returning
1152 * the same sort key columns the Append expects.
1154 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1155 subpath->parent->relids,
1165 * Check that we got the same sort key information. We just
1166 * Assert that the sortops match, since those depend only on the
1167 * pathkeys; but it seems like a good idea to check the sort
1168 * column numbers explicitly, to ensure the tlists match up.
1170 Assert(numsortkeys == nodenumsortkeys);
1171 if (memcmp(sortColIdx, nodeSortColIdx,
1172 numsortkeys * sizeof(AttrNumber)) != 0)
1173 elog(ERROR, "Append child's targetlist doesn't match Append");
1174 Assert(memcmp(sortOperators, nodeSortOperators,
1175 numsortkeys * sizeof(Oid)) == 0);
1176 Assert(memcmp(collations, nodeCollations,
1177 numsortkeys * sizeof(Oid)) == 0);
1178 Assert(memcmp(nullsFirst, nodeNullsFirst,
1179 numsortkeys * sizeof(bool)) == 0);
1181 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1182 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1184 Sort *sort = make_sort(subplan, numsortkeys,
1185 sortColIdx, sortOperators,
1186 collations, nullsFirst);
1188 label_sort_with_costsize(root, sort, best_path->limit_tuples);
1189 subplan = (Plan *) sort;
1193 subplans = lappend(subplans, subplan);
1197 * If any quals exist, they may be useful to perform further partition
1198 * pruning during execution. Gather information needed by the executor to
1199 * do partition pruning.
1201 if (enable_partition_pruning &&
1202 rel->reloptkind == RELOPT_BASEREL &&
1203 best_path->partitioned_rels != NIL)
1207 prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1209 if (best_path->path.param_info)
1211 List *prmquals = best_path->path.param_info->ppi_clauses;
1213 prmquals = extract_actual_clauses(prmquals, false);
1214 prmquals = (List *) replace_nestloop_params(root,
1217 prunequal = list_concat(prunequal, prmquals);
1220 if (prunequal != NIL)
1222 make_partition_pruneinfo(root, rel,
1223 best_path->subpaths,
1224 best_path->partitioned_rels,
1228 plan->appendplans = subplans;
1229 plan->first_partial_plan = best_path->first_partial_path;
1230 plan->part_prune_info = partpruneinfo;
1232 copy_generic_path_info(&plan->plan, (Path *) best_path);
1234 return (Plan *) plan;
1238 * create_merge_append_plan
1239 * Create a MergeAppend plan for 'best_path' and (recursively) plans
1242 * Returns a Plan node.
1245 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
1247 MergeAppend *node = makeNode(MergeAppend);
1248 Plan *plan = &node->plan;
1249 List *tlist = build_path_tlist(root, &best_path->path);
1250 List *pathkeys = best_path->path.pathkeys;
1251 List *subplans = NIL;
1253 RelOptInfo *rel = best_path->path.parent;
1254 PartitionPruneInfo *partpruneinfo = NULL;
1257 * We don't have the actual creation of the MergeAppend node split out
1258 * into a separate make_xxx function. This is because we want to run
1259 * prepare_sort_from_pathkeys on it before we do so on the individual
1260 * child plans, to make cross-checking the sort info easier.
1262 copy_generic_path_info(plan, (Path *) best_path);
1263 plan->targetlist = tlist;
1265 plan->lefttree = NULL;
1266 plan->righttree = NULL;
1268 /* Compute sort column info, and adjust MergeAppend's tlist as needed */
1269 (void) prepare_sort_from_pathkeys(plan, pathkeys,
1270 best_path->path.parent->relids,
1275 &node->sortOperators,
1280 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1281 * even to subplans that don't need an explicit sort, to make sure they
1282 * are returning the same sort key columns the MergeAppend expects.
1284 foreach(subpaths, best_path->subpaths)
1286 Path *subpath = (Path *) lfirst(subpaths);
1289 AttrNumber *sortColIdx;
1294 /* Build the child plan */
1295 /* Must insist that all children return the same tlist */
1296 subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1298 /* Compute sort column info, and adjust subplan's tlist as needed */
1299 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1300 subpath->parent->relids,
1310 * Check that we got the same sort key information. We just Assert
1311 * that the sortops match, since those depend only on the pathkeys;
1312 * but it seems like a good idea to check the sort column numbers
1313 * explicitly, to ensure the tlists really do match up.
1315 Assert(numsortkeys == node->numCols);
1316 if (memcmp(sortColIdx, node->sortColIdx,
1317 numsortkeys * sizeof(AttrNumber)) != 0)
1318 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1319 Assert(memcmp(sortOperators, node->sortOperators,
1320 numsortkeys * sizeof(Oid)) == 0);
1321 Assert(memcmp(collations, node->collations,
1322 numsortkeys * sizeof(Oid)) == 0);
1323 Assert(memcmp(nullsFirst, node->nullsFirst,
1324 numsortkeys * sizeof(bool)) == 0);
1326 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1327 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1329 Sort *sort = make_sort(subplan, numsortkeys,
1330 sortColIdx, sortOperators,
1331 collations, nullsFirst);
1333 label_sort_with_costsize(root, sort, best_path->limit_tuples);
1334 subplan = (Plan *) sort;
1337 subplans = lappend(subplans, subplan);
1341 * If any quals exist, they may be useful to perform further partition
1342 * pruning during execution. Gather information needed by the executor to
1343 * do partition pruning.
1345 if (enable_partition_pruning &&
1346 rel->reloptkind == RELOPT_BASEREL &&
1347 best_path->partitioned_rels != NIL)
1351 prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1353 if (best_path->path.param_info)
1355 List *prmquals = best_path->path.param_info->ppi_clauses;
1357 prmquals = extract_actual_clauses(prmquals, false);
1358 prmquals = (List *) replace_nestloop_params(root,
1361 prunequal = list_concat(prunequal, prmquals);
1364 if (prunequal != NIL)
1365 partpruneinfo = make_partition_pruneinfo(root, rel,
1366 best_path->subpaths,
1367 best_path->partitioned_rels,
1371 node->mergeplans = subplans;
1372 node->part_prune_info = partpruneinfo;
1374 return (Plan *) node;
1378 * create_group_result_plan
1379 * Create a Result plan for 'best_path'.
1380 * This is only used for degenerate grouping cases.
1382 * Returns a Plan node.
1385 create_group_result_plan(PlannerInfo *root, GroupResultPath *best_path)
1391 tlist = build_path_tlist(root, &best_path->path);
1393 /* best_path->quals is just bare clauses */
1394 quals = order_qual_clauses(root, best_path->quals);
1396 plan = make_result(tlist, (Node *) quals, NULL);
1398 copy_generic_path_info(&plan->plan, (Path *) best_path);
1404 * create_project_set_plan
1405 * Create a ProjectSet plan for 'best_path'.
1407 * Returns a Plan node.
1410 create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
1416 /* Since we intend to project, we don't need to constrain child tlist */
1417 subplan = create_plan_recurse(root, best_path->subpath, 0);
1419 tlist = build_path_tlist(root, &best_path->path);
1421 plan = make_project_set(tlist, subplan);
1423 copy_generic_path_info(&plan->plan, (Path *) best_path);
1429 * create_material_plan
1430 * Create a Material plan for 'best_path' and (recursively) plans
1433 * Returns a Plan node.
1436 create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
1442 * We don't want any excess columns in the materialized tuples, so request
1443 * a smaller tlist. Otherwise, since Material doesn't project, tlist
1444 * requirements pass through.
1446 subplan = create_plan_recurse(root, best_path->subpath,
1447 flags | CP_SMALL_TLIST);
1449 plan = make_material(subplan);
1451 copy_generic_path_info(&plan->plan, (Path *) best_path);
1457 * create_unique_plan
1458 * Create a Unique plan for 'best_path' and (recursively) plans
1461 * Returns a Plan node.
1464 create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
1474 AttrNumber *groupColIdx;
1475 Oid *groupCollations;
1479 /* Unique doesn't project, so tlist requirements pass through */
1480 subplan = create_plan_recurse(root, best_path->subpath, flags);
1482 /* Done if we don't need to do any actual unique-ifying */
1483 if (best_path->umethod == UNIQUE_PATH_NOOP)
1487 * As constructed, the subplan has a "flat" tlist containing just the Vars
1488 * needed here and at upper levels. The values we are supposed to
1489 * unique-ify may be expressions in these variables. We have to add any
1490 * such expressions to the subplan's tlist.
1492 * The subplan may have a "physical" tlist if it is a simple scan plan. If
1493 * we're going to sort, this should be reduced to the regular tlist, so
1494 * that we don't sort more data than we need to. For hashing, the tlist
1495 * should be left as-is if we don't need to add any expressions; but if we
1496 * do have to add expressions, then a projection step will be needed at
1497 * runtime anyway, so we may as well remove unneeded items. Therefore
1498 * newtlist starts from build_path_tlist() not just a copy of the
1499 * subplan's tlist; and we don't install it into the subplan unless we are
1500 * sorting or stuff has to be added.
1502 in_operators = best_path->in_operators;
1503 uniq_exprs = best_path->uniq_exprs;
1505 /* initialize modified subplan tlist as just the "required" vars */
1506 newtlist = build_path_tlist(root, &best_path->path);
1507 nextresno = list_length(newtlist) + 1;
1510 foreach(l, uniq_exprs)
1512 Expr *uniqexpr = lfirst(l);
1515 tle = tlist_member(uniqexpr, newtlist);
1518 tle = makeTargetEntry((Expr *) uniqexpr,
1522 newtlist = lappend(newtlist, tle);
1528 /* Use change_plan_targetlist in case we need to insert a Result node */
1529 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
1530 subplan = change_plan_targetlist(subplan, newtlist,
1531 best_path->path.parallel_safe);
1534 * Build control information showing which subplan output columns are to
1535 * be examined by the grouping step. Unfortunately we can't merge this
1536 * with the previous loop, since we didn't then know which version of the
1537 * subplan tlist we'd end up using.
1539 newtlist = subplan->targetlist;
1540 numGroupCols = list_length(uniq_exprs);
1541 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
1542 groupCollations = (Oid *) palloc(numGroupCols * sizeof(Oid));
1545 foreach(l, uniq_exprs)
1547 Expr *uniqexpr = lfirst(l);
1550 tle = tlist_member(uniqexpr, newtlist);
1551 if (!tle) /* shouldn't happen */
1552 elog(ERROR, "failed to find unique expression in subplan tlist");
1553 groupColIdx[groupColPos] = tle->resno;
1554 groupCollations[groupColPos] = exprCollation((Node *) tle->expr);
1558 if (best_path->umethod == UNIQUE_PATH_HASH)
1560 Oid *groupOperators;
1563 * Get the hashable equality operators for the Agg node to use.
1564 * Normally these are the same as the IN clause operators, but if
1565 * those are cross-type operators then the equality operators are the
1566 * ones for the IN clause operators' RHS datatype.
1568 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
1570 foreach(l, in_operators)
1572 Oid in_oper = lfirst_oid(l);
1575 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
1576 elog(ERROR, "could not find compatible hash operator for operator %u",
1578 groupOperators[groupColPos++] = eq_oper;
1582 * Since the Agg node is going to project anyway, we can give it the
1583 * minimum output tlist, without any stuff we might have added to the
1586 plan = (Plan *) make_agg(build_path_tlist(root, &best_path->path),
1596 best_path->path.rows,
1601 List *sortList = NIL;
1604 /* Create an ORDER BY list to sort the input compatibly */
1606 foreach(l, in_operators)
1608 Oid in_oper = lfirst_oid(l);
1612 SortGroupClause *sortcl;
1614 sortop = get_ordering_op_for_equality_op(in_oper, false);
1615 if (!OidIsValid(sortop)) /* shouldn't happen */
1616 elog(ERROR, "could not find ordering operator for equality operator %u",
1620 * The Unique node will need equality operators. Normally these
1621 * are the same as the IN clause operators, but if those are
1622 * cross-type operators then the equality operators are the ones
1623 * for the IN clause operators' RHS datatype.
1625 eqop = get_equality_op_for_ordering_op(sortop, NULL);
1626 if (!OidIsValid(eqop)) /* shouldn't happen */
1627 elog(ERROR, "could not find equality operator for ordering operator %u",
1630 tle = get_tle_by_resno(subplan->targetlist,
1631 groupColIdx[groupColPos]);
1632 Assert(tle != NULL);
1634 sortcl = makeNode(SortGroupClause);
1635 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
1636 subplan->targetlist);
1637 sortcl->eqop = eqop;
1638 sortcl->sortop = sortop;
1639 sortcl->nulls_first = false;
1640 sortcl->hashable = false; /* no need to make this accurate */
1641 sortList = lappend(sortList, sortcl);
1644 sort = make_sort_from_sortclauses(sortList, subplan);
1645 label_sort_with_costsize(root, sort, -1.0);
1646 plan = (Plan *) make_unique_from_sortclauses((Plan *) sort, sortList);
1649 /* Copy cost data from Path to Plan */
1650 copy_generic_path_info(plan, &best_path->path);
1656 * create_gather_plan
1658 * Create a Gather plan for 'best_path' and (recursively) plans
1662 create_gather_plan(PlannerInfo *root, GatherPath *best_path)
1664 Gather *gather_plan;
1669 * Although the Gather node can project, we prefer to push down such work
1670 * to its child node, so demand an exact tlist from the child.
1672 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1674 tlist = build_path_tlist(root, &best_path->path);
1676 gather_plan = make_gather(tlist,
1678 best_path->num_workers,
1679 assign_special_exec_param(root),
1680 best_path->single_copy,
1683 copy_generic_path_info(&gather_plan->plan, &best_path->path);
1685 /* use parallel mode for parallel plans. */
1686 root->glob->parallelModeNeeded = true;
1692 * create_gather_merge_plan
1694 * Create a Gather Merge plan for 'best_path' and (recursively)
1695 * plans for its subpaths.
1697 static GatherMerge *
1698 create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
1700 GatherMerge *gm_plan;
1702 List *pathkeys = best_path->path.pathkeys;
1703 List *tlist = build_path_tlist(root, &best_path->path);
1705 /* As with Gather, it's best to project away columns in the workers. */
1706 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1708 /* Create a shell for a GatherMerge plan. */
1709 gm_plan = makeNode(GatherMerge);
1710 gm_plan->plan.targetlist = tlist;
1711 gm_plan->num_workers = best_path->num_workers;
1712 copy_generic_path_info(&gm_plan->plan, &best_path->path);
1714 /* Assign the rescan Param. */
1715 gm_plan->rescan_param = assign_special_exec_param(root);
1717 /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1718 Assert(pathkeys != NIL);
1720 /* Compute sort column info, and adjust subplan's tlist as needed */
1721 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1722 best_path->subpath->parent->relids,
1723 gm_plan->sortColIdx,
1726 &gm_plan->sortColIdx,
1727 &gm_plan->sortOperators,
1728 &gm_plan->collations,
1729 &gm_plan->nullsFirst);
1732 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1733 if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1734 subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1735 gm_plan->sortColIdx,
1736 gm_plan->sortOperators,
1737 gm_plan->collations,
1738 gm_plan->nullsFirst);
1740 /* Now insert the subplan under GatherMerge. */
1741 gm_plan->plan.lefttree = subplan;
1743 /* use parallel mode for parallel plans. */
1744 root->glob->parallelModeNeeded = true;
1750 * create_projection_plan
1752 * Create a plan tree to do a projection step and (recursively) plans
1753 * for its subpaths. We may need a Result node for the projection,
1754 * but sometimes we can just let the subplan do the work.
1757 create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
1762 bool needs_result_node = false;
1765 * Convert our subpath to a Plan and determine whether we need a Result
1768 * In most cases where we don't need to project, creation_projection_path
1769 * will have set dummypp, but not always. First, some createplan.c
1770 * routines change the tlists of their nodes. (An example is that
1771 * create_merge_append_plan might add resjunk sort columns to a
1772 * MergeAppend.) Second, create_projection_path has no way of knowing
1773 * what path node will be placed on top of the projection path and
1774 * therefore can't predict whether it will require an exact tlist. For
1775 * both of these reasons, we have to recheck here.
1777 if (use_physical_tlist(root, &best_path->path, flags))
1780 * Our caller doesn't really care what tlist we return, so we don't
1781 * actually need to project. However, we may still need to ensure
1782 * proper sortgroupref labels, if the caller cares about those.
1784 subplan = create_plan_recurse(root, best_path->subpath, 0);
1785 tlist = subplan->targetlist;
1786 if (flags & CP_LABEL_TLIST)
1787 apply_pathtarget_labeling_to_tlist(tlist,
1788 best_path->path.pathtarget);
1790 else if (is_projection_capable_path(best_path->subpath))
1793 * Our caller requires that we return the exact tlist, but no separate
1794 * result node is needed because the subpath is projection-capable.
1795 * Tell create_plan_recurse that we're going to ignore the tlist it
1798 subplan = create_plan_recurse(root, best_path->subpath,
1800 tlist = build_path_tlist(root, &best_path->path);
1805 * It looks like we need a result node, unless by good fortune the
1806 * requested tlist is exactly the one the child wants to produce.
1808 subplan = create_plan_recurse(root, best_path->subpath, 0);
1809 tlist = build_path_tlist(root, &best_path->path);
1810 needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
1814 * If we make a different decision about whether to include a Result node
1815 * than create_projection_path did, we'll have made slightly wrong cost
1816 * estimates; but label the plan with the cost estimates we actually used,
1817 * not "corrected" ones. (XXX this could be cleaned up if we moved more
1818 * of the sortcolumn setup logic into Path creation, but that would add
1819 * expense to creating Paths we might end up not using.)
1821 if (!needs_result_node)
1823 /* Don't need a separate Result, just assign tlist to subplan */
1825 plan->targetlist = tlist;
1827 /* Label plan with the estimated costs we actually used */
1828 plan->startup_cost = best_path->path.startup_cost;
1829 plan->total_cost = best_path->path.total_cost;
1830 plan->plan_rows = best_path->path.rows;
1831 plan->plan_width = best_path->path.pathtarget->width;
1832 plan->parallel_safe = best_path->path.parallel_safe;
1833 /* ... but don't change subplan's parallel_aware flag */
1837 /* We need a Result node */
1838 plan = (Plan *) make_result(tlist, NULL, subplan);
1840 copy_generic_path_info(plan, (Path *) best_path);
1847 * inject_projection_plan
1848 * Insert a Result node to do a projection step.
1850 * This is used in a few places where we decide on-the-fly that we need a
1851 * projection step as part of the tree generated for some Path node.
1852 * We should try to get rid of this in favor of doing it more honestly.
1854 * One reason it's ugly is we have to be told the right parallel_safe marking
1855 * to apply (since the tlist might be unsafe even if the child plan is safe).
1858 inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
1862 plan = (Plan *) make_result(tlist, NULL, subplan);
1865 * In principle, we should charge tlist eval cost plus cpu_per_tuple per
1866 * row for the Result node. But the former has probably been factored in
1867 * already and the latter was not accounted for during Path construction,
1868 * so being formally correct might just make the EXPLAIN output look less
1869 * consistent not more so. Hence, just copy the subplan's cost.
1871 copy_plan_costsize(plan, subplan);
1872 plan->parallel_safe = parallel_safe;
1878 * change_plan_targetlist
1879 * Externally available wrapper for inject_projection_plan.
1881 * This is meant for use by FDW plan-generation functions, which might
1882 * want to adjust the tlist computed by some subplan tree. In general,
1883 * a Result node is needed to compute the new tlist, but we can optimize
1886 * In most cases, tlist_parallel_safe can just be passed as the parallel_safe
1887 * flag of the FDW's own Path node.
1890 change_plan_targetlist(Plan *subplan, List *tlist, bool tlist_parallel_safe)
1893 * If the top plan node can't do projections and its existing target list
1894 * isn't already what we need, we need to add a Result node to help it
1897 if (!is_projection_capable_plan(subplan) &&
1898 !tlist_same_exprs(tlist, subplan->targetlist))
1899 subplan = inject_projection_plan(subplan, tlist,
1900 subplan->parallel_safe &&
1901 tlist_parallel_safe);
1904 /* Else we can just replace the plan node's tlist */
1905 subplan->targetlist = tlist;
1906 subplan->parallel_safe &= tlist_parallel_safe;
1914 * Create a Sort plan for 'best_path' and (recursively) plans
1918 create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
1924 * We don't want any excess columns in the sorted tuples, so request a
1925 * smaller tlist. Otherwise, since Sort doesn't project, tlist
1926 * requirements pass through.
1928 subplan = create_plan_recurse(root, best_path->subpath,
1929 flags | CP_SMALL_TLIST);
1932 * make_sort_from_pathkeys() indirectly calls find_ec_member_for_tle(),
1933 * which will ignore any child EC members that don't belong to the given
1934 * relids. Thus, if this sort path is based on a child relation, we must
1937 plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys,
1938 IS_OTHER_REL(best_path->subpath->parent) ?
1939 best_path->path.parent->relids : NULL);
1941 copy_generic_path_info(&plan->plan, (Path *) best_path);
1949 * Create a Group plan for 'best_path' and (recursively) plans
1953 create_group_plan(PlannerInfo *root, GroupPath *best_path)
1961 * Group can project, so no need to be terribly picky about child tlist,
1962 * but we do need grouping columns to be available
1964 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1966 tlist = build_path_tlist(root, &best_path->path);
1968 quals = order_qual_clauses(root, best_path->qual);
1970 plan = make_group(tlist,
1972 list_length(best_path->groupClause),
1973 extract_grouping_cols(best_path->groupClause,
1974 subplan->targetlist),
1975 extract_grouping_ops(best_path->groupClause),
1976 extract_grouping_collations(best_path->groupClause,
1977 subplan->targetlist),
1980 copy_generic_path_info(&plan->plan, (Path *) best_path);
1986 * create_upper_unique_plan
1988 * Create a Unique plan for 'best_path' and (recursively) plans
1992 create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
1998 * Unique doesn't project, so tlist requirements pass through; moreover we
1999 * need grouping columns to be labeled.
2001 subplan = create_plan_recurse(root, best_path->subpath,
2002 flags | CP_LABEL_TLIST);
2004 plan = make_unique_from_pathkeys(subplan,
2005 best_path->path.pathkeys,
2006 best_path->numkeys);
2008 copy_generic_path_info(&plan->plan, (Path *) best_path);
2016 * Create an Agg plan for 'best_path' and (recursively) plans
2020 create_agg_plan(PlannerInfo *root, AggPath *best_path)
2028 * Agg can project, so no need to be terribly picky about child tlist, but
2029 * we do need grouping columns to be available
2031 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2033 tlist = build_path_tlist(root, &best_path->path);
2035 quals = order_qual_clauses(root, best_path->qual);
2037 plan = make_agg(tlist, quals,
2038 best_path->aggstrategy,
2039 best_path->aggsplit,
2040 list_length(best_path->groupClause),
2041 extract_grouping_cols(best_path->groupClause,
2042 subplan->targetlist),
2043 extract_grouping_ops(best_path->groupClause),
2044 extract_grouping_collations(best_path->groupClause,
2045 subplan->targetlist),
2048 best_path->numGroups,
2051 copy_generic_path_info(&plan->plan, (Path *) best_path);
2057 * Given a groupclause for a collection of grouping sets, produce the
2058 * corresponding groupColIdx.
2060 * root->grouping_map maps the tleSortGroupRef to the actual column position in
2061 * the input tuple. So we get the ref from the entries in the groupclause and
2062 * look them up there.
2065 remap_groupColIdx(PlannerInfo *root, List *groupClause)
2067 AttrNumber *grouping_map = root->grouping_map;
2068 AttrNumber *new_grpColIdx;
2072 Assert(grouping_map);
2074 new_grpColIdx = palloc0(sizeof(AttrNumber) * list_length(groupClause));
2077 foreach(lc, groupClause)
2079 SortGroupClause *clause = lfirst(lc);
2081 new_grpColIdx[i++] = grouping_map[clause->tleSortGroupRef];
2084 return new_grpColIdx;
2088 * create_groupingsets_plan
2089 * Create a plan for 'best_path' and (recursively) plans
2092 * What we emit is an Agg plan with some vestigial Agg and Sort nodes
2093 * hanging off the side. The top Agg implements the last grouping set
2094 * specified in the GroupingSetsPath, and any additional grouping sets
2095 * each give rise to a subsidiary Agg and Sort node in the top Agg's
2096 * "chain" list. These nodes don't participate in the plan directly,
2097 * but they are a convenient way to represent the required data for
2100 * Returns a Plan node.
2103 create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
2107 List *rollups = best_path->rollups;
2108 AttrNumber *grouping_map;
2113 /* Shouldn't get here without grouping sets */
2114 Assert(root->parse->groupingSets);
2115 Assert(rollups != NIL);
2118 * Agg can project, so no need to be terribly picky about child tlist, but
2119 * we do need grouping columns to be available
2121 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2124 * Compute the mapping from tleSortGroupRef to column index in the child's
2125 * tlist. First, identify max SortGroupRef in groupClause, for array
2129 foreach(lc, root->parse->groupClause)
2131 SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2133 if (gc->tleSortGroupRef > maxref)
2134 maxref = gc->tleSortGroupRef;
2137 grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2139 /* Now look up the column numbers in the child's tlist */
2140 foreach(lc, root->parse->groupClause)
2142 SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2143 TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
2145 grouping_map[gc->tleSortGroupRef] = tle->resno;
2149 * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2150 * in GroupingFunc nodes. Save it for setrefs.c to use.
2152 * This doesn't work if we're in an inheritance subtree (see notes in
2153 * create_modifytable_plan). Fortunately we can't be because there would
2154 * never be grouping in an UPDATE/DELETE; but let's Assert that.
2156 Assert(root->inhTargetKind == INHKIND_NONE);
2157 Assert(root->grouping_map == NULL);
2158 root->grouping_map = grouping_map;
2161 * Generate the side nodes that describe the other sort and group
2162 * operations besides the top one. Note that we don't worry about putting
2163 * accurate cost estimates in the side nodes; only the topmost Agg node's
2164 * costs will be shown by EXPLAIN.
2167 if (list_length(rollups) > 1)
2169 ListCell *lc2 = lnext(list_head(rollups));
2170 bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2172 for_each_cell(lc, lc2)
2174 RollupData *rollup = lfirst(lc);
2175 AttrNumber *new_grpColIdx;
2176 Plan *sort_plan = NULL;
2180 new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2182 if (!rollup->is_hashed && !is_first_sort)
2184 sort_plan = (Plan *)
2185 make_sort_from_groupcols(rollup->groupClause,
2190 if (!rollup->is_hashed)
2191 is_first_sort = false;
2193 if (rollup->is_hashed)
2195 else if (list_length(linitial(rollup->gsets)) == 0)
2200 agg_plan = (Plan *) make_agg(NIL,
2204 list_length((List *) linitial(rollup->gsets)),
2206 extract_grouping_ops(rollup->groupClause),
2207 extract_grouping_collations(rollup->groupClause, subplan->targetlist),
2214 * Remove stuff we don't need to avoid bloating debug output.
2218 sort_plan->targetlist = NIL;
2219 sort_plan->lefttree = NULL;
2222 chain = lappend(chain, agg_plan);
2227 * Now make the real Agg node
2230 RollupData *rollup = linitial(rollups);
2231 AttrNumber *top_grpColIdx;
2234 top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2236 numGroupCols = list_length((List *) linitial(rollup->gsets));
2238 plan = make_agg(build_path_tlist(root, &best_path->path),
2240 best_path->aggstrategy,
2244 extract_grouping_ops(rollup->groupClause),
2245 extract_grouping_collations(rollup->groupClause, subplan->targetlist),
2251 /* Copy cost data from Path to Plan */
2252 copy_generic_path_info(&plan->plan, &best_path->path);
2255 return (Plan *) plan;
2259 * create_minmaxagg_plan
2261 * Create a Result plan for 'best_path' and (recursively) plans
2265 create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
2271 /* Prepare an InitPlan for each aggregate's subquery. */
2272 foreach(lc, best_path->mmaggregates)
2274 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2275 PlannerInfo *subroot = mminfo->subroot;
2276 Query *subparse = subroot->parse;
2280 * Generate the plan for the subquery. We already have a Path, but we
2281 * have to convert it to a Plan and attach a LIMIT node above it.
2282 * Since we are entering a different planner context (subroot),
2283 * recurse to create_plan not create_plan_recurse.
2285 plan = create_plan(subroot, mminfo->path);
2287 plan = (Plan *) make_limit(plan,
2288 subparse->limitOffset,
2289 subparse->limitCount);
2291 /* Must apply correct cost/width data to Limit node */
2292 plan->startup_cost = mminfo->path->startup_cost;
2293 plan->total_cost = mminfo->pathcost;
2294 plan->plan_rows = 1;
2295 plan->plan_width = mminfo->path->pathtarget->width;
2296 plan->parallel_aware = false;
2297 plan->parallel_safe = mminfo->path->parallel_safe;
2299 /* Convert the plan into an InitPlan in the outer query. */
2300 SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2303 /* Generate the output plan --- basically just a Result */
2304 tlist = build_path_tlist(root, &best_path->path);
2306 plan = make_result(tlist, (Node *) best_path->quals, NULL);
2308 copy_generic_path_info(&plan->plan, (Path *) best_path);
2311 * During setrefs.c, we'll need to replace references to the Agg nodes
2312 * with InitPlan output params. (We can't just do that locally in the
2313 * MinMaxAgg node, because path nodes above here may have Agg references
2314 * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2316 * This doesn't work if we're in an inheritance subtree (see notes in
2317 * create_modifytable_plan). Fortunately we can't be because there would
2318 * never be aggregates in an UPDATE/DELETE; but let's Assert that.
2320 Assert(root->inhTargetKind == INHKIND_NONE);
2321 Assert(root->minmax_aggs == NIL);
2322 root->minmax_aggs = best_path->mmaggregates;
2328 * create_windowagg_plan
2330 * Create a WindowAgg plan for 'best_path' and (recursively) plans
2334 create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
2337 WindowClause *wc = best_path->winclause;
2338 int numPart = list_length(wc->partitionClause);
2339 int numOrder = list_length(wc->orderClause);
2343 AttrNumber *partColIdx;
2345 Oid *partCollations;
2347 AttrNumber *ordColIdx;
2353 * WindowAgg can project, so no need to be terribly picky about child
2354 * tlist, but we do need grouping columns to be available
2356 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2358 tlist = build_path_tlist(root, &best_path->path);
2361 * Convert SortGroupClause lists into arrays of attr indexes and equality
2362 * operators, as wanted by executor. (Note: in principle, it's possible
2363 * to drop some of the sort columns, if they were proved redundant by
2364 * pathkey logic. However, it doesn't seem worth going out of our way to
2365 * optimize such cases. In any case, we must *not* remove the ordering
2366 * column for RANGE OFFSET cases, as the executor needs that for in_range
2367 * tests even if it's known to be equal to some partitioning column.)
2369 partColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numPart);
2370 partOperators = (Oid *) palloc(sizeof(Oid) * numPart);
2371 partCollations = (Oid *) palloc(sizeof(Oid) * numPart);
2374 foreach(lc, wc->partitionClause)
2376 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2377 TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
2379 Assert(OidIsValid(sgc->eqop));
2380 partColIdx[partNumCols] = tle->resno;
2381 partOperators[partNumCols] = sgc->eqop;
2382 partCollations[partNumCols] = exprCollation((Node *) tle->expr);
2386 ordColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numOrder);
2387 ordOperators = (Oid *) palloc(sizeof(Oid) * numOrder);
2388 ordCollations = (Oid *) palloc(sizeof(Oid) * numOrder);
2391 foreach(lc, wc->orderClause)
2393 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2394 TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
2396 Assert(OidIsValid(sgc->eqop));
2397 ordColIdx[ordNumCols] = tle->resno;
2398 ordOperators[ordNumCols] = sgc->eqop;
2399 ordCollations[ordNumCols] = exprCollation((Node *) tle->expr);
2403 /* And finally we can make the WindowAgg node */
2404 plan = make_windowagg(tlist,
2417 wc->startInRangeFunc,
2421 wc->inRangeNullsFirst,
2424 copy_generic_path_info(&plan->plan, (Path *) best_path);
2432 * Create a SetOp plan for 'best_path' and (recursively) plans
2436 create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
2443 * SetOp doesn't project, so tlist requirements pass through; moreover we
2444 * need grouping columns to be labeled.
2446 subplan = create_plan_recurse(root, best_path->subpath,
2447 flags | CP_LABEL_TLIST);
2449 /* Convert numGroups to long int --- but 'ware overflow! */
2450 numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2452 plan = make_setop(best_path->cmd,
2453 best_path->strategy,
2455 best_path->distinctList,
2456 best_path->flagColIdx,
2457 best_path->firstFlag,
2460 copy_generic_path_info(&plan->plan, (Path *) best_path);
2466 * create_recursiveunion_plan
2468 * Create a RecursiveUnion plan for 'best_path' and (recursively) plans
2471 static RecursiveUnion *
2472 create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
2474 RecursiveUnion *plan;
2480 /* Need both children to produce same tlist, so force it */
2481 leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2482 rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2484 tlist = build_path_tlist(root, &best_path->path);
2486 /* Convert numGroups to long int --- but 'ware overflow! */
2487 numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2489 plan = make_recursive_union(tlist,
2493 best_path->distinctList,
2496 copy_generic_path_info(&plan->plan, (Path *) best_path);
2502 * create_lockrows_plan
2504 * Create a LockRows plan for 'best_path' and (recursively) plans
2508 create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
2514 /* LockRows doesn't project, so tlist requirements pass through */
2515 subplan = create_plan_recurse(root, best_path->subpath, flags);
2517 plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2519 copy_generic_path_info(&plan->plan, (Path *) best_path);
2525 * create_modifytable_plan
2526 * Create a ModifyTable plan for 'best_path'.
2528 * Returns a Plan node.
2530 static ModifyTable *
2531 create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
2534 List *subplans = NIL;
2538 /* Build the plan for each input path */
2539 forboth(subpaths, best_path->subpaths,
2540 subroots, best_path->subroots)
2542 Path *subpath = (Path *) lfirst(subpaths);
2543 PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2547 * In an inherited UPDATE/DELETE, reference the per-child modified
2548 * subroot while creating Plans from Paths for the child rel. This is
2549 * a kluge, but otherwise it's too hard to ensure that Plan creation
2550 * functions (particularly in FDWs) don't depend on the contents of
2551 * "root" matching what they saw at Path creation time. The main
2552 * downside is that creation functions for Plans that might appear
2553 * below a ModifyTable cannot expect to modify the contents of "root"
2554 * and have it "stick" for subsequent processing such as setrefs.c.
2555 * That's not great, but it seems better than the alternative.
2557 subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2559 /* Transfer resname/resjunk labeling, too, to keep executor happy */
2560 apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2562 subplans = lappend(subplans, subplan);
2565 plan = make_modifytable(root,
2566 best_path->operation,
2567 best_path->canSetTag,
2568 best_path->nominalRelation,
2569 best_path->rootRelation,
2570 best_path->partColsUpdated,
2571 best_path->resultRelations,
2573 best_path->subroots,
2574 best_path->withCheckOptionLists,
2575 best_path->returningLists,
2576 best_path->rowMarks,
2577 best_path->onconflict,
2578 best_path->epqParam);
2580 copy_generic_path_info(&plan->plan, &best_path->path);
2588 * Create a Limit plan for 'best_path' and (recursively) plans
2592 create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
2597 /* Limit doesn't project, so tlist requirements pass through */
2598 subplan = create_plan_recurse(root, best_path->subpath, flags);
2600 plan = make_limit(subplan,
2601 best_path->limitOffset,
2602 best_path->limitCount);
2604 copy_generic_path_info(&plan->plan, (Path *) best_path);
2610 /*****************************************************************************
2612 * BASE-RELATION SCAN METHODS
2614 *****************************************************************************/
2618 * create_seqscan_plan
2619 * Returns a seqscan plan for the base relation scanned by 'best_path'
2620 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2623 create_seqscan_plan(PlannerInfo *root, Path *best_path,
2624 List *tlist, List *scan_clauses)
2627 Index scan_relid = best_path->parent->relid;
2629 /* it should be a base rel... */
2630 Assert(scan_relid > 0);
2631 Assert(best_path->parent->rtekind == RTE_RELATION);
2633 /* Sort clauses into best execution order */
2634 scan_clauses = order_qual_clauses(root, scan_clauses);
2636 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2637 scan_clauses = extract_actual_clauses(scan_clauses, false);
2639 /* Replace any outer-relation variables with nestloop params */
2640 if (best_path->param_info)
2642 scan_clauses = (List *)
2643 replace_nestloop_params(root, (Node *) scan_clauses);
2646 scan_plan = make_seqscan(tlist,
2650 copy_generic_path_info(&scan_plan->plan, best_path);
2656 * create_samplescan_plan
2657 * Returns a samplescan plan for the base relation scanned by 'best_path'
2658 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2661 create_samplescan_plan(PlannerInfo *root, Path *best_path,
2662 List *tlist, List *scan_clauses)
2664 SampleScan *scan_plan;
2665 Index scan_relid = best_path->parent->relid;
2667 TableSampleClause *tsc;
2669 /* it should be a base rel with a tablesample clause... */
2670 Assert(scan_relid > 0);
2671 rte = planner_rt_fetch(scan_relid, root);
2672 Assert(rte->rtekind == RTE_RELATION);
2673 tsc = rte->tablesample;
2674 Assert(tsc != NULL);
2676 /* Sort clauses into best execution order */
2677 scan_clauses = order_qual_clauses(root, scan_clauses);
2679 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2680 scan_clauses = extract_actual_clauses(scan_clauses, false);
2682 /* Replace any outer-relation variables with nestloop params */
2683 if (best_path->param_info)
2685 scan_clauses = (List *)
2686 replace_nestloop_params(root, (Node *) scan_clauses);
2687 tsc = (TableSampleClause *)
2688 replace_nestloop_params(root, (Node *) tsc);
2691 scan_plan = make_samplescan(tlist,
2696 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2702 * create_indexscan_plan
2703 * Returns an indexscan plan for the base relation scanned by 'best_path'
2704 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2706 * We use this for both plain IndexScans and IndexOnlyScans, because the
2707 * qual preprocessing work is the same for both. Note that the caller tells
2708 * us which to build --- we don't look at best_path->path.pathtype, because
2709 * create_bitmap_subplan needs to be able to override the prior decision.
2712 create_indexscan_plan(PlannerInfo *root,
2713 IndexPath *best_path,
2719 List *indexclauses = best_path->indexclauses;
2720 List *indexorderbys = best_path->indexorderbys;
2721 Index baserelid = best_path->path.parent->relid;
2722 Oid indexoid = best_path->indexinfo->indexoid;
2724 List *stripped_indexquals;
2725 List *fixed_indexquals;
2726 List *fixed_indexorderbys;
2727 List *indexorderbyops = NIL;
2730 /* it should be a base rel... */
2731 Assert(baserelid > 0);
2732 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2735 * Extract the index qual expressions (stripped of RestrictInfos) from the
2736 * IndexClauses list, and prepare a copy with index Vars substituted for
2737 * table Vars. (This step also does replace_nestloop_params on the
2738 * fixed_indexquals.)
2740 fix_indexqual_references(root, best_path,
2741 &stripped_indexquals,
2745 * Likewise fix up index attr references in the ORDER BY expressions.
2747 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2750 * The qpqual list must contain all restrictions not automatically handled
2751 * by the index, other than pseudoconstant clauses which will be handled
2752 * by a separate gating plan node. All the predicates in the indexquals
2753 * will be checked (either by the index itself, or by nodeIndexscan.c),
2754 * but if there are any "special" operators involved then they must be
2755 * included in qpqual. The upshot is that qpqual must contain
2756 * scan_clauses minus whatever appears in indexquals.
2758 * is_redundant_with_indexclauses() detects cases where a scan clause is
2759 * present in the indexclauses list or is generated from the same
2760 * EquivalenceClass as some indexclause, and is therefore redundant with
2761 * it, though not equal. (The latter happens when indxpath.c prefers a
2762 * different derived equality than what generate_join_implied_equalities
2763 * picked for a parameterized scan's ppi_clauses.) Note that it will not
2764 * match to lossy index clauses, which is critical because we have to
2765 * include the original clause in qpqual in that case.
2767 * In some situations (particularly with OR'd index conditions) we may
2768 * have scan_clauses that are not equal to, but are logically implied by,
2769 * the index quals; so we also try a predicate_implied_by() check to see
2770 * if we can discard quals that way. (predicate_implied_by assumes its
2771 * first input contains only immutable functions, so we have to check
2774 * Note: if you change this bit of code you should also look at
2775 * extract_nonindex_conditions() in costsize.c.
2778 foreach(l, scan_clauses)
2780 RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2782 if (rinfo->pseudoconstant)
2783 continue; /* we may drop pseudoconstants here */
2784 if (is_redundant_with_indexclauses(rinfo, indexclauses))
2785 continue; /* dup or derived from same EquivalenceClass */
2786 if (!contain_mutable_functions((Node *) rinfo->clause) &&
2787 predicate_implied_by(list_make1(rinfo->clause), stripped_indexquals,
2789 continue; /* provably implied by indexquals */
2790 qpqual = lappend(qpqual, rinfo);
2793 /* Sort clauses into best execution order */
2794 qpqual = order_qual_clauses(root, qpqual);
2796 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2797 qpqual = extract_actual_clauses(qpqual, false);
2800 * We have to replace any outer-relation variables with nestloop params in
2801 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2802 * annoying to have to do this separately from the processing in
2803 * fix_indexqual_references --- rethink this when generalizing the inner
2804 * indexscan support. But note we can't really do this earlier because
2805 * it'd break the comparisons to predicates above ... (or would it? Those
2806 * wouldn't have outer refs)
2808 if (best_path->path.param_info)
2810 stripped_indexquals = (List *)
2811 replace_nestloop_params(root, (Node *) stripped_indexquals);
2813 replace_nestloop_params(root, (Node *) qpqual);
2814 indexorderbys = (List *)
2815 replace_nestloop_params(root, (Node *) indexorderbys);
2819 * If there are ORDER BY expressions, look up the sort operators for their
2824 ListCell *pathkeyCell,
2828 * PathKey contains OID of the btree opfamily we're sorting by, but
2829 * that's not quite enough because we need the expression's datatype
2830 * to look up the sort operator in the operator family.
2832 Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2833 forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2835 PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2836 Node *expr = (Node *) lfirst(exprCell);
2837 Oid exprtype = exprType(expr);
2840 /* Get sort operator from opfamily */
2841 sortop = get_opfamily_member(pathkey->pk_opfamily,
2844 pathkey->pk_strategy);
2845 if (!OidIsValid(sortop))
2846 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2847 pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
2848 indexorderbyops = lappend_oid(indexorderbyops, sortop);
2852 /* Finally ready to build the plan node */
2854 scan_plan = (Scan *) make_indexonlyscan(tlist,
2859 fixed_indexorderbys,
2860 best_path->indexinfo->indextlist,
2861 best_path->indexscandir);
2863 scan_plan = (Scan *) make_indexscan(tlist,
2868 stripped_indexquals,
2869 fixed_indexorderbys,
2872 best_path->indexscandir);
2874 copy_generic_path_info(&scan_plan->plan, &best_path->path);
2880 * create_bitmap_scan_plan
2881 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
2882 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2884 static BitmapHeapScan *
2885 create_bitmap_scan_plan(PlannerInfo *root,
2886 BitmapHeapPath *best_path,
2890 Index baserelid = best_path->path.parent->relid;
2891 Plan *bitmapqualplan;
2892 List *bitmapqualorig;
2897 BitmapHeapScan *scan_plan;
2899 /* it should be a base rel... */
2900 Assert(baserelid > 0);
2901 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2903 /* Process the bitmapqual tree into a Plan tree and qual lists */
2904 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
2905 &bitmapqualorig, &indexquals,
2908 if (best_path->path.parallel_aware)
2909 bitmap_subplan_mark_shared(bitmapqualplan);
2912 * The qpqual list must contain all restrictions not automatically handled
2913 * by the index, other than pseudoconstant clauses which will be handled
2914 * by a separate gating plan node. All the predicates in the indexquals
2915 * will be checked (either by the index itself, or by
2916 * nodeBitmapHeapscan.c), but if there are any "special" operators
2917 * involved then they must be added to qpqual. The upshot is that qpqual
2918 * must contain scan_clauses minus whatever appears in indexquals.
2920 * This loop is similar to the comparable code in create_indexscan_plan(),
2921 * but with some differences because it has to compare the scan clauses to
2922 * stripped (no RestrictInfos) indexquals. See comments there for more
2925 * In normal cases simple equal() checks will be enough to spot duplicate
2926 * clauses, so we try that first. We next see if the scan clause is
2927 * redundant with any top-level indexqual by virtue of being generated
2928 * from the same EC. After that, try predicate_implied_by().
2930 * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
2931 * useful for getting rid of qpquals that are implied by index predicates,
2932 * because the predicate conditions are included in the "indexquals"
2933 * returned by create_bitmap_subplan(). Bitmap scans have to do it that
2934 * way because predicate conditions need to be rechecked if the scan
2935 * becomes lossy, so they have to be included in bitmapqualorig.
2938 foreach(l, scan_clauses)
2940 RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2941 Node *clause = (Node *) rinfo->clause;
2943 if (rinfo->pseudoconstant)
2944 continue; /* we may drop pseudoconstants here */
2945 if (list_member(indexquals, clause))
2946 continue; /* simple duplicate */
2947 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
2948 continue; /* derived from same EquivalenceClass */
2949 if (!contain_mutable_functions(clause) &&
2950 predicate_implied_by(list_make1(clause), indexquals, false))
2951 continue; /* provably implied by indexquals */
2952 qpqual = lappend(qpqual, rinfo);
2955 /* Sort clauses into best execution order */
2956 qpqual = order_qual_clauses(root, qpqual);
2958 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2959 qpqual = extract_actual_clauses(qpqual, false);
2962 * When dealing with special operators, we will at this point have
2963 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
2964 * 'em from bitmapqualorig, since there's no point in making the tests
2967 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
2970 * We have to replace any outer-relation variables with nestloop params in
2971 * the qpqual and bitmapqualorig expressions. (This was already done for
2972 * expressions attached to plan nodes in the bitmapqualplan tree.)
2974 if (best_path->path.param_info)
2977 replace_nestloop_params(root, (Node *) qpqual);
2978 bitmapqualorig = (List *)
2979 replace_nestloop_params(root, (Node *) bitmapqualorig);
2982 /* Finally ready to build the plan node */
2983 scan_plan = make_bitmap_heapscan(tlist,
2989 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2995 * Given a bitmapqual tree, generate the Plan tree that implements it
2997 * As byproducts, we also return in *qual and *indexqual the qual lists
2998 * (in implicit-AND form, without RestrictInfos) describing the original index
2999 * conditions and the generated indexqual conditions. (These are the same in
3000 * simple cases, but when special index operators are involved, the former
3001 * list includes the special conditions while the latter includes the actual
3002 * indexable conditions derived from them.) Both lists include partial-index
3003 * predicates, because we have to recheck predicates as well as index
3004 * conditions if the bitmap scan becomes lossy.
3006 * In addition, we return a list of EquivalenceClass pointers for all the
3007 * top-level indexquals that were possibly-redundantly derived from ECs.
3008 * This allows removal of scan_clauses that are redundant with such quals.
3009 * (We do not attempt to detect such redundancies for quals that are within
3010 * OR subtrees. This could be done in a less hacky way if we returned the
3011 * indexquals in RestrictInfo form, but that would be slower and still pretty
3012 * messy, since we'd have to build new RestrictInfos in many cases.)
3015 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
3016 List **qual, List **indexqual, List **indexECs)
3020 if (IsA(bitmapqual, BitmapAndPath))
3022 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
3023 List *subplans = NIL;
3024 List *subquals = NIL;
3025 List *subindexquals = NIL;
3026 List *subindexECs = NIL;
3030 * There may well be redundant quals among the subplans, since a
3031 * top-level WHERE qual might have gotten used to form several
3032 * different index quals. We don't try exceedingly hard to eliminate
3033 * redundancies, but we do eliminate obvious duplicates by using
3034 * list_concat_unique.
3036 foreach(l, apath->bitmapquals)
3043 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3044 &subqual, &subindexqual,
3046 subplans = lappend(subplans, subplan);
3047 subquals = list_concat_unique(subquals, subqual);
3048 subindexquals = list_concat_unique(subindexquals, subindexqual);
3049 /* Duplicates in indexECs aren't worth getting rid of */
3050 subindexECs = list_concat(subindexECs, subindexEC);
3052 plan = (Plan *) make_bitmap_and(subplans);
3053 plan->startup_cost = apath->path.startup_cost;
3054 plan->total_cost = apath->path.total_cost;
3056 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
3057 plan->plan_width = 0; /* meaningless */
3058 plan->parallel_aware = false;
3059 plan->parallel_safe = apath->path.parallel_safe;
3061 *indexqual = subindexquals;
3062 *indexECs = subindexECs;
3064 else if (IsA(bitmapqual, BitmapOrPath))
3066 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
3067 List *subplans = NIL;
3068 List *subquals = NIL;
3069 List *subindexquals = NIL;
3070 bool const_true_subqual = false;
3071 bool const_true_subindexqual = false;
3075 * Here, we only detect qual-free subplans. A qual-free subplan would
3076 * cause us to generate "... OR true ..." which we may as well reduce
3077 * to just "true". We do not try to eliminate redundant subclauses
3078 * because (a) it's not as likely as in the AND case, and (b) we might
3079 * well be working with hundreds or even thousands of OR conditions,
3080 * perhaps from a long IN list. The performance of list_append_unique
3081 * would be unacceptable.
3083 foreach(l, opath->bitmapquals)
3090 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3091 &subqual, &subindexqual,
3093 subplans = lappend(subplans, subplan);
3095 const_true_subqual = true;
3096 else if (!const_true_subqual)
3097 subquals = lappend(subquals,
3098 make_ands_explicit(subqual));
3099 if (subindexqual == NIL)
3100 const_true_subindexqual = true;
3101 else if (!const_true_subindexqual)
3102 subindexquals = lappend(subindexquals,
3103 make_ands_explicit(subindexqual));
3107 * In the presence of ScalarArrayOpExpr quals, we might have built
3108 * BitmapOrPaths with just one subpath; don't add an OR step.
3110 if (list_length(subplans) == 1)
3112 plan = (Plan *) linitial(subplans);
3116 plan = (Plan *) make_bitmap_or(subplans);
3117 plan->startup_cost = opath->path.startup_cost;
3118 plan->total_cost = opath->path.total_cost;
3120 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3121 plan->plan_width = 0; /* meaningless */
3122 plan->parallel_aware = false;
3123 plan->parallel_safe = opath->path.parallel_safe;
3127 * If there were constant-TRUE subquals, the OR reduces to constant
3128 * TRUE. Also, avoid generating one-element ORs, which could happen
3129 * due to redundancy elimination or ScalarArrayOpExpr quals.
3131 if (const_true_subqual)
3133 else if (list_length(subquals) <= 1)
3136 *qual = list_make1(make_orclause(subquals));
3137 if (const_true_subindexqual)
3139 else if (list_length(subindexquals) <= 1)
3140 *indexqual = subindexquals;
3142 *indexqual = list_make1(make_orclause(subindexquals));
3145 else if (IsA(bitmapqual, IndexPath))
3147 IndexPath *ipath = (IndexPath *) bitmapqual;
3150 List *subindexquals;
3154 /* Use the regular indexscan plan build machinery... */
3155 iscan = castNode(IndexScan,
3156 create_indexscan_plan(root, ipath,
3158 /* then convert to a bitmap indexscan */
3159 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
3162 iscan->indexqualorig);
3163 /* and set its cost/width fields appropriately */
3164 plan->startup_cost = 0.0;
3165 plan->total_cost = ipath->indextotalcost;
3167 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3168 plan->plan_width = 0; /* meaningless */
3169 plan->parallel_aware = false;
3170 plan->parallel_safe = ipath->path.parallel_safe;
3171 /* Extract original index clauses, actual index quals, relevant ECs */
3173 subindexquals = NIL;
3175 foreach(l, ipath->indexclauses)
3177 IndexClause *iclause = (IndexClause *) lfirst(l);
3178 RestrictInfo *rinfo = iclause->rinfo;
3180 Assert(!rinfo->pseudoconstant);
3181 subquals = lappend(subquals, rinfo->clause);
3182 subindexquals = list_concat(subindexquals,
3183 get_actual_clauses(iclause->indexquals));
3184 if (rinfo->parent_ec)
3185 subindexECs = lappend(subindexECs, rinfo->parent_ec);
3187 /* We can add any index predicate conditions, too */
3188 foreach(l, ipath->indexinfo->indpred)
3190 Expr *pred = (Expr *) lfirst(l);
3193 * We know that the index predicate must have been implied by the
3194 * query condition as a whole, but it may or may not be implied by
3195 * the conditions that got pushed into the bitmapqual. Avoid
3196 * generating redundant conditions.
3198 if (!predicate_implied_by(list_make1(pred), subquals, false))
3200 subquals = lappend(subquals, pred);
3201 subindexquals = lappend(subindexquals, pred);
3205 *indexqual = subindexquals;
3206 *indexECs = subindexECs;
3210 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3211 plan = NULL; /* keep compiler quiet */
3218 * create_tidscan_plan
3219 * Returns a tidscan plan for the base relation scanned by 'best_path'
3220 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3223 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
3224 List *tlist, List *scan_clauses)
3227 Index scan_relid = best_path->path.parent->relid;
3228 List *tidquals = best_path->tidquals;
3230 /* it should be a base rel... */
3231 Assert(scan_relid > 0);
3232 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3235 * The qpqual list must contain all restrictions not enforced by the
3236 * tidquals list. Since tidquals has OR semantics, we have to be careful
3237 * about matching it up to scan_clauses. It's convenient to handle the
3238 * single-tidqual case separately from the multiple-tidqual case. In the
3239 * single-tidqual case, we look through the scan_clauses while they are
3240 * still in RestrictInfo form, and drop any that are redundant with the
3243 * In normal cases simple pointer equality checks will be enough to spot
3244 * duplicate RestrictInfos, so we try that first.
3246 * Another common case is that a scan_clauses entry is generated from the
3247 * same EquivalenceClass as some tidqual, and is therefore redundant with
3248 * it, though not equal.
3250 * Unlike indexpaths, we don't bother with predicate_implied_by(); the
3251 * number of cases where it could win are pretty small.
3253 if (list_length(tidquals) == 1)
3258 foreach(l, scan_clauses)
3260 RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3262 if (rinfo->pseudoconstant)
3263 continue; /* we may drop pseudoconstants here */
3264 if (list_member_ptr(tidquals, rinfo))
3265 continue; /* simple duplicate */
3266 if (is_redundant_derived_clause(rinfo, tidquals))
3267 continue; /* derived from same EquivalenceClass */
3268 qpqual = lappend(qpqual, rinfo);
3270 scan_clauses = qpqual;
3273 /* Sort clauses into best execution order */
3274 scan_clauses = order_qual_clauses(root, scan_clauses);
3276 /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3277 tidquals = extract_actual_clauses(tidquals, false);
3278 scan_clauses = extract_actual_clauses(scan_clauses, false);
3281 * If we have multiple tidquals, it's more convenient to remove duplicate
3282 * scan_clauses after stripping the RestrictInfos. In this situation,
3283 * because the tidquals represent OR sub-clauses, they could not have come
3284 * from EquivalenceClasses so we don't have to worry about matching up
3285 * non-identical clauses. On the other hand, because tidpath.c will have
3286 * extracted those sub-clauses from some OR clause and built its own list,
3287 * we will certainly not have pointer equality to any scan clause. So
3288 * convert the tidquals list to an explicit OR clause and see if we can
3289 * match it via equal() to any scan clause.
3291 if (list_length(tidquals) > 1)
3292 scan_clauses = list_difference(scan_clauses,
3293 list_make1(make_orclause(tidquals)));
3295 /* Replace any outer-relation variables with nestloop params */
3296 if (best_path->path.param_info)
3299 replace_nestloop_params(root, (Node *) tidquals);
3300 scan_clauses = (List *)
3301 replace_nestloop_params(root, (Node *) scan_clauses);
3304 scan_plan = make_tidscan(tlist,
3309 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3315 * create_subqueryscan_plan
3316 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
3317 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3319 static SubqueryScan *
3320 create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path,
3321 List *tlist, List *scan_clauses)
3323 SubqueryScan *scan_plan;
3324 RelOptInfo *rel = best_path->path.parent;
3325 Index scan_relid = rel->relid;
3328 /* it should be a subquery base rel... */
3329 Assert(scan_relid > 0);
3330 Assert(rel->rtekind == RTE_SUBQUERY);
3333 * Recursively create Plan from Path for subquery. Since we are entering
3334 * a different planner context (subroot), recurse to create_plan not
3335 * create_plan_recurse.
3337 subplan = create_plan(rel->subroot, best_path->subpath);
3339 /* Sort clauses into best execution order */
3340 scan_clauses = order_qual_clauses(root, scan_clauses);
3342 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3343 scan_clauses = extract_actual_clauses(scan_clauses, false);
3345 /* Replace any outer-relation variables with nestloop params */
3346 if (best_path->path.param_info)
3348 scan_clauses = (List *)
3349 replace_nestloop_params(root, (Node *) scan_clauses);
3350 process_subquery_nestloop_params(root,
3351 rel->subplan_params);
3354 scan_plan = make_subqueryscan(tlist,
3359 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3365 * create_functionscan_plan
3366 * Returns a functionscan plan for the base relation scanned by 'best_path'
3367 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3369 static FunctionScan *
3370 create_functionscan_plan(PlannerInfo *root, Path *best_path,
3371 List *tlist, List *scan_clauses)
3373 FunctionScan *scan_plan;
3374 Index scan_relid = best_path->parent->relid;
3378 /* it should be a function base rel... */
3379 Assert(scan_relid > 0);
3380 rte = planner_rt_fetch(scan_relid, root);
3381 Assert(rte->rtekind == RTE_FUNCTION);
3382 functions = rte->functions;
3384 /* Sort clauses into best execution order */
3385 scan_clauses = order_qual_clauses(root, scan_clauses);
3387 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3388 scan_clauses = extract_actual_clauses(scan_clauses, false);
3390 /* Replace any outer-relation variables with nestloop params */
3391 if (best_path->param_info)
3393 scan_clauses = (List *)
3394 replace_nestloop_params(root, (Node *) scan_clauses);
3395 /* The function expressions could contain nestloop params, too */
3396 functions = (List *) replace_nestloop_params(root, (Node *) functions);
3399 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3400 functions, rte->funcordinality);
3402 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3408 * create_tablefuncscan_plan
3409 * Returns a tablefuncscan plan for the base relation scanned by 'best_path'
3410 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3412 static TableFuncScan *
3413 create_tablefuncscan_plan(PlannerInfo *root, Path *best_path,
3414 List *tlist, List *scan_clauses)
3416 TableFuncScan *scan_plan;
3417 Index scan_relid = best_path->parent->relid;
3419 TableFunc *tablefunc;
3421 /* it should be a function base rel... */
3422 Assert(scan_relid > 0);
3423 rte = planner_rt_fetch(scan_relid, root);
3424 Assert(rte->rtekind == RTE_TABLEFUNC);
3425 tablefunc = rte->tablefunc;
3427 /* Sort clauses into best execution order */
3428 scan_clauses = order_qual_clauses(root, scan_clauses);
3430 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3431 scan_clauses = extract_actual_clauses(scan_clauses, false);
3433 /* Replace any outer-relation variables with nestloop params */
3434 if (best_path->param_info)
3436 scan_clauses = (List *)
3437 replace_nestloop_params(root, (Node *) scan_clauses);
3438 /* The function expressions could contain nestloop params, too */
3439 tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
3442 scan_plan = make_tablefuncscan(tlist, scan_clauses, scan_relid,
3445 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3451 * create_valuesscan_plan
3452 * Returns a valuesscan plan for the base relation scanned by 'best_path'
3453 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3456 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
3457 List *tlist, List *scan_clauses)
3459 ValuesScan *scan_plan;
3460 Index scan_relid = best_path->parent->relid;
3464 /* it should be a values base rel... */
3465 Assert(scan_relid > 0);
3466 rte = planner_rt_fetch(scan_relid, root);
3467 Assert(rte->rtekind == RTE_VALUES);
3468 values_lists = rte->values_lists;
3470 /* Sort clauses into best execution order */
3471 scan_clauses = order_qual_clauses(root, scan_clauses);
3473 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3474 scan_clauses = extract_actual_clauses(scan_clauses, false);
3476 /* Replace any outer-relation variables with nestloop params */
3477 if (best_path->param_info)
3479 scan_clauses = (List *)
3480 replace_nestloop_params(root, (Node *) scan_clauses);
3481 /* The values lists could contain nestloop params, too */
3482 values_lists = (List *)
3483 replace_nestloop_params(root, (Node *) values_lists);
3486 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
3489 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3495 * create_ctescan_plan
3496 * Returns a ctescan plan for the base relation scanned by 'best_path'
3497 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3500 create_ctescan_plan(PlannerInfo *root, Path *best_path,
3501 List *tlist, List *scan_clauses)
3504 Index scan_relid = best_path->parent->relid;
3506 SubPlan *ctesplan = NULL;
3509 PlannerInfo *cteroot;
3514 Assert(scan_relid > 0);
3515 rte = planner_rt_fetch(scan_relid, root);
3516 Assert(rte->rtekind == RTE_CTE);
3517 Assert(!rte->self_reference);
3520 * Find the referenced CTE, and locate the SubPlan previously made for it.
3522 levelsup = rte->ctelevelsup;
3524 while (levelsup-- > 0)
3526 cteroot = cteroot->parent_root;
3527 if (!cteroot) /* shouldn't happen */
3528 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3532 * Note: cte_plan_ids can be shorter than cteList, if we are still working
3533 * on planning the CTEs (ie, this is a side-reference from another CTE).
3534 * So we mustn't use forboth here.
3537 foreach(lc, cteroot->parse->cteList)
3539 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3541 if (strcmp(cte->ctename, rte->ctename) == 0)
3545 if (lc == NULL) /* shouldn't happen */
3546 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3547 if (ndx >= list_length(cteroot->cte_plan_ids))
3548 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3549 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3550 Assert(plan_id > 0);
3551 foreach(lc, cteroot->init_plans)
3553 ctesplan = (SubPlan *) lfirst(lc);
3554 if (ctesplan->plan_id == plan_id)
3557 if (lc == NULL) /* shouldn't happen */
3558 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3561 * We need the CTE param ID, which is the sole member of the SubPlan's
3564 cte_param_id = linitial_int(ctesplan->setParam);
3566 /* Sort clauses into best execution order */
3567 scan_clauses = order_qual_clauses(root, scan_clauses);
3569 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3570 scan_clauses = extract_actual_clauses(scan_clauses, false);
3572 /* Replace any outer-relation variables with nestloop params */
3573 if (best_path->param_info)
3575 scan_clauses = (List *)
3576 replace_nestloop_params(root, (Node *) scan_clauses);
3579 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3580 plan_id, cte_param_id);
3582 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3588 * create_namedtuplestorescan_plan
3589 * Returns a tuplestorescan plan for the base relation scanned by
3590 * 'best_path' with restriction clauses 'scan_clauses' and targetlist
3593 static NamedTuplestoreScan *
3594 create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path,
3595 List *tlist, List *scan_clauses)
3597 NamedTuplestoreScan *scan_plan;
3598 Index scan_relid = best_path->parent->relid;
3601 Assert(scan_relid > 0);
3602 rte = planner_rt_fetch(scan_relid, root);
3603 Assert(rte->rtekind == RTE_NAMEDTUPLESTORE);
3605 /* Sort clauses into best execution order */
3606 scan_clauses = order_qual_clauses(root, scan_clauses);
3608 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3609 scan_clauses = extract_actual_clauses(scan_clauses, false);
3611 /* Replace any outer-relation variables with nestloop params */
3612 if (best_path->param_info)
3614 scan_clauses = (List *)
3615 replace_nestloop_params(root, (Node *) scan_clauses);
3618 scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3621 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3627 * create_resultscan_plan
3628 * Returns a Result plan for the RTE_RESULT base relation scanned by
3629 * 'best_path' with restriction clauses 'scan_clauses' and targetlist
3633 create_resultscan_plan(PlannerInfo *root, Path *best_path,
3634 List *tlist, List *scan_clauses)
3637 Index scan_relid = best_path->parent->relid;
3638 RangeTblEntry *rte PG_USED_FOR_ASSERTS_ONLY;
3640 Assert(scan_relid > 0);
3641 rte = planner_rt_fetch(scan_relid, root);
3642 Assert(rte->rtekind == RTE_RESULT);
3644 /* Sort clauses into best execution order */
3645 scan_clauses = order_qual_clauses(root, scan_clauses);
3647 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3648 scan_clauses = extract_actual_clauses(scan_clauses, false);
3650 /* Replace any outer-relation variables with nestloop params */
3651 if (best_path->param_info)
3653 scan_clauses = (List *)
3654 replace_nestloop_params(root, (Node *) scan_clauses);
3657 scan_plan = make_result(tlist, (Node *) scan_clauses, NULL);
3659 copy_generic_path_info(&scan_plan->plan, best_path);
3665 * create_worktablescan_plan
3666 * Returns a worktablescan plan for the base relation scanned by 'best_path'
3667 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3669 static WorkTableScan *
3670 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
3671 List *tlist, List *scan_clauses)
3673 WorkTableScan *scan_plan;
3674 Index scan_relid = best_path->parent->relid;
3677 PlannerInfo *cteroot;
3679 Assert(scan_relid > 0);
3680 rte = planner_rt_fetch(scan_relid, root);
3681 Assert(rte->rtekind == RTE_CTE);
3682 Assert(rte->self_reference);
3685 * We need to find the worktable param ID, which is in the plan level
3686 * that's processing the recursive UNION, which is one level *below* where
3687 * the CTE comes from.
3689 levelsup = rte->ctelevelsup;
3690 if (levelsup == 0) /* shouldn't happen */
3691 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3694 while (levelsup-- > 0)
3696 cteroot = cteroot->parent_root;
3697 if (!cteroot) /* shouldn't happen */
3698 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3700 if (cteroot->wt_param_id < 0) /* shouldn't happen */
3701 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
3703 /* Sort clauses into best execution order */
3704 scan_clauses = order_qual_clauses(root, scan_clauses);
3706 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3707 scan_clauses = extract_actual_clauses(scan_clauses, false);
3709 /* Replace any outer-relation variables with nestloop params */
3710 if (best_path->param_info)
3712 scan_clauses = (List *)
3713 replace_nestloop_params(root, (Node *) scan_clauses);
3716 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
3717 cteroot->wt_param_id);
3719 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3725 * create_foreignscan_plan
3726 * Returns a foreignscan plan for the relation scanned by 'best_path'
3727 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3729 static ForeignScan *
3730 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
3731 List *tlist, List *scan_clauses)
3733 ForeignScan *scan_plan;
3734 RelOptInfo *rel = best_path->path.parent;
3735 Index scan_relid = rel->relid;
3736 Oid rel_oid = InvalidOid;
3737 Plan *outer_plan = NULL;
3739 Assert(rel->fdwroutine != NULL);
3741 /* transform the child path if any */
3742 if (best_path->fdw_outerpath)
3743 outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3747 * If we're scanning a base relation, fetch its OID. (Irrelevant if
3748 * scanning a join relation.)
3754 Assert(rel->rtekind == RTE_RELATION);
3755 rte = planner_rt_fetch(scan_relid, root);
3756 Assert(rte->rtekind == RTE_RELATION);
3757 rel_oid = rte->relid;
3761 * Sort clauses into best execution order. We do this first since the FDW
3762 * might have more info than we do and wish to adjust the ordering.
3764 scan_clauses = order_qual_clauses(root, scan_clauses);
3767 * Let the FDW perform its processing on the restriction clauses and
3768 * generate the plan node. Note that the FDW might remove restriction
3769 * clauses that it intends to execute remotely, or even add more (if it
3770 * has selected some join clauses for remote use but also wants them
3771 * rechecked locally).
3773 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
3775 tlist, scan_clauses,
3778 /* Copy cost data from Path to Plan; no need to make FDW do this */
3779 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3781 /* Copy foreign server OID; likewise, no need to make FDW do this */
3782 scan_plan->fs_server = rel->serverid;
3785 * Likewise, copy the relids that are represented by this foreign scan. An
3786 * upper rel doesn't have relids set, but it covers all the base relations
3787 * participating in the underlying scan, so use root's all_baserels.
3789 if (rel->reloptkind == RELOPT_UPPER_REL)
3790 scan_plan->fs_relids = root->all_baserels;
3792 scan_plan->fs_relids = best_path->path.parent->relids;
3795 * If this is a foreign join, and to make it valid to push down we had to
3796 * assume that the current user is the same as some user explicitly named
3797 * in the query, mark the finished plan as depending on the current user.
3799 if (rel->useridiscurrent)
3800 root->glob->dependsOnRole = true;
3803 * Replace any outer-relation variables with nestloop params in the qual,
3804 * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
3805 * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
3806 * fdw_recheck_quals could have come from join clauses, so doing this
3807 * beforehand on the scan_clauses wouldn't work.) We assume
3808 * fdw_scan_tlist contains no such variables.
3810 if (best_path->path.param_info)
3812 scan_plan->scan.plan.qual = (List *)
3813 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
3814 scan_plan->fdw_exprs = (List *)
3815 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
3816 scan_plan->fdw_recheck_quals = (List *)
3817 replace_nestloop_params(root,
3818 (Node *) scan_plan->fdw_recheck_quals);
3822 * If rel is a base relation, detect whether any system columns are
3823 * requested from the rel. (If rel is a join relation, rel->relid will be
3824 * 0, but there can be no Var with relid 0 in the rel's targetlist or the
3825 * restriction clauses, so we skip this in that case. Note that any such
3826 * columns in base relations that were joined are assumed to be contained
3827 * in fdw_scan_tlist.) This is a bit of a kluge and might go away
3828 * someday, so we intentionally leave it out of the API presented to FDWs.
3830 scan_plan->fsSystemCol = false;
3833 Bitmapset *attrs_used = NULL;
3838 * First, examine all the attributes needed for joins or final output.
3839 * Note: we must look at rel's targetlist, not the attr_needed data,
3840 * because attr_needed isn't computed for inheritance child rels.
3842 pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
3844 /* Add all the attributes used by restriction clauses. */
3845 foreach(lc, rel->baserestrictinfo)
3847 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3849 pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
3852 /* Now, are any system columns requested from rel? */
3853 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
3855 if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
3857 scan_plan->fsSystemCol = true;
3862 bms_free(attrs_used);
3869 * create_custom_plan
3871 * Transform a CustomPath into a Plan.
3874 create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
3875 List *tlist, List *scan_clauses)
3878 RelOptInfo *rel = best_path->path.parent;
3879 List *custom_plans = NIL;
3882 /* Recursively transform child paths. */
3883 foreach(lc, best_path->custom_paths)
3885 Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3888 custom_plans = lappend(custom_plans, plan);
3892 * Sort clauses into the best execution order, although custom-scan
3893 * provider can reorder them again.
3895 scan_clauses = order_qual_clauses(root, scan_clauses);
3898 * Invoke custom plan provider to create the Plan node represented by the
3901 cplan = castNode(CustomScan,
3902 best_path->methods->PlanCustomPath(root,
3910 * Copy cost data from Path to Plan; no need to make custom-plan providers
3913 copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3915 /* Likewise, copy the relids that are represented by this custom scan */
3916 cplan->custom_relids = best_path->path.parent->relids;
3919 * Replace any outer-relation variables with nestloop params in the qual
3920 * and custom_exprs expressions. We do this last so that the custom-plan
3921 * provider doesn't have to be involved. (Note that parts of custom_exprs
3922 * could have come from join clauses, so doing this beforehand on the
3923 * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3926 if (best_path->path.param_info)
3928 cplan->scan.plan.qual = (List *)
3929 replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3930 cplan->custom_exprs = (List *)
3931 replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3938 /*****************************************************************************
3942 *****************************************************************************/
3945 create_nestloop_plan(PlannerInfo *root,
3946 NestPath *best_path)
3948 NestLoop *join_plan;
3951 List *tlist = build_path_tlist(root, &best_path->path);
3952 List *joinrestrictclauses = best_path->joinrestrictinfo;
3957 Relids saveOuterRels = root->curOuterRels;
3959 /* NestLoop can project, so no need to be picky about child tlists */
3960 outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3962 /* For a nestloop, include outer relids in curOuterRels for inner side */
3963 root->curOuterRels = bms_union(root->curOuterRels,
3964 best_path->outerjoinpath->parent->relids);
3966 inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3968 /* Restore curOuterRels */
3969 bms_free(root->curOuterRels);
3970 root->curOuterRels = saveOuterRels;
3972 /* Sort join qual clauses into best execution order */
3973 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3975 /* Get the join qual clauses (in plain expression form) */
3976 /* Any pseudoconstant clauses are ignored here */
3977 if (IS_OUTER_JOIN(best_path->jointype))
3979 extract_actual_join_clauses(joinrestrictclauses,
3980 best_path->path.parent->relids,
3981 &joinclauses, &otherclauses);
3985 /* We can treat all clauses alike for an inner join */
3986 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3990 /* Replace any outer-relation variables with nestloop params */
3991 if (best_path->path.param_info)
3993 joinclauses = (List *)
3994 replace_nestloop_params(root, (Node *) joinclauses);
3995 otherclauses = (List *)
3996 replace_nestloop_params(root, (Node *) otherclauses);
4000 * Identify any nestloop parameters that should be supplied by this join
4001 * node, and remove them from root->curOuterParams.
4003 outerrelids = best_path->outerjoinpath->parent->relids;
4004 nestParams = identify_current_nestloop_params(root, outerrelids);
4006 join_plan = make_nestloop(tlist,
4012 best_path->jointype,
4013 best_path->inner_unique);
4015 copy_generic_path_info(&join_plan->join.plan, &best_path->path);
4021 create_mergejoin_plan(PlannerInfo *root,
4022 MergePath *best_path)
4024 MergeJoin *join_plan;
4027 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4031 List *outerpathkeys;
4032 List *innerpathkeys;
4035 Oid *mergecollations;
4036 int *mergestrategies;
4037 bool *mergenullsfirst;
4039 EquivalenceClass *opeclass;
4044 Path *outer_path = best_path->jpath.outerjoinpath;
4045 Path *inner_path = best_path->jpath.innerjoinpath;
4048 * MergeJoin can project, so we don't have to demand exact tlists from the
4049 * inputs. However, if we're intending to sort an input's result, it's
4050 * best to request a small tlist so we aren't sorting more data than
4053 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4054 (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4056 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4057 (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4059 /* Sort join qual clauses into best execution order */
4060 /* NB: do NOT reorder the mergeclauses */
4061 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4063 /* Get the join qual clauses (in plain expression form) */
4064 /* Any pseudoconstant clauses are ignored here */
4065 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4067 extract_actual_join_clauses(joinclauses,
4068 best_path->jpath.path.parent->relids,
4069 &joinclauses, &otherclauses);
4073 /* We can treat all clauses alike for an inner join */
4074 joinclauses = extract_actual_clauses(joinclauses, false);
4079 * Remove the mergeclauses from the list of join qual clauses, leaving the
4080 * list of quals that must be checked as qpquals.
4082 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
4083 joinclauses = list_difference(joinclauses, mergeclauses);
4086 * Replace any outer-relation variables with nestloop params. There
4087 * should not be any in the mergeclauses.
4089 if (best_path->jpath.path.param_info)
4091 joinclauses = (List *)
4092 replace_nestloop_params(root, (Node *) joinclauses);
4093 otherclauses = (List *)
4094 replace_nestloop_params(root, (Node *) otherclauses);
4098 * Rearrange mergeclauses, if needed, so that the outer variable is always
4099 * on the left; mark the mergeclause restrictinfos with correct
4100 * outer_is_left status.
4102 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
4103 best_path->jpath.outerjoinpath->parent->relids);
4106 * Create explicit sort nodes for the outer and inner paths if necessary.
4108 if (best_path->outersortkeys)
4110 Relids outer_relids = outer_path->parent->relids;
4111 Sort *sort = make_sort_from_pathkeys(outer_plan,
4112 best_path->outersortkeys,
4115 label_sort_with_costsize(root, sort, -1.0);
4116 outer_plan = (Plan *) sort;
4117 outerpathkeys = best_path->outersortkeys;
4120 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
4122 if (best_path->innersortkeys)
4124 Relids inner_relids = inner_path->parent->relids;
4125 Sort *sort = make_sort_from_pathkeys(inner_plan,
4126 best_path->innersortkeys,
4129 label_sort_with_costsize(root, sort, -1.0);
4130 inner_plan = (Plan *) sort;
4131 innerpathkeys = best_path->innersortkeys;
4134 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
4137 * If specified, add a materialize node to shield the inner plan from the
4138 * need to handle mark/restore.
4140 if (best_path->materialize_inner)
4142 Plan *matplan = (Plan *) make_material(inner_plan);
4145 * We assume the materialize will not spill to disk, and therefore
4146 * charge just cpu_operator_cost per tuple. (Keep this estimate in
4147 * sync with final_cost_mergejoin.)
4149 copy_plan_costsize(matplan, inner_plan);
4150 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4152 inner_plan = matplan;
4156 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4157 * executor. The information is in the pathkeys for the two inputs, but
4158 * we need to be careful about the possibility of mergeclauses sharing a
4159 * pathkey, as well as the possibility that the inner pathkeys are not in
4160 * an order matching the mergeclauses.
4162 nClauses = list_length(mergeclauses);
4163 Assert(nClauses == list_length(best_path->path_mergeclauses));
4164 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4165 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4166 mergestrategies = (int *) palloc(nClauses * sizeof(int));
4167 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4171 lop = list_head(outerpathkeys);
4172 lip = list_head(innerpathkeys);
4174 foreach(lc, best_path->path_mergeclauses)
4176 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
4177 EquivalenceClass *oeclass;
4178 EquivalenceClass *ieclass;
4179 PathKey *ipathkey = NULL;
4180 EquivalenceClass *ipeclass = NULL;
4181 bool first_inner_match = false;
4183 /* fetch outer/inner eclass from mergeclause */
4184 if (rinfo->outer_is_left)
4186 oeclass = rinfo->left_ec;
4187 ieclass = rinfo->right_ec;
4191 oeclass = rinfo->right_ec;
4192 ieclass = rinfo->left_ec;
4194 Assert(oeclass != NULL);
4195 Assert(ieclass != NULL);
4198 * We must identify the pathkey elements associated with this clause
4199 * by matching the eclasses (which should give a unique match, since
4200 * the pathkey lists should be canonical). In typical cases the merge
4201 * clauses are one-to-one with the pathkeys, but when dealing with
4202 * partially redundant query conditions, things are more complicated.
4204 * lop and lip reference the first as-yet-unmatched pathkey elements.
4205 * If they're NULL then all pathkey elements have been matched.
4207 * The ordering of the outer pathkeys should match the mergeclauses,
4208 * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4209 * could be more than one mergeclause for the same outer pathkey, but
4210 * no pathkey may be entirely skipped over.
4212 if (oeclass != opeclass) /* multiple matches are not interesting */
4214 /* doesn't match the current opathkey, so must match the next */
4216 elog(ERROR, "outer pathkeys do not match mergeclauses");
4217 opathkey = (PathKey *) lfirst(lop);
4218 opeclass = opathkey->pk_eclass;
4220 if (oeclass != opeclass)
4221 elog(ERROR, "outer pathkeys do not match mergeclauses");
4225 * The inner pathkeys likewise should not have skipped-over keys, but
4226 * it's possible for a mergeclause to reference some earlier inner
4227 * pathkey if we had redundant pathkeys. For example we might have
4228 * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4229 * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4230 * mechanism drops the second sort by x as redundant, and this code
4233 * It's also possible for the implied inner-rel ordering to be like
4234 * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4235 * redundant; but this means that the sort ordering of a redundant
4236 * inner pathkey should not be considered significant. So we must
4237 * detect whether this is the first clause matching an inner pathkey.
4241 ipathkey = (PathKey *) lfirst(lip);
4242 ipeclass = ipathkey->pk_eclass;
4243 if (ieclass == ipeclass)
4245 /* successful first match to this inner pathkey */
4247 first_inner_match = true;
4250 if (!first_inner_match)
4252 /* redundant clause ... must match something before lip */
4255 foreach(l2, innerpathkeys)
4259 ipathkey = (PathKey *) lfirst(l2);
4260 ipeclass = ipathkey->pk_eclass;
4261 if (ieclass == ipeclass)
4264 if (ieclass != ipeclass)
4265 elog(ERROR, "inner pathkeys do not match mergeclauses");
4269 * The pathkeys should always match each other as to opfamily and
4270 * collation (which affect equality), but if we're considering a
4271 * redundant inner pathkey, its sort ordering might not match. In
4272 * such cases we may ignore the inner pathkey's sort ordering and use
4273 * the outer's. (In effect, we're lying to the executor about the
4274 * sort direction of this inner column, but it does not matter since
4275 * the run-time row comparisons would only reach this column when
4276 * there's equality for the earlier column containing the same eclass.
4277 * There could be only one value in this column for the range of inner
4278 * rows having a given value in the earlier column, so it does not
4279 * matter which way we imagine this column to be ordered.) But a
4280 * non-redundant inner pathkey had better match outer's ordering too.
4282 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4283 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4284 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4285 if (first_inner_match &&
4286 (opathkey->pk_strategy != ipathkey->pk_strategy ||
4287 opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4288 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4290 /* OK, save info for executor */
4291 mergefamilies[i] = opathkey->pk_opfamily;
4292 mergecollations[i] = opathkey->pk_eclass->ec_collation;
4293 mergestrategies[i] = opathkey->pk_strategy;
4294 mergenullsfirst[i] = opathkey->pk_nulls_first;
4299 * Note: it is not an error if we have additional pathkey elements (i.e.,
4300 * lop or lip isn't NULL here). The input paths might be better-sorted
4301 * than we need for the current mergejoin.
4305 * Now we can build the mergejoin node.
4307 join_plan = make_mergejoin(tlist,
4317 best_path->jpath.jointype,
4318 best_path->jpath.inner_unique,
4319 best_path->skip_mark_restore);
4321 /* Costs of sort and material steps are included in path cost already */
4322 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4328 create_hashjoin_plan(PlannerInfo *root,
4329 HashPath *best_path)
4331 HashJoin *join_plan;
4335 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4339 Oid skewTable = InvalidOid;
4340 AttrNumber skewColumn = InvalidAttrNumber;
4341 bool skewInherit = false;
4344 * HashJoin can project, so we don't have to demand exact tlists from the
4345 * inputs. However, it's best to request a small tlist from the inner
4346 * side, so that we aren't storing more data than necessary. Likewise, if
4347 * we anticipate batching, request a small tlist from the outer side so
4348 * that we don't put extra data in the outer batch files.
4350 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4351 (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4353 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4356 /* Sort join qual clauses into best execution order */
4357 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4358 /* There's no point in sorting the hash clauses ... */
4360 /* Get the join qual clauses (in plain expression form) */
4361 /* Any pseudoconstant clauses are ignored here */
4362 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4364 extract_actual_join_clauses(joinclauses,
4365 best_path->jpath.path.parent->relids,
4366 &joinclauses, &otherclauses);
4370 /* We can treat all clauses alike for an inner join */
4371 joinclauses = extract_actual_clauses(joinclauses, false);
4376 * Remove the hashclauses from the list of join qual clauses, leaving the
4377 * list of quals that must be checked as qpquals.
4379 hashclauses = get_actual_clauses(best_path->path_hashclauses);
4380 joinclauses = list_difference(joinclauses, hashclauses);
4383 * Replace any outer-relation variables with nestloop params. There
4384 * should not be any in the hashclauses.
4386 if (best_path->jpath.path.param_info)
4388 joinclauses = (List *)
4389 replace_nestloop_params(root, (Node *) joinclauses);
4390 otherclauses = (List *)
4391 replace_nestloop_params(root, (Node *) otherclauses);
4395 * Rearrange hashclauses, if needed, so that the outer variable is always
4398 hashclauses = get_switched_clauses(best_path->path_hashclauses,
4399 best_path->jpath.outerjoinpath->parent->relids);
4402 * If there is a single join clause and we can identify the outer variable
4403 * as a simple column reference, supply its identity for possible use in
4404 * skew optimization. (Note: in principle we could do skew optimization
4405 * with multiple join clauses, but we'd have to be able to determine the
4406 * most common combinations of outer values, which we don't currently have
4407 * enough stats for.)
4409 if (list_length(hashclauses) == 1)
4411 OpExpr *clause = (OpExpr *) linitial(hashclauses);
4414 Assert(is_opclause(clause));
4415 node = (Node *) linitial(clause->args);
4416 if (IsA(node, RelabelType))
4417 node = (Node *) ((RelabelType *) node)->arg;
4420 Var *var = (Var *) node;
4423 rte = root->simple_rte_array[var->varno];
4424 if (rte->rtekind == RTE_RELATION)
4426 skewTable = rte->relid;
4427 skewColumn = var->varattno;
4428 skewInherit = rte->inh;
4434 * Build the hash node and hash join node.
4436 hash_plan = make_hash(inner_plan,
4442 * Set Hash node's startup & total costs equal to total cost of input
4443 * plan; this only affects EXPLAIN display not decisions.
4445 copy_plan_costsize(&hash_plan->plan, inner_plan);
4446 hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4449 * If parallel-aware, the executor will also need an estimate of the total
4450 * number of rows expected from all participants so that it can size the
4451 * shared hash table.
4453 if (best_path->jpath.path.parallel_aware)
4455 hash_plan->plan.parallel_aware = true;
4456 hash_plan->rows_total = best_path->inner_rows_total;
4459 join_plan = make_hashjoin(tlist,
4465 best_path->jpath.jointype,
4466 best_path->jpath.inner_unique);
4468 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4474 /*****************************************************************************
4476 * SUPPORTING ROUTINES
4478 *****************************************************************************/
4481 * replace_nestloop_params
4482 * Replace outer-relation Vars and PlaceHolderVars in the given expression
4483 * with nestloop Params
4485 * All Vars and PlaceHolderVars belonging to the relation(s) identified by
4486 * root->curOuterRels are replaced by Params, and entries are added to
4487 * root->curOuterParams if not already present.
4490 replace_nestloop_params(PlannerInfo *root, Node *expr)
4492 /* No setup needed for tree walk, so away we go */
4493 return replace_nestloop_params_mutator(expr, root);
4497 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
4503 Var *var = (Var *) node;
4505 /* Upper-level Vars should be long gone at this point */
4506 Assert(var->varlevelsup == 0);
4507 /* If not to be replaced, we can just return the Var unmodified */
4508 if (!bms_is_member(var->varno, root->curOuterRels))
4510 /* Replace the Var with a nestloop Param */
4511 return (Node *) replace_nestloop_param_var(root, var);
4513 if (IsA(node, PlaceHolderVar))
4515 PlaceHolderVar *phv = (PlaceHolderVar *) node;
4517 /* Upper-level PlaceHolderVars should be long gone at this point */
4518 Assert(phv->phlevelsup == 0);
4521 * Check whether we need to replace the PHV. We use bms_overlap as a
4522 * cheap/quick test to see if the PHV might be evaluated in the outer
4523 * rels, and then grab its PlaceHolderInfo to tell for sure.
4525 if (!bms_overlap(phv->phrels, root->curOuterRels) ||
4526 !bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
4527 root->curOuterRels))
4530 * We can't replace the whole PHV, but we might still need to
4531 * replace Vars or PHVs within its expression, in case it ends up
4532 * actually getting evaluated here. (It might get evaluated in
4533 * this plan node, or some child node; in the latter case we don't
4534 * really need to process the expression here, but we haven't got
4535 * enough info to tell if that's the case.) Flat-copy the PHV
4536 * node and then recurse on its expression.
4538 * Note that after doing this, we might have different
4539 * representations of the contents of the same PHV in different
4540 * parts of the plan tree. This is OK because equal() will just
4541 * match on phid/phlevelsup, so setrefs.c will still recognize an
4542 * upper-level reference to a lower-level copy of the same PHV.
4544 PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
4546 memcpy(newphv, phv, sizeof(PlaceHolderVar));
4547 newphv->phexpr = (Expr *)
4548 replace_nestloop_params_mutator((Node *) phv->phexpr,
4550 return (Node *) newphv;
4552 /* Replace the PlaceHolderVar with a nestloop Param */
4553 return (Node *) replace_nestloop_param_placeholdervar(root, phv);
4555 return expression_tree_mutator(node,
4556 replace_nestloop_params_mutator,
4561 * fix_indexqual_references
4562 * Adjust indexqual clauses to the form the executor's indexqual
4565 * We have three tasks here:
4566 * * Select the actual qual clauses out of the input IndexClause list,
4567 * and remove RestrictInfo nodes from the qual clauses.
4568 * * Replace any outer-relation Var or PHV nodes with nestloop Params.
4569 * (XXX eventually, that responsibility should go elsewhere?)
4570 * * Index keys must be represented by Var nodes with varattno set to the
4571 * index's attribute number, not the attribute number in the original rel.
4573 * *stripped_indexquals_p receives a list of the actual qual clauses.
4575 * *fixed_indexquals_p receives a list of the adjusted quals. This is a copy
4576 * that shares no substructure with the original; this is needed in case there
4577 * are subplans in it (we need two separate copies of the subplan tree, or
4578 * things will go awry).
4581 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
4582 List **stripped_indexquals_p, List **fixed_indexquals_p)
4584 IndexOptInfo *index = index_path->indexinfo;
4585 List *stripped_indexquals;
4586 List *fixed_indexquals;
4589 stripped_indexquals = fixed_indexquals = NIL;
4591 foreach(lc, index_path->indexclauses)
4593 IndexClause *iclause = lfirst_node(IndexClause, lc);
4594 int indexcol = iclause->indexcol;
4597 foreach(lc2, iclause->indexquals)
4599 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
4600 Node *clause = (Node *) rinfo->clause;
4602 stripped_indexquals = lappend(stripped_indexquals, clause);
4603 clause = fix_indexqual_clause(root, index, indexcol,
4604 clause, iclause->indexcols);
4605 fixed_indexquals = lappend(fixed_indexquals, clause);
4609 *stripped_indexquals_p = stripped_indexquals;
4610 *fixed_indexquals_p = fixed_indexquals;
4614 * fix_indexorderby_references
4615 * Adjust indexorderby clauses to the form the executor's index
4618 * This is a simplified version of fix_indexqual_references. The input is
4619 * bare clauses and a separate indexcol list, instead of IndexClauses.
4622 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
4624 IndexOptInfo *index = index_path->indexinfo;
4625 List *fixed_indexorderbys;
4629 fixed_indexorderbys = NIL;
4631 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
4633 Node *clause = (Node *) lfirst(lcc);
4634 int indexcol = lfirst_int(lci);
4636 clause = fix_indexqual_clause(root, index, indexcol, clause, NIL);
4637 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
4640 return fixed_indexorderbys;
4644 * fix_indexqual_clause
4645 * Convert a single indexqual clause to the form needed by the executor.
4647 * We replace nestloop params here, and replace the index key variables
4648 * or expressions by index Var nodes.
4651 fix_indexqual_clause(PlannerInfo *root, IndexOptInfo *index, int indexcol,
4652 Node *clause, List *indexcolnos)
4655 * Replace any outer-relation variables with nestloop params.
4657 * This also makes a copy of the clause, so it's safe to modify it
4660 clause = replace_nestloop_params(root, clause);
4662 if (IsA(clause, OpExpr))
4664 OpExpr *op = (OpExpr *) clause;
4666 /* Replace the indexkey expression with an index Var. */
4667 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
4671 else if (IsA(clause, RowCompareExpr))
4673 RowCompareExpr *rc = (RowCompareExpr *) clause;
4677 /* Replace the indexkey expressions with index Vars. */
4678 Assert(list_length(rc->largs) == list_length(indexcolnos));
4679 forboth(lca, rc->largs, lcai, indexcolnos)
4681 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
4686 else if (IsA(clause, ScalarArrayOpExpr))
4688 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
4690 /* Replace the indexkey expression with an index Var. */
4691 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
4695 else if (IsA(clause, NullTest))
4697 NullTest *nt = (NullTest *) clause;
4699 /* Replace the indexkey expression with an index Var. */
4700 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
4705 elog(ERROR, "unsupported indexqual type: %d",
4706 (int) nodeTag(clause));
4712 * fix_indexqual_operand
4713 * Convert an indexqual expression to a Var referencing the index column.
4715 * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
4716 * equal to the index's attribute number (index column position).
4718 * Most of the code here is just for sanity cross-checking that the given
4719 * expression actually matches the index column it's claimed to.
4722 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
4726 ListCell *indexpr_item;
4729 * Remove any binary-compatible relabeling of the indexkey
4731 if (IsA(node, RelabelType))
4732 node = (Node *) ((RelabelType *) node)->arg;
4734 Assert(indexcol >= 0 && indexcol < index->ncolumns);
4736 if (index->indexkeys[indexcol] != 0)
4738 /* It's a simple index column */
4739 if (IsA(node, Var) &&
4740 ((Var *) node)->varno == index->rel->relid &&
4741 ((Var *) node)->varattno == index->indexkeys[indexcol])
4743 result = (Var *) copyObject(node);
4744 result->varno = INDEX_VAR;
4745 result->varattno = indexcol + 1;
4746 return (Node *) result;
4749 elog(ERROR, "index key does not match expected index column");
4752 /* It's an index expression, so find and cross-check the expression */
4753 indexpr_item = list_head(index->indexprs);
4754 for (pos = 0; pos < index->ncolumns; pos++)
4756 if (index->indexkeys[pos] == 0)
4758 if (indexpr_item == NULL)
4759 elog(ERROR, "too few entries in indexprs list");
4760 if (pos == indexcol)
4764 indexkey = (Node *) lfirst(indexpr_item);
4765 if (indexkey && IsA(indexkey, RelabelType))
4766 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4767 if (equal(node, indexkey))
4769 result = makeVar(INDEX_VAR, indexcol + 1,
4770 exprType(lfirst(indexpr_item)), -1,
4771 exprCollation(lfirst(indexpr_item)),
4773 return (Node *) result;
4776 elog(ERROR, "index key does not match expected index column");
4778 indexpr_item = lnext(indexpr_item);
4783 elog(ERROR, "index key does not match expected index column");
4784 return NULL; /* keep compiler quiet */
4788 * get_switched_clauses
4789 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
4790 * extract the bare clauses, and rearrange the elements within the
4791 * clauses, if needed, so the outer join variable is on the left and
4792 * the inner is on the right. The original clause data structure is not
4793 * touched; a modified list is returned. We do, however, set the transient
4794 * outer_is_left field in each RestrictInfo to show which side was which.
4797 get_switched_clauses(List *clauses, Relids outerrelids)
4804 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
4805 OpExpr *clause = (OpExpr *) restrictinfo->clause;
4807 Assert(is_opclause(clause));
4808 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
4811 * Duplicate just enough of the structure to allow commuting the
4812 * clause without changing the original list. Could use
4813 * copyObject, but a complete deep copy is overkill.
4815 OpExpr *temp = makeNode(OpExpr);
4817 temp->opno = clause->opno;
4818 temp->opfuncid = InvalidOid;
4819 temp->opresulttype = clause->opresulttype;
4820 temp->opretset = clause->opretset;
4821 temp->opcollid = clause->opcollid;
4822 temp->inputcollid = clause->inputcollid;
4823 temp->args = list_copy(clause->args);
4824 temp->location = clause->location;
4825 /* Commute it --- note this modifies the temp node in-place. */
4826 CommuteOpExpr(temp);
4827 t_list = lappend(t_list, temp);
4828 restrictinfo->outer_is_left = false;
4832 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
4833 t_list = lappend(t_list, clause);
4834 restrictinfo->outer_is_left = true;
4841 * order_qual_clauses
4842 * Given a list of qual clauses that will all be evaluated at the same
4843 * plan node, sort the list into the order we want to check the quals
4846 * When security barrier quals are used in the query, we may have quals with
4847 * different security levels in the list. Quals of lower security_level
4848 * must go before quals of higher security_level, except that we can grant
4849 * exceptions to move up quals that are leakproof. When security level
4850 * doesn't force the decision, we prefer to order clauses by estimated
4851 * execution cost, cheapest first.
4853 * Ideally the order should be driven by a combination of execution cost and
4854 * selectivity, but it's not immediately clear how to account for both,
4855 * and given the uncertainty of the estimates the reliability of the decisions
4856 * would be doubtful anyway. So we just order by security level then
4857 * estimated per-tuple cost, being careful not to change the order when
4858 * (as is often the case) the estimates are identical.
4860 * Although this will work on either bare clauses or RestrictInfos, it's
4861 * much faster to apply it to RestrictInfos, since it can re-use cost
4862 * information that is cached in RestrictInfos. XXX in the bare-clause
4863 * case, we are also not able to apply security considerations. That is
4864 * all right for the moment, because the bare-clause case doesn't occur
4865 * anywhere that barrier quals could be present, but it would be better to
4868 * Note: some callers pass lists that contain entries that will later be
4869 * removed; this is the easiest way to let this routine see RestrictInfos
4870 * instead of bare clauses. This is another reason why trying to consider
4871 * selectivity in the ordering would likely do the wrong thing.
4874 order_qual_clauses(PlannerInfo *root, List *clauses)
4880 Index security_level;
4882 int nitems = list_length(clauses);
4888 /* No need to work hard for 0 or 1 clause */
4893 * Collect the items and costs into an array. This is to avoid repeated
4894 * cost_qual_eval work if the inputs aren't RestrictInfos.
4896 items = (QualItem *) palloc(nitems * sizeof(QualItem));
4898 foreach(lc, clauses)
4900 Node *clause = (Node *) lfirst(lc);
4903 cost_qual_eval_node(&qcost, clause, root);
4904 items[i].clause = clause;
4905 items[i].cost = qcost.per_tuple;
4906 if (IsA(clause, RestrictInfo))
4908 RestrictInfo *rinfo = (RestrictInfo *) clause;
4911 * If a clause is leakproof, it doesn't have to be constrained by
4912 * its nominal security level. If it's also reasonably cheap
4913 * (here defined as 10X cpu_operator_cost), pretend it has
4914 * security_level 0, which will allow it to go in front of
4915 * more-expensive quals of lower security levels. Of course, that
4916 * will also force it to go in front of cheaper quals of its own
4917 * security level, which is not so great, but we can alleviate
4918 * that risk by applying the cost limit cutoff.
4920 if (rinfo->leakproof && items[i].cost < 10 * cpu_operator_cost)
4921 items[i].security_level = 0;
4923 items[i].security_level = rinfo->security_level;
4926 items[i].security_level = 0;
4931 * Sort. We don't use qsort() because it's not guaranteed stable for
4932 * equal keys. The expected number of entries is small enough that a
4933 * simple insertion sort should be good enough.
4935 for (i = 1; i < nitems; i++)
4937 QualItem newitem = items[i];
4940 /* insert newitem into the already-sorted subarray */
4941 for (j = i; j > 0; j--)
4943 QualItem *olditem = &items[j - 1];
4945 if (newitem.security_level > olditem->security_level ||
4946 (newitem.security_level == olditem->security_level &&
4947 newitem.cost >= olditem->cost))
4949 items[j] = *olditem;
4954 /* Convert back to a list */
4956 for (i = 0; i < nitems; i++)
4957 result = lappend(result, items[i].clause);
4963 * Copy cost and size info from a Path node to the Plan node created from it.
4964 * The executor usually won't use this info, but it's needed by EXPLAIN.
4965 * Also copy the parallel-related flags, which the executor *will* use.
4968 copy_generic_path_info(Plan *dest, Path *src)
4970 dest->startup_cost = src->startup_cost;
4971 dest->total_cost = src->total_cost;
4972 dest->plan_rows = src->rows;
4973 dest->plan_width = src->pathtarget->width;
4974 dest->parallel_aware = src->parallel_aware;
4975 dest->parallel_safe = src->parallel_safe;
4979 * Copy cost and size info from a lower plan node to an inserted node.
4980 * (Most callers alter the info after copying it.)
4983 copy_plan_costsize(Plan *dest, Plan *src)
4985 dest->startup_cost = src->startup_cost;
4986 dest->total_cost = src->total_cost;
4987 dest->plan_rows = src->plan_rows;
4988 dest->plan_width = src->plan_width;
4989 /* Assume the inserted node is not parallel-aware. */
4990 dest->parallel_aware = false;
4991 /* Assume the inserted node is parallel-safe, if child plan is. */
4992 dest->parallel_safe = src->parallel_safe;
4996 * Some places in this file build Sort nodes that don't have a directly
4997 * corresponding Path node. The cost of the sort is, or should have been,
4998 * included in the cost of the Path node we're working from, but since it's
4999 * not split out, we have to re-figure it using cost_sort(). This is just
5000 * to label the Sort node nicely for EXPLAIN.
5002 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
5005 label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
5007 Plan *lefttree = plan->plan.lefttree;
5008 Path sort_path; /* dummy for result of cost_sort */
5010 cost_sort(&sort_path, root, NIL,
5011 lefttree->total_cost,
5012 lefttree->plan_rows,
5013 lefttree->plan_width,
5017 plan->plan.startup_cost = sort_path.startup_cost;
5018 plan->plan.total_cost = sort_path.total_cost;
5019 plan->plan.plan_rows = lefttree->plan_rows;
5020 plan->plan.plan_width = lefttree->plan_width;
5021 plan->plan.parallel_aware = false;
5022 plan->plan.parallel_safe = lefttree->parallel_safe;
5026 * bitmap_subplan_mark_shared
5027 * Set isshared flag in bitmap subplan so that it will be created in
5031 bitmap_subplan_mark_shared(Plan *plan)
5033 if (IsA(plan, BitmapAnd))
5034 bitmap_subplan_mark_shared(
5035 linitial(((BitmapAnd *) plan)->bitmapplans));
5036 else if (IsA(plan, BitmapOr))
5038 ((BitmapOr *) plan)->isshared = true;
5039 bitmap_subplan_mark_shared(
5040 linitial(((BitmapOr *) plan)->bitmapplans));
5042 else if (IsA(plan, BitmapIndexScan))
5043 ((BitmapIndexScan *) plan)->isshared = true;
5045 elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5048 /*****************************************************************************
5050 * PLAN NODE BUILDING ROUTINES
5052 * In general, these functions are not passed the original Path and therefore
5053 * leave it to the caller to fill in the cost/width fields from the Path,
5054 * typically by calling copy_generic_path_info(). This convention is
5055 * somewhat historical, but it does support a few places above where we build
5056 * a plan node without having an exactly corresponding Path node. Under no
5057 * circumstances should one of these functions do its own cost calculations,
5058 * as that would be redundant with calculations done while building Paths.
5060 *****************************************************************************/
5063 make_seqscan(List *qptlist,
5067 SeqScan *node = makeNode(SeqScan);
5068 Plan *plan = &node->plan;
5070 plan->targetlist = qptlist;
5071 plan->qual = qpqual;
5072 plan->lefttree = NULL;
5073 plan->righttree = NULL;
5074 node->scanrelid = scanrelid;
5080 make_samplescan(List *qptlist,
5083 TableSampleClause *tsc)
5085 SampleScan *node = makeNode(SampleScan);
5086 Plan *plan = &node->scan.plan;
5088 plan->targetlist = qptlist;
5089 plan->qual = qpqual;
5090 plan->lefttree = NULL;
5091 plan->righttree = NULL;
5092 node->scan.scanrelid = scanrelid;
5093 node->tablesample = tsc;
5099 make_indexscan(List *qptlist,
5104 List *indexqualorig,
5106 List *indexorderbyorig,
5107 List *indexorderbyops,
5108 ScanDirection indexscandir)
5110 IndexScan *node = makeNode(IndexScan);
5111 Plan *plan = &node->scan.plan;
5113 plan->targetlist = qptlist;
5114 plan->qual = qpqual;
5115 plan->lefttree = NULL;
5116 plan->righttree = NULL;
5117 node->scan.scanrelid = scanrelid;
5118 node->indexid = indexid;
5119 node->indexqual = indexqual;
5120 node->indexqualorig = indexqualorig;
5121 node->indexorderby = indexorderby;
5122 node->indexorderbyorig = indexorderbyorig;
5123 node->indexorderbyops = indexorderbyops;
5124 node->indexorderdir = indexscandir;
5129 static IndexOnlyScan *
5130 make_indexonlyscan(List *qptlist,
5137 ScanDirection indexscandir)
5139 IndexOnlyScan *node = makeNode(IndexOnlyScan);
5140 Plan *plan = &node->scan.plan;
5142 plan->targetlist = qptlist;
5143 plan->qual = qpqual;
5144 plan->lefttree = NULL;
5145 plan->righttree = NULL;
5146 node->scan.scanrelid = scanrelid;
5147 node->indexid = indexid;
5148 node->indexqual = indexqual;
5149 node->indexorderby = indexorderby;
5150 node->indextlist = indextlist;
5151 node->indexorderdir = indexscandir;
5156 static BitmapIndexScan *
5157 make_bitmap_indexscan(Index scanrelid,
5160 List *indexqualorig)
5162 BitmapIndexScan *node = makeNode(BitmapIndexScan);
5163 Plan *plan = &node->scan.plan;
5165 plan->targetlist = NIL; /* not used */
5166 plan->qual = NIL; /* not used */
5167 plan->lefttree = NULL;
5168 plan->righttree = NULL;
5169 node->scan.scanrelid = scanrelid;
5170 node->indexid = indexid;
5171 node->indexqual = indexqual;
5172 node->indexqualorig = indexqualorig;
5177 static BitmapHeapScan *
5178 make_bitmap_heapscan(List *qptlist,
5181 List *bitmapqualorig,
5184 BitmapHeapScan *node = makeNode(BitmapHeapScan);
5185 Plan *plan = &node->scan.plan;
5187 plan->targetlist = qptlist;
5188 plan->qual = qpqual;
5189 plan->lefttree = lefttree;
5190 plan->righttree = NULL;
5191 node->scan.scanrelid = scanrelid;
5192 node->bitmapqualorig = bitmapqualorig;
5198 make_tidscan(List *qptlist,
5203 TidScan *node = makeNode(TidScan);
5204 Plan *plan = &node->scan.plan;
5206 plan->targetlist = qptlist;
5207 plan->qual = qpqual;
5208 plan->lefttree = NULL;
5209 plan->righttree = NULL;
5210 node->scan.scanrelid = scanrelid;
5211 node->tidquals = tidquals;
5216 static SubqueryScan *
5217 make_subqueryscan(List *qptlist,
5222 SubqueryScan *node = makeNode(SubqueryScan);
5223 Plan *plan = &node->scan.plan;
5225 plan->targetlist = qptlist;
5226 plan->qual = qpqual;
5227 plan->lefttree = NULL;
5228 plan->righttree = NULL;
5229 node->scan.scanrelid = scanrelid;
5230 node->subplan = subplan;
5235 static FunctionScan *
5236 make_functionscan(List *qptlist,
5240 bool funcordinality)
5242 FunctionScan *node = makeNode(FunctionScan);
5243 Plan *plan = &node->scan.plan;
5245 plan->targetlist = qptlist;
5246 plan->qual = qpqual;
5247 plan->lefttree = NULL;
5248 plan->righttree = NULL;
5249 node->scan.scanrelid = scanrelid;
5250 node->functions = functions;
5251 node->funcordinality = funcordinality;
5256 static TableFuncScan *
5257 make_tablefuncscan(List *qptlist,
5260 TableFunc *tablefunc)
5262 TableFuncScan *node = makeNode(TableFuncScan);
5263 Plan *plan = &node->scan.plan;
5265 plan->targetlist = qptlist;
5266 plan->qual = qpqual;
5267 plan->lefttree = NULL;
5268 plan->righttree = NULL;
5269 node->scan.scanrelid = scanrelid;
5270 node->tablefunc = tablefunc;
5276 make_valuesscan(List *qptlist,
5281 ValuesScan *node = makeNode(ValuesScan);
5282 Plan *plan = &node->scan.plan;
5284 plan->targetlist = qptlist;
5285 plan->qual = qpqual;
5286 plan->lefttree = NULL;
5287 plan->righttree = NULL;
5288 node->scan.scanrelid = scanrelid;
5289 node->values_lists = values_lists;
5295 make_ctescan(List *qptlist,
5301 CteScan *node = makeNode(CteScan);
5302 Plan *plan = &node->scan.plan;
5304 plan->targetlist = qptlist;
5305 plan->qual = qpqual;
5306 plan->lefttree = NULL;
5307 plan->righttree = NULL;
5308 node->scan.scanrelid = scanrelid;
5309 node->ctePlanId = ctePlanId;
5310 node->cteParam = cteParam;
5315 static NamedTuplestoreScan *
5316 make_namedtuplestorescan(List *qptlist,
5321 NamedTuplestoreScan *node = makeNode(NamedTuplestoreScan);
5322 Plan *plan = &node->scan.plan;
5324 /* cost should be inserted by caller */
5325 plan->targetlist = qptlist;
5326 plan->qual = qpqual;
5327 plan->lefttree = NULL;
5328 plan->righttree = NULL;
5329 node->scan.scanrelid = scanrelid;
5330 node->enrname = enrname;
5335 static WorkTableScan *
5336 make_worktablescan(List *qptlist,
5341 WorkTableScan *node = makeNode(WorkTableScan);
5342 Plan *plan = &node->scan.plan;
5344 plan->targetlist = qptlist;
5345 plan->qual = qpqual;
5346 plan->lefttree = NULL;
5347 plan->righttree = NULL;
5348 node->scan.scanrelid = scanrelid;
5349 node->wtParam = wtParam;
5355 make_foreignscan(List *qptlist,
5360 List *fdw_scan_tlist,
5361 List *fdw_recheck_quals,
5364 ForeignScan *node = makeNode(ForeignScan);
5365 Plan *plan = &node->scan.plan;
5367 /* cost will be filled in by create_foreignscan_plan */
5368 plan->targetlist = qptlist;
5369 plan->qual = qpqual;
5370 plan->lefttree = outer_plan;
5371 plan->righttree = NULL;
5372 node->scan.scanrelid = scanrelid;
5373 node->operation = CMD_SELECT;
5374 /* fs_server will be filled in by create_foreignscan_plan */
5375 node->fs_server = InvalidOid;
5376 node->fdw_exprs = fdw_exprs;
5377 node->fdw_private = fdw_private;
5378 node->fdw_scan_tlist = fdw_scan_tlist;
5379 node->fdw_recheck_quals = fdw_recheck_quals;
5380 /* fs_relids will be filled in by create_foreignscan_plan */
5381 node->fs_relids = NULL;
5382 /* fsSystemCol will be filled in by create_foreignscan_plan */
5383 node->fsSystemCol = false;
5388 static RecursiveUnion *
5389 make_recursive_union(List *tlist,
5396 RecursiveUnion *node = makeNode(RecursiveUnion);
5397 Plan *plan = &node->plan;
5398 int numCols = list_length(distinctList);
5400 plan->targetlist = tlist;
5402 plan->lefttree = lefttree;
5403 plan->righttree = righttree;
5404 node->wtParam = wtParam;
5407 * convert SortGroupClause list into arrays of attr indexes and equality
5408 * operators, as wanted by executor
5410 node->numCols = numCols;
5414 AttrNumber *dupColIdx;
5419 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
5420 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
5421 dupCollations = (Oid *) palloc(sizeof(Oid) * numCols);
5423 foreach(slitem, distinctList)
5425 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
5426 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
5429 dupColIdx[keyno] = tle->resno;
5430 dupOperators[keyno] = sortcl->eqop;
5431 dupCollations[keyno] = exprCollation((Node *) tle->expr);
5432 Assert(OidIsValid(dupOperators[keyno]));
5435 node->dupColIdx = dupColIdx;
5436 node->dupOperators = dupOperators;
5437 node->dupCollations = dupCollations;
5439 node->numGroups = numGroups;
5445 make_bitmap_and(List *bitmapplans)
5447 BitmapAnd *node = makeNode(BitmapAnd);
5448 Plan *plan = &node->plan;
5450 plan->targetlist = NIL;
5452 plan->lefttree = NULL;
5453 plan->righttree = NULL;
5454 node->bitmapplans = bitmapplans;
5460 make_bitmap_or(List *bitmapplans)
5462 BitmapOr *node = makeNode(BitmapOr);
5463 Plan *plan = &node->plan;
5465 plan->targetlist = NIL;
5467 plan->lefttree = NULL;
5468 plan->righttree = NULL;
5469 node->bitmapplans = bitmapplans;
5475 make_nestloop(List *tlist,
5484 NestLoop *node = makeNode(NestLoop);
5485 Plan *plan = &node->join.plan;
5487 plan->targetlist = tlist;
5488 plan->qual = otherclauses;
5489 plan->lefttree = lefttree;
5490 plan->righttree = righttree;
5491 node->join.jointype = jointype;
5492 node->join.inner_unique = inner_unique;
5493 node->join.joinqual = joinclauses;
5494 node->nestParams = nestParams;
5500 make_hashjoin(List *tlist,
5509 HashJoin *node = makeNode(HashJoin);
5510 Plan *plan = &node->join.plan;
5512 plan->targetlist = tlist;
5513 plan->qual = otherclauses;
5514 plan->lefttree = lefttree;
5515 plan->righttree = righttree;
5516 node->hashclauses = hashclauses;
5517 node->join.jointype = jointype;
5518 node->join.inner_unique = inner_unique;
5519 node->join.joinqual = joinclauses;
5525 make_hash(Plan *lefttree,
5527 AttrNumber skewColumn,
5530 Hash *node = makeNode(Hash);
5531 Plan *plan = &node->plan;
5533 plan->targetlist = lefttree->targetlist;
5535 plan->lefttree = lefttree;
5536 plan->righttree = NULL;
5538 node->skewTable = skewTable;
5539 node->skewColumn = skewColumn;
5540 node->skewInherit = skewInherit;
5546 make_mergejoin(List *tlist,
5551 Oid *mergecollations,
5552 int *mergestrategies,
5553 bool *mergenullsfirst,
5558 bool skip_mark_restore)
5560 MergeJoin *node = makeNode(MergeJoin);
5561 Plan *plan = &node->join.plan;
5563 plan->targetlist = tlist;
5564 plan->qual = otherclauses;
5565 plan->lefttree = lefttree;
5566 plan->righttree = righttree;
5567 node->skip_mark_restore = skip_mark_restore;
5568 node->mergeclauses = mergeclauses;
5569 node->mergeFamilies = mergefamilies;
5570 node->mergeCollations = mergecollations;
5571 node->mergeStrategies = mergestrategies;
5572 node->mergeNullsFirst = mergenullsfirst;
5573 node->join.jointype = jointype;
5574 node->join.inner_unique = inner_unique;
5575 node->join.joinqual = joinclauses;
5581 * make_sort --- basic routine to build a Sort plan node
5583 * Caller must have built the sortColIdx, sortOperators, collations, and
5584 * nullsFirst arrays already.
5587 make_sort(Plan *lefttree, int numCols,
5588 AttrNumber *sortColIdx, Oid *sortOperators,
5589 Oid *collations, bool *nullsFirst)
5591 Sort *node = makeNode(Sort);
5592 Plan *plan = &node->plan;
5594 plan->targetlist = lefttree->targetlist;
5596 plan->lefttree = lefttree;
5597 plan->righttree = NULL;
5598 node->numCols = numCols;
5599 node->sortColIdx = sortColIdx;
5600 node->sortOperators = sortOperators;
5601 node->collations = collations;
5602 node->nullsFirst = nullsFirst;
5608 * prepare_sort_from_pathkeys
5609 * Prepare to sort according to given pathkeys
5611 * This is used to set up for Sort, MergeAppend, and Gather Merge nodes. It
5612 * calculates the executor's representation of the sort key information, and
5613 * adjusts the plan targetlist if needed to add resjunk sort columns.
5616 * 'lefttree' is the plan node which yields input tuples
5617 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
5618 * 'relids' identifies the child relation being sorted, if any
5619 * 'reqColIdx' is NULL or an array of required sort key column numbers
5620 * 'adjust_tlist_in_place' is true if lefttree must be modified in-place
5622 * We must convert the pathkey information into arrays of sort key column
5623 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
5624 * which is the representation the executor wants. These are returned into
5625 * the output parameters *p_numsortkeys etc.
5627 * When looking for matches to an EquivalenceClass's members, we will only
5628 * consider child EC members if they belong to given 'relids'. This protects
5629 * against possible incorrect matches to child expressions that contain no
5632 * If reqColIdx isn't NULL then it contains sort key column numbers that
5633 * we should match. This is used when making child plans for a MergeAppend;
5634 * it's an error if we can't match the columns.
5636 * If the pathkeys include expressions that aren't simple Vars, we will
5637 * usually need to add resjunk items to the input plan's targetlist to
5638 * compute these expressions, since a Sort or MergeAppend node itself won't
5639 * do any such calculations. If the input plan type isn't one that can do
5640 * projections, this means adding a Result node just to do the projection.
5641 * However, the caller can pass adjust_tlist_in_place = true to force the
5642 * lefttree tlist to be modified in-place regardless of whether the node type
5643 * can project --- we use this for fixing the tlist of MergeAppend itself.
5645 * Returns the node which is to be the input to the Sort (either lefttree,
5646 * or a Result stacked atop lefttree).
5649 prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
5651 const AttrNumber *reqColIdx,
5652 bool adjust_tlist_in_place,
5654 AttrNumber **p_sortColIdx,
5655 Oid **p_sortOperators,
5657 bool **p_nullsFirst)
5659 List *tlist = lefttree->targetlist;
5662 AttrNumber *sortColIdx;
5668 * We will need at most list_length(pathkeys) sort columns; possibly less
5670 numsortkeys = list_length(pathkeys);
5671 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5672 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5673 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5674 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5678 foreach(i, pathkeys)
5680 PathKey *pathkey = (PathKey *) lfirst(i);
5681 EquivalenceClass *ec = pathkey->pk_eclass;
5682 EquivalenceMember *em;
5683 TargetEntry *tle = NULL;
5684 Oid pk_datatype = InvalidOid;
5688 if (ec->ec_has_volatile)
5691 * If the pathkey's EquivalenceClass is volatile, then it must
5692 * have come from an ORDER BY clause, and we have to match it to
5693 * that same targetlist entry.
5695 if (ec->ec_sortref == 0) /* can't happen */
5696 elog(ERROR, "volatile EquivalenceClass has no sortref");
5697 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
5699 Assert(list_length(ec->ec_members) == 1);
5700 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
5702 else if (reqColIdx != NULL)
5705 * If we are given a sort column number to match, only consider
5706 * the single TLE at that position. It's possible that there is
5707 * no such TLE, in which case fall through and generate a resjunk
5708 * targetentry (we assume this must have happened in the parent
5709 * plan as well). If there is a TLE but it doesn't match the
5710 * pathkey's EC, we do the same, which is probably the wrong thing
5711 * but we'll leave it to caller to complain about the mismatch.
5713 tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
5716 em = find_ec_member_for_tle(ec, tle, relids);
5719 /* found expr at right place in tlist */
5720 pk_datatype = em->em_datatype;
5729 * Otherwise, we can sort by any non-constant expression listed in
5730 * the pathkey's EquivalenceClass. For now, we take the first
5731 * tlist item found in the EC. If there's no match, we'll generate
5732 * a resjunk entry using the first EC member that is an expression
5733 * in the input's vars. (The non-const restriction only matters
5734 * if the EC is below_outer_join; but if it isn't, it won't
5735 * contain consts anyway, else we'd have discarded the pathkey as
5738 * XXX if we have a choice, is there any way of figuring out which
5739 * might be cheapest to execute? (For example, int4lt is likely
5740 * much cheaper to execute than numericlt, but both might appear
5741 * in the same equivalence class...) Not clear that we ever will
5742 * have an interesting choice in practice, so it may not matter.
5746 tle = (TargetEntry *) lfirst(j);
5747 em = find_ec_member_for_tle(ec, tle, relids);
5750 /* found expr already in tlist */
5751 pk_datatype = em->em_datatype;
5761 * No matching tlist item; look for a computable expression. Note
5762 * that we treat Aggrefs as if they were variables; this is
5763 * necessary when attempting to sort the output from an Agg node
5764 * for use in a WindowFunc (since grouping_planner will have
5765 * treated the Aggrefs as variables, too). Likewise, if we find a
5766 * WindowFunc in a sort expression, treat it as a variable.
5768 Expr *sortexpr = NULL;
5770 foreach(j, ec->ec_members)
5772 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
5777 * We shouldn't be trying to sort by an equivalence class that
5778 * contains a constant, so no need to consider such cases any
5781 if (em->em_is_const)
5785 * Ignore child members unless they belong to the rel being
5788 if (em->em_is_child &&
5789 !bms_is_subset(em->em_relids, relids))
5792 sortexpr = em->em_expr;
5793 exprvars = pull_var_clause((Node *) sortexpr,
5794 PVC_INCLUDE_AGGREGATES |
5795 PVC_INCLUDE_WINDOWFUNCS |
5796 PVC_INCLUDE_PLACEHOLDERS);
5797 foreach(k, exprvars)
5799 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
5802 list_free(exprvars);
5805 pk_datatype = em->em_datatype;
5806 break; /* found usable expression */
5810 elog(ERROR, "could not find pathkey item to sort");
5813 * Do we need to insert a Result node?
5815 if (!adjust_tlist_in_place &&
5816 !is_projection_capable_plan(lefttree))
5818 /* copy needed so we don't modify input's tlist below */
5819 tlist = copyObject(tlist);
5820 lefttree = inject_projection_plan(lefttree, tlist,
5821 lefttree->parallel_safe);
5824 /* Don't bother testing is_projection_capable_plan again */
5825 adjust_tlist_in_place = true;
5828 * Add resjunk entry to input's tlist
5830 tle = makeTargetEntry(sortexpr,
5831 list_length(tlist) + 1,
5834 tlist = lappend(tlist, tle);
5835 lefttree->targetlist = tlist; /* just in case NIL before */
5839 * Look up the correct sort operator from the PathKey's slightly
5840 * abstracted representation.
5842 sortop = get_opfamily_member(pathkey->pk_opfamily,
5845 pathkey->pk_strategy);
5846 if (!OidIsValid(sortop)) /* should not happen */
5847 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
5848 pathkey->pk_strategy, pk_datatype, pk_datatype,
5849 pathkey->pk_opfamily);
5851 /* Add the column to the sort arrays */
5852 sortColIdx[numsortkeys] = tle->resno;
5853 sortOperators[numsortkeys] = sortop;
5854 collations[numsortkeys] = ec->ec_collation;
5855 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
5859 /* Return results */
5860 *p_numsortkeys = numsortkeys;
5861 *p_sortColIdx = sortColIdx;
5862 *p_sortOperators = sortOperators;
5863 *p_collations = collations;
5864 *p_nullsFirst = nullsFirst;
5870 * find_ec_member_for_tle
5871 * Locate an EquivalenceClass member matching the given TLE, if any
5873 * Child EC members are ignored unless they belong to given 'relids'.
5875 static EquivalenceMember *
5876 find_ec_member_for_tle(EquivalenceClass *ec,
5883 /* We ignore binary-compatible relabeling on both ends */
5885 while (tlexpr && IsA(tlexpr, RelabelType))
5886 tlexpr = ((RelabelType *) tlexpr)->arg;
5888 foreach(lc, ec->ec_members)
5890 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
5894 * We shouldn't be trying to sort by an equivalence class that
5895 * contains a constant, so no need to consider such cases any further.
5897 if (em->em_is_const)
5901 * Ignore child members unless they belong to the rel being sorted.
5903 if (em->em_is_child &&
5904 !bms_is_subset(em->em_relids, relids))
5907 /* Match if same expression (after stripping relabel) */
5908 emexpr = em->em_expr;
5909 while (emexpr && IsA(emexpr, RelabelType))
5910 emexpr = ((RelabelType *) emexpr)->arg;
5912 if (equal(emexpr, tlexpr))
5920 * make_sort_from_pathkeys
5921 * Create sort plan to sort according to given pathkeys
5923 * 'lefttree' is the node which yields input tuples
5924 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
5925 * 'relids' is the set of relations required by prepare_sort_from_pathkeys()
5928 make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
5931 AttrNumber *sortColIdx;
5936 /* Compute sort column info, and adjust lefttree as needed */
5937 lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
5947 /* Now build the Sort node */
5948 return make_sort(lefttree, numsortkeys,
5949 sortColIdx, sortOperators,
5950 collations, nullsFirst);
5954 * make_sort_from_sortclauses
5955 * Create sort plan to sort according to given sortclauses
5957 * 'sortcls' is a list of SortGroupClauses
5958 * 'lefttree' is the node which yields input tuples
5961 make_sort_from_sortclauses(List *sortcls, Plan *lefttree)
5963 List *sub_tlist = lefttree->targetlist;
5966 AttrNumber *sortColIdx;
5971 /* Convert list-ish representation to arrays wanted by executor */
5972 numsortkeys = list_length(sortcls);
5973 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5974 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5975 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5976 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5981 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
5982 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
5984 sortColIdx[numsortkeys] = tle->resno;
5985 sortOperators[numsortkeys] = sortcl->sortop;
5986 collations[numsortkeys] = exprCollation((Node *) tle->expr);
5987 nullsFirst[numsortkeys] = sortcl->nulls_first;
5991 return make_sort(lefttree, numsortkeys,
5992 sortColIdx, sortOperators,
5993 collations, nullsFirst);
5997 * make_sort_from_groupcols
5998 * Create sort plan to sort based on grouping columns
6000 * 'groupcls' is the list of SortGroupClauses
6001 * 'grpColIdx' gives the column numbers to use
6003 * This might look like it could be merged with make_sort_from_sortclauses,
6004 * but presently we *must* use the grpColIdx[] array to locate sort columns,
6005 * because the child plan's tlist is not marked with ressortgroupref info
6006 * appropriate to the grouping node. So, only the sort ordering info
6007 * is used from the SortGroupClause entries.
6010 make_sort_from_groupcols(List *groupcls,
6011 AttrNumber *grpColIdx,
6014 List *sub_tlist = lefttree->targetlist;
6017 AttrNumber *sortColIdx;
6022 /* Convert list-ish representation to arrays wanted by executor */
6023 numsortkeys = list_length(groupcls);
6024 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6025 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6026 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6027 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6030 foreach(l, groupcls)
6032 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
6033 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
6036 elog(ERROR, "could not retrieve tle for sort-from-groupcols");
6038 sortColIdx[numsortkeys] = tle->resno;
6039 sortOperators[numsortkeys] = grpcl->sortop;
6040 collations[numsortkeys] = exprCollation((Node *) tle->expr);
6041 nullsFirst[numsortkeys] = grpcl->nulls_first;
6045 return make_sort(lefttree, numsortkeys,
6046 sortColIdx, sortOperators,
6047 collations, nullsFirst);
6051 make_material(Plan *lefttree)
6053 Material *node = makeNode(Material);
6054 Plan *plan = &node->plan;
6056 plan->targetlist = lefttree->targetlist;
6058 plan->lefttree = lefttree;
6059 plan->righttree = NULL;
6065 * materialize_finished_plan: stick a Material node atop a completed plan
6067 * There are a couple of places where we want to attach a Material node
6068 * after completion of create_plan(), without any MaterialPath path.
6069 * Those places should probably be refactored someday to do this on the
6070 * Path representation, but it's not worth the trouble yet.
6073 materialize_finished_plan(Plan *subplan)
6076 Path matpath; /* dummy for result of cost_material */
6078 matplan = (Plan *) make_material(subplan);
6081 * XXX horrid kluge: if there are any initPlans attached to the subplan,
6082 * move them up to the Material node, which is now effectively the top
6083 * plan node in its query level. This prevents failure in
6084 * SS_finalize_plan(), which see for comments. We don't bother adjusting
6085 * the subplan's cost estimate for this.
6087 matplan->initPlan = subplan->initPlan;
6088 subplan->initPlan = NIL;
6091 cost_material(&matpath,
6092 subplan->startup_cost,
6093 subplan->total_cost,
6095 subplan->plan_width);
6096 matplan->startup_cost = matpath.startup_cost;
6097 matplan->total_cost = matpath.total_cost;
6098 matplan->plan_rows = subplan->plan_rows;
6099 matplan->plan_width = subplan->plan_width;
6100 matplan->parallel_aware = false;
6101 matplan->parallel_safe = subplan->parallel_safe;
6107 make_agg(List *tlist, List *qual,
6108 AggStrategy aggstrategy, AggSplit aggsplit,
6109 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations,
6110 List *groupingSets, List *chain,
6111 double dNumGroups, Plan *lefttree)
6113 Agg *node = makeNode(Agg);
6114 Plan *plan = &node->plan;
6117 /* Reduce to long, but 'ware overflow! */
6118 numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
6120 node->aggstrategy = aggstrategy;
6121 node->aggsplit = aggsplit;
6122 node->numCols = numGroupCols;
6123 node->grpColIdx = grpColIdx;
6124 node->grpOperators = grpOperators;
6125 node->grpCollations = grpCollations;
6126 node->numGroups = numGroups;
6127 node->aggParams = NULL; /* SS_finalize_plan() will fill this */
6128 node->groupingSets = groupingSets;
6129 node->chain = chain;
6132 plan->targetlist = tlist;
6133 plan->lefttree = lefttree;
6134 plan->righttree = NULL;
6140 make_windowagg(List *tlist, Index winref,
6141 int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations,
6142 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations,
6143 int frameOptions, Node *startOffset, Node *endOffset,
6144 Oid startInRangeFunc, Oid endInRangeFunc,
6145 Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst,
6148 WindowAgg *node = makeNode(WindowAgg);
6149 Plan *plan = &node->plan;
6151 node->winref = winref;
6152 node->partNumCols = partNumCols;
6153 node->partColIdx = partColIdx;
6154 node->partOperators = partOperators;
6155 node->partCollations = partCollations;
6156 node->ordNumCols = ordNumCols;
6157 node->ordColIdx = ordColIdx;
6158 node->ordOperators = ordOperators;
6159 node->ordCollations = ordCollations;
6160 node->frameOptions = frameOptions;
6161 node->startOffset = startOffset;
6162 node->endOffset = endOffset;
6163 node->startInRangeFunc = startInRangeFunc;
6164 node->endInRangeFunc = endInRangeFunc;
6165 node->inRangeColl = inRangeColl;
6166 node->inRangeAsc = inRangeAsc;
6167 node->inRangeNullsFirst = inRangeNullsFirst;
6169 plan->targetlist = tlist;
6170 plan->lefttree = lefttree;
6171 plan->righttree = NULL;
6172 /* WindowAgg nodes never have a qual clause */
6179 make_group(List *tlist,
6182 AttrNumber *grpColIdx,
6187 Group *node = makeNode(Group);
6188 Plan *plan = &node->plan;
6190 node->numCols = numGroupCols;
6191 node->grpColIdx = grpColIdx;
6192 node->grpOperators = grpOperators;
6193 node->grpCollations = grpCollations;
6196 plan->targetlist = tlist;
6197 plan->lefttree = lefttree;
6198 plan->righttree = NULL;
6204 * distinctList is a list of SortGroupClauses, identifying the targetlist items
6205 * that should be considered by the Unique filter. The input path must
6206 * already be sorted accordingly.
6209 make_unique_from_sortclauses(Plan *lefttree, List *distinctList)
6211 Unique *node = makeNode(Unique);
6212 Plan *plan = &node->plan;
6213 int numCols = list_length(distinctList);
6215 AttrNumber *uniqColIdx;
6217 Oid *uniqCollations;
6220 plan->targetlist = lefttree->targetlist;
6222 plan->lefttree = lefttree;
6223 plan->righttree = NULL;
6226 * convert SortGroupClause list into arrays of attr indexes and equality
6227 * operators, as wanted by executor
6229 Assert(numCols > 0);
6230 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6231 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6232 uniqCollations = (Oid *) palloc(sizeof(Oid) * numCols);
6234 foreach(slitem, distinctList)
6236 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
6237 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
6239 uniqColIdx[keyno] = tle->resno;
6240 uniqOperators[keyno] = sortcl->eqop;
6241 uniqCollations[keyno] = exprCollation((Node *) tle->expr);
6242 Assert(OidIsValid(uniqOperators[keyno]));
6246 node->numCols = numCols;
6247 node->uniqColIdx = uniqColIdx;
6248 node->uniqOperators = uniqOperators;
6249 node->uniqCollations = uniqCollations;
6255 * as above, but use pathkeys to identify the sort columns and semantics
6258 make_unique_from_pathkeys(Plan *lefttree, List *pathkeys, int numCols)
6260 Unique *node = makeNode(Unique);
6261 Plan *plan = &node->plan;
6263 AttrNumber *uniqColIdx;
6265 Oid *uniqCollations;
6268 plan->targetlist = lefttree->targetlist;
6270 plan->lefttree = lefttree;
6271 plan->righttree = NULL;
6274 * Convert pathkeys list into arrays of attr indexes and equality
6275 * operators, as wanted by executor. This has a lot in common with
6276 * prepare_sort_from_pathkeys ... maybe unify sometime?
6278 Assert(numCols >= 0 && numCols <= list_length(pathkeys));
6279 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6280 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6281 uniqCollations = (Oid *) palloc(sizeof(Oid) * numCols);
6283 foreach(lc, pathkeys)
6285 PathKey *pathkey = (PathKey *) lfirst(lc);
6286 EquivalenceClass *ec = pathkey->pk_eclass;
6287 EquivalenceMember *em;
6288 TargetEntry *tle = NULL;
6289 Oid pk_datatype = InvalidOid;
6293 /* Ignore pathkeys beyond the specified number of columns */
6294 if (keyno >= numCols)
6297 if (ec->ec_has_volatile)
6300 * If the pathkey's EquivalenceClass is volatile, then it must
6301 * have come from an ORDER BY clause, and we have to match it to
6302 * that same targetlist entry.
6304 if (ec->ec_sortref == 0) /* can't happen */
6305 elog(ERROR, "volatile EquivalenceClass has no sortref");
6306 tle = get_sortgroupref_tle(ec->ec_sortref, plan->targetlist);
6308 Assert(list_length(ec->ec_members) == 1);
6309 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
6314 * Otherwise, we can use any non-constant expression listed in the
6315 * pathkey's EquivalenceClass. For now, we take the first tlist
6316 * item found in the EC.
6318 foreach(j, plan->targetlist)
6320 tle = (TargetEntry *) lfirst(j);
6321 em = find_ec_member_for_tle(ec, tle, NULL);
6324 /* found expr already in tlist */
6325 pk_datatype = em->em_datatype;
6333 elog(ERROR, "could not find pathkey item to sort");
6336 * Look up the correct equality operator from the PathKey's slightly
6337 * abstracted representation.
6339 eqop = get_opfamily_member(pathkey->pk_opfamily,
6342 BTEqualStrategyNumber);
6343 if (!OidIsValid(eqop)) /* should not happen */
6344 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
6345 BTEqualStrategyNumber, pk_datatype, pk_datatype,
6346 pathkey->pk_opfamily);
6348 uniqColIdx[keyno] = tle->resno;
6349 uniqOperators[keyno] = eqop;
6350 uniqCollations[keyno] = ec->ec_collation;
6355 node->numCols = numCols;
6356 node->uniqColIdx = uniqColIdx;
6357 node->uniqOperators = uniqOperators;
6358 node->uniqCollations = uniqCollations;
6364 make_gather(List *qptlist,
6371 Gather *node = makeNode(Gather);
6372 Plan *plan = &node->plan;
6374 plan->targetlist = qptlist;
6375 plan->qual = qpqual;
6376 plan->lefttree = subplan;
6377 plan->righttree = NULL;
6378 node->num_workers = nworkers;
6379 node->rescan_param = rescan_param;
6380 node->single_copy = single_copy;
6381 node->invisible = false;
6382 node->initParam = NULL;
6388 * distinctList is a list of SortGroupClauses, identifying the targetlist
6389 * items that should be considered by the SetOp filter. The input path must
6390 * already be sorted accordingly.
6393 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
6394 List *distinctList, AttrNumber flagColIdx, int firstFlag,
6397 SetOp *node = makeNode(SetOp);
6398 Plan *plan = &node->plan;
6399 int numCols = list_length(distinctList);
6401 AttrNumber *dupColIdx;
6406 plan->targetlist = lefttree->targetlist;
6408 plan->lefttree = lefttree;
6409 plan->righttree = NULL;
6412 * convert SortGroupClause list into arrays of attr indexes and equality
6413 * operators, as wanted by executor
6415 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6416 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6417 dupCollations = (Oid *) palloc(sizeof(Oid) * numCols);
6419 foreach(slitem, distinctList)
6421 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
6422 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
6424 dupColIdx[keyno] = tle->resno;
6425 dupOperators[keyno] = sortcl->eqop;
6426 dupCollations[keyno] = exprCollation((Node *) tle->expr);
6427 Assert(OidIsValid(dupOperators[keyno]));
6432 node->strategy = strategy;
6433 node->numCols = numCols;
6434 node->dupColIdx = dupColIdx;
6435 node->dupOperators = dupOperators;
6436 node->dupCollations = dupCollations;
6437 node->flagColIdx = flagColIdx;
6438 node->firstFlag = firstFlag;
6439 node->numGroups = numGroups;
6446 * Build a LockRows plan node
6449 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
6451 LockRows *node = makeNode(LockRows);
6452 Plan *plan = &node->plan;
6454 plan->targetlist = lefttree->targetlist;
6456 plan->lefttree = lefttree;
6457 plan->righttree = NULL;
6459 node->rowMarks = rowMarks;
6460 node->epqParam = epqParam;
6467 * Build a Limit plan node
6470 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
6472 Limit *node = makeNode(Limit);
6473 Plan *plan = &node->plan;
6475 plan->targetlist = lefttree->targetlist;
6477 plan->lefttree = lefttree;
6478 plan->righttree = NULL;
6480 node->limitOffset = limitOffset;
6481 node->limitCount = limitCount;
6488 * Build a Result plan node
6491 make_result(List *tlist,
6492 Node *resconstantqual,
6495 Result *node = makeNode(Result);
6496 Plan *plan = &node->plan;
6498 plan->targetlist = tlist;
6500 plan->lefttree = subplan;
6501 plan->righttree = NULL;
6502 node->resconstantqual = resconstantqual;
6509 * Build a ProjectSet plan node
6512 make_project_set(List *tlist,
6515 ProjectSet *node = makeNode(ProjectSet);
6516 Plan *plan = &node->plan;
6518 plan->targetlist = tlist;
6520 plan->lefttree = subplan;
6521 plan->righttree = NULL;
6528 * Build a ModifyTable plan node
6530 static ModifyTable *
6531 make_modifytable(PlannerInfo *root,
6532 CmdType operation, bool canSetTag,
6533 Index nominalRelation, Index rootRelation,
6534 bool partColsUpdated,
6535 List *resultRelations, List *subplans, List *subroots,
6536 List *withCheckOptionLists, List *returningLists,
6537 List *rowMarks, OnConflictExpr *onconflict, int epqParam)
6539 ModifyTable *node = makeNode(ModifyTable);
6540 List *fdw_private_list;
6541 Bitmapset *direct_modify_plans;
6546 Assert(list_length(resultRelations) == list_length(subplans));
6547 Assert(list_length(resultRelations) == list_length(subroots));
6548 Assert(withCheckOptionLists == NIL ||
6549 list_length(resultRelations) == list_length(withCheckOptionLists));
6550 Assert(returningLists == NIL ||
6551 list_length(resultRelations) == list_length(returningLists));
6553 node->plan.lefttree = NULL;
6554 node->plan.righttree = NULL;
6555 node->plan.qual = NIL;
6556 /* setrefs.c will fill in the targetlist, if needed */
6557 node->plan.targetlist = NIL;
6559 node->operation = operation;
6560 node->canSetTag = canSetTag;
6561 node->nominalRelation = nominalRelation;
6562 node->rootRelation = rootRelation;
6563 node->partColsUpdated = partColsUpdated;
6564 node->resultRelations = resultRelations;
6565 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
6566 node->rootResultRelIndex = -1; /* will be set correctly in setrefs.c */
6567 node->plans = subplans;
6570 node->onConflictAction = ONCONFLICT_NONE;
6571 node->onConflictSet = NIL;
6572 node->onConflictWhere = NULL;
6573 node->arbiterIndexes = NIL;
6574 node->exclRelRTI = 0;
6575 node->exclRelTlist = NIL;
6579 node->onConflictAction = onconflict->action;
6580 node->onConflictSet = onconflict->onConflictSet;
6581 node->onConflictWhere = onconflict->onConflictWhere;
6584 * If a set of unique index inference elements was provided (an
6585 * INSERT...ON CONFLICT "inference specification"), then infer
6586 * appropriate unique indexes (or throw an error if none are
6589 node->arbiterIndexes = infer_arbiter_indexes(root);
6591 node->exclRelRTI = onconflict->exclRelIndex;
6592 node->exclRelTlist = onconflict->exclRelTlist;
6594 node->withCheckOptionLists = withCheckOptionLists;
6595 node->returningLists = returningLists;
6596 node->rowMarks = rowMarks;
6597 node->epqParam = epqParam;
6600 * For each result relation that is a foreign table, allow the FDW to
6601 * construct private plan data, and accumulate it all into a list.
6603 fdw_private_list = NIL;
6604 direct_modify_plans = NULL;
6606 forboth(lc, resultRelations, lc2, subroots)
6608 Index rti = lfirst_int(lc);
6609 PlannerInfo *subroot = lfirst_node(PlannerInfo, lc2);
6610 FdwRoutine *fdwroutine;
6615 * If possible, we want to get the FdwRoutine from our RelOptInfo for
6616 * the table. But sometimes we don't have a RelOptInfo and must get
6617 * it the hard way. (In INSERT, the target relation is not scanned,
6618 * so it's not a baserel; and there are also corner cases for
6619 * updatable views where the target rel isn't a baserel.)
6621 if (rti < subroot->simple_rel_array_size &&
6622 subroot->simple_rel_array[rti] != NULL)
6624 RelOptInfo *resultRel = subroot->simple_rel_array[rti];
6626 fdwroutine = resultRel->fdwroutine;
6630 RangeTblEntry *rte = planner_rt_fetch(rti, subroot);
6632 Assert(rte->rtekind == RTE_RELATION);
6633 if (rte->relkind == RELKIND_FOREIGN_TABLE)
6634 fdwroutine = GetFdwRoutineByRelId(rte->relid);
6640 * Try to modify the foreign table directly if (1) the FDW provides
6641 * callback functions needed for that and (2) there are no local
6642 * structures that need to be run for each modified row: row-level
6643 * triggers on the foreign table, stored generated columns, WITH CHECK
6644 * OPTIONs from parent views.
6646 direct_modify = false;
6647 if (fdwroutine != NULL &&
6648 fdwroutine->PlanDirectModify != NULL &&
6649 fdwroutine->BeginDirectModify != NULL &&
6650 fdwroutine->IterateDirectModify != NULL &&
6651 fdwroutine->EndDirectModify != NULL &&
6652 withCheckOptionLists == NIL &&
6653 !has_row_triggers(subroot, rti, operation) &&
6654 !has_stored_generated_columns(subroot, rti))
6655 direct_modify = fdwroutine->PlanDirectModify(subroot, node, rti, i);
6657 direct_modify_plans = bms_add_member(direct_modify_plans, i);
6659 if (!direct_modify &&
6660 fdwroutine != NULL &&
6661 fdwroutine->PlanForeignModify != NULL)
6662 fdw_private = fdwroutine->PlanForeignModify(subroot, node, rti, i);
6665 fdw_private_list = lappend(fdw_private_list, fdw_private);
6668 node->fdwPrivLists = fdw_private_list;
6669 node->fdwDirectModifyPlans = direct_modify_plans;
6675 * is_projection_capable_path
6676 * Check whether a given Path node is able to do projection.
6679 is_projection_capable_path(Path *path)
6681 /* Most plan types can project, so just list the ones that can't */
6682 switch (path->pathtype)
6693 case T_RecursiveUnion:
6698 * Append can't project, but if an AppendPath is being used to
6699 * represent a dummy path, what will actually be generated is a
6700 * Result which can project.
6702 return IS_DUMMY_APPEND(path);
6706 * Although ProjectSet certainly projects, say "no" because we
6707 * don't want the planner to randomly replace its tlist with
6708 * something else; the SRFs have to stay at top level. This might
6709 * get relaxed later.
6719 * is_projection_capable_plan
6720 * Check whether a given Plan node is able to do projection.
6723 is_projection_capable_plan(Plan *plan)
6725 /* Most plan types can project, so just list the ones that can't */
6726 switch (nodeTag(plan))
6738 case T_RecursiveUnion:
6743 * Although ProjectSet certainly projects, say "no" because we
6744 * don't want the planner to randomly replace its tlist with
6745 * something else; the SRFs have to stay at top level. This might
6746 * get relaxed later.