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-2016, 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/stratnum.h"
23 #include "access/sysattr.h"
24 #include "catalog/pg_class.h"
25 #include "foreign/fdwapi.h"
26 #include "miscadmin.h"
27 #include "nodes/extensible.h"
28 #include "nodes/makefuncs.h"
29 #include "nodes/nodeFuncs.h"
30 #include "optimizer/clauses.h"
31 #include "optimizer/cost.h"
32 #include "optimizer/paths.h"
33 #include "optimizer/placeholder.h"
34 #include "optimizer/plancat.h"
35 #include "optimizer/planmain.h"
36 #include "optimizer/planner.h"
37 #include "optimizer/predtest.h"
38 #include "optimizer/restrictinfo.h"
39 #include "optimizer/subselect.h"
40 #include "optimizer/tlist.h"
41 #include "optimizer/var.h"
42 #include "parser/parse_clause.h"
43 #include "parser/parsetree.h"
44 #include "utils/lsyscache.h"
48 * Flag bits that can appear in the flags argument of create_plan_recurse().
49 * These can be OR-ed together.
51 * CP_EXACT_TLIST specifies that the generated plan node must return exactly
52 * the tlist specified by the path's pathtarget (this overrides both
53 * CP_SMALL_TLIST and CP_LABEL_TLIST, if those are set). Otherwise, the
54 * plan node is allowed to return just the Vars and PlaceHolderVars needed
55 * to evaluate the pathtarget.
57 * CP_SMALL_TLIST specifies that a narrower tlist is preferred. This is
58 * passed down by parent nodes such as Sort and Hash, which will have to
59 * store the returned tuples.
61 * CP_LABEL_TLIST specifies that the plan node must return columns matching
62 * any sortgrouprefs specified in its pathtarget, with appropriate
63 * ressortgroupref labels. This is passed down by parent nodes such as Sort
64 * and Group, which need these values to be available in their inputs.
66 #define CP_EXACT_TLIST 0x0001 /* Plan must return specified tlist */
67 #define CP_SMALL_TLIST 0x0002 /* Prefer narrower tlists */
68 #define CP_LABEL_TLIST 0x0004 /* tlist must contain sortgrouprefs */
71 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path,
73 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path,
75 static List *build_path_tlist(PlannerInfo *root, Path *path);
76 static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags);
77 static List *get_gating_quals(PlannerInfo *root, List *quals);
78 static Plan *create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
80 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
81 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
82 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
83 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
84 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path,
86 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path,
88 static Gather *create_gather_plan(PlannerInfo *root, GatherPath *best_path);
89 static Plan *create_projection_plan(PlannerInfo *root, ProjectionPath *best_path);
90 static Plan *inject_projection_plan(Plan *subplan, List *tlist);
91 static Sort *create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags);
92 static Group *create_group_plan(PlannerInfo *root, GroupPath *best_path);
93 static Unique *create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path,
95 static Agg *create_agg_plan(PlannerInfo *root, AggPath *best_path);
96 static Plan *create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path);
97 static Result *create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path);
98 static WindowAgg *create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path);
99 static SetOp *create_setop_plan(PlannerInfo *root, SetOpPath *best_path,
101 static RecursiveUnion *create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path);
102 static void get_column_info_for_window(PlannerInfo *root, WindowClause *wc,
104 int numSortCols, AttrNumber *sortColIdx,
106 AttrNumber **partColIdx,
109 AttrNumber **ordColIdx,
111 static LockRows *create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
113 static ModifyTable *create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path);
114 static Limit *create_limit_plan(PlannerInfo *root, LimitPath *best_path,
116 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
117 List *tlist, List *scan_clauses);
118 static SampleScan *create_samplescan_plan(PlannerInfo *root, Path *best_path,
119 List *tlist, List *scan_clauses);
120 static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
121 List *tlist, List *scan_clauses, bool indexonly);
122 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
123 BitmapHeapPath *best_path,
124 List *tlist, List *scan_clauses);
125 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
126 List **qual, List **indexqual, List **indexECs);
127 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
128 List *tlist, List *scan_clauses);
129 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root,
130 SubqueryScanPath *best_path,
131 List *tlist, List *scan_clauses);
132 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
133 List *tlist, List *scan_clauses);
134 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
135 List *tlist, List *scan_clauses);
136 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
137 List *tlist, List *scan_clauses);
138 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
139 List *tlist, List *scan_clauses);
140 static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
141 List *tlist, List *scan_clauses);
142 static CustomScan *create_customscan_plan(PlannerInfo *root,
143 CustomPath *best_path,
144 List *tlist, List *scan_clauses);
145 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path);
146 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path);
147 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path);
148 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
149 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
150 static void process_subquery_nestloop_params(PlannerInfo *root,
151 List *subplan_params);
152 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path);
153 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
154 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
155 static List *get_switched_clauses(List *clauses, Relids outerrelids);
156 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
157 static void copy_generic_path_info(Plan *dest, Path *src);
158 static void copy_plan_costsize(Plan *dest, Plan *src);
159 static void label_sort_with_costsize(PlannerInfo *root, Sort *plan,
160 double limit_tuples);
161 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
162 static SampleScan *make_samplescan(List *qptlist, List *qpqual, Index scanrelid,
163 TableSampleClause *tsc);
164 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
165 Oid indexid, List *indexqual, List *indexqualorig,
166 List *indexorderby, List *indexorderbyorig,
167 List *indexorderbyops,
168 ScanDirection indexscandir);
169 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
170 Index scanrelid, Oid indexid,
171 List *indexqual, List *indexorderby,
173 ScanDirection indexscandir);
174 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
176 List *indexqualorig);
177 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
180 List *bitmapqualorig,
182 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
184 static SubqueryScan *make_subqueryscan(List *qptlist,
188 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
189 Index scanrelid, List *functions, bool funcordinality);
190 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
191 Index scanrelid, List *values_lists);
192 static CteScan *make_ctescan(List *qptlist, List *qpqual,
193 Index scanrelid, int ctePlanId, int cteParam);
194 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
195 Index scanrelid, int wtParam);
196 static Append *make_append(List *appendplans, List *tlist);
197 static RecursiveUnion *make_recursive_union(List *tlist,
203 static BitmapAnd *make_bitmap_and(List *bitmapplans);
204 static BitmapOr *make_bitmap_or(List *bitmapplans);
205 static NestLoop *make_nestloop(List *tlist,
206 List *joinclauses, List *otherclauses, List *nestParams,
207 Plan *lefttree, Plan *righttree,
209 static HashJoin *make_hashjoin(List *tlist,
210 List *joinclauses, List *otherclauses,
212 Plan *lefttree, Plan *righttree,
214 static Hash *make_hash(Plan *lefttree,
216 AttrNumber skewColumn,
219 int32 skewColTypmod);
220 static MergeJoin *make_mergejoin(List *tlist,
221 List *joinclauses, List *otherclauses,
224 Oid *mergecollations,
225 int *mergestrategies,
226 bool *mergenullsfirst,
227 Plan *lefttree, Plan *righttree,
229 static Sort *make_sort(Plan *lefttree, int numCols,
230 AttrNumber *sortColIdx, Oid *sortOperators,
231 Oid *collations, bool *nullsFirst);
232 static Plan *prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
234 const AttrNumber *reqColIdx,
235 bool adjust_tlist_in_place,
237 AttrNumber **p_sortColIdx,
238 Oid **p_sortOperators,
240 bool **p_nullsFirst);
241 static EquivalenceMember *find_ec_member_for_tle(EquivalenceClass *ec,
244 static Sort *make_sort_from_pathkeys(Plan *lefttree, List *pathkeys);
245 static Sort *make_sort_from_groupcols(List *groupcls,
246 AttrNumber *grpColIdx,
248 static Material *make_material(Plan *lefttree);
249 static WindowAgg *make_windowagg(List *tlist, Index winref,
250 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
251 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
252 int frameOptions, Node *startOffset, Node *endOffset,
254 static Group *make_group(List *tlist, List *qual, int numGroupCols,
255 AttrNumber *grpColIdx, Oid *grpOperators,
257 static Unique *make_unique_from_sortclauses(Plan *lefttree, List *distinctList);
258 static Unique *make_unique_from_pathkeys(Plan *lefttree,
259 List *pathkeys, int numCols);
260 static Gather *make_gather(List *qptlist, List *qpqual,
261 int nworkers, bool single_copy, Plan *subplan);
262 static SetOp *make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
263 List *distinctList, AttrNumber flagColIdx, int firstFlag,
265 static LockRows *make_lockrows(Plan *lefttree, List *rowMarks, int epqParam);
266 static Result *make_result(List *tlist, Node *resconstantqual, Plan *subplan);
267 static ModifyTable *make_modifytable(PlannerInfo *root,
268 CmdType operation, bool canSetTag,
269 Index nominalRelation,
270 List *resultRelations, List *subplans,
271 List *withCheckOptionLists, List *returningLists,
272 List *rowMarks, OnConflictExpr *onconflict, int epqParam);
277 * Creates the access plan for a query by recursively processing the
278 * desired tree of pathnodes, starting at the node 'best_path'. For
279 * every pathnode found, we create a corresponding plan node containing
280 * appropriate id, target list, and qualification information.
282 * The tlists and quals in the plan tree are still in planner format,
283 * ie, Vars still correspond to the parser's numbering. This will be
284 * fixed later by setrefs.c.
286 * best_path is the best access path
288 * Returns a Plan tree.
291 create_plan(PlannerInfo *root, Path *best_path)
295 /* plan_params should not be in use in current query level */
296 Assert(root->plan_params == NIL);
298 /* Initialize this module's private workspace in PlannerInfo */
299 root->curOuterRels = NULL;
300 root->curOuterParams = NIL;
302 /* Recursively process the path tree, demanding the correct tlist result */
303 plan = create_plan_recurse(root, best_path, CP_EXACT_TLIST);
306 * Make sure the topmost plan node's targetlist exposes the original
307 * column names and other decorative info. Targetlists generated within
308 * the planner don't bother with that stuff, but we must have it on the
309 * top-level tlist seen at execution time. However, ModifyTable plan
310 * nodes don't have a tlist matching the querytree targetlist.
312 if (!IsA(plan, ModifyTable))
313 apply_tlist_labeling(plan->targetlist, root->processed_tlist);
316 * Attach any initPlans created in this query level to the topmost plan
317 * node. (The initPlans could actually go in any plan node at or above
318 * where they're referenced, but there seems no reason to put them any
319 * lower than the topmost node for the query level.)
321 SS_attach_initplans(root, plan);
323 /* Update parallel safety information if needed. */
324 if (!best_path->parallel_safe)
325 root->glob->wholePlanParallelSafe = false;
327 /* Check we successfully assigned all NestLoopParams to plan nodes */
328 if (root->curOuterParams != NIL)
329 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
332 * Reset plan_params to ensure param IDs used for nestloop params are not
335 root->plan_params = NIL;
341 * create_plan_recurse
342 * Recursive guts of create_plan().
345 create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
349 switch (best_path->pathtype)
354 case T_IndexOnlyScan:
355 case T_BitmapHeapScan:
361 case T_WorkTableScan:
364 plan = create_scan_plan(root, best_path, flags);
369 plan = create_join_plan(root,
370 (JoinPath *) best_path);
373 plan = create_append_plan(root,
374 (AppendPath *) best_path);
377 plan = create_merge_append_plan(root,
378 (MergeAppendPath *) best_path);
381 if (IsA(best_path, ProjectionPath))
383 plan = create_projection_plan(root,
384 (ProjectionPath *) best_path);
386 else if (IsA(best_path, MinMaxAggPath))
388 plan = (Plan *) create_minmaxagg_plan(root,
389 (MinMaxAggPath *) best_path);
393 Assert(IsA(best_path, ResultPath));
394 plan = (Plan *) create_result_plan(root,
395 (ResultPath *) best_path);
399 plan = (Plan *) create_material_plan(root,
400 (MaterialPath *) best_path,
404 if (IsA(best_path, UpperUniquePath))
406 plan = (Plan *) create_upper_unique_plan(root,
407 (UpperUniquePath *) best_path,
412 Assert(IsA(best_path, UniquePath));
413 plan = create_unique_plan(root,
414 (UniquePath *) best_path,
419 plan = (Plan *) create_gather_plan(root,
420 (GatherPath *) best_path);
423 plan = (Plan *) create_sort_plan(root,
424 (SortPath *) best_path,
428 plan = (Plan *) create_group_plan(root,
429 (GroupPath *) best_path);
432 if (IsA(best_path, GroupingSetsPath))
433 plan = create_groupingsets_plan(root,
434 (GroupingSetsPath *) best_path);
437 Assert(IsA(best_path, AggPath));
438 plan = (Plan *) create_agg_plan(root,
439 (AggPath *) best_path);
443 plan = (Plan *) create_windowagg_plan(root,
444 (WindowAggPath *) best_path);
447 plan = (Plan *) create_setop_plan(root,
448 (SetOpPath *) best_path,
451 case T_RecursiveUnion:
452 plan = (Plan *) create_recursiveunion_plan(root,
453 (RecursiveUnionPath *) best_path);
456 plan = (Plan *) create_lockrows_plan(root,
457 (LockRowsPath *) best_path,
461 plan = (Plan *) create_modifytable_plan(root,
462 (ModifyTablePath *) best_path);
465 plan = (Plan *) create_limit_plan(root,
466 (LimitPath *) best_path,
470 elog(ERROR, "unrecognized node type: %d",
471 (int) best_path->pathtype);
472 plan = NULL; /* keep compiler quiet */
481 * Create a scan plan for the parent relation of 'best_path'.
484 create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
486 RelOptInfo *rel = best_path->parent;
488 List *gating_clauses;
493 * Extract the relevant restriction clauses from the parent relation. The
494 * executor must apply all these restrictions during the scan, except for
495 * pseudoconstants which we'll take care of below.
497 * If this is a plain indexscan or index-only scan, we need not consider
498 * restriction clauses that are implied by the index's predicate, so use
499 * indrestrictinfo not baserestrictinfo. Note that we can't do that for
500 * bitmap indexscans, since there's not necessarily a single index
501 * involved; but it doesn't matter since create_bitmap_scan_plan() will be
502 * able to get rid of such clauses anyway via predicate proof.
504 switch (best_path->pathtype)
507 case T_IndexOnlyScan:
508 Assert(IsA(best_path, IndexPath));
509 scan_clauses = ((IndexPath *) best_path)->indexinfo->indrestrictinfo;
512 scan_clauses = rel->baserestrictinfo;
517 * If this is a parameterized scan, we also need to enforce all the join
518 * clauses available from the outer relation(s).
520 * For paranoia's sake, don't modify the stored baserestrictinfo list.
522 if (best_path->param_info)
523 scan_clauses = list_concat(list_copy(scan_clauses),
524 best_path->param_info->ppi_clauses);
527 * Detect whether we have any pseudoconstant quals to deal with. Then, if
528 * we'll need a gating Result node, it will be able to project, so there
529 * are no requirements on the child's tlist.
531 gating_clauses = get_gating_quals(root, scan_clauses);
536 * For table scans, rather than using the relation targetlist (which is
537 * only those Vars actually needed by the query), we prefer to generate a
538 * tlist containing all Vars in order. This will allow the executor to
539 * optimize away projection of the table tuples, if possible.
541 if (use_physical_tlist(root, best_path, flags))
543 if (best_path->pathtype == T_IndexOnlyScan)
545 /* For index-only scan, the preferred tlist is the index's */
546 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
549 * Transfer any sortgroupref data to the replacement tlist, unless
550 * we don't care because the gating Result will handle it.
553 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
557 tlist = build_physical_tlist(root, rel);
560 /* Failed because of dropped cols, so use regular method */
561 tlist = build_path_tlist(root, best_path);
565 /* As above, transfer sortgroupref data to replacement tlist */
567 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
573 tlist = build_path_tlist(root, best_path);
576 switch (best_path->pathtype)
579 plan = (Plan *) create_seqscan_plan(root,
586 plan = (Plan *) create_samplescan_plan(root,
593 plan = (Plan *) create_indexscan_plan(root,
594 (IndexPath *) best_path,
600 case T_IndexOnlyScan:
601 plan = (Plan *) create_indexscan_plan(root,
602 (IndexPath *) best_path,
608 case T_BitmapHeapScan:
609 plan = (Plan *) create_bitmap_scan_plan(root,
610 (BitmapHeapPath *) best_path,
616 plan = (Plan *) create_tidscan_plan(root,
617 (TidPath *) best_path,
623 plan = (Plan *) create_subqueryscan_plan(root,
624 (SubqueryScanPath *) best_path,
630 plan = (Plan *) create_functionscan_plan(root,
637 plan = (Plan *) create_valuesscan_plan(root,
644 plan = (Plan *) create_ctescan_plan(root,
650 case T_WorkTableScan:
651 plan = (Plan *) create_worktablescan_plan(root,
658 plan = (Plan *) create_foreignscan_plan(root,
659 (ForeignPath *) best_path,
665 plan = (Plan *) create_customscan_plan(root,
666 (CustomPath *) best_path,
672 elog(ERROR, "unrecognized node type: %d",
673 (int) best_path->pathtype);
674 plan = NULL; /* keep compiler quiet */
679 * If there are any pseudoconstant clauses attached to this node, insert a
680 * gating Result node that evaluates the pseudoconstants as one-time
684 plan = create_gating_plan(root, best_path, plan, gating_clauses);
690 * Build a target list (ie, a list of TargetEntry) for the Path's output.
692 * This is almost just make_tlist_from_pathtarget(), but we also have to
693 * deal with replacing nestloop params.
696 build_path_tlist(PlannerInfo *root, Path *path)
699 Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
703 foreach(v, path->pathtarget->exprs)
705 Node *node = (Node *) lfirst(v);
709 * If it's a parameterized path, there might be lateral references in
710 * the tlist, which need to be replaced with Params. There's no need
711 * to remake the TargetEntry nodes, so apply this to each list item
714 if (path->param_info)
715 node = replace_nestloop_params(root, node);
717 tle = makeTargetEntry((Expr *) node,
722 tle->ressortgroupref = sortgrouprefs[resno - 1];
724 tlist = lappend(tlist, tle);
732 * Decide whether to use a tlist matching relation structure,
733 * rather than only those Vars actually referenced.
736 use_physical_tlist(PlannerInfo *root, Path *path, int flags)
738 RelOptInfo *rel = path->parent;
743 * Forget it if either exact tlist or small tlist is demanded.
745 if (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST))
749 * We can do this for real relation scans, subquery scans, function scans,
750 * values scans, and CTE scans (but not for, eg, joins).
752 if (rel->rtekind != RTE_RELATION &&
753 rel->rtekind != RTE_SUBQUERY &&
754 rel->rtekind != RTE_FUNCTION &&
755 rel->rtekind != RTE_VALUES &&
756 rel->rtekind != RTE_CTE)
760 * Can't do it with inheritance cases either (mainly because Append
761 * doesn't project; this test may be unnecessary now that
762 * create_append_plan instructs its children to return an exact tlist).
764 if (rel->reloptkind != RELOPT_BASEREL)
768 * Can't do it if any system columns or whole-row Vars are requested.
769 * (This could possibly be fixed but would take some fragile assumptions
770 * in setrefs.c, I think.)
772 for (i = rel->min_attr; i <= 0; i++)
774 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
779 * Can't do it if the rel is required to emit any placeholder expressions,
782 foreach(lc, root->placeholder_list)
784 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
786 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
787 bms_is_subset(phinfo->ph_eval_at, rel->relids))
792 * Also, can't do it if CP_LABEL_TLIST is specified and path is requested
793 * to emit any sort/group columns that are not simple Vars. (If they are
794 * simple Vars, they should appear in the physical tlist, and
795 * apply_pathtarget_labeling_to_tlist will take care of getting them
796 * labeled again.) We also have to check that no two sort/group columns
797 * are the same Var, else that element of the physical tlist would need
798 * conflicting ressortgroupref labels.
800 if ((flags & CP_LABEL_TLIST) && path->pathtarget->sortgrouprefs)
802 Bitmapset *sortgroupatts = NULL;
805 foreach(lc, path->pathtarget->exprs)
807 Expr *expr = (Expr *) lfirst(lc);
809 if (path->pathtarget->sortgrouprefs[i])
811 if (expr && IsA(expr, Var))
813 int attno = ((Var *) expr)->varattno;
815 attno -= FirstLowInvalidHeapAttributeNumber;
816 if (bms_is_member(attno, sortgroupatts))
818 sortgroupatts = bms_add_member(sortgroupatts, attno);
832 * See if there are pseudoconstant quals in a node's quals list
834 * If the node's quals list includes any pseudoconstant quals,
835 * return just those quals.
838 get_gating_quals(PlannerInfo *root, List *quals)
840 /* No need to look if we know there are no pseudoconstants */
841 if (!root->hasPseudoConstantQuals)
844 /* Sort into desirable execution order while still in RestrictInfo form */
845 quals = order_qual_clauses(root, quals);
847 /* Pull out any pseudoconstant quals from the RestrictInfo list */
848 return extract_actual_clauses(quals, true);
853 * Deal with pseudoconstant qual clauses
855 * Add a gating Result node atop the already-built plan.
858 create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
863 Assert(gating_quals);
866 * Since we need a Result node anyway, always return the path's requested
867 * tlist; that's never a wrong choice, even if the parent node didn't ask
868 * for CP_EXACT_TLIST.
870 gplan = (Plan *) make_result(build_path_tlist(root, path),
871 (Node *) gating_quals,
875 * Notice that we don't change cost or size estimates when doing gating.
876 * The costs of qual eval were already included in the subplan's cost.
877 * Leaving the size alone amounts to assuming that the gating qual will
878 * succeed, which is the conservative estimate for planning upper queries.
879 * We certainly don't want to assume the output size is zero (unless the
880 * gating qual is actually constant FALSE, and that case is dealt with in
881 * clausesel.c). Interpolating between the two cases is silly, because it
882 * doesn't reflect what will really happen at runtime, and besides which
883 * in most cases we have only a very bad idea of the probability of the
884 * gating qual being true.
886 copy_plan_costsize(gplan, plan);
893 * Create a join plan for 'best_path' and (recursively) plans for its
894 * inner and outer paths.
897 create_join_plan(PlannerInfo *root, JoinPath *best_path)
900 List *gating_clauses;
902 switch (best_path->path.pathtype)
905 plan = (Plan *) create_mergejoin_plan(root,
906 (MergePath *) best_path);
909 plan = (Plan *) create_hashjoin_plan(root,
910 (HashPath *) best_path);
913 plan = (Plan *) create_nestloop_plan(root,
914 (NestPath *) best_path);
917 elog(ERROR, "unrecognized node type: %d",
918 (int) best_path->path.pathtype);
919 plan = NULL; /* keep compiler quiet */
924 * If there are any pseudoconstant clauses attached to this node, insert a
925 * gating Result node that evaluates the pseudoconstants as one-time
928 gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
930 plan = create_gating_plan(root, (Path *) best_path, plan,
936 * * Expensive function pullups may have pulled local predicates * into
937 * this path node. Put them in the qpqual of the plan node. * JMH,
940 if (get_loc_restrictinfo(best_path) != NIL)
941 set_qpqual((Plan) plan,
942 list_concat(get_qpqual((Plan) plan),
943 get_actual_clauses(get_loc_restrictinfo(best_path))));
951 * Create an Append plan for 'best_path' and (recursively) plans
954 * Returns a Plan node.
957 create_append_plan(PlannerInfo *root, AppendPath *best_path)
960 List *tlist = build_path_tlist(root, &best_path->path);
961 List *subplans = NIL;
965 * The subpaths list could be empty, if every child was proven empty by
966 * constraint exclusion. In that case generate a dummy plan that returns
969 * Note that an AppendPath with no members is also generated in certain
970 * cases where there was no appending construct at all, but we know the
971 * relation is empty (see set_dummy_rel_pathlist).
973 if (best_path->subpaths == NIL)
975 /* Generate a Result plan with constant-FALSE gating qual */
978 plan = (Plan *) make_result(tlist,
979 (Node *) list_make1(makeBoolConst(false,
983 copy_generic_path_info(plan, (Path *) best_path);
988 /* Build the plan for each child */
989 foreach(subpaths, best_path->subpaths)
991 Path *subpath = (Path *) lfirst(subpaths);
994 /* Must insist that all children return the same tlist */
995 subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
997 subplans = lappend(subplans, subplan);
1001 * XXX ideally, if there's just one child, we'd not bother to generate an
1002 * Append node but just return the single child. At the moment this does
1003 * not work because the varno of the child scan plan won't match the
1004 * parent-rel Vars it'll be asked to emit.
1007 plan = make_append(subplans, tlist);
1009 copy_generic_path_info(&plan->plan, (Path *) best_path);
1011 return (Plan *) plan;
1015 * create_merge_append_plan
1016 * Create a MergeAppend plan for 'best_path' and (recursively) plans
1019 * Returns a Plan node.
1022 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
1024 MergeAppend *node = makeNode(MergeAppend);
1025 Plan *plan = &node->plan;
1026 List *tlist = build_path_tlist(root, &best_path->path);
1027 List *pathkeys = best_path->path.pathkeys;
1028 List *subplans = NIL;
1032 * We don't have the actual creation of the MergeAppend node split out
1033 * into a separate make_xxx function. This is because we want to run
1034 * prepare_sort_from_pathkeys on it before we do so on the individual
1035 * child plans, to make cross-checking the sort info easier.
1037 copy_generic_path_info(plan, (Path *) best_path);
1038 plan->targetlist = tlist;
1040 plan->lefttree = NULL;
1041 plan->righttree = NULL;
1043 /* Compute sort column info, and adjust MergeAppend's tlist as needed */
1044 (void) prepare_sort_from_pathkeys(plan, pathkeys,
1045 best_path->path.parent->relids,
1050 &node->sortOperators,
1055 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1056 * even to subplans that don't need an explicit sort, to make sure they
1057 * are returning the same sort key columns the MergeAppend expects.
1059 foreach(subpaths, best_path->subpaths)
1061 Path *subpath = (Path *) lfirst(subpaths);
1064 AttrNumber *sortColIdx;
1069 /* Build the child plan */
1070 /* Must insist that all children return the same tlist */
1071 subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1073 /* Compute sort column info, and adjust subplan's tlist as needed */
1074 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1075 subpath->parent->relids,
1085 * Check that we got the same sort key information. We just Assert
1086 * that the sortops match, since those depend only on the pathkeys;
1087 * but it seems like a good idea to check the sort column numbers
1088 * explicitly, to ensure the tlists really do match up.
1090 Assert(numsortkeys == node->numCols);
1091 if (memcmp(sortColIdx, node->sortColIdx,
1092 numsortkeys * sizeof(AttrNumber)) != 0)
1093 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1094 Assert(memcmp(sortOperators, node->sortOperators,
1095 numsortkeys * sizeof(Oid)) == 0);
1096 Assert(memcmp(collations, node->collations,
1097 numsortkeys * sizeof(Oid)) == 0);
1098 Assert(memcmp(nullsFirst, node->nullsFirst,
1099 numsortkeys * sizeof(bool)) == 0);
1101 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1102 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1104 Sort *sort = make_sort(subplan, numsortkeys,
1105 sortColIdx, sortOperators,
1106 collations, nullsFirst);
1108 label_sort_with_costsize(root, sort, best_path->limit_tuples);
1109 subplan = (Plan *) sort;
1112 subplans = lappend(subplans, subplan);
1115 node->mergeplans = subplans;
1117 return (Plan *) node;
1121 * create_result_plan
1122 * Create a Result plan for 'best_path'.
1123 * This is only used for degenerate cases, such as a query with an empty
1126 * Returns a Plan node.
1129 create_result_plan(PlannerInfo *root, ResultPath *best_path)
1135 tlist = build_path_tlist(root, &best_path->path);
1137 /* best_path->quals is just bare clauses */
1138 quals = order_qual_clauses(root, best_path->quals);
1140 plan = make_result(tlist, (Node *) quals, NULL);
1142 copy_generic_path_info(&plan->plan, (Path *) best_path);
1148 * create_material_plan
1149 * Create a Material plan for 'best_path' and (recursively) plans
1152 * Returns a Plan node.
1155 create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
1161 * We don't want any excess columns in the materialized tuples, so request
1162 * a smaller tlist. Otherwise, since Material doesn't project, tlist
1163 * requirements pass through.
1165 subplan = create_plan_recurse(root, best_path->subpath,
1166 flags | CP_SMALL_TLIST);
1168 plan = make_material(subplan);
1170 copy_generic_path_info(&plan->plan, (Path *) best_path);
1176 * create_unique_plan
1177 * Create a Unique plan for 'best_path' and (recursively) plans
1180 * Returns a Plan node.
1183 create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
1193 AttrNumber *groupColIdx;
1197 /* Unique doesn't project, so tlist requirements pass through */
1198 subplan = create_plan_recurse(root, best_path->subpath, flags);
1200 /* Done if we don't need to do any actual unique-ifying */
1201 if (best_path->umethod == UNIQUE_PATH_NOOP)
1205 * As constructed, the subplan has a "flat" tlist containing just the Vars
1206 * needed here and at upper levels. The values we are supposed to
1207 * unique-ify may be expressions in these variables. We have to add any
1208 * such expressions to the subplan's tlist.
1210 * The subplan may have a "physical" tlist if it is a simple scan plan. If
1211 * we're going to sort, this should be reduced to the regular tlist, so
1212 * that we don't sort more data than we need to. For hashing, the tlist
1213 * should be left as-is if we don't need to add any expressions; but if we
1214 * do have to add expressions, then a projection step will be needed at
1215 * runtime anyway, so we may as well remove unneeded items. Therefore
1216 * newtlist starts from build_path_tlist() not just a copy of the
1217 * subplan's tlist; and we don't install it into the subplan unless we are
1218 * sorting or stuff has to be added.
1220 in_operators = best_path->in_operators;
1221 uniq_exprs = best_path->uniq_exprs;
1223 /* initialize modified subplan tlist as just the "required" vars */
1224 newtlist = build_path_tlist(root, &best_path->path);
1225 nextresno = list_length(newtlist) + 1;
1228 foreach(l, uniq_exprs)
1230 Node *uniqexpr = lfirst(l);
1233 tle = tlist_member(uniqexpr, newtlist);
1236 tle = makeTargetEntry((Expr *) uniqexpr,
1240 newtlist = lappend(newtlist, tle);
1246 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
1249 * If the top plan node can't do projections and its existing target
1250 * list isn't already what we need, we need to add a Result node to
1253 if (!is_projection_capable_plan(subplan) &&
1254 !tlist_same_exprs(newtlist, subplan->targetlist))
1255 subplan = inject_projection_plan(subplan, newtlist);
1257 subplan->targetlist = newtlist;
1261 * Build control information showing which subplan output columns are to
1262 * be examined by the grouping step. Unfortunately we can't merge this
1263 * with the previous loop, since we didn't then know which version of the
1264 * subplan tlist we'd end up using.
1266 newtlist = subplan->targetlist;
1267 numGroupCols = list_length(uniq_exprs);
1268 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
1271 foreach(l, uniq_exprs)
1273 Node *uniqexpr = lfirst(l);
1276 tle = tlist_member(uniqexpr, newtlist);
1277 if (!tle) /* shouldn't happen */
1278 elog(ERROR, "failed to find unique expression in subplan tlist");
1279 groupColIdx[groupColPos++] = tle->resno;
1282 if (best_path->umethod == UNIQUE_PATH_HASH)
1284 Oid *groupOperators;
1287 * Get the hashable equality operators for the Agg node to use.
1288 * Normally these are the same as the IN clause operators, but if
1289 * those are cross-type operators then the equality operators are the
1290 * ones for the IN clause operators' RHS datatype.
1292 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
1294 foreach(l, in_operators)
1296 Oid in_oper = lfirst_oid(l);
1299 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
1300 elog(ERROR, "could not find compatible hash operator for operator %u",
1302 groupOperators[groupColPos++] = eq_oper;
1306 * Since the Agg node is going to project anyway, we can give it the
1307 * minimum output tlist, without any stuff we might have added to the
1310 plan = (Plan *) make_agg(build_path_tlist(root, &best_path->path),
1319 best_path->path.rows,
1324 List *sortList = NIL;
1327 /* Create an ORDER BY list to sort the input compatibly */
1329 foreach(l, in_operators)
1331 Oid in_oper = lfirst_oid(l);
1335 SortGroupClause *sortcl;
1337 sortop = get_ordering_op_for_equality_op(in_oper, false);
1338 if (!OidIsValid(sortop)) /* shouldn't happen */
1339 elog(ERROR, "could not find ordering operator for equality operator %u",
1343 * The Unique node will need equality operators. Normally these
1344 * are the same as the IN clause operators, but if those are
1345 * cross-type operators then the equality operators are the ones
1346 * for the IN clause operators' RHS datatype.
1348 eqop = get_equality_op_for_ordering_op(sortop, NULL);
1349 if (!OidIsValid(eqop)) /* shouldn't happen */
1350 elog(ERROR, "could not find equality operator for ordering operator %u",
1353 tle = get_tle_by_resno(subplan->targetlist,
1354 groupColIdx[groupColPos]);
1355 Assert(tle != NULL);
1357 sortcl = makeNode(SortGroupClause);
1358 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
1359 subplan->targetlist);
1360 sortcl->eqop = eqop;
1361 sortcl->sortop = sortop;
1362 sortcl->nulls_first = false;
1363 sortcl->hashable = false; /* no need to make this accurate */
1364 sortList = lappend(sortList, sortcl);
1367 sort = make_sort_from_sortclauses(sortList, subplan);
1368 label_sort_with_costsize(root, sort, -1.0);
1369 plan = (Plan *) make_unique_from_sortclauses((Plan *) sort, sortList);
1372 /* Copy cost data from Path to Plan */
1373 copy_generic_path_info(plan, &best_path->path);
1379 * create_gather_plan
1381 * Create a Gather plan for 'best_path' and (recursively) plans
1385 create_gather_plan(PlannerInfo *root, GatherPath *best_path)
1387 Gather *gather_plan;
1392 * Although the Gather node can project, we prefer to push down such work
1393 * to its child node, so demand an exact tlist from the child.
1395 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1397 tlist = build_path_tlist(root, &best_path->path);
1399 gather_plan = make_gather(tlist,
1401 best_path->path.parallel_workers,
1402 best_path->single_copy,
1405 copy_generic_path_info(&gather_plan->plan, &best_path->path);
1407 /* use parallel mode for parallel plans. */
1408 root->glob->parallelModeNeeded = true;
1414 * create_projection_plan
1416 * Create a plan tree to do a projection step and (recursively) plans
1417 * for its subpaths. We may need a Result node for the projection,
1418 * but sometimes we can just let the subplan do the work.
1421 create_projection_plan(PlannerInfo *root, ProjectionPath *best_path)
1427 /* Since we intend to project, we don't need to constrain child tlist */
1428 subplan = create_plan_recurse(root, best_path->subpath, 0);
1430 tlist = build_path_tlist(root, &best_path->path);
1433 * We might not really need a Result node here, either because the subplan
1434 * can project or because it's returning the right list of expressions
1435 * anyway. Usually create_projection_path will have detected that and set
1436 * dummypp if we don't need a Result; but its decision can't be final,
1437 * because some createplan.c routines change the tlists of their nodes.
1438 * (An example is that create_merge_append_plan might add resjunk sort
1439 * columns to a MergeAppend.) So we have to recheck here. If we do
1440 * arrive at a different answer than create_projection_path did, we'll
1441 * have made slightly wrong cost estimates; but label the plan with the
1442 * cost estimates we actually used, not "corrected" ones. (XXX this could
1443 * be cleaned up if we moved more of the sortcolumn setup logic into Path
1444 * creation, but that would add expense to creating Paths we might end up
1447 if (is_projection_capable_path(best_path->subpath) ||
1448 tlist_same_exprs(tlist, subplan->targetlist))
1450 /* Don't need a separate Result, just assign tlist to subplan */
1452 plan->targetlist = tlist;
1454 /* Label plan with the estimated costs we actually used */
1455 plan->startup_cost = best_path->path.startup_cost;
1456 plan->total_cost = best_path->path.total_cost;
1457 plan->plan_rows = best_path->path.rows;
1458 plan->plan_width = best_path->path.pathtarget->width;
1459 /* ... but be careful not to munge subplan's parallel-aware flag */
1463 /* We need a Result node */
1464 plan = (Plan *) make_result(tlist, NULL, subplan);
1466 copy_generic_path_info(plan, (Path *) best_path);
1473 * inject_projection_plan
1474 * Insert a Result node to do a projection step.
1476 * This is used in a few places where we decide on-the-fly that we need a
1477 * projection step as part of the tree generated for some Path node.
1478 * We should try to get rid of this in favor of doing it more honestly.
1481 inject_projection_plan(Plan *subplan, List *tlist)
1485 plan = (Plan *) make_result(tlist, NULL, subplan);
1488 * In principle, we should charge tlist eval cost plus cpu_per_tuple per
1489 * row for the Result node. But the former has probably been factored in
1490 * already and the latter was not accounted for during Path construction,
1491 * so being formally correct might just make the EXPLAIN output look less
1492 * consistent not more so. Hence, just copy the subplan's cost.
1494 copy_plan_costsize(plan, subplan);
1502 * Create a Sort plan for 'best_path' and (recursively) plans
1506 create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
1512 * We don't want any excess columns in the sorted tuples, so request a
1513 * smaller tlist. Otherwise, since Sort doesn't project, tlist
1514 * requirements pass through.
1516 subplan = create_plan_recurse(root, best_path->subpath,
1517 flags | CP_SMALL_TLIST);
1519 plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys);
1521 copy_generic_path_info(&plan->plan, (Path *) best_path);
1529 * Create a Group plan for 'best_path' and (recursively) plans
1533 create_group_plan(PlannerInfo *root, GroupPath *best_path)
1541 * Group can project, so no need to be terribly picky about child tlist,
1542 * but we do need grouping columns to be available
1544 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1546 tlist = build_path_tlist(root, &best_path->path);
1548 quals = order_qual_clauses(root, best_path->qual);
1550 plan = make_group(tlist,
1552 list_length(best_path->groupClause),
1553 extract_grouping_cols(best_path->groupClause,
1554 subplan->targetlist),
1555 extract_grouping_ops(best_path->groupClause),
1558 copy_generic_path_info(&plan->plan, (Path *) best_path);
1564 * create_upper_unique_plan
1566 * Create a Unique plan for 'best_path' and (recursively) plans
1570 create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
1576 * Unique doesn't project, so tlist requirements pass through; moreover we
1577 * need grouping columns to be labeled.
1579 subplan = create_plan_recurse(root, best_path->subpath,
1580 flags | CP_LABEL_TLIST);
1582 plan = make_unique_from_pathkeys(subplan,
1583 best_path->path.pathkeys,
1584 best_path->numkeys);
1586 copy_generic_path_info(&plan->plan, (Path *) best_path);
1594 * Create an Agg plan for 'best_path' and (recursively) plans
1598 create_agg_plan(PlannerInfo *root, AggPath *best_path)
1606 * Agg can project, so no need to be terribly picky about child tlist, but
1607 * we do need grouping columns to be available
1609 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1611 tlist = build_path_tlist(root, &best_path->path);
1613 quals = order_qual_clauses(root, best_path->qual);
1615 plan = make_agg(tlist, quals,
1616 best_path->aggstrategy,
1617 best_path->aggsplit,
1618 list_length(best_path->groupClause),
1619 extract_grouping_cols(best_path->groupClause,
1620 subplan->targetlist),
1621 extract_grouping_ops(best_path->groupClause),
1624 best_path->numGroups,
1627 copy_generic_path_info(&plan->plan, (Path *) best_path);
1633 * Given a groupclause for a collection of grouping sets, produce the
1634 * corresponding groupColIdx.
1636 * root->grouping_map maps the tleSortGroupRef to the actual column position in
1637 * the input tuple. So we get the ref from the entries in the groupclause and
1638 * look them up there.
1641 remap_groupColIdx(PlannerInfo *root, List *groupClause)
1643 AttrNumber *grouping_map = root->grouping_map;
1644 AttrNumber *new_grpColIdx;
1648 Assert(grouping_map);
1650 new_grpColIdx = palloc0(sizeof(AttrNumber) * list_length(groupClause));
1653 foreach(lc, groupClause)
1655 SortGroupClause *clause = lfirst(lc);
1657 new_grpColIdx[i++] = grouping_map[clause->tleSortGroupRef];
1660 return new_grpColIdx;
1664 * create_groupingsets_plan
1665 * Create a plan for 'best_path' and (recursively) plans
1668 * What we emit is an Agg plan with some vestigial Agg and Sort nodes
1669 * hanging off the side. The top Agg implements the last grouping set
1670 * specified in the GroupingSetsPath, and any additional grouping sets
1671 * each give rise to a subsidiary Agg and Sort node in the top Agg's
1672 * "chain" list. These nodes don't participate in the plan directly,
1673 * but they are a convenient way to represent the required data for
1676 * Returns a Plan node.
1679 create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
1683 List *rollup_groupclauses = best_path->rollup_groupclauses;
1684 List *rollup_lists = best_path->rollup_lists;
1685 AttrNumber *grouping_map;
1691 /* Shouldn't get here without grouping sets */
1692 Assert(root->parse->groupingSets);
1693 Assert(rollup_lists != NIL);
1694 Assert(list_length(rollup_lists) == list_length(rollup_groupclauses));
1697 * Agg can project, so no need to be terribly picky about child tlist, but
1698 * we do need grouping columns to be available
1700 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1703 * Compute the mapping from tleSortGroupRef to column index in the child's
1704 * tlist. First, identify max SortGroupRef in groupClause, for array
1708 foreach(lc, root->parse->groupClause)
1710 SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
1712 if (gc->tleSortGroupRef > maxref)
1713 maxref = gc->tleSortGroupRef;
1716 grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
1718 /* Now look up the column numbers in the child's tlist */
1719 foreach(lc, root->parse->groupClause)
1721 SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
1722 TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
1724 grouping_map[gc->tleSortGroupRef] = tle->resno;
1728 * During setrefs.c, we'll need the grouping_map to fix up the cols lists
1729 * in GroupingFunc nodes. Save it for setrefs.c to use.
1731 * This doesn't work if we're in an inheritance subtree (see notes in
1732 * create_modifytable_plan). Fortunately we can't be because there would
1733 * never be grouping in an UPDATE/DELETE; but let's Assert that.
1735 Assert(!root->hasInheritedTarget);
1736 Assert(root->grouping_map == NULL);
1737 root->grouping_map = grouping_map;
1740 * Generate the side nodes that describe the other sort and group
1741 * operations besides the top one. Note that we don't worry about putting
1742 * accurate cost estimates in the side nodes; only the topmost Agg node's
1743 * costs will be shown by EXPLAIN.
1746 if (list_length(rollup_groupclauses) > 1)
1748 forboth(lc, rollup_groupclauses, lc2, rollup_lists)
1750 List *groupClause = (List *) lfirst(lc);
1751 List *gsets = (List *) lfirst(lc2);
1752 AttrNumber *new_grpColIdx;
1756 /* We want to iterate over all but the last rollup list elements */
1757 if (lnext(lc) == NULL)
1760 new_grpColIdx = remap_groupColIdx(root, groupClause);
1762 sort_plan = (Plan *)
1763 make_sort_from_groupcols(groupClause,
1767 agg_plan = (Plan *) make_agg(NIL,
1771 list_length((List *) linitial(gsets)),
1773 extract_grouping_ops(groupClause),
1776 0, /* numGroups not needed */
1780 * Nuke stuff we don't need to avoid bloating debug output.
1782 sort_plan->targetlist = NIL;
1783 sort_plan->lefttree = NULL;
1785 chain = lappend(chain, agg_plan);
1790 * Now make the final Agg node
1793 List *groupClause = (List *) llast(rollup_groupclauses);
1794 List *gsets = (List *) llast(rollup_lists);
1795 AttrNumber *top_grpColIdx;
1798 top_grpColIdx = remap_groupColIdx(root, groupClause);
1800 numGroupCols = list_length((List *) linitial(gsets));
1802 plan = make_agg(build_path_tlist(root, &best_path->path),
1804 (numGroupCols > 0) ? AGG_SORTED : AGG_PLAIN,
1808 extract_grouping_ops(groupClause),
1811 0, /* numGroups not needed */
1814 /* Copy cost data from Path to Plan */
1815 copy_generic_path_info(&plan->plan, &best_path->path);
1818 return (Plan *) plan;
1822 * create_minmaxagg_plan
1824 * Create a Result plan for 'best_path' and (recursively) plans
1828 create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
1834 /* Prepare an InitPlan for each aggregate's subquery. */
1835 foreach(lc, best_path->mmaggregates)
1837 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1838 PlannerInfo *subroot = mminfo->subroot;
1839 Query *subparse = subroot->parse;
1843 * Generate the plan for the subquery. We already have a Path, but we
1844 * have to convert it to a Plan and attach a LIMIT node above it.
1845 * Since we are entering a different planner context (subroot),
1846 * recurse to create_plan not create_plan_recurse.
1848 plan = create_plan(subroot, mminfo->path);
1850 plan = (Plan *) make_limit(plan,
1851 subparse->limitOffset,
1852 subparse->limitCount);
1854 /* Must apply correct cost/width data to Limit node */
1855 plan->startup_cost = mminfo->path->startup_cost;
1856 plan->total_cost = mminfo->pathcost;
1857 plan->plan_rows = 1;
1858 plan->plan_width = mminfo->path->pathtarget->width;
1859 plan->parallel_aware = false;
1861 /* Convert the plan into an InitPlan in the outer query. */
1862 SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
1865 /* Generate the output plan --- basically just a Result */
1866 tlist = build_path_tlist(root, &best_path->path);
1868 plan = make_result(tlist, (Node *) best_path->quals, NULL);
1870 copy_generic_path_info(&plan->plan, (Path *) best_path);
1873 * During setrefs.c, we'll need to replace references to the Agg nodes
1874 * with InitPlan output params. (We can't just do that locally in the
1875 * MinMaxAgg node, because path nodes above here may have Agg references
1876 * as well.) Save the mmaggregates list to tell setrefs.c to do that.
1878 * This doesn't work if we're in an inheritance subtree (see notes in
1879 * create_modifytable_plan). Fortunately we can't be because there would
1880 * never be aggregates in an UPDATE/DELETE; but let's Assert that.
1882 Assert(!root->hasInheritedTarget);
1883 Assert(root->minmax_aggs == NIL);
1884 root->minmax_aggs = best_path->mmaggregates;
1890 * create_windowagg_plan
1892 * Create a WindowAgg plan for 'best_path' and (recursively) plans
1896 create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
1899 WindowClause *wc = best_path->winclause;
1903 AttrNumber *sortColIdx;
1908 AttrNumber *partColIdx;
1911 AttrNumber *ordColIdx;
1915 * WindowAgg can project, so no need to be terribly picky about child
1916 * tlist, but we do need grouping columns to be available
1918 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1920 tlist = build_path_tlist(root, &best_path->path);
1923 * We shouldn't need to actually sort, but it's convenient to use
1924 * prepare_sort_from_pathkeys to identify the input's sort columns.
1926 subplan = prepare_sort_from_pathkeys(subplan,
1927 best_path->winpathkeys,
1937 /* Now deconstruct that into partition and ordering portions */
1938 get_column_info_for_window(root,
1940 subplan->targetlist,
1950 /* And finally we can make the WindowAgg node */
1951 plan = make_windowagg(tlist,
1964 copy_generic_path_info(&plan->plan, (Path *) best_path);
1970 * get_column_info_for_window
1971 * Get the partitioning/ordering column numbers and equality operators
1972 * for a WindowAgg node.
1974 * This depends on the behavior of planner.c's make_pathkeys_for_window!
1976 * We are given the target WindowClause and an array of the input column
1977 * numbers associated with the resulting pathkeys. In the easy case, there
1978 * are the same number of pathkey columns as partitioning + ordering columns
1979 * and we just have to copy some data around. However, it's possible that
1980 * some of the original partitioning + ordering columns were eliminated as
1981 * redundant during the transformation to pathkeys. (This can happen even
1982 * though the parser gets rid of obvious duplicates. A typical scenario is a
1983 * window specification "PARTITION BY x ORDER BY y" coupled with a clause
1984 * "WHERE x = y" that causes the two sort columns to be recognized as
1985 * redundant.) In that unusual case, we have to work a lot harder to
1986 * determine which keys are significant.
1988 * The method used here is a bit brute-force: add the sort columns to a list
1989 * one at a time and note when the resulting pathkey list gets longer. But
1990 * it's a sufficiently uncommon case that a faster way doesn't seem worth
1991 * the amount of code refactoring that'd be needed.
1994 get_column_info_for_window(PlannerInfo *root, WindowClause *wc, List *tlist,
1995 int numSortCols, AttrNumber *sortColIdx,
1997 AttrNumber **partColIdx,
1998 Oid **partOperators,
2000 AttrNumber **ordColIdx,
2003 int numPart = list_length(wc->partitionClause);
2004 int numOrder = list_length(wc->orderClause);
2006 if (numSortCols == numPart + numOrder)
2009 *partNumCols = numPart;
2010 *partColIdx = sortColIdx;
2011 *partOperators = extract_grouping_ops(wc->partitionClause);
2012 *ordNumCols = numOrder;
2013 *ordColIdx = sortColIdx + numPart;
2014 *ordOperators = extract_grouping_ops(wc->orderClause);
2023 /* first, allocate what's certainly enough space for the arrays */
2025 *partColIdx = (AttrNumber *) palloc(numPart * sizeof(AttrNumber));
2026 *partOperators = (Oid *) palloc(numPart * sizeof(Oid));
2028 *ordColIdx = (AttrNumber *) palloc(numOrder * sizeof(AttrNumber));
2029 *ordOperators = (Oid *) palloc(numOrder * sizeof(Oid));
2033 foreach(lc, wc->partitionClause)
2035 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2038 sortclauses = lappend(sortclauses, sgc);
2039 new_pathkeys = make_pathkeys_for_sortclauses(root,
2042 if (list_length(new_pathkeys) > list_length(pathkeys))
2044 /* this sort clause is actually significant */
2045 (*partColIdx)[*partNumCols] = sortColIdx[scidx++];
2046 (*partOperators)[*partNumCols] = sgc->eqop;
2048 pathkeys = new_pathkeys;
2051 foreach(lc, wc->orderClause)
2053 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2056 sortclauses = lappend(sortclauses, sgc);
2057 new_pathkeys = make_pathkeys_for_sortclauses(root,
2060 if (list_length(new_pathkeys) > list_length(pathkeys))
2062 /* this sort clause is actually significant */
2063 (*ordColIdx)[*ordNumCols] = sortColIdx[scidx++];
2064 (*ordOperators)[*ordNumCols] = sgc->eqop;
2066 pathkeys = new_pathkeys;
2069 /* complain if we didn't eat exactly the right number of sort cols */
2070 if (scidx != numSortCols)
2071 elog(ERROR, "failed to deconstruct sort operators into partitioning/ordering operators");
2078 * Create a SetOp plan for 'best_path' and (recursively) plans
2082 create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
2089 * SetOp doesn't project, so tlist requirements pass through; moreover we
2090 * need grouping columns to be labeled.
2092 subplan = create_plan_recurse(root, best_path->subpath,
2093 flags | CP_LABEL_TLIST);
2095 /* Convert numGroups to long int --- but 'ware overflow! */
2096 numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2098 plan = make_setop(best_path->cmd,
2099 best_path->strategy,
2101 best_path->distinctList,
2102 best_path->flagColIdx,
2103 best_path->firstFlag,
2106 copy_generic_path_info(&plan->plan, (Path *) best_path);
2112 * create_recursiveunion_plan
2114 * Create a RecursiveUnion plan for 'best_path' and (recursively) plans
2117 static RecursiveUnion *
2118 create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
2120 RecursiveUnion *plan;
2126 /* Need both children to produce same tlist, so force it */
2127 leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2128 rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2130 tlist = build_path_tlist(root, &best_path->path);
2132 /* Convert numGroups to long int --- but 'ware overflow! */
2133 numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2135 plan = make_recursive_union(tlist,
2139 best_path->distinctList,
2142 copy_generic_path_info(&plan->plan, (Path *) best_path);
2148 * create_lockrows_plan
2150 * Create a LockRows plan for 'best_path' and (recursively) plans
2154 create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
2160 /* LockRows doesn't project, so tlist requirements pass through */
2161 subplan = create_plan_recurse(root, best_path->subpath, flags);
2163 plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2165 copy_generic_path_info(&plan->plan, (Path *) best_path);
2171 * create_modifytable_plan
2172 * Create a ModifyTable plan for 'best_path'.
2174 * Returns a Plan node.
2176 static ModifyTable *
2177 create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
2180 List *subplans = NIL;
2184 /* Build the plan for each input path */
2185 forboth(subpaths, best_path->subpaths,
2186 subroots, best_path->subroots)
2188 Path *subpath = (Path *) lfirst(subpaths);
2189 PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2193 * In an inherited UPDATE/DELETE, reference the per-child modified
2194 * subroot while creating Plans from Paths for the child rel. This is
2195 * a kluge, but otherwise it's too hard to ensure that Plan creation
2196 * functions (particularly in FDWs) don't depend on the contents of
2197 * "root" matching what they saw at Path creation time. The main
2198 * downside is that creation functions for Plans that might appear
2199 * below a ModifyTable cannot expect to modify the contents of "root"
2200 * and have it "stick" for subsequent processing such as setrefs.c.
2201 * That's not great, but it seems better than the alternative.
2203 subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2205 /* Transfer resname/resjunk labeling, too, to keep executor happy */
2206 apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2208 subplans = lappend(subplans, subplan);
2211 plan = make_modifytable(root,
2212 best_path->operation,
2213 best_path->canSetTag,
2214 best_path->nominalRelation,
2215 best_path->resultRelations,
2217 best_path->withCheckOptionLists,
2218 best_path->returningLists,
2219 best_path->rowMarks,
2220 best_path->onconflict,
2221 best_path->epqParam);
2223 copy_generic_path_info(&plan->plan, &best_path->path);
2231 * Create a Limit plan for 'best_path' and (recursively) plans
2235 create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
2240 /* Limit doesn't project, so tlist requirements pass through */
2241 subplan = create_plan_recurse(root, best_path->subpath, flags);
2243 plan = make_limit(subplan,
2244 best_path->limitOffset,
2245 best_path->limitCount);
2247 copy_generic_path_info(&plan->plan, (Path *) best_path);
2253 /*****************************************************************************
2255 * BASE-RELATION SCAN METHODS
2257 *****************************************************************************/
2261 * create_seqscan_plan
2262 * Returns a seqscan plan for the base relation scanned by 'best_path'
2263 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2266 create_seqscan_plan(PlannerInfo *root, Path *best_path,
2267 List *tlist, List *scan_clauses)
2270 Index scan_relid = best_path->parent->relid;
2272 /* it should be a base rel... */
2273 Assert(scan_relid > 0);
2274 Assert(best_path->parent->rtekind == RTE_RELATION);
2276 /* Sort clauses into best execution order */
2277 scan_clauses = order_qual_clauses(root, scan_clauses);
2279 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2280 scan_clauses = extract_actual_clauses(scan_clauses, false);
2282 /* Replace any outer-relation variables with nestloop params */
2283 if (best_path->param_info)
2285 scan_clauses = (List *)
2286 replace_nestloop_params(root, (Node *) scan_clauses);
2289 scan_plan = make_seqscan(tlist,
2293 copy_generic_path_info(&scan_plan->plan, best_path);
2299 * create_samplescan_plan
2300 * Returns a samplescan plan for the base relation scanned by 'best_path'
2301 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2304 create_samplescan_plan(PlannerInfo *root, Path *best_path,
2305 List *tlist, List *scan_clauses)
2307 SampleScan *scan_plan;
2308 Index scan_relid = best_path->parent->relid;
2310 TableSampleClause *tsc;
2312 /* it should be a base rel with a tablesample clause... */
2313 Assert(scan_relid > 0);
2314 rte = planner_rt_fetch(scan_relid, root);
2315 Assert(rte->rtekind == RTE_RELATION);
2316 tsc = rte->tablesample;
2317 Assert(tsc != NULL);
2319 /* Sort clauses into best execution order */
2320 scan_clauses = order_qual_clauses(root, scan_clauses);
2322 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2323 scan_clauses = extract_actual_clauses(scan_clauses, false);
2325 /* Replace any outer-relation variables with nestloop params */
2326 if (best_path->param_info)
2328 scan_clauses = (List *)
2329 replace_nestloop_params(root, (Node *) scan_clauses);
2330 tsc = (TableSampleClause *)
2331 replace_nestloop_params(root, (Node *) tsc);
2334 scan_plan = make_samplescan(tlist,
2339 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2345 * create_indexscan_plan
2346 * Returns an indexscan plan for the base relation scanned by 'best_path'
2347 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2349 * We use this for both plain IndexScans and IndexOnlyScans, because the
2350 * qual preprocessing work is the same for both. Note that the caller tells
2351 * us which to build --- we don't look at best_path->path.pathtype, because
2352 * create_bitmap_subplan needs to be able to override the prior decision.
2355 create_indexscan_plan(PlannerInfo *root,
2356 IndexPath *best_path,
2362 List *indexquals = best_path->indexquals;
2363 List *indexorderbys = best_path->indexorderbys;
2364 Index baserelid = best_path->path.parent->relid;
2365 Oid indexoid = best_path->indexinfo->indexoid;
2367 List *stripped_indexquals;
2368 List *fixed_indexquals;
2369 List *fixed_indexorderbys;
2370 List *indexorderbyops = NIL;
2373 /* it should be a base rel... */
2374 Assert(baserelid > 0);
2375 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2378 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
2379 * executor as indexqualorig
2381 stripped_indexquals = get_actual_clauses(indexquals);
2384 * The executor needs a copy with the indexkey on the left of each clause
2385 * and with index Vars substituted for table ones.
2387 fixed_indexquals = fix_indexqual_references(root, best_path);
2390 * Likewise fix up index attr references in the ORDER BY expressions.
2392 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2395 * The qpqual list must contain all restrictions not automatically handled
2396 * by the index, other than pseudoconstant clauses which will be handled
2397 * by a separate gating plan node. All the predicates in the indexquals
2398 * will be checked (either by the index itself, or by nodeIndexscan.c),
2399 * but if there are any "special" operators involved then they must be
2400 * included in qpqual. The upshot is that qpqual must contain
2401 * scan_clauses minus whatever appears in indexquals.
2403 * In normal cases simple pointer equality checks will be enough to spot
2404 * duplicate RestrictInfos, so we try that first.
2406 * Another common case is that a scan_clauses entry is generated from the
2407 * same EquivalenceClass as some indexqual, and is therefore redundant
2408 * with it, though not equal. (This happens when indxpath.c prefers a
2409 * different derived equality than what generate_join_implied_equalities
2410 * picked for a parameterized scan's ppi_clauses.)
2412 * In some situations (particularly with OR'd index conditions) we may
2413 * have scan_clauses that are not equal to, but are logically implied by,
2414 * the index quals; so we also try a predicate_implied_by() check to see
2415 * if we can discard quals that way. (predicate_implied_by assumes its
2416 * first input contains only immutable functions, so we have to check
2419 * Note: if you change this bit of code you should also look at
2420 * extract_nonindex_conditions() in costsize.c.
2423 foreach(l, scan_clauses)
2425 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2427 Assert(IsA(rinfo, RestrictInfo));
2428 if (rinfo->pseudoconstant)
2429 continue; /* we may drop pseudoconstants here */
2430 if (list_member_ptr(indexquals, rinfo))
2431 continue; /* simple duplicate */
2432 if (is_redundant_derived_clause(rinfo, indexquals))
2433 continue; /* derived from same EquivalenceClass */
2434 if (!contain_mutable_functions((Node *) rinfo->clause) &&
2435 predicate_implied_by(list_make1(rinfo->clause), indexquals))
2436 continue; /* provably implied by indexquals */
2437 qpqual = lappend(qpqual, rinfo);
2440 /* Sort clauses into best execution order */
2441 qpqual = order_qual_clauses(root, qpqual);
2443 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2444 qpqual = extract_actual_clauses(qpqual, false);
2447 * We have to replace any outer-relation variables with nestloop params in
2448 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2449 * annoying to have to do this separately from the processing in
2450 * fix_indexqual_references --- rethink this when generalizing the inner
2451 * indexscan support. But note we can't really do this earlier because
2452 * it'd break the comparisons to predicates above ... (or would it? Those
2453 * wouldn't have outer refs)
2455 if (best_path->path.param_info)
2457 stripped_indexquals = (List *)
2458 replace_nestloop_params(root, (Node *) stripped_indexquals);
2460 replace_nestloop_params(root, (Node *) qpqual);
2461 indexorderbys = (List *)
2462 replace_nestloop_params(root, (Node *) indexorderbys);
2466 * If there are ORDER BY expressions, look up the sort operators for their
2471 ListCell *pathkeyCell,
2475 * PathKey contains OID of the btree opfamily we're sorting by, but
2476 * that's not quite enough because we need the expression's datatype
2477 * to look up the sort operator in the operator family.
2479 Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2480 forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2482 PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2483 Node *expr = (Node *) lfirst(exprCell);
2484 Oid exprtype = exprType(expr);
2487 /* Get sort operator from opfamily */
2488 sortop = get_opfamily_member(pathkey->pk_opfamily,
2491 pathkey->pk_strategy);
2492 if (!OidIsValid(sortop))
2493 elog(ERROR, "failed to find sort operator for ORDER BY expression");
2494 indexorderbyops = lappend_oid(indexorderbyops, sortop);
2498 /* Finally ready to build the plan node */
2500 scan_plan = (Scan *) make_indexonlyscan(tlist,
2505 fixed_indexorderbys,
2506 best_path->indexinfo->indextlist,
2507 best_path->indexscandir);
2509 scan_plan = (Scan *) make_indexscan(tlist,
2514 stripped_indexquals,
2515 fixed_indexorderbys,
2518 best_path->indexscandir);
2520 copy_generic_path_info(&scan_plan->plan, &best_path->path);
2526 * create_bitmap_scan_plan
2527 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
2528 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2530 static BitmapHeapScan *
2531 create_bitmap_scan_plan(PlannerInfo *root,
2532 BitmapHeapPath *best_path,
2536 Index baserelid = best_path->path.parent->relid;
2537 Plan *bitmapqualplan;
2538 List *bitmapqualorig;
2543 BitmapHeapScan *scan_plan;
2545 /* it should be a base rel... */
2546 Assert(baserelid > 0);
2547 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2549 /* Process the bitmapqual tree into a Plan tree and qual lists */
2550 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
2551 &bitmapqualorig, &indexquals,
2555 * The qpqual list must contain all restrictions not automatically handled
2556 * by the index, other than pseudoconstant clauses which will be handled
2557 * by a separate gating plan node. All the predicates in the indexquals
2558 * will be checked (either by the index itself, or by
2559 * nodeBitmapHeapscan.c), but if there are any "special" operators
2560 * involved then they must be added to qpqual. The upshot is that qpqual
2561 * must contain scan_clauses minus whatever appears in indexquals.
2563 * This loop is similar to the comparable code in create_indexscan_plan(),
2564 * but with some differences because it has to compare the scan clauses to
2565 * stripped (no RestrictInfos) indexquals. See comments there for more
2568 * In normal cases simple equal() checks will be enough to spot duplicate
2569 * clauses, so we try that first. We next see if the scan clause is
2570 * redundant with any top-level indexqual by virtue of being generated
2571 * from the same EC. After that, try predicate_implied_by().
2573 * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
2574 * useful for getting rid of qpquals that are implied by index predicates,
2575 * because the predicate conditions are included in the "indexquals"
2576 * returned by create_bitmap_subplan(). Bitmap scans have to do it that
2577 * way because predicate conditions need to be rechecked if the scan
2578 * becomes lossy, so they have to be included in bitmapqualorig.
2581 foreach(l, scan_clauses)
2583 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2584 Node *clause = (Node *) rinfo->clause;
2586 Assert(IsA(rinfo, RestrictInfo));
2587 if (rinfo->pseudoconstant)
2588 continue; /* we may drop pseudoconstants here */
2589 if (list_member(indexquals, clause))
2590 continue; /* simple duplicate */
2591 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
2592 continue; /* derived from same EquivalenceClass */
2593 if (!contain_mutable_functions(clause) &&
2594 predicate_implied_by(list_make1(clause), indexquals))
2595 continue; /* provably implied by indexquals */
2596 qpqual = lappend(qpqual, rinfo);
2599 /* Sort clauses into best execution order */
2600 qpqual = order_qual_clauses(root, qpqual);
2602 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2603 qpqual = extract_actual_clauses(qpqual, false);
2606 * When dealing with special operators, we will at this point have
2607 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
2608 * 'em from bitmapqualorig, since there's no point in making the tests
2611 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
2614 * We have to replace any outer-relation variables with nestloop params in
2615 * the qpqual and bitmapqualorig expressions. (This was already done for
2616 * expressions attached to plan nodes in the bitmapqualplan tree.)
2618 if (best_path->path.param_info)
2621 replace_nestloop_params(root, (Node *) qpqual);
2622 bitmapqualorig = (List *)
2623 replace_nestloop_params(root, (Node *) bitmapqualorig);
2626 /* Finally ready to build the plan node */
2627 scan_plan = make_bitmap_heapscan(tlist,
2633 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2639 * Given a bitmapqual tree, generate the Plan tree that implements it
2641 * As byproducts, we also return in *qual and *indexqual the qual lists
2642 * (in implicit-AND form, without RestrictInfos) describing the original index
2643 * conditions and the generated indexqual conditions. (These are the same in
2644 * simple cases, but when special index operators are involved, the former
2645 * list includes the special conditions while the latter includes the actual
2646 * indexable conditions derived from them.) Both lists include partial-index
2647 * predicates, because we have to recheck predicates as well as index
2648 * conditions if the bitmap scan becomes lossy.
2650 * In addition, we return a list of EquivalenceClass pointers for all the
2651 * top-level indexquals that were possibly-redundantly derived from ECs.
2652 * This allows removal of scan_clauses that are redundant with such quals.
2653 * (We do not attempt to detect such redundancies for quals that are within
2654 * OR subtrees. This could be done in a less hacky way if we returned the
2655 * indexquals in RestrictInfo form, but that would be slower and still pretty
2656 * messy, since we'd have to build new RestrictInfos in many cases.)
2659 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
2660 List **qual, List **indexqual, List **indexECs)
2664 if (IsA(bitmapqual, BitmapAndPath))
2666 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
2667 List *subplans = NIL;
2668 List *subquals = NIL;
2669 List *subindexquals = NIL;
2670 List *subindexECs = NIL;
2674 * There may well be redundant quals among the subplans, since a
2675 * top-level WHERE qual might have gotten used to form several
2676 * different index quals. We don't try exceedingly hard to eliminate
2677 * redundancies, but we do eliminate obvious duplicates by using
2678 * list_concat_unique.
2680 foreach(l, apath->bitmapquals)
2687 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2688 &subqual, &subindexqual,
2690 subplans = lappend(subplans, subplan);
2691 subquals = list_concat_unique(subquals, subqual);
2692 subindexquals = list_concat_unique(subindexquals, subindexqual);
2693 /* Duplicates in indexECs aren't worth getting rid of */
2694 subindexECs = list_concat(subindexECs, subindexEC);
2696 plan = (Plan *) make_bitmap_and(subplans);
2697 plan->startup_cost = apath->path.startup_cost;
2698 plan->total_cost = apath->path.total_cost;
2700 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
2701 plan->plan_width = 0; /* meaningless */
2702 plan->parallel_aware = false;
2704 *indexqual = subindexquals;
2705 *indexECs = subindexECs;
2707 else if (IsA(bitmapqual, BitmapOrPath))
2709 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
2710 List *subplans = NIL;
2711 List *subquals = NIL;
2712 List *subindexquals = NIL;
2713 bool const_true_subqual = false;
2714 bool const_true_subindexqual = false;
2718 * Here, we only detect qual-free subplans. A qual-free subplan would
2719 * cause us to generate "... OR true ..." which we may as well reduce
2720 * to just "true". We do not try to eliminate redundant subclauses
2721 * because (a) it's not as likely as in the AND case, and (b) we might
2722 * well be working with hundreds or even thousands of OR conditions,
2723 * perhaps from a long IN list. The performance of list_append_unique
2724 * would be unacceptable.
2726 foreach(l, opath->bitmapquals)
2733 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2734 &subqual, &subindexqual,
2736 subplans = lappend(subplans, subplan);
2738 const_true_subqual = true;
2739 else if (!const_true_subqual)
2740 subquals = lappend(subquals,
2741 make_ands_explicit(subqual));
2742 if (subindexqual == NIL)
2743 const_true_subindexqual = true;
2744 else if (!const_true_subindexqual)
2745 subindexquals = lappend(subindexquals,
2746 make_ands_explicit(subindexqual));
2750 * In the presence of ScalarArrayOpExpr quals, we might have built
2751 * BitmapOrPaths with just one subpath; don't add an OR step.
2753 if (list_length(subplans) == 1)
2755 plan = (Plan *) linitial(subplans);
2759 plan = (Plan *) make_bitmap_or(subplans);
2760 plan->startup_cost = opath->path.startup_cost;
2761 plan->total_cost = opath->path.total_cost;
2763 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
2764 plan->plan_width = 0; /* meaningless */
2765 plan->parallel_aware = false;
2769 * If there were constant-TRUE subquals, the OR reduces to constant
2770 * TRUE. Also, avoid generating one-element ORs, which could happen
2771 * due to redundancy elimination or ScalarArrayOpExpr quals.
2773 if (const_true_subqual)
2775 else if (list_length(subquals) <= 1)
2778 *qual = list_make1(make_orclause(subquals));
2779 if (const_true_subindexqual)
2781 else if (list_length(subindexquals) <= 1)
2782 *indexqual = subindexquals;
2784 *indexqual = list_make1(make_orclause(subindexquals));
2787 else if (IsA(bitmapqual, IndexPath))
2789 IndexPath *ipath = (IndexPath *) bitmapqual;
2794 /* Use the regular indexscan plan build machinery... */
2795 iscan = (IndexScan *) create_indexscan_plan(root, ipath,
2797 Assert(IsA(iscan, IndexScan));
2798 /* then convert to a bitmap indexscan */
2799 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
2802 iscan->indexqualorig);
2803 /* and set its cost/width fields appropriately */
2804 plan->startup_cost = 0.0;
2805 plan->total_cost = ipath->indextotalcost;
2807 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
2808 plan->plan_width = 0; /* meaningless */
2809 plan->parallel_aware = false;
2810 *qual = get_actual_clauses(ipath->indexclauses);
2811 *indexqual = get_actual_clauses(ipath->indexquals);
2812 foreach(l, ipath->indexinfo->indpred)
2814 Expr *pred = (Expr *) lfirst(l);
2817 * We know that the index predicate must have been implied by the
2818 * query condition as a whole, but it may or may not be implied by
2819 * the conditions that got pushed into the bitmapqual. Avoid
2820 * generating redundant conditions.
2822 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
2824 *qual = lappend(*qual, pred);
2825 *indexqual = lappend(*indexqual, pred);
2829 foreach(l, ipath->indexquals)
2831 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2833 if (rinfo->parent_ec)
2834 subindexECs = lappend(subindexECs, rinfo->parent_ec);
2836 *indexECs = subindexECs;
2840 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
2841 plan = NULL; /* keep compiler quiet */
2848 * create_tidscan_plan
2849 * Returns a tidscan plan for the base relation scanned by 'best_path'
2850 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2853 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
2854 List *tlist, List *scan_clauses)
2857 Index scan_relid = best_path->path.parent->relid;
2858 List *tidquals = best_path->tidquals;
2861 /* it should be a base rel... */
2862 Assert(scan_relid > 0);
2863 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2865 /* Sort clauses into best execution order */
2866 scan_clauses = order_qual_clauses(root, scan_clauses);
2868 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2869 scan_clauses = extract_actual_clauses(scan_clauses, false);
2871 /* Replace any outer-relation variables with nestloop params */
2872 if (best_path->path.param_info)
2875 replace_nestloop_params(root, (Node *) tidquals);
2876 scan_clauses = (List *)
2877 replace_nestloop_params(root, (Node *) scan_clauses);
2881 * Remove any clauses that are TID quals. This is a bit tricky since the
2882 * tidquals list has implicit OR semantics.
2884 ortidquals = tidquals;
2885 if (list_length(ortidquals) > 1)
2886 ortidquals = list_make1(make_orclause(ortidquals));
2887 scan_clauses = list_difference(scan_clauses, ortidquals);
2889 scan_plan = make_tidscan(tlist,
2894 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2900 * create_subqueryscan_plan
2901 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
2902 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2904 static SubqueryScan *
2905 create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path,
2906 List *tlist, List *scan_clauses)
2908 SubqueryScan *scan_plan;
2909 RelOptInfo *rel = best_path->path.parent;
2910 Index scan_relid = rel->relid;
2913 /* it should be a subquery base rel... */
2914 Assert(scan_relid > 0);
2915 Assert(rel->rtekind == RTE_SUBQUERY);
2918 * Recursively create Plan from Path for subquery. Since we are entering
2919 * a different planner context (subroot), recurse to create_plan not
2920 * create_plan_recurse.
2922 subplan = create_plan(rel->subroot, best_path->subpath);
2924 /* Sort clauses into best execution order */
2925 scan_clauses = order_qual_clauses(root, scan_clauses);
2927 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2928 scan_clauses = extract_actual_clauses(scan_clauses, false);
2930 /* Replace any outer-relation variables with nestloop params */
2931 if (best_path->path.param_info)
2933 scan_clauses = (List *)
2934 replace_nestloop_params(root, (Node *) scan_clauses);
2935 process_subquery_nestloop_params(root,
2936 rel->subplan_params);
2939 scan_plan = make_subqueryscan(tlist,
2944 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2950 * create_functionscan_plan
2951 * Returns a functionscan plan for the base relation scanned by 'best_path'
2952 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2954 static FunctionScan *
2955 create_functionscan_plan(PlannerInfo *root, Path *best_path,
2956 List *tlist, List *scan_clauses)
2958 FunctionScan *scan_plan;
2959 Index scan_relid = best_path->parent->relid;
2963 /* it should be a function base rel... */
2964 Assert(scan_relid > 0);
2965 rte = planner_rt_fetch(scan_relid, root);
2966 Assert(rte->rtekind == RTE_FUNCTION);
2967 functions = rte->functions;
2969 /* Sort clauses into best execution order */
2970 scan_clauses = order_qual_clauses(root, scan_clauses);
2972 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2973 scan_clauses = extract_actual_clauses(scan_clauses, false);
2975 /* Replace any outer-relation variables with nestloop params */
2976 if (best_path->param_info)
2978 scan_clauses = (List *)
2979 replace_nestloop_params(root, (Node *) scan_clauses);
2980 /* The function expressions could contain nestloop params, too */
2981 functions = (List *) replace_nestloop_params(root, (Node *) functions);
2984 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
2985 functions, rte->funcordinality);
2987 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2993 * create_valuesscan_plan
2994 * Returns a valuesscan plan for the base relation scanned by 'best_path'
2995 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2998 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
2999 List *tlist, List *scan_clauses)
3001 ValuesScan *scan_plan;
3002 Index scan_relid = best_path->parent->relid;
3006 /* it should be a values base rel... */
3007 Assert(scan_relid > 0);
3008 rte = planner_rt_fetch(scan_relid, root);
3009 Assert(rte->rtekind == RTE_VALUES);
3010 values_lists = rte->values_lists;
3012 /* Sort clauses into best execution order */
3013 scan_clauses = order_qual_clauses(root, scan_clauses);
3015 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3016 scan_clauses = extract_actual_clauses(scan_clauses, false);
3018 /* Replace any outer-relation variables with nestloop params */
3019 if (best_path->param_info)
3021 scan_clauses = (List *)
3022 replace_nestloop_params(root, (Node *) scan_clauses);
3023 /* The values lists could contain nestloop params, too */
3024 values_lists = (List *)
3025 replace_nestloop_params(root, (Node *) values_lists);
3028 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
3031 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3037 * create_ctescan_plan
3038 * Returns a ctescan plan for the base relation scanned by 'best_path'
3039 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3042 create_ctescan_plan(PlannerInfo *root, Path *best_path,
3043 List *tlist, List *scan_clauses)
3046 Index scan_relid = best_path->parent->relid;
3048 SubPlan *ctesplan = NULL;
3051 PlannerInfo *cteroot;
3056 Assert(scan_relid > 0);
3057 rte = planner_rt_fetch(scan_relid, root);
3058 Assert(rte->rtekind == RTE_CTE);
3059 Assert(!rte->self_reference);
3062 * Find the referenced CTE, and locate the SubPlan previously made for it.
3064 levelsup = rte->ctelevelsup;
3066 while (levelsup-- > 0)
3068 cteroot = cteroot->parent_root;
3069 if (!cteroot) /* shouldn't happen */
3070 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3074 * Note: cte_plan_ids can be shorter than cteList, if we are still working
3075 * on planning the CTEs (ie, this is a side-reference from another CTE).
3076 * So we mustn't use forboth here.
3079 foreach(lc, cteroot->parse->cteList)
3081 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3083 if (strcmp(cte->ctename, rte->ctename) == 0)
3087 if (lc == NULL) /* shouldn't happen */
3088 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3089 if (ndx >= list_length(cteroot->cte_plan_ids))
3090 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3091 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3092 Assert(plan_id > 0);
3093 foreach(lc, cteroot->init_plans)
3095 ctesplan = (SubPlan *) lfirst(lc);
3096 if (ctesplan->plan_id == plan_id)
3099 if (lc == NULL) /* shouldn't happen */
3100 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3103 * We need the CTE param ID, which is the sole member of the SubPlan's
3106 cte_param_id = linitial_int(ctesplan->setParam);
3108 /* Sort clauses into best execution order */
3109 scan_clauses = order_qual_clauses(root, scan_clauses);
3111 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3112 scan_clauses = extract_actual_clauses(scan_clauses, false);
3114 /* Replace any outer-relation variables with nestloop params */
3115 if (best_path->param_info)
3117 scan_clauses = (List *)
3118 replace_nestloop_params(root, (Node *) scan_clauses);
3121 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3122 plan_id, cte_param_id);
3124 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3130 * create_worktablescan_plan
3131 * Returns a worktablescan plan for the base relation scanned by 'best_path'
3132 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3134 static WorkTableScan *
3135 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
3136 List *tlist, List *scan_clauses)
3138 WorkTableScan *scan_plan;
3139 Index scan_relid = best_path->parent->relid;
3142 PlannerInfo *cteroot;
3144 Assert(scan_relid > 0);
3145 rte = planner_rt_fetch(scan_relid, root);
3146 Assert(rte->rtekind == RTE_CTE);
3147 Assert(rte->self_reference);
3150 * We need to find the worktable param ID, which is in the plan level
3151 * that's processing the recursive UNION, which is one level *below* where
3152 * the CTE comes from.
3154 levelsup = rte->ctelevelsup;
3155 if (levelsup == 0) /* shouldn't happen */
3156 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3159 while (levelsup-- > 0)
3161 cteroot = cteroot->parent_root;
3162 if (!cteroot) /* shouldn't happen */
3163 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3165 if (cteroot->wt_param_id < 0) /* shouldn't happen */
3166 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
3168 /* Sort clauses into best execution order */
3169 scan_clauses = order_qual_clauses(root, scan_clauses);
3171 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3172 scan_clauses = extract_actual_clauses(scan_clauses, false);
3174 /* Replace any outer-relation variables with nestloop params */
3175 if (best_path->param_info)
3177 scan_clauses = (List *)
3178 replace_nestloop_params(root, (Node *) scan_clauses);
3181 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
3182 cteroot->wt_param_id);
3184 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3190 * create_foreignscan_plan
3191 * Returns a foreignscan plan for the relation scanned by 'best_path'
3192 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3194 static ForeignScan *
3195 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
3196 List *tlist, List *scan_clauses)
3198 ForeignScan *scan_plan;
3199 RelOptInfo *rel = best_path->path.parent;
3200 Index scan_relid = rel->relid;
3201 Oid rel_oid = InvalidOid;
3202 Plan *outer_plan = NULL;
3204 Assert(rel->fdwroutine != NULL);
3206 /* transform the child path if any */
3207 if (best_path->fdw_outerpath)
3208 outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3212 * If we're scanning a base relation, fetch its OID. (Irrelevant if
3213 * scanning a join relation.)
3219 Assert(rel->rtekind == RTE_RELATION);
3220 rte = planner_rt_fetch(scan_relid, root);
3221 Assert(rte->rtekind == RTE_RELATION);
3222 rel_oid = rte->relid;
3226 * Sort clauses into best execution order. We do this first since the FDW
3227 * might have more info than we do and wish to adjust the ordering.
3229 scan_clauses = order_qual_clauses(root, scan_clauses);
3232 * Let the FDW perform its processing on the restriction clauses and
3233 * generate the plan node. Note that the FDW might remove restriction
3234 * clauses that it intends to execute remotely, or even add more (if it
3235 * has selected some join clauses for remote use but also wants them
3236 * rechecked locally).
3238 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
3240 tlist, scan_clauses,
3243 /* Copy cost data from Path to Plan; no need to make FDW do this */
3244 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3246 /* Copy foreign server OID; likewise, no need to make FDW do this */
3247 scan_plan->fs_server = rel->serverid;
3249 /* Likewise, copy the relids that are represented by this foreign scan */
3250 scan_plan->fs_relids = best_path->path.parent->relids;
3253 * If a join between foreign relations was pushed down, remember it. The
3254 * push-down safety of the join depends upon the server and user mapping
3255 * being same. That can change between planning and execution time, in
3256 * which case the plan should be invalidated.
3258 if (scan_relid == 0)
3259 root->glob->hasForeignJoin = true;
3262 * Replace any outer-relation variables with nestloop params in the qual,
3263 * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
3264 * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
3265 * fdw_recheck_quals could have come from join clauses, so doing this
3266 * beforehand on the scan_clauses wouldn't work.) We assume
3267 * fdw_scan_tlist contains no such variables.
3269 if (best_path->path.param_info)
3271 scan_plan->scan.plan.qual = (List *)
3272 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
3273 scan_plan->fdw_exprs = (List *)
3274 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
3275 scan_plan->fdw_recheck_quals = (List *)
3276 replace_nestloop_params(root,
3277 (Node *) scan_plan->fdw_recheck_quals);
3281 * If rel is a base relation, detect whether any system columns are
3282 * requested from the rel. (If rel is a join relation, rel->relid will be
3283 * 0, but there can be no Var with relid 0 in the rel's targetlist or the
3284 * restriction clauses, so we skip this in that case. Note that any such
3285 * columns in base relations that were joined are assumed to be contained
3286 * in fdw_scan_tlist.) This is a bit of a kluge and might go away
3287 * someday, so we intentionally leave it out of the API presented to FDWs.
3289 scan_plan->fsSystemCol = false;
3292 Bitmapset *attrs_used = NULL;
3297 * First, examine all the attributes needed for joins or final output.
3298 * Note: we must look at rel's targetlist, not the attr_needed data,
3299 * because attr_needed isn't computed for inheritance child rels.
3301 pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
3303 /* Add all the attributes used by restriction clauses. */
3304 foreach(lc, rel->baserestrictinfo)
3306 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3308 pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
3311 /* Now, are any system columns requested from rel? */
3312 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
3314 if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
3316 scan_plan->fsSystemCol = true;
3321 bms_free(attrs_used);
3328 * create_custom_plan
3330 * Transform a CustomPath into a Plan.
3333 create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
3334 List *tlist, List *scan_clauses)
3337 RelOptInfo *rel = best_path->path.parent;
3338 List *custom_plans = NIL;
3341 /* Recursively transform child paths. */
3342 foreach(lc, best_path->custom_paths)
3344 Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3347 custom_plans = lappend(custom_plans, plan);
3351 * Sort clauses into the best execution order, although custom-scan
3352 * provider can reorder them again.
3354 scan_clauses = order_qual_clauses(root, scan_clauses);
3357 * Invoke custom plan provider to create the Plan node represented by the
3360 cplan = (CustomScan *) best_path->methods->PlanCustomPath(root,
3366 Assert(IsA(cplan, CustomScan));
3369 * Copy cost data from Path to Plan; no need to make custom-plan providers
3372 copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3374 /* Likewise, copy the relids that are represented by this custom scan */
3375 cplan->custom_relids = best_path->path.parent->relids;
3378 * Replace any outer-relation variables with nestloop params in the qual
3379 * and custom_exprs expressions. We do this last so that the custom-plan
3380 * provider doesn't have to be involved. (Note that parts of custom_exprs
3381 * could have come from join clauses, so doing this beforehand on the
3382 * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3385 if (best_path->path.param_info)
3387 cplan->scan.plan.qual = (List *)
3388 replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3389 cplan->custom_exprs = (List *)
3390 replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3397 /*****************************************************************************
3401 *****************************************************************************/
3404 create_nestloop_plan(PlannerInfo *root,
3405 NestPath *best_path)
3407 NestLoop *join_plan;
3410 List *tlist = build_path_tlist(root, &best_path->path);
3411 List *joinrestrictclauses = best_path->joinrestrictinfo;
3416 Relids saveOuterRels = root->curOuterRels;
3421 /* NestLoop can project, so no need to be picky about child tlists */
3422 outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3424 /* For a nestloop, include outer relids in curOuterRels for inner side */
3425 root->curOuterRels = bms_union(root->curOuterRels,
3426 best_path->outerjoinpath->parent->relids);
3428 inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3430 /* Restore curOuterRels */
3431 bms_free(root->curOuterRels);
3432 root->curOuterRels = saveOuterRels;
3434 /* Sort join qual clauses into best execution order */
3435 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3437 /* Get the join qual clauses (in plain expression form) */
3438 /* Any pseudoconstant clauses are ignored here */
3439 if (IS_OUTER_JOIN(best_path->jointype))
3441 extract_actual_join_clauses(joinrestrictclauses,
3442 &joinclauses, &otherclauses);
3446 /* We can treat all clauses alike for an inner join */
3447 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3451 /* Replace any outer-relation variables with nestloop params */
3452 if (best_path->path.param_info)
3454 joinclauses = (List *)
3455 replace_nestloop_params(root, (Node *) joinclauses);
3456 otherclauses = (List *)
3457 replace_nestloop_params(root, (Node *) otherclauses);
3461 * Identify any nestloop parameters that should be supplied by this join
3462 * node, and move them from root->curOuterParams to the nestParams list.
3464 outerrelids = best_path->outerjoinpath->parent->relids;
3467 for (cell = list_head(root->curOuterParams); cell; cell = next)
3469 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
3472 if (IsA(nlp->paramval, Var) &&
3473 bms_is_member(nlp->paramval->varno, outerrelids))
3475 root->curOuterParams = list_delete_cell(root->curOuterParams,
3477 nestParams = lappend(nestParams, nlp);
3479 else if (IsA(nlp->paramval, PlaceHolderVar) &&
3480 bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
3482 bms_is_subset(find_placeholder_info(root,
3483 (PlaceHolderVar *) nlp->paramval,
3487 root->curOuterParams = list_delete_cell(root->curOuterParams,
3489 nestParams = lappend(nestParams, nlp);
3495 join_plan = make_nestloop(tlist,
3501 best_path->jointype);
3503 copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3509 create_mergejoin_plan(PlannerInfo *root,
3510 MergePath *best_path)
3512 MergeJoin *join_plan;
3515 List *tlist = build_path_tlist(root, &best_path->jpath.path);
3519 List *outerpathkeys;
3520 List *innerpathkeys;
3523 Oid *mergecollations;
3524 int *mergestrategies;
3525 bool *mergenullsfirst;
3532 * MergeJoin can project, so we don't have to demand exact tlists from the
3533 * inputs. However, if we're intending to sort an input's result, it's
3534 * best to request a small tlist so we aren't sorting more data than
3537 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
3538 (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3540 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
3541 (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3543 /* Sort join qual clauses into best execution order */
3544 /* NB: do NOT reorder the mergeclauses */
3545 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
3547 /* Get the join qual clauses (in plain expression form) */
3548 /* Any pseudoconstant clauses are ignored here */
3549 if (IS_OUTER_JOIN(best_path->jpath.jointype))
3551 extract_actual_join_clauses(joinclauses,
3552 &joinclauses, &otherclauses);
3556 /* We can treat all clauses alike for an inner join */
3557 joinclauses = extract_actual_clauses(joinclauses, false);
3562 * Remove the mergeclauses from the list of join qual clauses, leaving the
3563 * list of quals that must be checked as qpquals.
3565 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
3566 joinclauses = list_difference(joinclauses, mergeclauses);
3569 * Replace any outer-relation variables with nestloop params. There
3570 * should not be any in the mergeclauses.
3572 if (best_path->jpath.path.param_info)
3574 joinclauses = (List *)
3575 replace_nestloop_params(root, (Node *) joinclauses);
3576 otherclauses = (List *)
3577 replace_nestloop_params(root, (Node *) otherclauses);
3581 * Rearrange mergeclauses, if needed, so that the outer variable is always
3582 * on the left; mark the mergeclause restrictinfos with correct
3583 * outer_is_left status.
3585 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
3586 best_path->jpath.outerjoinpath->parent->relids);
3589 * Create explicit sort nodes for the outer and inner paths if necessary.
3591 if (best_path->outersortkeys)
3593 Sort *sort = make_sort_from_pathkeys(outer_plan,
3594 best_path->outersortkeys);
3596 label_sort_with_costsize(root, sort, -1.0);
3597 outer_plan = (Plan *) sort;
3598 outerpathkeys = best_path->outersortkeys;
3601 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
3603 if (best_path->innersortkeys)
3605 Sort *sort = make_sort_from_pathkeys(inner_plan,
3606 best_path->innersortkeys);
3608 label_sort_with_costsize(root, sort, -1.0);
3609 inner_plan = (Plan *) sort;
3610 innerpathkeys = best_path->innersortkeys;
3613 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
3616 * If specified, add a materialize node to shield the inner plan from the
3617 * need to handle mark/restore.
3619 if (best_path->materialize_inner)
3621 Plan *matplan = (Plan *) make_material(inner_plan);
3624 * We assume the materialize will not spill to disk, and therefore
3625 * charge just cpu_operator_cost per tuple. (Keep this estimate in
3626 * sync with final_cost_mergejoin.)
3628 copy_plan_costsize(matplan, inner_plan);
3629 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
3631 inner_plan = matplan;
3635 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
3636 * executor. The information is in the pathkeys for the two inputs, but
3637 * we need to be careful about the possibility of mergeclauses sharing a
3638 * pathkey (compare find_mergeclauses_for_pathkeys()).
3640 nClauses = list_length(mergeclauses);
3641 Assert(nClauses == list_length(best_path->path_mergeclauses));
3642 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
3643 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
3644 mergestrategies = (int *) palloc(nClauses * sizeof(int));
3645 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
3647 lop = list_head(outerpathkeys);
3648 lip = list_head(innerpathkeys);
3650 foreach(lc, best_path->path_mergeclauses)
3652 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3653 EquivalenceClass *oeclass;
3654 EquivalenceClass *ieclass;
3657 EquivalenceClass *opeclass;
3658 EquivalenceClass *ipeclass;
3661 /* fetch outer/inner eclass from mergeclause */
3662 Assert(IsA(rinfo, RestrictInfo));
3663 if (rinfo->outer_is_left)
3665 oeclass = rinfo->left_ec;
3666 ieclass = rinfo->right_ec;
3670 oeclass = rinfo->right_ec;
3671 ieclass = rinfo->left_ec;
3673 Assert(oeclass != NULL);
3674 Assert(ieclass != NULL);
3677 * For debugging purposes, we check that the eclasses match the paths'
3678 * pathkeys. In typical cases the merge clauses are one-to-one with
3679 * the pathkeys, but when dealing with partially redundant query
3680 * conditions, we might have clauses that re-reference earlier path
3681 * keys. The case that we need to reject is where a pathkey is
3682 * entirely skipped over.
3684 * lop and lip reference the first as-yet-unused pathkey elements;
3685 * it's okay to match them, or any element before them. If they're
3686 * NULL then we have found all pathkey elements to be used.
3690 opathkey = (PathKey *) lfirst(lop);
3691 opeclass = opathkey->pk_eclass;
3692 if (oeclass == opeclass)
3694 /* fast path for typical case */
3699 /* redundant clauses ... must match something before lop */
3700 foreach(l2, outerpathkeys)
3704 opathkey = (PathKey *) lfirst(l2);
3705 opeclass = opathkey->pk_eclass;
3706 if (oeclass == opeclass)
3709 if (oeclass != opeclass)
3710 elog(ERROR, "outer pathkeys do not match mergeclauses");
3715 /* redundant clauses ... must match some already-used pathkey */
3718 foreach(l2, outerpathkeys)
3720 opathkey = (PathKey *) lfirst(l2);
3721 opeclass = opathkey->pk_eclass;
3722 if (oeclass == opeclass)
3726 elog(ERROR, "outer pathkeys do not match mergeclauses");
3731 ipathkey = (PathKey *) lfirst(lip);
3732 ipeclass = ipathkey->pk_eclass;
3733 if (ieclass == ipeclass)
3735 /* fast path for typical case */
3740 /* redundant clauses ... must match something before lip */
3741 foreach(l2, innerpathkeys)
3745 ipathkey = (PathKey *) lfirst(l2);
3746 ipeclass = ipathkey->pk_eclass;
3747 if (ieclass == ipeclass)
3750 if (ieclass != ipeclass)
3751 elog(ERROR, "inner pathkeys do not match mergeclauses");
3756 /* redundant clauses ... must match some already-used pathkey */
3759 foreach(l2, innerpathkeys)
3761 ipathkey = (PathKey *) lfirst(l2);
3762 ipeclass = ipathkey->pk_eclass;
3763 if (ieclass == ipeclass)
3767 elog(ERROR, "inner pathkeys do not match mergeclauses");
3770 /* pathkeys should match each other too (more debugging) */
3771 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
3772 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
3773 opathkey->pk_strategy != ipathkey->pk_strategy ||
3774 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
3775 elog(ERROR, "left and right pathkeys do not match in mergejoin");
3777 /* OK, save info for executor */
3778 mergefamilies[i] = opathkey->pk_opfamily;
3779 mergecollations[i] = opathkey->pk_eclass->ec_collation;
3780 mergestrategies[i] = opathkey->pk_strategy;
3781 mergenullsfirst[i] = opathkey->pk_nulls_first;
3786 * Note: it is not an error if we have additional pathkey elements (i.e.,
3787 * lop or lip isn't NULL here). The input paths might be better-sorted
3788 * than we need for the current mergejoin.
3792 * Now we can build the mergejoin node.
3794 join_plan = make_mergejoin(tlist,
3804 best_path->jpath.jointype);
3806 /* Costs of sort and material steps are included in path cost already */
3807 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
3813 create_hashjoin_plan(PlannerInfo *root,
3814 HashPath *best_path)
3816 HashJoin *join_plan;
3820 List *tlist = build_path_tlist(root, &best_path->jpath.path);
3824 Oid skewTable = InvalidOid;
3825 AttrNumber skewColumn = InvalidAttrNumber;
3826 bool skewInherit = false;
3827 Oid skewColType = InvalidOid;
3828 int32 skewColTypmod = -1;
3831 * HashJoin can project, so we don't have to demand exact tlists from the
3832 * inputs. However, it's best to request a small tlist from the inner
3833 * side, so that we aren't storing more data than necessary. Likewise, if
3834 * we anticipate batching, request a small tlist from the outer side so
3835 * that we don't put extra data in the outer batch files.
3837 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
3838 (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
3840 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
3843 /* Sort join qual clauses into best execution order */
3844 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
3845 /* There's no point in sorting the hash clauses ... */
3847 /* Get the join qual clauses (in plain expression form) */
3848 /* Any pseudoconstant clauses are ignored here */
3849 if (IS_OUTER_JOIN(best_path->jpath.jointype))
3851 extract_actual_join_clauses(joinclauses,
3852 &joinclauses, &otherclauses);
3856 /* We can treat all clauses alike for an inner join */
3857 joinclauses = extract_actual_clauses(joinclauses, false);
3862 * Remove the hashclauses from the list of join qual clauses, leaving the
3863 * list of quals that must be checked as qpquals.
3865 hashclauses = get_actual_clauses(best_path->path_hashclauses);
3866 joinclauses = list_difference(joinclauses, hashclauses);
3869 * Replace any outer-relation variables with nestloop params. There
3870 * should not be any in the hashclauses.
3872 if (best_path->jpath.path.param_info)
3874 joinclauses = (List *)
3875 replace_nestloop_params(root, (Node *) joinclauses);
3876 otherclauses = (List *)
3877 replace_nestloop_params(root, (Node *) otherclauses);
3881 * Rearrange hashclauses, if needed, so that the outer variable is always
3884 hashclauses = get_switched_clauses(best_path->path_hashclauses,
3885 best_path->jpath.outerjoinpath->parent->relids);
3888 * If there is a single join clause and we can identify the outer variable
3889 * as a simple column reference, supply its identity for possible use in
3890 * skew optimization. (Note: in principle we could do skew optimization
3891 * with multiple join clauses, but we'd have to be able to determine the
3892 * most common combinations of outer values, which we don't currently have
3893 * enough stats for.)
3895 if (list_length(hashclauses) == 1)
3897 OpExpr *clause = (OpExpr *) linitial(hashclauses);
3900 Assert(is_opclause(clause));
3901 node = (Node *) linitial(clause->args);
3902 if (IsA(node, RelabelType))
3903 node = (Node *) ((RelabelType *) node)->arg;
3906 Var *var = (Var *) node;
3909 rte = root->simple_rte_array[var->varno];
3910 if (rte->rtekind == RTE_RELATION)
3912 skewTable = rte->relid;
3913 skewColumn = var->varattno;
3914 skewInherit = rte->inh;
3915 skewColType = var->vartype;
3916 skewColTypmod = var->vartypmod;
3922 * Build the hash node and hash join node.
3924 hash_plan = make_hash(inner_plan,
3932 * Set Hash node's startup & total costs equal to total cost of input
3933 * plan; this only affects EXPLAIN display not decisions.
3935 copy_plan_costsize(&hash_plan->plan, inner_plan);
3936 hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
3938 join_plan = make_hashjoin(tlist,
3944 best_path->jpath.jointype);
3946 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
3952 /*****************************************************************************
3954 * SUPPORTING ROUTINES
3956 *****************************************************************************/
3959 * replace_nestloop_params
3960 * Replace outer-relation Vars and PlaceHolderVars in the given expression
3961 * with nestloop Params
3963 * All Vars and PlaceHolderVars belonging to the relation(s) identified by
3964 * root->curOuterRels are replaced by Params, and entries are added to
3965 * root->curOuterParams if not already present.
3968 replace_nestloop_params(PlannerInfo *root, Node *expr)
3970 /* No setup needed for tree walk, so away we go */
3971 return replace_nestloop_params_mutator(expr, root);
3975 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
3981 Var *var = (Var *) node;
3986 /* Upper-level Vars should be long gone at this point */
3987 Assert(var->varlevelsup == 0);
3988 /* If not to be replaced, we can just return the Var unmodified */
3989 if (!bms_is_member(var->varno, root->curOuterRels))
3991 /* Create a Param representing the Var */
3992 param = assign_nestloop_param_var(root, var);
3993 /* Is this param already listed in root->curOuterParams? */
3994 foreach(lc, root->curOuterParams)
3996 nlp = (NestLoopParam *) lfirst(lc);
3997 if (nlp->paramno == param->paramid)
3999 Assert(equal(var, nlp->paramval));
4000 /* Present, so we can just return the Param */
4001 return (Node *) param;
4005 nlp = makeNode(NestLoopParam);
4006 nlp->paramno = param->paramid;
4007 nlp->paramval = var;
4008 root->curOuterParams = lappend(root->curOuterParams, nlp);
4009 /* And return the replacement Param */
4010 return (Node *) param;
4012 if (IsA(node, PlaceHolderVar))
4014 PlaceHolderVar *phv = (PlaceHolderVar *) node;
4019 /* Upper-level PlaceHolderVars should be long gone at this point */
4020 Assert(phv->phlevelsup == 0);
4023 * Check whether we need to replace the PHV. We use bms_overlap as a
4024 * cheap/quick test to see if the PHV might be evaluated in the outer
4025 * rels, and then grab its PlaceHolderInfo to tell for sure.
4027 if (!bms_overlap(phv->phrels, root->curOuterRels) ||
4028 !bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
4029 root->curOuterRels))
4032 * We can't replace the whole PHV, but we might still need to
4033 * replace Vars or PHVs within its expression, in case it ends up
4034 * actually getting evaluated here. (It might get evaluated in
4035 * this plan node, or some child node; in the latter case we don't
4036 * really need to process the expression here, but we haven't got
4037 * enough info to tell if that's the case.) Flat-copy the PHV
4038 * node and then recurse on its expression.
4040 * Note that after doing this, we might have different
4041 * representations of the contents of the same PHV in different
4042 * parts of the plan tree. This is OK because equal() will just
4043 * match on phid/phlevelsup, so setrefs.c will still recognize an
4044 * upper-level reference to a lower-level copy of the same PHV.
4046 PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
4048 memcpy(newphv, phv, sizeof(PlaceHolderVar));
4049 newphv->phexpr = (Expr *)
4050 replace_nestloop_params_mutator((Node *) phv->phexpr,
4052 return (Node *) newphv;
4054 /* Create a Param representing the PlaceHolderVar */
4055 param = assign_nestloop_param_placeholdervar(root, phv);
4056 /* Is this param already listed in root->curOuterParams? */
4057 foreach(lc, root->curOuterParams)
4059 nlp = (NestLoopParam *) lfirst(lc);
4060 if (nlp->paramno == param->paramid)
4062 Assert(equal(phv, nlp->paramval));
4063 /* Present, so we can just return the Param */
4064 return (Node *) param;
4068 nlp = makeNode(NestLoopParam);
4069 nlp->paramno = param->paramid;
4070 nlp->paramval = (Var *) phv;
4071 root->curOuterParams = lappend(root->curOuterParams, nlp);
4072 /* And return the replacement Param */
4073 return (Node *) param;
4075 return expression_tree_mutator(node,
4076 replace_nestloop_params_mutator,
4081 * process_subquery_nestloop_params
4082 * Handle params of a parameterized subquery that need to be fed
4083 * from an outer nestloop.
4085 * Currently, that would be *all* params that a subquery in FROM has demanded
4086 * from the current query level, since they must be LATERAL references.
4088 * The subplan's references to the outer variables are already represented
4089 * as PARAM_EXEC Params, so we need not modify the subplan here. What we
4090 * do need to do is add entries to root->curOuterParams to signal the parent
4091 * nestloop plan node that it must provide these values.
4094 process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
4098 foreach(ppl, subplan_params)
4100 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(ppl);
4102 if (IsA(pitem->item, Var))
4104 Var *var = (Var *) pitem->item;
4108 /* If not from a nestloop outer rel, complain */
4109 if (!bms_is_member(var->varno, root->curOuterRels))
4110 elog(ERROR, "non-LATERAL parameter required by subquery");
4111 /* Is this param already listed in root->curOuterParams? */
4112 foreach(lc, root->curOuterParams)
4114 nlp = (NestLoopParam *) lfirst(lc);
4115 if (nlp->paramno == pitem->paramId)
4117 Assert(equal(var, nlp->paramval));
4118 /* Present, so nothing to do */
4125 nlp = makeNode(NestLoopParam);
4126 nlp->paramno = pitem->paramId;
4127 nlp->paramval = copyObject(var);
4128 root->curOuterParams = lappend(root->curOuterParams, nlp);
4131 else if (IsA(pitem->item, PlaceHolderVar))
4133 PlaceHolderVar *phv = (PlaceHolderVar *) pitem->item;
4137 /* If not from a nestloop outer rel, complain */
4138 if (!bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
4139 root->curOuterRels))
4140 elog(ERROR, "non-LATERAL parameter required by subquery");
4141 /* Is this param already listed in root->curOuterParams? */
4142 foreach(lc, root->curOuterParams)
4144 nlp = (NestLoopParam *) lfirst(lc);
4145 if (nlp->paramno == pitem->paramId)
4147 Assert(equal(phv, nlp->paramval));
4148 /* Present, so nothing to do */
4155 nlp = makeNode(NestLoopParam);
4156 nlp->paramno = pitem->paramId;
4157 nlp->paramval = copyObject(phv);
4158 root->curOuterParams = lappend(root->curOuterParams, nlp);
4162 elog(ERROR, "unexpected type of subquery parameter");
4167 * fix_indexqual_references
4168 * Adjust indexqual clauses to the form the executor's indexqual
4171 * We have four tasks here:
4172 * * Remove RestrictInfo nodes from the input clauses.
4173 * * Replace any outer-relation Var or PHV nodes with nestloop Params.
4174 * (XXX eventually, that responsibility should go elsewhere?)
4175 * * Index keys must be represented by Var nodes with varattno set to the
4176 * index's attribute number, not the attribute number in the original rel.
4177 * * If the index key is on the right, commute the clause to put it on the
4180 * The result is a modified copy of the path's indexquals list --- the
4181 * original is not changed. Note also that the copy shares no substructure
4182 * with the original; this is needed in case there is a subplan in it (we need
4183 * two separate copies of the subplan tree, or things will go awry).
4186 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
4188 IndexOptInfo *index = index_path->indexinfo;
4189 List *fixed_indexquals;
4193 fixed_indexquals = NIL;
4195 forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
4197 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
4198 int indexcol = lfirst_int(lci);
4201 Assert(IsA(rinfo, RestrictInfo));
4204 * Replace any outer-relation variables with nestloop params.
4206 * This also makes a copy of the clause, so it's safe to modify it
4209 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
4211 if (IsA(clause, OpExpr))
4213 OpExpr *op = (OpExpr *) clause;
4215 if (list_length(op->args) != 2)
4216 elog(ERROR, "indexqual clause is not binary opclause");
4219 * Check to see if the indexkey is on the right; if so, commute
4220 * the clause. The indexkey should be the side that refers to
4221 * (only) the base relation.
4223 if (!bms_equal(rinfo->left_relids, index->rel->relids))
4227 * Now replace the indexkey expression with an index Var.
4229 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
4233 else if (IsA(clause, RowCompareExpr))
4235 RowCompareExpr *rc = (RowCompareExpr *) clause;
4243 * Re-discover which index columns are used in the rowcompare.
4245 newrc = adjust_rowcompare_for_index(rc,
4252 * Trouble if adjust_rowcompare_for_index thought the
4253 * RowCompareExpr didn't match the index as-is; the clause should
4254 * have gone through that routine already.
4256 if (newrc != (Expr *) rc)
4257 elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
4260 * Check to see if the indexkey is on the right; if so, commute
4264 CommuteRowCompareExpr(rc);
4267 * Now replace the indexkey expressions with index Vars.
4269 Assert(list_length(rc->largs) == list_length(indexcolnos));
4270 forboth(lca, rc->largs, lcai, indexcolnos)
4272 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
4277 else if (IsA(clause, ScalarArrayOpExpr))
4279 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
4281 /* Never need to commute... */
4283 /* Replace the indexkey expression with an index Var. */
4284 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
4288 else if (IsA(clause, NullTest))
4290 NullTest *nt = (NullTest *) clause;
4292 /* Replace the indexkey expression with an index Var. */
4293 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
4298 elog(ERROR, "unsupported indexqual type: %d",
4299 (int) nodeTag(clause));
4301 fixed_indexquals = lappend(fixed_indexquals, clause);
4304 return fixed_indexquals;
4308 * fix_indexorderby_references
4309 * Adjust indexorderby clauses to the form the executor's index
4312 * This is a simplified version of fix_indexqual_references. The input does
4313 * not have RestrictInfo nodes, and we assume that indxpath.c already
4314 * commuted the clauses to put the index keys on the left. Also, we don't
4315 * bother to support any cases except simple OpExprs, since nothing else
4316 * is allowed for ordering operators.
4319 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
4321 IndexOptInfo *index = index_path->indexinfo;
4322 List *fixed_indexorderbys;
4326 fixed_indexorderbys = NIL;
4328 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
4330 Node *clause = (Node *) lfirst(lcc);
4331 int indexcol = lfirst_int(lci);
4334 * Replace any outer-relation variables with nestloop params.
4336 * This also makes a copy of the clause, so it's safe to modify it
4339 clause = replace_nestloop_params(root, clause);
4341 if (IsA(clause, OpExpr))
4343 OpExpr *op = (OpExpr *) clause;
4345 if (list_length(op->args) != 2)
4346 elog(ERROR, "indexorderby clause is not binary opclause");
4349 * Now replace the indexkey expression with an index Var.
4351 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
4356 elog(ERROR, "unsupported indexorderby type: %d",
4357 (int) nodeTag(clause));
4359 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
4362 return fixed_indexorderbys;
4366 * fix_indexqual_operand
4367 * Convert an indexqual expression to a Var referencing the index column.
4369 * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
4370 * equal to the index's attribute number (index column position).
4372 * Most of the code here is just for sanity cross-checking that the given
4373 * expression actually matches the index column it's claimed to.
4376 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
4380 ListCell *indexpr_item;
4383 * Remove any binary-compatible relabeling of the indexkey
4385 if (IsA(node, RelabelType))
4386 node = (Node *) ((RelabelType *) node)->arg;
4388 Assert(indexcol >= 0 && indexcol < index->ncolumns);
4390 if (index->indexkeys[indexcol] != 0)
4392 /* It's a simple index column */
4393 if (IsA(node, Var) &&
4394 ((Var *) node)->varno == index->rel->relid &&
4395 ((Var *) node)->varattno == index->indexkeys[indexcol])
4397 result = (Var *) copyObject(node);
4398 result->varno = INDEX_VAR;
4399 result->varattno = indexcol + 1;
4400 return (Node *) result;
4403 elog(ERROR, "index key does not match expected index column");
4406 /* It's an index expression, so find and cross-check the expression */
4407 indexpr_item = list_head(index->indexprs);
4408 for (pos = 0; pos < index->ncolumns; pos++)
4410 if (index->indexkeys[pos] == 0)
4412 if (indexpr_item == NULL)
4413 elog(ERROR, "too few entries in indexprs list");
4414 if (pos == indexcol)
4418 indexkey = (Node *) lfirst(indexpr_item);
4419 if (indexkey && IsA(indexkey, RelabelType))
4420 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4421 if (equal(node, indexkey))
4423 result = makeVar(INDEX_VAR, indexcol + 1,
4424 exprType(lfirst(indexpr_item)), -1,
4425 exprCollation(lfirst(indexpr_item)),
4427 return (Node *) result;
4430 elog(ERROR, "index key does not match expected index column");
4432 indexpr_item = lnext(indexpr_item);
4437 elog(ERROR, "index key does not match expected index column");
4438 return NULL; /* keep compiler quiet */
4442 * get_switched_clauses
4443 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
4444 * extract the bare clauses, and rearrange the elements within the
4445 * clauses, if needed, so the outer join variable is on the left and
4446 * the inner is on the right. The original clause data structure is not
4447 * touched; a modified list is returned. We do, however, set the transient
4448 * outer_is_left field in each RestrictInfo to show which side was which.
4451 get_switched_clauses(List *clauses, Relids outerrelids)
4458 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
4459 OpExpr *clause = (OpExpr *) restrictinfo->clause;
4461 Assert(is_opclause(clause));
4462 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
4465 * Duplicate just enough of the structure to allow commuting the
4466 * clause without changing the original list. Could use
4467 * copyObject, but a complete deep copy is overkill.
4469 OpExpr *temp = makeNode(OpExpr);
4471 temp->opno = clause->opno;
4472 temp->opfuncid = InvalidOid;
4473 temp->opresulttype = clause->opresulttype;
4474 temp->opretset = clause->opretset;
4475 temp->opcollid = clause->opcollid;
4476 temp->inputcollid = clause->inputcollid;
4477 temp->args = list_copy(clause->args);
4478 temp->location = clause->location;
4479 /* Commute it --- note this modifies the temp node in-place. */
4480 CommuteOpExpr(temp);
4481 t_list = lappend(t_list, temp);
4482 restrictinfo->outer_is_left = false;
4486 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
4487 t_list = lappend(t_list, clause);
4488 restrictinfo->outer_is_left = true;
4495 * order_qual_clauses
4496 * Given a list of qual clauses that will all be evaluated at the same
4497 * plan node, sort the list into the order we want to check the quals
4500 * Ideally the order should be driven by a combination of execution cost and
4501 * selectivity, but it's not immediately clear how to account for both,
4502 * and given the uncertainty of the estimates the reliability of the decisions
4503 * would be doubtful anyway. So we just order by estimated per-tuple cost,
4504 * being careful not to change the order when (as is often the case) the
4505 * estimates are identical.
4507 * Although this will work on either bare clauses or RestrictInfos, it's
4508 * much faster to apply it to RestrictInfos, since it can re-use cost
4509 * information that is cached in RestrictInfos.
4511 * Note: some callers pass lists that contain entries that will later be
4512 * removed; this is the easiest way to let this routine see RestrictInfos
4513 * instead of bare clauses. It's OK because we only sort by cost, but
4514 * a cost/selectivity combination would likely do the wrong thing.
4517 order_qual_clauses(PlannerInfo *root, List *clauses)
4524 int nitems = list_length(clauses);
4530 /* No need to work hard for 0 or 1 clause */
4535 * Collect the items and costs into an array. This is to avoid repeated
4536 * cost_qual_eval work if the inputs aren't RestrictInfos.
4538 items = (QualItem *) palloc(nitems * sizeof(QualItem));
4540 foreach(lc, clauses)
4542 Node *clause = (Node *) lfirst(lc);
4545 cost_qual_eval_node(&qcost, clause, root);
4546 items[i].clause = clause;
4547 items[i].cost = qcost.per_tuple;
4552 * Sort. We don't use qsort() because it's not guaranteed stable for
4553 * equal keys. The expected number of entries is small enough that a
4554 * simple insertion sort should be good enough.
4556 for (i = 1; i < nitems; i++)
4558 QualItem newitem = items[i];
4561 /* insert newitem into the already-sorted subarray */
4562 for (j = i; j > 0; j--)
4564 if (newitem.cost >= items[j - 1].cost)
4566 items[j] = items[j - 1];
4571 /* Convert back to a list */
4573 for (i = 0; i < nitems; i++)
4574 result = lappend(result, items[i].clause);
4580 * Copy cost and size info from a Path node to the Plan node created from it.
4581 * The executor usually won't use this info, but it's needed by EXPLAIN.
4582 * Also copy the parallel-aware flag, which the executor *will* use.
4585 copy_generic_path_info(Plan *dest, Path *src)
4587 dest->startup_cost = src->startup_cost;
4588 dest->total_cost = src->total_cost;
4589 dest->plan_rows = src->rows;
4590 dest->plan_width = src->pathtarget->width;
4591 dest->parallel_aware = src->parallel_aware;
4595 * Copy cost and size info from a lower plan node to an inserted node.
4596 * (Most callers alter the info after copying it.)
4599 copy_plan_costsize(Plan *dest, Plan *src)
4601 dest->startup_cost = src->startup_cost;
4602 dest->total_cost = src->total_cost;
4603 dest->plan_rows = src->plan_rows;
4604 dest->plan_width = src->plan_width;
4605 /* Assume the inserted node is not parallel-aware. */
4606 dest->parallel_aware = false;
4610 * Some places in this file build Sort nodes that don't have a directly
4611 * corresponding Path node. The cost of the sort is, or should have been,
4612 * included in the cost of the Path node we're working from, but since it's
4613 * not split out, we have to re-figure it using cost_sort(). This is just
4614 * to label the Sort node nicely for EXPLAIN.
4616 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
4619 label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
4621 Plan *lefttree = plan->plan.lefttree;
4622 Path sort_path; /* dummy for result of cost_sort */
4624 cost_sort(&sort_path, root, NIL,
4625 lefttree->total_cost,
4626 lefttree->plan_rows,
4627 lefttree->plan_width,
4631 plan->plan.startup_cost = sort_path.startup_cost;
4632 plan->plan.total_cost = sort_path.total_cost;
4633 plan->plan.plan_rows = lefttree->plan_rows;
4634 plan->plan.plan_width = lefttree->plan_width;
4635 plan->plan.parallel_aware = false;
4639 /*****************************************************************************
4641 * PLAN NODE BUILDING ROUTINES
4643 * In general, these functions are not passed the original Path and therefore
4644 * leave it to the caller to fill in the cost/width fields from the Path,
4645 * typically by calling copy_generic_path_info(). This convention is
4646 * somewhat historical, but it does support a few places above where we build
4647 * a plan node without having an exactly corresponding Path node. Under no
4648 * circumstances should one of these functions do its own cost calculations,
4649 * as that would be redundant with calculations done while building Paths.
4651 *****************************************************************************/
4654 make_seqscan(List *qptlist,
4658 SeqScan *node = makeNode(SeqScan);
4659 Plan *plan = &node->plan;
4661 plan->targetlist = qptlist;
4662 plan->qual = qpqual;
4663 plan->lefttree = NULL;
4664 plan->righttree = NULL;
4665 node->scanrelid = scanrelid;
4671 make_samplescan(List *qptlist,
4674 TableSampleClause *tsc)
4676 SampleScan *node = makeNode(SampleScan);
4677 Plan *plan = &node->scan.plan;
4679 plan->targetlist = qptlist;
4680 plan->qual = qpqual;
4681 plan->lefttree = NULL;
4682 plan->righttree = NULL;
4683 node->scan.scanrelid = scanrelid;
4684 node->tablesample = tsc;
4690 make_indexscan(List *qptlist,
4695 List *indexqualorig,
4697 List *indexorderbyorig,
4698 List *indexorderbyops,
4699 ScanDirection indexscandir)
4701 IndexScan *node = makeNode(IndexScan);
4702 Plan *plan = &node->scan.plan;
4704 plan->targetlist = qptlist;
4705 plan->qual = qpqual;
4706 plan->lefttree = NULL;
4707 plan->righttree = NULL;
4708 node->scan.scanrelid = scanrelid;
4709 node->indexid = indexid;
4710 node->indexqual = indexqual;
4711 node->indexqualorig = indexqualorig;
4712 node->indexorderby = indexorderby;
4713 node->indexorderbyorig = indexorderbyorig;
4714 node->indexorderbyops = indexorderbyops;
4715 node->indexorderdir = indexscandir;
4720 static IndexOnlyScan *
4721 make_indexonlyscan(List *qptlist,
4728 ScanDirection indexscandir)
4730 IndexOnlyScan *node = makeNode(IndexOnlyScan);
4731 Plan *plan = &node->scan.plan;
4733 plan->targetlist = qptlist;
4734 plan->qual = qpqual;
4735 plan->lefttree = NULL;
4736 plan->righttree = NULL;
4737 node->scan.scanrelid = scanrelid;
4738 node->indexid = indexid;
4739 node->indexqual = indexqual;
4740 node->indexorderby = indexorderby;
4741 node->indextlist = indextlist;
4742 node->indexorderdir = indexscandir;
4747 static BitmapIndexScan *
4748 make_bitmap_indexscan(Index scanrelid,
4751 List *indexqualorig)
4753 BitmapIndexScan *node = makeNode(BitmapIndexScan);
4754 Plan *plan = &node->scan.plan;
4756 plan->targetlist = NIL; /* not used */
4757 plan->qual = NIL; /* not used */
4758 plan->lefttree = NULL;
4759 plan->righttree = NULL;
4760 node->scan.scanrelid = scanrelid;
4761 node->indexid = indexid;
4762 node->indexqual = indexqual;
4763 node->indexqualorig = indexqualorig;
4768 static BitmapHeapScan *
4769 make_bitmap_heapscan(List *qptlist,
4772 List *bitmapqualorig,
4775 BitmapHeapScan *node = makeNode(BitmapHeapScan);
4776 Plan *plan = &node->scan.plan;
4778 plan->targetlist = qptlist;
4779 plan->qual = qpqual;
4780 plan->lefttree = lefttree;
4781 plan->righttree = NULL;
4782 node->scan.scanrelid = scanrelid;
4783 node->bitmapqualorig = bitmapqualorig;
4789 make_tidscan(List *qptlist,
4794 TidScan *node = makeNode(TidScan);
4795 Plan *plan = &node->scan.plan;
4797 plan->targetlist = qptlist;
4798 plan->qual = qpqual;
4799 plan->lefttree = NULL;
4800 plan->righttree = NULL;
4801 node->scan.scanrelid = scanrelid;
4802 node->tidquals = tidquals;
4807 static SubqueryScan *
4808 make_subqueryscan(List *qptlist,
4813 SubqueryScan *node = makeNode(SubqueryScan);
4814 Plan *plan = &node->scan.plan;
4816 plan->targetlist = qptlist;
4817 plan->qual = qpqual;
4818 plan->lefttree = NULL;
4819 plan->righttree = NULL;
4820 node->scan.scanrelid = scanrelid;
4821 node->subplan = subplan;
4826 static FunctionScan *
4827 make_functionscan(List *qptlist,
4831 bool funcordinality)
4833 FunctionScan *node = makeNode(FunctionScan);
4834 Plan *plan = &node->scan.plan;
4836 plan->targetlist = qptlist;
4837 plan->qual = qpqual;
4838 plan->lefttree = NULL;
4839 plan->righttree = NULL;
4840 node->scan.scanrelid = scanrelid;
4841 node->functions = functions;
4842 node->funcordinality = funcordinality;
4848 make_valuesscan(List *qptlist,
4853 ValuesScan *node = makeNode(ValuesScan);
4854 Plan *plan = &node->scan.plan;
4856 plan->targetlist = qptlist;
4857 plan->qual = qpqual;
4858 plan->lefttree = NULL;
4859 plan->righttree = NULL;
4860 node->scan.scanrelid = scanrelid;
4861 node->values_lists = values_lists;
4867 make_ctescan(List *qptlist,
4873 CteScan *node = makeNode(CteScan);
4874 Plan *plan = &node->scan.plan;
4876 plan->targetlist = qptlist;
4877 plan->qual = qpqual;
4878 plan->lefttree = NULL;
4879 plan->righttree = NULL;
4880 node->scan.scanrelid = scanrelid;
4881 node->ctePlanId = ctePlanId;
4882 node->cteParam = cteParam;
4887 static WorkTableScan *
4888 make_worktablescan(List *qptlist,
4893 WorkTableScan *node = makeNode(WorkTableScan);
4894 Plan *plan = &node->scan.plan;
4896 plan->targetlist = qptlist;
4897 plan->qual = qpqual;
4898 plan->lefttree = NULL;
4899 plan->righttree = NULL;
4900 node->scan.scanrelid = scanrelid;
4901 node->wtParam = wtParam;
4907 make_foreignscan(List *qptlist,
4912 List *fdw_scan_tlist,
4913 List *fdw_recheck_quals,
4916 ForeignScan *node = makeNode(ForeignScan);
4917 Plan *plan = &node->scan.plan;
4919 /* cost will be filled in by create_foreignscan_plan */
4920 plan->targetlist = qptlist;
4921 plan->qual = qpqual;
4922 plan->lefttree = outer_plan;
4923 plan->righttree = NULL;
4924 node->scan.scanrelid = scanrelid;
4925 node->operation = CMD_SELECT;
4926 /* fs_server will be filled in by create_foreignscan_plan */
4927 node->fs_server = InvalidOid;
4928 node->fdw_exprs = fdw_exprs;
4929 node->fdw_private = fdw_private;
4930 node->fdw_scan_tlist = fdw_scan_tlist;
4931 node->fdw_recheck_quals = fdw_recheck_quals;
4932 /* fs_relids will be filled in by create_foreignscan_plan */
4933 node->fs_relids = NULL;
4934 /* fsSystemCol will be filled in by create_foreignscan_plan */
4935 node->fsSystemCol = false;
4941 make_append(List *appendplans, List *tlist)
4943 Append *node = makeNode(Append);
4944 Plan *plan = &node->plan;
4946 plan->targetlist = tlist;
4948 plan->lefttree = NULL;
4949 plan->righttree = NULL;
4950 node->appendplans = appendplans;
4955 static RecursiveUnion *
4956 make_recursive_union(List *tlist,
4963 RecursiveUnion *node = makeNode(RecursiveUnion);
4964 Plan *plan = &node->plan;
4965 int numCols = list_length(distinctList);
4967 plan->targetlist = tlist;
4969 plan->lefttree = lefttree;
4970 plan->righttree = righttree;
4971 node->wtParam = wtParam;
4974 * convert SortGroupClause list into arrays of attr indexes and equality
4975 * operators, as wanted by executor
4977 node->numCols = numCols;
4981 AttrNumber *dupColIdx;
4985 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4986 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4988 foreach(slitem, distinctList)
4990 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4991 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
4994 dupColIdx[keyno] = tle->resno;
4995 dupOperators[keyno] = sortcl->eqop;
4996 Assert(OidIsValid(dupOperators[keyno]));
4999 node->dupColIdx = dupColIdx;
5000 node->dupOperators = dupOperators;
5002 node->numGroups = numGroups;
5008 make_bitmap_and(List *bitmapplans)
5010 BitmapAnd *node = makeNode(BitmapAnd);
5011 Plan *plan = &node->plan;
5013 plan->targetlist = NIL;
5015 plan->lefttree = NULL;
5016 plan->righttree = NULL;
5017 node->bitmapplans = bitmapplans;
5023 make_bitmap_or(List *bitmapplans)
5025 BitmapOr *node = makeNode(BitmapOr);
5026 Plan *plan = &node->plan;
5028 plan->targetlist = NIL;
5030 plan->lefttree = NULL;
5031 plan->righttree = NULL;
5032 node->bitmapplans = bitmapplans;
5038 make_nestloop(List *tlist,
5046 NestLoop *node = makeNode(NestLoop);
5047 Plan *plan = &node->join.plan;
5049 plan->targetlist = tlist;
5050 plan->qual = otherclauses;
5051 plan->lefttree = lefttree;
5052 plan->righttree = righttree;
5053 node->join.jointype = jointype;
5054 node->join.joinqual = joinclauses;
5055 node->nestParams = nestParams;
5061 make_hashjoin(List *tlist,
5069 HashJoin *node = makeNode(HashJoin);
5070 Plan *plan = &node->join.plan;
5072 plan->targetlist = tlist;
5073 plan->qual = otherclauses;
5074 plan->lefttree = lefttree;
5075 plan->righttree = righttree;
5076 node->hashclauses = hashclauses;
5077 node->join.jointype = jointype;
5078 node->join.joinqual = joinclauses;
5084 make_hash(Plan *lefttree,
5086 AttrNumber skewColumn,
5089 int32 skewColTypmod)
5091 Hash *node = makeNode(Hash);
5092 Plan *plan = &node->plan;
5094 plan->targetlist = lefttree->targetlist;
5096 plan->lefttree = lefttree;
5097 plan->righttree = NULL;
5099 node->skewTable = skewTable;
5100 node->skewColumn = skewColumn;
5101 node->skewInherit = skewInherit;
5102 node->skewColType = skewColType;
5103 node->skewColTypmod = skewColTypmod;
5109 make_mergejoin(List *tlist,
5114 Oid *mergecollations,
5115 int *mergestrategies,
5116 bool *mergenullsfirst,
5121 MergeJoin *node = makeNode(MergeJoin);
5122 Plan *plan = &node->join.plan;
5124 plan->targetlist = tlist;
5125 plan->qual = otherclauses;
5126 plan->lefttree = lefttree;
5127 plan->righttree = righttree;
5128 node->mergeclauses = mergeclauses;
5129 node->mergeFamilies = mergefamilies;
5130 node->mergeCollations = mergecollations;
5131 node->mergeStrategies = mergestrategies;
5132 node->mergeNullsFirst = mergenullsfirst;
5133 node->join.jointype = jointype;
5134 node->join.joinqual = joinclauses;
5140 * make_sort --- basic routine to build a Sort plan node
5142 * Caller must have built the sortColIdx, sortOperators, collations, and
5143 * nullsFirst arrays already.
5146 make_sort(Plan *lefttree, int numCols,
5147 AttrNumber *sortColIdx, Oid *sortOperators,
5148 Oid *collations, bool *nullsFirst)
5150 Sort *node = makeNode(Sort);
5151 Plan *plan = &node->plan;
5153 plan->targetlist = lefttree->targetlist;
5155 plan->lefttree = lefttree;
5156 plan->righttree = NULL;
5157 node->numCols = numCols;
5158 node->sortColIdx = sortColIdx;
5159 node->sortOperators = sortOperators;
5160 node->collations = collations;
5161 node->nullsFirst = nullsFirst;
5167 * prepare_sort_from_pathkeys
5168 * Prepare to sort according to given pathkeys
5170 * This is used to set up for both Sort and MergeAppend nodes. It calculates
5171 * the executor's representation of the sort key information, and adjusts the
5172 * plan targetlist if needed to add resjunk sort columns.
5175 * 'lefttree' is the plan node which yields input tuples
5176 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
5177 * 'relids' identifies the child relation being sorted, if any
5178 * 'reqColIdx' is NULL or an array of required sort key column numbers
5179 * 'adjust_tlist_in_place' is TRUE if lefttree must be modified in-place
5181 * We must convert the pathkey information into arrays of sort key column
5182 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
5183 * which is the representation the executor wants. These are returned into
5184 * the output parameters *p_numsortkeys etc.
5186 * When looking for matches to an EquivalenceClass's members, we will only
5187 * consider child EC members if they match 'relids'. This protects against
5188 * possible incorrect matches to child expressions that contain no Vars.
5190 * If reqColIdx isn't NULL then it contains sort key column numbers that
5191 * we should match. This is used when making child plans for a MergeAppend;
5192 * it's an error if we can't match the columns.
5194 * If the pathkeys include expressions that aren't simple Vars, we will
5195 * usually need to add resjunk items to the input plan's targetlist to
5196 * compute these expressions, since the Sort/MergeAppend node itself won't
5197 * do any such calculations. If the input plan type isn't one that can do
5198 * projections, this means adding a Result node just to do the projection.
5199 * However, the caller can pass adjust_tlist_in_place = TRUE to force the
5200 * lefttree tlist to be modified in-place regardless of whether the node type
5201 * can project --- we use this for fixing the tlist of MergeAppend itself.
5203 * Returns the node which is to be the input to the Sort (either lefttree,
5204 * or a Result stacked atop lefttree).
5207 prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
5209 const AttrNumber *reqColIdx,
5210 bool adjust_tlist_in_place,
5212 AttrNumber **p_sortColIdx,
5213 Oid **p_sortOperators,
5215 bool **p_nullsFirst)
5217 List *tlist = lefttree->targetlist;
5220 AttrNumber *sortColIdx;
5226 * We will need at most list_length(pathkeys) sort columns; possibly less
5228 numsortkeys = list_length(pathkeys);
5229 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5230 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5231 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5232 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5236 foreach(i, pathkeys)
5238 PathKey *pathkey = (PathKey *) lfirst(i);
5239 EquivalenceClass *ec = pathkey->pk_eclass;
5240 EquivalenceMember *em;
5241 TargetEntry *tle = NULL;
5242 Oid pk_datatype = InvalidOid;
5246 if (ec->ec_has_volatile)
5249 * If the pathkey's EquivalenceClass is volatile, then it must
5250 * have come from an ORDER BY clause, and we have to match it to
5251 * that same targetlist entry.
5253 if (ec->ec_sortref == 0) /* can't happen */
5254 elog(ERROR, "volatile EquivalenceClass has no sortref");
5255 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
5257 Assert(list_length(ec->ec_members) == 1);
5258 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
5260 else if (reqColIdx != NULL)
5263 * If we are given a sort column number to match, only consider
5264 * the single TLE at that position. It's possible that there is
5265 * no such TLE, in which case fall through and generate a resjunk
5266 * targetentry (we assume this must have happened in the parent
5267 * plan as well). If there is a TLE but it doesn't match the
5268 * pathkey's EC, we do the same, which is probably the wrong thing
5269 * but we'll leave it to caller to complain about the mismatch.
5271 tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
5274 em = find_ec_member_for_tle(ec, tle, relids);
5277 /* found expr at right place in tlist */
5278 pk_datatype = em->em_datatype;
5287 * Otherwise, we can sort by any non-constant expression listed in
5288 * the pathkey's EquivalenceClass. For now, we take the first
5289 * tlist item found in the EC. If there's no match, we'll generate
5290 * a resjunk entry using the first EC member that is an expression
5291 * in the input's vars. (The non-const restriction only matters
5292 * if the EC is below_outer_join; but if it isn't, it won't
5293 * contain consts anyway, else we'd have discarded the pathkey as
5296 * XXX if we have a choice, is there any way of figuring out which
5297 * might be cheapest to execute? (For example, int4lt is likely
5298 * much cheaper to execute than numericlt, but both might appear
5299 * in the same equivalence class...) Not clear that we ever will
5300 * have an interesting choice in practice, so it may not matter.
5304 tle = (TargetEntry *) lfirst(j);
5305 em = find_ec_member_for_tle(ec, tle, relids);
5308 /* found expr already in tlist */
5309 pk_datatype = em->em_datatype;
5319 * No matching tlist item; look for a computable expression. Note
5320 * that we treat Aggrefs as if they were variables; this is
5321 * necessary when attempting to sort the output from an Agg node
5322 * for use in a WindowFunc (since grouping_planner will have
5323 * treated the Aggrefs as variables, too). Likewise, if we find a
5324 * WindowFunc in a sort expression, treat it as a variable.
5326 Expr *sortexpr = NULL;
5328 foreach(j, ec->ec_members)
5330 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
5335 * We shouldn't be trying to sort by an equivalence class that
5336 * contains a constant, so no need to consider such cases any
5339 if (em->em_is_const)
5343 * Ignore child members unless they match the rel being
5346 if (em->em_is_child &&
5347 !bms_equal(em->em_relids, relids))
5350 sortexpr = em->em_expr;
5351 exprvars = pull_var_clause((Node *) sortexpr,
5352 PVC_INCLUDE_AGGREGATES |
5353 PVC_INCLUDE_WINDOWFUNCS |
5354 PVC_INCLUDE_PLACEHOLDERS);
5355 foreach(k, exprvars)
5357 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
5360 list_free(exprvars);
5363 pk_datatype = em->em_datatype;
5364 break; /* found usable expression */
5368 elog(ERROR, "could not find pathkey item to sort");
5371 * Do we need to insert a Result node?
5373 if (!adjust_tlist_in_place &&
5374 !is_projection_capable_plan(lefttree))
5376 /* copy needed so we don't modify input's tlist below */
5377 tlist = copyObject(tlist);
5378 lefttree = inject_projection_plan(lefttree, tlist);
5381 /* Don't bother testing is_projection_capable_plan again */
5382 adjust_tlist_in_place = true;
5385 * Add resjunk entry to input's tlist
5387 tle = makeTargetEntry(sortexpr,
5388 list_length(tlist) + 1,
5391 tlist = lappend(tlist, tle);
5392 lefttree->targetlist = tlist; /* just in case NIL before */
5396 * Look up the correct sort operator from the PathKey's slightly
5397 * abstracted representation.
5399 sortop = get_opfamily_member(pathkey->pk_opfamily,
5402 pathkey->pk_strategy);
5403 if (!OidIsValid(sortop)) /* should not happen */
5404 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
5405 pathkey->pk_strategy, pk_datatype, pk_datatype,
5406 pathkey->pk_opfamily);
5408 /* Add the column to the sort arrays */
5409 sortColIdx[numsortkeys] = tle->resno;
5410 sortOperators[numsortkeys] = sortop;
5411 collations[numsortkeys] = ec->ec_collation;
5412 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
5416 /* Return results */
5417 *p_numsortkeys = numsortkeys;
5418 *p_sortColIdx = sortColIdx;
5419 *p_sortOperators = sortOperators;
5420 *p_collations = collations;
5421 *p_nullsFirst = nullsFirst;
5427 * find_ec_member_for_tle
5428 * Locate an EquivalenceClass member matching the given TLE, if any
5430 * Child EC members are ignored unless they match 'relids'.
5432 static EquivalenceMember *
5433 find_ec_member_for_tle(EquivalenceClass *ec,
5440 /* We ignore binary-compatible relabeling on both ends */
5442 while (tlexpr && IsA(tlexpr, RelabelType))
5443 tlexpr = ((RelabelType *) tlexpr)->arg;
5445 foreach(lc, ec->ec_members)
5447 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
5451 * We shouldn't be trying to sort by an equivalence class that
5452 * contains a constant, so no need to consider such cases any further.
5454 if (em->em_is_const)
5458 * Ignore child members unless they match the rel being sorted.
5460 if (em->em_is_child &&
5461 !bms_equal(em->em_relids, relids))
5464 /* Match if same expression (after stripping relabel) */
5465 emexpr = em->em_expr;
5466 while (emexpr && IsA(emexpr, RelabelType))
5467 emexpr = ((RelabelType *) emexpr)->arg;
5469 if (equal(emexpr, tlexpr))
5477 * make_sort_from_pathkeys
5478 * Create sort plan to sort according to given pathkeys
5480 * 'lefttree' is the node which yields input tuples
5481 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
5484 make_sort_from_pathkeys(Plan *lefttree, List *pathkeys)
5487 AttrNumber *sortColIdx;
5492 /* Compute sort column info, and adjust lefttree as needed */
5493 lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
5503 /* Now build the Sort node */
5504 return make_sort(lefttree, numsortkeys,
5505 sortColIdx, sortOperators,
5506 collations, nullsFirst);
5510 * make_sort_from_sortclauses
5511 * Create sort plan to sort according to given sortclauses
5513 * 'sortcls' is a list of SortGroupClauses
5514 * 'lefttree' is the node which yields input tuples
5517 make_sort_from_sortclauses(List *sortcls, Plan *lefttree)
5519 List *sub_tlist = lefttree->targetlist;
5522 AttrNumber *sortColIdx;
5527 /* Convert list-ish representation to arrays wanted by executor */
5528 numsortkeys = list_length(sortcls);
5529 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5530 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5531 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5532 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5537 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
5538 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
5540 sortColIdx[numsortkeys] = tle->resno;
5541 sortOperators[numsortkeys] = sortcl->sortop;
5542 collations[numsortkeys] = exprCollation((Node *) tle->expr);
5543 nullsFirst[numsortkeys] = sortcl->nulls_first;
5547 return make_sort(lefttree, numsortkeys,
5548 sortColIdx, sortOperators,
5549 collations, nullsFirst);
5553 * make_sort_from_groupcols
5554 * Create sort plan to sort based on grouping columns
5556 * 'groupcls' is the list of SortGroupClauses
5557 * 'grpColIdx' gives the column numbers to use
5559 * This might look like it could be merged with make_sort_from_sortclauses,
5560 * but presently we *must* use the grpColIdx[] array to locate sort columns,
5561 * because the child plan's tlist is not marked with ressortgroupref info
5562 * appropriate to the grouping node. So, only the sort ordering info
5563 * is used from the SortGroupClause entries.
5566 make_sort_from_groupcols(List *groupcls,
5567 AttrNumber *grpColIdx,
5570 List *sub_tlist = lefttree->targetlist;
5573 AttrNumber *sortColIdx;
5578 /* Convert list-ish representation to arrays wanted by executor */
5579 numsortkeys = list_length(groupcls);
5580 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5581 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5582 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5583 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5586 foreach(l, groupcls)
5588 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
5589 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
5592 elog(ERROR, "could not retrieve tle for sort-from-groupcols");
5594 sortColIdx[numsortkeys] = tle->resno;
5595 sortOperators[numsortkeys] = grpcl->sortop;
5596 collations[numsortkeys] = exprCollation((Node *) tle->expr);
5597 nullsFirst[numsortkeys] = grpcl->nulls_first;
5601 return make_sort(lefttree, numsortkeys,
5602 sortColIdx, sortOperators,
5603 collations, nullsFirst);
5607 make_material(Plan *lefttree)
5609 Material *node = makeNode(Material);
5610 Plan *plan = &node->plan;
5612 plan->targetlist = lefttree->targetlist;
5614 plan->lefttree = lefttree;
5615 plan->righttree = NULL;
5621 * materialize_finished_plan: stick a Material node atop a completed plan
5623 * There are a couple of places where we want to attach a Material node
5624 * after completion of create_plan(), without any MaterialPath path.
5625 * Those places should probably be refactored someday to do this on the
5626 * Path representation, but it's not worth the trouble yet.
5629 materialize_finished_plan(Plan *subplan)
5632 Path matpath; /* dummy for result of cost_material */
5634 matplan = (Plan *) make_material(subplan);
5637 cost_material(&matpath,
5638 subplan->startup_cost,
5639 subplan->total_cost,
5641 subplan->plan_width);
5642 matplan->startup_cost = matpath.startup_cost;
5643 matplan->total_cost = matpath.total_cost;
5644 matplan->plan_rows = subplan->plan_rows;
5645 matplan->plan_width = subplan->plan_width;
5646 matplan->parallel_aware = false;
5652 make_agg(List *tlist, List *qual,
5653 AggStrategy aggstrategy, AggSplit aggsplit,
5654 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
5655 List *groupingSets, List *chain,
5656 double dNumGroups, Plan *lefttree)
5658 Agg *node = makeNode(Agg);
5659 Plan *plan = &node->plan;
5662 /* Reduce to long, but 'ware overflow! */
5663 numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
5665 node->aggstrategy = aggstrategy;
5666 node->aggsplit = aggsplit;
5667 node->numCols = numGroupCols;
5668 node->grpColIdx = grpColIdx;
5669 node->grpOperators = grpOperators;
5670 node->numGroups = numGroups;
5671 node->groupingSets = groupingSets;
5672 node->chain = chain;
5675 plan->targetlist = tlist;
5676 plan->lefttree = lefttree;
5677 plan->righttree = NULL;
5683 make_windowagg(List *tlist, Index winref,
5684 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
5685 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
5686 int frameOptions, Node *startOffset, Node *endOffset,
5689 WindowAgg *node = makeNode(WindowAgg);
5690 Plan *plan = &node->plan;
5692 node->winref = winref;
5693 node->partNumCols = partNumCols;
5694 node->partColIdx = partColIdx;
5695 node->partOperators = partOperators;
5696 node->ordNumCols = ordNumCols;
5697 node->ordColIdx = ordColIdx;
5698 node->ordOperators = ordOperators;
5699 node->frameOptions = frameOptions;
5700 node->startOffset = startOffset;
5701 node->endOffset = endOffset;
5703 plan->targetlist = tlist;
5704 plan->lefttree = lefttree;
5705 plan->righttree = NULL;
5706 /* WindowAgg nodes never have a qual clause */
5713 make_group(List *tlist,
5716 AttrNumber *grpColIdx,
5720 Group *node = makeNode(Group);
5721 Plan *plan = &node->plan;
5723 node->numCols = numGroupCols;
5724 node->grpColIdx = grpColIdx;
5725 node->grpOperators = grpOperators;
5728 plan->targetlist = tlist;
5729 plan->lefttree = lefttree;
5730 plan->righttree = NULL;
5736 * distinctList is a list of SortGroupClauses, identifying the targetlist items
5737 * that should be considered by the Unique filter. The input path must
5738 * already be sorted accordingly.
5741 make_unique_from_sortclauses(Plan *lefttree, List *distinctList)
5743 Unique *node = makeNode(Unique);
5744 Plan *plan = &node->plan;
5745 int numCols = list_length(distinctList);
5747 AttrNumber *uniqColIdx;
5751 plan->targetlist = lefttree->targetlist;
5753 plan->lefttree = lefttree;
5754 plan->righttree = NULL;
5757 * convert SortGroupClause list into arrays of attr indexes and equality
5758 * operators, as wanted by executor
5760 Assert(numCols > 0);
5761 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
5762 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
5764 foreach(slitem, distinctList)
5766 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
5767 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
5769 uniqColIdx[keyno] = tle->resno;
5770 uniqOperators[keyno] = sortcl->eqop;
5771 Assert(OidIsValid(uniqOperators[keyno]));
5775 node->numCols = numCols;
5776 node->uniqColIdx = uniqColIdx;
5777 node->uniqOperators = uniqOperators;
5783 * as above, but use pathkeys to identify the sort columns and semantics
5786 make_unique_from_pathkeys(Plan *lefttree, List *pathkeys, int numCols)
5788 Unique *node = makeNode(Unique);
5789 Plan *plan = &node->plan;
5791 AttrNumber *uniqColIdx;
5795 plan->targetlist = lefttree->targetlist;
5797 plan->lefttree = lefttree;
5798 plan->righttree = NULL;
5801 * Convert pathkeys list into arrays of attr indexes and equality
5802 * operators, as wanted by executor. This has a lot in common with
5803 * prepare_sort_from_pathkeys ... maybe unify sometime?
5805 Assert(numCols >= 0 && numCols <= list_length(pathkeys));
5806 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
5807 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
5809 foreach(lc, pathkeys)
5811 PathKey *pathkey = (PathKey *) lfirst(lc);
5812 EquivalenceClass *ec = pathkey->pk_eclass;
5813 EquivalenceMember *em;
5814 TargetEntry *tle = NULL;
5815 Oid pk_datatype = InvalidOid;
5819 /* Ignore pathkeys beyond the specified number of columns */
5820 if (keyno >= numCols)
5823 if (ec->ec_has_volatile)
5826 * If the pathkey's EquivalenceClass is volatile, then it must
5827 * have come from an ORDER BY clause, and we have to match it to
5828 * that same targetlist entry.
5830 if (ec->ec_sortref == 0) /* can't happen */
5831 elog(ERROR, "volatile EquivalenceClass has no sortref");
5832 tle = get_sortgroupref_tle(ec->ec_sortref, plan->targetlist);
5834 Assert(list_length(ec->ec_members) == 1);
5835 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
5840 * Otherwise, we can use any non-constant expression listed in the
5841 * pathkey's EquivalenceClass. For now, we take the first tlist
5842 * item found in the EC.
5844 foreach(j, plan->targetlist)
5846 tle = (TargetEntry *) lfirst(j);
5847 em = find_ec_member_for_tle(ec, tle, NULL);
5850 /* found expr already in tlist */
5851 pk_datatype = em->em_datatype;
5859 elog(ERROR, "could not find pathkey item to sort");
5862 * Look up the correct equality operator from the PathKey's slightly
5863 * abstracted representation.
5865 eqop = get_opfamily_member(pathkey->pk_opfamily,
5868 BTEqualStrategyNumber);
5869 if (!OidIsValid(eqop)) /* should not happen */
5870 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
5871 BTEqualStrategyNumber, pk_datatype, pk_datatype,
5872 pathkey->pk_opfamily);
5874 uniqColIdx[keyno] = tle->resno;
5875 uniqOperators[keyno] = eqop;
5880 node->numCols = numCols;
5881 node->uniqColIdx = uniqColIdx;
5882 node->uniqOperators = uniqOperators;
5888 make_gather(List *qptlist,
5894 Gather *node = makeNode(Gather);
5895 Plan *plan = &node->plan;
5897 plan->targetlist = qptlist;
5898 plan->qual = qpqual;
5899 plan->lefttree = subplan;
5900 plan->righttree = NULL;
5901 node->num_workers = nworkers;
5902 node->single_copy = single_copy;
5903 node->invisible = false;
5909 * distinctList is a list of SortGroupClauses, identifying the targetlist
5910 * items that should be considered by the SetOp filter. The input path must
5911 * already be sorted accordingly.
5914 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
5915 List *distinctList, AttrNumber flagColIdx, int firstFlag,
5918 SetOp *node = makeNode(SetOp);
5919 Plan *plan = &node->plan;
5920 int numCols = list_length(distinctList);
5922 AttrNumber *dupColIdx;
5926 plan->targetlist = lefttree->targetlist;
5928 plan->lefttree = lefttree;
5929 plan->righttree = NULL;
5932 * convert SortGroupClause list into arrays of attr indexes and equality
5933 * operators, as wanted by executor
5935 Assert(numCols > 0);
5936 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
5937 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
5939 foreach(slitem, distinctList)
5941 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
5942 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
5944 dupColIdx[keyno] = tle->resno;
5945 dupOperators[keyno] = sortcl->eqop;
5946 Assert(OidIsValid(dupOperators[keyno]));
5951 node->strategy = strategy;
5952 node->numCols = numCols;
5953 node->dupColIdx = dupColIdx;
5954 node->dupOperators = dupOperators;
5955 node->flagColIdx = flagColIdx;
5956 node->firstFlag = firstFlag;
5957 node->numGroups = numGroups;
5964 * Build a LockRows plan node
5967 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
5969 LockRows *node = makeNode(LockRows);
5970 Plan *plan = &node->plan;
5972 plan->targetlist = lefttree->targetlist;
5974 plan->lefttree = lefttree;
5975 plan->righttree = NULL;
5977 node->rowMarks = rowMarks;
5978 node->epqParam = epqParam;
5985 * Build a Limit plan node
5988 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
5990 Limit *node = makeNode(Limit);
5991 Plan *plan = &node->plan;
5993 plan->targetlist = lefttree->targetlist;
5995 plan->lefttree = lefttree;
5996 plan->righttree = NULL;
5998 node->limitOffset = limitOffset;
5999 node->limitCount = limitCount;
6006 * Build a Result plan node
6009 make_result(List *tlist,
6010 Node *resconstantqual,
6013 Result *node = makeNode(Result);
6014 Plan *plan = &node->plan;
6016 plan->targetlist = tlist;
6018 plan->lefttree = subplan;
6019 plan->righttree = NULL;
6020 node->resconstantqual = resconstantqual;
6027 * Build a ModifyTable plan node
6029 static ModifyTable *
6030 make_modifytable(PlannerInfo *root,
6031 CmdType operation, bool canSetTag,
6032 Index nominalRelation,
6033 List *resultRelations, List *subplans,
6034 List *withCheckOptionLists, List *returningLists,
6035 List *rowMarks, OnConflictExpr *onconflict, int epqParam)
6037 ModifyTable *node = makeNode(ModifyTable);
6038 List *fdw_private_list;
6039 Bitmapset *direct_modify_plans;
6043 Assert(list_length(resultRelations) == list_length(subplans));
6044 Assert(withCheckOptionLists == NIL ||
6045 list_length(resultRelations) == list_length(withCheckOptionLists));
6046 Assert(returningLists == NIL ||
6047 list_length(resultRelations) == list_length(returningLists));
6049 node->plan.lefttree = NULL;
6050 node->plan.righttree = NULL;
6051 node->plan.qual = NIL;
6052 /* setrefs.c will fill in the targetlist, if needed */
6053 node->plan.targetlist = NIL;
6055 node->operation = operation;
6056 node->canSetTag = canSetTag;
6057 node->nominalRelation = nominalRelation;
6058 node->resultRelations = resultRelations;
6059 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
6060 node->plans = subplans;
6063 node->onConflictAction = ONCONFLICT_NONE;
6064 node->onConflictSet = NIL;
6065 node->onConflictWhere = NULL;
6066 node->arbiterIndexes = NIL;
6067 node->exclRelRTI = 0;
6068 node->exclRelTlist = NIL;
6072 node->onConflictAction = onconflict->action;
6073 node->onConflictSet = onconflict->onConflictSet;
6074 node->onConflictWhere = onconflict->onConflictWhere;
6077 * If a set of unique index inference elements was provided (an
6078 * INSERT...ON CONFLICT "inference specification"), then infer
6079 * appropriate unique indexes (or throw an error if none are
6082 node->arbiterIndexes = infer_arbiter_indexes(root);
6084 node->exclRelRTI = onconflict->exclRelIndex;
6085 node->exclRelTlist = onconflict->exclRelTlist;
6087 node->withCheckOptionLists = withCheckOptionLists;
6088 node->returningLists = returningLists;
6089 node->rowMarks = rowMarks;
6090 node->epqParam = epqParam;
6093 * For each result relation that is a foreign table, allow the FDW to
6094 * construct private plan data, and accumulate it all into a list.
6096 fdw_private_list = NIL;
6097 direct_modify_plans = NULL;
6099 foreach(lc, resultRelations)
6101 Index rti = lfirst_int(lc);
6102 FdwRoutine *fdwroutine;
6107 * If possible, we want to get the FdwRoutine from our RelOptInfo for
6108 * the table. But sometimes we don't have a RelOptInfo and must get
6109 * it the hard way. (In INSERT, the target relation is not scanned,
6110 * so it's not a baserel; and there are also corner cases for
6111 * updatable views where the target rel isn't a baserel.)
6113 if (rti < root->simple_rel_array_size &&
6114 root->simple_rel_array[rti] != NULL)
6116 RelOptInfo *resultRel = root->simple_rel_array[rti];
6118 fdwroutine = resultRel->fdwroutine;
6122 RangeTblEntry *rte = planner_rt_fetch(rti, root);
6124 Assert(rte->rtekind == RTE_RELATION);
6125 if (rte->relkind == RELKIND_FOREIGN_TABLE)
6126 fdwroutine = GetFdwRoutineByRelId(rte->relid);
6132 * If the target foreign table has any row-level triggers, we can't
6133 * modify the foreign table directly.
6135 direct_modify = false;
6136 if (fdwroutine != NULL &&
6137 fdwroutine->PlanDirectModify != NULL &&
6138 fdwroutine->BeginDirectModify != NULL &&
6139 fdwroutine->IterateDirectModify != NULL &&
6140 fdwroutine->EndDirectModify != NULL &&
6141 !has_row_triggers(root, rti, operation))
6142 direct_modify = fdwroutine->PlanDirectModify(root, node, rti, i);
6144 direct_modify_plans = bms_add_member(direct_modify_plans, i);
6146 if (!direct_modify &&
6147 fdwroutine != NULL &&
6148 fdwroutine->PlanForeignModify != NULL)
6149 fdw_private = fdwroutine->PlanForeignModify(root, node, rti, i);
6152 fdw_private_list = lappend(fdw_private_list, fdw_private);
6155 node->fdwPrivLists = fdw_private_list;
6156 node->fdwDirectModifyPlans = direct_modify_plans;
6162 * is_projection_capable_path
6163 * Check whether a given Path node is able to do projection.
6166 is_projection_capable_path(Path *path)
6168 /* Most plan types can project, so just list the ones that can't */
6169 switch (path->pathtype)
6180 case T_RecursiveUnion:
6185 * Append can't project, but if it's being used to represent a
6186 * dummy path, claim that it can project. This prevents us from
6187 * converting a rel from dummy to non-dummy status by applying a
6188 * projection to its dummy path.
6190 return IS_DUMMY_PATH(path);
6198 * is_projection_capable_plan
6199 * Check whether a given Plan node is able to do projection.
6202 is_projection_capable_plan(Plan *plan)
6204 /* Most plan types can project, so just list the ones that can't */
6205 switch (nodeTag(plan))
6217 case T_RecursiveUnion: