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);
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);
1188 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1189 /* use the indexscan-specific rows estimate, not the parent rel's */
1190 scan_plan->scan.plan.plan_rows = best_path->rows;
1196 * create_bitmap_scan_plan
1197 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
1198 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1200 static BitmapHeapScan *
1201 create_bitmap_scan_plan(PlannerInfo *root,
1202 BitmapHeapPath *best_path,
1206 Index baserelid = best_path->path.parent->relid;
1207 Plan *bitmapqualplan;
1208 List *bitmapqualorig;
1212 BitmapHeapScan *scan_plan;
1214 /* it should be a base rel... */
1215 Assert(baserelid > 0);
1216 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1218 /* Process the bitmapqual tree into a Plan tree and qual lists */
1219 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1220 &bitmapqualorig, &indexquals);
1222 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1223 scan_clauses = extract_actual_clauses(scan_clauses, false);
1226 * If this is a innerjoin scan, the indexclauses will contain join clauses
1227 * that are not present in scan_clauses (since the passed-in value is just
1228 * the rel's baserestrictinfo list). We must add these clauses to
1229 * scan_clauses to ensure they get checked. In most cases we will remove
1230 * the join clauses again below, but if a join clause contains a special
1231 * operator, we need to make sure it gets into the scan_clauses.
1233 if (best_path->isjoininner)
1235 scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
1239 * The qpqual list must contain all restrictions not automatically handled
1240 * by the index. All the predicates in the indexquals will be checked
1241 * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
1242 * are any "special" operators involved then they must be added to qpqual.
1243 * The upshot is that qpqual must contain scan_clauses minus whatever
1244 * appears in indexquals.
1246 * In normal cases simple equal() checks will be enough to spot duplicate
1247 * clauses, so we try that first. In some situations (particularly with
1248 * OR'd index conditions) we may have scan_clauses that are not equal to,
1249 * but are logically implied by, the index quals; so we also try a
1250 * predicate_implied_by() check to see if we can discard quals that way.
1251 * (predicate_implied_by assumes its first input contains only immutable
1252 * functions, so we have to check that.)
1254 * Unlike create_indexscan_plan(), we need take no special thought here
1255 * for partial index predicates; this is because the predicate conditions
1256 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1257 * to do it that way because predicate conditions need to be rechecked if
1258 * the scan becomes lossy.
1261 foreach(l, scan_clauses)
1263 Node *clause = (Node *) lfirst(l);
1265 if (list_member(indexquals, clause))
1267 if (!contain_mutable_functions(clause))
1269 List *clausel = list_make1(clause);
1271 if (predicate_implied_by(clausel, indexquals))
1274 qpqual = lappend(qpqual, clause);
1277 /* Sort clauses into best execution order */
1278 qpqual = order_qual_clauses(root, qpqual);
1281 * When dealing with special operators, we will at this point have
1282 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
1283 * 'em from bitmapqualorig, since there's no point in making the tests
1286 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1288 /* Finally ready to build the plan node */
1289 scan_plan = make_bitmap_heapscan(tlist,
1295 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1296 /* use the indexscan-specific rows estimate, not the parent rel's */
1297 scan_plan->scan.plan.plan_rows = best_path->rows;
1303 * Given a bitmapqual tree, generate the Plan tree that implements it
1305 * As byproducts, we also return in *qual and *indexqual the qual lists
1306 * (in implicit-AND form, without RestrictInfos) describing the original index
1307 * conditions and the generated indexqual conditions. (These are the same in
1308 * simple cases, but when special index operators are involved, the former
1309 * list includes the special conditions while the latter includes the actual
1310 * indexable conditions derived from them.) Both lists include partial-index
1311 * predicates, because we have to recheck predicates as well as index
1312 * conditions if the bitmap scan becomes lossy.
1314 * Note: if you find yourself changing this, you probably need to change
1315 * make_restrictinfo_from_bitmapqual too.
1318 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1319 List **qual, List **indexqual)
1323 if (IsA(bitmapqual, BitmapAndPath))
1325 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1326 List *subplans = NIL;
1327 List *subquals = NIL;
1328 List *subindexquals = NIL;
1332 * There may well be redundant quals among the subplans, since a
1333 * top-level WHERE qual might have gotten used to form several
1334 * different index quals. We don't try exceedingly hard to eliminate
1335 * redundancies, but we do eliminate obvious duplicates by using
1336 * list_concat_unique.
1338 foreach(l, apath->bitmapquals)
1344 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1345 &subqual, &subindexqual);
1346 subplans = lappend(subplans, subplan);
1347 subquals = list_concat_unique(subquals, subqual);
1348 subindexquals = list_concat_unique(subindexquals, subindexqual);
1350 plan = (Plan *) make_bitmap_and(subplans);
1351 plan->startup_cost = apath->path.startup_cost;
1352 plan->total_cost = apath->path.total_cost;
1354 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1355 plan->plan_width = 0; /* meaningless */
1357 *indexqual = subindexquals;
1359 else if (IsA(bitmapqual, BitmapOrPath))
1361 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1362 List *subplans = NIL;
1363 List *subquals = NIL;
1364 List *subindexquals = NIL;
1365 bool const_true_subqual = false;
1366 bool const_true_subindexqual = false;
1370 * Here, we only detect qual-free subplans. A qual-free subplan would
1371 * cause us to generate "... OR true ..." which we may as well reduce
1372 * to just "true". We do not try to eliminate redundant subclauses
1373 * because (a) it's not as likely as in the AND case, and (b) we might
1374 * well be working with hundreds or even thousands of OR conditions,
1375 * perhaps from a long IN list. The performance of list_append_unique
1376 * would be unacceptable.
1378 foreach(l, opath->bitmapquals)
1384 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1385 &subqual, &subindexqual);
1386 subplans = lappend(subplans, subplan);
1388 const_true_subqual = true;
1389 else if (!const_true_subqual)
1390 subquals = lappend(subquals,
1391 make_ands_explicit(subqual));
1392 if (subindexqual == NIL)
1393 const_true_subindexqual = true;
1394 else if (!const_true_subindexqual)
1395 subindexquals = lappend(subindexquals,
1396 make_ands_explicit(subindexqual));
1400 * In the presence of ScalarArrayOpExpr quals, we might have built
1401 * BitmapOrPaths with just one subpath; don't add an OR step.
1403 if (list_length(subplans) == 1)
1405 plan = (Plan *) linitial(subplans);
1409 plan = (Plan *) make_bitmap_or(subplans);
1410 plan->startup_cost = opath->path.startup_cost;
1411 plan->total_cost = opath->path.total_cost;
1413 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1414 plan->plan_width = 0; /* meaningless */
1418 * If there were constant-TRUE subquals, the OR reduces to constant
1419 * TRUE. Also, avoid generating one-element ORs, which could happen
1420 * due to redundancy elimination or ScalarArrayOpExpr quals.
1422 if (const_true_subqual)
1424 else if (list_length(subquals) <= 1)
1427 *qual = list_make1(make_orclause(subquals));
1428 if (const_true_subindexqual)
1430 else if (list_length(subindexquals) <= 1)
1431 *indexqual = subindexquals;
1433 *indexqual = list_make1(make_orclause(subindexquals));
1435 else if (IsA(bitmapqual, IndexPath))
1437 IndexPath *ipath = (IndexPath *) bitmapqual;
1441 /* Use the regular indexscan plan build machinery... */
1442 iscan = create_indexscan_plan(root, ipath, NIL, NIL);
1443 /* then convert to a bitmap indexscan */
1444 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1447 iscan->indexqualorig);
1448 plan->startup_cost = 0.0;
1449 plan->total_cost = ipath->indextotalcost;
1451 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1452 plan->plan_width = 0; /* meaningless */
1453 *qual = get_actual_clauses(ipath->indexclauses);
1454 *indexqual = get_actual_clauses(ipath->indexquals);
1455 foreach(l, ipath->indexinfo->indpred)
1457 Expr *pred = (Expr *) lfirst(l);
1460 * We know that the index predicate must have been implied by the
1461 * query condition as a whole, but it may or may not be implied by
1462 * the conditions that got pushed into the bitmapqual. Avoid
1463 * generating redundant conditions.
1465 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1467 *qual = lappend(*qual, pred);
1468 *indexqual = lappend(*indexqual, pred);
1473 * Replace outer-relation variables with nestloop params, but only
1474 * after doing the above comparisons to index predicates.
1476 if (ipath->isjoininner)
1479 replace_nestloop_params(root, (Node *) *qual);
1480 *indexqual = (List *)
1481 replace_nestloop_params(root, (Node *) *indexqual);
1486 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1487 plan = NULL; /* keep compiler quiet */
1494 * create_tidscan_plan
1495 * Returns a tidscan plan for the base relation scanned by 'best_path'
1496 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1499 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1500 List *tlist, List *scan_clauses)
1503 Index scan_relid = best_path->path.parent->relid;
1506 /* it should be a base rel... */
1507 Assert(scan_relid > 0);
1508 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1510 /* Sort clauses into best execution order */
1511 scan_clauses = order_qual_clauses(root, scan_clauses);
1513 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1514 scan_clauses = extract_actual_clauses(scan_clauses, false);
1517 * Remove any clauses that are TID quals. This is a bit tricky since the
1518 * tidquals list has implicit OR semantics.
1520 ortidquals = best_path->tidquals;
1521 if (list_length(ortidquals) > 1)
1522 ortidquals = list_make1(make_orclause(ortidquals));
1523 scan_clauses = list_difference(scan_clauses, ortidquals);
1525 scan_plan = make_tidscan(tlist,
1528 best_path->tidquals);
1530 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1536 * create_subqueryscan_plan
1537 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1538 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1540 static SubqueryScan *
1541 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1542 List *tlist, List *scan_clauses)
1544 SubqueryScan *scan_plan;
1545 Index scan_relid = best_path->parent->relid;
1547 /* it should be a subquery base rel... */
1548 Assert(scan_relid > 0);
1549 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1551 /* Sort clauses into best execution order */
1552 scan_clauses = order_qual_clauses(root, scan_clauses);
1554 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1555 scan_clauses = extract_actual_clauses(scan_clauses, false);
1557 scan_plan = make_subqueryscan(tlist,
1560 best_path->parent->subplan);
1562 copy_path_costsize(&scan_plan->scan.plan, best_path);
1568 * create_functionscan_plan
1569 * Returns a functionscan plan for the base relation scanned by 'best_path'
1570 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1572 static FunctionScan *
1573 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1574 List *tlist, List *scan_clauses)
1576 FunctionScan *scan_plan;
1577 Index scan_relid = best_path->parent->relid;
1580 /* it should be a function base rel... */
1581 Assert(scan_relid > 0);
1582 rte = planner_rt_fetch(scan_relid, root);
1583 Assert(rte->rtekind == RTE_FUNCTION);
1585 /* Sort clauses into best execution order */
1586 scan_clauses = order_qual_clauses(root, scan_clauses);
1588 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1589 scan_clauses = extract_actual_clauses(scan_clauses, false);
1591 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1593 rte->eref->colnames,
1595 rte->funccoltypmods,
1596 rte->funccolcollations);
1598 copy_path_costsize(&scan_plan->scan.plan, best_path);
1604 * create_valuesscan_plan
1605 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1606 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1609 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1610 List *tlist, List *scan_clauses)
1612 ValuesScan *scan_plan;
1613 Index scan_relid = best_path->parent->relid;
1616 /* it should be a values base rel... */
1617 Assert(scan_relid > 0);
1618 rte = planner_rt_fetch(scan_relid, root);
1619 Assert(rte->rtekind == RTE_VALUES);
1621 /* Sort clauses into best execution order */
1622 scan_clauses = order_qual_clauses(root, scan_clauses);
1624 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1625 scan_clauses = extract_actual_clauses(scan_clauses, false);
1627 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1630 copy_path_costsize(&scan_plan->scan.plan, best_path);
1636 * create_ctescan_plan
1637 * Returns a ctescan plan for the base relation scanned by 'best_path'
1638 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1641 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1642 List *tlist, List *scan_clauses)
1645 Index scan_relid = best_path->parent->relid;
1647 SubPlan *ctesplan = NULL;
1650 PlannerInfo *cteroot;
1655 Assert(scan_relid > 0);
1656 rte = planner_rt_fetch(scan_relid, root);
1657 Assert(rte->rtekind == RTE_CTE);
1658 Assert(!rte->self_reference);
1661 * Find the referenced CTE, and locate the SubPlan previously made for it.
1663 levelsup = rte->ctelevelsup;
1665 while (levelsup-- > 0)
1667 cteroot = cteroot->parent_root;
1668 if (!cteroot) /* shouldn't happen */
1669 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1673 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1674 * on planning the CTEs (ie, this is a side-reference from another CTE).
1675 * So we mustn't use forboth here.
1678 foreach(lc, cteroot->parse->cteList)
1680 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1682 if (strcmp(cte->ctename, rte->ctename) == 0)
1686 if (lc == NULL) /* shouldn't happen */
1687 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1688 if (ndx >= list_length(cteroot->cte_plan_ids))
1689 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1690 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1691 Assert(plan_id > 0);
1692 foreach(lc, cteroot->init_plans)
1694 ctesplan = (SubPlan *) lfirst(lc);
1695 if (ctesplan->plan_id == plan_id)
1698 if (lc == NULL) /* shouldn't happen */
1699 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1702 * We need the CTE param ID, which is the sole member of the SubPlan's
1705 cte_param_id = linitial_int(ctesplan->setParam);
1707 /* Sort clauses into best execution order */
1708 scan_clauses = order_qual_clauses(root, scan_clauses);
1710 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1711 scan_clauses = extract_actual_clauses(scan_clauses, false);
1713 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1714 plan_id, cte_param_id);
1716 copy_path_costsize(&scan_plan->scan.plan, best_path);
1722 * create_worktablescan_plan
1723 * Returns a worktablescan plan for the base relation scanned by 'best_path'
1724 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1726 static WorkTableScan *
1727 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1728 List *tlist, List *scan_clauses)
1730 WorkTableScan *scan_plan;
1731 Index scan_relid = best_path->parent->relid;
1734 PlannerInfo *cteroot;
1736 Assert(scan_relid > 0);
1737 rte = planner_rt_fetch(scan_relid, root);
1738 Assert(rte->rtekind == RTE_CTE);
1739 Assert(rte->self_reference);
1742 * We need to find the worktable param ID, which is in the plan level
1743 * that's processing the recursive UNION, which is one level *below* where
1744 * the CTE comes from.
1746 levelsup = rte->ctelevelsup;
1747 if (levelsup == 0) /* shouldn't happen */
1748 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1751 while (levelsup-- > 0)
1753 cteroot = cteroot->parent_root;
1754 if (!cteroot) /* shouldn't happen */
1755 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1757 if (cteroot->wt_param_id < 0) /* shouldn't happen */
1758 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1760 /* Sort clauses into best execution order */
1761 scan_clauses = order_qual_clauses(root, scan_clauses);
1763 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1764 scan_clauses = extract_actual_clauses(scan_clauses, false);
1766 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
1767 cteroot->wt_param_id);
1769 copy_path_costsize(&scan_plan->scan.plan, best_path);
1775 * create_foreignscan_plan
1776 * Returns a foreignscan plan for the base relation scanned by 'best_path'
1777 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1779 static ForeignScan *
1780 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
1781 List *tlist, List *scan_clauses)
1783 ForeignScan *scan_plan;
1784 RelOptInfo *rel = best_path->path.parent;
1785 Index scan_relid = rel->relid;
1790 /* it should be a base rel... */
1791 Assert(scan_relid > 0);
1792 Assert(rel->rtekind == RTE_RELATION);
1793 rte = planner_rt_fetch(scan_relid, root);
1794 Assert(rte->rtekind == RTE_RELATION);
1796 /* Sort clauses into best execution order */
1797 scan_clauses = order_qual_clauses(root, scan_clauses);
1799 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1800 scan_clauses = extract_actual_clauses(scan_clauses, false);
1802 /* Detect whether any system columns are requested from rel */
1803 fsSystemCol = false;
1804 for (i = rel->min_attr; i < 0; i++)
1806 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
1813 scan_plan = make_foreignscan(tlist,
1817 best_path->fdwplan);
1819 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1825 /*****************************************************************************
1829 *****************************************************************************/
1832 create_nestloop_plan(PlannerInfo *root,
1833 NestPath *best_path,
1837 NestLoop *join_plan;
1838 List *tlist = build_relation_tlist(best_path->path.parent);
1839 List *joinrestrictclauses = best_path->joinrestrictinfo;
1849 * If the inner path is a nestloop inner indexscan, it might be using some
1850 * of the join quals as index quals, in which case we don't have to check
1851 * them again at the join node. Remove any join quals that are redundant.
1853 joinrestrictclauses =
1854 select_nonredundant_join_clauses(root,
1855 joinrestrictclauses,
1856 best_path->innerjoinpath);
1858 /* Sort join qual clauses into best execution order */
1859 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
1861 /* Get the join qual clauses (in plain expression form) */
1862 /* Any pseudoconstant clauses are ignored here */
1863 if (IS_OUTER_JOIN(best_path->jointype))
1865 extract_actual_join_clauses(joinrestrictclauses,
1866 &joinclauses, &otherclauses);
1870 /* We can treat all clauses alike for an inner join */
1871 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
1876 * Identify any nestloop parameters that should be supplied by this join
1877 * node, and move them from root->curOuterParams to the nestParams list.
1879 outerrelids = best_path->outerjoinpath->parent->relids;
1882 for (cell = list_head(root->curOuterParams); cell; cell = next)
1884 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
1887 if (bms_is_member(nlp->paramval->varno, outerrelids))
1889 root->curOuterParams = list_delete_cell(root->curOuterParams,
1891 nestParams = lappend(nestParams, nlp);
1897 join_plan = make_nestloop(tlist,
1903 best_path->jointype);
1905 copy_path_costsize(&join_plan->join.plan, &best_path->path);
1911 create_mergejoin_plan(PlannerInfo *root,
1912 MergePath *best_path,
1916 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1920 List *outerpathkeys;
1921 List *innerpathkeys;
1924 Oid *mergecollations;
1925 int *mergestrategies;
1926 bool *mergenullsfirst;
1927 MergeJoin *join_plan;
1933 /* Sort join qual clauses into best execution order */
1934 /* NB: do NOT reorder the mergeclauses */
1935 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1937 /* Get the join qual clauses (in plain expression form) */
1938 /* Any pseudoconstant clauses are ignored here */
1939 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1941 extract_actual_join_clauses(joinclauses,
1942 &joinclauses, &otherclauses);
1946 /* We can treat all clauses alike for an inner join */
1947 joinclauses = extract_actual_clauses(joinclauses, false);
1952 * Remove the mergeclauses from the list of join qual clauses, leaving the
1953 * list of quals that must be checked as qpquals.
1955 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
1956 joinclauses = list_difference(joinclauses, mergeclauses);
1959 * Rearrange mergeclauses, if needed, so that the outer variable is always
1960 * on the left; mark the mergeclause restrictinfos with correct
1961 * outer_is_left status.
1963 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
1964 best_path->jpath.outerjoinpath->parent->relids);
1967 * Create explicit sort nodes for the outer and inner paths if necessary.
1968 * Make sure there are no excess columns in the inputs if sorting.
1970 if (best_path->outersortkeys)
1972 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1973 outer_plan = (Plan *)
1974 make_sort_from_pathkeys(root,
1976 best_path->outersortkeys,
1978 outerpathkeys = best_path->outersortkeys;
1981 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
1983 if (best_path->innersortkeys)
1985 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1986 inner_plan = (Plan *)
1987 make_sort_from_pathkeys(root,
1989 best_path->innersortkeys,
1991 innerpathkeys = best_path->innersortkeys;
1994 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
1997 * If specified, add a materialize node to shield the inner plan from the
1998 * need to handle mark/restore.
2000 if (best_path->materialize_inner)
2002 Plan *matplan = (Plan *) make_material(inner_plan);
2005 * We assume the materialize will not spill to disk, and therefore
2006 * charge just cpu_operator_cost per tuple. (Keep this estimate in
2007 * sync with cost_mergejoin.)
2009 copy_plan_costsize(matplan, inner_plan);
2010 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
2012 inner_plan = matplan;
2016 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
2017 * executor. The information is in the pathkeys for the two inputs, but
2018 * we need to be careful about the possibility of mergeclauses sharing a
2019 * pathkey (compare find_mergeclauses_for_pathkeys()).
2021 nClauses = list_length(mergeclauses);
2022 Assert(nClauses == list_length(best_path->path_mergeclauses));
2023 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
2024 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
2025 mergestrategies = (int *) palloc(nClauses * sizeof(int));
2026 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
2028 lop = list_head(outerpathkeys);
2029 lip = list_head(innerpathkeys);
2031 foreach(lc, best_path->path_mergeclauses)
2033 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2034 EquivalenceClass *oeclass;
2035 EquivalenceClass *ieclass;
2038 EquivalenceClass *opeclass;
2039 EquivalenceClass *ipeclass;
2042 /* fetch outer/inner eclass from mergeclause */
2043 Assert(IsA(rinfo, RestrictInfo));
2044 if (rinfo->outer_is_left)
2046 oeclass = rinfo->left_ec;
2047 ieclass = rinfo->right_ec;
2051 oeclass = rinfo->right_ec;
2052 ieclass = rinfo->left_ec;
2054 Assert(oeclass != NULL);
2055 Assert(ieclass != NULL);
2058 * For debugging purposes, we check that the eclasses match the paths'
2059 * pathkeys. In typical cases the merge clauses are one-to-one with
2060 * the pathkeys, but when dealing with partially redundant query
2061 * conditions, we might have clauses that re-reference earlier path
2062 * keys. The case that we need to reject is where a pathkey is
2063 * entirely skipped over.
2065 * lop and lip reference the first as-yet-unused pathkey elements;
2066 * it's okay to match them, or any element before them. If they're
2067 * NULL then we have found all pathkey elements to be used.
2071 opathkey = (PathKey *) lfirst(lop);
2072 opeclass = opathkey->pk_eclass;
2073 if (oeclass == opeclass)
2075 /* fast path for typical case */
2080 /* redundant clauses ... must match something before lop */
2081 foreach(l2, outerpathkeys)
2085 opathkey = (PathKey *) lfirst(l2);
2086 opeclass = opathkey->pk_eclass;
2087 if (oeclass == opeclass)
2090 if (oeclass != opeclass)
2091 elog(ERROR, "outer pathkeys do not match mergeclauses");
2096 /* redundant clauses ... must match some already-used pathkey */
2099 foreach(l2, outerpathkeys)
2101 opathkey = (PathKey *) lfirst(l2);
2102 opeclass = opathkey->pk_eclass;
2103 if (oeclass == opeclass)
2107 elog(ERROR, "outer pathkeys do not match mergeclauses");
2112 ipathkey = (PathKey *) lfirst(lip);
2113 ipeclass = ipathkey->pk_eclass;
2114 if (ieclass == ipeclass)
2116 /* fast path for typical case */
2121 /* redundant clauses ... must match something before lip */
2122 foreach(l2, innerpathkeys)
2126 ipathkey = (PathKey *) lfirst(l2);
2127 ipeclass = ipathkey->pk_eclass;
2128 if (ieclass == ipeclass)
2131 if (ieclass != ipeclass)
2132 elog(ERROR, "inner pathkeys do not match mergeclauses");
2137 /* redundant clauses ... must match some already-used pathkey */
2140 foreach(l2, innerpathkeys)
2142 ipathkey = (PathKey *) lfirst(l2);
2143 ipeclass = ipathkey->pk_eclass;
2144 if (ieclass == ipeclass)
2148 elog(ERROR, "inner pathkeys do not match mergeclauses");
2151 /* pathkeys should match each other too (more debugging) */
2152 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
2153 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
2154 opathkey->pk_strategy != ipathkey->pk_strategy ||
2155 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
2156 elog(ERROR, "left and right pathkeys do not match in mergejoin");
2158 /* OK, save info for executor */
2159 mergefamilies[i] = opathkey->pk_opfamily;
2160 mergecollations[i] = opathkey->pk_eclass->ec_collation;
2161 mergestrategies[i] = opathkey->pk_strategy;
2162 mergenullsfirst[i] = opathkey->pk_nulls_first;
2167 * Note: it is not an error if we have additional pathkey elements (i.e.,
2168 * lop or lip isn't NULL here). The input paths might be better-sorted
2169 * than we need for the current mergejoin.
2173 * Now we can build the mergejoin node.
2175 join_plan = make_mergejoin(tlist,
2185 best_path->jpath.jointype);
2187 /* Costs of sort and material steps are included in path cost already */
2188 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2194 create_hashjoin_plan(PlannerInfo *root,
2195 HashPath *best_path,
2199 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
2203 Oid skewTable = InvalidOid;
2204 AttrNumber skewColumn = InvalidAttrNumber;
2205 bool skewInherit = false;
2206 Oid skewColType = InvalidOid;
2207 int32 skewColTypmod = -1;
2208 HashJoin *join_plan;
2211 /* Sort join qual clauses into best execution order */
2212 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2213 /* There's no point in sorting the hash clauses ... */
2215 /* Get the join qual clauses (in plain expression form) */
2216 /* Any pseudoconstant clauses are ignored here */
2217 if (IS_OUTER_JOIN(best_path->jpath.jointype))
2219 extract_actual_join_clauses(joinclauses,
2220 &joinclauses, &otherclauses);
2224 /* We can treat all clauses alike for an inner join */
2225 joinclauses = extract_actual_clauses(joinclauses, false);
2230 * Remove the hashclauses from the list of join qual clauses, leaving the
2231 * list of quals that must be checked as qpquals.
2233 hashclauses = get_actual_clauses(best_path->path_hashclauses);
2234 joinclauses = list_difference(joinclauses, hashclauses);
2237 * Rearrange hashclauses, if needed, so that the outer variable is always
2240 hashclauses = get_switched_clauses(best_path->path_hashclauses,
2241 best_path->jpath.outerjoinpath->parent->relids);
2243 /* We don't want any excess columns in the hashed tuples */
2244 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
2246 /* If we expect batching, suppress excess columns in outer tuples too */
2247 if (best_path->num_batches > 1)
2248 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
2251 * If there is a single join clause and we can identify the outer variable
2252 * as a simple column reference, supply its identity for possible use in
2253 * skew optimization. (Note: in principle we could do skew optimization
2254 * with multiple join clauses, but we'd have to be able to determine the
2255 * most common combinations of outer values, which we don't currently have
2256 * enough stats for.)
2258 if (list_length(hashclauses) == 1)
2260 OpExpr *clause = (OpExpr *) linitial(hashclauses);
2263 Assert(is_opclause(clause));
2264 node = (Node *) linitial(clause->args);
2265 if (IsA(node, RelabelType))
2266 node = (Node *) ((RelabelType *) node)->arg;
2269 Var *var = (Var *) node;
2272 rte = root->simple_rte_array[var->varno];
2273 if (rte->rtekind == RTE_RELATION)
2275 skewTable = rte->relid;
2276 skewColumn = var->varattno;
2277 skewInherit = rte->inh;
2278 skewColType = var->vartype;
2279 skewColTypmod = var->vartypmod;
2285 * Build the hash node and hash join node.
2287 hash_plan = make_hash(inner_plan,
2293 join_plan = make_hashjoin(tlist,
2299 best_path->jpath.jointype);
2301 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2307 /*****************************************************************************
2309 * SUPPORTING ROUTINES
2311 *****************************************************************************/
2314 * replace_nestloop_params
2315 * Replace outer-relation Vars in the given expression with nestloop Params
2317 * All Vars belonging to the relation(s) identified by root->curOuterRels
2318 * are replaced by Params, and entries are added to root->curOuterParams if
2319 * not already present.
2322 replace_nestloop_params(PlannerInfo *root, Node *expr)
2324 /* No setup needed for tree walk, so away we go */
2325 return replace_nestloop_params_mutator(expr, root);
2329 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
2335 Var *var = (Var *) node;
2340 /* Upper-level Vars should be long gone at this point */
2341 Assert(var->varlevelsup == 0);
2342 /* If not to be replaced, we can just return the Var unmodified */
2343 if (!bms_is_member(var->varno, root->curOuterRels))
2345 /* Create a Param representing the Var */
2346 param = assign_nestloop_param(root, var);
2347 /* Is this param already listed in root->curOuterParams? */
2348 foreach(lc, root->curOuterParams)
2350 nlp = (NestLoopParam *) lfirst(lc);
2351 if (nlp->paramno == param->paramid)
2353 Assert(equal(var, nlp->paramval));
2354 /* Present, so we can just return the Param */
2355 return (Node *) param;
2359 nlp = makeNode(NestLoopParam);
2360 nlp->paramno = param->paramid;
2361 nlp->paramval = var;
2362 root->curOuterParams = lappend(root->curOuterParams, nlp);
2363 /* And return the replacement Param */
2364 return (Node *) param;
2366 return expression_tree_mutator(node,
2367 replace_nestloop_params_mutator,
2372 * fix_indexqual_references
2373 * Adjust indexqual clauses to the form the executor's indexqual
2376 * We have four tasks here:
2377 * * Remove RestrictInfo nodes from the input clauses.
2378 * * Replace any outer-relation Var nodes with nestloop Params.
2379 * (XXX eventually, that responsibility should go elsewhere?)
2380 * * Index keys must be represented by Var nodes with varattno set to the
2381 * index's attribute number, not the attribute number in the original rel.
2382 * * If the index key is on the right, commute the clause to put it on the
2385 * The result is a modified copy of the indexquals list --- the
2386 * original is not changed. Note also that the copy shares no substructure
2387 * with the original; this is needed in case there is a subplan in it (we need
2388 * two separate copies of the subplan tree, or things will go awry).
2391 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
2394 IndexOptInfo *index = index_path->indexinfo;
2395 List *fixed_indexquals;
2398 fixed_indexquals = NIL;
2400 foreach(l, indexquals)
2402 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2405 Assert(IsA(rinfo, RestrictInfo));
2408 * Replace any outer-relation variables with nestloop params.
2410 * This also makes a copy of the clause, so it's safe to modify it
2413 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
2415 if (IsA(clause, OpExpr))
2417 OpExpr *op = (OpExpr *) clause;
2419 if (list_length(op->args) != 2)
2420 elog(ERROR, "indexqual clause is not binary opclause");
2423 * Check to see if the indexkey is on the right; if so, commute
2424 * the clause. The indexkey should be the side that refers to
2425 * (only) the base relation.
2427 if (!bms_equal(rinfo->left_relids, index->rel->relids))
2431 * Now, determine which index attribute this is and change the
2432 * indexkey operand as needed.
2434 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2437 else if (IsA(clause, RowCompareExpr))
2439 RowCompareExpr *rc = (RowCompareExpr *) clause;
2443 * Check to see if the indexkey is on the right; if so, commute
2444 * the clause. The indexkey should be the side that refers to
2445 * (only) the base relation.
2447 if (!bms_overlap(pull_varnos(linitial(rc->largs)),
2448 index->rel->relids))
2449 CommuteRowCompareExpr(rc);
2452 * For each column in the row comparison, determine which index
2453 * attribute this is and change the indexkey operand as needed.
2455 foreach(lc, rc->largs)
2457 lfirst(lc) = fix_indexqual_operand(lfirst(lc),
2461 else if (IsA(clause, ScalarArrayOpExpr))
2463 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2465 /* Never need to commute... */
2468 * Determine which index attribute this is and change the indexkey
2469 * operand as needed.
2471 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2474 else if (IsA(clause, NullTest))
2476 NullTest *nt = (NullTest *) clause;
2478 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
2482 elog(ERROR, "unsupported indexqual type: %d",
2483 (int) nodeTag(clause));
2485 fixed_indexquals = lappend(fixed_indexquals, clause);
2488 return fixed_indexquals;
2492 * fix_indexorderby_references
2493 * Adjust indexorderby clauses to the form the executor's index
2496 * This is a simplified version of fix_indexqual_references. The input does
2497 * not have RestrictInfo nodes, and we assume that indxqual.c already
2498 * commuted the clauses to put the index keys on the left. Also, we don't
2499 * bother to support any cases except simple OpExprs, since nothing else
2500 * is allowed for ordering operators.
2503 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path,
2504 List *indexorderbys)
2506 IndexOptInfo *index = index_path->indexinfo;
2507 List *fixed_indexorderbys;
2510 fixed_indexorderbys = NIL;
2512 foreach(l, indexorderbys)
2514 Node *clause = (Node *) lfirst(l);
2517 * Replace any outer-relation variables with nestloop params.
2519 * This also makes a copy of the clause, so it's safe to modify it
2522 clause = replace_nestloop_params(root, clause);
2524 if (IsA(clause, OpExpr))
2526 OpExpr *op = (OpExpr *) clause;
2528 if (list_length(op->args) != 2)
2529 elog(ERROR, "indexorderby clause is not binary opclause");
2532 * Now, determine which index attribute this is and change the
2533 * indexkey operand as needed.
2535 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2539 elog(ERROR, "unsupported indexorderby type: %d",
2540 (int) nodeTag(clause));
2542 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
2545 return fixed_indexorderbys;
2549 * fix_indexqual_operand
2550 * Convert an indexqual expression to a Var referencing the index column.
2553 fix_indexqual_operand(Node *node, IndexOptInfo *index)
2556 * We represent index keys by Var nodes having the varno of the base table
2557 * but varattno equal to the index's attribute number (index column
2558 * position). This is a bit hokey ... would be cleaner to use a
2559 * special-purpose node type that could not be mistaken for a regular Var.
2560 * But it will do for now.
2564 ListCell *indexpr_item;
2567 * Remove any binary-compatible relabeling of the indexkey
2569 if (IsA(node, RelabelType))
2570 node = (Node *) ((RelabelType *) node)->arg;
2572 if (IsA(node, Var) &&
2573 ((Var *) node)->varno == index->rel->relid)
2575 /* Try to match against simple index columns */
2576 int varatt = ((Var *) node)->varattno;
2580 for (pos = 0; pos < index->ncolumns; pos++)
2582 if (index->indexkeys[pos] == varatt)
2584 result = (Var *) copyObject(node);
2585 result->varattno = pos + 1;
2586 return (Node *) result;
2592 /* Try to match against index expressions */
2593 indexpr_item = list_head(index->indexprs);
2594 for (pos = 0; pos < index->ncolumns; pos++)
2596 if (index->indexkeys[pos] == 0)
2600 if (indexpr_item == NULL)
2601 elog(ERROR, "too few entries in indexprs list");
2602 indexkey = (Node *) lfirst(indexpr_item);
2603 if (indexkey && IsA(indexkey, RelabelType))
2604 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2605 if (equal(node, indexkey))
2608 result = makeVar(index->rel->relid, pos + 1,
2609 exprType(lfirst(indexpr_item)), -1,
2610 exprCollation(lfirst(indexpr_item)),
2612 return (Node *) result;
2614 indexpr_item = lnext(indexpr_item);
2619 elog(ERROR, "node is not an index attribute");
2620 return NULL; /* keep compiler quiet */
2624 * get_switched_clauses
2625 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2626 * extract the bare clauses, and rearrange the elements within the
2627 * clauses, if needed, so the outer join variable is on the left and
2628 * the inner is on the right. The original clause data structure is not
2629 * touched; a modified list is returned. We do, however, set the transient
2630 * outer_is_left field in each RestrictInfo to show which side was which.
2633 get_switched_clauses(List *clauses, Relids outerrelids)
2640 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
2641 OpExpr *clause = (OpExpr *) restrictinfo->clause;
2643 Assert(is_opclause(clause));
2644 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
2647 * Duplicate just enough of the structure to allow commuting the
2648 * clause without changing the original list. Could use
2649 * copyObject, but a complete deep copy is overkill.
2651 OpExpr *temp = makeNode(OpExpr);
2653 temp->opno = clause->opno;
2654 temp->opfuncid = InvalidOid;
2655 temp->opresulttype = clause->opresulttype;
2656 temp->opretset = clause->opretset;
2657 temp->opcollid = clause->opcollid;
2658 temp->inputcollid = clause->inputcollid;
2659 temp->args = list_copy(clause->args);
2660 temp->location = clause->location;
2661 /* Commute it --- note this modifies the temp node in-place. */
2662 CommuteOpExpr(temp);
2663 t_list = lappend(t_list, temp);
2664 restrictinfo->outer_is_left = false;
2668 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
2669 t_list = lappend(t_list, clause);
2670 restrictinfo->outer_is_left = true;
2677 * order_qual_clauses
2678 * Given a list of qual clauses that will all be evaluated at the same
2679 * plan node, sort the list into the order we want to check the quals
2682 * Ideally the order should be driven by a combination of execution cost and
2683 * selectivity, but it's not immediately clear how to account for both,
2684 * and given the uncertainty of the estimates the reliability of the decisions
2685 * would be doubtful anyway. So we just order by estimated per-tuple cost,
2686 * being careful not to change the order when (as is often the case) the
2687 * estimates are identical.
2689 * Although this will work on either bare clauses or RestrictInfos, it's
2690 * much faster to apply it to RestrictInfos, since it can re-use cost
2691 * information that is cached in RestrictInfos.
2693 * Note: some callers pass lists that contain entries that will later be
2694 * removed; this is the easiest way to let this routine see RestrictInfos
2695 * instead of bare clauses. It's OK because we only sort by cost, but
2696 * a cost/selectivity combination would likely do the wrong thing.
2699 order_qual_clauses(PlannerInfo *root, List *clauses)
2706 int nitems = list_length(clauses);
2712 /* No need to work hard for 0 or 1 clause */
2717 * Collect the items and costs into an array. This is to avoid repeated
2718 * cost_qual_eval work if the inputs aren't RestrictInfos.
2720 items = (QualItem *) palloc(nitems * sizeof(QualItem));
2722 foreach(lc, clauses)
2724 Node *clause = (Node *) lfirst(lc);
2727 cost_qual_eval_node(&qcost, clause, root);
2728 items[i].clause = clause;
2729 items[i].cost = qcost.per_tuple;
2734 * Sort. We don't use qsort() because it's not guaranteed stable for
2735 * equal keys. The expected number of entries is small enough that a
2736 * simple insertion sort should be good enough.
2738 for (i = 1; i < nitems; i++)
2740 QualItem newitem = items[i];
2743 /* insert newitem into the already-sorted subarray */
2744 for (j = i; j > 0; j--)
2746 if (newitem.cost >= items[j - 1].cost)
2748 items[j] = items[j - 1];
2753 /* Convert back to a list */
2755 for (i = 0; i < nitems; i++)
2756 result = lappend(result, items[i].clause);
2762 * Copy cost and size info from a Path node to the Plan node created from it.
2763 * The executor usually won't use this info, but it's needed by EXPLAIN.
2766 copy_path_costsize(Plan *dest, Path *src)
2770 dest->startup_cost = src->startup_cost;
2771 dest->total_cost = src->total_cost;
2772 dest->plan_rows = src->parent->rows;
2773 dest->plan_width = src->parent->width;
2777 dest->startup_cost = 0;
2778 dest->total_cost = 0;
2779 dest->plan_rows = 0;
2780 dest->plan_width = 0;
2785 * Copy cost and size info from a lower plan node to an inserted node.
2786 * (Most callers alter the info after copying it.)
2789 copy_plan_costsize(Plan *dest, Plan *src)
2793 dest->startup_cost = src->startup_cost;
2794 dest->total_cost = src->total_cost;
2795 dest->plan_rows = src->plan_rows;
2796 dest->plan_width = src->plan_width;
2800 dest->startup_cost = 0;
2801 dest->total_cost = 0;
2802 dest->plan_rows = 0;
2803 dest->plan_width = 0;
2808 /*****************************************************************************
2810 * PLAN NODE BUILDING ROUTINES
2812 * Some of these are exported because they are called to build plan nodes
2813 * in contexts where we're not deriving the plan node from a path node.
2815 *****************************************************************************/
2818 make_seqscan(List *qptlist,
2822 SeqScan *node = makeNode(SeqScan);
2823 Plan *plan = &node->plan;
2825 /* cost should be inserted by caller */
2826 plan->targetlist = qptlist;
2827 plan->qual = qpqual;
2828 plan->lefttree = NULL;
2829 plan->righttree = NULL;
2830 node->scanrelid = scanrelid;
2836 make_indexscan(List *qptlist,
2841 List *indexqualorig,
2843 List *indexorderbyorig,
2844 ScanDirection indexscandir)
2846 IndexScan *node = makeNode(IndexScan);
2847 Plan *plan = &node->scan.plan;
2849 /* cost should be inserted by caller */
2850 plan->targetlist = qptlist;
2851 plan->qual = qpqual;
2852 plan->lefttree = NULL;
2853 plan->righttree = NULL;
2854 node->scan.scanrelid = scanrelid;
2855 node->indexid = indexid;
2856 node->indexqual = indexqual;
2857 node->indexqualorig = indexqualorig;
2858 node->indexorderby = indexorderby;
2859 node->indexorderbyorig = indexorderbyorig;
2860 node->indexorderdir = indexscandir;
2865 static BitmapIndexScan *
2866 make_bitmap_indexscan(Index scanrelid,
2869 List *indexqualorig)
2871 BitmapIndexScan *node = makeNode(BitmapIndexScan);
2872 Plan *plan = &node->scan.plan;
2874 /* cost should be inserted by caller */
2875 plan->targetlist = NIL; /* not used */
2876 plan->qual = NIL; /* not used */
2877 plan->lefttree = NULL;
2878 plan->righttree = NULL;
2879 node->scan.scanrelid = scanrelid;
2880 node->indexid = indexid;
2881 node->indexqual = indexqual;
2882 node->indexqualorig = indexqualorig;
2887 static BitmapHeapScan *
2888 make_bitmap_heapscan(List *qptlist,
2891 List *bitmapqualorig,
2894 BitmapHeapScan *node = makeNode(BitmapHeapScan);
2895 Plan *plan = &node->scan.plan;
2897 /* cost should be inserted by caller */
2898 plan->targetlist = qptlist;
2899 plan->qual = qpqual;
2900 plan->lefttree = lefttree;
2901 plan->righttree = NULL;
2902 node->scan.scanrelid = scanrelid;
2903 node->bitmapqualorig = bitmapqualorig;
2909 make_tidscan(List *qptlist,
2914 TidScan *node = makeNode(TidScan);
2915 Plan *plan = &node->scan.plan;
2917 /* cost should be inserted by caller */
2918 plan->targetlist = qptlist;
2919 plan->qual = qpqual;
2920 plan->lefttree = NULL;
2921 plan->righttree = NULL;
2922 node->scan.scanrelid = scanrelid;
2923 node->tidquals = tidquals;
2929 make_subqueryscan(List *qptlist,
2934 SubqueryScan *node = makeNode(SubqueryScan);
2935 Plan *plan = &node->scan.plan;
2938 * Cost is figured here for the convenience of prepunion.c. Note this is
2939 * only correct for the case where qpqual is empty; otherwise caller
2940 * should overwrite cost with a better estimate.
2942 copy_plan_costsize(plan, subplan);
2943 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
2945 plan->targetlist = qptlist;
2946 plan->qual = qpqual;
2947 plan->lefttree = NULL;
2948 plan->righttree = NULL;
2949 node->scan.scanrelid = scanrelid;
2950 node->subplan = subplan;
2955 static FunctionScan *
2956 make_functionscan(List *qptlist,
2962 List *funccoltypmods,
2963 List *funccolcollations)
2965 FunctionScan *node = makeNode(FunctionScan);
2966 Plan *plan = &node->scan.plan;
2968 /* cost should be inserted by caller */
2969 plan->targetlist = qptlist;
2970 plan->qual = qpqual;
2971 plan->lefttree = NULL;
2972 plan->righttree = NULL;
2973 node->scan.scanrelid = scanrelid;
2974 node->funcexpr = funcexpr;
2975 node->funccolnames = funccolnames;
2976 node->funccoltypes = funccoltypes;
2977 node->funccoltypmods = funccoltypmods;
2978 node->funccolcollations = funccolcollations;
2984 make_valuesscan(List *qptlist,
2989 ValuesScan *node = makeNode(ValuesScan);
2990 Plan *plan = &node->scan.plan;
2992 /* cost should be inserted by caller */
2993 plan->targetlist = qptlist;
2994 plan->qual = qpqual;
2995 plan->lefttree = NULL;
2996 plan->righttree = NULL;
2997 node->scan.scanrelid = scanrelid;
2998 node->values_lists = values_lists;
3004 make_ctescan(List *qptlist,
3010 CteScan *node = makeNode(CteScan);
3011 Plan *plan = &node->scan.plan;
3013 /* cost should be inserted by caller */
3014 plan->targetlist = qptlist;
3015 plan->qual = qpqual;
3016 plan->lefttree = NULL;
3017 plan->righttree = NULL;
3018 node->scan.scanrelid = scanrelid;
3019 node->ctePlanId = ctePlanId;
3020 node->cteParam = cteParam;
3025 static WorkTableScan *
3026 make_worktablescan(List *qptlist,
3031 WorkTableScan *node = makeNode(WorkTableScan);
3032 Plan *plan = &node->scan.plan;
3034 /* cost should be inserted by caller */
3035 plan->targetlist = qptlist;
3036 plan->qual = qpqual;
3037 plan->lefttree = NULL;
3038 plan->righttree = NULL;
3039 node->scan.scanrelid = scanrelid;
3040 node->wtParam = wtParam;
3045 static ForeignScan *
3046 make_foreignscan(List *qptlist,
3052 ForeignScan *node = makeNode(ForeignScan);
3053 Plan *plan = &node->scan.plan;
3055 /* cost should be inserted by caller */
3056 plan->targetlist = qptlist;
3057 plan->qual = qpqual;
3058 plan->lefttree = NULL;
3059 plan->righttree = NULL;
3060 node->scan.scanrelid = scanrelid;
3061 node->fsSystemCol = fsSystemCol;
3062 node->fdwplan = fdwplan;
3068 make_append(List *appendplans, List *tlist)
3070 Append *node = makeNode(Append);
3071 Plan *plan = &node->plan;
3076 * Compute cost as sum of subplan costs. We charge nothing extra for the
3077 * Append itself, which perhaps is too optimistic, but since it doesn't do
3078 * any selection or projection, it is a pretty cheap node.
3080 * If you change this, see also create_append_path(). Also, the size
3081 * calculations should match set_append_rel_pathlist(). It'd be better
3082 * not to duplicate all this logic, but some callers of this function
3083 * aren't working from an appendrel or AppendPath, so there's noplace to
3084 * copy the data from.
3086 plan->startup_cost = 0;
3087 plan->total_cost = 0;
3088 plan->plan_rows = 0;
3090 foreach(subnode, appendplans)
3092 Plan *subplan = (Plan *) lfirst(subnode);
3094 if (subnode == list_head(appendplans)) /* first node? */
3095 plan->startup_cost = subplan->startup_cost;
3096 plan->total_cost += subplan->total_cost;
3097 plan->plan_rows += subplan->plan_rows;
3098 total_size += subplan->plan_width * subplan->plan_rows;
3100 if (plan->plan_rows > 0)
3101 plan->plan_width = rint(total_size / plan->plan_rows);
3103 plan->plan_width = 0;
3105 plan->targetlist = tlist;
3107 plan->lefttree = NULL;
3108 plan->righttree = NULL;
3109 node->appendplans = appendplans;
3115 make_recursive_union(List *tlist,
3122 RecursiveUnion *node = makeNode(RecursiveUnion);
3123 Plan *plan = &node->plan;
3124 int numCols = list_length(distinctList);
3126 cost_recursive_union(plan, lefttree, righttree);
3128 plan->targetlist = tlist;
3130 plan->lefttree = lefttree;
3131 plan->righttree = righttree;
3132 node->wtParam = wtParam;
3135 * convert SortGroupClause list into arrays of attr indexes and equality
3136 * operators, as wanted by executor
3138 node->numCols = numCols;
3142 AttrNumber *dupColIdx;
3146 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3147 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3149 foreach(slitem, distinctList)
3151 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3152 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
3155 dupColIdx[keyno] = tle->resno;
3156 dupOperators[keyno] = sortcl->eqop;
3157 Assert(OidIsValid(dupOperators[keyno]));
3160 node->dupColIdx = dupColIdx;
3161 node->dupOperators = dupOperators;
3163 node->numGroups = numGroups;
3169 make_bitmap_and(List *bitmapplans)
3171 BitmapAnd *node = makeNode(BitmapAnd);
3172 Plan *plan = &node->plan;
3174 /* cost should be inserted by caller */
3175 plan->targetlist = NIL;
3177 plan->lefttree = NULL;
3178 plan->righttree = NULL;
3179 node->bitmapplans = bitmapplans;
3185 make_bitmap_or(List *bitmapplans)
3187 BitmapOr *node = makeNode(BitmapOr);
3188 Plan *plan = &node->plan;
3190 /* cost should be inserted by caller */
3191 plan->targetlist = NIL;
3193 plan->lefttree = NULL;
3194 plan->righttree = NULL;
3195 node->bitmapplans = bitmapplans;
3201 make_nestloop(List *tlist,
3209 NestLoop *node = makeNode(NestLoop);
3210 Plan *plan = &node->join.plan;
3212 /* cost should be inserted by caller */
3213 plan->targetlist = tlist;
3214 plan->qual = otherclauses;
3215 plan->lefttree = lefttree;
3216 plan->righttree = righttree;
3217 node->join.jointype = jointype;
3218 node->join.joinqual = joinclauses;
3219 node->nestParams = nestParams;
3225 make_hashjoin(List *tlist,
3233 HashJoin *node = makeNode(HashJoin);
3234 Plan *plan = &node->join.plan;
3236 /* cost should be inserted by caller */
3237 plan->targetlist = tlist;
3238 plan->qual = otherclauses;
3239 plan->lefttree = lefttree;
3240 plan->righttree = righttree;
3241 node->hashclauses = hashclauses;
3242 node->join.jointype = jointype;
3243 node->join.joinqual = joinclauses;
3249 make_hash(Plan *lefttree,
3251 AttrNumber skewColumn,
3254 int32 skewColTypmod)
3256 Hash *node = makeNode(Hash);
3257 Plan *plan = &node->plan;
3259 copy_plan_costsize(plan, lefttree);
3262 * For plausibility, make startup & total costs equal total cost of input
3263 * plan; this only affects EXPLAIN display not decisions.
3265 plan->startup_cost = plan->total_cost;
3266 plan->targetlist = lefttree->targetlist;
3268 plan->lefttree = lefttree;
3269 plan->righttree = NULL;
3271 node->skewTable = skewTable;
3272 node->skewColumn = skewColumn;
3273 node->skewInherit = skewInherit;
3274 node->skewColType = skewColType;
3275 node->skewColTypmod = skewColTypmod;
3281 make_mergejoin(List *tlist,
3286 Oid *mergecollations,
3287 int *mergestrategies,
3288 bool *mergenullsfirst,
3293 MergeJoin *node = makeNode(MergeJoin);
3294 Plan *plan = &node->join.plan;
3296 /* cost should be inserted by caller */
3297 plan->targetlist = tlist;
3298 plan->qual = otherclauses;
3299 plan->lefttree = lefttree;
3300 plan->righttree = righttree;
3301 node->mergeclauses = mergeclauses;
3302 node->mergeFamilies = mergefamilies;
3303 node->mergeCollations = mergecollations;
3304 node->mergeStrategies = mergestrategies;
3305 node->mergeNullsFirst = mergenullsfirst;
3306 node->join.jointype = jointype;
3307 node->join.joinqual = joinclauses;
3313 * make_sort --- basic routine to build a Sort plan node
3315 * Caller must have built the sortColIdx, sortOperators, collations, and
3316 * nullsFirst arrays already.
3317 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
3320 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
3321 AttrNumber *sortColIdx, Oid *sortOperators,
3322 Oid *collations, bool *nullsFirst,
3323 double limit_tuples)
3325 Sort *node = makeNode(Sort);
3326 Plan *plan = &node->plan;
3327 Path sort_path; /* dummy for result of cost_sort */
3329 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3330 cost_sort(&sort_path, root, NIL,
3331 lefttree->total_cost,
3332 lefttree->plan_rows,
3333 lefttree->plan_width,
3337 plan->startup_cost = sort_path.startup_cost;
3338 plan->total_cost = sort_path.total_cost;
3339 plan->targetlist = lefttree->targetlist;
3341 plan->lefttree = lefttree;
3342 plan->righttree = NULL;
3343 node->numCols = numCols;
3344 node->sortColIdx = sortColIdx;
3345 node->sortOperators = sortOperators;
3346 node->collations = collations;
3347 node->nullsFirst = nullsFirst;
3353 * add_sort_column --- utility subroutine for building sort info arrays
3355 * We need this routine because the same column might be selected more than
3356 * once as a sort key column; if so, the extra mentions are redundant.
3358 * Caller is assumed to have allocated the arrays large enough for the
3359 * max possible number of columns. Return value is the new column count.
3362 add_sort_column(AttrNumber colIdx, Oid sortOp, Oid coll, bool nulls_first,
3363 int numCols, AttrNumber *sortColIdx,
3364 Oid *sortOperators, Oid *collations, bool *nullsFirst)
3368 Assert(OidIsValid(sortOp));
3370 for (i = 0; i < numCols; i++)
3373 * Note: we check sortOp because it's conceivable that "ORDER BY foo
3374 * USING <, foo USING <<<" is not redundant, if <<< distinguishes
3375 * values that < considers equal. We need not check nulls_first
3376 * however because a lower-order column with the same sortop but
3377 * opposite nulls direction is redundant.
3379 * We could probably consider sort keys with the same sortop and
3380 * different collations to be redundant too, but for the moment treat
3381 * them as not redundant. This will be needed if we ever support
3382 * collations with different notions of equality.
3384 if (sortColIdx[i] == colIdx &&
3385 sortOperators[numCols] == sortOp &&
3386 collations[numCols] == coll)
3388 /* Already sorting by this col, so extra sort key is useless */
3393 /* Add the column */
3394 sortColIdx[numCols] = colIdx;
3395 sortOperators[numCols] = sortOp;
3396 collations[numCols] = coll;
3397 nullsFirst[numCols] = nulls_first;
3402 * prepare_sort_from_pathkeys
3403 * Prepare to sort according to given pathkeys
3405 * This is used to set up for both Sort and MergeAppend nodes. It calculates
3406 * the executor's representation of the sort key information, and adjusts the
3407 * plan targetlist if needed to add resjunk sort columns.
3410 * 'lefttree' is the node which yields input tuples
3411 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3412 * 'adjust_tlist_in_place' is TRUE if lefttree must be modified in-place
3414 * We must convert the pathkey information into arrays of sort key column
3415 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
3416 * which is the representation the executor wants. These are returned into
3417 * the output parameters *p_numsortkeys etc.
3419 * If the pathkeys include expressions that aren't simple Vars, we will
3420 * usually need to add resjunk items to the input plan's targetlist to
3421 * compute these expressions, since the Sort/MergeAppend node itself won't
3422 * do any such calculations. If the input plan type isn't one that can do
3423 * projections, this means adding a Result node just to do the projection.
3424 * However, the caller can pass adjust_tlist_in_place = TRUE to force the
3425 * lefttree tlist to be modified in-place regardless of whether the node type
3426 * can project --- we use this for fixing the tlist of MergeAppend itself.
3428 * Returns the node which is to be the input to the Sort (either lefttree,
3429 * or a Result stacked atop lefttree).
3432 prepare_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3433 bool adjust_tlist_in_place,
3435 AttrNumber **p_sortColIdx,
3436 Oid **p_sortOperators,
3438 bool **p_nullsFirst)
3440 List *tlist = lefttree->targetlist;
3443 AttrNumber *sortColIdx;
3449 * We will need at most list_length(pathkeys) sort columns; possibly less
3451 numsortkeys = list_length(pathkeys);
3452 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3453 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3454 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3455 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3459 foreach(i, pathkeys)
3461 PathKey *pathkey = (PathKey *) lfirst(i);
3462 EquivalenceClass *ec = pathkey->pk_eclass;
3463 TargetEntry *tle = NULL;
3464 Oid pk_datatype = InvalidOid;
3468 if (ec->ec_has_volatile)
3471 * If the pathkey's EquivalenceClass is volatile, then it must
3472 * have come from an ORDER BY clause, and we have to match it to
3473 * that same targetlist entry.
3475 if (ec->ec_sortref == 0) /* can't happen */
3476 elog(ERROR, "volatile EquivalenceClass has no sortref");
3477 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
3479 Assert(list_length(ec->ec_members) == 1);
3480 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
3485 * Otherwise, we can sort by any non-constant expression listed in
3486 * the pathkey's EquivalenceClass. For now, we take the first one
3487 * that corresponds to an available item in the tlist. If there
3488 * isn't any, use the first one that is an expression in the
3489 * input's vars. (The non-const restriction only matters if the
3490 * EC is below_outer_join; but if it isn't, it won't contain
3491 * consts anyway, else we'd have discarded the pathkey as
3494 * XXX if we have a choice, is there any way of figuring out which
3495 * might be cheapest to execute? (For example, int4lt is likely
3496 * much cheaper to execute than numericlt, but both might appear
3497 * in the same equivalence class...) Not clear that we ever will
3498 * have an interesting choice in practice, so it may not matter.
3500 foreach(j, ec->ec_members)
3502 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3505 * We shouldn't be trying to sort by an equivalence class that
3506 * contains a constant, so no need to consider such cases any
3509 if (em->em_is_const)
3512 tle = tlist_member((Node *) em->em_expr, tlist);
3515 pk_datatype = em->em_datatype;
3516 break; /* found expr already in tlist */
3520 * We can also use it if the pathkey expression is a relabel
3521 * of the tlist entry, or vice versa. This is needed for
3522 * binary-compatible cases (cf. make_pathkey_from_sortinfo).
3523 * We prefer an exact match, though, so we do the basic search
3526 tle = tlist_member_ignore_relabel((Node *) em->em_expr, tlist);
3529 pk_datatype = em->em_datatype;
3530 break; /* found expr already in tlist */
3536 /* No matching tlist item; look for a computable expression */
3537 Expr *sortexpr = NULL;
3539 foreach(j, ec->ec_members)
3541 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3545 if (em->em_is_const)
3547 sortexpr = em->em_expr;
3548 exprvars = pull_var_clause((Node *) sortexpr,
3549 PVC_RECURSE_AGGREGATES,
3550 PVC_INCLUDE_PLACEHOLDERS);
3551 foreach(k, exprvars)
3553 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
3556 list_free(exprvars);
3559 pk_datatype = em->em_datatype;
3560 break; /* found usable expression */
3564 elog(ERROR, "could not find pathkey item to sort");
3567 * Do we need to insert a Result node?
3569 if (!adjust_tlist_in_place &&
3570 !is_projection_capable_plan(lefttree))
3572 /* copy needed so we don't modify input's tlist below */
3573 tlist = copyObject(tlist);
3574 lefttree = (Plan *) make_result(root, tlist, NULL,
3578 /* Don't bother testing is_projection_capable_plan again */
3579 adjust_tlist_in_place = true;
3582 * Add resjunk entry to input's tlist
3584 tle = makeTargetEntry(sortexpr,
3585 list_length(tlist) + 1,
3588 tlist = lappend(tlist, tle);
3589 lefttree->targetlist = tlist; /* just in case NIL before */
3594 * Look up the correct sort operator from the PathKey's slightly
3595 * abstracted representation.
3597 sortop = get_opfamily_member(pathkey->pk_opfamily,
3600 pathkey->pk_strategy);
3601 if (!OidIsValid(sortop)) /* should not happen */
3602 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
3603 pathkey->pk_strategy, pk_datatype, pk_datatype,
3604 pathkey->pk_opfamily);
3607 * The column might already be selected as a sort key, if the pathkeys
3608 * contain duplicate entries. (This can happen in scenarios where
3609 * multiple mergejoinable clauses mention the same var, for example.)
3610 * So enter it only once in the sort arrays.
3612 numsortkeys = add_sort_column(tle->resno,
3614 pathkey->pk_eclass->ec_collation,
3615 pathkey->pk_nulls_first,
3617 sortColIdx, sortOperators,
3618 collations, nullsFirst);
3621 Assert(numsortkeys > 0);
3623 /* Return results */
3624 *p_numsortkeys = numsortkeys;
3625 *p_sortColIdx = sortColIdx;
3626 *p_sortOperators = sortOperators;
3627 *p_collations = collations;
3628 *p_nullsFirst = nullsFirst;
3634 * make_sort_from_pathkeys
3635 * Create sort plan to sort according to given pathkeys
3637 * 'lefttree' is the node which yields input tuples
3638 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3639 * 'limit_tuples' is the bound on the number of output tuples;
3643 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3644 double limit_tuples)
3647 AttrNumber *sortColIdx;
3652 /* Compute sort column info, and adjust lefttree as needed */
3653 lefttree = prepare_sort_from_pathkeys(root, lefttree, pathkeys,
3661 /* Now build the Sort node */
3662 return make_sort(root, lefttree, numsortkeys,
3663 sortColIdx, sortOperators, collations,
3664 nullsFirst, limit_tuples);
3668 * make_sort_from_sortclauses
3669 * Create sort plan to sort according to given sortclauses
3671 * 'sortcls' is a list of SortGroupClauses
3672 * 'lefttree' is the node which yields input tuples
3675 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
3677 List *sub_tlist = lefttree->targetlist;
3680 AttrNumber *sortColIdx;
3686 * We will need at most list_length(sortcls) sort columns; possibly less
3688 numsortkeys = list_length(sortcls);
3689 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3690 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3691 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3692 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3698 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
3699 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
3702 * Check for the possibility of duplicate order-by clauses --- the
3703 * parser should have removed 'em, but no point in sorting
3706 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
3707 exprCollation((Node *) tle->expr),
3708 sortcl->nulls_first,
3710 sortColIdx, sortOperators,
3711 collations, nullsFirst);
3714 Assert(numsortkeys > 0);
3716 return make_sort(root, lefttree, numsortkeys,
3717 sortColIdx, sortOperators, collations,
3722 * make_sort_from_groupcols
3723 * Create sort plan to sort based on grouping columns
3725 * 'groupcls' is the list of SortGroupClauses
3726 * 'grpColIdx' gives the column numbers to use
3728 * This might look like it could be merged with make_sort_from_sortclauses,
3729 * but presently we *must* use the grpColIdx[] array to locate sort columns,
3730 * because the child plan's tlist is not marked with ressortgroupref info
3731 * appropriate to the grouping node. So, only the sort ordering info
3732 * is used from the SortGroupClause entries.
3735 make_sort_from_groupcols(PlannerInfo *root,
3737 AttrNumber *grpColIdx,
3740 List *sub_tlist = lefttree->targetlist;
3744 AttrNumber *sortColIdx;
3750 * We will need at most list_length(groupcls) sort columns; possibly less
3752 numsortkeys = list_length(groupcls);
3753 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3754 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3755 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3756 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3760 foreach(l, groupcls)
3762 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
3763 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
3766 * Check for the possibility of duplicate group-by clauses --- the
3767 * parser should have removed 'em, but no point in sorting
3770 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
3771 exprCollation((Node *) tle->expr),
3774 sortColIdx, sortOperators,
3775 collations, nullsFirst);
3779 Assert(numsortkeys > 0);
3781 return make_sort(root, lefttree, numsortkeys,
3782 sortColIdx, sortOperators, collations,
3787 make_material(Plan *lefttree)
3789 Material *node = makeNode(Material);
3790 Plan *plan = &node->plan;
3792 /* cost should be inserted by caller */
3793 plan->targetlist = lefttree->targetlist;
3795 plan->lefttree = lefttree;
3796 plan->righttree = NULL;
3802 * materialize_finished_plan: stick a Material node atop a completed plan
3804 * There are a couple of places where we want to attach a Material node
3805 * after completion of subquery_planner(). This currently requires hackery.
3806 * Since subquery_planner has already run SS_finalize_plan on the subplan
3807 * tree, we have to kluge up parameter lists for the Material node.
3808 * Possibly this could be fixed by postponing SS_finalize_plan processing
3809 * until setrefs.c is run?
3812 materialize_finished_plan(Plan *subplan)
3815 Path matpath; /* dummy for result of cost_material */
3817 matplan = (Plan *) make_material(subplan);
3820 cost_material(&matpath,
3821 subplan->startup_cost,
3822 subplan->total_cost,
3824 subplan->plan_width);
3825 matplan->startup_cost = matpath.startup_cost;
3826 matplan->total_cost = matpath.total_cost;
3827 matplan->plan_rows = subplan->plan_rows;
3828 matplan->plan_width = subplan->plan_width;
3830 /* parameter kluge --- see comments above */
3831 matplan->extParam = bms_copy(subplan->extParam);
3832 matplan->allParam = bms_copy(subplan->allParam);
3838 make_agg(PlannerInfo *root, List *tlist, List *qual,
3839 AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
3840 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
3844 Agg *node = makeNode(Agg);
3845 Plan *plan = &node->plan;
3846 Path agg_path; /* dummy for result of cost_agg */
3849 node->aggstrategy = aggstrategy;
3850 node->numCols = numGroupCols;
3851 node->grpColIdx = grpColIdx;
3852 node->grpOperators = grpOperators;
3853 node->numGroups = numGroups;
3855 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3856 cost_agg(&agg_path, root,
3857 aggstrategy, aggcosts,
3858 numGroupCols, numGroups,
3859 lefttree->startup_cost,
3860 lefttree->total_cost,
3861 lefttree->plan_rows);
3862 plan->startup_cost = agg_path.startup_cost;
3863 plan->total_cost = agg_path.total_cost;
3866 * We will produce a single output tuple if not grouping, and a tuple per
3869 if (aggstrategy == AGG_PLAIN)
3870 plan->plan_rows = 1;
3872 plan->plan_rows = numGroups;
3875 * We also need to account for the cost of evaluation of the qual (ie, the
3876 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
3877 * anything for Aggref nodes; this is okay since they are really
3878 * comparable to Vars.
3880 * See notes in grouping_planner about why only make_agg, make_windowagg
3881 * and make_group worry about tlist eval cost.
3885 cost_qual_eval(&qual_cost, qual, root);
3886 plan->startup_cost += qual_cost.startup;
3887 plan->total_cost += qual_cost.startup;
3888 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3890 cost_qual_eval(&qual_cost, tlist, root);
3891 plan->startup_cost += qual_cost.startup;
3892 plan->total_cost += qual_cost.startup;
3893 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3896 plan->targetlist = tlist;
3897 plan->lefttree = lefttree;
3898 plan->righttree = NULL;
3904 make_windowagg(PlannerInfo *root, List *tlist,
3905 List *windowFuncs, Index winref,
3906 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
3907 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
3908 int frameOptions, Node *startOffset, Node *endOffset,
3911 WindowAgg *node = makeNode(WindowAgg);
3912 Plan *plan = &node->plan;
3913 Path windowagg_path; /* dummy for result of cost_windowagg */
3916 node->winref = winref;
3917 node->partNumCols = partNumCols;
3918 node->partColIdx = partColIdx;
3919 node->partOperators = partOperators;
3920 node->ordNumCols = ordNumCols;
3921 node->ordColIdx = ordColIdx;
3922 node->ordOperators = ordOperators;
3923 node->frameOptions = frameOptions;
3924 node->startOffset = startOffset;
3925 node->endOffset = endOffset;
3927 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3928 cost_windowagg(&windowagg_path, root,
3929 windowFuncs, partNumCols, ordNumCols,
3930 lefttree->startup_cost,
3931 lefttree->total_cost,
3932 lefttree->plan_rows);
3933 plan->startup_cost = windowagg_path.startup_cost;
3934 plan->total_cost = windowagg_path.total_cost;
3937 * We also need to account for the cost of evaluation of the tlist.
3939 * See notes in grouping_planner about why only make_agg, make_windowagg
3940 * and make_group worry about tlist eval cost.
3942 cost_qual_eval(&qual_cost, tlist, root);
3943 plan->startup_cost += qual_cost.startup;
3944 plan->total_cost += qual_cost.startup;
3945 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3947 plan->targetlist = tlist;
3948 plan->lefttree = lefttree;
3949 plan->righttree = NULL;
3950 /* WindowAgg nodes never have a qual clause */
3957 make_group(PlannerInfo *root,
3961 AttrNumber *grpColIdx,
3966 Group *node = makeNode(Group);
3967 Plan *plan = &node->plan;
3968 Path group_path; /* dummy for result of cost_group */
3971 node->numCols = numGroupCols;
3972 node->grpColIdx = grpColIdx;
3973 node->grpOperators = grpOperators;
3975 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3976 cost_group(&group_path, root,
3977 numGroupCols, numGroups,
3978 lefttree->startup_cost,
3979 lefttree->total_cost,
3980 lefttree->plan_rows);
3981 plan->startup_cost = group_path.startup_cost;
3982 plan->total_cost = group_path.total_cost;
3984 /* One output tuple per estimated result group */
3985 plan->plan_rows = numGroups;
3988 * We also need to account for the cost of evaluation of the qual (ie, the
3989 * HAVING clause) and the tlist.
3991 * XXX this double-counts the cost of evaluation of any expressions used
3992 * for grouping, since in reality those will have been evaluated at a
3993 * lower plan level and will only be copied by the Group node. Worth
3996 * See notes in grouping_planner about why only make_agg, make_windowagg
3997 * and make_group worry about tlist eval cost.
4001 cost_qual_eval(&qual_cost, qual, root);
4002 plan->startup_cost += qual_cost.startup;
4003 plan->total_cost += qual_cost.startup;
4004 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4006 cost_qual_eval(&qual_cost, tlist, root);
4007 plan->startup_cost += qual_cost.startup;
4008 plan->total_cost += qual_cost.startup;
4009 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4012 plan->targetlist = tlist;
4013 plan->lefttree = lefttree;
4014 plan->righttree = NULL;
4020 * distinctList is a list of SortGroupClauses, identifying the targetlist items
4021 * that should be considered by the Unique filter. The input path must
4022 * already be sorted accordingly.
4025 make_unique(Plan *lefttree, List *distinctList)
4027 Unique *node = makeNode(Unique);
4028 Plan *plan = &node->plan;
4029 int numCols = list_length(distinctList);
4031 AttrNumber *uniqColIdx;
4035 copy_plan_costsize(plan, lefttree);
4038 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4039 * all columns get compared at most of the tuples. (XXX probably this is
4042 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
4045 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
4046 * we assume the filter removes nothing. The caller must alter this if he
4047 * has a better idea.
4050 plan->targetlist = lefttree->targetlist;
4052 plan->lefttree = lefttree;
4053 plan->righttree = NULL;
4056 * convert SortGroupClause list into arrays of attr indexes and equality
4057 * operators, as wanted by executor
4059 Assert(numCols > 0);
4060 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4061 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4063 foreach(slitem, distinctList)
4065 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4066 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4068 uniqColIdx[keyno] = tle->resno;
4069 uniqOperators[keyno] = sortcl->eqop;
4070 Assert(OidIsValid(uniqOperators[keyno]));
4074 node->numCols = numCols;
4075 node->uniqColIdx = uniqColIdx;
4076 node->uniqOperators = uniqOperators;
4082 * distinctList is a list of SortGroupClauses, identifying the targetlist
4083 * items that should be considered by the SetOp filter. The input path must
4084 * already be sorted accordingly.
4087 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
4088 List *distinctList, AttrNumber flagColIdx, int firstFlag,
4089 long numGroups, double outputRows)
4091 SetOp *node = makeNode(SetOp);
4092 Plan *plan = &node->plan;
4093 int numCols = list_length(distinctList);
4095 AttrNumber *dupColIdx;
4099 copy_plan_costsize(plan, lefttree);
4100 plan->plan_rows = outputRows;
4103 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4104 * all columns get compared at most of the tuples.
4106 plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
4108 plan->targetlist = lefttree->targetlist;
4110 plan->lefttree = lefttree;
4111 plan->righttree = NULL;
4114 * convert SortGroupClause list into arrays of attr indexes and equality
4115 * operators, as wanted by executor
4117 Assert(numCols > 0);
4118 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4119 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4121 foreach(slitem, distinctList)
4123 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4124 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4126 dupColIdx[keyno] = tle->resno;
4127 dupOperators[keyno] = sortcl->eqop;
4128 Assert(OidIsValid(dupOperators[keyno]));
4133 node->strategy = strategy;
4134 node->numCols = numCols;
4135 node->dupColIdx = dupColIdx;
4136 node->dupOperators = dupOperators;
4137 node->flagColIdx = flagColIdx;
4138 node->firstFlag = firstFlag;
4139 node->numGroups = numGroups;
4146 * Build a LockRows plan node
4149 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
4151 LockRows *node = makeNode(LockRows);
4152 Plan *plan = &node->plan;
4154 copy_plan_costsize(plan, lefttree);
4156 /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
4157 plan->total_cost += cpu_tuple_cost * plan->plan_rows;
4159 plan->targetlist = lefttree->targetlist;
4161 plan->lefttree = lefttree;
4162 plan->righttree = NULL;
4164 node->rowMarks = rowMarks;
4165 node->epqParam = epqParam;
4171 * Note: offset_est and count_est are passed in to save having to repeat
4172 * work already done to estimate the values of the limitOffset and limitCount
4173 * expressions. Their values are as returned by preprocess_limit (0 means
4174 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
4175 * with that function!
4178 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
4179 int64 offset_est, int64 count_est)
4181 Limit *node = makeNode(Limit);
4182 Plan *plan = &node->plan;
4184 copy_plan_costsize(plan, lefttree);
4187 * Adjust the output rows count and costs according to the offset/limit.
4188 * This is only a cosmetic issue if we are at top level, but if we are
4189 * building a subquery then it's important to report correct info to the
4192 * When the offset or count couldn't be estimated, use 10% of the
4193 * estimated number of rows emitted from the subplan.
4195 if (offset_est != 0)
4200 offset_rows = (double) offset_est;
4202 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4203 if (offset_rows > plan->plan_rows)
4204 offset_rows = plan->plan_rows;
4205 if (plan->plan_rows > 0)
4206 plan->startup_cost +=
4207 (plan->total_cost - plan->startup_cost)
4208 * offset_rows / plan->plan_rows;
4209 plan->plan_rows -= offset_rows;
4210 if (plan->plan_rows < 1)
4211 plan->plan_rows = 1;
4219 count_rows = (double) count_est;
4221 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4222 if (count_rows > plan->plan_rows)
4223 count_rows = plan->plan_rows;
4224 if (plan->plan_rows > 0)
4225 plan->total_cost = plan->startup_cost +
4226 (plan->total_cost - plan->startup_cost)
4227 * count_rows / plan->plan_rows;
4228 plan->plan_rows = count_rows;
4229 if (plan->plan_rows < 1)
4230 plan->plan_rows = 1;
4233 plan->targetlist = lefttree->targetlist;
4235 plan->lefttree = lefttree;
4236 plan->righttree = NULL;
4238 node->limitOffset = limitOffset;
4239 node->limitCount = limitCount;
4246 * Build a Result plan node
4248 * If we have a subplan, assume that any evaluation costs for the gating qual
4249 * were already factored into the subplan's startup cost, and just copy the
4250 * subplan cost. If there's no subplan, we should include the qual eval
4251 * cost. In either case, tlist eval cost is not to be included here.
4254 make_result(PlannerInfo *root,
4256 Node *resconstantqual,
4259 Result *node = makeNode(Result);
4260 Plan *plan = &node->plan;
4263 copy_plan_costsize(plan, subplan);
4266 plan->startup_cost = 0;
4267 plan->total_cost = cpu_tuple_cost;
4268 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
4269 plan->plan_width = 0; /* XXX is it worth being smarter? */
4270 if (resconstantqual)
4274 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
4275 /* resconstantqual is evaluated once at startup */
4276 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
4277 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
4281 plan->targetlist = tlist;
4283 plan->lefttree = subplan;
4284 plan->righttree = NULL;
4285 node->resconstantqual = resconstantqual;
4292 * Build a ModifyTable plan node
4294 * Currently, we don't charge anything extra for the actual table modification
4295 * work, nor for the RETURNING expressions if any. It would only be window
4296 * dressing, since these are always top-level nodes and there is no way for
4297 * the costs to change any higher-level planning choices. But we might want
4298 * to make it look better sometime.
4301 make_modifytable(CmdType operation, bool canSetTag,
4302 List *resultRelations,
4303 List *subplans, List *returningLists,
4304 List *rowMarks, int epqParam)
4306 ModifyTable *node = makeNode(ModifyTable);
4307 Plan *plan = &node->plan;
4311 Assert(list_length(resultRelations) == list_length(subplans));
4312 Assert(returningLists == NIL ||
4313 list_length(resultRelations) == list_length(returningLists));
4316 * Compute cost as sum of subplan costs.
4318 plan->startup_cost = 0;
4319 plan->total_cost = 0;
4320 plan->plan_rows = 0;
4322 foreach(subnode, subplans)
4324 Plan *subplan = (Plan *) lfirst(subnode);
4326 if (subnode == list_head(subplans)) /* first node? */
4327 plan->startup_cost = subplan->startup_cost;
4328 plan->total_cost += subplan->total_cost;
4329 plan->plan_rows += subplan->plan_rows;
4330 total_size += subplan->plan_width * subplan->plan_rows;
4332 if (plan->plan_rows > 0)
4333 plan->plan_width = rint(total_size / plan->plan_rows);
4335 plan->plan_width = 0;
4337 node->plan.lefttree = NULL;
4338 node->plan.righttree = NULL;
4339 node->plan.qual = NIL;
4342 * Set up the visible plan targetlist as being the same as the first
4343 * RETURNING list. This is for the use of EXPLAIN; the executor won't pay
4344 * any attention to the targetlist.
4347 node->plan.targetlist = copyObject(linitial(returningLists));
4349 node->plan.targetlist = NIL;
4351 node->operation = operation;
4352 node->canSetTag = canSetTag;
4353 node->resultRelations = resultRelations;
4354 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
4355 node->plans = subplans;
4356 node->returningLists = returningLists;
4357 node->rowMarks = rowMarks;
4358 node->epqParam = epqParam;
4364 * is_projection_capable_plan
4365 * Check whether a given Plan node is able to do projection.
4368 is_projection_capable_plan(Plan *plan)
4370 /* Most plan types can project, so just list the ones that can't */
4371 switch (nodeTag(plan))
4383 case T_RecursiveUnion: