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-2012, 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/skey.h"
23 #include "foreign/fdwapi.h"
24 #include "miscadmin.h"
25 #include "nodes/makefuncs.h"
26 #include "nodes/nodeFuncs.h"
27 #include "optimizer/clauses.h"
28 #include "optimizer/cost.h"
29 #include "optimizer/paths.h"
30 #include "optimizer/placeholder.h"
31 #include "optimizer/plancat.h"
32 #include "optimizer/planmain.h"
33 #include "optimizer/planner.h"
34 #include "optimizer/predtest.h"
35 #include "optimizer/restrictinfo.h"
36 #include "optimizer/subselect.h"
37 #include "optimizer/tlist.h"
38 #include "optimizer/var.h"
39 #include "parser/parse_clause.h"
40 #include "parser/parsetree.h"
41 #include "utils/lsyscache.h"
44 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path);
45 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
46 static List *build_relation_tlist(RelOptInfo *rel);
47 static bool use_physical_tlist(PlannerInfo *root, RelOptInfo *rel);
48 static void disuse_physical_tlist(Plan *plan, Path *path);
49 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
50 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
51 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
52 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
53 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
54 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
55 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
56 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
57 List *tlist, List *scan_clauses);
58 static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
59 List *tlist, List *scan_clauses, bool indexonly);
60 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
61 BitmapHeapPath *best_path,
62 List *tlist, List *scan_clauses);
63 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
64 List **qual, List **indexqual, List **indexECs);
65 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
66 List *tlist, List *scan_clauses);
67 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
68 List *tlist, List *scan_clauses);
69 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
70 List *tlist, List *scan_clauses);
71 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
72 List *tlist, List *scan_clauses);
73 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
74 List *tlist, List *scan_clauses);
75 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
76 List *tlist, List *scan_clauses);
77 static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
78 List *tlist, List *scan_clauses);
79 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
80 Plan *outer_plan, Plan *inner_plan);
81 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
82 Plan *outer_plan, Plan *inner_plan);
83 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
84 Plan *outer_plan, Plan *inner_plan);
85 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
86 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
87 static void identify_nestloop_extparams(PlannerInfo *root, Plan *subplan);
88 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path);
89 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
90 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
91 static List *get_switched_clauses(List *clauses, Relids outerrelids);
92 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
93 static void copy_path_costsize(Plan *dest, Path *src);
94 static void copy_plan_costsize(Plan *dest, Plan *src);
95 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
96 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
97 Oid indexid, List *indexqual, List *indexqualorig,
98 List *indexorderby, List *indexorderbyorig,
99 ScanDirection indexscandir);
100 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
101 Index scanrelid, Oid indexid,
102 List *indexqual, List *indexorderby,
104 ScanDirection indexscandir);
105 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
107 List *indexqualorig);
108 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
111 List *bitmapqualorig,
113 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
115 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
116 Index scanrelid, Node *funcexpr, List *funccolnames,
117 List *funccoltypes, List *funccoltypmods,
118 List *funccolcollations);
119 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
120 Index scanrelid, List *values_lists);
121 static CteScan *make_ctescan(List *qptlist, List *qpqual,
122 Index scanrelid, int ctePlanId, int cteParam);
123 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
124 Index scanrelid, int wtParam);
125 static BitmapAnd *make_bitmap_and(List *bitmapplans);
126 static BitmapOr *make_bitmap_or(List *bitmapplans);
127 static NestLoop *make_nestloop(List *tlist,
128 List *joinclauses, List *otherclauses, List *nestParams,
129 Plan *lefttree, Plan *righttree,
131 static HashJoin *make_hashjoin(List *tlist,
132 List *joinclauses, List *otherclauses,
134 Plan *lefttree, Plan *righttree,
136 static Hash *make_hash(Plan *lefttree,
138 AttrNumber skewColumn,
141 int32 skewColTypmod);
142 static MergeJoin *make_mergejoin(List *tlist,
143 List *joinclauses, List *otherclauses,
146 Oid *mergecollations,
147 int *mergestrategies,
148 bool *mergenullsfirst,
149 Plan *lefttree, Plan *righttree,
151 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
152 AttrNumber *sortColIdx, Oid *sortOperators,
153 Oid *collations, bool *nullsFirst,
154 double limit_tuples);
155 static Plan *prepare_sort_from_pathkeys(PlannerInfo *root,
156 Plan *lefttree, List *pathkeys,
158 const AttrNumber *reqColIdx,
159 bool adjust_tlist_in_place,
161 AttrNumber **p_sortColIdx,
162 Oid **p_sortOperators,
164 bool **p_nullsFirst);
165 static EquivalenceMember *find_ec_member_for_tle(EquivalenceClass *ec,
168 static Material *make_material(Plan *lefttree);
173 * Creates the access plan for a query by recursively processing the
174 * desired tree of pathnodes, starting at the node 'best_path'. For
175 * every pathnode found, we create a corresponding plan node containing
176 * appropriate id, target list, and qualification information.
178 * The tlists and quals in the plan tree are still in planner format,
179 * ie, Vars still correspond to the parser's numbering. This will be
180 * fixed later by setrefs.c.
182 * best_path is the best access path
184 * Returns a Plan tree.
187 create_plan(PlannerInfo *root, Path *best_path)
191 /* Initialize this module's private workspace in PlannerInfo */
192 root->curOuterRels = NULL;
193 root->curOuterParams = NIL;
195 /* Recursively process the path tree */
196 plan = create_plan_recurse(root, best_path);
198 /* Check we successfully assigned all NestLoopParams to plan nodes */
199 if (root->curOuterParams != NIL)
200 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
206 * create_plan_recurse
207 * Recursive guts of create_plan().
210 create_plan_recurse(PlannerInfo *root, Path *best_path)
214 switch (best_path->pathtype)
218 case T_IndexOnlyScan:
219 case T_BitmapHeapScan:
225 case T_WorkTableScan:
227 plan = create_scan_plan(root, best_path);
232 plan = create_join_plan(root,
233 (JoinPath *) best_path);
236 plan = create_append_plan(root,
237 (AppendPath *) best_path);
240 plan = create_merge_append_plan(root,
241 (MergeAppendPath *) best_path);
244 plan = (Plan *) create_result_plan(root,
245 (ResultPath *) best_path);
248 plan = (Plan *) create_material_plan(root,
249 (MaterialPath *) best_path);
252 plan = create_unique_plan(root,
253 (UniquePath *) best_path);
256 elog(ERROR, "unrecognized node type: %d",
257 (int) best_path->pathtype);
258 plan = NULL; /* keep compiler quiet */
267 * Create a scan plan for the parent relation of 'best_path'.
270 create_scan_plan(PlannerInfo *root, Path *best_path)
272 RelOptInfo *rel = best_path->parent;
278 * For table scans, rather than using the relation targetlist (which is
279 * only those Vars actually needed by the query), we prefer to generate a
280 * tlist containing all Vars in order. This will allow the executor to
281 * optimize away projection of the table tuples, if possible. (Note that
282 * planner.c may replace the tlist we generate here, forcing projection to
285 if (use_physical_tlist(root, rel))
287 if (best_path->pathtype == T_IndexOnlyScan)
289 /* For index-only scan, the preferred tlist is the index's */
290 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
294 tlist = build_physical_tlist(root, rel);
295 /* if fail because of dropped cols, use regular method */
297 tlist = build_relation_tlist(rel);
302 tlist = build_relation_tlist(rel);
305 * If it's a parameterized otherrel, there might be lateral references
306 * in the tlist, which need to be replaced with Params. This cannot
307 * happen for regular baserels, though. Note use_physical_tlist()
308 * always fails for otherrels, so we don't need to check this above.
310 if (rel->reloptkind != RELOPT_BASEREL && best_path->param_info)
311 tlist = (List *) replace_nestloop_params(root, (Node *) tlist);
315 * Extract the relevant restriction clauses from the parent relation. The
316 * executor must apply all these restrictions during the scan, except for
317 * pseudoconstants which we'll take care of below.
319 scan_clauses = rel->baserestrictinfo;
322 * If this is a parameterized scan, we also need to enforce all the join
323 * clauses available from the outer relation(s).
325 * For paranoia's sake, don't modify the stored baserestrictinfo list.
327 if (best_path->param_info)
328 scan_clauses = list_concat(list_copy(scan_clauses),
329 best_path->param_info->ppi_clauses);
331 switch (best_path->pathtype)
334 plan = (Plan *) create_seqscan_plan(root,
341 plan = (Plan *) create_indexscan_plan(root,
342 (IndexPath *) best_path,
348 case T_IndexOnlyScan:
349 plan = (Plan *) create_indexscan_plan(root,
350 (IndexPath *) best_path,
356 case T_BitmapHeapScan:
357 plan = (Plan *) create_bitmap_scan_plan(root,
358 (BitmapHeapPath *) best_path,
364 plan = (Plan *) create_tidscan_plan(root,
365 (TidPath *) best_path,
371 plan = (Plan *) create_subqueryscan_plan(root,
378 plan = (Plan *) create_functionscan_plan(root,
385 plan = (Plan *) create_valuesscan_plan(root,
392 plan = (Plan *) create_ctescan_plan(root,
398 case T_WorkTableScan:
399 plan = (Plan *) create_worktablescan_plan(root,
406 plan = (Plan *) create_foreignscan_plan(root,
407 (ForeignPath *) best_path,
413 elog(ERROR, "unrecognized node type: %d",
414 (int) best_path->pathtype);
415 plan = NULL; /* keep compiler quiet */
420 * If there are any pseudoconstant clauses attached to this node, insert a
421 * gating Result node that evaluates the pseudoconstants as one-time
424 if (root->hasPseudoConstantQuals)
425 plan = create_gating_plan(root, plan, scan_clauses);
431 * Build a target list (ie, a list of TargetEntry) for a relation.
434 build_relation_tlist(RelOptInfo *rel)
440 foreach(v, rel->reltargetlist)
442 /* Do we really need to copy here? Not sure */
443 Node *node = (Node *) copyObject(lfirst(v));
445 tlist = lappend(tlist, makeTargetEntry((Expr *) node,
456 * Decide whether to use a tlist matching relation structure,
457 * rather than only those Vars actually referenced.
460 use_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
466 * We can do this for real relation scans, subquery scans, function scans,
467 * values scans, and CTE scans (but not for, eg, joins).
469 if (rel->rtekind != RTE_RELATION &&
470 rel->rtekind != RTE_SUBQUERY &&
471 rel->rtekind != RTE_FUNCTION &&
472 rel->rtekind != RTE_VALUES &&
473 rel->rtekind != RTE_CTE)
477 * Can't do it with inheritance cases either (mainly because Append
480 if (rel->reloptkind != RELOPT_BASEREL)
484 * Can't do it if any system columns or whole-row Vars are requested.
485 * (This could possibly be fixed but would take some fragile assumptions
486 * in setrefs.c, I think.)
488 for (i = rel->min_attr; i <= 0; i++)
490 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
495 * Can't do it if the rel is required to emit any placeholder expressions,
498 foreach(lc, root->placeholder_list)
500 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
502 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
503 bms_is_subset(phinfo->ph_eval_at, rel->relids))
511 * disuse_physical_tlist
512 * Switch a plan node back to emitting only Vars actually referenced.
514 * If the plan node immediately above a scan would prefer to get only
515 * needed Vars and not a physical tlist, it must call this routine to
516 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
517 * and Material nodes want this, so they don't have to store useless columns.
520 disuse_physical_tlist(Plan *plan, Path *path)
522 /* Only need to undo it for path types handled by create_scan_plan() */
523 switch (path->pathtype)
527 case T_IndexOnlyScan:
528 case T_BitmapHeapScan:
534 case T_WorkTableScan:
536 plan->targetlist = build_relation_tlist(path->parent);
545 * Deal with pseudoconstant qual clauses
547 * If the node's quals list includes any pseudoconstant quals, put them
548 * into a gating Result node atop the already-built plan. Otherwise,
549 * return the plan as-is.
551 * Note that we don't change cost or size estimates when doing gating.
552 * The costs of qual eval were already folded into the plan's startup cost.
553 * Leaving the size alone amounts to assuming that the gating qual will
554 * succeed, which is the conservative estimate for planning upper queries.
555 * We certainly don't want to assume the output size is zero (unless the
556 * gating qual is actually constant FALSE, and that case is dealt with in
557 * clausesel.c). Interpolating between the two cases is silly, because
558 * it doesn't reflect what will really happen at runtime, and besides which
559 * in most cases we have only a very bad idea of the probability of the gating
563 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
565 List *pseudoconstants;
567 /* Sort into desirable execution order while still in RestrictInfo form */
568 quals = order_qual_clauses(root, quals);
570 /* Pull out any pseudoconstant quals from the RestrictInfo list */
571 pseudoconstants = extract_actual_clauses(quals, true);
573 if (!pseudoconstants)
576 return (Plan *) make_result(root,
578 (Node *) pseudoconstants,
584 * Create a join plan for 'best_path' and (recursively) plans for its
585 * inner and outer paths.
588 create_join_plan(PlannerInfo *root, JoinPath *best_path)
593 Relids saveOuterRels = root->curOuterRels;
595 outer_plan = create_plan_recurse(root, best_path->outerjoinpath);
597 /* For a nestloop, include outer relids in curOuterRels for inner side */
598 if (best_path->path.pathtype == T_NestLoop)
599 root->curOuterRels = bms_union(root->curOuterRels,
600 best_path->outerjoinpath->parent->relids);
602 inner_plan = create_plan_recurse(root, best_path->innerjoinpath);
604 switch (best_path->path.pathtype)
607 plan = (Plan *) create_mergejoin_plan(root,
608 (MergePath *) best_path,
613 plan = (Plan *) create_hashjoin_plan(root,
614 (HashPath *) best_path,
619 /* Restore curOuterRels */
620 bms_free(root->curOuterRels);
621 root->curOuterRels = saveOuterRels;
623 plan = (Plan *) create_nestloop_plan(root,
624 (NestPath *) best_path,
629 elog(ERROR, "unrecognized node type: %d",
630 (int) best_path->path.pathtype);
631 plan = NULL; /* keep compiler quiet */
636 * If there are any pseudoconstant clauses attached to this node, insert a
637 * gating Result node that evaluates the pseudoconstants as one-time
640 if (root->hasPseudoConstantQuals)
641 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
646 * * Expensive function pullups may have pulled local predicates * into
647 * this path node. Put them in the qpqual of the plan node. * JMH,
650 if (get_loc_restrictinfo(best_path) != NIL)
651 set_qpqual((Plan) plan,
652 list_concat(get_qpqual((Plan) plan),
653 get_actual_clauses(get_loc_restrictinfo(best_path))));
661 * Create an Append plan for 'best_path' and (recursively) plans
664 * Returns a Plan node.
667 create_append_plan(PlannerInfo *root, AppendPath *best_path)
670 List *tlist = build_relation_tlist(best_path->path.parent);
671 List *subplans = NIL;
675 * It is possible for the subplans list to contain only one entry, or even
676 * no entries. Handle these cases specially.
678 * XXX ideally, if there's just one entry, we'd not bother to generate an
679 * Append node but just return the single child. At the moment this does
680 * not work because the varno of the child scan plan won't match the
681 * parent-rel Vars it'll be asked to emit.
683 if (best_path->subpaths == NIL)
685 /* Generate a Result plan with constant-FALSE gating qual */
686 return (Plan *) make_result(root,
688 (Node *) list_make1(makeBoolConst(false,
693 /* Normal case with multiple subpaths */
694 foreach(subpaths, best_path->subpaths)
696 Path *subpath = (Path *) lfirst(subpaths);
698 subplans = lappend(subplans, create_plan_recurse(root, subpath));
701 plan = make_append(subplans, tlist);
703 return (Plan *) plan;
707 * create_merge_append_plan
708 * Create a MergeAppend plan for 'best_path' and (recursively) plans
711 * Returns a Plan node.
714 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
716 MergeAppend *node = makeNode(MergeAppend);
717 Plan *plan = &node->plan;
718 List *tlist = build_relation_tlist(best_path->path.parent);
719 List *pathkeys = best_path->path.pathkeys;
720 List *subplans = NIL;
724 * We don't have the actual creation of the MergeAppend node split out
725 * into a separate make_xxx function. This is because we want to run
726 * prepare_sort_from_pathkeys on it before we do so on the individual
727 * child plans, to make cross-checking the sort info easier.
729 copy_path_costsize(plan, (Path *) best_path);
730 plan->targetlist = tlist;
732 plan->lefttree = NULL;
733 plan->righttree = NULL;
735 /* Compute sort column info, and adjust MergeAppend's tlist as needed */
736 (void) prepare_sort_from_pathkeys(root, plan, pathkeys,
742 &node->sortOperators,
747 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
748 * even to subplans that don't need an explicit sort, to make sure they
749 * are returning the same sort key columns the MergeAppend expects.
751 foreach(subpaths, best_path->subpaths)
753 Path *subpath = (Path *) lfirst(subpaths);
756 AttrNumber *sortColIdx;
761 /* Build the child plan */
762 subplan = create_plan_recurse(root, subpath);
764 /* Compute sort column info, and adjust subplan's tlist as needed */
765 subplan = prepare_sort_from_pathkeys(root, subplan, pathkeys,
766 subpath->parent->relids,
776 * Check that we got the same sort key information. We just Assert
777 * that the sortops match, since those depend only on the pathkeys;
778 * but it seems like a good idea to check the sort column numbers
779 * explicitly, to ensure the tlists really do match up.
781 Assert(numsortkeys == node->numCols);
782 if (memcmp(sortColIdx, node->sortColIdx,
783 numsortkeys * sizeof(AttrNumber)) != 0)
784 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
785 Assert(memcmp(sortOperators, node->sortOperators,
786 numsortkeys * sizeof(Oid)) == 0);
787 Assert(memcmp(collations, node->collations,
788 numsortkeys * sizeof(Oid)) == 0);
789 Assert(memcmp(nullsFirst, node->nullsFirst,
790 numsortkeys * sizeof(bool)) == 0);
792 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
793 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
794 subplan = (Plan *) make_sort(root, subplan, numsortkeys,
795 sortColIdx, sortOperators,
796 collations, nullsFirst,
797 best_path->limit_tuples);
799 subplans = lappend(subplans, subplan);
802 node->mergeplans = subplans;
804 return (Plan *) node;
809 * Create a Result plan for 'best_path'.
810 * This is only used for the case of a query with an empty jointree.
812 * Returns a Plan node.
815 create_result_plan(PlannerInfo *root, ResultPath *best_path)
820 /* The tlist will be installed later, since we have no RelOptInfo */
821 Assert(best_path->path.parent == NULL);
824 /* best_path->quals is just bare clauses */
826 quals = order_qual_clauses(root, best_path->quals);
828 return make_result(root, tlist, (Node *) quals, NULL);
832 * create_material_plan
833 * Create a Material plan for 'best_path' and (recursively) plans
836 * Returns a Plan node.
839 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
844 subplan = create_plan_recurse(root, best_path->subpath);
846 /* We don't want any excess columns in the materialized tuples */
847 disuse_physical_tlist(subplan, best_path->subpath);
849 plan = make_material(subplan);
851 copy_path_costsize(&plan->plan, (Path *) best_path);
858 * Create a Unique plan for 'best_path' and (recursively) plans
861 * Returns a Plan node.
864 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
874 AttrNumber *groupColIdx;
878 subplan = create_plan_recurse(root, best_path->subpath);
880 /* Done if we don't need to do any actual unique-ifying */
881 if (best_path->umethod == UNIQUE_PATH_NOOP)
885 * As constructed, the subplan has a "flat" tlist containing just the Vars
886 * needed here and at upper levels. The values we are supposed to
887 * unique-ify may be expressions in these variables. We have to add any
888 * such expressions to the subplan's tlist.
890 * The subplan may have a "physical" tlist if it is a simple scan plan. If
891 * we're going to sort, this should be reduced to the regular tlist, so
892 * that we don't sort more data than we need to. For hashing, the tlist
893 * should be left as-is if we don't need to add any expressions; but if we
894 * do have to add expressions, then a projection step will be needed at
895 * runtime anyway, so we may as well remove unneeded items. Therefore
896 * newtlist starts from build_relation_tlist() not just a copy of the
897 * subplan's tlist; and we don't install it into the subplan unless we are
898 * sorting or stuff has to be added.
900 in_operators = best_path->in_operators;
901 uniq_exprs = best_path->uniq_exprs;
903 /* initialize modified subplan tlist as just the "required" vars */
904 newtlist = build_relation_tlist(best_path->path.parent);
905 nextresno = list_length(newtlist) + 1;
908 foreach(l, uniq_exprs)
910 Node *uniqexpr = lfirst(l);
913 tle = tlist_member(uniqexpr, newtlist);
916 tle = makeTargetEntry((Expr *) uniqexpr,
920 newtlist = lappend(newtlist, tle);
926 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
929 * If the top plan node can't do projections, we need to add a Result
930 * node to help it along.
932 if (!is_projection_capable_plan(subplan))
933 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
935 subplan->targetlist = newtlist;
939 * Build control information showing which subplan output columns are to
940 * be examined by the grouping step. Unfortunately we can't merge this
941 * with the previous loop, since we didn't then know which version of the
942 * subplan tlist we'd end up using.
944 newtlist = subplan->targetlist;
945 numGroupCols = list_length(uniq_exprs);
946 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
949 foreach(l, uniq_exprs)
951 Node *uniqexpr = lfirst(l);
954 tle = tlist_member(uniqexpr, newtlist);
955 if (!tle) /* shouldn't happen */
956 elog(ERROR, "failed to find unique expression in subplan tlist");
957 groupColIdx[groupColPos++] = tle->resno;
960 if (best_path->umethod == UNIQUE_PATH_HASH)
965 numGroups = (long) Min(best_path->path.rows, (double) LONG_MAX);
968 * Get the hashable equality operators for the Agg node to use.
969 * Normally these are the same as the IN clause operators, but if
970 * those are cross-type operators then the equality operators are the
971 * ones for the IN clause operators' RHS datatype.
973 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
975 foreach(l, in_operators)
977 Oid in_oper = lfirst_oid(l);
980 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
981 elog(ERROR, "could not find compatible hash operator for operator %u",
983 groupOperators[groupColPos++] = eq_oper;
987 * Since the Agg node is going to project anyway, we can give it the
988 * minimum output tlist, without any stuff we might have added to the
991 plan = (Plan *) make_agg(root,
992 build_relation_tlist(best_path->path.parent),
1004 List *sortList = NIL;
1006 /* Create an ORDER BY list to sort the input compatibly */
1008 foreach(l, in_operators)
1010 Oid in_oper = lfirst_oid(l);
1014 SortGroupClause *sortcl;
1016 sortop = get_ordering_op_for_equality_op(in_oper, false);
1017 if (!OidIsValid(sortop)) /* shouldn't happen */
1018 elog(ERROR, "could not find ordering operator for equality operator %u",
1022 * The Unique node will need equality operators. Normally these
1023 * are the same as the IN clause operators, but if those are
1024 * cross-type operators then the equality operators are the ones
1025 * for the IN clause operators' RHS datatype.
1027 eqop = get_equality_op_for_ordering_op(sortop, NULL);
1028 if (!OidIsValid(eqop)) /* shouldn't happen */
1029 elog(ERROR, "could not find equality operator for ordering operator %u",
1032 tle = get_tle_by_resno(subplan->targetlist,
1033 groupColIdx[groupColPos]);
1034 Assert(tle != NULL);
1036 sortcl = makeNode(SortGroupClause);
1037 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
1038 subplan->targetlist);
1039 sortcl->eqop = eqop;
1040 sortcl->sortop = sortop;
1041 sortcl->nulls_first = false;
1042 sortcl->hashable = false; /* no need to make this accurate */
1043 sortList = lappend(sortList, sortcl);
1046 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
1047 plan = (Plan *) make_unique(plan, sortList);
1050 /* Adjust output size estimate (other fields should be OK already) */
1051 plan->plan_rows = best_path->path.rows;
1057 /*****************************************************************************
1059 * BASE-RELATION SCAN METHODS
1061 *****************************************************************************/
1065 * create_seqscan_plan
1066 * Returns a seqscan plan for the base relation scanned by 'best_path'
1067 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1070 create_seqscan_plan(PlannerInfo *root, Path *best_path,
1071 List *tlist, List *scan_clauses)
1074 Index scan_relid = best_path->parent->relid;
1076 /* it should be a base rel... */
1077 Assert(scan_relid > 0);
1078 Assert(best_path->parent->rtekind == RTE_RELATION);
1080 /* Sort clauses into best execution order */
1081 scan_clauses = order_qual_clauses(root, scan_clauses);
1083 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1084 scan_clauses = extract_actual_clauses(scan_clauses, false);
1086 /* Replace any outer-relation variables with nestloop params */
1087 if (best_path->param_info)
1089 scan_clauses = (List *)
1090 replace_nestloop_params(root, (Node *) scan_clauses);
1093 scan_plan = make_seqscan(tlist,
1097 copy_path_costsize(&scan_plan->plan, best_path);
1103 * create_indexscan_plan
1104 * Returns an indexscan plan for the base relation scanned by 'best_path'
1105 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1107 * We use this for both plain IndexScans and IndexOnlyScans, because the
1108 * qual preprocessing work is the same for both. Note that the caller tells
1109 * us which to build --- we don't look at best_path->path.pathtype, because
1110 * create_bitmap_subplan needs to be able to override the prior decision.
1113 create_indexscan_plan(PlannerInfo *root,
1114 IndexPath *best_path,
1120 List *indexquals = best_path->indexquals;
1121 List *indexorderbys = best_path->indexorderbys;
1122 Index baserelid = best_path->path.parent->relid;
1123 Oid indexoid = best_path->indexinfo->indexoid;
1125 List *stripped_indexquals;
1126 List *fixed_indexquals;
1127 List *fixed_indexorderbys;
1130 /* it should be a base rel... */
1131 Assert(baserelid > 0);
1132 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1135 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
1136 * executor as indexqualorig
1138 stripped_indexquals = get_actual_clauses(indexquals);
1141 * The executor needs a copy with the indexkey on the left of each clause
1142 * and with index Vars substituted for table ones.
1144 fixed_indexquals = fix_indexqual_references(root, best_path);
1147 * Likewise fix up index attr references in the ORDER BY expressions.
1149 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
1152 * The qpqual list must contain all restrictions not automatically handled
1153 * by the index, other than pseudoconstant clauses which will be handled
1154 * by a separate gating plan node. All the predicates in the indexquals
1155 * will be checked (either by the index itself, or by nodeIndexscan.c),
1156 * but if there are any "special" operators involved then they must be
1157 * included in qpqual. The upshot is that qpqual must contain
1158 * scan_clauses minus whatever appears in indexquals.
1160 * In normal cases simple pointer equality checks will be enough to spot
1161 * duplicate RestrictInfos, so we try that first.
1163 * Another common case is that a scan_clauses entry is generated from the
1164 * same EquivalenceClass as some indexqual, and is therefore redundant
1165 * with it, though not equal. (This happens when indxpath.c prefers a
1166 * different derived equality than what generate_join_implied_equalities
1167 * picked for a parameterized scan's ppi_clauses.)
1169 * In some situations (particularly with OR'd index conditions) we may
1170 * have scan_clauses that are not equal to, but are logically implied by,
1171 * the index quals; so we also try a predicate_implied_by() check to see
1172 * if we can discard quals that way. (predicate_implied_by assumes its
1173 * first input contains only immutable functions, so we have to check
1176 * We can also discard quals that are implied by a partial index's
1177 * predicate, but only in a plain SELECT; when scanning a target relation
1178 * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
1179 * plan so that they'll be properly rechecked by EvalPlanQual testing.
1182 foreach(l, scan_clauses)
1184 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1186 Assert(IsA(rinfo, RestrictInfo));
1187 if (rinfo->pseudoconstant)
1188 continue; /* we may drop pseudoconstants here */
1189 if (list_member_ptr(indexquals, rinfo))
1190 continue; /* simple duplicate */
1191 if (is_redundant_derived_clause(rinfo, indexquals))
1192 continue; /* derived from same EquivalenceClass */
1193 if (!contain_mutable_functions((Node *) rinfo->clause))
1195 List *clausel = list_make1(rinfo->clause);
1197 if (predicate_implied_by(clausel, indexquals))
1198 continue; /* provably implied by indexquals */
1199 if (best_path->indexinfo->indpred)
1201 if (baserelid != root->parse->resultRelation &&
1202 get_parse_rowmark(root->parse, baserelid) == NULL)
1203 if (predicate_implied_by(clausel,
1204 best_path->indexinfo->indpred))
1205 continue; /* implied by index predicate */
1208 qpqual = lappend(qpqual, rinfo);
1211 /* Sort clauses into best execution order */
1212 qpqual = order_qual_clauses(root, qpqual);
1214 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1215 qpqual = extract_actual_clauses(qpqual, false);
1218 * We have to replace any outer-relation variables with nestloop params in
1219 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
1220 * annoying to have to do this separately from the processing in
1221 * fix_indexqual_references --- rethink this when generalizing the inner
1222 * indexscan support. But note we can't really do this earlier because
1223 * it'd break the comparisons to predicates above ... (or would it? Those
1224 * wouldn't have outer refs)
1226 if (best_path->path.param_info)
1228 stripped_indexquals = (List *)
1229 replace_nestloop_params(root, (Node *) stripped_indexquals);
1231 replace_nestloop_params(root, (Node *) qpqual);
1232 indexorderbys = (List *)
1233 replace_nestloop_params(root, (Node *) indexorderbys);
1236 /* Finally ready to build the plan node */
1238 scan_plan = (Scan *) make_indexonlyscan(tlist,
1243 fixed_indexorderbys,
1244 best_path->indexinfo->indextlist,
1245 best_path->indexscandir);
1247 scan_plan = (Scan *) make_indexscan(tlist,
1252 stripped_indexquals,
1253 fixed_indexorderbys,
1255 best_path->indexscandir);
1257 copy_path_costsize(&scan_plan->plan, &best_path->path);
1263 * create_bitmap_scan_plan
1264 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
1265 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1267 static BitmapHeapScan *
1268 create_bitmap_scan_plan(PlannerInfo *root,
1269 BitmapHeapPath *best_path,
1273 Index baserelid = best_path->path.parent->relid;
1274 Plan *bitmapqualplan;
1275 List *bitmapqualorig;
1280 BitmapHeapScan *scan_plan;
1282 /* it should be a base rel... */
1283 Assert(baserelid > 0);
1284 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1286 /* Process the bitmapqual tree into a Plan tree and qual lists */
1287 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1288 &bitmapqualorig, &indexquals,
1292 * The qpqual list must contain all restrictions not automatically handled
1293 * by the index, other than pseudoconstant clauses which will be handled
1294 * by a separate gating plan node. All the predicates in the indexquals
1295 * will be checked (either by the index itself, or by
1296 * nodeBitmapHeapscan.c), but if there are any "special" operators
1297 * involved then they must be added to qpqual. The upshot is that qpqual
1298 * must contain scan_clauses minus whatever appears in indexquals.
1300 * This loop is similar to the comparable code in create_indexscan_plan(),
1301 * but with some differences because it has to compare the scan clauses to
1302 * stripped (no RestrictInfos) indexquals. See comments there for more
1305 * In normal cases simple equal() checks will be enough to spot duplicate
1306 * clauses, so we try that first. We next see if the scan clause is
1307 * redundant with any top-level indexqual by virtue of being generated
1308 * from the same EC. After that, try predicate_implied_by().
1310 * Unlike create_indexscan_plan(), we need take no special thought here
1311 * for partial index predicates; this is because the predicate conditions
1312 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1313 * to do it that way because predicate conditions need to be rechecked if
1314 * the scan becomes lossy, so they have to be included in bitmapqualorig.
1317 foreach(l, scan_clauses)
1319 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1320 Node *clause = (Node *) rinfo->clause;
1322 Assert(IsA(rinfo, RestrictInfo));
1323 if (rinfo->pseudoconstant)
1324 continue; /* we may drop pseudoconstants here */
1325 if (list_member(indexquals, clause))
1326 continue; /* simple duplicate */
1327 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
1328 continue; /* derived from same EquivalenceClass */
1329 if (!contain_mutable_functions(clause))
1331 List *clausel = list_make1(clause);
1333 if (predicate_implied_by(clausel, indexquals))
1334 continue; /* provably implied by indexquals */
1336 qpqual = lappend(qpqual, rinfo);
1339 /* Sort clauses into best execution order */
1340 qpqual = order_qual_clauses(root, qpqual);
1342 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1343 qpqual = extract_actual_clauses(qpqual, false);
1346 * When dealing with special operators, we will at this point have
1347 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
1348 * 'em from bitmapqualorig, since there's no point in making the tests
1351 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1354 * We have to replace any outer-relation variables with nestloop params in
1355 * the qpqual and bitmapqualorig expressions. (This was already done for
1356 * expressions attached to plan nodes in the bitmapqualplan tree.)
1358 if (best_path->path.param_info)
1361 replace_nestloop_params(root, (Node *) qpqual);
1362 bitmapqualorig = (List *)
1363 replace_nestloop_params(root, (Node *) bitmapqualorig);
1366 /* Finally ready to build the plan node */
1367 scan_plan = make_bitmap_heapscan(tlist,
1373 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1379 * Given a bitmapqual tree, generate the Plan tree that implements it
1381 * As byproducts, we also return in *qual and *indexqual the qual lists
1382 * (in implicit-AND form, without RestrictInfos) describing the original index
1383 * conditions and the generated indexqual conditions. (These are the same in
1384 * simple cases, but when special index operators are involved, the former
1385 * list includes the special conditions while the latter includes the actual
1386 * indexable conditions derived from them.) Both lists include partial-index
1387 * predicates, because we have to recheck predicates as well as index
1388 * conditions if the bitmap scan becomes lossy.
1390 * In addition, we return a list of EquivalenceClass pointers for all the
1391 * top-level indexquals that were possibly-redundantly derived from ECs.
1392 * This allows removal of scan_clauses that are redundant with such quals.
1393 * (We do not attempt to detect such redundancies for quals that are within
1394 * OR subtrees. This could be done in a less hacky way if we returned the
1395 * indexquals in RestrictInfo form, but that would be slower and still pretty
1396 * messy, since we'd have to build new RestrictInfos in many cases.)
1398 * Note: if you find yourself changing this, you probably need to change
1399 * make_restrictinfo_from_bitmapqual too.
1402 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1403 List **qual, List **indexqual, List **indexECs)
1407 if (IsA(bitmapqual, BitmapAndPath))
1409 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1410 List *subplans = NIL;
1411 List *subquals = NIL;
1412 List *subindexquals = NIL;
1413 List *subindexECs = NIL;
1417 * There may well be redundant quals among the subplans, since a
1418 * top-level WHERE qual might have gotten used to form several
1419 * different index quals. We don't try exceedingly hard to eliminate
1420 * redundancies, but we do eliminate obvious duplicates by using
1421 * list_concat_unique.
1423 foreach(l, apath->bitmapquals)
1430 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1431 &subqual, &subindexqual,
1433 subplans = lappend(subplans, subplan);
1434 subquals = list_concat_unique(subquals, subqual);
1435 subindexquals = list_concat_unique(subindexquals, subindexqual);
1436 /* Duplicates in indexECs aren't worth getting rid of */
1437 subindexECs = list_concat(subindexECs, subindexEC);
1439 plan = (Plan *) make_bitmap_and(subplans);
1440 plan->startup_cost = apath->path.startup_cost;
1441 plan->total_cost = apath->path.total_cost;
1443 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1444 plan->plan_width = 0; /* meaningless */
1446 *indexqual = subindexquals;
1447 *indexECs = subindexECs;
1449 else if (IsA(bitmapqual, BitmapOrPath))
1451 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1452 List *subplans = NIL;
1453 List *subquals = NIL;
1454 List *subindexquals = NIL;
1455 bool const_true_subqual = false;
1456 bool const_true_subindexqual = false;
1460 * Here, we only detect qual-free subplans. A qual-free subplan would
1461 * cause us to generate "... OR true ..." which we may as well reduce
1462 * to just "true". We do not try to eliminate redundant subclauses
1463 * because (a) it's not as likely as in the AND case, and (b) we might
1464 * well be working with hundreds or even thousands of OR conditions,
1465 * perhaps from a long IN list. The performance of list_append_unique
1466 * would be unacceptable.
1468 foreach(l, opath->bitmapquals)
1475 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1476 &subqual, &subindexqual,
1478 subplans = lappend(subplans, subplan);
1480 const_true_subqual = true;
1481 else if (!const_true_subqual)
1482 subquals = lappend(subquals,
1483 make_ands_explicit(subqual));
1484 if (subindexqual == NIL)
1485 const_true_subindexqual = true;
1486 else if (!const_true_subindexqual)
1487 subindexquals = lappend(subindexquals,
1488 make_ands_explicit(subindexqual));
1492 * In the presence of ScalarArrayOpExpr quals, we might have built
1493 * BitmapOrPaths with just one subpath; don't add an OR step.
1495 if (list_length(subplans) == 1)
1497 plan = (Plan *) linitial(subplans);
1501 plan = (Plan *) make_bitmap_or(subplans);
1502 plan->startup_cost = opath->path.startup_cost;
1503 plan->total_cost = opath->path.total_cost;
1505 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1506 plan->plan_width = 0; /* meaningless */
1510 * If there were constant-TRUE subquals, the OR reduces to constant
1511 * TRUE. Also, avoid generating one-element ORs, which could happen
1512 * due to redundancy elimination or ScalarArrayOpExpr quals.
1514 if (const_true_subqual)
1516 else if (list_length(subquals) <= 1)
1519 *qual = list_make1(make_orclause(subquals));
1520 if (const_true_subindexqual)
1522 else if (list_length(subindexquals) <= 1)
1523 *indexqual = subindexquals;
1525 *indexqual = list_make1(make_orclause(subindexquals));
1528 else if (IsA(bitmapqual, IndexPath))
1530 IndexPath *ipath = (IndexPath *) bitmapqual;
1535 /* Use the regular indexscan plan build machinery... */
1536 iscan = (IndexScan *) create_indexscan_plan(root, ipath,
1538 Assert(IsA(iscan, IndexScan));
1539 /* then convert to a bitmap indexscan */
1540 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1543 iscan->indexqualorig);
1544 plan->startup_cost = 0.0;
1545 plan->total_cost = ipath->indextotalcost;
1547 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1548 plan->plan_width = 0; /* meaningless */
1549 *qual = get_actual_clauses(ipath->indexclauses);
1550 *indexqual = get_actual_clauses(ipath->indexquals);
1551 foreach(l, ipath->indexinfo->indpred)
1553 Expr *pred = (Expr *) lfirst(l);
1556 * We know that the index predicate must have been implied by the
1557 * query condition as a whole, but it may or may not be implied by
1558 * the conditions that got pushed into the bitmapqual. Avoid
1559 * generating redundant conditions.
1561 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1563 *qual = lappend(*qual, pred);
1564 *indexqual = lappend(*indexqual, pred);
1568 foreach(l, ipath->indexquals)
1570 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1572 if (rinfo->parent_ec)
1573 subindexECs = lappend(subindexECs, rinfo->parent_ec);
1575 *indexECs = subindexECs;
1579 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1580 plan = NULL; /* keep compiler quiet */
1587 * create_tidscan_plan
1588 * Returns a tidscan plan for the base relation scanned by 'best_path'
1589 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1592 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1593 List *tlist, List *scan_clauses)
1596 Index scan_relid = best_path->path.parent->relid;
1597 List *tidquals = best_path->tidquals;
1600 /* it should be a base rel... */
1601 Assert(scan_relid > 0);
1602 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1604 /* Sort clauses into best execution order */
1605 scan_clauses = order_qual_clauses(root, scan_clauses);
1607 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1608 scan_clauses = extract_actual_clauses(scan_clauses, false);
1610 /* Replace any outer-relation variables with nestloop params */
1611 if (best_path->path.param_info)
1614 replace_nestloop_params(root, (Node *) tidquals);
1615 scan_clauses = (List *)
1616 replace_nestloop_params(root, (Node *) scan_clauses);
1620 * Remove any clauses that are TID quals. This is a bit tricky since the
1621 * tidquals list has implicit OR semantics.
1623 ortidquals = tidquals;
1624 if (list_length(ortidquals) > 1)
1625 ortidquals = list_make1(make_orclause(ortidquals));
1626 scan_clauses = list_difference(scan_clauses, ortidquals);
1628 scan_plan = make_tidscan(tlist,
1633 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1639 * create_subqueryscan_plan
1640 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1641 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1643 static SubqueryScan *
1644 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1645 List *tlist, List *scan_clauses)
1647 SubqueryScan *scan_plan;
1648 Index scan_relid = best_path->parent->relid;
1650 /* it should be a subquery base rel... */
1651 Assert(scan_relid > 0);
1652 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1654 /* Sort clauses into best execution order */
1655 scan_clauses = order_qual_clauses(root, scan_clauses);
1657 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1658 scan_clauses = extract_actual_clauses(scan_clauses, false);
1660 /* Replace any outer-relation variables with nestloop params */
1661 if (best_path->param_info)
1663 scan_clauses = (List *)
1664 replace_nestloop_params(root, (Node *) scan_clauses);
1665 identify_nestloop_extparams(root, best_path->parent->subplan);
1668 scan_plan = make_subqueryscan(tlist,
1671 best_path->parent->subplan);
1673 copy_path_costsize(&scan_plan->scan.plan, best_path);
1679 * create_functionscan_plan
1680 * Returns a functionscan plan for the base relation scanned by 'best_path'
1681 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1683 static FunctionScan *
1684 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1685 List *tlist, List *scan_clauses)
1687 FunctionScan *scan_plan;
1688 Index scan_relid = best_path->parent->relid;
1692 /* it should be a function base rel... */
1693 Assert(scan_relid > 0);
1694 rte = planner_rt_fetch(scan_relid, root);
1695 Assert(rte->rtekind == RTE_FUNCTION);
1696 funcexpr = rte->funcexpr;
1698 /* Sort clauses into best execution order */
1699 scan_clauses = order_qual_clauses(root, scan_clauses);
1701 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1702 scan_clauses = extract_actual_clauses(scan_clauses, false);
1704 /* Replace any outer-relation variables with nestloop params */
1705 if (best_path->param_info)
1707 scan_clauses = (List *)
1708 replace_nestloop_params(root, (Node *) scan_clauses);
1709 /* The func expression itself could contain nestloop params, too */
1710 funcexpr = replace_nestloop_params(root, funcexpr);
1713 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1715 rte->eref->colnames,
1717 rte->funccoltypmods,
1718 rte->funccolcollations);
1720 copy_path_costsize(&scan_plan->scan.plan, best_path);
1726 * create_valuesscan_plan
1727 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1728 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1731 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1732 List *tlist, List *scan_clauses)
1734 ValuesScan *scan_plan;
1735 Index scan_relid = best_path->parent->relid;
1739 /* it should be a values base rel... */
1740 Assert(scan_relid > 0);
1741 rte = planner_rt_fetch(scan_relid, root);
1742 Assert(rte->rtekind == RTE_VALUES);
1743 values_lists = rte->values_lists;
1745 /* Sort clauses into best execution order */
1746 scan_clauses = order_qual_clauses(root, scan_clauses);
1748 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1749 scan_clauses = extract_actual_clauses(scan_clauses, false);
1751 /* Replace any outer-relation variables with nestloop params */
1752 if (best_path->param_info)
1754 scan_clauses = (List *)
1755 replace_nestloop_params(root, (Node *) scan_clauses);
1756 /* The values lists could contain nestloop params, too */
1757 values_lists = (List *)
1758 replace_nestloop_params(root, (Node *) values_lists);
1761 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1764 copy_path_costsize(&scan_plan->scan.plan, best_path);
1770 * create_ctescan_plan
1771 * Returns a ctescan plan for the base relation scanned by 'best_path'
1772 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1775 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1776 List *tlist, List *scan_clauses)
1779 Index scan_relid = best_path->parent->relid;
1781 SubPlan *ctesplan = NULL;
1784 PlannerInfo *cteroot;
1789 Assert(scan_relid > 0);
1790 rte = planner_rt_fetch(scan_relid, root);
1791 Assert(rte->rtekind == RTE_CTE);
1792 Assert(!rte->self_reference);
1795 * Find the referenced CTE, and locate the SubPlan previously made for it.
1797 levelsup = rte->ctelevelsup;
1799 while (levelsup-- > 0)
1801 cteroot = cteroot->parent_root;
1802 if (!cteroot) /* shouldn't happen */
1803 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1807 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1808 * on planning the CTEs (ie, this is a side-reference from another CTE).
1809 * So we mustn't use forboth here.
1812 foreach(lc, cteroot->parse->cteList)
1814 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1816 if (strcmp(cte->ctename, rte->ctename) == 0)
1820 if (lc == NULL) /* shouldn't happen */
1821 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1822 if (ndx >= list_length(cteroot->cte_plan_ids))
1823 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1824 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1825 Assert(plan_id > 0);
1826 foreach(lc, cteroot->init_plans)
1828 ctesplan = (SubPlan *) lfirst(lc);
1829 if (ctesplan->plan_id == plan_id)
1832 if (lc == NULL) /* shouldn't happen */
1833 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1836 * We need the CTE param ID, which is the sole member of the SubPlan's
1839 cte_param_id = linitial_int(ctesplan->setParam);
1841 /* Sort clauses into best execution order */
1842 scan_clauses = order_qual_clauses(root, scan_clauses);
1844 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1845 scan_clauses = extract_actual_clauses(scan_clauses, false);
1847 /* Replace any outer-relation variables with nestloop params */
1848 if (best_path->param_info)
1850 scan_clauses = (List *)
1851 replace_nestloop_params(root, (Node *) scan_clauses);
1854 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1855 plan_id, cte_param_id);
1857 copy_path_costsize(&scan_plan->scan.plan, best_path);
1863 * create_worktablescan_plan
1864 * Returns a worktablescan plan for the base relation scanned by 'best_path'
1865 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1867 static WorkTableScan *
1868 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1869 List *tlist, List *scan_clauses)
1871 WorkTableScan *scan_plan;
1872 Index scan_relid = best_path->parent->relid;
1875 PlannerInfo *cteroot;
1877 Assert(scan_relid > 0);
1878 rte = planner_rt_fetch(scan_relid, root);
1879 Assert(rte->rtekind == RTE_CTE);
1880 Assert(rte->self_reference);
1883 * We need to find the worktable param ID, which is in the plan level
1884 * that's processing the recursive UNION, which is one level *below* where
1885 * the CTE comes from.
1887 levelsup = rte->ctelevelsup;
1888 if (levelsup == 0) /* shouldn't happen */
1889 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1892 while (levelsup-- > 0)
1894 cteroot = cteroot->parent_root;
1895 if (!cteroot) /* shouldn't happen */
1896 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1898 if (cteroot->wt_param_id < 0) /* shouldn't happen */
1899 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1901 /* Sort clauses into best execution order */
1902 scan_clauses = order_qual_clauses(root, scan_clauses);
1904 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1905 scan_clauses = extract_actual_clauses(scan_clauses, false);
1907 /* Replace any outer-relation variables with nestloop params */
1908 if (best_path->param_info)
1910 scan_clauses = (List *)
1911 replace_nestloop_params(root, (Node *) scan_clauses);
1914 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
1915 cteroot->wt_param_id);
1917 copy_path_costsize(&scan_plan->scan.plan, best_path);
1923 * create_foreignscan_plan
1924 * Returns a foreignscan plan for the base relation scanned by 'best_path'
1925 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1927 static ForeignScan *
1928 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
1929 List *tlist, List *scan_clauses)
1931 ForeignScan *scan_plan;
1932 RelOptInfo *rel = best_path->path.parent;
1933 Index scan_relid = rel->relid;
1937 /* it should be a base rel... */
1938 Assert(scan_relid > 0);
1939 Assert(rel->rtekind == RTE_RELATION);
1940 rte = planner_rt_fetch(scan_relid, root);
1941 Assert(rte->rtekind == RTE_RELATION);
1944 * Sort clauses into best execution order. We do this first since the FDW
1945 * might have more info than we do and wish to adjust the ordering.
1947 scan_clauses = order_qual_clauses(root, scan_clauses);
1950 * Let the FDW perform its processing on the restriction clauses and
1951 * generate the plan node. Note that the FDW might remove restriction
1952 * clauses that it intends to execute remotely, or even add more (if it
1953 * has selected some join clauses for remote use but also wants them
1954 * rechecked locally).
1956 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rte->relid,
1958 tlist, scan_clauses);
1960 /* Copy cost data from Path to Plan; no need to make FDW do this */
1961 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1964 * Replace any outer-relation variables with nestloop params in the qual
1965 * and fdw_exprs expressions. We do this last so that the FDW doesn't
1966 * have to be involved. (Note that parts of fdw_exprs could have come
1967 * from join clauses, so doing this beforehand on the scan_clauses
1970 if (best_path->path.param_info)
1972 scan_plan->scan.plan.qual = (List *)
1973 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
1974 scan_plan->fdw_exprs = (List *)
1975 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
1979 * Detect whether any system columns are requested from rel. This is a
1980 * bit of a kluge and might go away someday, so we intentionally leave it
1981 * out of the API presented to FDWs.
1983 scan_plan->fsSystemCol = false;
1984 for (i = rel->min_attr; i < 0; i++)
1986 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
1988 scan_plan->fsSystemCol = true;
1997 /*****************************************************************************
2001 *****************************************************************************/
2004 create_nestloop_plan(PlannerInfo *root,
2005 NestPath *best_path,
2009 NestLoop *join_plan;
2010 List *tlist = build_relation_tlist(best_path->path.parent);
2011 List *joinrestrictclauses = best_path->joinrestrictinfo;
2020 /* Sort join qual clauses into best execution order */
2021 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
2023 /* Get the join qual clauses (in plain expression form) */
2024 /* Any pseudoconstant clauses are ignored here */
2025 if (IS_OUTER_JOIN(best_path->jointype))
2027 extract_actual_join_clauses(joinrestrictclauses,
2028 &joinclauses, &otherclauses);
2032 /* We can treat all clauses alike for an inner join */
2033 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
2037 /* Replace any outer-relation variables with nestloop params */
2038 if (best_path->path.param_info)
2040 joinclauses = (List *)
2041 replace_nestloop_params(root, (Node *) joinclauses);
2042 otherclauses = (List *)
2043 replace_nestloop_params(root, (Node *) otherclauses);
2047 * Identify any nestloop parameters that should be supplied by this join
2048 * node, and move them from root->curOuterParams to the nestParams list.
2050 outerrelids = best_path->outerjoinpath->parent->relids;
2053 for (cell = list_head(root->curOuterParams); cell; cell = next)
2055 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
2058 if (IsA(nlp->paramval, Var) &&
2059 bms_is_member(nlp->paramval->varno, outerrelids))
2061 root->curOuterParams = list_delete_cell(root->curOuterParams,
2063 nestParams = lappend(nestParams, nlp);
2065 else if (IsA(nlp->paramval, PlaceHolderVar) &&
2066 bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
2068 bms_is_subset(find_placeholder_info(root,
2069 (PlaceHolderVar *) nlp->paramval,
2073 root->curOuterParams = list_delete_cell(root->curOuterParams,
2075 nestParams = lappend(nestParams, nlp);
2081 join_plan = make_nestloop(tlist,
2087 best_path->jointype);
2089 copy_path_costsize(&join_plan->join.plan, &best_path->path);
2095 create_mergejoin_plan(PlannerInfo *root,
2096 MergePath *best_path,
2100 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
2104 List *outerpathkeys;
2105 List *innerpathkeys;
2108 Oid *mergecollations;
2109 int *mergestrategies;
2110 bool *mergenullsfirst;
2111 MergeJoin *join_plan;
2117 /* Sort join qual clauses into best execution order */
2118 /* NB: do NOT reorder the mergeclauses */
2119 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2121 /* Get the join qual clauses (in plain expression form) */
2122 /* Any pseudoconstant clauses are ignored here */
2123 if (IS_OUTER_JOIN(best_path->jpath.jointype))
2125 extract_actual_join_clauses(joinclauses,
2126 &joinclauses, &otherclauses);
2130 /* We can treat all clauses alike for an inner join */
2131 joinclauses = extract_actual_clauses(joinclauses, false);
2136 * Remove the mergeclauses from the list of join qual clauses, leaving the
2137 * list of quals that must be checked as qpquals.
2139 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
2140 joinclauses = list_difference(joinclauses, mergeclauses);
2143 * Replace any outer-relation variables with nestloop params. There
2144 * should not be any in the mergeclauses.
2146 if (best_path->jpath.path.param_info)
2148 joinclauses = (List *)
2149 replace_nestloop_params(root, (Node *) joinclauses);
2150 otherclauses = (List *)
2151 replace_nestloop_params(root, (Node *) otherclauses);
2155 * Rearrange mergeclauses, if needed, so that the outer variable is always
2156 * on the left; mark the mergeclause restrictinfos with correct
2157 * outer_is_left status.
2159 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
2160 best_path->jpath.outerjoinpath->parent->relids);
2163 * Create explicit sort nodes for the outer and inner paths if necessary.
2164 * Make sure there are no excess columns in the inputs if sorting.
2166 if (best_path->outersortkeys)
2168 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
2169 outer_plan = (Plan *)
2170 make_sort_from_pathkeys(root,
2172 best_path->outersortkeys,
2174 outerpathkeys = best_path->outersortkeys;
2177 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
2179 if (best_path->innersortkeys)
2181 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
2182 inner_plan = (Plan *)
2183 make_sort_from_pathkeys(root,
2185 best_path->innersortkeys,
2187 innerpathkeys = best_path->innersortkeys;
2190 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
2193 * If specified, add a materialize node to shield the inner plan from the
2194 * need to handle mark/restore.
2196 if (best_path->materialize_inner)
2198 Plan *matplan = (Plan *) make_material(inner_plan);
2201 * We assume the materialize will not spill to disk, and therefore
2202 * charge just cpu_operator_cost per tuple. (Keep this estimate in
2203 * sync with final_cost_mergejoin.)
2205 copy_plan_costsize(matplan, inner_plan);
2206 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
2208 inner_plan = matplan;
2212 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
2213 * executor. The information is in the pathkeys for the two inputs, but
2214 * we need to be careful about the possibility of mergeclauses sharing a
2215 * pathkey (compare find_mergeclauses_for_pathkeys()).
2217 nClauses = list_length(mergeclauses);
2218 Assert(nClauses == list_length(best_path->path_mergeclauses));
2219 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
2220 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
2221 mergestrategies = (int *) palloc(nClauses * sizeof(int));
2222 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
2224 lop = list_head(outerpathkeys);
2225 lip = list_head(innerpathkeys);
2227 foreach(lc, best_path->path_mergeclauses)
2229 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2230 EquivalenceClass *oeclass;
2231 EquivalenceClass *ieclass;
2234 EquivalenceClass *opeclass;
2235 EquivalenceClass *ipeclass;
2238 /* fetch outer/inner eclass from mergeclause */
2239 Assert(IsA(rinfo, RestrictInfo));
2240 if (rinfo->outer_is_left)
2242 oeclass = rinfo->left_ec;
2243 ieclass = rinfo->right_ec;
2247 oeclass = rinfo->right_ec;
2248 ieclass = rinfo->left_ec;
2250 Assert(oeclass != NULL);
2251 Assert(ieclass != NULL);
2254 * For debugging purposes, we check that the eclasses match the paths'
2255 * pathkeys. In typical cases the merge clauses are one-to-one with
2256 * the pathkeys, but when dealing with partially redundant query
2257 * conditions, we might have clauses that re-reference earlier path
2258 * keys. The case that we need to reject is where a pathkey is
2259 * entirely skipped over.
2261 * lop and lip reference the first as-yet-unused pathkey elements;
2262 * it's okay to match them, or any element before them. If they're
2263 * NULL then we have found all pathkey elements to be used.
2267 opathkey = (PathKey *) lfirst(lop);
2268 opeclass = opathkey->pk_eclass;
2269 if (oeclass == opeclass)
2271 /* fast path for typical case */
2276 /* redundant clauses ... must match something before lop */
2277 foreach(l2, outerpathkeys)
2281 opathkey = (PathKey *) lfirst(l2);
2282 opeclass = opathkey->pk_eclass;
2283 if (oeclass == opeclass)
2286 if (oeclass != opeclass)
2287 elog(ERROR, "outer pathkeys do not match mergeclauses");
2292 /* redundant clauses ... must match some already-used pathkey */
2295 foreach(l2, outerpathkeys)
2297 opathkey = (PathKey *) lfirst(l2);
2298 opeclass = opathkey->pk_eclass;
2299 if (oeclass == opeclass)
2303 elog(ERROR, "outer pathkeys do not match mergeclauses");
2308 ipathkey = (PathKey *) lfirst(lip);
2309 ipeclass = ipathkey->pk_eclass;
2310 if (ieclass == ipeclass)
2312 /* fast path for typical case */
2317 /* redundant clauses ... must match something before lip */
2318 foreach(l2, innerpathkeys)
2322 ipathkey = (PathKey *) lfirst(l2);
2323 ipeclass = ipathkey->pk_eclass;
2324 if (ieclass == ipeclass)
2327 if (ieclass != ipeclass)
2328 elog(ERROR, "inner pathkeys do not match mergeclauses");
2333 /* redundant clauses ... must match some already-used pathkey */
2336 foreach(l2, innerpathkeys)
2338 ipathkey = (PathKey *) lfirst(l2);
2339 ipeclass = ipathkey->pk_eclass;
2340 if (ieclass == ipeclass)
2344 elog(ERROR, "inner pathkeys do not match mergeclauses");
2347 /* pathkeys should match each other too (more debugging) */
2348 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
2349 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
2350 opathkey->pk_strategy != ipathkey->pk_strategy ||
2351 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
2352 elog(ERROR, "left and right pathkeys do not match in mergejoin");
2354 /* OK, save info for executor */
2355 mergefamilies[i] = opathkey->pk_opfamily;
2356 mergecollations[i] = opathkey->pk_eclass->ec_collation;
2357 mergestrategies[i] = opathkey->pk_strategy;
2358 mergenullsfirst[i] = opathkey->pk_nulls_first;
2363 * Note: it is not an error if we have additional pathkey elements (i.e.,
2364 * lop or lip isn't NULL here). The input paths might be better-sorted
2365 * than we need for the current mergejoin.
2369 * Now we can build the mergejoin node.
2371 join_plan = make_mergejoin(tlist,
2381 best_path->jpath.jointype);
2383 /* Costs of sort and material steps are included in path cost already */
2384 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2390 create_hashjoin_plan(PlannerInfo *root,
2391 HashPath *best_path,
2395 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
2399 Oid skewTable = InvalidOid;
2400 AttrNumber skewColumn = InvalidAttrNumber;
2401 bool skewInherit = false;
2402 Oid skewColType = InvalidOid;
2403 int32 skewColTypmod = -1;
2404 HashJoin *join_plan;
2407 /* Sort join qual clauses into best execution order */
2408 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2409 /* There's no point in sorting the hash clauses ... */
2411 /* Get the join qual clauses (in plain expression form) */
2412 /* Any pseudoconstant clauses are ignored here */
2413 if (IS_OUTER_JOIN(best_path->jpath.jointype))
2415 extract_actual_join_clauses(joinclauses,
2416 &joinclauses, &otherclauses);
2420 /* We can treat all clauses alike for an inner join */
2421 joinclauses = extract_actual_clauses(joinclauses, false);
2426 * Remove the hashclauses from the list of join qual clauses, leaving the
2427 * list of quals that must be checked as qpquals.
2429 hashclauses = get_actual_clauses(best_path->path_hashclauses);
2430 joinclauses = list_difference(joinclauses, hashclauses);
2433 * Replace any outer-relation variables with nestloop params. There
2434 * should not be any in the hashclauses.
2436 if (best_path->jpath.path.param_info)
2438 joinclauses = (List *)
2439 replace_nestloop_params(root, (Node *) joinclauses);
2440 otherclauses = (List *)
2441 replace_nestloop_params(root, (Node *) otherclauses);
2445 * Rearrange hashclauses, if needed, so that the outer variable is always
2448 hashclauses = get_switched_clauses(best_path->path_hashclauses,
2449 best_path->jpath.outerjoinpath->parent->relids);
2451 /* We don't want any excess columns in the hashed tuples */
2452 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
2454 /* If we expect batching, suppress excess columns in outer tuples too */
2455 if (best_path->num_batches > 1)
2456 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
2459 * If there is a single join clause and we can identify the outer variable
2460 * as a simple column reference, supply its identity for possible use in
2461 * skew optimization. (Note: in principle we could do skew optimization
2462 * with multiple join clauses, but we'd have to be able to determine the
2463 * most common combinations of outer values, which we don't currently have
2464 * enough stats for.)
2466 if (list_length(hashclauses) == 1)
2468 OpExpr *clause = (OpExpr *) linitial(hashclauses);
2471 Assert(is_opclause(clause));
2472 node = (Node *) linitial(clause->args);
2473 if (IsA(node, RelabelType))
2474 node = (Node *) ((RelabelType *) node)->arg;
2477 Var *var = (Var *) node;
2480 rte = root->simple_rte_array[var->varno];
2481 if (rte->rtekind == RTE_RELATION)
2483 skewTable = rte->relid;
2484 skewColumn = var->varattno;
2485 skewInherit = rte->inh;
2486 skewColType = var->vartype;
2487 skewColTypmod = var->vartypmod;
2493 * Build the hash node and hash join node.
2495 hash_plan = make_hash(inner_plan,
2501 join_plan = make_hashjoin(tlist,
2507 best_path->jpath.jointype);
2509 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2515 /*****************************************************************************
2517 * SUPPORTING ROUTINES
2519 *****************************************************************************/
2522 * replace_nestloop_params
2523 * Replace outer-relation Vars and PlaceHolderVars in the given expression
2524 * with nestloop Params
2526 * All Vars and PlaceHolderVars belonging to the relation(s) identified by
2527 * root->curOuterRels are replaced by Params, and entries are added to
2528 * root->curOuterParams if not already present.
2531 replace_nestloop_params(PlannerInfo *root, Node *expr)
2533 /* No setup needed for tree walk, so away we go */
2534 return replace_nestloop_params_mutator(expr, root);
2538 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
2544 Var *var = (Var *) node;
2549 /* Upper-level Vars should be long gone at this point */
2550 Assert(var->varlevelsup == 0);
2551 /* If not to be replaced, we can just return the Var unmodified */
2552 if (!bms_is_member(var->varno, root->curOuterRels))
2554 /* Create a Param representing the Var */
2555 param = assign_nestloop_param_var(root, var);
2556 /* Is this param already listed in root->curOuterParams? */
2557 foreach(lc, root->curOuterParams)
2559 nlp = (NestLoopParam *) lfirst(lc);
2560 if (nlp->paramno == param->paramid)
2562 Assert(equal(var, nlp->paramval));
2563 /* Present, so we can just return the Param */
2564 return (Node *) param;
2568 nlp = makeNode(NestLoopParam);
2569 nlp->paramno = param->paramid;
2570 nlp->paramval = var;
2571 root->curOuterParams = lappend(root->curOuterParams, nlp);
2572 /* And return the replacement Param */
2573 return (Node *) param;
2575 if (IsA(node, PlaceHolderVar))
2577 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2582 /* Upper-level PlaceHolderVars should be long gone at this point */
2583 Assert(phv->phlevelsup == 0);
2586 * If not to be replaced, just return the PlaceHolderVar unmodified.
2587 * We use bms_overlap as a cheap/quick test to see if the PHV might be
2588 * evaluated in the outer rels, and then grab its PlaceHolderInfo to
2591 if (!bms_overlap(phv->phrels, root->curOuterRels))
2593 if (!bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
2594 root->curOuterRels))
2596 /* Create a Param representing the PlaceHolderVar */
2597 param = assign_nestloop_param_placeholdervar(root, phv);
2598 /* Is this param already listed in root->curOuterParams? */
2599 foreach(lc, root->curOuterParams)
2601 nlp = (NestLoopParam *) lfirst(lc);
2602 if (nlp->paramno == param->paramid)
2604 Assert(equal(phv, nlp->paramval));
2605 /* Present, so we can just return the Param */
2606 return (Node *) param;
2610 nlp = makeNode(NestLoopParam);
2611 nlp->paramno = param->paramid;
2612 nlp->paramval = (Var *) phv;
2613 root->curOuterParams = lappend(root->curOuterParams, nlp);
2614 /* And return the replacement Param */
2615 return (Node *) param;
2617 return expression_tree_mutator(node,
2618 replace_nestloop_params_mutator,
2623 * identify_nestloop_extparams
2624 * Identify extParams of a parameterized subquery that need to be fed
2625 * from an outer nestloop.
2627 * The subplan's references to the outer variables are already represented
2628 * as PARAM_EXEC Params, so we need not modify the subplan here. What we
2629 * do need to do is add entries to root->curOuterParams to signal the parent
2630 * nestloop plan node that it must provide these values.
2633 identify_nestloop_extparams(PlannerInfo *root, Plan *subplan)
2638 /* Examine each extParam of the subquery's plan */
2639 tmpset = bms_copy(subplan->extParam);
2640 while ((paramid = bms_first_member(tmpset)) >= 0)
2642 PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
2644 /* Ignore anything coming from an upper query level */
2645 if (pitem->abslevel != root->query_level)
2648 if (IsA(pitem->item, Var))
2650 Var *var = (Var *) pitem->item;
2654 /* If not from a nestloop outer rel, nothing to do */
2655 if (!bms_is_member(var->varno, root->curOuterRels))
2657 /* Is this param already listed in root->curOuterParams? */
2658 foreach(lc, root->curOuterParams)
2660 nlp = (NestLoopParam *) lfirst(lc);
2661 if (nlp->paramno == paramid)
2663 Assert(equal(var, nlp->paramval));
2664 /* Present, so nothing to do */
2671 nlp = makeNode(NestLoopParam);
2672 nlp->paramno = paramid;
2673 nlp->paramval = copyObject(var);
2674 root->curOuterParams = lappend(root->curOuterParams, nlp);
2677 else if (IsA(pitem->item, PlaceHolderVar))
2679 PlaceHolderVar *phv = (PlaceHolderVar *) pitem->item;
2684 * If not from a nestloop outer rel, nothing to do. We use
2685 * bms_overlap as a cheap/quick test to see if the PHV might be
2686 * evaluated in the outer rels, and then grab its PlaceHolderInfo
2689 if (!bms_overlap(phv->phrels, root->curOuterRels))
2691 if (!bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
2692 root->curOuterRels))
2694 /* Is this param already listed in root->curOuterParams? */
2695 foreach(lc, root->curOuterParams)
2697 nlp = (NestLoopParam *) lfirst(lc);
2698 if (nlp->paramno == paramid)
2700 Assert(equal(phv, nlp->paramval));
2701 /* Present, so nothing to do */
2708 nlp = makeNode(NestLoopParam);
2709 nlp->paramno = paramid;
2710 nlp->paramval = copyObject(phv);
2711 root->curOuterParams = lappend(root->curOuterParams, nlp);
2719 * fix_indexqual_references
2720 * Adjust indexqual clauses to the form the executor's indexqual
2723 * We have four tasks here:
2724 * * Remove RestrictInfo nodes from the input clauses.
2725 * * Replace any outer-relation Var or PHV nodes with nestloop Params.
2726 * (XXX eventually, that responsibility should go elsewhere?)
2727 * * Index keys must be represented by Var nodes with varattno set to the
2728 * index's attribute number, not the attribute number in the original rel.
2729 * * If the index key is on the right, commute the clause to put it on the
2732 * The result is a modified copy of the path's indexquals list --- the
2733 * original is not changed. Note also that the copy shares no substructure
2734 * with the original; this is needed in case there is a subplan in it (we need
2735 * two separate copies of the subplan tree, or things will go awry).
2738 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
2740 IndexOptInfo *index = index_path->indexinfo;
2741 List *fixed_indexquals;
2745 fixed_indexquals = NIL;
2747 forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
2749 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
2750 int indexcol = lfirst_int(lci);
2753 Assert(IsA(rinfo, RestrictInfo));
2756 * Replace any outer-relation variables with nestloop params.
2758 * This also makes a copy of the clause, so it's safe to modify it
2761 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
2763 if (IsA(clause, OpExpr))
2765 OpExpr *op = (OpExpr *) clause;
2767 if (list_length(op->args) != 2)
2768 elog(ERROR, "indexqual clause is not binary opclause");
2771 * Check to see if the indexkey is on the right; if so, commute
2772 * the clause. The indexkey should be the side that refers to
2773 * (only) the base relation.
2775 if (!bms_equal(rinfo->left_relids, index->rel->relids))
2779 * Now replace the indexkey expression with an index Var.
2781 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2785 else if (IsA(clause, RowCompareExpr))
2787 RowCompareExpr *rc = (RowCompareExpr *) clause;
2795 * Re-discover which index columns are used in the rowcompare.
2797 newrc = adjust_rowcompare_for_index(rc,
2804 * Trouble if adjust_rowcompare_for_index thought the
2805 * RowCompareExpr didn't match the index as-is; the clause should
2806 * have gone through that routine already.
2808 if (newrc != (Expr *) rc)
2809 elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
2812 * Check to see if the indexkey is on the right; if so, commute
2816 CommuteRowCompareExpr(rc);
2819 * Now replace the indexkey expressions with index Vars.
2821 Assert(list_length(rc->largs) == list_length(indexcolnos));
2822 forboth(lca, rc->largs, lcai, indexcolnos)
2824 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
2829 else if (IsA(clause, ScalarArrayOpExpr))
2831 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2833 /* Never need to commute... */
2835 /* Replace the indexkey expression with an index Var. */
2836 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2840 else if (IsA(clause, NullTest))
2842 NullTest *nt = (NullTest *) clause;
2844 /* Replace the indexkey expression with an index Var. */
2845 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
2850 elog(ERROR, "unsupported indexqual type: %d",
2851 (int) nodeTag(clause));
2853 fixed_indexquals = lappend(fixed_indexquals, clause);
2856 return fixed_indexquals;
2860 * fix_indexorderby_references
2861 * Adjust indexorderby clauses to the form the executor's index
2864 * This is a simplified version of fix_indexqual_references. The input does
2865 * not have RestrictInfo nodes, and we assume that indxpath.c already
2866 * commuted the clauses to put the index keys on the left. Also, we don't
2867 * bother to support any cases except simple OpExprs, since nothing else
2868 * is allowed for ordering operators.
2871 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
2873 IndexOptInfo *index = index_path->indexinfo;
2874 List *fixed_indexorderbys;
2878 fixed_indexorderbys = NIL;
2880 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
2882 Node *clause = (Node *) lfirst(lcc);
2883 int indexcol = lfirst_int(lci);
2886 * Replace any outer-relation variables with nestloop params.
2888 * This also makes a copy of the clause, so it's safe to modify it
2891 clause = replace_nestloop_params(root, clause);
2893 if (IsA(clause, OpExpr))
2895 OpExpr *op = (OpExpr *) clause;
2897 if (list_length(op->args) != 2)
2898 elog(ERROR, "indexorderby clause is not binary opclause");
2901 * Now replace the indexkey expression with an index Var.
2903 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2908 elog(ERROR, "unsupported indexorderby type: %d",
2909 (int) nodeTag(clause));
2911 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
2914 return fixed_indexorderbys;
2918 * fix_indexqual_operand
2919 * Convert an indexqual expression to a Var referencing the index column.
2921 * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
2922 * equal to the index's attribute number (index column position).
2924 * Most of the code here is just for sanity cross-checking that the given
2925 * expression actually matches the index column it's claimed to.
2928 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
2932 ListCell *indexpr_item;
2935 * Remove any binary-compatible relabeling of the indexkey
2937 if (IsA(node, RelabelType))
2938 node = (Node *) ((RelabelType *) node)->arg;
2940 Assert(indexcol >= 0 && indexcol < index->ncolumns);
2942 if (index->indexkeys[indexcol] != 0)
2944 /* It's a simple index column */
2945 if (IsA(node, Var) &&
2946 ((Var *) node)->varno == index->rel->relid &&
2947 ((Var *) node)->varattno == index->indexkeys[indexcol])
2949 result = (Var *) copyObject(node);
2950 result->varno = INDEX_VAR;
2951 result->varattno = indexcol + 1;
2952 return (Node *) result;
2955 elog(ERROR, "index key does not match expected index column");
2958 /* It's an index expression, so find and cross-check the expression */
2959 indexpr_item = list_head(index->indexprs);
2960 for (pos = 0; pos < index->ncolumns; pos++)
2962 if (index->indexkeys[pos] == 0)
2964 if (indexpr_item == NULL)
2965 elog(ERROR, "too few entries in indexprs list");
2966 if (pos == indexcol)
2970 indexkey = (Node *) lfirst(indexpr_item);
2971 if (indexkey && IsA(indexkey, RelabelType))
2972 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2973 if (equal(node, indexkey))
2975 result = makeVar(INDEX_VAR, indexcol + 1,
2976 exprType(lfirst(indexpr_item)), -1,
2977 exprCollation(lfirst(indexpr_item)),
2979 return (Node *) result;
2982 elog(ERROR, "index key does not match expected index column");
2984 indexpr_item = lnext(indexpr_item);
2989 elog(ERROR, "index key does not match expected index column");
2990 return NULL; /* keep compiler quiet */
2994 * get_switched_clauses
2995 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2996 * extract the bare clauses, and rearrange the elements within the
2997 * clauses, if needed, so the outer join variable is on the left and
2998 * the inner is on the right. The original clause data structure is not
2999 * touched; a modified list is returned. We do, however, set the transient
3000 * outer_is_left field in each RestrictInfo to show which side was which.
3003 get_switched_clauses(List *clauses, Relids outerrelids)
3010 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
3011 OpExpr *clause = (OpExpr *) restrictinfo->clause;
3013 Assert(is_opclause(clause));
3014 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
3017 * Duplicate just enough of the structure to allow commuting the
3018 * clause without changing the original list. Could use
3019 * copyObject, but a complete deep copy is overkill.
3021 OpExpr *temp = makeNode(OpExpr);
3023 temp->opno = clause->opno;
3024 temp->opfuncid = InvalidOid;
3025 temp->opresulttype = clause->opresulttype;
3026 temp->opretset = clause->opretset;
3027 temp->opcollid = clause->opcollid;
3028 temp->inputcollid = clause->inputcollid;
3029 temp->args = list_copy(clause->args);
3030 temp->location = clause->location;
3031 /* Commute it --- note this modifies the temp node in-place. */
3032 CommuteOpExpr(temp);
3033 t_list = lappend(t_list, temp);
3034 restrictinfo->outer_is_left = false;
3038 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
3039 t_list = lappend(t_list, clause);
3040 restrictinfo->outer_is_left = true;
3047 * order_qual_clauses
3048 * Given a list of qual clauses that will all be evaluated at the same
3049 * plan node, sort the list into the order we want to check the quals
3052 * Ideally the order should be driven by a combination of execution cost and
3053 * selectivity, but it's not immediately clear how to account for both,
3054 * and given the uncertainty of the estimates the reliability of the decisions
3055 * would be doubtful anyway. So we just order by estimated per-tuple cost,
3056 * being careful not to change the order when (as is often the case) the
3057 * estimates are identical.
3059 * Although this will work on either bare clauses or RestrictInfos, it's
3060 * much faster to apply it to RestrictInfos, since it can re-use cost
3061 * information that is cached in RestrictInfos.
3063 * Note: some callers pass lists that contain entries that will later be
3064 * removed; this is the easiest way to let this routine see RestrictInfos
3065 * instead of bare clauses. It's OK because we only sort by cost, but
3066 * a cost/selectivity combination would likely do the wrong thing.
3069 order_qual_clauses(PlannerInfo *root, List *clauses)
3076 int nitems = list_length(clauses);
3082 /* No need to work hard for 0 or 1 clause */
3087 * Collect the items and costs into an array. This is to avoid repeated
3088 * cost_qual_eval work if the inputs aren't RestrictInfos.
3090 items = (QualItem *) palloc(nitems * sizeof(QualItem));
3092 foreach(lc, clauses)
3094 Node *clause = (Node *) lfirst(lc);
3097 cost_qual_eval_node(&qcost, clause, root);
3098 items[i].clause = clause;
3099 items[i].cost = qcost.per_tuple;
3104 * Sort. We don't use qsort() because it's not guaranteed stable for
3105 * equal keys. The expected number of entries is small enough that a
3106 * simple insertion sort should be good enough.
3108 for (i = 1; i < nitems; i++)
3110 QualItem newitem = items[i];
3113 /* insert newitem into the already-sorted subarray */
3114 for (j = i; j > 0; j--)
3116 if (newitem.cost >= items[j - 1].cost)
3118 items[j] = items[j - 1];
3123 /* Convert back to a list */
3125 for (i = 0; i < nitems; i++)
3126 result = lappend(result, items[i].clause);
3132 * Copy cost and size info from a Path node to the Plan node created from it.
3133 * The executor usually won't use this info, but it's needed by EXPLAIN.
3136 copy_path_costsize(Plan *dest, Path *src)
3140 dest->startup_cost = src->startup_cost;
3141 dest->total_cost = src->total_cost;
3142 dest->plan_rows = src->rows;
3143 dest->plan_width = src->parent->width;
3147 dest->startup_cost = 0;
3148 dest->total_cost = 0;
3149 dest->plan_rows = 0;
3150 dest->plan_width = 0;
3155 * Copy cost and size info from a lower plan node to an inserted node.
3156 * (Most callers alter the info after copying it.)
3159 copy_plan_costsize(Plan *dest, Plan *src)
3163 dest->startup_cost = src->startup_cost;
3164 dest->total_cost = src->total_cost;
3165 dest->plan_rows = src->plan_rows;
3166 dest->plan_width = src->plan_width;
3170 dest->startup_cost = 0;
3171 dest->total_cost = 0;
3172 dest->plan_rows = 0;
3173 dest->plan_width = 0;
3178 /*****************************************************************************
3180 * PLAN NODE BUILDING ROUTINES
3182 * Some of these are exported because they are called to build plan nodes
3183 * in contexts where we're not deriving the plan node from a path node.
3185 *****************************************************************************/
3188 make_seqscan(List *qptlist,
3192 SeqScan *node = makeNode(SeqScan);
3193 Plan *plan = &node->plan;
3195 /* cost should be inserted by caller */
3196 plan->targetlist = qptlist;
3197 plan->qual = qpqual;
3198 plan->lefttree = NULL;
3199 plan->righttree = NULL;
3200 node->scanrelid = scanrelid;
3206 make_indexscan(List *qptlist,
3211 List *indexqualorig,
3213 List *indexorderbyorig,
3214 ScanDirection indexscandir)
3216 IndexScan *node = makeNode(IndexScan);
3217 Plan *plan = &node->scan.plan;
3219 /* cost should be inserted by caller */
3220 plan->targetlist = qptlist;
3221 plan->qual = qpqual;
3222 plan->lefttree = NULL;
3223 plan->righttree = NULL;
3224 node->scan.scanrelid = scanrelid;
3225 node->indexid = indexid;
3226 node->indexqual = indexqual;
3227 node->indexqualorig = indexqualorig;
3228 node->indexorderby = indexorderby;
3229 node->indexorderbyorig = indexorderbyorig;
3230 node->indexorderdir = indexscandir;
3235 static IndexOnlyScan *
3236 make_indexonlyscan(List *qptlist,
3243 ScanDirection indexscandir)
3245 IndexOnlyScan *node = makeNode(IndexOnlyScan);
3246 Plan *plan = &node->scan.plan;
3248 /* cost should be inserted by caller */
3249 plan->targetlist = qptlist;
3250 plan->qual = qpqual;
3251 plan->lefttree = NULL;
3252 plan->righttree = NULL;
3253 node->scan.scanrelid = scanrelid;
3254 node->indexid = indexid;
3255 node->indexqual = indexqual;
3256 node->indexorderby = indexorderby;
3257 node->indextlist = indextlist;
3258 node->indexorderdir = indexscandir;
3263 static BitmapIndexScan *
3264 make_bitmap_indexscan(Index scanrelid,
3267 List *indexqualorig)
3269 BitmapIndexScan *node = makeNode(BitmapIndexScan);
3270 Plan *plan = &node->scan.plan;
3272 /* cost should be inserted by caller */
3273 plan->targetlist = NIL; /* not used */
3274 plan->qual = NIL; /* not used */
3275 plan->lefttree = NULL;
3276 plan->righttree = NULL;
3277 node->scan.scanrelid = scanrelid;
3278 node->indexid = indexid;
3279 node->indexqual = indexqual;
3280 node->indexqualorig = indexqualorig;
3285 static BitmapHeapScan *
3286 make_bitmap_heapscan(List *qptlist,
3289 List *bitmapqualorig,
3292 BitmapHeapScan *node = makeNode(BitmapHeapScan);
3293 Plan *plan = &node->scan.plan;
3295 /* cost should be inserted by caller */
3296 plan->targetlist = qptlist;
3297 plan->qual = qpqual;
3298 plan->lefttree = lefttree;
3299 plan->righttree = NULL;
3300 node->scan.scanrelid = scanrelid;
3301 node->bitmapqualorig = bitmapqualorig;
3307 make_tidscan(List *qptlist,
3312 TidScan *node = makeNode(TidScan);
3313 Plan *plan = &node->scan.plan;
3315 /* cost should be inserted by caller */
3316 plan->targetlist = qptlist;
3317 plan->qual = qpqual;
3318 plan->lefttree = NULL;
3319 plan->righttree = NULL;
3320 node->scan.scanrelid = scanrelid;
3321 node->tidquals = tidquals;
3327 make_subqueryscan(List *qptlist,
3332 SubqueryScan *node = makeNode(SubqueryScan);
3333 Plan *plan = &node->scan.plan;
3336 * Cost is figured here for the convenience of prepunion.c. Note this is
3337 * only correct for the case where qpqual is empty; otherwise caller
3338 * should overwrite cost with a better estimate.
3340 copy_plan_costsize(plan, subplan);
3341 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
3343 plan->targetlist = qptlist;
3344 plan->qual = qpqual;
3345 plan->lefttree = NULL;
3346 plan->righttree = NULL;
3347 node->scan.scanrelid = scanrelid;
3348 node->subplan = subplan;
3353 static FunctionScan *
3354 make_functionscan(List *qptlist,
3360 List *funccoltypmods,
3361 List *funccolcollations)
3363 FunctionScan *node = makeNode(FunctionScan);
3364 Plan *plan = &node->scan.plan;
3366 /* cost should be inserted by caller */
3367 plan->targetlist = qptlist;
3368 plan->qual = qpqual;
3369 plan->lefttree = NULL;
3370 plan->righttree = NULL;
3371 node->scan.scanrelid = scanrelid;
3372 node->funcexpr = funcexpr;
3373 node->funccolnames = funccolnames;
3374 node->funccoltypes = funccoltypes;
3375 node->funccoltypmods = funccoltypmods;
3376 node->funccolcollations = funccolcollations;
3382 make_valuesscan(List *qptlist,
3387 ValuesScan *node = makeNode(ValuesScan);
3388 Plan *plan = &node->scan.plan;
3390 /* cost should be inserted by caller */
3391 plan->targetlist = qptlist;
3392 plan->qual = qpqual;
3393 plan->lefttree = NULL;
3394 plan->righttree = NULL;
3395 node->scan.scanrelid = scanrelid;
3396 node->values_lists = values_lists;
3402 make_ctescan(List *qptlist,
3408 CteScan *node = makeNode(CteScan);
3409 Plan *plan = &node->scan.plan;
3411 /* cost should be inserted by caller */
3412 plan->targetlist = qptlist;
3413 plan->qual = qpqual;
3414 plan->lefttree = NULL;
3415 plan->righttree = NULL;
3416 node->scan.scanrelid = scanrelid;
3417 node->ctePlanId = ctePlanId;
3418 node->cteParam = cteParam;
3423 static WorkTableScan *
3424 make_worktablescan(List *qptlist,
3429 WorkTableScan *node = makeNode(WorkTableScan);
3430 Plan *plan = &node->scan.plan;
3432 /* cost should be inserted by caller */
3433 plan->targetlist = qptlist;
3434 plan->qual = qpqual;
3435 plan->lefttree = NULL;
3436 plan->righttree = NULL;
3437 node->scan.scanrelid = scanrelid;
3438 node->wtParam = wtParam;
3444 make_foreignscan(List *qptlist,
3450 ForeignScan *node = makeNode(ForeignScan);
3451 Plan *plan = &node->scan.plan;
3453 /* cost will be filled in by create_foreignscan_plan */
3454 plan->targetlist = qptlist;
3455 plan->qual = qpqual;
3456 plan->lefttree = NULL;
3457 plan->righttree = NULL;
3458 node->scan.scanrelid = scanrelid;
3459 node->fdw_exprs = fdw_exprs;
3460 node->fdw_private = fdw_private;
3461 /* fsSystemCol will be filled in by create_foreignscan_plan */
3462 node->fsSystemCol = false;
3468 make_append(List *appendplans, List *tlist)
3470 Append *node = makeNode(Append);
3471 Plan *plan = &node->plan;
3476 * Compute cost as sum of subplan costs. We charge nothing extra for the
3477 * Append itself, which perhaps is too optimistic, but since it doesn't do
3478 * any selection or projection, it is a pretty cheap node.
3480 * If you change this, see also create_append_path(). Also, the size
3481 * calculations should match set_append_rel_pathlist(). It'd be better
3482 * not to duplicate all this logic, but some callers of this function
3483 * aren't working from an appendrel or AppendPath, so there's noplace to
3484 * copy the data from.
3486 plan->startup_cost = 0;
3487 plan->total_cost = 0;
3488 plan->plan_rows = 0;
3490 foreach(subnode, appendplans)
3492 Plan *subplan = (Plan *) lfirst(subnode);
3494 if (subnode == list_head(appendplans)) /* first node? */
3495 plan->startup_cost = subplan->startup_cost;
3496 plan->total_cost += subplan->total_cost;
3497 plan->plan_rows += subplan->plan_rows;
3498 total_size += subplan->plan_width * subplan->plan_rows;
3500 if (plan->plan_rows > 0)
3501 plan->plan_width = rint(total_size / plan->plan_rows);
3503 plan->plan_width = 0;
3505 plan->targetlist = tlist;
3507 plan->lefttree = NULL;
3508 plan->righttree = NULL;
3509 node->appendplans = appendplans;
3515 make_recursive_union(List *tlist,
3522 RecursiveUnion *node = makeNode(RecursiveUnion);
3523 Plan *plan = &node->plan;
3524 int numCols = list_length(distinctList);
3526 cost_recursive_union(plan, lefttree, righttree);
3528 plan->targetlist = tlist;
3530 plan->lefttree = lefttree;
3531 plan->righttree = righttree;
3532 node->wtParam = wtParam;
3535 * convert SortGroupClause list into arrays of attr indexes and equality
3536 * operators, as wanted by executor
3538 node->numCols = numCols;
3542 AttrNumber *dupColIdx;
3546 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3547 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3549 foreach(slitem, distinctList)
3551 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3552 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
3555 dupColIdx[keyno] = tle->resno;
3556 dupOperators[keyno] = sortcl->eqop;
3557 Assert(OidIsValid(dupOperators[keyno]));
3560 node->dupColIdx = dupColIdx;
3561 node->dupOperators = dupOperators;
3563 node->numGroups = numGroups;
3569 make_bitmap_and(List *bitmapplans)
3571 BitmapAnd *node = makeNode(BitmapAnd);
3572 Plan *plan = &node->plan;
3574 /* cost should be inserted by caller */
3575 plan->targetlist = NIL;
3577 plan->lefttree = NULL;
3578 plan->righttree = NULL;
3579 node->bitmapplans = bitmapplans;
3585 make_bitmap_or(List *bitmapplans)
3587 BitmapOr *node = makeNode(BitmapOr);
3588 Plan *plan = &node->plan;
3590 /* cost should be inserted by caller */
3591 plan->targetlist = NIL;
3593 plan->lefttree = NULL;
3594 plan->righttree = NULL;
3595 node->bitmapplans = bitmapplans;
3601 make_nestloop(List *tlist,
3609 NestLoop *node = makeNode(NestLoop);
3610 Plan *plan = &node->join.plan;
3612 /* cost should be inserted by caller */
3613 plan->targetlist = tlist;
3614 plan->qual = otherclauses;
3615 plan->lefttree = lefttree;
3616 plan->righttree = righttree;
3617 node->join.jointype = jointype;
3618 node->join.joinqual = joinclauses;
3619 node->nestParams = nestParams;
3625 make_hashjoin(List *tlist,
3633 HashJoin *node = makeNode(HashJoin);
3634 Plan *plan = &node->join.plan;
3636 /* cost should be inserted by caller */
3637 plan->targetlist = tlist;
3638 plan->qual = otherclauses;
3639 plan->lefttree = lefttree;
3640 plan->righttree = righttree;
3641 node->hashclauses = hashclauses;
3642 node->join.jointype = jointype;
3643 node->join.joinqual = joinclauses;
3649 make_hash(Plan *lefttree,
3651 AttrNumber skewColumn,
3654 int32 skewColTypmod)
3656 Hash *node = makeNode(Hash);
3657 Plan *plan = &node->plan;
3659 copy_plan_costsize(plan, lefttree);
3662 * For plausibility, make startup & total costs equal total cost of input
3663 * plan; this only affects EXPLAIN display not decisions.
3665 plan->startup_cost = plan->total_cost;
3666 plan->targetlist = lefttree->targetlist;
3668 plan->lefttree = lefttree;
3669 plan->righttree = NULL;
3671 node->skewTable = skewTable;
3672 node->skewColumn = skewColumn;
3673 node->skewInherit = skewInherit;
3674 node->skewColType = skewColType;
3675 node->skewColTypmod = skewColTypmod;
3681 make_mergejoin(List *tlist,
3686 Oid *mergecollations,
3687 int *mergestrategies,
3688 bool *mergenullsfirst,
3693 MergeJoin *node = makeNode(MergeJoin);
3694 Plan *plan = &node->join.plan;
3696 /* cost should be inserted by caller */
3697 plan->targetlist = tlist;
3698 plan->qual = otherclauses;
3699 plan->lefttree = lefttree;
3700 plan->righttree = righttree;
3701 node->mergeclauses = mergeclauses;
3702 node->mergeFamilies = mergefamilies;
3703 node->mergeCollations = mergecollations;
3704 node->mergeStrategies = mergestrategies;
3705 node->mergeNullsFirst = mergenullsfirst;
3706 node->join.jointype = jointype;
3707 node->join.joinqual = joinclauses;
3713 * make_sort --- basic routine to build a Sort plan node
3715 * Caller must have built the sortColIdx, sortOperators, collations, and
3716 * nullsFirst arrays already.
3717 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
3720 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
3721 AttrNumber *sortColIdx, Oid *sortOperators,
3722 Oid *collations, bool *nullsFirst,
3723 double limit_tuples)
3725 Sort *node = makeNode(Sort);
3726 Plan *plan = &node->plan;
3727 Path sort_path; /* dummy for result of cost_sort */
3729 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3730 cost_sort(&sort_path, root, NIL,
3731 lefttree->total_cost,
3732 lefttree->plan_rows,
3733 lefttree->plan_width,
3737 plan->startup_cost = sort_path.startup_cost;
3738 plan->total_cost = sort_path.total_cost;
3739 plan->targetlist = lefttree->targetlist;
3741 plan->lefttree = lefttree;
3742 plan->righttree = NULL;
3743 node->numCols = numCols;
3744 node->sortColIdx = sortColIdx;
3745 node->sortOperators = sortOperators;
3746 node->collations = collations;
3747 node->nullsFirst = nullsFirst;
3753 * prepare_sort_from_pathkeys
3754 * Prepare to sort according to given pathkeys
3756 * This is used to set up for both Sort and MergeAppend nodes. It calculates
3757 * the executor's representation of the sort key information, and adjusts the
3758 * plan targetlist if needed to add resjunk sort columns.
3761 * 'lefttree' is the plan node which yields input tuples
3762 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3763 * 'relids' identifies the child relation being sorted, if any
3764 * 'reqColIdx' is NULL or an array of required sort key column numbers
3765 * 'adjust_tlist_in_place' is TRUE if lefttree must be modified in-place
3767 * We must convert the pathkey information into arrays of sort key column
3768 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
3769 * which is the representation the executor wants. These are returned into
3770 * the output parameters *p_numsortkeys etc.
3772 * When looking for matches to an EquivalenceClass's members, we will only
3773 * consider child EC members if they match 'relids'. This protects against
3774 * possible incorrect matches to child expressions that contain no Vars.
3776 * If reqColIdx isn't NULL then it contains sort key column numbers that
3777 * we should match. This is used when making child plans for a MergeAppend;
3778 * it's an error if we can't match the columns.
3780 * If the pathkeys include expressions that aren't simple Vars, we will
3781 * usually need to add resjunk items to the input plan's targetlist to
3782 * compute these expressions, since the Sort/MergeAppend node itself won't
3783 * do any such calculations. If the input plan type isn't one that can do
3784 * projections, this means adding a Result node just to do the projection.
3785 * However, the caller can pass adjust_tlist_in_place = TRUE to force the
3786 * lefttree tlist to be modified in-place regardless of whether the node type
3787 * can project --- we use this for fixing the tlist of MergeAppend itself.
3789 * Returns the node which is to be the input to the Sort (either lefttree,
3790 * or a Result stacked atop lefttree).
3793 prepare_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3795 const AttrNumber *reqColIdx,
3796 bool adjust_tlist_in_place,
3798 AttrNumber **p_sortColIdx,
3799 Oid **p_sortOperators,
3801 bool **p_nullsFirst)
3803 List *tlist = lefttree->targetlist;
3806 AttrNumber *sortColIdx;
3812 * We will need at most list_length(pathkeys) sort columns; possibly less
3814 numsortkeys = list_length(pathkeys);
3815 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3816 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3817 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3818 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3822 foreach(i, pathkeys)
3824 PathKey *pathkey = (PathKey *) lfirst(i);
3825 EquivalenceClass *ec = pathkey->pk_eclass;
3826 EquivalenceMember *em;
3827 TargetEntry *tle = NULL;
3828 Oid pk_datatype = InvalidOid;
3832 if (ec->ec_has_volatile)
3835 * If the pathkey's EquivalenceClass is volatile, then it must
3836 * have come from an ORDER BY clause, and we have to match it to
3837 * that same targetlist entry.
3839 if (ec->ec_sortref == 0) /* can't happen */
3840 elog(ERROR, "volatile EquivalenceClass has no sortref");
3841 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
3843 Assert(list_length(ec->ec_members) == 1);
3844 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
3846 else if (reqColIdx != NULL)
3849 * If we are given a sort column number to match, only consider
3850 * the single TLE at that position. It's possible that there is
3851 * no such TLE, in which case fall through and generate a resjunk
3852 * targetentry (we assume this must have happened in the parent
3853 * plan as well). If there is a TLE but it doesn't match the
3854 * pathkey's EC, we do the same, which is probably the wrong thing
3855 * but we'll leave it to caller to complain about the mismatch.
3857 tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
3860 em = find_ec_member_for_tle(ec, tle, relids);
3863 /* found expr at right place in tlist */
3864 pk_datatype = em->em_datatype;
3873 * Otherwise, we can sort by any non-constant expression listed in
3874 * the pathkey's EquivalenceClass. For now, we take the first
3875 * tlist item found in the EC. If there's no match, we'll generate
3876 * a resjunk entry using the first EC member that is an expression
3877 * in the input's vars. (The non-const restriction only matters
3878 * if the EC is below_outer_join; but if it isn't, it won't
3879 * contain consts anyway, else we'd have discarded the pathkey as
3882 * XXX if we have a choice, is there any way of figuring out which
3883 * might be cheapest to execute? (For example, int4lt is likely
3884 * much cheaper to execute than numericlt, but both might appear
3885 * in the same equivalence class...) Not clear that we ever will
3886 * have an interesting choice in practice, so it may not matter.
3890 tle = (TargetEntry *) lfirst(j);
3891 em = find_ec_member_for_tle(ec, tle, relids);
3894 /* found expr already in tlist */
3895 pk_datatype = em->em_datatype;
3905 * No matching tlist item; look for a computable expression. Note
3906 * that we treat Aggrefs as if they were variables; this is
3907 * necessary when attempting to sort the output from an Agg node
3908 * for use in a WindowFunc (since grouping_planner will have
3909 * treated the Aggrefs as variables, too).
3911 Expr *sortexpr = NULL;
3913 foreach(j, ec->ec_members)
3915 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3920 * We shouldn't be trying to sort by an equivalence class that
3921 * contains a constant, so no need to consider such cases any
3924 if (em->em_is_const)
3928 * Ignore child members unless they match the rel being
3931 if (em->em_is_child &&
3932 !bms_equal(em->em_relids, relids))
3935 sortexpr = em->em_expr;
3936 exprvars = pull_var_clause((Node *) sortexpr,
3937 PVC_INCLUDE_AGGREGATES,
3938 PVC_INCLUDE_PLACEHOLDERS);
3939 foreach(k, exprvars)
3941 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
3944 list_free(exprvars);
3947 pk_datatype = em->em_datatype;
3948 break; /* found usable expression */
3952 elog(ERROR, "could not find pathkey item to sort");
3955 * Do we need to insert a Result node?
3957 if (!adjust_tlist_in_place &&
3958 !is_projection_capable_plan(lefttree))
3960 /* copy needed so we don't modify input's tlist below */
3961 tlist = copyObject(tlist);
3962 lefttree = (Plan *) make_result(root, tlist, NULL,
3966 /* Don't bother testing is_projection_capable_plan again */
3967 adjust_tlist_in_place = true;
3970 * Add resjunk entry to input's tlist
3972 tle = makeTargetEntry(sortexpr,
3973 list_length(tlist) + 1,
3976 tlist = lappend(tlist, tle);
3977 lefttree->targetlist = tlist; /* just in case NIL before */
3981 * Look up the correct sort operator from the PathKey's slightly
3982 * abstracted representation.
3984 sortop = get_opfamily_member(pathkey->pk_opfamily,
3987 pathkey->pk_strategy);
3988 if (!OidIsValid(sortop)) /* should not happen */
3989 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
3990 pathkey->pk_strategy, pk_datatype, pk_datatype,
3991 pathkey->pk_opfamily);
3993 /* Add the column to the sort arrays */
3994 sortColIdx[numsortkeys] = tle->resno;
3995 sortOperators[numsortkeys] = sortop;
3996 collations[numsortkeys] = ec->ec_collation;
3997 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
4001 /* Return results */
4002 *p_numsortkeys = numsortkeys;
4003 *p_sortColIdx = sortColIdx;
4004 *p_sortOperators = sortOperators;
4005 *p_collations = collations;
4006 *p_nullsFirst = nullsFirst;
4012 * find_ec_member_for_tle
4013 * Locate an EquivalenceClass member matching the given TLE, if any
4015 * Child EC members are ignored unless they match 'relids'.
4017 static EquivalenceMember *
4018 find_ec_member_for_tle(EquivalenceClass *ec,
4025 /* We ignore binary-compatible relabeling on both ends */
4027 while (tlexpr && IsA(tlexpr, RelabelType))
4028 tlexpr = ((RelabelType *) tlexpr)->arg;
4030 foreach(lc, ec->ec_members)
4032 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
4036 * We shouldn't be trying to sort by an equivalence class that
4037 * contains a constant, so no need to consider such cases any further.
4039 if (em->em_is_const)
4043 * Ignore child members unless they match the rel being sorted.
4045 if (em->em_is_child &&
4046 !bms_equal(em->em_relids, relids))
4049 /* Match if same expression (after stripping relabel) */
4050 emexpr = em->em_expr;
4051 while (emexpr && IsA(emexpr, RelabelType))
4052 emexpr = ((RelabelType *) emexpr)->arg;
4054 if (equal(emexpr, tlexpr))
4062 * make_sort_from_pathkeys
4063 * Create sort plan to sort according to given pathkeys
4065 * 'lefttree' is the node which yields input tuples
4066 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
4067 * 'limit_tuples' is the bound on the number of output tuples;
4071 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
4072 double limit_tuples)
4075 AttrNumber *sortColIdx;
4080 /* Compute sort column info, and adjust lefttree as needed */
4081 lefttree = prepare_sort_from_pathkeys(root, lefttree, pathkeys,
4091 /* Now build the Sort node */
4092 return make_sort(root, lefttree, numsortkeys,
4093 sortColIdx, sortOperators, collations,
4094 nullsFirst, limit_tuples);
4098 * make_sort_from_sortclauses
4099 * Create sort plan to sort according to given sortclauses
4101 * 'sortcls' is a list of SortGroupClauses
4102 * 'lefttree' is the node which yields input tuples
4105 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
4107 List *sub_tlist = lefttree->targetlist;
4110 AttrNumber *sortColIdx;
4115 /* Convert list-ish representation to arrays wanted by executor */
4116 numsortkeys = list_length(sortcls);
4117 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
4118 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
4119 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
4120 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
4125 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
4126 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
4128 sortColIdx[numsortkeys] = tle->resno;
4129 sortOperators[numsortkeys] = sortcl->sortop;
4130 collations[numsortkeys] = exprCollation((Node *) tle->expr);
4131 nullsFirst[numsortkeys] = sortcl->nulls_first;
4135 return make_sort(root, lefttree, numsortkeys,
4136 sortColIdx, sortOperators, collations,
4141 * make_sort_from_groupcols
4142 * Create sort plan to sort based on grouping columns
4144 * 'groupcls' is the list of SortGroupClauses
4145 * 'grpColIdx' gives the column numbers to use
4147 * This might look like it could be merged with make_sort_from_sortclauses,
4148 * but presently we *must* use the grpColIdx[] array to locate sort columns,
4149 * because the child plan's tlist is not marked with ressortgroupref info
4150 * appropriate to the grouping node. So, only the sort ordering info
4151 * is used from the SortGroupClause entries.
4154 make_sort_from_groupcols(PlannerInfo *root,
4156 AttrNumber *grpColIdx,
4159 List *sub_tlist = lefttree->targetlist;
4162 AttrNumber *sortColIdx;
4167 /* Convert list-ish representation to arrays wanted by executor */
4168 numsortkeys = list_length(groupcls);
4169 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
4170 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
4171 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
4172 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
4175 foreach(l, groupcls)
4177 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
4178 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
4180 sortColIdx[numsortkeys] = tle->resno;
4181 sortOperators[numsortkeys] = grpcl->sortop;
4182 collations[numsortkeys] = exprCollation((Node *) tle->expr);
4183 nullsFirst[numsortkeys] = grpcl->nulls_first;
4187 return make_sort(root, lefttree, numsortkeys,
4188 sortColIdx, sortOperators, collations,
4193 make_material(Plan *lefttree)
4195 Material *node = makeNode(Material);
4196 Plan *plan = &node->plan;
4198 /* cost should be inserted by caller */
4199 plan->targetlist = lefttree->targetlist;
4201 plan->lefttree = lefttree;
4202 plan->righttree = NULL;
4208 * materialize_finished_plan: stick a Material node atop a completed plan
4210 * There are a couple of places where we want to attach a Material node
4211 * after completion of subquery_planner(). This currently requires hackery.
4212 * Since subquery_planner has already run SS_finalize_plan on the subplan
4213 * tree, we have to kluge up parameter lists for the Material node.
4214 * Possibly this could be fixed by postponing SS_finalize_plan processing
4215 * until setrefs.c is run?
4218 materialize_finished_plan(Plan *subplan)
4221 Path matpath; /* dummy for result of cost_material */
4223 matplan = (Plan *) make_material(subplan);
4226 cost_material(&matpath,
4227 subplan->startup_cost,
4228 subplan->total_cost,
4230 subplan->plan_width);
4231 matplan->startup_cost = matpath.startup_cost;
4232 matplan->total_cost = matpath.total_cost;
4233 matplan->plan_rows = subplan->plan_rows;
4234 matplan->plan_width = subplan->plan_width;
4236 /* parameter kluge --- see comments above */
4237 matplan->extParam = bms_copy(subplan->extParam);
4238 matplan->allParam = bms_copy(subplan->allParam);
4244 make_agg(PlannerInfo *root, List *tlist, List *qual,
4245 AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
4246 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
4250 Agg *node = makeNode(Agg);
4251 Plan *plan = &node->plan;
4252 Path agg_path; /* dummy for result of cost_agg */
4255 node->aggstrategy = aggstrategy;
4256 node->numCols = numGroupCols;
4257 node->grpColIdx = grpColIdx;
4258 node->grpOperators = grpOperators;
4259 node->numGroups = numGroups;
4261 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4262 cost_agg(&agg_path, root,
4263 aggstrategy, aggcosts,
4264 numGroupCols, numGroups,
4265 lefttree->startup_cost,
4266 lefttree->total_cost,
4267 lefttree->plan_rows);
4268 plan->startup_cost = agg_path.startup_cost;
4269 plan->total_cost = agg_path.total_cost;
4272 * We will produce a single output tuple if not grouping, and a tuple per
4275 if (aggstrategy == AGG_PLAIN)
4276 plan->plan_rows = 1;
4278 plan->plan_rows = numGroups;
4281 * We also need to account for the cost of evaluation of the qual (ie, the
4282 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
4283 * anything for Aggref nodes; this is okay since they are really
4284 * comparable to Vars.
4286 * See notes in add_tlist_costs_to_plan about why only make_agg,
4287 * make_windowagg and make_group worry about tlist eval cost.
4291 cost_qual_eval(&qual_cost, qual, root);
4292 plan->startup_cost += qual_cost.startup;
4293 plan->total_cost += qual_cost.startup;
4294 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4296 add_tlist_costs_to_plan(root, plan, tlist);
4299 plan->targetlist = tlist;
4300 plan->lefttree = lefttree;
4301 plan->righttree = NULL;
4307 make_windowagg(PlannerInfo *root, List *tlist,
4308 List *windowFuncs, Index winref,
4309 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
4310 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
4311 int frameOptions, Node *startOffset, Node *endOffset,
4314 WindowAgg *node = makeNode(WindowAgg);
4315 Plan *plan = &node->plan;
4316 Path windowagg_path; /* dummy for result of cost_windowagg */
4318 node->winref = winref;
4319 node->partNumCols = partNumCols;
4320 node->partColIdx = partColIdx;
4321 node->partOperators = partOperators;
4322 node->ordNumCols = ordNumCols;
4323 node->ordColIdx = ordColIdx;
4324 node->ordOperators = ordOperators;
4325 node->frameOptions = frameOptions;
4326 node->startOffset = startOffset;
4327 node->endOffset = endOffset;
4329 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4330 cost_windowagg(&windowagg_path, root,
4331 windowFuncs, partNumCols, ordNumCols,
4332 lefttree->startup_cost,
4333 lefttree->total_cost,
4334 lefttree->plan_rows);
4335 plan->startup_cost = windowagg_path.startup_cost;
4336 plan->total_cost = windowagg_path.total_cost;
4339 * We also need to account for the cost of evaluation of the tlist.
4341 * See notes in add_tlist_costs_to_plan about why only make_agg,
4342 * make_windowagg and make_group worry about tlist eval cost.
4344 add_tlist_costs_to_plan(root, plan, tlist);
4346 plan->targetlist = tlist;
4347 plan->lefttree = lefttree;
4348 plan->righttree = NULL;
4349 /* WindowAgg nodes never have a qual clause */
4356 make_group(PlannerInfo *root,
4360 AttrNumber *grpColIdx,
4365 Group *node = makeNode(Group);
4366 Plan *plan = &node->plan;
4367 Path group_path; /* dummy for result of cost_group */
4370 node->numCols = numGroupCols;
4371 node->grpColIdx = grpColIdx;
4372 node->grpOperators = grpOperators;
4374 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4375 cost_group(&group_path, root,
4376 numGroupCols, numGroups,
4377 lefttree->startup_cost,
4378 lefttree->total_cost,
4379 lefttree->plan_rows);
4380 plan->startup_cost = group_path.startup_cost;
4381 plan->total_cost = group_path.total_cost;
4383 /* One output tuple per estimated result group */
4384 plan->plan_rows = numGroups;
4387 * We also need to account for the cost of evaluation of the qual (ie, the
4388 * HAVING clause) and the tlist.
4390 * XXX this double-counts the cost of evaluation of any expressions used
4391 * for grouping, since in reality those will have been evaluated at a
4392 * lower plan level and will only be copied by the Group node. Worth
4395 * See notes in add_tlist_costs_to_plan about why only make_agg,
4396 * make_windowagg and make_group worry about tlist eval cost.
4400 cost_qual_eval(&qual_cost, qual, root);
4401 plan->startup_cost += qual_cost.startup;
4402 plan->total_cost += qual_cost.startup;
4403 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4405 add_tlist_costs_to_plan(root, plan, tlist);
4408 plan->targetlist = tlist;
4409 plan->lefttree = lefttree;
4410 plan->righttree = NULL;
4416 * distinctList is a list of SortGroupClauses, identifying the targetlist items
4417 * that should be considered by the Unique filter. The input path must
4418 * already be sorted accordingly.
4421 make_unique(Plan *lefttree, List *distinctList)
4423 Unique *node = makeNode(Unique);
4424 Plan *plan = &node->plan;
4425 int numCols = list_length(distinctList);
4427 AttrNumber *uniqColIdx;
4431 copy_plan_costsize(plan, lefttree);
4434 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4435 * all columns get compared at most of the tuples. (XXX probably this is
4438 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
4441 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
4442 * we assume the filter removes nothing. The caller must alter this if he
4443 * has a better idea.
4446 plan->targetlist = lefttree->targetlist;
4448 plan->lefttree = lefttree;
4449 plan->righttree = NULL;
4452 * convert SortGroupClause list into arrays of attr indexes and equality
4453 * operators, as wanted by executor
4455 Assert(numCols > 0);
4456 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4457 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4459 foreach(slitem, distinctList)
4461 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4462 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4464 uniqColIdx[keyno] = tle->resno;
4465 uniqOperators[keyno] = sortcl->eqop;
4466 Assert(OidIsValid(uniqOperators[keyno]));
4470 node->numCols = numCols;
4471 node->uniqColIdx = uniqColIdx;
4472 node->uniqOperators = uniqOperators;
4478 * distinctList is a list of SortGroupClauses, identifying the targetlist
4479 * items that should be considered by the SetOp filter. The input path must
4480 * already be sorted accordingly.
4483 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
4484 List *distinctList, AttrNumber flagColIdx, int firstFlag,
4485 long numGroups, double outputRows)
4487 SetOp *node = makeNode(SetOp);
4488 Plan *plan = &node->plan;
4489 int numCols = list_length(distinctList);
4491 AttrNumber *dupColIdx;
4495 copy_plan_costsize(plan, lefttree);
4496 plan->plan_rows = outputRows;
4499 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4500 * all columns get compared at most of the tuples.
4502 plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
4504 plan->targetlist = lefttree->targetlist;
4506 plan->lefttree = lefttree;
4507 plan->righttree = NULL;
4510 * convert SortGroupClause list into arrays of attr indexes and equality
4511 * operators, as wanted by executor
4513 Assert(numCols > 0);
4514 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4515 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4517 foreach(slitem, distinctList)
4519 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4520 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4522 dupColIdx[keyno] = tle->resno;
4523 dupOperators[keyno] = sortcl->eqop;
4524 Assert(OidIsValid(dupOperators[keyno]));
4529 node->strategy = strategy;
4530 node->numCols = numCols;
4531 node->dupColIdx = dupColIdx;
4532 node->dupOperators = dupOperators;
4533 node->flagColIdx = flagColIdx;
4534 node->firstFlag = firstFlag;
4535 node->numGroups = numGroups;
4542 * Build a LockRows plan node
4545 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
4547 LockRows *node = makeNode(LockRows);
4548 Plan *plan = &node->plan;
4550 copy_plan_costsize(plan, lefttree);
4552 /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
4553 plan->total_cost += cpu_tuple_cost * plan->plan_rows;
4555 plan->targetlist = lefttree->targetlist;
4557 plan->lefttree = lefttree;
4558 plan->righttree = NULL;
4560 node->rowMarks = rowMarks;
4561 node->epqParam = epqParam;
4567 * Note: offset_est and count_est are passed in to save having to repeat
4568 * work already done to estimate the values of the limitOffset and limitCount
4569 * expressions. Their values are as returned by preprocess_limit (0 means
4570 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
4571 * with that function!
4574 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
4575 int64 offset_est, int64 count_est)
4577 Limit *node = makeNode(Limit);
4578 Plan *plan = &node->plan;
4580 copy_plan_costsize(plan, lefttree);
4583 * Adjust the output rows count and costs according to the offset/limit.
4584 * This is only a cosmetic issue if we are at top level, but if we are
4585 * building a subquery then it's important to report correct info to the
4588 * When the offset or count couldn't be estimated, use 10% of the
4589 * estimated number of rows emitted from the subplan.
4591 if (offset_est != 0)
4596 offset_rows = (double) offset_est;
4598 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4599 if (offset_rows > plan->plan_rows)
4600 offset_rows = plan->plan_rows;
4601 if (plan->plan_rows > 0)
4602 plan->startup_cost +=
4603 (plan->total_cost - plan->startup_cost)
4604 * offset_rows / plan->plan_rows;
4605 plan->plan_rows -= offset_rows;
4606 if (plan->plan_rows < 1)
4607 plan->plan_rows = 1;
4615 count_rows = (double) count_est;
4617 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4618 if (count_rows > plan->plan_rows)
4619 count_rows = plan->plan_rows;
4620 if (plan->plan_rows > 0)
4621 plan->total_cost = plan->startup_cost +
4622 (plan->total_cost - plan->startup_cost)
4623 * count_rows / plan->plan_rows;
4624 plan->plan_rows = count_rows;
4625 if (plan->plan_rows < 1)
4626 plan->plan_rows = 1;
4629 plan->targetlist = lefttree->targetlist;
4631 plan->lefttree = lefttree;
4632 plan->righttree = NULL;
4634 node->limitOffset = limitOffset;
4635 node->limitCount = limitCount;
4642 * Build a Result plan node
4644 * If we have a subplan, assume that any evaluation costs for the gating qual
4645 * were already factored into the subplan's startup cost, and just copy the
4646 * subplan cost. If there's no subplan, we should include the qual eval
4647 * cost. In either case, tlist eval cost is not to be included here.
4650 make_result(PlannerInfo *root,
4652 Node *resconstantqual,
4655 Result *node = makeNode(Result);
4656 Plan *plan = &node->plan;
4659 copy_plan_costsize(plan, subplan);
4662 plan->startup_cost = 0;
4663 plan->total_cost = cpu_tuple_cost;
4664 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
4665 plan->plan_width = 0; /* XXX is it worth being smarter? */
4666 if (resconstantqual)
4670 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
4671 /* resconstantqual is evaluated once at startup */
4672 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
4673 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
4677 plan->targetlist = tlist;
4679 plan->lefttree = subplan;
4680 plan->righttree = NULL;
4681 node->resconstantqual = resconstantqual;
4688 * Build a ModifyTable plan node
4690 * Currently, we don't charge anything extra for the actual table modification
4691 * work, nor for the RETURNING expressions if any. It would only be window
4692 * dressing, since these are always top-level nodes and there is no way for
4693 * the costs to change any higher-level planning choices. But we might want
4694 * to make it look better sometime.
4697 make_modifytable(CmdType operation, bool canSetTag,
4698 List *resultRelations,
4699 List *subplans, List *returningLists,
4700 List *rowMarks, int epqParam)
4702 ModifyTable *node = makeNode(ModifyTable);
4703 Plan *plan = &node->plan;
4707 Assert(list_length(resultRelations) == list_length(subplans));
4708 Assert(returningLists == NIL ||
4709 list_length(resultRelations) == list_length(returningLists));
4712 * Compute cost as sum of subplan costs.
4714 plan->startup_cost = 0;
4715 plan->total_cost = 0;
4716 plan->plan_rows = 0;
4718 foreach(subnode, subplans)
4720 Plan *subplan = (Plan *) lfirst(subnode);
4722 if (subnode == list_head(subplans)) /* first node? */
4723 plan->startup_cost = subplan->startup_cost;
4724 plan->total_cost += subplan->total_cost;
4725 plan->plan_rows += subplan->plan_rows;
4726 total_size += subplan->plan_width * subplan->plan_rows;
4728 if (plan->plan_rows > 0)
4729 plan->plan_width = rint(total_size / plan->plan_rows);
4731 plan->plan_width = 0;
4733 node->plan.lefttree = NULL;
4734 node->plan.righttree = NULL;
4735 node->plan.qual = NIL;
4736 /* setrefs.c will fill in the targetlist, if needed */
4737 node->plan.targetlist = NIL;
4739 node->operation = operation;
4740 node->canSetTag = canSetTag;
4741 node->resultRelations = resultRelations;
4742 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
4743 node->plans = subplans;
4744 node->returningLists = returningLists;
4745 node->rowMarks = rowMarks;
4746 node->epqParam = epqParam;
4752 * is_projection_capable_plan
4753 * Check whether a given Plan node is able to do projection.
4756 is_projection_capable_plan(Plan *plan)
4758 /* Most plan types can project, so just list the ones that can't */
4759 switch (nodeTag(plan))
4771 case T_RecursiveUnion: