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-2011, 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/plancat.h"
31 #include "optimizer/planmain.h"
32 #include "optimizer/predtest.h"
33 #include "optimizer/restrictinfo.h"
34 #include "optimizer/subselect.h"
35 #include "optimizer/tlist.h"
36 #include "optimizer/var.h"
37 #include "parser/parse_clause.h"
38 #include "parser/parsetree.h"
39 #include "utils/lsyscache.h"
42 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path);
43 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
44 static List *build_relation_tlist(RelOptInfo *rel);
45 static bool use_physical_tlist(PlannerInfo *root, RelOptInfo *rel);
46 static void disuse_physical_tlist(Plan *plan, Path *path);
47 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
48 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
49 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
50 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
51 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
52 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
53 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
54 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
55 List *tlist, List *scan_clauses);
56 static IndexScan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
57 List *tlist, List *scan_clauses);
58 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
59 BitmapHeapPath *best_path,
60 List *tlist, List *scan_clauses);
61 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
62 List **qual, List **indexqual);
63 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
64 List *tlist, List *scan_clauses);
65 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
66 List *tlist, List *scan_clauses);
67 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
68 List *tlist, List *scan_clauses);
69 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
70 List *tlist, List *scan_clauses);
71 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
72 List *tlist, List *scan_clauses);
73 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
74 List *tlist, List *scan_clauses);
75 static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
76 List *tlist, List *scan_clauses);
77 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
78 Plan *outer_plan, Plan *inner_plan);
79 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
80 Plan *outer_plan, Plan *inner_plan);
81 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
82 Plan *outer_plan, Plan *inner_plan);
83 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
84 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
85 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
87 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path,
89 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index);
90 static List *get_switched_clauses(List *clauses, Relids outerrelids);
91 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
92 static void copy_path_costsize(Plan *dest, Path *src);
93 static void copy_plan_costsize(Plan *dest, Plan *src);
94 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
95 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
96 Oid indexid, List *indexqual, List *indexqualorig,
97 List *indexorderby, List *indexorderbyorig,
98 ScanDirection indexscandir, bool indexonly);
99 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
101 List *indexqualorig);
102 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
105 List *bitmapqualorig,
107 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
109 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
110 Index scanrelid, Node *funcexpr, List *funccolnames,
111 List *funccoltypes, List *funccoltypmods,
112 List *funccolcollations);
113 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
114 Index scanrelid, List *values_lists);
115 static CteScan *make_ctescan(List *qptlist, List *qpqual,
116 Index scanrelid, int ctePlanId, int cteParam);
117 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
118 Index scanrelid, int wtParam);
119 static ForeignScan *make_foreignscan(List *qptlist, List *qpqual,
120 Index scanrelid, bool fsSystemCol, FdwPlan *fdwplan);
121 static BitmapAnd *make_bitmap_and(List *bitmapplans);
122 static BitmapOr *make_bitmap_or(List *bitmapplans);
123 static NestLoop *make_nestloop(List *tlist,
124 List *joinclauses, List *otherclauses, List *nestParams,
125 Plan *lefttree, Plan *righttree,
127 static HashJoin *make_hashjoin(List *tlist,
128 List *joinclauses, List *otherclauses,
130 Plan *lefttree, Plan *righttree,
132 static Hash *make_hash(Plan *lefttree,
134 AttrNumber skewColumn,
137 int32 skewColTypmod);
138 static MergeJoin *make_mergejoin(List *tlist,
139 List *joinclauses, List *otherclauses,
142 Oid *mergecollations,
143 int *mergestrategies,
144 bool *mergenullsfirst,
145 Plan *lefttree, Plan *righttree,
147 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
148 AttrNumber *sortColIdx, Oid *sortOperators,
149 Oid *collations, bool *nullsFirst,
150 double limit_tuples);
151 static Plan *prepare_sort_from_pathkeys(PlannerInfo *root,
152 Plan *lefttree, List *pathkeys,
153 bool adjust_tlist_in_place,
155 AttrNumber **p_sortColIdx,
156 Oid **p_sortOperators,
158 bool **p_nullsFirst);
159 static Material *make_material(Plan *lefttree);
164 * Creates the access plan for a query by recursively processing the
165 * desired tree of pathnodes, starting at the node 'best_path'. For
166 * every pathnode found, we create a corresponding plan node containing
167 * appropriate id, target list, and qualification information.
169 * The tlists and quals in the plan tree are still in planner format,
170 * ie, Vars still correspond to the parser's numbering. This will be
171 * fixed later by setrefs.c.
173 * best_path is the best access path
175 * Returns a Plan tree.
178 create_plan(PlannerInfo *root, Path *best_path)
182 /* Initialize this module's private workspace in PlannerInfo */
183 root->curOuterRels = NULL;
184 root->curOuterParams = NIL;
186 /* Recursively process the path tree */
187 plan = create_plan_recurse(root, best_path);
189 /* Check we successfully assigned all NestLoopParams to plan nodes */
190 if (root->curOuterParams != NIL)
191 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
197 * create_plan_recurse
198 * Recursive guts of create_plan().
201 create_plan_recurse(PlannerInfo *root, Path *best_path)
205 switch (best_path->pathtype)
209 case T_BitmapHeapScan:
215 case T_WorkTableScan:
217 plan = create_scan_plan(root, best_path);
222 plan = create_join_plan(root,
223 (JoinPath *) best_path);
226 plan = create_append_plan(root,
227 (AppendPath *) best_path);
230 plan = create_merge_append_plan(root,
231 (MergeAppendPath *) best_path);
234 plan = (Plan *) create_result_plan(root,
235 (ResultPath *) best_path);
238 plan = (Plan *) create_material_plan(root,
239 (MaterialPath *) best_path);
242 plan = create_unique_plan(root,
243 (UniquePath *) best_path);
246 elog(ERROR, "unrecognized node type: %d",
247 (int) best_path->pathtype);
248 plan = NULL; /* keep compiler quiet */
257 * Create a scan plan for the parent relation of 'best_path'.
260 create_scan_plan(PlannerInfo *root, Path *best_path)
262 RelOptInfo *rel = best_path->parent;
268 * For table scans, rather than using the relation targetlist (which is
269 * only those Vars actually needed by the query), we prefer to generate a
270 * tlist containing all Vars in order. This will allow the executor to
271 * optimize away projection of the table tuples, if possible. (Note that
272 * planner.c may replace the tlist we generate here, forcing projection to
275 if (use_physical_tlist(root, rel))
277 tlist = build_physical_tlist(root, rel);
278 /* if fail because of dropped cols, use regular method */
280 tlist = build_relation_tlist(rel);
283 tlist = build_relation_tlist(rel);
286 * Extract the relevant restriction clauses from the parent relation. The
287 * executor must apply all these restrictions during the scan, except for
288 * pseudoconstants which we'll take care of below.
290 scan_clauses = rel->baserestrictinfo;
292 switch (best_path->pathtype)
295 plan = (Plan *) create_seqscan_plan(root,
302 plan = (Plan *) create_indexscan_plan(root,
303 (IndexPath *) best_path,
308 case T_BitmapHeapScan:
309 plan = (Plan *) create_bitmap_scan_plan(root,
310 (BitmapHeapPath *) best_path,
316 plan = (Plan *) create_tidscan_plan(root,
317 (TidPath *) best_path,
323 plan = (Plan *) create_subqueryscan_plan(root,
330 plan = (Plan *) create_functionscan_plan(root,
337 plan = (Plan *) create_valuesscan_plan(root,
344 plan = (Plan *) create_ctescan_plan(root,
350 case T_WorkTableScan:
351 plan = (Plan *) create_worktablescan_plan(root,
358 plan = (Plan *) create_foreignscan_plan(root,
359 (ForeignPath *) best_path,
365 elog(ERROR, "unrecognized node type: %d",
366 (int) best_path->pathtype);
367 plan = NULL; /* keep compiler quiet */
372 * If there are any pseudoconstant clauses attached to this node, insert a
373 * gating Result node that evaluates the pseudoconstants as one-time
376 if (root->hasPseudoConstantQuals)
377 plan = create_gating_plan(root, plan, scan_clauses);
383 * Build a target list (ie, a list of TargetEntry) for a relation.
386 build_relation_tlist(RelOptInfo *rel)
392 foreach(v, rel->reltargetlist)
394 /* Do we really need to copy here? Not sure */
395 Node *node = (Node *) copyObject(lfirst(v));
397 tlist = lappend(tlist, makeTargetEntry((Expr *) node,
408 * Decide whether to use a tlist matching relation structure,
409 * rather than only those Vars actually referenced.
412 use_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
418 * We can do this for real relation scans, subquery scans, function scans,
419 * values scans, and CTE scans (but not for, eg, joins).
421 if (rel->rtekind != RTE_RELATION &&
422 rel->rtekind != RTE_SUBQUERY &&
423 rel->rtekind != RTE_FUNCTION &&
424 rel->rtekind != RTE_VALUES &&
425 rel->rtekind != RTE_CTE)
429 * Can't do it with inheritance cases either (mainly because Append
432 if (rel->reloptkind != RELOPT_BASEREL)
436 * Can't do it if any system columns or whole-row Vars are requested.
437 * (This could possibly be fixed but would take some fragile assumptions
438 * in setrefs.c, I think.)
440 for (i = rel->min_attr; i <= 0; i++)
442 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
447 * Can't do it if the rel is required to emit any placeholder expressions,
450 foreach(lc, root->placeholder_list)
452 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
454 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
455 bms_is_subset(phinfo->ph_eval_at, rel->relids))
463 * disuse_physical_tlist
464 * Switch a plan node back to emitting only Vars actually referenced.
466 * If the plan node immediately above a scan would prefer to get only
467 * needed Vars and not a physical tlist, it must call this routine to
468 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
469 * and Material nodes want this, so they don't have to store useless columns.
472 disuse_physical_tlist(Plan *plan, Path *path)
474 /* Only need to undo it for path types handled by create_scan_plan() */
475 switch (path->pathtype)
479 case T_BitmapHeapScan:
485 case T_WorkTableScan:
487 plan->targetlist = build_relation_tlist(path->parent);
496 * Deal with pseudoconstant qual clauses
498 * If the node's quals list includes any pseudoconstant quals, put them
499 * into a gating Result node atop the already-built plan. Otherwise,
500 * return the plan as-is.
502 * Note that we don't change cost or size estimates when doing gating.
503 * The costs of qual eval were already folded into the plan's startup cost.
504 * Leaving the size alone amounts to assuming that the gating qual will
505 * succeed, which is the conservative estimate for planning upper queries.
506 * We certainly don't want to assume the output size is zero (unless the
507 * gating qual is actually constant FALSE, and that case is dealt with in
508 * clausesel.c). Interpolating between the two cases is silly, because
509 * it doesn't reflect what will really happen at runtime, and besides which
510 * in most cases we have only a very bad idea of the probability of the gating
514 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
516 List *pseudoconstants;
518 /* Sort into desirable execution order while still in RestrictInfo form */
519 quals = order_qual_clauses(root, quals);
521 /* Pull out any pseudoconstant quals from the RestrictInfo list */
522 pseudoconstants = extract_actual_clauses(quals, true);
524 if (!pseudoconstants)
527 return (Plan *) make_result(root,
529 (Node *) pseudoconstants,
535 * Create a join plan for 'best_path' and (recursively) plans for its
536 * inner and outer paths.
539 create_join_plan(PlannerInfo *root, JoinPath *best_path)
544 Relids saveOuterRels = root->curOuterRels;
546 outer_plan = create_plan_recurse(root, best_path->outerjoinpath);
548 /* For a nestloop, include outer relids in curOuterRels for inner side */
549 if (best_path->path.pathtype == T_NestLoop)
550 root->curOuterRels = bms_union(root->curOuterRels,
551 best_path->outerjoinpath->parent->relids);
553 inner_plan = create_plan_recurse(root, best_path->innerjoinpath);
555 switch (best_path->path.pathtype)
558 plan = (Plan *) create_mergejoin_plan(root,
559 (MergePath *) best_path,
564 plan = (Plan *) create_hashjoin_plan(root,
565 (HashPath *) best_path,
570 /* Restore curOuterRels */
571 bms_free(root->curOuterRels);
572 root->curOuterRels = saveOuterRels;
574 plan = (Plan *) create_nestloop_plan(root,
575 (NestPath *) best_path,
580 elog(ERROR, "unrecognized node type: %d",
581 (int) best_path->path.pathtype);
582 plan = NULL; /* keep compiler quiet */
587 * If there are any pseudoconstant clauses attached to this node, insert a
588 * gating Result node that evaluates the pseudoconstants as one-time
591 if (root->hasPseudoConstantQuals)
592 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
597 * * Expensive function pullups may have pulled local predicates * into
598 * this path node. Put them in the qpqual of the plan node. * JMH,
601 if (get_loc_restrictinfo(best_path) != NIL)
602 set_qpqual((Plan) plan,
603 list_concat(get_qpqual((Plan) plan),
604 get_actual_clauses(get_loc_restrictinfo(best_path))));
612 * Create an Append plan for 'best_path' and (recursively) plans
615 * Returns a Plan node.
618 create_append_plan(PlannerInfo *root, AppendPath *best_path)
621 List *tlist = build_relation_tlist(best_path->path.parent);
622 List *subplans = NIL;
626 * It is possible for the subplans list to contain only one entry, or even
627 * no entries. Handle these cases specially.
629 * XXX ideally, if there's just one entry, we'd not bother to generate an
630 * Append node but just return the single child. At the moment this does
631 * not work because the varno of the child scan plan won't match the
632 * parent-rel Vars it'll be asked to emit.
634 if (best_path->subpaths == NIL)
636 /* Generate a Result plan with constant-FALSE gating qual */
637 return (Plan *) make_result(root,
639 (Node *) list_make1(makeBoolConst(false,
644 /* Normal case with multiple subpaths */
645 foreach(subpaths, best_path->subpaths)
647 Path *subpath = (Path *) lfirst(subpaths);
649 subplans = lappend(subplans, create_plan_recurse(root, subpath));
652 plan = make_append(subplans, tlist);
654 return (Plan *) plan;
658 * create_merge_append_plan
659 * Create a MergeAppend plan for 'best_path' and (recursively) plans
662 * Returns a Plan node.
665 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
667 MergeAppend *node = makeNode(MergeAppend);
668 Plan *plan = &node->plan;
669 List *tlist = build_relation_tlist(best_path->path.parent);
670 List *pathkeys = best_path->path.pathkeys;
671 List *subplans = NIL;
675 * We don't have the actual creation of the MergeAppend node split out
676 * into a separate make_xxx function. This is because we want to run
677 * prepare_sort_from_pathkeys on it before we do so on the individual
678 * child plans, to make cross-checking the sort info easier.
680 copy_path_costsize(plan, (Path *) best_path);
681 plan->targetlist = tlist;
683 plan->lefttree = NULL;
684 plan->righttree = NULL;
686 /* Compute sort column info, and adjust MergeAppend's tlist as needed */
687 (void) prepare_sort_from_pathkeys(root, plan, pathkeys,
691 &node->sortOperators,
696 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
697 * even to subplans that don't need an explicit sort, to make sure they
698 * are returning the same sort key columns the MergeAppend expects.
700 foreach(subpaths, best_path->subpaths)
702 Path *subpath = (Path *) lfirst(subpaths);
705 AttrNumber *sortColIdx;
710 /* Build the child plan */
711 subplan = create_plan_recurse(root, subpath);
713 /* Compute sort column info, and adjust subplan's tlist as needed */
714 subplan = prepare_sort_from_pathkeys(root, subplan, pathkeys,
723 * Check that we got the same sort key information. We just Assert
724 * that the sortops match, since those depend only on the pathkeys;
725 * but it seems like a good idea to check the sort column numbers
726 * explicitly, to ensure the tlists really do match up.
728 Assert(numsortkeys == node->numCols);
729 if (memcmp(sortColIdx, node->sortColIdx,
730 numsortkeys * sizeof(AttrNumber)) != 0)
731 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
732 Assert(memcmp(sortOperators, node->sortOperators,
733 numsortkeys * sizeof(Oid)) == 0);
734 Assert(memcmp(collations, node->collations,
735 numsortkeys * sizeof(Oid)) == 0);
736 Assert(memcmp(nullsFirst, node->nullsFirst,
737 numsortkeys * sizeof(bool)) == 0);
739 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
740 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
741 subplan = (Plan *) make_sort(root, subplan, numsortkeys,
742 sortColIdx, sortOperators,
743 collations, nullsFirst,
744 best_path->limit_tuples);
746 subplans = lappend(subplans, subplan);
749 node->mergeplans = subplans;
751 return (Plan *) node;
756 * Create a Result plan for 'best_path'.
757 * This is only used for the case of a query with an empty jointree.
759 * Returns a Plan node.
762 create_result_plan(PlannerInfo *root, ResultPath *best_path)
767 /* The tlist will be installed later, since we have no RelOptInfo */
768 Assert(best_path->path.parent == NULL);
771 /* best_path->quals is just bare clauses */
773 quals = order_qual_clauses(root, best_path->quals);
775 return make_result(root, tlist, (Node *) quals, NULL);
779 * create_material_plan
780 * Create a Material plan for 'best_path' and (recursively) plans
783 * Returns a Plan node.
786 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
791 subplan = create_plan_recurse(root, best_path->subpath);
793 /* We don't want any excess columns in the materialized tuples */
794 disuse_physical_tlist(subplan, best_path->subpath);
796 plan = make_material(subplan);
798 copy_path_costsize(&plan->plan, (Path *) best_path);
805 * Create a Unique plan for 'best_path' and (recursively) plans
808 * Returns a Plan node.
811 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
821 AttrNumber *groupColIdx;
825 subplan = create_plan_recurse(root, best_path->subpath);
827 /* Done if we don't need to do any actual unique-ifying */
828 if (best_path->umethod == UNIQUE_PATH_NOOP)
832 * As constructed, the subplan has a "flat" tlist containing just the Vars
833 * needed here and at upper levels. The values we are supposed to
834 * unique-ify may be expressions in these variables. We have to add any
835 * such expressions to the subplan's tlist.
837 * The subplan may have a "physical" tlist if it is a simple scan plan. If
838 * we're going to sort, this should be reduced to the regular tlist, so
839 * that we don't sort more data than we need to. For hashing, the tlist
840 * should be left as-is if we don't need to add any expressions; but if we
841 * do have to add expressions, then a projection step will be needed at
842 * runtime anyway, so we may as well remove unneeded items. Therefore
843 * newtlist starts from build_relation_tlist() not just a copy of the
844 * subplan's tlist; and we don't install it into the subplan unless we are
845 * sorting or stuff has to be added.
847 in_operators = best_path->in_operators;
848 uniq_exprs = best_path->uniq_exprs;
850 /* initialize modified subplan tlist as just the "required" vars */
851 newtlist = build_relation_tlist(best_path->path.parent);
852 nextresno = list_length(newtlist) + 1;
855 foreach(l, uniq_exprs)
857 Node *uniqexpr = lfirst(l);
860 tle = tlist_member(uniqexpr, newtlist);
863 tle = makeTargetEntry((Expr *) uniqexpr,
867 newtlist = lappend(newtlist, tle);
873 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
876 * If the top plan node can't do projections, we need to add a Result
877 * node to help it along.
879 if (!is_projection_capable_plan(subplan))
880 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
882 subplan->targetlist = newtlist;
886 * Build control information showing which subplan output columns are to
887 * be examined by the grouping step. Unfortunately we can't merge this
888 * with the previous loop, since we didn't then know which version of the
889 * subplan tlist we'd end up using.
891 newtlist = subplan->targetlist;
892 numGroupCols = list_length(uniq_exprs);
893 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
896 foreach(l, uniq_exprs)
898 Node *uniqexpr = lfirst(l);
901 tle = tlist_member(uniqexpr, newtlist);
902 if (!tle) /* shouldn't happen */
903 elog(ERROR, "failed to find unique expression in subplan tlist");
904 groupColIdx[groupColPos++] = tle->resno;
907 if (best_path->umethod == UNIQUE_PATH_HASH)
912 numGroups = (long) Min(best_path->rows, (double) LONG_MAX);
915 * Get the hashable equality operators for the Agg node to use.
916 * Normally these are the same as the IN clause operators, but if
917 * those are cross-type operators then the equality operators are the
918 * ones for the IN clause operators' RHS datatype.
920 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
922 foreach(l, in_operators)
924 Oid in_oper = lfirst_oid(l);
927 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
928 elog(ERROR, "could not find compatible hash operator for operator %u",
930 groupOperators[groupColPos++] = eq_oper;
934 * Since the Agg node is going to project anyway, we can give it the
935 * minimum output tlist, without any stuff we might have added to the
938 plan = (Plan *) make_agg(root,
939 build_relation_tlist(best_path->path.parent),
951 List *sortList = NIL;
953 /* Create an ORDER BY list to sort the input compatibly */
955 foreach(l, in_operators)
957 Oid in_oper = lfirst_oid(l);
961 SortGroupClause *sortcl;
963 sortop = get_ordering_op_for_equality_op(in_oper, false);
964 if (!OidIsValid(sortop)) /* shouldn't happen */
965 elog(ERROR, "could not find ordering operator for equality operator %u",
969 * The Unique node will need equality operators. Normally these
970 * are the same as the IN clause operators, but if those are
971 * cross-type operators then the equality operators are the ones
972 * for the IN clause operators' RHS datatype.
974 eqop = get_equality_op_for_ordering_op(sortop, NULL);
975 if (!OidIsValid(eqop)) /* shouldn't happen */
976 elog(ERROR, "could not find equality operator for ordering operator %u",
979 tle = get_tle_by_resno(subplan->targetlist,
980 groupColIdx[groupColPos]);
983 sortcl = makeNode(SortGroupClause);
984 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
985 subplan->targetlist);
987 sortcl->sortop = sortop;
988 sortcl->nulls_first = false;
989 sortcl->hashable = false; /* no need to make this accurate */
990 sortList = lappend(sortList, sortcl);
993 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
994 plan = (Plan *) make_unique(plan, sortList);
997 /* Adjust output size estimate (other fields should be OK already) */
998 plan->plan_rows = best_path->rows;
1004 /*****************************************************************************
1006 * BASE-RELATION SCAN METHODS
1008 *****************************************************************************/
1012 * create_seqscan_plan
1013 * Returns a seqscan plan for the base relation scanned by 'best_path'
1014 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1017 create_seqscan_plan(PlannerInfo *root, Path *best_path,
1018 List *tlist, List *scan_clauses)
1021 Index scan_relid = best_path->parent->relid;
1023 /* it should be a base rel... */
1024 Assert(scan_relid > 0);
1025 Assert(best_path->parent->rtekind == RTE_RELATION);
1027 /* Sort clauses into best execution order */
1028 scan_clauses = order_qual_clauses(root, scan_clauses);
1030 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1031 scan_clauses = extract_actual_clauses(scan_clauses, false);
1033 scan_plan = make_seqscan(tlist,
1037 copy_path_costsize(&scan_plan->plan, best_path);
1043 * create_indexscan_plan
1044 * Returns an indexscan plan for the base relation scanned by 'best_path'
1045 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1047 * The indexquals list of the path contains implicitly-ANDed qual conditions.
1048 * The list can be empty --- then no index restrictions will be applied during
1052 create_indexscan_plan(PlannerInfo *root,
1053 IndexPath *best_path,
1057 List *indexquals = best_path->indexquals;
1058 List *indexorderbys = best_path->indexorderbys;
1059 Index baserelid = best_path->path.parent->relid;
1060 Oid indexoid = best_path->indexinfo->indexoid;
1062 List *stripped_indexquals;
1063 List *fixed_indexquals;
1064 List *fixed_indexorderbys;
1066 IndexScan *scan_plan;
1068 /* it should be a base rel... */
1069 Assert(baserelid > 0);
1070 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1073 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
1074 * executor as indexqualorig
1076 stripped_indexquals = get_actual_clauses(indexquals);
1079 * The executor needs a copy with the indexkey on the left of each clause
1080 * and with index attr numbers substituted for table ones.
1082 fixed_indexquals = fix_indexqual_references(root, best_path, indexquals);
1085 * Likewise fix up index attr references in the ORDER BY expressions.
1087 fixed_indexorderbys = fix_indexorderby_references(root, best_path, indexorderbys);
1090 * If this is an innerjoin scan, the indexclauses will contain join
1091 * clauses that are not present in scan_clauses (since the passed-in value
1092 * is just the rel's baserestrictinfo list). We must add these clauses to
1093 * scan_clauses to ensure they get checked. In most cases we will remove
1094 * the join clauses again below, but if a join clause contains a special
1095 * operator, we need to make sure it gets into the scan_clauses.
1097 * Note: pointer comparison should be enough to determine RestrictInfo
1100 if (best_path->isjoininner)
1101 scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
1104 * The qpqual list must contain all restrictions not automatically handled
1105 * by the index. All the predicates in the indexquals will be checked
1106 * (either by the index itself, or by nodeIndexscan.c), but if there are
1107 * any "special" operators involved then they must be included in qpqual.
1108 * The upshot is that qpqual must contain scan_clauses minus whatever
1109 * appears in indexquals.
1111 * In normal cases simple pointer equality checks will be enough to spot
1112 * duplicate RestrictInfos, so we try that first. In some situations
1113 * (particularly with OR'd index conditions) we may have scan_clauses that
1114 * are not equal to, but are logically implied by, the index quals; so we
1115 * also try a predicate_implied_by() check to see if we can discard quals
1116 * that way. (predicate_implied_by assumes its first input contains only
1117 * immutable functions, so we have to check that.)
1119 * We can also discard quals that are implied by a partial index's
1120 * predicate, but only in a plain SELECT; when scanning a target relation
1121 * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
1122 * plan so that they'll be properly rechecked by EvalPlanQual testing.
1125 foreach(l, scan_clauses)
1127 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1129 Assert(IsA(rinfo, RestrictInfo));
1130 if (rinfo->pseudoconstant)
1131 continue; /* we may drop pseudoconstants here */
1132 if (list_member_ptr(indexquals, rinfo))
1134 if (!contain_mutable_functions((Node *) rinfo->clause))
1136 List *clausel = list_make1(rinfo->clause);
1138 if (predicate_implied_by(clausel, indexquals))
1140 if (best_path->indexinfo->indpred)
1142 if (baserelid != root->parse->resultRelation &&
1143 get_parse_rowmark(root->parse, baserelid) == NULL)
1144 if (predicate_implied_by(clausel,
1145 best_path->indexinfo->indpred))
1149 qpqual = lappend(qpqual, rinfo);
1152 /* Sort clauses into best execution order */
1153 qpqual = order_qual_clauses(root, qpqual);
1155 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1156 qpqual = extract_actual_clauses(qpqual, false);
1159 * We have to replace any outer-relation variables with nestloop params in
1160 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
1161 * annoying to have to do this separately from the processing in
1162 * fix_indexqual_references --- rethink this when generalizing the inner
1163 * indexscan support. But note we can't really do this earlier because
1164 * it'd break the comparisons to predicates above ... (or would it? Those
1165 * wouldn't have outer refs)
1167 if (best_path->isjoininner)
1169 stripped_indexquals = (List *)
1170 replace_nestloop_params(root, (Node *) stripped_indexquals);
1172 replace_nestloop_params(root, (Node *) qpqual);
1173 indexorderbys = (List *)
1174 replace_nestloop_params(root, (Node *) indexorderbys);
1177 /* Finally ready to build the plan node */
1178 scan_plan = make_indexscan(tlist,
1183 stripped_indexquals,
1184 fixed_indexorderbys,
1186 best_path->indexscandir,
1187 best_path->indexonly);
1189 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1190 /* use the indexscan-specific rows estimate, not the parent rel's */
1191 scan_plan->scan.plan.plan_rows = best_path->rows;
1197 * create_bitmap_scan_plan
1198 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
1199 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1201 static BitmapHeapScan *
1202 create_bitmap_scan_plan(PlannerInfo *root,
1203 BitmapHeapPath *best_path,
1207 Index baserelid = best_path->path.parent->relid;
1208 Plan *bitmapqualplan;
1209 List *bitmapqualorig;
1213 BitmapHeapScan *scan_plan;
1215 /* it should be a base rel... */
1216 Assert(baserelid > 0);
1217 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1219 /* Process the bitmapqual tree into a Plan tree and qual lists */
1220 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1221 &bitmapqualorig, &indexquals);
1223 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1224 scan_clauses = extract_actual_clauses(scan_clauses, false);
1227 * If this is a innerjoin scan, the indexclauses will contain join clauses
1228 * that are not present in scan_clauses (since the passed-in value is just
1229 * the rel's baserestrictinfo list). We must add these clauses to
1230 * scan_clauses to ensure they get checked. In most cases we will remove
1231 * the join clauses again below, but if a join clause contains a special
1232 * operator, we need to make sure it gets into the scan_clauses.
1234 if (best_path->isjoininner)
1236 scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
1240 * The qpqual list must contain all restrictions not automatically handled
1241 * by the index. All the predicates in the indexquals will be checked
1242 * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
1243 * are any "special" operators involved then they must be added to qpqual.
1244 * The upshot is that qpqual must contain scan_clauses minus whatever
1245 * appears in indexquals.
1247 * In normal cases simple equal() checks will be enough to spot duplicate
1248 * clauses, so we try that first. In some situations (particularly with
1249 * OR'd index conditions) we may have scan_clauses that are not equal to,
1250 * but are logically implied by, the index quals; so we also try a
1251 * predicate_implied_by() check to see if we can discard quals that way.
1252 * (predicate_implied_by assumes its first input contains only immutable
1253 * functions, so we have to check that.)
1255 * Unlike create_indexscan_plan(), we need take no special thought here
1256 * for partial index predicates; this is because the predicate conditions
1257 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1258 * to do it that way because predicate conditions need to be rechecked if
1259 * the scan becomes lossy.
1262 foreach(l, scan_clauses)
1264 Node *clause = (Node *) lfirst(l);
1266 if (list_member(indexquals, clause))
1268 if (!contain_mutable_functions(clause))
1270 List *clausel = list_make1(clause);
1272 if (predicate_implied_by(clausel, indexquals))
1275 qpqual = lappend(qpqual, clause);
1278 /* Sort clauses into best execution order */
1279 qpqual = order_qual_clauses(root, qpqual);
1282 * When dealing with special operators, we will at this point have
1283 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
1284 * 'em from bitmapqualorig, since there's no point in making the tests
1287 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1289 /* Finally ready to build the plan node */
1290 scan_plan = make_bitmap_heapscan(tlist,
1296 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1297 /* use the indexscan-specific rows estimate, not the parent rel's */
1298 scan_plan->scan.plan.plan_rows = best_path->rows;
1304 * Given a bitmapqual tree, generate the Plan tree that implements it
1306 * As byproducts, we also return in *qual and *indexqual the qual lists
1307 * (in implicit-AND form, without RestrictInfos) describing the original index
1308 * conditions and the generated indexqual conditions. (These are the same in
1309 * simple cases, but when special index operators are involved, the former
1310 * list includes the special conditions while the latter includes the actual
1311 * indexable conditions derived from them.) Both lists include partial-index
1312 * predicates, because we have to recheck predicates as well as index
1313 * conditions if the bitmap scan becomes lossy.
1315 * Note: if you find yourself changing this, you probably need to change
1316 * make_restrictinfo_from_bitmapqual too.
1319 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1320 List **qual, List **indexqual)
1324 if (IsA(bitmapqual, BitmapAndPath))
1326 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1327 List *subplans = NIL;
1328 List *subquals = NIL;
1329 List *subindexquals = NIL;
1333 * There may well be redundant quals among the subplans, since a
1334 * top-level WHERE qual might have gotten used to form several
1335 * different index quals. We don't try exceedingly hard to eliminate
1336 * redundancies, but we do eliminate obvious duplicates by using
1337 * list_concat_unique.
1339 foreach(l, apath->bitmapquals)
1345 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1346 &subqual, &subindexqual);
1347 subplans = lappend(subplans, subplan);
1348 subquals = list_concat_unique(subquals, subqual);
1349 subindexquals = list_concat_unique(subindexquals, subindexqual);
1351 plan = (Plan *) make_bitmap_and(subplans);
1352 plan->startup_cost = apath->path.startup_cost;
1353 plan->total_cost = apath->path.total_cost;
1355 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1356 plan->plan_width = 0; /* meaningless */
1358 *indexqual = subindexquals;
1360 else if (IsA(bitmapqual, BitmapOrPath))
1362 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1363 List *subplans = NIL;
1364 List *subquals = NIL;
1365 List *subindexquals = NIL;
1366 bool const_true_subqual = false;
1367 bool const_true_subindexqual = false;
1371 * Here, we only detect qual-free subplans. A qual-free subplan would
1372 * cause us to generate "... OR true ..." which we may as well reduce
1373 * to just "true". We do not try to eliminate redundant subclauses
1374 * because (a) it's not as likely as in the AND case, and (b) we might
1375 * well be working with hundreds or even thousands of OR conditions,
1376 * perhaps from a long IN list. The performance of list_append_unique
1377 * would be unacceptable.
1379 foreach(l, opath->bitmapquals)
1385 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1386 &subqual, &subindexqual);
1387 subplans = lappend(subplans, subplan);
1389 const_true_subqual = true;
1390 else if (!const_true_subqual)
1391 subquals = lappend(subquals,
1392 make_ands_explicit(subqual));
1393 if (subindexqual == NIL)
1394 const_true_subindexqual = true;
1395 else if (!const_true_subindexqual)
1396 subindexquals = lappend(subindexquals,
1397 make_ands_explicit(subindexqual));
1401 * In the presence of ScalarArrayOpExpr quals, we might have built
1402 * BitmapOrPaths with just one subpath; don't add an OR step.
1404 if (list_length(subplans) == 1)
1406 plan = (Plan *) linitial(subplans);
1410 plan = (Plan *) make_bitmap_or(subplans);
1411 plan->startup_cost = opath->path.startup_cost;
1412 plan->total_cost = opath->path.total_cost;
1414 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1415 plan->plan_width = 0; /* meaningless */
1419 * If there were constant-TRUE subquals, the OR reduces to constant
1420 * TRUE. Also, avoid generating one-element ORs, which could happen
1421 * due to redundancy elimination or ScalarArrayOpExpr quals.
1423 if (const_true_subqual)
1425 else if (list_length(subquals) <= 1)
1428 *qual = list_make1(make_orclause(subquals));
1429 if (const_true_subindexqual)
1431 else if (list_length(subindexquals) <= 1)
1432 *indexqual = subindexquals;
1434 *indexqual = list_make1(make_orclause(subindexquals));
1436 else if (IsA(bitmapqual, IndexPath))
1438 IndexPath *ipath = (IndexPath *) bitmapqual;
1442 /* Use the regular indexscan plan build machinery... */
1443 iscan = create_indexscan_plan(root, ipath, NIL, NIL);
1444 /* then convert to a bitmap indexscan */
1445 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1448 iscan->indexqualorig);
1449 plan->startup_cost = 0.0;
1450 plan->total_cost = ipath->indextotalcost;
1452 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1453 plan->plan_width = 0; /* meaningless */
1454 *qual = get_actual_clauses(ipath->indexclauses);
1455 *indexqual = get_actual_clauses(ipath->indexquals);
1456 foreach(l, ipath->indexinfo->indpred)
1458 Expr *pred = (Expr *) lfirst(l);
1461 * We know that the index predicate must have been implied by the
1462 * query condition as a whole, but it may or may not be implied by
1463 * the conditions that got pushed into the bitmapqual. Avoid
1464 * generating redundant conditions.
1466 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1468 *qual = lappend(*qual, pred);
1469 *indexqual = lappend(*indexqual, pred);
1474 * Replace outer-relation variables with nestloop params, but only
1475 * after doing the above comparisons to index predicates.
1477 if (ipath->isjoininner)
1480 replace_nestloop_params(root, (Node *) *qual);
1481 *indexqual = (List *)
1482 replace_nestloop_params(root, (Node *) *indexqual);
1487 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1488 plan = NULL; /* keep compiler quiet */
1495 * create_tidscan_plan
1496 * Returns a tidscan plan for the base relation scanned by 'best_path'
1497 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1500 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1501 List *tlist, List *scan_clauses)
1504 Index scan_relid = best_path->path.parent->relid;
1507 /* it should be a base rel... */
1508 Assert(scan_relid > 0);
1509 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1511 /* Sort clauses into best execution order */
1512 scan_clauses = order_qual_clauses(root, scan_clauses);
1514 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1515 scan_clauses = extract_actual_clauses(scan_clauses, false);
1518 * Remove any clauses that are TID quals. This is a bit tricky since the
1519 * tidquals list has implicit OR semantics.
1521 ortidquals = best_path->tidquals;
1522 if (list_length(ortidquals) > 1)
1523 ortidquals = list_make1(make_orclause(ortidquals));
1524 scan_clauses = list_difference(scan_clauses, ortidquals);
1526 scan_plan = make_tidscan(tlist,
1529 best_path->tidquals);
1531 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1537 * create_subqueryscan_plan
1538 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1539 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1541 static SubqueryScan *
1542 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1543 List *tlist, List *scan_clauses)
1545 SubqueryScan *scan_plan;
1546 Index scan_relid = best_path->parent->relid;
1548 /* it should be a subquery base rel... */
1549 Assert(scan_relid > 0);
1550 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1552 /* Sort clauses into best execution order */
1553 scan_clauses = order_qual_clauses(root, scan_clauses);
1555 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1556 scan_clauses = extract_actual_clauses(scan_clauses, false);
1558 scan_plan = make_subqueryscan(tlist,
1561 best_path->parent->subplan);
1563 copy_path_costsize(&scan_plan->scan.plan, best_path);
1569 * create_functionscan_plan
1570 * Returns a functionscan plan for the base relation scanned by 'best_path'
1571 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1573 static FunctionScan *
1574 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1575 List *tlist, List *scan_clauses)
1577 FunctionScan *scan_plan;
1578 Index scan_relid = best_path->parent->relid;
1581 /* it should be a function base rel... */
1582 Assert(scan_relid > 0);
1583 rte = planner_rt_fetch(scan_relid, root);
1584 Assert(rte->rtekind == RTE_FUNCTION);
1586 /* Sort clauses into best execution order */
1587 scan_clauses = order_qual_clauses(root, scan_clauses);
1589 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1590 scan_clauses = extract_actual_clauses(scan_clauses, false);
1592 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1594 rte->eref->colnames,
1596 rte->funccoltypmods,
1597 rte->funccolcollations);
1599 copy_path_costsize(&scan_plan->scan.plan, best_path);
1605 * create_valuesscan_plan
1606 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1607 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1610 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1611 List *tlist, List *scan_clauses)
1613 ValuesScan *scan_plan;
1614 Index scan_relid = best_path->parent->relid;
1617 /* it should be a values base rel... */
1618 Assert(scan_relid > 0);
1619 rte = planner_rt_fetch(scan_relid, root);
1620 Assert(rte->rtekind == RTE_VALUES);
1622 /* Sort clauses into best execution order */
1623 scan_clauses = order_qual_clauses(root, scan_clauses);
1625 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1626 scan_clauses = extract_actual_clauses(scan_clauses, false);
1628 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1631 copy_path_costsize(&scan_plan->scan.plan, best_path);
1637 * create_ctescan_plan
1638 * Returns a ctescan plan for the base relation scanned by 'best_path'
1639 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1642 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1643 List *tlist, List *scan_clauses)
1646 Index scan_relid = best_path->parent->relid;
1648 SubPlan *ctesplan = NULL;
1651 PlannerInfo *cteroot;
1656 Assert(scan_relid > 0);
1657 rte = planner_rt_fetch(scan_relid, root);
1658 Assert(rte->rtekind == RTE_CTE);
1659 Assert(!rte->self_reference);
1662 * Find the referenced CTE, and locate the SubPlan previously made for it.
1664 levelsup = rte->ctelevelsup;
1666 while (levelsup-- > 0)
1668 cteroot = cteroot->parent_root;
1669 if (!cteroot) /* shouldn't happen */
1670 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1674 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1675 * on planning the CTEs (ie, this is a side-reference from another CTE).
1676 * So we mustn't use forboth here.
1679 foreach(lc, cteroot->parse->cteList)
1681 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1683 if (strcmp(cte->ctename, rte->ctename) == 0)
1687 if (lc == NULL) /* shouldn't happen */
1688 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1689 if (ndx >= list_length(cteroot->cte_plan_ids))
1690 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1691 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1692 Assert(plan_id > 0);
1693 foreach(lc, cteroot->init_plans)
1695 ctesplan = (SubPlan *) lfirst(lc);
1696 if (ctesplan->plan_id == plan_id)
1699 if (lc == NULL) /* shouldn't happen */
1700 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1703 * We need the CTE param ID, which is the sole member of the SubPlan's
1706 cte_param_id = linitial_int(ctesplan->setParam);
1708 /* Sort clauses into best execution order */
1709 scan_clauses = order_qual_clauses(root, scan_clauses);
1711 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1712 scan_clauses = extract_actual_clauses(scan_clauses, false);
1714 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1715 plan_id, cte_param_id);
1717 copy_path_costsize(&scan_plan->scan.plan, best_path);
1723 * create_worktablescan_plan
1724 * Returns a worktablescan plan for the base relation scanned by 'best_path'
1725 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1727 static WorkTableScan *
1728 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1729 List *tlist, List *scan_clauses)
1731 WorkTableScan *scan_plan;
1732 Index scan_relid = best_path->parent->relid;
1735 PlannerInfo *cteroot;
1737 Assert(scan_relid > 0);
1738 rte = planner_rt_fetch(scan_relid, root);
1739 Assert(rte->rtekind == RTE_CTE);
1740 Assert(rte->self_reference);
1743 * We need to find the worktable param ID, which is in the plan level
1744 * that's processing the recursive UNION, which is one level *below* where
1745 * the CTE comes from.
1747 levelsup = rte->ctelevelsup;
1748 if (levelsup == 0) /* shouldn't happen */
1749 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1752 while (levelsup-- > 0)
1754 cteroot = cteroot->parent_root;
1755 if (!cteroot) /* shouldn't happen */
1756 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1758 if (cteroot->wt_param_id < 0) /* shouldn't happen */
1759 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1761 /* Sort clauses into best execution order */
1762 scan_clauses = order_qual_clauses(root, scan_clauses);
1764 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1765 scan_clauses = extract_actual_clauses(scan_clauses, false);
1767 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
1768 cteroot->wt_param_id);
1770 copy_path_costsize(&scan_plan->scan.plan, best_path);
1776 * create_foreignscan_plan
1777 * Returns a foreignscan plan for the base relation scanned by 'best_path'
1778 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1780 static ForeignScan *
1781 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
1782 List *tlist, List *scan_clauses)
1784 ForeignScan *scan_plan;
1785 RelOptInfo *rel = best_path->path.parent;
1786 Index scan_relid = rel->relid;
1791 /* it should be a base rel... */
1792 Assert(scan_relid > 0);
1793 Assert(rel->rtekind == RTE_RELATION);
1794 rte = planner_rt_fetch(scan_relid, root);
1795 Assert(rte->rtekind == RTE_RELATION);
1797 /* Sort clauses into best execution order */
1798 scan_clauses = order_qual_clauses(root, scan_clauses);
1800 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1801 scan_clauses = extract_actual_clauses(scan_clauses, false);
1803 /* Detect whether any system columns are requested from rel */
1804 fsSystemCol = false;
1805 for (i = rel->min_attr; i < 0; i++)
1807 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
1814 scan_plan = make_foreignscan(tlist,
1818 best_path->fdwplan);
1820 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1826 /*****************************************************************************
1830 *****************************************************************************/
1833 create_nestloop_plan(PlannerInfo *root,
1834 NestPath *best_path,
1838 NestLoop *join_plan;
1839 List *tlist = build_relation_tlist(best_path->path.parent);
1840 List *joinrestrictclauses = best_path->joinrestrictinfo;
1850 * If the inner path is a nestloop inner indexscan, it might be using some
1851 * of the join quals as index quals, in which case we don't have to check
1852 * them again at the join node. Remove any join quals that are redundant.
1854 joinrestrictclauses =
1855 select_nonredundant_join_clauses(root,
1856 joinrestrictclauses,
1857 best_path->innerjoinpath);
1859 /* Sort join qual clauses into best execution order */
1860 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
1862 /* Get the join qual clauses (in plain expression form) */
1863 /* Any pseudoconstant clauses are ignored here */
1864 if (IS_OUTER_JOIN(best_path->jointype))
1866 extract_actual_join_clauses(joinrestrictclauses,
1867 &joinclauses, &otherclauses);
1871 /* We can treat all clauses alike for an inner join */
1872 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
1877 * Identify any nestloop parameters that should be supplied by this join
1878 * node, and move them from root->curOuterParams to the nestParams list.
1880 outerrelids = best_path->outerjoinpath->parent->relids;
1883 for (cell = list_head(root->curOuterParams); cell; cell = next)
1885 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
1888 if (bms_is_member(nlp->paramval->varno, outerrelids))
1890 root->curOuterParams = list_delete_cell(root->curOuterParams,
1892 nestParams = lappend(nestParams, nlp);
1898 join_plan = make_nestloop(tlist,
1904 best_path->jointype);
1906 copy_path_costsize(&join_plan->join.plan, &best_path->path);
1912 create_mergejoin_plan(PlannerInfo *root,
1913 MergePath *best_path,
1917 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1921 List *outerpathkeys;
1922 List *innerpathkeys;
1925 Oid *mergecollations;
1926 int *mergestrategies;
1927 bool *mergenullsfirst;
1928 MergeJoin *join_plan;
1934 /* Sort join qual clauses into best execution order */
1935 /* NB: do NOT reorder the mergeclauses */
1936 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1938 /* Get the join qual clauses (in plain expression form) */
1939 /* Any pseudoconstant clauses are ignored here */
1940 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1942 extract_actual_join_clauses(joinclauses,
1943 &joinclauses, &otherclauses);
1947 /* We can treat all clauses alike for an inner join */
1948 joinclauses = extract_actual_clauses(joinclauses, false);
1953 * Remove the mergeclauses from the list of join qual clauses, leaving the
1954 * list of quals that must be checked as qpquals.
1956 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
1957 joinclauses = list_difference(joinclauses, mergeclauses);
1960 * Rearrange mergeclauses, if needed, so that the outer variable is always
1961 * on the left; mark the mergeclause restrictinfos with correct
1962 * outer_is_left status.
1964 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
1965 best_path->jpath.outerjoinpath->parent->relids);
1968 * Create explicit sort nodes for the outer and inner paths if necessary.
1969 * Make sure there are no excess columns in the inputs if sorting.
1971 if (best_path->outersortkeys)
1973 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1974 outer_plan = (Plan *)
1975 make_sort_from_pathkeys(root,
1977 best_path->outersortkeys,
1979 outerpathkeys = best_path->outersortkeys;
1982 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
1984 if (best_path->innersortkeys)
1986 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1987 inner_plan = (Plan *)
1988 make_sort_from_pathkeys(root,
1990 best_path->innersortkeys,
1992 innerpathkeys = best_path->innersortkeys;
1995 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
1998 * If specified, add a materialize node to shield the inner plan from the
1999 * need to handle mark/restore.
2001 if (best_path->materialize_inner)
2003 Plan *matplan = (Plan *) make_material(inner_plan);
2006 * We assume the materialize will not spill to disk, and therefore
2007 * charge just cpu_operator_cost per tuple. (Keep this estimate in
2008 * sync with cost_mergejoin.)
2010 copy_plan_costsize(matplan, inner_plan);
2011 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
2013 inner_plan = matplan;
2017 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
2018 * executor. The information is in the pathkeys for the two inputs, but
2019 * we need to be careful about the possibility of mergeclauses sharing a
2020 * pathkey (compare find_mergeclauses_for_pathkeys()).
2022 nClauses = list_length(mergeclauses);
2023 Assert(nClauses == list_length(best_path->path_mergeclauses));
2024 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
2025 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
2026 mergestrategies = (int *) palloc(nClauses * sizeof(int));
2027 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
2029 lop = list_head(outerpathkeys);
2030 lip = list_head(innerpathkeys);
2032 foreach(lc, best_path->path_mergeclauses)
2034 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2035 EquivalenceClass *oeclass;
2036 EquivalenceClass *ieclass;
2039 EquivalenceClass *opeclass;
2040 EquivalenceClass *ipeclass;
2043 /* fetch outer/inner eclass from mergeclause */
2044 Assert(IsA(rinfo, RestrictInfo));
2045 if (rinfo->outer_is_left)
2047 oeclass = rinfo->left_ec;
2048 ieclass = rinfo->right_ec;
2052 oeclass = rinfo->right_ec;
2053 ieclass = rinfo->left_ec;
2055 Assert(oeclass != NULL);
2056 Assert(ieclass != NULL);
2059 * For debugging purposes, we check that the eclasses match the paths'
2060 * pathkeys. In typical cases the merge clauses are one-to-one with
2061 * the pathkeys, but when dealing with partially redundant query
2062 * conditions, we might have clauses that re-reference earlier path
2063 * keys. The case that we need to reject is where a pathkey is
2064 * entirely skipped over.
2066 * lop and lip reference the first as-yet-unused pathkey elements;
2067 * it's okay to match them, or any element before them. If they're
2068 * NULL then we have found all pathkey elements to be used.
2072 opathkey = (PathKey *) lfirst(lop);
2073 opeclass = opathkey->pk_eclass;
2074 if (oeclass == opeclass)
2076 /* fast path for typical case */
2081 /* redundant clauses ... must match something before lop */
2082 foreach(l2, outerpathkeys)
2086 opathkey = (PathKey *) lfirst(l2);
2087 opeclass = opathkey->pk_eclass;
2088 if (oeclass == opeclass)
2091 if (oeclass != opeclass)
2092 elog(ERROR, "outer pathkeys do not match mergeclauses");
2097 /* redundant clauses ... must match some already-used pathkey */
2100 foreach(l2, outerpathkeys)
2102 opathkey = (PathKey *) lfirst(l2);
2103 opeclass = opathkey->pk_eclass;
2104 if (oeclass == opeclass)
2108 elog(ERROR, "outer pathkeys do not match mergeclauses");
2113 ipathkey = (PathKey *) lfirst(lip);
2114 ipeclass = ipathkey->pk_eclass;
2115 if (ieclass == ipeclass)
2117 /* fast path for typical case */
2122 /* redundant clauses ... must match something before lip */
2123 foreach(l2, innerpathkeys)
2127 ipathkey = (PathKey *) lfirst(l2);
2128 ipeclass = ipathkey->pk_eclass;
2129 if (ieclass == ipeclass)
2132 if (ieclass != ipeclass)
2133 elog(ERROR, "inner pathkeys do not match mergeclauses");
2138 /* redundant clauses ... must match some already-used pathkey */
2141 foreach(l2, innerpathkeys)
2143 ipathkey = (PathKey *) lfirst(l2);
2144 ipeclass = ipathkey->pk_eclass;
2145 if (ieclass == ipeclass)
2149 elog(ERROR, "inner pathkeys do not match mergeclauses");
2152 /* pathkeys should match each other too (more debugging) */
2153 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
2154 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
2155 opathkey->pk_strategy != ipathkey->pk_strategy ||
2156 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
2157 elog(ERROR, "left and right pathkeys do not match in mergejoin");
2159 /* OK, save info for executor */
2160 mergefamilies[i] = opathkey->pk_opfamily;
2161 mergecollations[i] = opathkey->pk_eclass->ec_collation;
2162 mergestrategies[i] = opathkey->pk_strategy;
2163 mergenullsfirst[i] = opathkey->pk_nulls_first;
2168 * Note: it is not an error if we have additional pathkey elements (i.e.,
2169 * lop or lip isn't NULL here). The input paths might be better-sorted
2170 * than we need for the current mergejoin.
2174 * Now we can build the mergejoin node.
2176 join_plan = make_mergejoin(tlist,
2186 best_path->jpath.jointype);
2188 /* Costs of sort and material steps are included in path cost already */
2189 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2195 create_hashjoin_plan(PlannerInfo *root,
2196 HashPath *best_path,
2200 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
2204 Oid skewTable = InvalidOid;
2205 AttrNumber skewColumn = InvalidAttrNumber;
2206 bool skewInherit = false;
2207 Oid skewColType = InvalidOid;
2208 int32 skewColTypmod = -1;
2209 HashJoin *join_plan;
2212 /* Sort join qual clauses into best execution order */
2213 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2214 /* There's no point in sorting the hash clauses ... */
2216 /* Get the join qual clauses (in plain expression form) */
2217 /* Any pseudoconstant clauses are ignored here */
2218 if (IS_OUTER_JOIN(best_path->jpath.jointype))
2220 extract_actual_join_clauses(joinclauses,
2221 &joinclauses, &otherclauses);
2225 /* We can treat all clauses alike for an inner join */
2226 joinclauses = extract_actual_clauses(joinclauses, false);
2231 * Remove the hashclauses from the list of join qual clauses, leaving the
2232 * list of quals that must be checked as qpquals.
2234 hashclauses = get_actual_clauses(best_path->path_hashclauses);
2235 joinclauses = list_difference(joinclauses, hashclauses);
2238 * Rearrange hashclauses, if needed, so that the outer variable is always
2241 hashclauses = get_switched_clauses(best_path->path_hashclauses,
2242 best_path->jpath.outerjoinpath->parent->relids);
2244 /* We don't want any excess columns in the hashed tuples */
2245 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
2247 /* If we expect batching, suppress excess columns in outer tuples too */
2248 if (best_path->num_batches > 1)
2249 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
2252 * If there is a single join clause and we can identify the outer variable
2253 * as a simple column reference, supply its identity for possible use in
2254 * skew optimization. (Note: in principle we could do skew optimization
2255 * with multiple join clauses, but we'd have to be able to determine the
2256 * most common combinations of outer values, which we don't currently have
2257 * enough stats for.)
2259 if (list_length(hashclauses) == 1)
2261 OpExpr *clause = (OpExpr *) linitial(hashclauses);
2264 Assert(is_opclause(clause));
2265 node = (Node *) linitial(clause->args);
2266 if (IsA(node, RelabelType))
2267 node = (Node *) ((RelabelType *) node)->arg;
2270 Var *var = (Var *) node;
2273 rte = root->simple_rte_array[var->varno];
2274 if (rte->rtekind == RTE_RELATION)
2276 skewTable = rte->relid;
2277 skewColumn = var->varattno;
2278 skewInherit = rte->inh;
2279 skewColType = var->vartype;
2280 skewColTypmod = var->vartypmod;
2286 * Build the hash node and hash join node.
2288 hash_plan = make_hash(inner_plan,
2294 join_plan = make_hashjoin(tlist,
2300 best_path->jpath.jointype);
2302 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2308 /*****************************************************************************
2310 * SUPPORTING ROUTINES
2312 *****************************************************************************/
2315 * replace_nestloop_params
2316 * Replace outer-relation Vars in the given expression with nestloop Params
2318 * All Vars belonging to the relation(s) identified by root->curOuterRels
2319 * are replaced by Params, and entries are added to root->curOuterParams if
2320 * not already present.
2323 replace_nestloop_params(PlannerInfo *root, Node *expr)
2325 /* No setup needed for tree walk, so away we go */
2326 return replace_nestloop_params_mutator(expr, root);
2330 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
2336 Var *var = (Var *) node;
2341 /* Upper-level Vars should be long gone at this point */
2342 Assert(var->varlevelsup == 0);
2343 /* If not to be replaced, we can just return the Var unmodified */
2344 if (!bms_is_member(var->varno, root->curOuterRels))
2346 /* Create a Param representing the Var */
2347 param = assign_nestloop_param(root, var);
2348 /* Is this param already listed in root->curOuterParams? */
2349 foreach(lc, root->curOuterParams)
2351 nlp = (NestLoopParam *) lfirst(lc);
2352 if (nlp->paramno == param->paramid)
2354 Assert(equal(var, nlp->paramval));
2355 /* Present, so we can just return the Param */
2356 return (Node *) param;
2360 nlp = makeNode(NestLoopParam);
2361 nlp->paramno = param->paramid;
2362 nlp->paramval = var;
2363 root->curOuterParams = lappend(root->curOuterParams, nlp);
2364 /* And return the replacement Param */
2365 return (Node *) param;
2367 return expression_tree_mutator(node,
2368 replace_nestloop_params_mutator,
2373 * fix_indexqual_references
2374 * Adjust indexqual clauses to the form the executor's indexqual
2377 * We have four tasks here:
2378 * * Remove RestrictInfo nodes from the input clauses.
2379 * * Replace any outer-relation Var nodes with nestloop Params.
2380 * (XXX eventually, that responsibility should go elsewhere?)
2381 * * Index keys must be represented by Var nodes with varattno set to the
2382 * index's attribute number, not the attribute number in the original rel.
2383 * * If the index key is on the right, commute the clause to put it on the
2386 * The result is a modified copy of the indexquals list --- the
2387 * original is not changed. Note also that the copy shares no substructure
2388 * with the original; this is needed in case there is a subplan in it (we need
2389 * two separate copies of the subplan tree, or things will go awry).
2392 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
2395 IndexOptInfo *index = index_path->indexinfo;
2396 List *fixed_indexquals;
2399 fixed_indexquals = NIL;
2401 foreach(l, indexquals)
2403 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2406 Assert(IsA(rinfo, RestrictInfo));
2409 * Replace any outer-relation variables with nestloop params.
2411 * This also makes a copy of the clause, so it's safe to modify it
2414 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
2416 if (IsA(clause, OpExpr))
2418 OpExpr *op = (OpExpr *) clause;
2420 if (list_length(op->args) != 2)
2421 elog(ERROR, "indexqual clause is not binary opclause");
2424 * Check to see if the indexkey is on the right; if so, commute
2425 * the clause. The indexkey should be the side that refers to
2426 * (only) the base relation.
2428 if (!bms_equal(rinfo->left_relids, index->rel->relids))
2432 * Now, determine which index attribute this is and change the
2433 * indexkey operand as needed.
2435 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2438 else if (IsA(clause, RowCompareExpr))
2440 RowCompareExpr *rc = (RowCompareExpr *) clause;
2444 * Check to see if the indexkey is on the right; if so, commute
2445 * the clause. The indexkey should be the side that refers to
2446 * (only) the base relation.
2448 if (!bms_overlap(pull_varnos(linitial(rc->largs)),
2449 index->rel->relids))
2450 CommuteRowCompareExpr(rc);
2453 * For each column in the row comparison, determine which index
2454 * attribute this is and change the indexkey operand as needed.
2456 foreach(lc, rc->largs)
2458 lfirst(lc) = fix_indexqual_operand(lfirst(lc),
2462 else if (IsA(clause, ScalarArrayOpExpr))
2464 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2466 /* Never need to commute... */
2469 * Determine which index attribute this is and change the indexkey
2470 * operand as needed.
2472 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2475 else if (IsA(clause, NullTest))
2477 NullTest *nt = (NullTest *) clause;
2479 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
2483 elog(ERROR, "unsupported indexqual type: %d",
2484 (int) nodeTag(clause));
2486 fixed_indexquals = lappend(fixed_indexquals, clause);
2489 return fixed_indexquals;
2493 * fix_indexorderby_references
2494 * Adjust indexorderby clauses to the form the executor's index
2497 * This is a simplified version of fix_indexqual_references. The input does
2498 * not have RestrictInfo nodes, and we assume that indxqual.c already
2499 * commuted the clauses to put the index keys on the left. Also, we don't
2500 * bother to support any cases except simple OpExprs, since nothing else
2501 * is allowed for ordering operators.
2504 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path,
2505 List *indexorderbys)
2507 IndexOptInfo *index = index_path->indexinfo;
2508 List *fixed_indexorderbys;
2511 fixed_indexorderbys = NIL;
2513 foreach(l, indexorderbys)
2515 Node *clause = (Node *) lfirst(l);
2518 * Replace any outer-relation variables with nestloop params.
2520 * This also makes a copy of the clause, so it's safe to modify it
2523 clause = replace_nestloop_params(root, clause);
2525 if (IsA(clause, OpExpr))
2527 OpExpr *op = (OpExpr *) clause;
2529 if (list_length(op->args) != 2)
2530 elog(ERROR, "indexorderby clause is not binary opclause");
2533 * Now, determine which index attribute this is and change the
2534 * indexkey operand as needed.
2536 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2540 elog(ERROR, "unsupported indexorderby type: %d",
2541 (int) nodeTag(clause));
2543 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
2546 return fixed_indexorderbys;
2550 * fix_indexqual_operand
2551 * Convert an indexqual expression to a Var referencing the index column.
2554 fix_indexqual_operand(Node *node, IndexOptInfo *index)
2557 * We represent index keys by Var nodes having the varno of the base table
2558 * but varattno equal to the index's attribute number (index column
2559 * position). This is a bit hokey ... would be cleaner to use a
2560 * special-purpose node type that could not be mistaken for a regular Var.
2561 * But it will do for now.
2565 ListCell *indexpr_item;
2568 * Remove any binary-compatible relabeling of the indexkey
2570 if (IsA(node, RelabelType))
2571 node = (Node *) ((RelabelType *) node)->arg;
2573 if (IsA(node, Var) &&
2574 ((Var *) node)->varno == index->rel->relid)
2576 /* Try to match against simple index columns */
2577 int varatt = ((Var *) node)->varattno;
2581 for (pos = 0; pos < index->ncolumns; pos++)
2583 if (index->indexkeys[pos] == varatt)
2585 result = (Var *) copyObject(node);
2586 result->varattno = pos + 1;
2587 return (Node *) result;
2593 /* Try to match against index expressions */
2594 indexpr_item = list_head(index->indexprs);
2595 for (pos = 0; pos < index->ncolumns; pos++)
2597 if (index->indexkeys[pos] == 0)
2601 if (indexpr_item == NULL)
2602 elog(ERROR, "too few entries in indexprs list");
2603 indexkey = (Node *) lfirst(indexpr_item);
2604 if (indexkey && IsA(indexkey, RelabelType))
2605 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2606 if (equal(node, indexkey))
2609 result = makeVar(index->rel->relid, pos + 1,
2610 exprType(lfirst(indexpr_item)), -1,
2611 exprCollation(lfirst(indexpr_item)),
2613 return (Node *) result;
2615 indexpr_item = lnext(indexpr_item);
2620 elog(ERROR, "node is not an index attribute");
2621 return NULL; /* keep compiler quiet */
2625 * get_switched_clauses
2626 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2627 * extract the bare clauses, and rearrange the elements within the
2628 * clauses, if needed, so the outer join variable is on the left and
2629 * the inner is on the right. The original clause data structure is not
2630 * touched; a modified list is returned. We do, however, set the transient
2631 * outer_is_left field in each RestrictInfo to show which side was which.
2634 get_switched_clauses(List *clauses, Relids outerrelids)
2641 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
2642 OpExpr *clause = (OpExpr *) restrictinfo->clause;
2644 Assert(is_opclause(clause));
2645 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
2648 * Duplicate just enough of the structure to allow commuting the
2649 * clause without changing the original list. Could use
2650 * copyObject, but a complete deep copy is overkill.
2652 OpExpr *temp = makeNode(OpExpr);
2654 temp->opno = clause->opno;
2655 temp->opfuncid = InvalidOid;
2656 temp->opresulttype = clause->opresulttype;
2657 temp->opretset = clause->opretset;
2658 temp->opcollid = clause->opcollid;
2659 temp->inputcollid = clause->inputcollid;
2660 temp->args = list_copy(clause->args);
2661 temp->location = clause->location;
2662 /* Commute it --- note this modifies the temp node in-place. */
2663 CommuteOpExpr(temp);
2664 t_list = lappend(t_list, temp);
2665 restrictinfo->outer_is_left = false;
2669 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
2670 t_list = lappend(t_list, clause);
2671 restrictinfo->outer_is_left = true;
2678 * order_qual_clauses
2679 * Given a list of qual clauses that will all be evaluated at the same
2680 * plan node, sort the list into the order we want to check the quals
2683 * Ideally the order should be driven by a combination of execution cost and
2684 * selectivity, but it's not immediately clear how to account for both,
2685 * and given the uncertainty of the estimates the reliability of the decisions
2686 * would be doubtful anyway. So we just order by estimated per-tuple cost,
2687 * being careful not to change the order when (as is often the case) the
2688 * estimates are identical.
2690 * Although this will work on either bare clauses or RestrictInfos, it's
2691 * much faster to apply it to RestrictInfos, since it can re-use cost
2692 * information that is cached in RestrictInfos.
2694 * Note: some callers pass lists that contain entries that will later be
2695 * removed; this is the easiest way to let this routine see RestrictInfos
2696 * instead of bare clauses. It's OK because we only sort by cost, but
2697 * a cost/selectivity combination would likely do the wrong thing.
2700 order_qual_clauses(PlannerInfo *root, List *clauses)
2707 int nitems = list_length(clauses);
2713 /* No need to work hard for 0 or 1 clause */
2718 * Collect the items and costs into an array. This is to avoid repeated
2719 * cost_qual_eval work if the inputs aren't RestrictInfos.
2721 items = (QualItem *) palloc(nitems * sizeof(QualItem));
2723 foreach(lc, clauses)
2725 Node *clause = (Node *) lfirst(lc);
2728 cost_qual_eval_node(&qcost, clause, root);
2729 items[i].clause = clause;
2730 items[i].cost = qcost.per_tuple;
2735 * Sort. We don't use qsort() because it's not guaranteed stable for
2736 * equal keys. The expected number of entries is small enough that a
2737 * simple insertion sort should be good enough.
2739 for (i = 1; i < nitems; i++)
2741 QualItem newitem = items[i];
2744 /* insert newitem into the already-sorted subarray */
2745 for (j = i; j > 0; j--)
2747 if (newitem.cost >= items[j - 1].cost)
2749 items[j] = items[j - 1];
2754 /* Convert back to a list */
2756 for (i = 0; i < nitems; i++)
2757 result = lappend(result, items[i].clause);
2763 * Copy cost and size info from a Path node to the Plan node created from it.
2764 * The executor usually won't use this info, but it's needed by EXPLAIN.
2767 copy_path_costsize(Plan *dest, Path *src)
2771 dest->startup_cost = src->startup_cost;
2772 dest->total_cost = src->total_cost;
2773 dest->plan_rows = src->parent->rows;
2774 dest->plan_width = src->parent->width;
2778 dest->startup_cost = 0;
2779 dest->total_cost = 0;
2780 dest->plan_rows = 0;
2781 dest->plan_width = 0;
2786 * Copy cost and size info from a lower plan node to an inserted node.
2787 * (Most callers alter the info after copying it.)
2790 copy_plan_costsize(Plan *dest, Plan *src)
2794 dest->startup_cost = src->startup_cost;
2795 dest->total_cost = src->total_cost;
2796 dest->plan_rows = src->plan_rows;
2797 dest->plan_width = src->plan_width;
2801 dest->startup_cost = 0;
2802 dest->total_cost = 0;
2803 dest->plan_rows = 0;
2804 dest->plan_width = 0;
2809 /*****************************************************************************
2811 * PLAN NODE BUILDING ROUTINES
2813 * Some of these are exported because they are called to build plan nodes
2814 * in contexts where we're not deriving the plan node from a path node.
2816 *****************************************************************************/
2819 make_seqscan(List *qptlist,
2823 SeqScan *node = makeNode(SeqScan);
2824 Plan *plan = &node->plan;
2826 /* cost should be inserted by caller */
2827 plan->targetlist = qptlist;
2828 plan->qual = qpqual;
2829 plan->lefttree = NULL;
2830 plan->righttree = NULL;
2831 node->scanrelid = scanrelid;
2837 make_indexscan(List *qptlist,
2842 List *indexqualorig,
2844 List *indexorderbyorig,
2845 ScanDirection indexscandir,
2848 IndexScan *node = makeNode(IndexScan);
2849 Plan *plan = &node->scan.plan;
2851 /* cost should be inserted by caller */
2852 plan->targetlist = qptlist;
2853 plan->qual = qpqual;
2854 plan->lefttree = NULL;
2855 plan->righttree = NULL;
2856 node->scan.scanrelid = scanrelid;
2857 node->indexid = indexid;
2858 node->indexqual = indexqual;
2859 node->indexqualorig = indexqualorig;
2860 node->indexorderby = indexorderby;
2861 node->indexorderbyorig = indexorderbyorig;
2862 node->indexorderdir = indexscandir;
2863 node->indexonly = indexonly;
2868 static BitmapIndexScan *
2869 make_bitmap_indexscan(Index scanrelid,
2872 List *indexqualorig)
2874 BitmapIndexScan *node = makeNode(BitmapIndexScan);
2875 Plan *plan = &node->scan.plan;
2877 /* cost should be inserted by caller */
2878 plan->targetlist = NIL; /* not used */
2879 plan->qual = NIL; /* not used */
2880 plan->lefttree = NULL;
2881 plan->righttree = NULL;
2882 node->scan.scanrelid = scanrelid;
2883 node->indexid = indexid;
2884 node->indexqual = indexqual;
2885 node->indexqualorig = indexqualorig;
2890 static BitmapHeapScan *
2891 make_bitmap_heapscan(List *qptlist,
2894 List *bitmapqualorig,
2897 BitmapHeapScan *node = makeNode(BitmapHeapScan);
2898 Plan *plan = &node->scan.plan;
2900 /* cost should be inserted by caller */
2901 plan->targetlist = qptlist;
2902 plan->qual = qpqual;
2903 plan->lefttree = lefttree;
2904 plan->righttree = NULL;
2905 node->scan.scanrelid = scanrelid;
2906 node->bitmapqualorig = bitmapqualorig;
2912 make_tidscan(List *qptlist,
2917 TidScan *node = makeNode(TidScan);
2918 Plan *plan = &node->scan.plan;
2920 /* cost should be inserted by caller */
2921 plan->targetlist = qptlist;
2922 plan->qual = qpqual;
2923 plan->lefttree = NULL;
2924 plan->righttree = NULL;
2925 node->scan.scanrelid = scanrelid;
2926 node->tidquals = tidquals;
2932 make_subqueryscan(List *qptlist,
2937 SubqueryScan *node = makeNode(SubqueryScan);
2938 Plan *plan = &node->scan.plan;
2941 * Cost is figured here for the convenience of prepunion.c. Note this is
2942 * only correct for the case where qpqual is empty; otherwise caller
2943 * should overwrite cost with a better estimate.
2945 copy_plan_costsize(plan, subplan);
2946 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
2948 plan->targetlist = qptlist;
2949 plan->qual = qpqual;
2950 plan->lefttree = NULL;
2951 plan->righttree = NULL;
2952 node->scan.scanrelid = scanrelid;
2953 node->subplan = subplan;
2958 static FunctionScan *
2959 make_functionscan(List *qptlist,
2965 List *funccoltypmods,
2966 List *funccolcollations)
2968 FunctionScan *node = makeNode(FunctionScan);
2969 Plan *plan = &node->scan.plan;
2971 /* cost should be inserted by caller */
2972 plan->targetlist = qptlist;
2973 plan->qual = qpqual;
2974 plan->lefttree = NULL;
2975 plan->righttree = NULL;
2976 node->scan.scanrelid = scanrelid;
2977 node->funcexpr = funcexpr;
2978 node->funccolnames = funccolnames;
2979 node->funccoltypes = funccoltypes;
2980 node->funccoltypmods = funccoltypmods;
2981 node->funccolcollations = funccolcollations;
2987 make_valuesscan(List *qptlist,
2992 ValuesScan *node = makeNode(ValuesScan);
2993 Plan *plan = &node->scan.plan;
2995 /* cost should be inserted by caller */
2996 plan->targetlist = qptlist;
2997 plan->qual = qpqual;
2998 plan->lefttree = NULL;
2999 plan->righttree = NULL;
3000 node->scan.scanrelid = scanrelid;
3001 node->values_lists = values_lists;
3007 make_ctescan(List *qptlist,
3013 CteScan *node = makeNode(CteScan);
3014 Plan *plan = &node->scan.plan;
3016 /* cost should be inserted by caller */
3017 plan->targetlist = qptlist;
3018 plan->qual = qpqual;
3019 plan->lefttree = NULL;
3020 plan->righttree = NULL;
3021 node->scan.scanrelid = scanrelid;
3022 node->ctePlanId = ctePlanId;
3023 node->cteParam = cteParam;
3028 static WorkTableScan *
3029 make_worktablescan(List *qptlist,
3034 WorkTableScan *node = makeNode(WorkTableScan);
3035 Plan *plan = &node->scan.plan;
3037 /* cost should be inserted by caller */
3038 plan->targetlist = qptlist;
3039 plan->qual = qpqual;
3040 plan->lefttree = NULL;
3041 plan->righttree = NULL;
3042 node->scan.scanrelid = scanrelid;
3043 node->wtParam = wtParam;
3048 static ForeignScan *
3049 make_foreignscan(List *qptlist,
3055 ForeignScan *node = makeNode(ForeignScan);
3056 Plan *plan = &node->scan.plan;
3058 /* cost should be inserted by caller */
3059 plan->targetlist = qptlist;
3060 plan->qual = qpqual;
3061 plan->lefttree = NULL;
3062 plan->righttree = NULL;
3063 node->scan.scanrelid = scanrelid;
3064 node->fsSystemCol = fsSystemCol;
3065 node->fdwplan = fdwplan;
3071 make_append(List *appendplans, List *tlist)
3073 Append *node = makeNode(Append);
3074 Plan *plan = &node->plan;
3079 * Compute cost as sum of subplan costs. We charge nothing extra for the
3080 * Append itself, which perhaps is too optimistic, but since it doesn't do
3081 * any selection or projection, it is a pretty cheap node.
3083 * If you change this, see also create_append_path(). Also, the size
3084 * calculations should match set_append_rel_pathlist(). It'd be better
3085 * not to duplicate all this logic, but some callers of this function
3086 * aren't working from an appendrel or AppendPath, so there's noplace to
3087 * copy the data from.
3089 plan->startup_cost = 0;
3090 plan->total_cost = 0;
3091 plan->plan_rows = 0;
3093 foreach(subnode, appendplans)
3095 Plan *subplan = (Plan *) lfirst(subnode);
3097 if (subnode == list_head(appendplans)) /* first node? */
3098 plan->startup_cost = subplan->startup_cost;
3099 plan->total_cost += subplan->total_cost;
3100 plan->plan_rows += subplan->plan_rows;
3101 total_size += subplan->plan_width * subplan->plan_rows;
3103 if (plan->plan_rows > 0)
3104 plan->plan_width = rint(total_size / plan->plan_rows);
3106 plan->plan_width = 0;
3108 plan->targetlist = tlist;
3110 plan->lefttree = NULL;
3111 plan->righttree = NULL;
3112 node->appendplans = appendplans;
3118 make_recursive_union(List *tlist,
3125 RecursiveUnion *node = makeNode(RecursiveUnion);
3126 Plan *plan = &node->plan;
3127 int numCols = list_length(distinctList);
3129 cost_recursive_union(plan, lefttree, righttree);
3131 plan->targetlist = tlist;
3133 plan->lefttree = lefttree;
3134 plan->righttree = righttree;
3135 node->wtParam = wtParam;
3138 * convert SortGroupClause list into arrays of attr indexes and equality
3139 * operators, as wanted by executor
3141 node->numCols = numCols;
3145 AttrNumber *dupColIdx;
3149 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3150 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3152 foreach(slitem, distinctList)
3154 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3155 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
3158 dupColIdx[keyno] = tle->resno;
3159 dupOperators[keyno] = sortcl->eqop;
3160 Assert(OidIsValid(dupOperators[keyno]));
3163 node->dupColIdx = dupColIdx;
3164 node->dupOperators = dupOperators;
3166 node->numGroups = numGroups;
3172 make_bitmap_and(List *bitmapplans)
3174 BitmapAnd *node = makeNode(BitmapAnd);
3175 Plan *plan = &node->plan;
3177 /* cost should be inserted by caller */
3178 plan->targetlist = NIL;
3180 plan->lefttree = NULL;
3181 plan->righttree = NULL;
3182 node->bitmapplans = bitmapplans;
3188 make_bitmap_or(List *bitmapplans)
3190 BitmapOr *node = makeNode(BitmapOr);
3191 Plan *plan = &node->plan;
3193 /* cost should be inserted by caller */
3194 plan->targetlist = NIL;
3196 plan->lefttree = NULL;
3197 plan->righttree = NULL;
3198 node->bitmapplans = bitmapplans;
3204 make_nestloop(List *tlist,
3212 NestLoop *node = makeNode(NestLoop);
3213 Plan *plan = &node->join.plan;
3215 /* cost should be inserted by caller */
3216 plan->targetlist = tlist;
3217 plan->qual = otherclauses;
3218 plan->lefttree = lefttree;
3219 plan->righttree = righttree;
3220 node->join.jointype = jointype;
3221 node->join.joinqual = joinclauses;
3222 node->nestParams = nestParams;
3228 make_hashjoin(List *tlist,
3236 HashJoin *node = makeNode(HashJoin);
3237 Plan *plan = &node->join.plan;
3239 /* cost should be inserted by caller */
3240 plan->targetlist = tlist;
3241 plan->qual = otherclauses;
3242 plan->lefttree = lefttree;
3243 plan->righttree = righttree;
3244 node->hashclauses = hashclauses;
3245 node->join.jointype = jointype;
3246 node->join.joinqual = joinclauses;
3252 make_hash(Plan *lefttree,
3254 AttrNumber skewColumn,
3257 int32 skewColTypmod)
3259 Hash *node = makeNode(Hash);
3260 Plan *plan = &node->plan;
3262 copy_plan_costsize(plan, lefttree);
3265 * For plausibility, make startup & total costs equal total cost of input
3266 * plan; this only affects EXPLAIN display not decisions.
3268 plan->startup_cost = plan->total_cost;
3269 plan->targetlist = lefttree->targetlist;
3271 plan->lefttree = lefttree;
3272 plan->righttree = NULL;
3274 node->skewTable = skewTable;
3275 node->skewColumn = skewColumn;
3276 node->skewInherit = skewInherit;
3277 node->skewColType = skewColType;
3278 node->skewColTypmod = skewColTypmod;
3284 make_mergejoin(List *tlist,
3289 Oid *mergecollations,
3290 int *mergestrategies,
3291 bool *mergenullsfirst,
3296 MergeJoin *node = makeNode(MergeJoin);
3297 Plan *plan = &node->join.plan;
3299 /* cost should be inserted by caller */
3300 plan->targetlist = tlist;
3301 plan->qual = otherclauses;
3302 plan->lefttree = lefttree;
3303 plan->righttree = righttree;
3304 node->mergeclauses = mergeclauses;
3305 node->mergeFamilies = mergefamilies;
3306 node->mergeCollations = mergecollations;
3307 node->mergeStrategies = mergestrategies;
3308 node->mergeNullsFirst = mergenullsfirst;
3309 node->join.jointype = jointype;
3310 node->join.joinqual = joinclauses;
3316 * make_sort --- basic routine to build a Sort plan node
3318 * Caller must have built the sortColIdx, sortOperators, collations, and
3319 * nullsFirst arrays already.
3320 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
3323 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
3324 AttrNumber *sortColIdx, Oid *sortOperators,
3325 Oid *collations, bool *nullsFirst,
3326 double limit_tuples)
3328 Sort *node = makeNode(Sort);
3329 Plan *plan = &node->plan;
3330 Path sort_path; /* dummy for result of cost_sort */
3332 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3333 cost_sort(&sort_path, root, NIL,
3334 lefttree->total_cost,
3335 lefttree->plan_rows,
3336 lefttree->plan_width,
3340 plan->startup_cost = sort_path.startup_cost;
3341 plan->total_cost = sort_path.total_cost;
3342 plan->targetlist = lefttree->targetlist;
3344 plan->lefttree = lefttree;
3345 plan->righttree = NULL;
3346 node->numCols = numCols;
3347 node->sortColIdx = sortColIdx;
3348 node->sortOperators = sortOperators;
3349 node->collations = collations;
3350 node->nullsFirst = nullsFirst;
3356 * add_sort_column --- utility subroutine for building sort info arrays
3358 * We need this routine because the same column might be selected more than
3359 * once as a sort key column; if so, the extra mentions are redundant.
3361 * Caller is assumed to have allocated the arrays large enough for the
3362 * max possible number of columns. Return value is the new column count.
3365 add_sort_column(AttrNumber colIdx, Oid sortOp, Oid coll, bool nulls_first,
3366 int numCols, AttrNumber *sortColIdx,
3367 Oid *sortOperators, Oid *collations, bool *nullsFirst)
3371 Assert(OidIsValid(sortOp));
3373 for (i = 0; i < numCols; i++)
3376 * Note: we check sortOp because it's conceivable that "ORDER BY foo
3377 * USING <, foo USING <<<" is not redundant, if <<< distinguishes
3378 * values that < considers equal. We need not check nulls_first
3379 * however because a lower-order column with the same sortop but
3380 * opposite nulls direction is redundant.
3382 * We could probably consider sort keys with the same sortop and
3383 * different collations to be redundant too, but for the moment treat
3384 * them as not redundant. This will be needed if we ever support
3385 * collations with different notions of equality.
3387 if (sortColIdx[i] == colIdx &&
3388 sortOperators[numCols] == sortOp &&
3389 collations[numCols] == coll)
3391 /* Already sorting by this col, so extra sort key is useless */
3396 /* Add the column */
3397 sortColIdx[numCols] = colIdx;
3398 sortOperators[numCols] = sortOp;
3399 collations[numCols] = coll;
3400 nullsFirst[numCols] = nulls_first;
3405 * prepare_sort_from_pathkeys
3406 * Prepare to sort according to given pathkeys
3408 * This is used to set up for both Sort and MergeAppend nodes. It calculates
3409 * the executor's representation of the sort key information, and adjusts the
3410 * plan targetlist if needed to add resjunk sort columns.
3413 * 'lefttree' is the node which yields input tuples
3414 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3415 * 'adjust_tlist_in_place' is TRUE if lefttree must be modified in-place
3417 * We must convert the pathkey information into arrays of sort key column
3418 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
3419 * which is the representation the executor wants. These are returned into
3420 * the output parameters *p_numsortkeys etc.
3422 * If the pathkeys include expressions that aren't simple Vars, we will
3423 * usually need to add resjunk items to the input plan's targetlist to
3424 * compute these expressions, since the Sort/MergeAppend node itself won't
3425 * do any such calculations. If the input plan type isn't one that can do
3426 * projections, this means adding a Result node just to do the projection.
3427 * However, the caller can pass adjust_tlist_in_place = TRUE to force the
3428 * lefttree tlist to be modified in-place regardless of whether the node type
3429 * can project --- we use this for fixing the tlist of MergeAppend itself.
3431 * Returns the node which is to be the input to the Sort (either lefttree,
3432 * or a Result stacked atop lefttree).
3435 prepare_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3436 bool adjust_tlist_in_place,
3438 AttrNumber **p_sortColIdx,
3439 Oid **p_sortOperators,
3441 bool **p_nullsFirst)
3443 List *tlist = lefttree->targetlist;
3446 AttrNumber *sortColIdx;
3452 * We will need at most list_length(pathkeys) sort columns; possibly less
3454 numsortkeys = list_length(pathkeys);
3455 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3456 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3457 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3458 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3462 foreach(i, pathkeys)
3464 PathKey *pathkey = (PathKey *) lfirst(i);
3465 EquivalenceClass *ec = pathkey->pk_eclass;
3466 TargetEntry *tle = NULL;
3467 Oid pk_datatype = InvalidOid;
3471 if (ec->ec_has_volatile)
3474 * If the pathkey's EquivalenceClass is volatile, then it must
3475 * have come from an ORDER BY clause, and we have to match it to
3476 * that same targetlist entry.
3478 if (ec->ec_sortref == 0) /* can't happen */
3479 elog(ERROR, "volatile EquivalenceClass has no sortref");
3480 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
3482 Assert(list_length(ec->ec_members) == 1);
3483 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
3488 * Otherwise, we can sort by any non-constant expression listed in
3489 * the pathkey's EquivalenceClass. For now, we take the first one
3490 * that corresponds to an available item in the tlist. If there
3491 * isn't any, use the first one that is an expression in the
3492 * input's vars. (The non-const restriction only matters if the
3493 * EC is below_outer_join; but if it isn't, it won't contain
3494 * consts anyway, else we'd have discarded the pathkey as
3497 * XXX if we have a choice, is there any way of figuring out which
3498 * might be cheapest to execute? (For example, int4lt is likely
3499 * much cheaper to execute than numericlt, but both might appear
3500 * in the same equivalence class...) Not clear that we ever will
3501 * have an interesting choice in practice, so it may not matter.
3503 foreach(j, ec->ec_members)
3505 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3508 * We shouldn't be trying to sort by an equivalence class that
3509 * contains a constant, so no need to consider such cases any
3512 if (em->em_is_const)
3515 tle = tlist_member((Node *) em->em_expr, tlist);
3518 pk_datatype = em->em_datatype;
3519 break; /* found expr already in tlist */
3523 * We can also use it if the pathkey expression is a relabel
3524 * of the tlist entry, or vice versa. This is needed for
3525 * binary-compatible cases (cf. make_pathkey_from_sortinfo).
3526 * We prefer an exact match, though, so we do the basic search
3529 tle = tlist_member_ignore_relabel((Node *) em->em_expr, tlist);
3532 pk_datatype = em->em_datatype;
3533 break; /* found expr already in tlist */
3540 * No matching tlist item; look for a computable expression.
3541 * Note that we treat Aggrefs as if they were variables; this
3542 * is necessary when attempting to sort the output from an Agg
3543 * node for use in a WindowFunc (since grouping_planner will
3544 * have treated the Aggrefs as variables, too).
3546 Expr *sortexpr = NULL;
3548 foreach(j, ec->ec_members)
3550 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3554 if (em->em_is_const)
3556 sortexpr = em->em_expr;
3557 exprvars = pull_var_clause((Node *) sortexpr,
3558 PVC_INCLUDE_AGGREGATES,
3559 PVC_INCLUDE_PLACEHOLDERS);
3560 foreach(k, exprvars)
3562 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
3565 list_free(exprvars);
3568 pk_datatype = em->em_datatype;
3569 break; /* found usable expression */
3573 elog(ERROR, "could not find pathkey item to sort");
3576 * Do we need to insert a Result node?
3578 if (!adjust_tlist_in_place &&
3579 !is_projection_capable_plan(lefttree))
3581 /* copy needed so we don't modify input's tlist below */
3582 tlist = copyObject(tlist);
3583 lefttree = (Plan *) make_result(root, tlist, NULL,
3587 /* Don't bother testing is_projection_capable_plan again */
3588 adjust_tlist_in_place = true;
3591 * Add resjunk entry to input's tlist
3593 tle = makeTargetEntry(sortexpr,
3594 list_length(tlist) + 1,
3597 tlist = lappend(tlist, tle);
3598 lefttree->targetlist = tlist; /* just in case NIL before */
3603 * Look up the correct sort operator from the PathKey's slightly
3604 * abstracted representation.
3606 sortop = get_opfamily_member(pathkey->pk_opfamily,
3609 pathkey->pk_strategy);
3610 if (!OidIsValid(sortop)) /* should not happen */
3611 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
3612 pathkey->pk_strategy, pk_datatype, pk_datatype,
3613 pathkey->pk_opfamily);
3616 * The column might already be selected as a sort key, if the pathkeys
3617 * contain duplicate entries. (This can happen in scenarios where
3618 * multiple mergejoinable clauses mention the same var, for example.)
3619 * So enter it only once in the sort arrays.
3621 numsortkeys = add_sort_column(tle->resno,
3623 pathkey->pk_eclass->ec_collation,
3624 pathkey->pk_nulls_first,
3626 sortColIdx, sortOperators,
3627 collations, nullsFirst);
3630 Assert(numsortkeys > 0);
3632 /* Return results */
3633 *p_numsortkeys = numsortkeys;
3634 *p_sortColIdx = sortColIdx;
3635 *p_sortOperators = sortOperators;
3636 *p_collations = collations;
3637 *p_nullsFirst = nullsFirst;
3643 * make_sort_from_pathkeys
3644 * Create sort plan to sort according to given pathkeys
3646 * 'lefttree' is the node which yields input tuples
3647 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3648 * 'limit_tuples' is the bound on the number of output tuples;
3652 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3653 double limit_tuples)
3656 AttrNumber *sortColIdx;
3661 /* Compute sort column info, and adjust lefttree as needed */
3662 lefttree = prepare_sort_from_pathkeys(root, lefttree, pathkeys,
3670 /* Now build the Sort node */
3671 return make_sort(root, lefttree, numsortkeys,
3672 sortColIdx, sortOperators, collations,
3673 nullsFirst, limit_tuples);
3677 * make_sort_from_sortclauses
3678 * Create sort plan to sort according to given sortclauses
3680 * 'sortcls' is a list of SortGroupClauses
3681 * 'lefttree' is the node which yields input tuples
3684 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
3686 List *sub_tlist = lefttree->targetlist;
3689 AttrNumber *sortColIdx;
3695 * We will need at most list_length(sortcls) sort columns; possibly less
3697 numsortkeys = list_length(sortcls);
3698 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3699 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3700 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3701 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3707 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
3708 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
3711 * Check for the possibility of duplicate order-by clauses --- the
3712 * parser should have removed 'em, but no point in sorting
3715 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
3716 exprCollation((Node *) tle->expr),
3717 sortcl->nulls_first,
3719 sortColIdx, sortOperators,
3720 collations, nullsFirst);
3723 Assert(numsortkeys > 0);
3725 return make_sort(root, lefttree, numsortkeys,
3726 sortColIdx, sortOperators, collations,
3731 * make_sort_from_groupcols
3732 * Create sort plan to sort based on grouping columns
3734 * 'groupcls' is the list of SortGroupClauses
3735 * 'grpColIdx' gives the column numbers to use
3737 * This might look like it could be merged with make_sort_from_sortclauses,
3738 * but presently we *must* use the grpColIdx[] array to locate sort columns,
3739 * because the child plan's tlist is not marked with ressortgroupref info
3740 * appropriate to the grouping node. So, only the sort ordering info
3741 * is used from the SortGroupClause entries.
3744 make_sort_from_groupcols(PlannerInfo *root,
3746 AttrNumber *grpColIdx,
3749 List *sub_tlist = lefttree->targetlist;
3753 AttrNumber *sortColIdx;
3759 * We will need at most list_length(groupcls) sort columns; possibly less
3761 numsortkeys = list_length(groupcls);
3762 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3763 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3764 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3765 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3769 foreach(l, groupcls)
3771 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
3772 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
3775 * Check for the possibility of duplicate group-by clauses --- the
3776 * parser should have removed 'em, but no point in sorting
3779 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
3780 exprCollation((Node *) tle->expr),
3783 sortColIdx, sortOperators,
3784 collations, nullsFirst);
3788 Assert(numsortkeys > 0);
3790 return make_sort(root, lefttree, numsortkeys,
3791 sortColIdx, sortOperators, collations,
3796 make_material(Plan *lefttree)
3798 Material *node = makeNode(Material);
3799 Plan *plan = &node->plan;
3801 /* cost should be inserted by caller */
3802 plan->targetlist = lefttree->targetlist;
3804 plan->lefttree = lefttree;
3805 plan->righttree = NULL;
3811 * materialize_finished_plan: stick a Material node atop a completed plan
3813 * There are a couple of places where we want to attach a Material node
3814 * after completion of subquery_planner(). This currently requires hackery.
3815 * Since subquery_planner has already run SS_finalize_plan on the subplan
3816 * tree, we have to kluge up parameter lists for the Material node.
3817 * Possibly this could be fixed by postponing SS_finalize_plan processing
3818 * until setrefs.c is run?
3821 materialize_finished_plan(Plan *subplan)
3824 Path matpath; /* dummy for result of cost_material */
3826 matplan = (Plan *) make_material(subplan);
3829 cost_material(&matpath,
3830 subplan->startup_cost,
3831 subplan->total_cost,
3833 subplan->plan_width);
3834 matplan->startup_cost = matpath.startup_cost;
3835 matplan->total_cost = matpath.total_cost;
3836 matplan->plan_rows = subplan->plan_rows;
3837 matplan->plan_width = subplan->plan_width;
3839 /* parameter kluge --- see comments above */
3840 matplan->extParam = bms_copy(subplan->extParam);
3841 matplan->allParam = bms_copy(subplan->allParam);
3847 make_agg(PlannerInfo *root, List *tlist, List *qual,
3848 AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
3849 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
3853 Agg *node = makeNode(Agg);
3854 Plan *plan = &node->plan;
3855 Path agg_path; /* dummy for result of cost_agg */
3858 node->aggstrategy = aggstrategy;
3859 node->numCols = numGroupCols;
3860 node->grpColIdx = grpColIdx;
3861 node->grpOperators = grpOperators;
3862 node->numGroups = numGroups;
3864 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3865 cost_agg(&agg_path, root,
3866 aggstrategy, aggcosts,
3867 numGroupCols, numGroups,
3868 lefttree->startup_cost,
3869 lefttree->total_cost,
3870 lefttree->plan_rows);
3871 plan->startup_cost = agg_path.startup_cost;
3872 plan->total_cost = agg_path.total_cost;
3875 * We will produce a single output tuple if not grouping, and a tuple per
3878 if (aggstrategy == AGG_PLAIN)
3879 plan->plan_rows = 1;
3881 plan->plan_rows = numGroups;
3884 * We also need to account for the cost of evaluation of the qual (ie, the
3885 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
3886 * anything for Aggref nodes; this is okay since they are really
3887 * comparable to Vars.
3889 * See notes in grouping_planner about why only make_agg, make_windowagg
3890 * and make_group worry about tlist eval cost.
3894 cost_qual_eval(&qual_cost, qual, root);
3895 plan->startup_cost += qual_cost.startup;
3896 plan->total_cost += qual_cost.startup;
3897 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3899 cost_qual_eval(&qual_cost, tlist, root);
3900 plan->startup_cost += qual_cost.startup;
3901 plan->total_cost += qual_cost.startup;
3902 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3905 plan->targetlist = tlist;
3906 plan->lefttree = lefttree;
3907 plan->righttree = NULL;
3913 make_windowagg(PlannerInfo *root, List *tlist,
3914 List *windowFuncs, Index winref,
3915 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
3916 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
3917 int frameOptions, Node *startOffset, Node *endOffset,
3920 WindowAgg *node = makeNode(WindowAgg);
3921 Plan *plan = &node->plan;
3922 Path windowagg_path; /* dummy for result of cost_windowagg */
3925 node->winref = winref;
3926 node->partNumCols = partNumCols;
3927 node->partColIdx = partColIdx;
3928 node->partOperators = partOperators;
3929 node->ordNumCols = ordNumCols;
3930 node->ordColIdx = ordColIdx;
3931 node->ordOperators = ordOperators;
3932 node->frameOptions = frameOptions;
3933 node->startOffset = startOffset;
3934 node->endOffset = endOffset;
3936 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3937 cost_windowagg(&windowagg_path, root,
3938 windowFuncs, partNumCols, ordNumCols,
3939 lefttree->startup_cost,
3940 lefttree->total_cost,
3941 lefttree->plan_rows);
3942 plan->startup_cost = windowagg_path.startup_cost;
3943 plan->total_cost = windowagg_path.total_cost;
3946 * We also need to account for the cost of evaluation of the tlist.
3948 * See notes in grouping_planner about why only make_agg, make_windowagg
3949 * and make_group worry about tlist eval cost.
3951 cost_qual_eval(&qual_cost, tlist, root);
3952 plan->startup_cost += qual_cost.startup;
3953 plan->total_cost += qual_cost.startup;
3954 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3956 plan->targetlist = tlist;
3957 plan->lefttree = lefttree;
3958 plan->righttree = NULL;
3959 /* WindowAgg nodes never have a qual clause */
3966 make_group(PlannerInfo *root,
3970 AttrNumber *grpColIdx,
3975 Group *node = makeNode(Group);
3976 Plan *plan = &node->plan;
3977 Path group_path; /* dummy for result of cost_group */
3980 node->numCols = numGroupCols;
3981 node->grpColIdx = grpColIdx;
3982 node->grpOperators = grpOperators;
3984 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3985 cost_group(&group_path, root,
3986 numGroupCols, numGroups,
3987 lefttree->startup_cost,
3988 lefttree->total_cost,
3989 lefttree->plan_rows);
3990 plan->startup_cost = group_path.startup_cost;
3991 plan->total_cost = group_path.total_cost;
3993 /* One output tuple per estimated result group */
3994 plan->plan_rows = numGroups;
3997 * We also need to account for the cost of evaluation of the qual (ie, the
3998 * HAVING clause) and the tlist.
4000 * XXX this double-counts the cost of evaluation of any expressions used
4001 * for grouping, since in reality those will have been evaluated at a
4002 * lower plan level and will only be copied by the Group node. Worth
4005 * See notes in grouping_planner about why only make_agg, make_windowagg
4006 * and make_group worry about tlist eval cost.
4010 cost_qual_eval(&qual_cost, qual, root);
4011 plan->startup_cost += qual_cost.startup;
4012 plan->total_cost += qual_cost.startup;
4013 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4015 cost_qual_eval(&qual_cost, tlist, root);
4016 plan->startup_cost += qual_cost.startup;
4017 plan->total_cost += qual_cost.startup;
4018 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4021 plan->targetlist = tlist;
4022 plan->lefttree = lefttree;
4023 plan->righttree = NULL;
4029 * distinctList is a list of SortGroupClauses, identifying the targetlist items
4030 * that should be considered by the Unique filter. The input path must
4031 * already be sorted accordingly.
4034 make_unique(Plan *lefttree, List *distinctList)
4036 Unique *node = makeNode(Unique);
4037 Plan *plan = &node->plan;
4038 int numCols = list_length(distinctList);
4040 AttrNumber *uniqColIdx;
4044 copy_plan_costsize(plan, lefttree);
4047 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4048 * all columns get compared at most of the tuples. (XXX probably this is
4051 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
4054 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
4055 * we assume the filter removes nothing. The caller must alter this if he
4056 * has a better idea.
4059 plan->targetlist = lefttree->targetlist;
4061 plan->lefttree = lefttree;
4062 plan->righttree = NULL;
4065 * convert SortGroupClause list into arrays of attr indexes and equality
4066 * operators, as wanted by executor
4068 Assert(numCols > 0);
4069 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4070 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4072 foreach(slitem, distinctList)
4074 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4075 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4077 uniqColIdx[keyno] = tle->resno;
4078 uniqOperators[keyno] = sortcl->eqop;
4079 Assert(OidIsValid(uniqOperators[keyno]));
4083 node->numCols = numCols;
4084 node->uniqColIdx = uniqColIdx;
4085 node->uniqOperators = uniqOperators;
4091 * distinctList is a list of SortGroupClauses, identifying the targetlist
4092 * items that should be considered by the SetOp filter. The input path must
4093 * already be sorted accordingly.
4096 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
4097 List *distinctList, AttrNumber flagColIdx, int firstFlag,
4098 long numGroups, double outputRows)
4100 SetOp *node = makeNode(SetOp);
4101 Plan *plan = &node->plan;
4102 int numCols = list_length(distinctList);
4104 AttrNumber *dupColIdx;
4108 copy_plan_costsize(plan, lefttree);
4109 plan->plan_rows = outputRows;
4112 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4113 * all columns get compared at most of the tuples.
4115 plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
4117 plan->targetlist = lefttree->targetlist;
4119 plan->lefttree = lefttree;
4120 plan->righttree = NULL;
4123 * convert SortGroupClause list into arrays of attr indexes and equality
4124 * operators, as wanted by executor
4126 Assert(numCols > 0);
4127 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4128 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4130 foreach(slitem, distinctList)
4132 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4133 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4135 dupColIdx[keyno] = tle->resno;
4136 dupOperators[keyno] = sortcl->eqop;
4137 Assert(OidIsValid(dupOperators[keyno]));
4142 node->strategy = strategy;
4143 node->numCols = numCols;
4144 node->dupColIdx = dupColIdx;
4145 node->dupOperators = dupOperators;
4146 node->flagColIdx = flagColIdx;
4147 node->firstFlag = firstFlag;
4148 node->numGroups = numGroups;
4155 * Build a LockRows plan node
4158 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
4160 LockRows *node = makeNode(LockRows);
4161 Plan *plan = &node->plan;
4163 copy_plan_costsize(plan, lefttree);
4165 /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
4166 plan->total_cost += cpu_tuple_cost * plan->plan_rows;
4168 plan->targetlist = lefttree->targetlist;
4170 plan->lefttree = lefttree;
4171 plan->righttree = NULL;
4173 node->rowMarks = rowMarks;
4174 node->epqParam = epqParam;
4180 * Note: offset_est and count_est are passed in to save having to repeat
4181 * work already done to estimate the values of the limitOffset and limitCount
4182 * expressions. Their values are as returned by preprocess_limit (0 means
4183 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
4184 * with that function!
4187 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
4188 int64 offset_est, int64 count_est)
4190 Limit *node = makeNode(Limit);
4191 Plan *plan = &node->plan;
4193 copy_plan_costsize(plan, lefttree);
4196 * Adjust the output rows count and costs according to the offset/limit.
4197 * This is only a cosmetic issue if we are at top level, but if we are
4198 * building a subquery then it's important to report correct info to the
4201 * When the offset or count couldn't be estimated, use 10% of the
4202 * estimated number of rows emitted from the subplan.
4204 if (offset_est != 0)
4209 offset_rows = (double) offset_est;
4211 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4212 if (offset_rows > plan->plan_rows)
4213 offset_rows = plan->plan_rows;
4214 if (plan->plan_rows > 0)
4215 plan->startup_cost +=
4216 (plan->total_cost - plan->startup_cost)
4217 * offset_rows / plan->plan_rows;
4218 plan->plan_rows -= offset_rows;
4219 if (plan->plan_rows < 1)
4220 plan->plan_rows = 1;
4228 count_rows = (double) count_est;
4230 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4231 if (count_rows > plan->plan_rows)
4232 count_rows = plan->plan_rows;
4233 if (plan->plan_rows > 0)
4234 plan->total_cost = plan->startup_cost +
4235 (plan->total_cost - plan->startup_cost)
4236 * count_rows / plan->plan_rows;
4237 plan->plan_rows = count_rows;
4238 if (plan->plan_rows < 1)
4239 plan->plan_rows = 1;
4242 plan->targetlist = lefttree->targetlist;
4244 plan->lefttree = lefttree;
4245 plan->righttree = NULL;
4247 node->limitOffset = limitOffset;
4248 node->limitCount = limitCount;
4255 * Build a Result plan node
4257 * If we have a subplan, assume that any evaluation costs for the gating qual
4258 * were already factored into the subplan's startup cost, and just copy the
4259 * subplan cost. If there's no subplan, we should include the qual eval
4260 * cost. In either case, tlist eval cost is not to be included here.
4263 make_result(PlannerInfo *root,
4265 Node *resconstantqual,
4268 Result *node = makeNode(Result);
4269 Plan *plan = &node->plan;
4272 copy_plan_costsize(plan, subplan);
4275 plan->startup_cost = 0;
4276 plan->total_cost = cpu_tuple_cost;
4277 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
4278 plan->plan_width = 0; /* XXX is it worth being smarter? */
4279 if (resconstantqual)
4283 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
4284 /* resconstantqual is evaluated once at startup */
4285 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
4286 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
4290 plan->targetlist = tlist;
4292 plan->lefttree = subplan;
4293 plan->righttree = NULL;
4294 node->resconstantqual = resconstantqual;
4301 * Build a ModifyTable plan node
4303 * Currently, we don't charge anything extra for the actual table modification
4304 * work, nor for the RETURNING expressions if any. It would only be window
4305 * dressing, since these are always top-level nodes and there is no way for
4306 * the costs to change any higher-level planning choices. But we might want
4307 * to make it look better sometime.
4310 make_modifytable(CmdType operation, bool canSetTag,
4311 List *resultRelations,
4312 List *subplans, List *returningLists,
4313 List *rowMarks, int epqParam)
4315 ModifyTable *node = makeNode(ModifyTable);
4316 Plan *plan = &node->plan;
4320 Assert(list_length(resultRelations) == list_length(subplans));
4321 Assert(returningLists == NIL ||
4322 list_length(resultRelations) == list_length(returningLists));
4325 * Compute cost as sum of subplan costs.
4327 plan->startup_cost = 0;
4328 plan->total_cost = 0;
4329 plan->plan_rows = 0;
4331 foreach(subnode, subplans)
4333 Plan *subplan = (Plan *) lfirst(subnode);
4335 if (subnode == list_head(subplans)) /* first node? */
4336 plan->startup_cost = subplan->startup_cost;
4337 plan->total_cost += subplan->total_cost;
4338 plan->plan_rows += subplan->plan_rows;
4339 total_size += subplan->plan_width * subplan->plan_rows;
4341 if (plan->plan_rows > 0)
4342 plan->plan_width = rint(total_size / plan->plan_rows);
4344 plan->plan_width = 0;
4346 node->plan.lefttree = NULL;
4347 node->plan.righttree = NULL;
4348 node->plan.qual = NIL;
4351 * Set up the visible plan targetlist as being the same as the first
4352 * RETURNING list. This is for the use of EXPLAIN; the executor won't pay
4353 * any attention to the targetlist.
4356 node->plan.targetlist = copyObject(linitial(returningLists));
4358 node->plan.targetlist = NIL;
4360 node->operation = operation;
4361 node->canSetTag = canSetTag;
4362 node->resultRelations = resultRelations;
4363 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
4364 node->plans = subplans;
4365 node->returningLists = returningLists;
4366 node->rowMarks = rowMarks;
4367 node->epqParam = epqParam;
4373 * is_projection_capable_plan
4374 * Check whether a given Plan node is able to do projection.
4377 is_projection_capable_plan(Plan *plan)
4379 /* Most plan types can project, so just list the ones that can't */
4380 switch (nodeTag(plan))
4392 case T_RecursiveUnion: