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/predtest.h"
34 #include "optimizer/restrictinfo.h"
35 #include "optimizer/subselect.h"
36 #include "optimizer/tlist.h"
37 #include "optimizer/var.h"
38 #include "parser/parse_clause.h"
39 #include "parser/parsetree.h"
40 #include "utils/lsyscache.h"
43 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path);
44 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
45 static List *build_relation_tlist(RelOptInfo *rel);
46 static bool use_physical_tlist(PlannerInfo *root, RelOptInfo *rel);
47 static void disuse_physical_tlist(Plan *plan, Path *path);
48 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
49 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
50 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
51 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
52 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
53 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
54 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
55 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
56 List *tlist, List *scan_clauses);
57 static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
58 List *tlist, List *scan_clauses, bool indexonly);
59 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
60 BitmapHeapPath *best_path,
61 List *tlist, List *scan_clauses);
62 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
63 List **qual, List **indexqual);
64 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
65 List *tlist, List *scan_clauses);
66 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
67 List *tlist, List *scan_clauses);
68 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
69 List *tlist, List *scan_clauses);
70 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
71 List *tlist, List *scan_clauses);
72 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
73 List *tlist, List *scan_clauses);
74 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
75 List *tlist, List *scan_clauses);
76 static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
77 List *tlist, List *scan_clauses);
78 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
79 Plan *outer_plan, Plan *inner_plan);
80 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
81 Plan *outer_plan, Plan *inner_plan);
82 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
83 Plan *outer_plan, Plan *inner_plan);
84 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
85 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
86 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path);
87 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
88 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
89 static List *get_switched_clauses(List *clauses, Relids outerrelids);
90 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
91 static void copy_path_costsize(Plan *dest, Path *src);
92 static void copy_plan_costsize(Plan *dest, Plan *src);
93 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
94 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
95 Oid indexid, List *indexqual, List *indexqualorig,
96 List *indexorderby, List *indexorderbyorig,
97 ScanDirection indexscandir);
98 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
99 Index scanrelid, Oid indexid,
100 List *indexqual, List *indexorderby,
102 ScanDirection indexscandir);
103 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
105 List *indexqualorig);
106 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
109 List *bitmapqualorig,
111 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
113 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
114 Index scanrelid, Node *funcexpr, List *funccolnames,
115 List *funccoltypes, List *funccoltypmods,
116 List *funccolcollations);
117 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
118 Index scanrelid, List *values_lists);
119 static CteScan *make_ctescan(List *qptlist, List *qpqual,
120 Index scanrelid, int ctePlanId, int cteParam);
121 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
122 Index scanrelid, int wtParam);
123 static ForeignScan *make_foreignscan(List *qptlist, List *qpqual,
124 Index scanrelid, bool fsSystemCol, FdwPlan *fdwplan);
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,
157 bool adjust_tlist_in_place,
159 AttrNumber **p_sortColIdx,
160 Oid **p_sortOperators,
162 bool **p_nullsFirst);
163 static Material *make_material(Plan *lefttree);
168 * Creates the access plan for a query by recursively processing the
169 * desired tree of pathnodes, starting at the node 'best_path'. For
170 * every pathnode found, we create a corresponding plan node containing
171 * appropriate id, target list, and qualification information.
173 * The tlists and quals in the plan tree are still in planner format,
174 * ie, Vars still correspond to the parser's numbering. This will be
175 * fixed later by setrefs.c.
177 * best_path is the best access path
179 * Returns a Plan tree.
182 create_plan(PlannerInfo *root, Path *best_path)
186 /* Initialize this module's private workspace in PlannerInfo */
187 root->curOuterRels = NULL;
188 root->curOuterParams = NIL;
190 /* Recursively process the path tree */
191 plan = create_plan_recurse(root, best_path);
193 /* Check we successfully assigned all NestLoopParams to plan nodes */
194 if (root->curOuterParams != NIL)
195 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
201 * create_plan_recurse
202 * Recursive guts of create_plan().
205 create_plan_recurse(PlannerInfo *root, Path *best_path)
209 switch (best_path->pathtype)
213 case T_IndexOnlyScan:
214 case T_BitmapHeapScan:
220 case T_WorkTableScan:
222 plan = create_scan_plan(root, best_path);
227 plan = create_join_plan(root,
228 (JoinPath *) best_path);
231 plan = create_append_plan(root,
232 (AppendPath *) best_path);
235 plan = create_merge_append_plan(root,
236 (MergeAppendPath *) best_path);
239 plan = (Plan *) create_result_plan(root,
240 (ResultPath *) best_path);
243 plan = (Plan *) create_material_plan(root,
244 (MaterialPath *) best_path);
247 plan = create_unique_plan(root,
248 (UniquePath *) best_path);
251 elog(ERROR, "unrecognized node type: %d",
252 (int) best_path->pathtype);
253 plan = NULL; /* keep compiler quiet */
262 * Create a scan plan for the parent relation of 'best_path'.
265 create_scan_plan(PlannerInfo *root, Path *best_path)
267 RelOptInfo *rel = best_path->parent;
273 * For table scans, rather than using the relation targetlist (which is
274 * only those Vars actually needed by the query), we prefer to generate a
275 * tlist containing all Vars in order. This will allow the executor to
276 * optimize away projection of the table tuples, if possible. (Note that
277 * planner.c may replace the tlist we generate here, forcing projection to
280 if (use_physical_tlist(root, rel))
282 if (best_path->pathtype == T_IndexOnlyScan)
284 /* For index-only scan, the preferred tlist is the index's */
285 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
289 tlist = build_physical_tlist(root, rel);
290 /* if fail because of dropped cols, use regular method */
292 tlist = build_relation_tlist(rel);
296 tlist = build_relation_tlist(rel);
299 * Extract the relevant restriction clauses from the parent relation. The
300 * executor must apply all these restrictions during the scan, except for
301 * pseudoconstants which we'll take care of below.
303 scan_clauses = rel->baserestrictinfo;
305 switch (best_path->pathtype)
308 plan = (Plan *) create_seqscan_plan(root,
315 plan = (Plan *) create_indexscan_plan(root,
316 (IndexPath *) best_path,
322 case T_IndexOnlyScan:
323 plan = (Plan *) create_indexscan_plan(root,
324 (IndexPath *) best_path,
330 case T_BitmapHeapScan:
331 plan = (Plan *) create_bitmap_scan_plan(root,
332 (BitmapHeapPath *) best_path,
338 plan = (Plan *) create_tidscan_plan(root,
339 (TidPath *) best_path,
345 plan = (Plan *) create_subqueryscan_plan(root,
352 plan = (Plan *) create_functionscan_plan(root,
359 plan = (Plan *) create_valuesscan_plan(root,
366 plan = (Plan *) create_ctescan_plan(root,
372 case T_WorkTableScan:
373 plan = (Plan *) create_worktablescan_plan(root,
380 plan = (Plan *) create_foreignscan_plan(root,
381 (ForeignPath *) best_path,
387 elog(ERROR, "unrecognized node type: %d",
388 (int) best_path->pathtype);
389 plan = NULL; /* keep compiler quiet */
394 * If there are any pseudoconstant clauses attached to this node, insert a
395 * gating Result node that evaluates the pseudoconstants as one-time
398 if (root->hasPseudoConstantQuals)
399 plan = create_gating_plan(root, plan, scan_clauses);
405 * Build a target list (ie, a list of TargetEntry) for a relation.
408 build_relation_tlist(RelOptInfo *rel)
414 foreach(v, rel->reltargetlist)
416 /* Do we really need to copy here? Not sure */
417 Node *node = (Node *) copyObject(lfirst(v));
419 tlist = lappend(tlist, makeTargetEntry((Expr *) node,
430 * Decide whether to use a tlist matching relation structure,
431 * rather than only those Vars actually referenced.
434 use_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
440 * We can do this for real relation scans, subquery scans, function scans,
441 * values scans, and CTE scans (but not for, eg, joins).
443 if (rel->rtekind != RTE_RELATION &&
444 rel->rtekind != RTE_SUBQUERY &&
445 rel->rtekind != RTE_FUNCTION &&
446 rel->rtekind != RTE_VALUES &&
447 rel->rtekind != RTE_CTE)
451 * Can't do it with inheritance cases either (mainly because Append
454 if (rel->reloptkind != RELOPT_BASEREL)
458 * Can't do it if any system columns or whole-row Vars are requested.
459 * (This could possibly be fixed but would take some fragile assumptions
460 * in setrefs.c, I think.)
462 for (i = rel->min_attr; i <= 0; i++)
464 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
469 * Can't do it if the rel is required to emit any placeholder expressions,
472 foreach(lc, root->placeholder_list)
474 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
476 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
477 bms_is_subset(phinfo->ph_eval_at, rel->relids))
485 * disuse_physical_tlist
486 * Switch a plan node back to emitting only Vars actually referenced.
488 * If the plan node immediately above a scan would prefer to get only
489 * needed Vars and not a physical tlist, it must call this routine to
490 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
491 * and Material nodes want this, so they don't have to store useless columns.
494 disuse_physical_tlist(Plan *plan, Path *path)
496 /* Only need to undo it for path types handled by create_scan_plan() */
497 switch (path->pathtype)
501 case T_IndexOnlyScan:
502 case T_BitmapHeapScan:
508 case T_WorkTableScan:
510 plan->targetlist = build_relation_tlist(path->parent);
519 * Deal with pseudoconstant qual clauses
521 * If the node's quals list includes any pseudoconstant quals, put them
522 * into a gating Result node atop the already-built plan. Otherwise,
523 * return the plan as-is.
525 * Note that we don't change cost or size estimates when doing gating.
526 * The costs of qual eval were already folded into the plan's startup cost.
527 * Leaving the size alone amounts to assuming that the gating qual will
528 * succeed, which is the conservative estimate for planning upper queries.
529 * We certainly don't want to assume the output size is zero (unless the
530 * gating qual is actually constant FALSE, and that case is dealt with in
531 * clausesel.c). Interpolating between the two cases is silly, because
532 * it doesn't reflect what will really happen at runtime, and besides which
533 * in most cases we have only a very bad idea of the probability of the gating
537 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
539 List *pseudoconstants;
541 /* Sort into desirable execution order while still in RestrictInfo form */
542 quals = order_qual_clauses(root, quals);
544 /* Pull out any pseudoconstant quals from the RestrictInfo list */
545 pseudoconstants = extract_actual_clauses(quals, true);
547 if (!pseudoconstants)
550 return (Plan *) make_result(root,
552 (Node *) pseudoconstants,
558 * Create a join plan for 'best_path' and (recursively) plans for its
559 * inner and outer paths.
562 create_join_plan(PlannerInfo *root, JoinPath *best_path)
567 Relids saveOuterRels = root->curOuterRels;
569 outer_plan = create_plan_recurse(root, best_path->outerjoinpath);
571 /* For a nestloop, include outer relids in curOuterRels for inner side */
572 if (best_path->path.pathtype == T_NestLoop)
573 root->curOuterRels = bms_union(root->curOuterRels,
574 best_path->outerjoinpath->parent->relids);
576 inner_plan = create_plan_recurse(root, best_path->innerjoinpath);
578 switch (best_path->path.pathtype)
581 plan = (Plan *) create_mergejoin_plan(root,
582 (MergePath *) best_path,
587 plan = (Plan *) create_hashjoin_plan(root,
588 (HashPath *) best_path,
593 /* Restore curOuterRels */
594 bms_free(root->curOuterRels);
595 root->curOuterRels = saveOuterRels;
597 plan = (Plan *) create_nestloop_plan(root,
598 (NestPath *) best_path,
603 elog(ERROR, "unrecognized node type: %d",
604 (int) best_path->path.pathtype);
605 plan = NULL; /* keep compiler quiet */
610 * If there are any pseudoconstant clauses attached to this node, insert a
611 * gating Result node that evaluates the pseudoconstants as one-time
614 if (root->hasPseudoConstantQuals)
615 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
620 * * Expensive function pullups may have pulled local predicates * into
621 * this path node. Put them in the qpqual of the plan node. * JMH,
624 if (get_loc_restrictinfo(best_path) != NIL)
625 set_qpqual((Plan) plan,
626 list_concat(get_qpqual((Plan) plan),
627 get_actual_clauses(get_loc_restrictinfo(best_path))));
635 * Create an Append plan for 'best_path' and (recursively) plans
638 * Returns a Plan node.
641 create_append_plan(PlannerInfo *root, AppendPath *best_path)
644 List *tlist = build_relation_tlist(best_path->path.parent);
645 List *subplans = NIL;
649 * It is possible for the subplans list to contain only one entry, or even
650 * no entries. Handle these cases specially.
652 * XXX ideally, if there's just one entry, we'd not bother to generate an
653 * Append node but just return the single child. At the moment this does
654 * not work because the varno of the child scan plan won't match the
655 * parent-rel Vars it'll be asked to emit.
657 if (best_path->subpaths == NIL)
659 /* Generate a Result plan with constant-FALSE gating qual */
660 return (Plan *) make_result(root,
662 (Node *) list_make1(makeBoolConst(false,
667 /* Normal case with multiple subpaths */
668 foreach(subpaths, best_path->subpaths)
670 Path *subpath = (Path *) lfirst(subpaths);
672 subplans = lappend(subplans, create_plan_recurse(root, subpath));
675 plan = make_append(subplans, tlist);
677 return (Plan *) plan;
681 * create_merge_append_plan
682 * Create a MergeAppend plan for 'best_path' and (recursively) plans
685 * Returns a Plan node.
688 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
690 MergeAppend *node = makeNode(MergeAppend);
691 Plan *plan = &node->plan;
692 List *tlist = build_relation_tlist(best_path->path.parent);
693 List *pathkeys = best_path->path.pathkeys;
694 List *subplans = NIL;
698 * We don't have the actual creation of the MergeAppend node split out
699 * into a separate make_xxx function. This is because we want to run
700 * prepare_sort_from_pathkeys on it before we do so on the individual
701 * child plans, to make cross-checking the sort info easier.
703 copy_path_costsize(plan, (Path *) best_path);
704 plan->targetlist = tlist;
706 plan->lefttree = NULL;
707 plan->righttree = NULL;
709 /* Compute sort column info, and adjust MergeAppend's tlist as needed */
710 (void) prepare_sort_from_pathkeys(root, plan, pathkeys,
714 &node->sortOperators,
719 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
720 * even to subplans that don't need an explicit sort, to make sure they
721 * are returning the same sort key columns the MergeAppend expects.
723 foreach(subpaths, best_path->subpaths)
725 Path *subpath = (Path *) lfirst(subpaths);
728 AttrNumber *sortColIdx;
733 /* Build the child plan */
734 subplan = create_plan_recurse(root, subpath);
736 /* Compute sort column info, and adjust subplan's tlist as needed */
737 subplan = prepare_sort_from_pathkeys(root, subplan, pathkeys,
746 * Check that we got the same sort key information. We just Assert
747 * that the sortops match, since those depend only on the pathkeys;
748 * but it seems like a good idea to check the sort column numbers
749 * explicitly, to ensure the tlists really do match up.
751 Assert(numsortkeys == node->numCols);
752 if (memcmp(sortColIdx, node->sortColIdx,
753 numsortkeys * sizeof(AttrNumber)) != 0)
754 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
755 Assert(memcmp(sortOperators, node->sortOperators,
756 numsortkeys * sizeof(Oid)) == 0);
757 Assert(memcmp(collations, node->collations,
758 numsortkeys * sizeof(Oid)) == 0);
759 Assert(memcmp(nullsFirst, node->nullsFirst,
760 numsortkeys * sizeof(bool)) == 0);
762 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
763 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
764 subplan = (Plan *) make_sort(root, subplan, numsortkeys,
765 sortColIdx, sortOperators,
766 collations, nullsFirst,
767 best_path->limit_tuples);
769 subplans = lappend(subplans, subplan);
772 node->mergeplans = subplans;
774 return (Plan *) node;
779 * Create a Result plan for 'best_path'.
780 * This is only used for the case of a query with an empty jointree.
782 * Returns a Plan node.
785 create_result_plan(PlannerInfo *root, ResultPath *best_path)
790 /* The tlist will be installed later, since we have no RelOptInfo */
791 Assert(best_path->path.parent == NULL);
794 /* best_path->quals is just bare clauses */
796 quals = order_qual_clauses(root, best_path->quals);
798 return make_result(root, tlist, (Node *) quals, NULL);
802 * create_material_plan
803 * Create a Material plan for 'best_path' and (recursively) plans
806 * Returns a Plan node.
809 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
814 subplan = create_plan_recurse(root, best_path->subpath);
816 /* We don't want any excess columns in the materialized tuples */
817 disuse_physical_tlist(subplan, best_path->subpath);
819 plan = make_material(subplan);
821 copy_path_costsize(&plan->plan, (Path *) best_path);
828 * Create a Unique plan for 'best_path' and (recursively) plans
831 * Returns a Plan node.
834 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
844 AttrNumber *groupColIdx;
848 subplan = create_plan_recurse(root, best_path->subpath);
850 /* Done if we don't need to do any actual unique-ifying */
851 if (best_path->umethod == UNIQUE_PATH_NOOP)
855 * As constructed, the subplan has a "flat" tlist containing just the Vars
856 * needed here and at upper levels. The values we are supposed to
857 * unique-ify may be expressions in these variables. We have to add any
858 * such expressions to the subplan's tlist.
860 * The subplan may have a "physical" tlist if it is a simple scan plan. If
861 * we're going to sort, this should be reduced to the regular tlist, so
862 * that we don't sort more data than we need to. For hashing, the tlist
863 * should be left as-is if we don't need to add any expressions; but if we
864 * do have to add expressions, then a projection step will be needed at
865 * runtime anyway, so we may as well remove unneeded items. Therefore
866 * newtlist starts from build_relation_tlist() not just a copy of the
867 * subplan's tlist; and we don't install it into the subplan unless we are
868 * sorting or stuff has to be added.
870 in_operators = best_path->in_operators;
871 uniq_exprs = best_path->uniq_exprs;
873 /* initialize modified subplan tlist as just the "required" vars */
874 newtlist = build_relation_tlist(best_path->path.parent);
875 nextresno = list_length(newtlist) + 1;
878 foreach(l, uniq_exprs)
880 Node *uniqexpr = lfirst(l);
883 tle = tlist_member(uniqexpr, newtlist);
886 tle = makeTargetEntry((Expr *) uniqexpr,
890 newtlist = lappend(newtlist, tle);
896 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
899 * If the top plan node can't do projections, we need to add a Result
900 * node to help it along.
902 if (!is_projection_capable_plan(subplan))
903 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
905 subplan->targetlist = newtlist;
909 * Build control information showing which subplan output columns are to
910 * be examined by the grouping step. Unfortunately we can't merge this
911 * with the previous loop, since we didn't then know which version of the
912 * subplan tlist we'd end up using.
914 newtlist = subplan->targetlist;
915 numGroupCols = list_length(uniq_exprs);
916 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
919 foreach(l, uniq_exprs)
921 Node *uniqexpr = lfirst(l);
924 tle = tlist_member(uniqexpr, newtlist);
925 if (!tle) /* shouldn't happen */
926 elog(ERROR, "failed to find unique expression in subplan tlist");
927 groupColIdx[groupColPos++] = tle->resno;
930 if (best_path->umethod == UNIQUE_PATH_HASH)
935 numGroups = (long) Min(best_path->path.rows, (double) LONG_MAX);
938 * Get the hashable equality operators for the Agg node to use.
939 * Normally these are the same as the IN clause operators, but if
940 * those are cross-type operators then the equality operators are the
941 * ones for the IN clause operators' RHS datatype.
943 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
945 foreach(l, in_operators)
947 Oid in_oper = lfirst_oid(l);
950 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
951 elog(ERROR, "could not find compatible hash operator for operator %u",
953 groupOperators[groupColPos++] = eq_oper;
957 * Since the Agg node is going to project anyway, we can give it the
958 * minimum output tlist, without any stuff we might have added to the
961 plan = (Plan *) make_agg(root,
962 build_relation_tlist(best_path->path.parent),
974 List *sortList = NIL;
976 /* Create an ORDER BY list to sort the input compatibly */
978 foreach(l, in_operators)
980 Oid in_oper = lfirst_oid(l);
984 SortGroupClause *sortcl;
986 sortop = get_ordering_op_for_equality_op(in_oper, false);
987 if (!OidIsValid(sortop)) /* shouldn't happen */
988 elog(ERROR, "could not find ordering operator for equality operator %u",
992 * The Unique node will need equality operators. Normally these
993 * are the same as the IN clause operators, but if those are
994 * cross-type operators then the equality operators are the ones
995 * for the IN clause operators' RHS datatype.
997 eqop = get_equality_op_for_ordering_op(sortop, NULL);
998 if (!OidIsValid(eqop)) /* shouldn't happen */
999 elog(ERROR, "could not find equality operator for ordering operator %u",
1002 tle = get_tle_by_resno(subplan->targetlist,
1003 groupColIdx[groupColPos]);
1004 Assert(tle != NULL);
1006 sortcl = makeNode(SortGroupClause);
1007 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
1008 subplan->targetlist);
1009 sortcl->eqop = eqop;
1010 sortcl->sortop = sortop;
1011 sortcl->nulls_first = false;
1012 sortcl->hashable = false; /* no need to make this accurate */
1013 sortList = lappend(sortList, sortcl);
1016 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
1017 plan = (Plan *) make_unique(plan, sortList);
1020 /* Adjust output size estimate (other fields should be OK already) */
1021 plan->plan_rows = best_path->path.rows;
1027 /*****************************************************************************
1029 * BASE-RELATION SCAN METHODS
1031 *****************************************************************************/
1035 * create_seqscan_plan
1036 * Returns a seqscan plan for the base relation scanned by 'best_path'
1037 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1040 create_seqscan_plan(PlannerInfo *root, Path *best_path,
1041 List *tlist, List *scan_clauses)
1044 Index scan_relid = best_path->parent->relid;
1046 /* it should be a base rel... */
1047 Assert(scan_relid > 0);
1048 Assert(best_path->parent->rtekind == RTE_RELATION);
1050 /* Sort clauses into best execution order */
1051 scan_clauses = order_qual_clauses(root, scan_clauses);
1053 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1054 scan_clauses = extract_actual_clauses(scan_clauses, false);
1056 scan_plan = make_seqscan(tlist,
1060 copy_path_costsize(&scan_plan->plan, best_path);
1066 * create_indexscan_plan
1067 * Returns an indexscan plan for the base relation scanned by 'best_path'
1068 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1070 * We use this for both plain IndexScans and IndexOnlyScans, because the
1071 * qual preprocessing work is the same for both. Note that the caller tells
1072 * us which to build --- we don't look at best_path->path.pathtype, because
1073 * create_bitmap_subplan needs to be able to override the prior decision.
1076 create_indexscan_plan(PlannerInfo *root,
1077 IndexPath *best_path,
1083 List *indexquals = best_path->indexquals;
1084 List *indexorderbys = best_path->indexorderbys;
1085 Index baserelid = best_path->path.parent->relid;
1086 Oid indexoid = best_path->indexinfo->indexoid;
1088 List *stripped_indexquals;
1089 List *fixed_indexquals;
1090 List *fixed_indexorderbys;
1093 /* it should be a base rel... */
1094 Assert(baserelid > 0);
1095 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1098 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
1099 * executor as indexqualorig
1101 stripped_indexquals = get_actual_clauses(indexquals);
1104 * The executor needs a copy with the indexkey on the left of each clause
1105 * and with index Vars substituted for table ones.
1107 fixed_indexquals = fix_indexqual_references(root, best_path);
1110 * Likewise fix up index attr references in the ORDER BY expressions.
1112 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
1115 * If this is a parameterized scan, the indexclauses will contain join
1116 * clauses that are not present in scan_clauses (since the passed-in value
1117 * is just the rel's baserestrictinfo list). We must add these clauses to
1118 * scan_clauses to ensure they get checked. In most cases we will remove
1119 * the join clauses again below, but if a join clause contains a special
1120 * operator, we need to make sure it gets into the scan_clauses.
1122 * Note: pointer comparison should be enough to determine RestrictInfo
1125 if (best_path->path.required_outer)
1126 scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
1129 * The qpqual list must contain all restrictions not automatically handled
1130 * by the index. All the predicates in the indexquals will be checked
1131 * (either by the index itself, or by nodeIndexscan.c), but if there are
1132 * any "special" operators involved then they must be included in qpqual.
1133 * The upshot is that qpqual must contain scan_clauses minus whatever
1134 * appears in indexquals.
1136 * In normal cases simple pointer equality checks will be enough to spot
1137 * duplicate RestrictInfos, so we try that first. In some situations
1138 * (particularly with OR'd index conditions) we may have scan_clauses that
1139 * are not equal to, but are logically implied by, the index quals; so we
1140 * also try a predicate_implied_by() check to see if we can discard quals
1141 * that way. (predicate_implied_by assumes its first input contains only
1142 * immutable functions, so we have to check that.)
1144 * We can also discard quals that are implied by a partial index's
1145 * predicate, but only in a plain SELECT; when scanning a target relation
1146 * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
1147 * plan so that they'll be properly rechecked by EvalPlanQual testing.
1150 foreach(l, scan_clauses)
1152 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1154 Assert(IsA(rinfo, RestrictInfo));
1155 if (rinfo->pseudoconstant)
1156 continue; /* we may drop pseudoconstants here */
1157 if (list_member_ptr(indexquals, rinfo))
1159 if (!contain_mutable_functions((Node *) rinfo->clause))
1161 List *clausel = list_make1(rinfo->clause);
1163 if (predicate_implied_by(clausel, indexquals))
1165 if (best_path->indexinfo->indpred)
1167 if (baserelid != root->parse->resultRelation &&
1168 get_parse_rowmark(root->parse, baserelid) == NULL)
1169 if (predicate_implied_by(clausel,
1170 best_path->indexinfo->indpred))
1174 qpqual = lappend(qpqual, rinfo);
1177 /* Sort clauses into best execution order */
1178 qpqual = order_qual_clauses(root, qpqual);
1180 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1181 qpqual = extract_actual_clauses(qpqual, false);
1184 * We have to replace any outer-relation variables with nestloop params in
1185 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
1186 * annoying to have to do this separately from the processing in
1187 * fix_indexqual_references --- rethink this when generalizing the inner
1188 * indexscan support. But note we can't really do this earlier because
1189 * it'd break the comparisons to predicates above ... (or would it? Those
1190 * wouldn't have outer refs)
1192 if (best_path->path.required_outer)
1194 stripped_indexquals = (List *)
1195 replace_nestloop_params(root, (Node *) stripped_indexquals);
1197 replace_nestloop_params(root, (Node *) qpqual);
1198 indexorderbys = (List *)
1199 replace_nestloop_params(root, (Node *) indexorderbys);
1202 /* Finally ready to build the plan node */
1204 scan_plan = (Scan *) make_indexonlyscan(tlist,
1209 fixed_indexorderbys,
1210 best_path->indexinfo->indextlist,
1211 best_path->indexscandir);
1213 scan_plan = (Scan *) make_indexscan(tlist,
1218 stripped_indexquals,
1219 fixed_indexorderbys,
1221 best_path->indexscandir);
1223 copy_path_costsize(&scan_plan->plan, &best_path->path);
1229 * create_bitmap_scan_plan
1230 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
1231 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1233 static BitmapHeapScan *
1234 create_bitmap_scan_plan(PlannerInfo *root,
1235 BitmapHeapPath *best_path,
1239 Index baserelid = best_path->path.parent->relid;
1240 Plan *bitmapqualplan;
1241 List *bitmapqualorig;
1245 BitmapHeapScan *scan_plan;
1247 /* it should be a base rel... */
1248 Assert(baserelid > 0);
1249 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1251 /* Process the bitmapqual tree into a Plan tree and qual lists */
1252 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1253 &bitmapqualorig, &indexquals);
1255 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1256 scan_clauses = extract_actual_clauses(scan_clauses, false);
1259 * If this is a parameterized scan, the indexclauses will contain join clauses
1260 * that are not present in scan_clauses (since the passed-in value is just
1261 * the rel's baserestrictinfo list). We must add these clauses to
1262 * scan_clauses to ensure they get checked. In most cases we will remove
1263 * the join clauses again below, but if a join clause contains a special
1264 * operator, we need to make sure it gets into the scan_clauses.
1266 if (best_path->path.required_outer)
1268 scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
1272 * The qpqual list must contain all restrictions not automatically handled
1273 * by the index. All the predicates in the indexquals will be checked
1274 * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
1275 * are any "special" operators involved then they must be added to qpqual.
1276 * The upshot is that qpqual must contain scan_clauses minus whatever
1277 * appears in indexquals.
1279 * In normal cases simple equal() checks will be enough to spot duplicate
1280 * clauses, so we try that first. In some situations (particularly with
1281 * OR'd index conditions) we may have scan_clauses that are not equal to,
1282 * but are logically implied by, the index quals; so we also try a
1283 * predicate_implied_by() check to see if we can discard quals that way.
1284 * (predicate_implied_by assumes its first input contains only immutable
1285 * functions, so we have to check that.)
1287 * Unlike create_indexscan_plan(), we need take no special thought here
1288 * for partial index predicates; this is because the predicate conditions
1289 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1290 * to do it that way because predicate conditions need to be rechecked if
1291 * the scan becomes lossy.
1294 foreach(l, scan_clauses)
1296 Node *clause = (Node *) lfirst(l);
1298 if (list_member(indexquals, clause))
1300 if (!contain_mutable_functions(clause))
1302 List *clausel = list_make1(clause);
1304 if (predicate_implied_by(clausel, indexquals))
1307 qpqual = lappend(qpqual, clause);
1310 /* Sort clauses into best execution order */
1311 qpqual = order_qual_clauses(root, qpqual);
1314 * When dealing with special operators, we will at this point have
1315 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
1316 * 'em from bitmapqualorig, since there's no point in making the tests
1319 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1321 /* Finally ready to build the plan node */
1322 scan_plan = make_bitmap_heapscan(tlist,
1328 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1334 * Given a bitmapqual tree, generate the Plan tree that implements it
1336 * As byproducts, we also return in *qual and *indexqual the qual lists
1337 * (in implicit-AND form, without RestrictInfos) describing the original index
1338 * conditions and the generated indexqual conditions. (These are the same in
1339 * simple cases, but when special index operators are involved, the former
1340 * list includes the special conditions while the latter includes the actual
1341 * indexable conditions derived from them.) Both lists include partial-index
1342 * predicates, because we have to recheck predicates as well as index
1343 * conditions if the bitmap scan becomes lossy.
1345 * Note: if you find yourself changing this, you probably need to change
1346 * make_restrictinfo_from_bitmapqual too.
1349 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1350 List **qual, List **indexqual)
1354 if (IsA(bitmapqual, BitmapAndPath))
1356 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1357 List *subplans = NIL;
1358 List *subquals = NIL;
1359 List *subindexquals = NIL;
1363 * There may well be redundant quals among the subplans, since a
1364 * top-level WHERE qual might have gotten used to form several
1365 * different index quals. We don't try exceedingly hard to eliminate
1366 * redundancies, but we do eliminate obvious duplicates by using
1367 * list_concat_unique.
1369 foreach(l, apath->bitmapquals)
1375 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1376 &subqual, &subindexqual);
1377 subplans = lappend(subplans, subplan);
1378 subquals = list_concat_unique(subquals, subqual);
1379 subindexquals = list_concat_unique(subindexquals, subindexqual);
1381 plan = (Plan *) make_bitmap_and(subplans);
1382 plan->startup_cost = apath->path.startup_cost;
1383 plan->total_cost = apath->path.total_cost;
1385 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1386 plan->plan_width = 0; /* meaningless */
1388 *indexqual = subindexquals;
1390 else if (IsA(bitmapqual, BitmapOrPath))
1392 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1393 List *subplans = NIL;
1394 List *subquals = NIL;
1395 List *subindexquals = NIL;
1396 bool const_true_subqual = false;
1397 bool const_true_subindexqual = false;
1401 * Here, we only detect qual-free subplans. A qual-free subplan would
1402 * cause us to generate "... OR true ..." which we may as well reduce
1403 * to just "true". We do not try to eliminate redundant subclauses
1404 * because (a) it's not as likely as in the AND case, and (b) we might
1405 * well be working with hundreds or even thousands of OR conditions,
1406 * perhaps from a long IN list. The performance of list_append_unique
1407 * would be unacceptable.
1409 foreach(l, opath->bitmapquals)
1415 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1416 &subqual, &subindexqual);
1417 subplans = lappend(subplans, subplan);
1419 const_true_subqual = true;
1420 else if (!const_true_subqual)
1421 subquals = lappend(subquals,
1422 make_ands_explicit(subqual));
1423 if (subindexqual == NIL)
1424 const_true_subindexqual = true;
1425 else if (!const_true_subindexqual)
1426 subindexquals = lappend(subindexquals,
1427 make_ands_explicit(subindexqual));
1431 * In the presence of ScalarArrayOpExpr quals, we might have built
1432 * BitmapOrPaths with just one subpath; don't add an OR step.
1434 if (list_length(subplans) == 1)
1436 plan = (Plan *) linitial(subplans);
1440 plan = (Plan *) make_bitmap_or(subplans);
1441 plan->startup_cost = opath->path.startup_cost;
1442 plan->total_cost = opath->path.total_cost;
1444 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1445 plan->plan_width = 0; /* meaningless */
1449 * If there were constant-TRUE subquals, the OR reduces to constant
1450 * TRUE. Also, avoid generating one-element ORs, which could happen
1451 * due to redundancy elimination or ScalarArrayOpExpr quals.
1453 if (const_true_subqual)
1455 else if (list_length(subquals) <= 1)
1458 *qual = list_make1(make_orclause(subquals));
1459 if (const_true_subindexqual)
1461 else if (list_length(subindexquals) <= 1)
1462 *indexqual = subindexquals;
1464 *indexqual = list_make1(make_orclause(subindexquals));
1466 else if (IsA(bitmapqual, IndexPath))
1468 IndexPath *ipath = (IndexPath *) bitmapqual;
1472 /* Use the regular indexscan plan build machinery... */
1473 iscan = (IndexScan *) create_indexscan_plan(root, ipath,
1475 Assert(IsA(iscan, IndexScan));
1476 /* then convert to a bitmap indexscan */
1477 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1480 iscan->indexqualorig);
1481 plan->startup_cost = 0.0;
1482 plan->total_cost = ipath->indextotalcost;
1484 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1485 plan->plan_width = 0; /* meaningless */
1486 *qual = get_actual_clauses(ipath->indexclauses);
1487 *indexqual = get_actual_clauses(ipath->indexquals);
1488 foreach(l, ipath->indexinfo->indpred)
1490 Expr *pred = (Expr *) lfirst(l);
1493 * We know that the index predicate must have been implied by the
1494 * query condition as a whole, but it may or may not be implied by
1495 * the conditions that got pushed into the bitmapqual. Avoid
1496 * generating redundant conditions.
1498 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1500 *qual = lappend(*qual, pred);
1501 *indexqual = lappend(*indexqual, pred);
1506 * Replace outer-relation variables with nestloop params, but only
1507 * after doing the above comparisons to index predicates.
1509 if (ipath->path.required_outer)
1512 replace_nestloop_params(root, (Node *) *qual);
1513 *indexqual = (List *)
1514 replace_nestloop_params(root, (Node *) *indexqual);
1519 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1520 plan = NULL; /* keep compiler quiet */
1527 * create_tidscan_plan
1528 * Returns a tidscan plan for the base relation scanned by 'best_path'
1529 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1532 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1533 List *tlist, List *scan_clauses)
1536 Index scan_relid = best_path->path.parent->relid;
1539 /* it should be a base rel... */
1540 Assert(scan_relid > 0);
1541 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1543 /* Sort clauses into best execution order */
1544 scan_clauses = order_qual_clauses(root, scan_clauses);
1546 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1547 scan_clauses = extract_actual_clauses(scan_clauses, false);
1550 * Remove any clauses that are TID quals. This is a bit tricky since the
1551 * tidquals list has implicit OR semantics.
1553 ortidquals = best_path->tidquals;
1554 if (list_length(ortidquals) > 1)
1555 ortidquals = list_make1(make_orclause(ortidquals));
1556 scan_clauses = list_difference(scan_clauses, ortidquals);
1558 scan_plan = make_tidscan(tlist,
1561 best_path->tidquals);
1563 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1569 * create_subqueryscan_plan
1570 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1571 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1573 static SubqueryScan *
1574 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1575 List *tlist, List *scan_clauses)
1577 SubqueryScan *scan_plan;
1578 Index scan_relid = best_path->parent->relid;
1580 /* it should be a subquery base rel... */
1581 Assert(scan_relid > 0);
1582 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1584 /* Sort clauses into best execution order */
1585 scan_clauses = order_qual_clauses(root, scan_clauses);
1587 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1588 scan_clauses = extract_actual_clauses(scan_clauses, false);
1590 scan_plan = make_subqueryscan(tlist,
1593 best_path->parent->subplan);
1595 copy_path_costsize(&scan_plan->scan.plan, best_path);
1601 * create_functionscan_plan
1602 * Returns a functionscan plan for the base relation scanned by 'best_path'
1603 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1605 static FunctionScan *
1606 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1607 List *tlist, List *scan_clauses)
1609 FunctionScan *scan_plan;
1610 Index scan_relid = best_path->parent->relid;
1613 /* it should be a function base rel... */
1614 Assert(scan_relid > 0);
1615 rte = planner_rt_fetch(scan_relid, root);
1616 Assert(rte->rtekind == RTE_FUNCTION);
1618 /* Sort clauses into best execution order */
1619 scan_clauses = order_qual_clauses(root, scan_clauses);
1621 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1622 scan_clauses = extract_actual_clauses(scan_clauses, false);
1624 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1626 rte->eref->colnames,
1628 rte->funccoltypmods,
1629 rte->funccolcollations);
1631 copy_path_costsize(&scan_plan->scan.plan, best_path);
1637 * create_valuesscan_plan
1638 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1639 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1642 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1643 List *tlist, List *scan_clauses)
1645 ValuesScan *scan_plan;
1646 Index scan_relid = best_path->parent->relid;
1649 /* it should be a values base rel... */
1650 Assert(scan_relid > 0);
1651 rte = planner_rt_fetch(scan_relid, root);
1652 Assert(rte->rtekind == RTE_VALUES);
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 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1663 copy_path_costsize(&scan_plan->scan.plan, best_path);
1669 * create_ctescan_plan
1670 * Returns a ctescan plan for the base relation scanned by 'best_path'
1671 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1674 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1675 List *tlist, List *scan_clauses)
1678 Index scan_relid = best_path->parent->relid;
1680 SubPlan *ctesplan = NULL;
1683 PlannerInfo *cteroot;
1688 Assert(scan_relid > 0);
1689 rte = planner_rt_fetch(scan_relid, root);
1690 Assert(rte->rtekind == RTE_CTE);
1691 Assert(!rte->self_reference);
1694 * Find the referenced CTE, and locate the SubPlan previously made for it.
1696 levelsup = rte->ctelevelsup;
1698 while (levelsup-- > 0)
1700 cteroot = cteroot->parent_root;
1701 if (!cteroot) /* shouldn't happen */
1702 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1706 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1707 * on planning the CTEs (ie, this is a side-reference from another CTE).
1708 * So we mustn't use forboth here.
1711 foreach(lc, cteroot->parse->cteList)
1713 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1715 if (strcmp(cte->ctename, rte->ctename) == 0)
1719 if (lc == NULL) /* shouldn't happen */
1720 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1721 if (ndx >= list_length(cteroot->cte_plan_ids))
1722 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1723 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1724 Assert(plan_id > 0);
1725 foreach(lc, cteroot->init_plans)
1727 ctesplan = (SubPlan *) lfirst(lc);
1728 if (ctesplan->plan_id == plan_id)
1731 if (lc == NULL) /* shouldn't happen */
1732 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1735 * We need the CTE param ID, which is the sole member of the SubPlan's
1738 cte_param_id = linitial_int(ctesplan->setParam);
1740 /* Sort clauses into best execution order */
1741 scan_clauses = order_qual_clauses(root, scan_clauses);
1743 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1744 scan_clauses = extract_actual_clauses(scan_clauses, false);
1746 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1747 plan_id, cte_param_id);
1749 copy_path_costsize(&scan_plan->scan.plan, best_path);
1755 * create_worktablescan_plan
1756 * Returns a worktablescan plan for the base relation scanned by 'best_path'
1757 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1759 static WorkTableScan *
1760 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1761 List *tlist, List *scan_clauses)
1763 WorkTableScan *scan_plan;
1764 Index scan_relid = best_path->parent->relid;
1767 PlannerInfo *cteroot;
1769 Assert(scan_relid > 0);
1770 rte = planner_rt_fetch(scan_relid, root);
1771 Assert(rte->rtekind == RTE_CTE);
1772 Assert(rte->self_reference);
1775 * We need to find the worktable param ID, which is in the plan level
1776 * that's processing the recursive UNION, which is one level *below* where
1777 * the CTE comes from.
1779 levelsup = rte->ctelevelsup;
1780 if (levelsup == 0) /* shouldn't happen */
1781 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1784 while (levelsup-- > 0)
1786 cteroot = cteroot->parent_root;
1787 if (!cteroot) /* shouldn't happen */
1788 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1790 if (cteroot->wt_param_id < 0) /* shouldn't happen */
1791 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1793 /* Sort clauses into best execution order */
1794 scan_clauses = order_qual_clauses(root, scan_clauses);
1796 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1797 scan_clauses = extract_actual_clauses(scan_clauses, false);
1799 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
1800 cteroot->wt_param_id);
1802 copy_path_costsize(&scan_plan->scan.plan, best_path);
1808 * create_foreignscan_plan
1809 * Returns a foreignscan plan for the base relation scanned by 'best_path'
1810 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1812 static ForeignScan *
1813 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
1814 List *tlist, List *scan_clauses)
1816 ForeignScan *scan_plan;
1817 RelOptInfo *rel = best_path->path.parent;
1818 Index scan_relid = rel->relid;
1823 /* it should be a base rel... */
1824 Assert(scan_relid > 0);
1825 Assert(rel->rtekind == RTE_RELATION);
1826 rte = planner_rt_fetch(scan_relid, root);
1827 Assert(rte->rtekind == RTE_RELATION);
1829 /* Sort clauses into best execution order */
1830 scan_clauses = order_qual_clauses(root, scan_clauses);
1832 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1833 scan_clauses = extract_actual_clauses(scan_clauses, false);
1835 /* Detect whether any system columns are requested from rel */
1836 fsSystemCol = false;
1837 for (i = rel->min_attr; i < 0; i++)
1839 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
1846 scan_plan = make_foreignscan(tlist,
1850 best_path->fdwplan);
1852 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1858 /*****************************************************************************
1862 *****************************************************************************/
1865 create_nestloop_plan(PlannerInfo *root,
1866 NestPath *best_path,
1870 NestLoop *join_plan;
1871 List *tlist = build_relation_tlist(best_path->path.parent);
1872 List *joinrestrictclauses = best_path->joinrestrictinfo;
1882 * If the inner path is parameterized, it might have already used some of
1883 * the join quals, in which case we don't have to check them again at the
1884 * join node. Remove any join quals that are redundant.
1886 joinrestrictclauses =
1887 select_nonredundant_join_clauses(joinrestrictclauses,
1888 best_path->innerjoinpath->param_clauses);
1890 /* Sort join qual clauses into best execution order */
1891 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
1893 /* Get the join qual clauses (in plain expression form) */
1894 /* Any pseudoconstant clauses are ignored here */
1895 if (IS_OUTER_JOIN(best_path->jointype))
1897 extract_actual_join_clauses(joinrestrictclauses,
1898 &joinclauses, &otherclauses);
1902 /* We can treat all clauses alike for an inner join */
1903 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
1908 * Identify any nestloop parameters that should be supplied by this join
1909 * node, and move them from root->curOuterParams to the nestParams list.
1911 outerrelids = best_path->outerjoinpath->parent->relids;
1914 for (cell = list_head(root->curOuterParams); cell; cell = next)
1916 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
1919 if (IsA(nlp->paramval, Var) &&
1920 bms_is_member(nlp->paramval->varno, outerrelids))
1922 root->curOuterParams = list_delete_cell(root->curOuterParams,
1924 nestParams = lappend(nestParams, nlp);
1926 else if (IsA(nlp->paramval, PlaceHolderVar) &&
1927 bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
1929 bms_is_subset(find_placeholder_info(root,
1930 (PlaceHolderVar *) nlp->paramval,
1934 root->curOuterParams = list_delete_cell(root->curOuterParams,
1936 nestParams = lappend(nestParams, nlp);
1942 join_plan = make_nestloop(tlist,
1948 best_path->jointype);
1950 copy_path_costsize(&join_plan->join.plan, &best_path->path);
1956 create_mergejoin_plan(PlannerInfo *root,
1957 MergePath *best_path,
1961 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1965 List *outerpathkeys;
1966 List *innerpathkeys;
1969 Oid *mergecollations;
1970 int *mergestrategies;
1971 bool *mergenullsfirst;
1972 MergeJoin *join_plan;
1978 /* Sort join qual clauses into best execution order */
1979 /* NB: do NOT reorder the mergeclauses */
1980 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1982 /* Get the join qual clauses (in plain expression form) */
1983 /* Any pseudoconstant clauses are ignored here */
1984 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1986 extract_actual_join_clauses(joinclauses,
1987 &joinclauses, &otherclauses);
1991 /* We can treat all clauses alike for an inner join */
1992 joinclauses = extract_actual_clauses(joinclauses, false);
1997 * Remove the mergeclauses from the list of join qual clauses, leaving the
1998 * list of quals that must be checked as qpquals.
2000 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
2001 joinclauses = list_difference(joinclauses, mergeclauses);
2004 * Rearrange mergeclauses, if needed, so that the outer variable is always
2005 * on the left; mark the mergeclause restrictinfos with correct
2006 * outer_is_left status.
2008 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
2009 best_path->jpath.outerjoinpath->parent->relids);
2012 * Create explicit sort nodes for the outer and inner paths if necessary.
2013 * Make sure there are no excess columns in the inputs if sorting.
2015 if (best_path->outersortkeys)
2017 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
2018 outer_plan = (Plan *)
2019 make_sort_from_pathkeys(root,
2021 best_path->outersortkeys,
2023 outerpathkeys = best_path->outersortkeys;
2026 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
2028 if (best_path->innersortkeys)
2030 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
2031 inner_plan = (Plan *)
2032 make_sort_from_pathkeys(root,
2034 best_path->innersortkeys,
2036 innerpathkeys = best_path->innersortkeys;
2039 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
2042 * If specified, add a materialize node to shield the inner plan from the
2043 * need to handle mark/restore.
2045 if (best_path->materialize_inner)
2047 Plan *matplan = (Plan *) make_material(inner_plan);
2050 * We assume the materialize will not spill to disk, and therefore
2051 * charge just cpu_operator_cost per tuple. (Keep this estimate in
2052 * sync with final_cost_mergejoin.)
2054 copy_plan_costsize(matplan, inner_plan);
2055 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
2057 inner_plan = matplan;
2061 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
2062 * executor. The information is in the pathkeys for the two inputs, but
2063 * we need to be careful about the possibility of mergeclauses sharing a
2064 * pathkey (compare find_mergeclauses_for_pathkeys()).
2066 nClauses = list_length(mergeclauses);
2067 Assert(nClauses == list_length(best_path->path_mergeclauses));
2068 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
2069 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
2070 mergestrategies = (int *) palloc(nClauses * sizeof(int));
2071 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
2073 lop = list_head(outerpathkeys);
2074 lip = list_head(innerpathkeys);
2076 foreach(lc, best_path->path_mergeclauses)
2078 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2079 EquivalenceClass *oeclass;
2080 EquivalenceClass *ieclass;
2083 EquivalenceClass *opeclass;
2084 EquivalenceClass *ipeclass;
2087 /* fetch outer/inner eclass from mergeclause */
2088 Assert(IsA(rinfo, RestrictInfo));
2089 if (rinfo->outer_is_left)
2091 oeclass = rinfo->left_ec;
2092 ieclass = rinfo->right_ec;
2096 oeclass = rinfo->right_ec;
2097 ieclass = rinfo->left_ec;
2099 Assert(oeclass != NULL);
2100 Assert(ieclass != NULL);
2103 * For debugging purposes, we check that the eclasses match the paths'
2104 * pathkeys. In typical cases the merge clauses are one-to-one with
2105 * the pathkeys, but when dealing with partially redundant query
2106 * conditions, we might have clauses that re-reference earlier path
2107 * keys. The case that we need to reject is where a pathkey is
2108 * entirely skipped over.
2110 * lop and lip reference the first as-yet-unused pathkey elements;
2111 * it's okay to match them, or any element before them. If they're
2112 * NULL then we have found all pathkey elements to be used.
2116 opathkey = (PathKey *) lfirst(lop);
2117 opeclass = opathkey->pk_eclass;
2118 if (oeclass == opeclass)
2120 /* fast path for typical case */
2125 /* redundant clauses ... must match something before lop */
2126 foreach(l2, outerpathkeys)
2130 opathkey = (PathKey *) lfirst(l2);
2131 opeclass = opathkey->pk_eclass;
2132 if (oeclass == opeclass)
2135 if (oeclass != opeclass)
2136 elog(ERROR, "outer pathkeys do not match mergeclauses");
2141 /* redundant clauses ... must match some already-used pathkey */
2144 foreach(l2, outerpathkeys)
2146 opathkey = (PathKey *) lfirst(l2);
2147 opeclass = opathkey->pk_eclass;
2148 if (oeclass == opeclass)
2152 elog(ERROR, "outer pathkeys do not match mergeclauses");
2157 ipathkey = (PathKey *) lfirst(lip);
2158 ipeclass = ipathkey->pk_eclass;
2159 if (ieclass == ipeclass)
2161 /* fast path for typical case */
2166 /* redundant clauses ... must match something before lip */
2167 foreach(l2, innerpathkeys)
2171 ipathkey = (PathKey *) lfirst(l2);
2172 ipeclass = ipathkey->pk_eclass;
2173 if (ieclass == ipeclass)
2176 if (ieclass != ipeclass)
2177 elog(ERROR, "inner pathkeys do not match mergeclauses");
2182 /* redundant clauses ... must match some already-used pathkey */
2185 foreach(l2, innerpathkeys)
2187 ipathkey = (PathKey *) lfirst(l2);
2188 ipeclass = ipathkey->pk_eclass;
2189 if (ieclass == ipeclass)
2193 elog(ERROR, "inner pathkeys do not match mergeclauses");
2196 /* pathkeys should match each other too (more debugging) */
2197 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
2198 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
2199 opathkey->pk_strategy != ipathkey->pk_strategy ||
2200 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
2201 elog(ERROR, "left and right pathkeys do not match in mergejoin");
2203 /* OK, save info for executor */
2204 mergefamilies[i] = opathkey->pk_opfamily;
2205 mergecollations[i] = opathkey->pk_eclass->ec_collation;
2206 mergestrategies[i] = opathkey->pk_strategy;
2207 mergenullsfirst[i] = opathkey->pk_nulls_first;
2212 * Note: it is not an error if we have additional pathkey elements (i.e.,
2213 * lop or lip isn't NULL here). The input paths might be better-sorted
2214 * than we need for the current mergejoin.
2218 * Now we can build the mergejoin node.
2220 join_plan = make_mergejoin(tlist,
2230 best_path->jpath.jointype);
2232 /* Costs of sort and material steps are included in path cost already */
2233 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2239 create_hashjoin_plan(PlannerInfo *root,
2240 HashPath *best_path,
2244 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
2248 Oid skewTable = InvalidOid;
2249 AttrNumber skewColumn = InvalidAttrNumber;
2250 bool skewInherit = false;
2251 Oid skewColType = InvalidOid;
2252 int32 skewColTypmod = -1;
2253 HashJoin *join_plan;
2256 /* Sort join qual clauses into best execution order */
2257 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2258 /* There's no point in sorting the hash clauses ... */
2260 /* Get the join qual clauses (in plain expression form) */
2261 /* Any pseudoconstant clauses are ignored here */
2262 if (IS_OUTER_JOIN(best_path->jpath.jointype))
2264 extract_actual_join_clauses(joinclauses,
2265 &joinclauses, &otherclauses);
2269 /* We can treat all clauses alike for an inner join */
2270 joinclauses = extract_actual_clauses(joinclauses, false);
2275 * Remove the hashclauses from the list of join qual clauses, leaving the
2276 * list of quals that must be checked as qpquals.
2278 hashclauses = get_actual_clauses(best_path->path_hashclauses);
2279 joinclauses = list_difference(joinclauses, hashclauses);
2282 * Rearrange hashclauses, if needed, so that the outer variable is always
2285 hashclauses = get_switched_clauses(best_path->path_hashclauses,
2286 best_path->jpath.outerjoinpath->parent->relids);
2288 /* We don't want any excess columns in the hashed tuples */
2289 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
2291 /* If we expect batching, suppress excess columns in outer tuples too */
2292 if (best_path->num_batches > 1)
2293 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
2296 * If there is a single join clause and we can identify the outer variable
2297 * as a simple column reference, supply its identity for possible use in
2298 * skew optimization. (Note: in principle we could do skew optimization
2299 * with multiple join clauses, but we'd have to be able to determine the
2300 * most common combinations of outer values, which we don't currently have
2301 * enough stats for.)
2303 if (list_length(hashclauses) == 1)
2305 OpExpr *clause = (OpExpr *) linitial(hashclauses);
2308 Assert(is_opclause(clause));
2309 node = (Node *) linitial(clause->args);
2310 if (IsA(node, RelabelType))
2311 node = (Node *) ((RelabelType *) node)->arg;
2314 Var *var = (Var *) node;
2317 rte = root->simple_rte_array[var->varno];
2318 if (rte->rtekind == RTE_RELATION)
2320 skewTable = rte->relid;
2321 skewColumn = var->varattno;
2322 skewInherit = rte->inh;
2323 skewColType = var->vartype;
2324 skewColTypmod = var->vartypmod;
2330 * Build the hash node and hash join node.
2332 hash_plan = make_hash(inner_plan,
2338 join_plan = make_hashjoin(tlist,
2344 best_path->jpath.jointype);
2346 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2352 /*****************************************************************************
2354 * SUPPORTING ROUTINES
2356 *****************************************************************************/
2359 * replace_nestloop_params
2360 * Replace outer-relation Vars and PlaceHolderVars in the given expression
2361 * with nestloop Params
2363 * All Vars and PlaceHolderVars belonging to the relation(s) identified by
2364 * root->curOuterRels are replaced by Params, and entries are added to
2365 * root->curOuterParams if not already present.
2368 replace_nestloop_params(PlannerInfo *root, Node *expr)
2370 /* No setup needed for tree walk, so away we go */
2371 return replace_nestloop_params_mutator(expr, root);
2375 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
2381 Var *var = (Var *) node;
2386 /* Upper-level Vars should be long gone at this point */
2387 Assert(var->varlevelsup == 0);
2388 /* If not to be replaced, we can just return the Var unmodified */
2389 if (!bms_is_member(var->varno, root->curOuterRels))
2391 /* Create a Param representing the Var */
2392 param = assign_nestloop_param_var(root, var);
2393 /* Is this param already listed in root->curOuterParams? */
2394 foreach(lc, root->curOuterParams)
2396 nlp = (NestLoopParam *) lfirst(lc);
2397 if (nlp->paramno == param->paramid)
2399 Assert(equal(var, nlp->paramval));
2400 /* Present, so we can just return the Param */
2401 return (Node *) param;
2405 nlp = makeNode(NestLoopParam);
2406 nlp->paramno = param->paramid;
2407 nlp->paramval = var;
2408 root->curOuterParams = lappend(root->curOuterParams, nlp);
2409 /* And return the replacement Param */
2410 return (Node *) param;
2412 if (IsA(node, PlaceHolderVar))
2414 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2419 /* Upper-level PlaceHolderVars should be long gone at this point */
2420 Assert(phv->phlevelsup == 0);
2423 * If not to be replaced, just return the PlaceHolderVar unmodified.
2424 * We use bms_overlap as a cheap/quick test to see if the PHV might
2425 * be evaluated in the outer rels, and then grab its PlaceHolderInfo
2428 if (!bms_overlap(phv->phrels, root->curOuterRels))
2430 if (!bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
2431 root->curOuterRels))
2433 /* Create a Param representing the PlaceHolderVar */
2434 param = assign_nestloop_param_placeholdervar(root, phv);
2435 /* Is this param already listed in root->curOuterParams? */
2436 foreach(lc, root->curOuterParams)
2438 nlp = (NestLoopParam *) lfirst(lc);
2439 if (nlp->paramno == param->paramid)
2441 Assert(equal(phv, nlp->paramval));
2442 /* Present, so we can just return the Param */
2443 return (Node *) param;
2447 nlp = makeNode(NestLoopParam);
2448 nlp->paramno = param->paramid;
2449 nlp->paramval = (Var *) phv;
2450 root->curOuterParams = lappend(root->curOuterParams, nlp);
2451 /* And return the replacement Param */
2452 return (Node *) param;
2454 return expression_tree_mutator(node,
2455 replace_nestloop_params_mutator,
2460 * fix_indexqual_references
2461 * Adjust indexqual clauses to the form the executor's indexqual
2464 * We have four tasks here:
2465 * * Remove RestrictInfo nodes from the input clauses.
2466 * * Replace any outer-relation Var or PHV nodes with nestloop Params.
2467 * (XXX eventually, that responsibility should go elsewhere?)
2468 * * Index keys must be represented by Var nodes with varattno set to the
2469 * index's attribute number, not the attribute number in the original rel.
2470 * * If the index key is on the right, commute the clause to put it on the
2473 * The result is a modified copy of the path's indexquals list --- the
2474 * original is not changed. Note also that the copy shares no substructure
2475 * with the original; this is needed in case there is a subplan in it (we need
2476 * two separate copies of the subplan tree, or things will go awry).
2479 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
2481 IndexOptInfo *index = index_path->indexinfo;
2482 List *fixed_indexquals;
2486 fixed_indexquals = NIL;
2488 forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
2490 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
2491 int indexcol = lfirst_int(lci);
2494 Assert(IsA(rinfo, RestrictInfo));
2497 * Replace any outer-relation variables with nestloop params.
2499 * This also makes a copy of the clause, so it's safe to modify it
2502 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
2504 if (IsA(clause, OpExpr))
2506 OpExpr *op = (OpExpr *) clause;
2508 if (list_length(op->args) != 2)
2509 elog(ERROR, "indexqual clause is not binary opclause");
2512 * Check to see if the indexkey is on the right; if so, commute
2513 * the clause. The indexkey should be the side that refers to
2514 * (only) the base relation.
2516 if (!bms_equal(rinfo->left_relids, index->rel->relids))
2520 * Now replace the indexkey expression with an index Var.
2522 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2526 else if (IsA(clause, RowCompareExpr))
2528 RowCompareExpr *rc = (RowCompareExpr *) clause;
2536 * Re-discover which index columns are used in the rowcompare.
2538 newrc = adjust_rowcompare_for_index(rc,
2545 * Trouble if adjust_rowcompare_for_index thought the
2546 * RowCompareExpr didn't match the index as-is; the clause should
2547 * have gone through that routine already.
2549 if (newrc != (Expr *) rc)
2550 elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
2553 * Check to see if the indexkey is on the right; if so, commute
2557 CommuteRowCompareExpr(rc);
2560 * Now replace the indexkey expressions with index Vars.
2562 Assert(list_length(rc->largs) == list_length(indexcolnos));
2563 forboth(lca, rc->largs, lcai, indexcolnos)
2565 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
2570 else if (IsA(clause, ScalarArrayOpExpr))
2572 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2574 /* Never need to commute... */
2576 /* Replace the indexkey expression with an index Var. */
2577 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2581 else if (IsA(clause, NullTest))
2583 NullTest *nt = (NullTest *) clause;
2585 /* Replace the indexkey expression with an index Var. */
2586 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
2591 elog(ERROR, "unsupported indexqual type: %d",
2592 (int) nodeTag(clause));
2594 fixed_indexquals = lappend(fixed_indexquals, clause);
2597 return fixed_indexquals;
2601 * fix_indexorderby_references
2602 * Adjust indexorderby clauses to the form the executor's index
2605 * This is a simplified version of fix_indexqual_references. The input does
2606 * not have RestrictInfo nodes, and we assume that indxpath.c already
2607 * commuted the clauses to put the index keys on the left. Also, we don't
2608 * bother to support any cases except simple OpExprs, since nothing else
2609 * is allowed for ordering operators.
2612 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
2614 IndexOptInfo *index = index_path->indexinfo;
2615 List *fixed_indexorderbys;
2619 fixed_indexorderbys = NIL;
2621 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
2623 Node *clause = (Node *) lfirst(lcc);
2624 int indexcol = lfirst_int(lci);
2627 * Replace any outer-relation variables with nestloop params.
2629 * This also makes a copy of the clause, so it's safe to modify it
2632 clause = replace_nestloop_params(root, clause);
2634 if (IsA(clause, OpExpr))
2636 OpExpr *op = (OpExpr *) clause;
2638 if (list_length(op->args) != 2)
2639 elog(ERROR, "indexorderby clause is not binary opclause");
2642 * Now replace the indexkey expression with an index Var.
2644 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2649 elog(ERROR, "unsupported indexorderby type: %d",
2650 (int) nodeTag(clause));
2652 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
2655 return fixed_indexorderbys;
2659 * fix_indexqual_operand
2660 * Convert an indexqual expression to a Var referencing the index column.
2662 * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
2663 * equal to the index's attribute number (index column position).
2665 * Most of the code here is just for sanity cross-checking that the given
2666 * expression actually matches the index column it's claimed to.
2669 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
2673 ListCell *indexpr_item;
2676 * Remove any binary-compatible relabeling of the indexkey
2678 if (IsA(node, RelabelType))
2679 node = (Node *) ((RelabelType *) node)->arg;
2681 Assert(indexcol >= 0 && indexcol < index->ncolumns);
2683 if (index->indexkeys[indexcol] != 0)
2685 /* It's a simple index column */
2686 if (IsA(node, Var) &&
2687 ((Var *) node)->varno == index->rel->relid &&
2688 ((Var *) node)->varattno == index->indexkeys[indexcol])
2690 result = (Var *) copyObject(node);
2691 result->varno = INDEX_VAR;
2692 result->varattno = indexcol + 1;
2693 return (Node *) result;
2696 elog(ERROR, "index key does not match expected index column");
2699 /* It's an index expression, so find and cross-check the expression */
2700 indexpr_item = list_head(index->indexprs);
2701 for (pos = 0; pos < index->ncolumns; pos++)
2703 if (index->indexkeys[pos] == 0)
2705 if (indexpr_item == NULL)
2706 elog(ERROR, "too few entries in indexprs list");
2707 if (pos == indexcol)
2711 indexkey = (Node *) lfirst(indexpr_item);
2712 if (indexkey && IsA(indexkey, RelabelType))
2713 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2714 if (equal(node, indexkey))
2716 result = makeVar(INDEX_VAR, indexcol + 1,
2717 exprType(lfirst(indexpr_item)), -1,
2718 exprCollation(lfirst(indexpr_item)),
2720 return (Node *) result;
2723 elog(ERROR, "index key does not match expected index column");
2725 indexpr_item = lnext(indexpr_item);
2730 elog(ERROR, "index key does not match expected index column");
2731 return NULL; /* keep compiler quiet */
2735 * get_switched_clauses
2736 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2737 * extract the bare clauses, and rearrange the elements within the
2738 * clauses, if needed, so the outer join variable is on the left and
2739 * the inner is on the right. The original clause data structure is not
2740 * touched; a modified list is returned. We do, however, set the transient
2741 * outer_is_left field in each RestrictInfo to show which side was which.
2744 get_switched_clauses(List *clauses, Relids outerrelids)
2751 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
2752 OpExpr *clause = (OpExpr *) restrictinfo->clause;
2754 Assert(is_opclause(clause));
2755 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
2758 * Duplicate just enough of the structure to allow commuting the
2759 * clause without changing the original list. Could use
2760 * copyObject, but a complete deep copy is overkill.
2762 OpExpr *temp = makeNode(OpExpr);
2764 temp->opno = clause->opno;
2765 temp->opfuncid = InvalidOid;
2766 temp->opresulttype = clause->opresulttype;
2767 temp->opretset = clause->opretset;
2768 temp->opcollid = clause->opcollid;
2769 temp->inputcollid = clause->inputcollid;
2770 temp->args = list_copy(clause->args);
2771 temp->location = clause->location;
2772 /* Commute it --- note this modifies the temp node in-place. */
2773 CommuteOpExpr(temp);
2774 t_list = lappend(t_list, temp);
2775 restrictinfo->outer_is_left = false;
2779 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
2780 t_list = lappend(t_list, clause);
2781 restrictinfo->outer_is_left = true;
2788 * order_qual_clauses
2789 * Given a list of qual clauses that will all be evaluated at the same
2790 * plan node, sort the list into the order we want to check the quals
2793 * Ideally the order should be driven by a combination of execution cost and
2794 * selectivity, but it's not immediately clear how to account for both,
2795 * and given the uncertainty of the estimates the reliability of the decisions
2796 * would be doubtful anyway. So we just order by estimated per-tuple cost,
2797 * being careful not to change the order when (as is often the case) the
2798 * estimates are identical.
2800 * Although this will work on either bare clauses or RestrictInfos, it's
2801 * much faster to apply it to RestrictInfos, since it can re-use cost
2802 * information that is cached in RestrictInfos.
2804 * Note: some callers pass lists that contain entries that will later be
2805 * removed; this is the easiest way to let this routine see RestrictInfos
2806 * instead of bare clauses. It's OK because we only sort by cost, but
2807 * a cost/selectivity combination would likely do the wrong thing.
2810 order_qual_clauses(PlannerInfo *root, List *clauses)
2817 int nitems = list_length(clauses);
2823 /* No need to work hard for 0 or 1 clause */
2828 * Collect the items and costs into an array. This is to avoid repeated
2829 * cost_qual_eval work if the inputs aren't RestrictInfos.
2831 items = (QualItem *) palloc(nitems * sizeof(QualItem));
2833 foreach(lc, clauses)
2835 Node *clause = (Node *) lfirst(lc);
2838 cost_qual_eval_node(&qcost, clause, root);
2839 items[i].clause = clause;
2840 items[i].cost = qcost.per_tuple;
2845 * Sort. We don't use qsort() because it's not guaranteed stable for
2846 * equal keys. The expected number of entries is small enough that a
2847 * simple insertion sort should be good enough.
2849 for (i = 1; i < nitems; i++)
2851 QualItem newitem = items[i];
2854 /* insert newitem into the already-sorted subarray */
2855 for (j = i; j > 0; j--)
2857 if (newitem.cost >= items[j - 1].cost)
2859 items[j] = items[j - 1];
2864 /* Convert back to a list */
2866 for (i = 0; i < nitems; i++)
2867 result = lappend(result, items[i].clause);
2873 * Copy cost and size info from a Path node to the Plan node created from it.
2874 * The executor usually won't use this info, but it's needed by EXPLAIN.
2877 copy_path_costsize(Plan *dest, Path *src)
2881 dest->startup_cost = src->startup_cost;
2882 dest->total_cost = src->total_cost;
2883 dest->plan_rows = src->rows;
2884 dest->plan_width = src->parent->width;
2888 dest->startup_cost = 0;
2889 dest->total_cost = 0;
2890 dest->plan_rows = 0;
2891 dest->plan_width = 0;
2896 * Copy cost and size info from a lower plan node to an inserted node.
2897 * (Most callers alter the info after copying it.)
2900 copy_plan_costsize(Plan *dest, Plan *src)
2904 dest->startup_cost = src->startup_cost;
2905 dest->total_cost = src->total_cost;
2906 dest->plan_rows = src->plan_rows;
2907 dest->plan_width = src->plan_width;
2911 dest->startup_cost = 0;
2912 dest->total_cost = 0;
2913 dest->plan_rows = 0;
2914 dest->plan_width = 0;
2919 /*****************************************************************************
2921 * PLAN NODE BUILDING ROUTINES
2923 * Some of these are exported because they are called to build plan nodes
2924 * in contexts where we're not deriving the plan node from a path node.
2926 *****************************************************************************/
2929 make_seqscan(List *qptlist,
2933 SeqScan *node = makeNode(SeqScan);
2934 Plan *plan = &node->plan;
2936 /* cost should be inserted by caller */
2937 plan->targetlist = qptlist;
2938 plan->qual = qpqual;
2939 plan->lefttree = NULL;
2940 plan->righttree = NULL;
2941 node->scanrelid = scanrelid;
2947 make_indexscan(List *qptlist,
2952 List *indexqualorig,
2954 List *indexorderbyorig,
2955 ScanDirection indexscandir)
2957 IndexScan *node = makeNode(IndexScan);
2958 Plan *plan = &node->scan.plan;
2960 /* cost should be inserted by caller */
2961 plan->targetlist = qptlist;
2962 plan->qual = qpqual;
2963 plan->lefttree = NULL;
2964 plan->righttree = NULL;
2965 node->scan.scanrelid = scanrelid;
2966 node->indexid = indexid;
2967 node->indexqual = indexqual;
2968 node->indexqualorig = indexqualorig;
2969 node->indexorderby = indexorderby;
2970 node->indexorderbyorig = indexorderbyorig;
2971 node->indexorderdir = indexscandir;
2976 static IndexOnlyScan *
2977 make_indexonlyscan(List *qptlist,
2984 ScanDirection indexscandir)
2986 IndexOnlyScan *node = makeNode(IndexOnlyScan);
2987 Plan *plan = &node->scan.plan;
2989 /* cost should be inserted by caller */
2990 plan->targetlist = qptlist;
2991 plan->qual = qpqual;
2992 plan->lefttree = NULL;
2993 plan->righttree = NULL;
2994 node->scan.scanrelid = scanrelid;
2995 node->indexid = indexid;
2996 node->indexqual = indexqual;
2997 node->indexorderby = indexorderby;
2998 node->indextlist = indextlist;
2999 node->indexorderdir = indexscandir;
3004 static BitmapIndexScan *
3005 make_bitmap_indexscan(Index scanrelid,
3008 List *indexqualorig)
3010 BitmapIndexScan *node = makeNode(BitmapIndexScan);
3011 Plan *plan = &node->scan.plan;
3013 /* cost should be inserted by caller */
3014 plan->targetlist = NIL; /* not used */
3015 plan->qual = NIL; /* not used */
3016 plan->lefttree = NULL;
3017 plan->righttree = NULL;
3018 node->scan.scanrelid = scanrelid;
3019 node->indexid = indexid;
3020 node->indexqual = indexqual;
3021 node->indexqualorig = indexqualorig;
3026 static BitmapHeapScan *
3027 make_bitmap_heapscan(List *qptlist,
3030 List *bitmapqualorig,
3033 BitmapHeapScan *node = makeNode(BitmapHeapScan);
3034 Plan *plan = &node->scan.plan;
3036 /* cost should be inserted by caller */
3037 plan->targetlist = qptlist;
3038 plan->qual = qpqual;
3039 plan->lefttree = lefttree;
3040 plan->righttree = NULL;
3041 node->scan.scanrelid = scanrelid;
3042 node->bitmapqualorig = bitmapqualorig;
3048 make_tidscan(List *qptlist,
3053 TidScan *node = makeNode(TidScan);
3054 Plan *plan = &node->scan.plan;
3056 /* cost should be inserted by caller */
3057 plan->targetlist = qptlist;
3058 plan->qual = qpqual;
3059 plan->lefttree = NULL;
3060 plan->righttree = NULL;
3061 node->scan.scanrelid = scanrelid;
3062 node->tidquals = tidquals;
3068 make_subqueryscan(List *qptlist,
3073 SubqueryScan *node = makeNode(SubqueryScan);
3074 Plan *plan = &node->scan.plan;
3077 * Cost is figured here for the convenience of prepunion.c. Note this is
3078 * only correct for the case where qpqual is empty; otherwise caller
3079 * should overwrite cost with a better estimate.
3081 copy_plan_costsize(plan, subplan);
3082 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
3084 plan->targetlist = qptlist;
3085 plan->qual = qpqual;
3086 plan->lefttree = NULL;
3087 plan->righttree = NULL;
3088 node->scan.scanrelid = scanrelid;
3089 node->subplan = subplan;
3094 static FunctionScan *
3095 make_functionscan(List *qptlist,
3101 List *funccoltypmods,
3102 List *funccolcollations)
3104 FunctionScan *node = makeNode(FunctionScan);
3105 Plan *plan = &node->scan.plan;
3107 /* cost should be inserted by caller */
3108 plan->targetlist = qptlist;
3109 plan->qual = qpqual;
3110 plan->lefttree = NULL;
3111 plan->righttree = NULL;
3112 node->scan.scanrelid = scanrelid;
3113 node->funcexpr = funcexpr;
3114 node->funccolnames = funccolnames;
3115 node->funccoltypes = funccoltypes;
3116 node->funccoltypmods = funccoltypmods;
3117 node->funccolcollations = funccolcollations;
3123 make_valuesscan(List *qptlist,
3128 ValuesScan *node = makeNode(ValuesScan);
3129 Plan *plan = &node->scan.plan;
3131 /* cost should be inserted by caller */
3132 plan->targetlist = qptlist;
3133 plan->qual = qpqual;
3134 plan->lefttree = NULL;
3135 plan->righttree = NULL;
3136 node->scan.scanrelid = scanrelid;
3137 node->values_lists = values_lists;
3143 make_ctescan(List *qptlist,
3149 CteScan *node = makeNode(CteScan);
3150 Plan *plan = &node->scan.plan;
3152 /* cost should be inserted by caller */
3153 plan->targetlist = qptlist;
3154 plan->qual = qpqual;
3155 plan->lefttree = NULL;
3156 plan->righttree = NULL;
3157 node->scan.scanrelid = scanrelid;
3158 node->ctePlanId = ctePlanId;
3159 node->cteParam = cteParam;
3164 static WorkTableScan *
3165 make_worktablescan(List *qptlist,
3170 WorkTableScan *node = makeNode(WorkTableScan);
3171 Plan *plan = &node->scan.plan;
3173 /* cost should be inserted by caller */
3174 plan->targetlist = qptlist;
3175 plan->qual = qpqual;
3176 plan->lefttree = NULL;
3177 plan->righttree = NULL;
3178 node->scan.scanrelid = scanrelid;
3179 node->wtParam = wtParam;
3184 static ForeignScan *
3185 make_foreignscan(List *qptlist,
3191 ForeignScan *node = makeNode(ForeignScan);
3192 Plan *plan = &node->scan.plan;
3194 /* cost should be inserted by caller */
3195 plan->targetlist = qptlist;
3196 plan->qual = qpqual;
3197 plan->lefttree = NULL;
3198 plan->righttree = NULL;
3199 node->scan.scanrelid = scanrelid;
3200 node->fsSystemCol = fsSystemCol;
3201 node->fdwplan = fdwplan;
3207 make_append(List *appendplans, List *tlist)
3209 Append *node = makeNode(Append);
3210 Plan *plan = &node->plan;
3215 * Compute cost as sum of subplan costs. We charge nothing extra for the
3216 * Append itself, which perhaps is too optimistic, but since it doesn't do
3217 * any selection or projection, it is a pretty cheap node.
3219 * If you change this, see also create_append_path(). Also, the size
3220 * calculations should match set_append_rel_pathlist(). It'd be better
3221 * not to duplicate all this logic, but some callers of this function
3222 * aren't working from an appendrel or AppendPath, so there's noplace to
3223 * copy the data from.
3225 plan->startup_cost = 0;
3226 plan->total_cost = 0;
3227 plan->plan_rows = 0;
3229 foreach(subnode, appendplans)
3231 Plan *subplan = (Plan *) lfirst(subnode);
3233 if (subnode == list_head(appendplans)) /* first node? */
3234 plan->startup_cost = subplan->startup_cost;
3235 plan->total_cost += subplan->total_cost;
3236 plan->plan_rows += subplan->plan_rows;
3237 total_size += subplan->plan_width * subplan->plan_rows;
3239 if (plan->plan_rows > 0)
3240 plan->plan_width = rint(total_size / plan->plan_rows);
3242 plan->plan_width = 0;
3244 plan->targetlist = tlist;
3246 plan->lefttree = NULL;
3247 plan->righttree = NULL;
3248 node->appendplans = appendplans;
3254 make_recursive_union(List *tlist,
3261 RecursiveUnion *node = makeNode(RecursiveUnion);
3262 Plan *plan = &node->plan;
3263 int numCols = list_length(distinctList);
3265 cost_recursive_union(plan, lefttree, righttree);
3267 plan->targetlist = tlist;
3269 plan->lefttree = lefttree;
3270 plan->righttree = righttree;
3271 node->wtParam = wtParam;
3274 * convert SortGroupClause list into arrays of attr indexes and equality
3275 * operators, as wanted by executor
3277 node->numCols = numCols;
3281 AttrNumber *dupColIdx;
3285 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3286 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3288 foreach(slitem, distinctList)
3290 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3291 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
3294 dupColIdx[keyno] = tle->resno;
3295 dupOperators[keyno] = sortcl->eqop;
3296 Assert(OidIsValid(dupOperators[keyno]));
3299 node->dupColIdx = dupColIdx;
3300 node->dupOperators = dupOperators;
3302 node->numGroups = numGroups;
3308 make_bitmap_and(List *bitmapplans)
3310 BitmapAnd *node = makeNode(BitmapAnd);
3311 Plan *plan = &node->plan;
3313 /* cost should be inserted by caller */
3314 plan->targetlist = NIL;
3316 plan->lefttree = NULL;
3317 plan->righttree = NULL;
3318 node->bitmapplans = bitmapplans;
3324 make_bitmap_or(List *bitmapplans)
3326 BitmapOr *node = makeNode(BitmapOr);
3327 Plan *plan = &node->plan;
3329 /* cost should be inserted by caller */
3330 plan->targetlist = NIL;
3332 plan->lefttree = NULL;
3333 plan->righttree = NULL;
3334 node->bitmapplans = bitmapplans;
3340 make_nestloop(List *tlist,
3348 NestLoop *node = makeNode(NestLoop);
3349 Plan *plan = &node->join.plan;
3351 /* cost should be inserted by caller */
3352 plan->targetlist = tlist;
3353 plan->qual = otherclauses;
3354 plan->lefttree = lefttree;
3355 plan->righttree = righttree;
3356 node->join.jointype = jointype;
3357 node->join.joinqual = joinclauses;
3358 node->nestParams = nestParams;
3364 make_hashjoin(List *tlist,
3372 HashJoin *node = makeNode(HashJoin);
3373 Plan *plan = &node->join.plan;
3375 /* cost should be inserted by caller */
3376 plan->targetlist = tlist;
3377 plan->qual = otherclauses;
3378 plan->lefttree = lefttree;
3379 plan->righttree = righttree;
3380 node->hashclauses = hashclauses;
3381 node->join.jointype = jointype;
3382 node->join.joinqual = joinclauses;
3388 make_hash(Plan *lefttree,
3390 AttrNumber skewColumn,
3393 int32 skewColTypmod)
3395 Hash *node = makeNode(Hash);
3396 Plan *plan = &node->plan;
3398 copy_plan_costsize(plan, lefttree);
3401 * For plausibility, make startup & total costs equal total cost of input
3402 * plan; this only affects EXPLAIN display not decisions.
3404 plan->startup_cost = plan->total_cost;
3405 plan->targetlist = lefttree->targetlist;
3407 plan->lefttree = lefttree;
3408 plan->righttree = NULL;
3410 node->skewTable = skewTable;
3411 node->skewColumn = skewColumn;
3412 node->skewInherit = skewInherit;
3413 node->skewColType = skewColType;
3414 node->skewColTypmod = skewColTypmod;
3420 make_mergejoin(List *tlist,
3425 Oid *mergecollations,
3426 int *mergestrategies,
3427 bool *mergenullsfirst,
3432 MergeJoin *node = makeNode(MergeJoin);
3433 Plan *plan = &node->join.plan;
3435 /* cost should be inserted by caller */
3436 plan->targetlist = tlist;
3437 plan->qual = otherclauses;
3438 plan->lefttree = lefttree;
3439 plan->righttree = righttree;
3440 node->mergeclauses = mergeclauses;
3441 node->mergeFamilies = mergefamilies;
3442 node->mergeCollations = mergecollations;
3443 node->mergeStrategies = mergestrategies;
3444 node->mergeNullsFirst = mergenullsfirst;
3445 node->join.jointype = jointype;
3446 node->join.joinqual = joinclauses;
3452 * make_sort --- basic routine to build a Sort plan node
3454 * Caller must have built the sortColIdx, sortOperators, collations, and
3455 * nullsFirst arrays already.
3456 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
3459 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
3460 AttrNumber *sortColIdx, Oid *sortOperators,
3461 Oid *collations, bool *nullsFirst,
3462 double limit_tuples)
3464 Sort *node = makeNode(Sort);
3465 Plan *plan = &node->plan;
3466 Path sort_path; /* dummy for result of cost_sort */
3468 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3469 cost_sort(&sort_path, root, NIL,
3470 lefttree->total_cost,
3471 lefttree->plan_rows,
3472 lefttree->plan_width,
3476 plan->startup_cost = sort_path.startup_cost;
3477 plan->total_cost = sort_path.total_cost;
3478 plan->targetlist = lefttree->targetlist;
3480 plan->lefttree = lefttree;
3481 plan->righttree = NULL;
3482 node->numCols = numCols;
3483 node->sortColIdx = sortColIdx;
3484 node->sortOperators = sortOperators;
3485 node->collations = collations;
3486 node->nullsFirst = nullsFirst;
3492 * add_sort_column --- utility subroutine for building sort info arrays
3494 * We need this routine because the same column might be selected more than
3495 * once as a sort key column; if so, the extra mentions are redundant.
3497 * Caller is assumed to have allocated the arrays large enough for the
3498 * max possible number of columns. Return value is the new column count.
3501 add_sort_column(AttrNumber colIdx, Oid sortOp, Oid coll, bool nulls_first,
3502 int numCols, AttrNumber *sortColIdx,
3503 Oid *sortOperators, Oid *collations, bool *nullsFirst)
3507 Assert(OidIsValid(sortOp));
3509 for (i = 0; i < numCols; i++)
3512 * Note: we check sortOp because it's conceivable that "ORDER BY foo
3513 * USING <, foo USING <<<" is not redundant, if <<< distinguishes
3514 * values that < considers equal. We need not check nulls_first
3515 * however because a lower-order column with the same sortop but
3516 * opposite nulls direction is redundant.
3518 * We could probably consider sort keys with the same sortop and
3519 * different collations to be redundant too, but for the moment treat
3520 * them as not redundant. This will be needed if we ever support
3521 * collations with different notions of equality.
3523 if (sortColIdx[i] == colIdx &&
3524 sortOperators[numCols] == sortOp &&
3525 collations[numCols] == coll)
3527 /* Already sorting by this col, so extra sort key is useless */
3532 /* Add the column */
3533 sortColIdx[numCols] = colIdx;
3534 sortOperators[numCols] = sortOp;
3535 collations[numCols] = coll;
3536 nullsFirst[numCols] = nulls_first;
3541 * prepare_sort_from_pathkeys
3542 * Prepare to sort according to given pathkeys
3544 * This is used to set up for both Sort and MergeAppend nodes. It calculates
3545 * the executor's representation of the sort key information, and adjusts the
3546 * plan targetlist if needed to add resjunk sort columns.
3549 * 'lefttree' is the node which yields input tuples
3550 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3551 * 'adjust_tlist_in_place' is TRUE if lefttree must be modified in-place
3553 * We must convert the pathkey information into arrays of sort key column
3554 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
3555 * which is the representation the executor wants. These are returned into
3556 * the output parameters *p_numsortkeys etc.
3558 * If the pathkeys include expressions that aren't simple Vars, we will
3559 * usually need to add resjunk items to the input plan's targetlist to
3560 * compute these expressions, since the Sort/MergeAppend node itself won't
3561 * do any such calculations. If the input plan type isn't one that can do
3562 * projections, this means adding a Result node just to do the projection.
3563 * However, the caller can pass adjust_tlist_in_place = TRUE to force the
3564 * lefttree tlist to be modified in-place regardless of whether the node type
3565 * can project --- we use this for fixing the tlist of MergeAppend itself.
3567 * Returns the node which is to be the input to the Sort (either lefttree,
3568 * or a Result stacked atop lefttree).
3571 prepare_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3572 bool adjust_tlist_in_place,
3574 AttrNumber **p_sortColIdx,
3575 Oid **p_sortOperators,
3577 bool **p_nullsFirst)
3579 List *tlist = lefttree->targetlist;
3582 AttrNumber *sortColIdx;
3588 * We will need at most list_length(pathkeys) sort columns; possibly less
3590 numsortkeys = list_length(pathkeys);
3591 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3592 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3593 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3594 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3598 foreach(i, pathkeys)
3600 PathKey *pathkey = (PathKey *) lfirst(i);
3601 EquivalenceClass *ec = pathkey->pk_eclass;
3602 TargetEntry *tle = NULL;
3603 Oid pk_datatype = InvalidOid;
3607 if (ec->ec_has_volatile)
3610 * If the pathkey's EquivalenceClass is volatile, then it must
3611 * have come from an ORDER BY clause, and we have to match it to
3612 * that same targetlist entry.
3614 if (ec->ec_sortref == 0) /* can't happen */
3615 elog(ERROR, "volatile EquivalenceClass has no sortref");
3616 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
3618 Assert(list_length(ec->ec_members) == 1);
3619 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
3624 * Otherwise, we can sort by any non-constant expression listed in
3625 * the pathkey's EquivalenceClass. For now, we take the first one
3626 * that corresponds to an available item in the tlist. If there
3627 * isn't any, use the first one that is an expression in the
3628 * input's vars. (The non-const restriction only matters if the
3629 * EC is below_outer_join; but if it isn't, it won't contain
3630 * consts anyway, else we'd have discarded the pathkey as
3633 * XXX if we have a choice, is there any way of figuring out which
3634 * might be cheapest to execute? (For example, int4lt is likely
3635 * much cheaper to execute than numericlt, but both might appear
3636 * in the same equivalence class...) Not clear that we ever will
3637 * have an interesting choice in practice, so it may not matter.
3639 foreach(j, ec->ec_members)
3641 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3644 * We shouldn't be trying to sort by an equivalence class that
3645 * contains a constant, so no need to consider such cases any
3648 if (em->em_is_const)
3651 tle = tlist_member((Node *) em->em_expr, tlist);
3654 pk_datatype = em->em_datatype;
3655 break; /* found expr already in tlist */
3659 * We can also use it if the pathkey expression is a relabel
3660 * of the tlist entry, or vice versa. This is needed for
3661 * binary-compatible cases (cf. make_pathkey_from_sortinfo).
3662 * We prefer an exact match, though, so we do the basic search
3665 tle = tlist_member_ignore_relabel((Node *) em->em_expr, tlist);
3668 pk_datatype = em->em_datatype;
3669 break; /* found expr already in tlist */
3676 * No matching tlist item; look for a computable expression.
3677 * Note that we treat Aggrefs as if they were variables; this
3678 * is necessary when attempting to sort the output from an Agg
3679 * node for use in a WindowFunc (since grouping_planner will
3680 * have treated the Aggrefs as variables, too).
3682 Expr *sortexpr = NULL;
3684 foreach(j, ec->ec_members)
3686 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3690 if (em->em_is_const)
3692 sortexpr = em->em_expr;
3693 exprvars = pull_var_clause((Node *) sortexpr,
3694 PVC_INCLUDE_AGGREGATES,
3695 PVC_INCLUDE_PLACEHOLDERS);
3696 foreach(k, exprvars)
3698 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
3701 list_free(exprvars);
3704 pk_datatype = em->em_datatype;
3705 break; /* found usable expression */
3709 elog(ERROR, "could not find pathkey item to sort");
3712 * Do we need to insert a Result node?
3714 if (!adjust_tlist_in_place &&
3715 !is_projection_capable_plan(lefttree))
3717 /* copy needed so we don't modify input's tlist below */
3718 tlist = copyObject(tlist);
3719 lefttree = (Plan *) make_result(root, tlist, NULL,
3723 /* Don't bother testing is_projection_capable_plan again */
3724 adjust_tlist_in_place = true;
3727 * Add resjunk entry to input's tlist
3729 tle = makeTargetEntry(sortexpr,
3730 list_length(tlist) + 1,
3733 tlist = lappend(tlist, tle);
3734 lefttree->targetlist = tlist; /* just in case NIL before */
3739 * Look up the correct sort operator from the PathKey's slightly
3740 * abstracted representation.
3742 sortop = get_opfamily_member(pathkey->pk_opfamily,
3745 pathkey->pk_strategy);
3746 if (!OidIsValid(sortop)) /* should not happen */
3747 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
3748 pathkey->pk_strategy, pk_datatype, pk_datatype,
3749 pathkey->pk_opfamily);
3752 * The column might already be selected as a sort key, if the pathkeys
3753 * contain duplicate entries. (This can happen in scenarios where
3754 * multiple mergejoinable clauses mention the same var, for example.)
3755 * So enter it only once in the sort arrays.
3757 numsortkeys = add_sort_column(tle->resno,
3759 pathkey->pk_eclass->ec_collation,
3760 pathkey->pk_nulls_first,
3762 sortColIdx, sortOperators,
3763 collations, nullsFirst);
3766 Assert(numsortkeys > 0);
3768 /* Return results */
3769 *p_numsortkeys = numsortkeys;
3770 *p_sortColIdx = sortColIdx;
3771 *p_sortOperators = sortOperators;
3772 *p_collations = collations;
3773 *p_nullsFirst = nullsFirst;
3779 * make_sort_from_pathkeys
3780 * Create sort plan to sort according to given pathkeys
3782 * 'lefttree' is the node which yields input tuples
3783 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3784 * 'limit_tuples' is the bound on the number of output tuples;
3788 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3789 double limit_tuples)
3792 AttrNumber *sortColIdx;
3797 /* Compute sort column info, and adjust lefttree as needed */
3798 lefttree = prepare_sort_from_pathkeys(root, lefttree, pathkeys,
3806 /* Now build the Sort node */
3807 return make_sort(root, lefttree, numsortkeys,
3808 sortColIdx, sortOperators, collations,
3809 nullsFirst, limit_tuples);
3813 * make_sort_from_sortclauses
3814 * Create sort plan to sort according to given sortclauses
3816 * 'sortcls' is a list of SortGroupClauses
3817 * 'lefttree' is the node which yields input tuples
3820 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
3822 List *sub_tlist = lefttree->targetlist;
3825 AttrNumber *sortColIdx;
3831 * We will need at most list_length(sortcls) sort columns; possibly less
3833 numsortkeys = list_length(sortcls);
3834 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3835 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3836 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3837 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3843 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
3844 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
3847 * Check for the possibility of duplicate order-by clauses --- the
3848 * parser should have removed 'em, but no point in sorting
3851 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
3852 exprCollation((Node *) tle->expr),
3853 sortcl->nulls_first,
3855 sortColIdx, sortOperators,
3856 collations, nullsFirst);
3859 Assert(numsortkeys > 0);
3861 return make_sort(root, lefttree, numsortkeys,
3862 sortColIdx, sortOperators, collations,
3867 * make_sort_from_groupcols
3868 * Create sort plan to sort based on grouping columns
3870 * 'groupcls' is the list of SortGroupClauses
3871 * 'grpColIdx' gives the column numbers to use
3873 * This might look like it could be merged with make_sort_from_sortclauses,
3874 * but presently we *must* use the grpColIdx[] array to locate sort columns,
3875 * because the child plan's tlist is not marked with ressortgroupref info
3876 * appropriate to the grouping node. So, only the sort ordering info
3877 * is used from the SortGroupClause entries.
3880 make_sort_from_groupcols(PlannerInfo *root,
3882 AttrNumber *grpColIdx,
3885 List *sub_tlist = lefttree->targetlist;
3889 AttrNumber *sortColIdx;
3895 * We will need at most list_length(groupcls) sort columns; possibly less
3897 numsortkeys = list_length(groupcls);
3898 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3899 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3900 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3901 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3905 foreach(l, groupcls)
3907 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
3908 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
3911 * Check for the possibility of duplicate group-by clauses --- the
3912 * parser should have removed 'em, but no point in sorting
3915 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
3916 exprCollation((Node *) tle->expr),
3919 sortColIdx, sortOperators,
3920 collations, nullsFirst);
3924 Assert(numsortkeys > 0);
3926 return make_sort(root, lefttree, numsortkeys,
3927 sortColIdx, sortOperators, collations,
3932 make_material(Plan *lefttree)
3934 Material *node = makeNode(Material);
3935 Plan *plan = &node->plan;
3937 /* cost should be inserted by caller */
3938 plan->targetlist = lefttree->targetlist;
3940 plan->lefttree = lefttree;
3941 plan->righttree = NULL;
3947 * materialize_finished_plan: stick a Material node atop a completed plan
3949 * There are a couple of places where we want to attach a Material node
3950 * after completion of subquery_planner(). This currently requires hackery.
3951 * Since subquery_planner has already run SS_finalize_plan on the subplan
3952 * tree, we have to kluge up parameter lists for the Material node.
3953 * Possibly this could be fixed by postponing SS_finalize_plan processing
3954 * until setrefs.c is run?
3957 materialize_finished_plan(Plan *subplan)
3960 Path matpath; /* dummy for result of cost_material */
3962 matplan = (Plan *) make_material(subplan);
3965 cost_material(&matpath,
3966 subplan->startup_cost,
3967 subplan->total_cost,
3969 subplan->plan_width);
3970 matplan->startup_cost = matpath.startup_cost;
3971 matplan->total_cost = matpath.total_cost;
3972 matplan->plan_rows = subplan->plan_rows;
3973 matplan->plan_width = subplan->plan_width;
3975 /* parameter kluge --- see comments above */
3976 matplan->extParam = bms_copy(subplan->extParam);
3977 matplan->allParam = bms_copy(subplan->allParam);
3983 make_agg(PlannerInfo *root, List *tlist, List *qual,
3984 AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
3985 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
3989 Agg *node = makeNode(Agg);
3990 Plan *plan = &node->plan;
3991 Path agg_path; /* dummy for result of cost_agg */
3994 node->aggstrategy = aggstrategy;
3995 node->numCols = numGroupCols;
3996 node->grpColIdx = grpColIdx;
3997 node->grpOperators = grpOperators;
3998 node->numGroups = numGroups;
4000 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4001 cost_agg(&agg_path, root,
4002 aggstrategy, aggcosts,
4003 numGroupCols, numGroups,
4004 lefttree->startup_cost,
4005 lefttree->total_cost,
4006 lefttree->plan_rows);
4007 plan->startup_cost = agg_path.startup_cost;
4008 plan->total_cost = agg_path.total_cost;
4011 * We will produce a single output tuple if not grouping, and a tuple per
4014 if (aggstrategy == AGG_PLAIN)
4015 plan->plan_rows = 1;
4017 plan->plan_rows = numGroups;
4020 * We also need to account for the cost of evaluation of the qual (ie, the
4021 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
4022 * anything for Aggref nodes; this is okay since they are really
4023 * comparable to Vars.
4025 * See notes in grouping_planner about why only make_agg, make_windowagg
4026 * and make_group worry about tlist eval cost.
4030 cost_qual_eval(&qual_cost, qual, root);
4031 plan->startup_cost += qual_cost.startup;
4032 plan->total_cost += qual_cost.startup;
4033 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4035 cost_qual_eval(&qual_cost, tlist, root);
4036 plan->startup_cost += qual_cost.startup;
4037 plan->total_cost += qual_cost.startup;
4038 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4041 plan->targetlist = tlist;
4042 plan->lefttree = lefttree;
4043 plan->righttree = NULL;
4049 make_windowagg(PlannerInfo *root, List *tlist,
4050 List *windowFuncs, Index winref,
4051 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
4052 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
4053 int frameOptions, Node *startOffset, Node *endOffset,
4056 WindowAgg *node = makeNode(WindowAgg);
4057 Plan *plan = &node->plan;
4058 Path windowagg_path; /* dummy for result of cost_windowagg */
4061 node->winref = winref;
4062 node->partNumCols = partNumCols;
4063 node->partColIdx = partColIdx;
4064 node->partOperators = partOperators;
4065 node->ordNumCols = ordNumCols;
4066 node->ordColIdx = ordColIdx;
4067 node->ordOperators = ordOperators;
4068 node->frameOptions = frameOptions;
4069 node->startOffset = startOffset;
4070 node->endOffset = endOffset;
4072 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4073 cost_windowagg(&windowagg_path, root,
4074 windowFuncs, partNumCols, ordNumCols,
4075 lefttree->startup_cost,
4076 lefttree->total_cost,
4077 lefttree->plan_rows);
4078 plan->startup_cost = windowagg_path.startup_cost;
4079 plan->total_cost = windowagg_path.total_cost;
4082 * We also need to account for the cost of evaluation of the tlist.
4084 * See notes in grouping_planner about why only make_agg, make_windowagg
4085 * and make_group worry about tlist eval cost.
4087 cost_qual_eval(&qual_cost, tlist, root);
4088 plan->startup_cost += qual_cost.startup;
4089 plan->total_cost += qual_cost.startup;
4090 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4092 plan->targetlist = tlist;
4093 plan->lefttree = lefttree;
4094 plan->righttree = NULL;
4095 /* WindowAgg nodes never have a qual clause */
4102 make_group(PlannerInfo *root,
4106 AttrNumber *grpColIdx,
4111 Group *node = makeNode(Group);
4112 Plan *plan = &node->plan;
4113 Path group_path; /* dummy for result of cost_group */
4116 node->numCols = numGroupCols;
4117 node->grpColIdx = grpColIdx;
4118 node->grpOperators = grpOperators;
4120 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4121 cost_group(&group_path, root,
4122 numGroupCols, numGroups,
4123 lefttree->startup_cost,
4124 lefttree->total_cost,
4125 lefttree->plan_rows);
4126 plan->startup_cost = group_path.startup_cost;
4127 plan->total_cost = group_path.total_cost;
4129 /* One output tuple per estimated result group */
4130 plan->plan_rows = numGroups;
4133 * We also need to account for the cost of evaluation of the qual (ie, the
4134 * HAVING clause) and the tlist.
4136 * XXX this double-counts the cost of evaluation of any expressions used
4137 * for grouping, since in reality those will have been evaluated at a
4138 * lower plan level and will only be copied by the Group node. Worth
4141 * See notes in grouping_planner about why only make_agg, make_windowagg
4142 * and make_group worry about tlist eval cost.
4146 cost_qual_eval(&qual_cost, qual, root);
4147 plan->startup_cost += qual_cost.startup;
4148 plan->total_cost += qual_cost.startup;
4149 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4151 cost_qual_eval(&qual_cost, tlist, root);
4152 plan->startup_cost += qual_cost.startup;
4153 plan->total_cost += qual_cost.startup;
4154 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4157 plan->targetlist = tlist;
4158 plan->lefttree = lefttree;
4159 plan->righttree = NULL;
4165 * distinctList is a list of SortGroupClauses, identifying the targetlist items
4166 * that should be considered by the Unique filter. The input path must
4167 * already be sorted accordingly.
4170 make_unique(Plan *lefttree, List *distinctList)
4172 Unique *node = makeNode(Unique);
4173 Plan *plan = &node->plan;
4174 int numCols = list_length(distinctList);
4176 AttrNumber *uniqColIdx;
4180 copy_plan_costsize(plan, lefttree);
4183 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4184 * all columns get compared at most of the tuples. (XXX probably this is
4187 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
4190 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
4191 * we assume the filter removes nothing. The caller must alter this if he
4192 * has a better idea.
4195 plan->targetlist = lefttree->targetlist;
4197 plan->lefttree = lefttree;
4198 plan->righttree = NULL;
4201 * convert SortGroupClause list into arrays of attr indexes and equality
4202 * operators, as wanted by executor
4204 Assert(numCols > 0);
4205 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4206 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4208 foreach(slitem, distinctList)
4210 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4211 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4213 uniqColIdx[keyno] = tle->resno;
4214 uniqOperators[keyno] = sortcl->eqop;
4215 Assert(OidIsValid(uniqOperators[keyno]));
4219 node->numCols = numCols;
4220 node->uniqColIdx = uniqColIdx;
4221 node->uniqOperators = uniqOperators;
4227 * distinctList is a list of SortGroupClauses, identifying the targetlist
4228 * items that should be considered by the SetOp filter. The input path must
4229 * already be sorted accordingly.
4232 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
4233 List *distinctList, AttrNumber flagColIdx, int firstFlag,
4234 long numGroups, double outputRows)
4236 SetOp *node = makeNode(SetOp);
4237 Plan *plan = &node->plan;
4238 int numCols = list_length(distinctList);
4240 AttrNumber *dupColIdx;
4244 copy_plan_costsize(plan, lefttree);
4245 plan->plan_rows = outputRows;
4248 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4249 * all columns get compared at most of the tuples.
4251 plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
4253 plan->targetlist = lefttree->targetlist;
4255 plan->lefttree = lefttree;
4256 plan->righttree = NULL;
4259 * convert SortGroupClause list into arrays of attr indexes and equality
4260 * operators, as wanted by executor
4262 Assert(numCols > 0);
4263 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4264 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4266 foreach(slitem, distinctList)
4268 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4269 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4271 dupColIdx[keyno] = tle->resno;
4272 dupOperators[keyno] = sortcl->eqop;
4273 Assert(OidIsValid(dupOperators[keyno]));
4278 node->strategy = strategy;
4279 node->numCols = numCols;
4280 node->dupColIdx = dupColIdx;
4281 node->dupOperators = dupOperators;
4282 node->flagColIdx = flagColIdx;
4283 node->firstFlag = firstFlag;
4284 node->numGroups = numGroups;
4291 * Build a LockRows plan node
4294 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
4296 LockRows *node = makeNode(LockRows);
4297 Plan *plan = &node->plan;
4299 copy_plan_costsize(plan, lefttree);
4301 /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
4302 plan->total_cost += cpu_tuple_cost * plan->plan_rows;
4304 plan->targetlist = lefttree->targetlist;
4306 plan->lefttree = lefttree;
4307 plan->righttree = NULL;
4309 node->rowMarks = rowMarks;
4310 node->epqParam = epqParam;
4316 * Note: offset_est and count_est are passed in to save having to repeat
4317 * work already done to estimate the values of the limitOffset and limitCount
4318 * expressions. Their values are as returned by preprocess_limit (0 means
4319 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
4320 * with that function!
4323 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
4324 int64 offset_est, int64 count_est)
4326 Limit *node = makeNode(Limit);
4327 Plan *plan = &node->plan;
4329 copy_plan_costsize(plan, lefttree);
4332 * Adjust the output rows count and costs according to the offset/limit.
4333 * This is only a cosmetic issue if we are at top level, but if we are
4334 * building a subquery then it's important to report correct info to the
4337 * When the offset or count couldn't be estimated, use 10% of the
4338 * estimated number of rows emitted from the subplan.
4340 if (offset_est != 0)
4345 offset_rows = (double) offset_est;
4347 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4348 if (offset_rows > plan->plan_rows)
4349 offset_rows = plan->plan_rows;
4350 if (plan->plan_rows > 0)
4351 plan->startup_cost +=
4352 (plan->total_cost - plan->startup_cost)
4353 * offset_rows / plan->plan_rows;
4354 plan->plan_rows -= offset_rows;
4355 if (plan->plan_rows < 1)
4356 plan->plan_rows = 1;
4364 count_rows = (double) count_est;
4366 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4367 if (count_rows > plan->plan_rows)
4368 count_rows = plan->plan_rows;
4369 if (plan->plan_rows > 0)
4370 plan->total_cost = plan->startup_cost +
4371 (plan->total_cost - plan->startup_cost)
4372 * count_rows / plan->plan_rows;
4373 plan->plan_rows = count_rows;
4374 if (plan->plan_rows < 1)
4375 plan->plan_rows = 1;
4378 plan->targetlist = lefttree->targetlist;
4380 plan->lefttree = lefttree;
4381 plan->righttree = NULL;
4383 node->limitOffset = limitOffset;
4384 node->limitCount = limitCount;
4391 * Build a Result plan node
4393 * If we have a subplan, assume that any evaluation costs for the gating qual
4394 * were already factored into the subplan's startup cost, and just copy the
4395 * subplan cost. If there's no subplan, we should include the qual eval
4396 * cost. In either case, tlist eval cost is not to be included here.
4399 make_result(PlannerInfo *root,
4401 Node *resconstantqual,
4404 Result *node = makeNode(Result);
4405 Plan *plan = &node->plan;
4408 copy_plan_costsize(plan, subplan);
4411 plan->startup_cost = 0;
4412 plan->total_cost = cpu_tuple_cost;
4413 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
4414 plan->plan_width = 0; /* XXX is it worth being smarter? */
4415 if (resconstantqual)
4419 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
4420 /* resconstantqual is evaluated once at startup */
4421 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
4422 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
4426 plan->targetlist = tlist;
4428 plan->lefttree = subplan;
4429 plan->righttree = NULL;
4430 node->resconstantqual = resconstantqual;
4437 * Build a ModifyTable plan node
4439 * Currently, we don't charge anything extra for the actual table modification
4440 * work, nor for the RETURNING expressions if any. It would only be window
4441 * dressing, since these are always top-level nodes and there is no way for
4442 * the costs to change any higher-level planning choices. But we might want
4443 * to make it look better sometime.
4446 make_modifytable(CmdType operation, bool canSetTag,
4447 List *resultRelations,
4448 List *subplans, List *returningLists,
4449 List *rowMarks, int epqParam)
4451 ModifyTable *node = makeNode(ModifyTable);
4452 Plan *plan = &node->plan;
4456 Assert(list_length(resultRelations) == list_length(subplans));
4457 Assert(returningLists == NIL ||
4458 list_length(resultRelations) == list_length(returningLists));
4461 * Compute cost as sum of subplan costs.
4463 plan->startup_cost = 0;
4464 plan->total_cost = 0;
4465 plan->plan_rows = 0;
4467 foreach(subnode, subplans)
4469 Plan *subplan = (Plan *) lfirst(subnode);
4471 if (subnode == list_head(subplans)) /* first node? */
4472 plan->startup_cost = subplan->startup_cost;
4473 plan->total_cost += subplan->total_cost;
4474 plan->plan_rows += subplan->plan_rows;
4475 total_size += subplan->plan_width * subplan->plan_rows;
4477 if (plan->plan_rows > 0)
4478 plan->plan_width = rint(total_size / plan->plan_rows);
4480 plan->plan_width = 0;
4482 node->plan.lefttree = NULL;
4483 node->plan.righttree = NULL;
4484 node->plan.qual = NIL;
4487 * Set up the visible plan targetlist as being the same as the first
4488 * RETURNING list. This is for the use of EXPLAIN; the executor won't pay
4489 * any attention to the targetlist.
4492 node->plan.targetlist = copyObject(linitial(returningLists));
4494 node->plan.targetlist = NIL;
4496 node->operation = operation;
4497 node->canSetTag = canSetTag;
4498 node->resultRelations = resultRelations;
4499 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
4500 node->plans = subplans;
4501 node->returningLists = returningLists;
4502 node->rowMarks = rowMarks;
4503 node->epqParam = epqParam;
4509 * is_projection_capable_plan
4510 * Check whether a given Plan node is able to do projection.
4513 is_projection_capable_plan(Plan *plan)
4515 /* Most plan types can project, so just list the ones that can't */
4516 switch (nodeTag(plan))
4528 case T_RecursiveUnion: