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-2009, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.265 2009/10/12 18:10:45 tgl Exp $
15 *-------------------------------------------------------------------------
22 #include "access/skey.h"
23 #include "nodes/makefuncs.h"
24 #include "nodes/nodeFuncs.h"
25 #include "optimizer/clauses.h"
26 #include "optimizer/cost.h"
27 #include "optimizer/plancat.h"
28 #include "optimizer/planmain.h"
29 #include "optimizer/predtest.h"
30 #include "optimizer/restrictinfo.h"
31 #include "optimizer/tlist.h"
32 #include "optimizer/var.h"
33 #include "parser/parse_clause.h"
34 #include "parser/parsetree.h"
35 #include "utils/lsyscache.h"
38 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
39 static List *build_relation_tlist(RelOptInfo *rel);
40 static bool use_physical_tlist(PlannerInfo *root, RelOptInfo *rel);
41 static void disuse_physical_tlist(Plan *plan, Path *path);
42 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
43 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
44 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
45 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
46 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
47 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
48 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
49 List *tlist, List *scan_clauses);
50 static IndexScan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
51 List *tlist, List *scan_clauses);
52 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
53 BitmapHeapPath *best_path,
54 List *tlist, List *scan_clauses);
55 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
56 List **qual, List **indexqual);
57 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
58 List *tlist, List *scan_clauses);
59 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
60 List *tlist, List *scan_clauses);
61 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
62 List *tlist, List *scan_clauses);
63 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
64 List *tlist, List *scan_clauses);
65 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
66 List *tlist, List *scan_clauses);
67 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
68 List *tlist, List *scan_clauses);
69 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
70 Plan *outer_plan, Plan *inner_plan);
71 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
72 Plan *outer_plan, Plan *inner_plan);
73 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
74 Plan *outer_plan, Plan *inner_plan);
75 static List *fix_indexqual_references(List *indexquals, IndexPath *index_path);
76 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index);
77 static List *get_switched_clauses(List *clauses, Relids outerrelids);
78 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
79 static void copy_path_costsize(Plan *dest, Path *src);
80 static void copy_plan_costsize(Plan *dest, Plan *src);
81 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
82 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
83 Oid indexid, List *indexqual, List *indexqualorig,
84 ScanDirection indexscandir);
85 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
88 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
93 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
95 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
96 Index scanrelid, Node *funcexpr, List *funccolnames,
97 List *funccoltypes, List *funccoltypmods);
98 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
99 Index scanrelid, List *values_lists);
100 static CteScan *make_ctescan(List *qptlist, List *qpqual,
101 Index scanrelid, int ctePlanId, int cteParam);
102 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
103 Index scanrelid, int wtParam);
104 static BitmapAnd *make_bitmap_and(List *bitmapplans);
105 static BitmapOr *make_bitmap_or(List *bitmapplans);
106 static NestLoop *make_nestloop(List *tlist,
107 List *joinclauses, List *otherclauses,
108 Plan *lefttree, Plan *righttree,
110 static HashJoin *make_hashjoin(List *tlist,
111 List *joinclauses, List *otherclauses,
113 Plan *lefttree, Plan *righttree,
115 static Hash *make_hash(Plan *lefttree,
117 AttrNumber skewColumn,
119 int32 skewColTypmod);
120 static MergeJoin *make_mergejoin(List *tlist,
121 List *joinclauses, List *otherclauses,
124 int *mergestrategies,
125 bool *mergenullsfirst,
126 Plan *lefttree, Plan *righttree,
128 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
129 AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
130 double limit_tuples);
131 static Material *make_material(Plan *lefttree);
136 * Creates the access plan for a query by tracing backwards through the
137 * desired chain of pathnodes, starting at the node 'best_path'. For
138 * every pathnode found, we create a corresponding plan node containing
139 * appropriate id, target list, and qualification information.
141 * The tlists and quals in the plan tree are still in planner format,
142 * ie, Vars still correspond to the parser's numbering. This will be
143 * fixed later by setrefs.c.
145 * best_path is the best access path
147 * Returns a Plan tree.
150 create_plan(PlannerInfo *root, Path *best_path)
154 switch (best_path->pathtype)
158 case T_BitmapHeapScan:
164 case T_WorkTableScan:
165 plan = create_scan_plan(root, best_path);
168 /* this is only used for no-op joins */
169 Assert(IsA(best_path, NoOpPath));
170 plan = create_plan(root, ((NoOpPath *) best_path)->subpath);
175 plan = create_join_plan(root,
176 (JoinPath *) best_path);
179 plan = create_append_plan(root,
180 (AppendPath *) best_path);
183 plan = (Plan *) create_result_plan(root,
184 (ResultPath *) best_path);
187 plan = (Plan *) create_material_plan(root,
188 (MaterialPath *) best_path);
191 plan = create_unique_plan(root,
192 (UniquePath *) best_path);
195 elog(ERROR, "unrecognized node type: %d",
196 (int) best_path->pathtype);
197 plan = NULL; /* keep compiler quiet */
206 * Create a scan plan for the parent relation of 'best_path'.
209 create_scan_plan(PlannerInfo *root, Path *best_path)
211 RelOptInfo *rel = best_path->parent;
217 * For table scans, rather than using the relation targetlist (which is
218 * only those Vars actually needed by the query), we prefer to generate a
219 * tlist containing all Vars in order. This will allow the executor to
220 * optimize away projection of the table tuples, if possible. (Note that
221 * planner.c may replace the tlist we generate here, forcing projection to
224 if (use_physical_tlist(root, rel))
226 tlist = build_physical_tlist(root, rel);
227 /* if fail because of dropped cols, use regular method */
229 tlist = build_relation_tlist(rel);
232 tlist = build_relation_tlist(rel);
235 * Extract the relevant restriction clauses from the parent relation. The
236 * executor must apply all these restrictions during the scan, except for
237 * pseudoconstants which we'll take care of below.
239 scan_clauses = rel->baserestrictinfo;
241 switch (best_path->pathtype)
244 plan = (Plan *) create_seqscan_plan(root,
251 plan = (Plan *) create_indexscan_plan(root,
252 (IndexPath *) best_path,
257 case T_BitmapHeapScan:
258 plan = (Plan *) create_bitmap_scan_plan(root,
259 (BitmapHeapPath *) best_path,
265 plan = (Plan *) create_tidscan_plan(root,
266 (TidPath *) best_path,
272 plan = (Plan *) create_subqueryscan_plan(root,
279 plan = (Plan *) create_functionscan_plan(root,
286 plan = (Plan *) create_valuesscan_plan(root,
293 plan = (Plan *) create_ctescan_plan(root,
299 case T_WorkTableScan:
300 plan = (Plan *) create_worktablescan_plan(root,
307 elog(ERROR, "unrecognized node type: %d",
308 (int) best_path->pathtype);
309 plan = NULL; /* keep compiler quiet */
314 * If there are any pseudoconstant clauses attached to this node, insert a
315 * gating Result node that evaluates the pseudoconstants as one-time
318 if (root->hasPseudoConstantQuals)
319 plan = create_gating_plan(root, plan, scan_clauses);
325 * Build a target list (ie, a list of TargetEntry) for a relation.
328 build_relation_tlist(RelOptInfo *rel)
334 foreach(v, rel->reltargetlist)
336 /* Do we really need to copy here? Not sure */
337 Node *node = (Node *) copyObject(lfirst(v));
339 tlist = lappend(tlist, makeTargetEntry((Expr *) node,
350 * Decide whether to use a tlist matching relation structure,
351 * rather than only those Vars actually referenced.
354 use_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
360 * We can do this for real relation scans, subquery scans, function scans,
361 * values scans, and CTE scans (but not for, eg, joins).
363 if (rel->rtekind != RTE_RELATION &&
364 rel->rtekind != RTE_SUBQUERY &&
365 rel->rtekind != RTE_FUNCTION &&
366 rel->rtekind != RTE_VALUES &&
367 rel->rtekind != RTE_CTE)
371 * Can't do it with inheritance cases either (mainly because Append
374 if (rel->reloptkind != RELOPT_BASEREL)
378 * Can't do it if any system columns or whole-row Vars are requested.
379 * (This could possibly be fixed but would take some fragile assumptions
380 * in setrefs.c, I think.)
382 for (i = rel->min_attr; i <= 0; i++)
384 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
389 * Can't do it if the rel is required to emit any placeholder expressions,
392 foreach(lc, root->placeholder_list)
394 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
396 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
397 bms_is_subset(phinfo->ph_eval_at, rel->relids))
405 * disuse_physical_tlist
406 * Switch a plan node back to emitting only Vars actually referenced.
408 * If the plan node immediately above a scan would prefer to get only
409 * needed Vars and not a physical tlist, it must call this routine to
410 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
411 * and Material nodes want this, so they don't have to store useless columns.
414 disuse_physical_tlist(Plan *plan, Path *path)
416 /* Only need to undo it for path types handled by create_scan_plan() */
417 switch (path->pathtype)
421 case T_BitmapHeapScan:
427 case T_WorkTableScan:
428 plan->targetlist = build_relation_tlist(path->parent);
437 * Deal with pseudoconstant qual clauses
439 * If the node's quals list includes any pseudoconstant quals, put them
440 * into a gating Result node atop the already-built plan. Otherwise,
441 * return the plan as-is.
443 * Note that we don't change cost or size estimates when doing gating.
444 * The costs of qual eval were already folded into the plan's startup cost.
445 * Leaving the size alone amounts to assuming that the gating qual will
446 * succeed, which is the conservative estimate for planning upper queries.
447 * We certainly don't want to assume the output size is zero (unless the
448 * gating qual is actually constant FALSE, and that case is dealt with in
449 * clausesel.c). Interpolating between the two cases is silly, because
450 * it doesn't reflect what will really happen at runtime, and besides which
451 * in most cases we have only a very bad idea of the probability of the gating
455 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
457 List *pseudoconstants;
459 /* Sort into desirable execution order while still in RestrictInfo form */
460 quals = order_qual_clauses(root, quals);
462 /* Pull out any pseudoconstant quals from the RestrictInfo list */
463 pseudoconstants = extract_actual_clauses(quals, true);
465 if (!pseudoconstants)
468 return (Plan *) make_result(root,
470 (Node *) pseudoconstants,
476 * Create a join plan for 'best_path' and (recursively) plans for its
477 * inner and outer paths.
480 create_join_plan(PlannerInfo *root, JoinPath *best_path)
486 outer_plan = create_plan(root, best_path->outerjoinpath);
487 inner_plan = create_plan(root, best_path->innerjoinpath);
489 switch (best_path->path.pathtype)
492 plan = (Plan *) create_mergejoin_plan(root,
493 (MergePath *) best_path,
498 plan = (Plan *) create_hashjoin_plan(root,
499 (HashPath *) best_path,
504 plan = (Plan *) create_nestloop_plan(root,
505 (NestPath *) best_path,
510 elog(ERROR, "unrecognized node type: %d",
511 (int) best_path->path.pathtype);
512 plan = NULL; /* keep compiler quiet */
517 * If there are any pseudoconstant clauses attached to this node, insert a
518 * gating Result node that evaluates the pseudoconstants as one-time
521 if (root->hasPseudoConstantQuals)
522 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
527 * * Expensive function pullups may have pulled local predicates * into
528 * this path node. Put them in the qpqual of the plan node. * JMH,
531 if (get_loc_restrictinfo(best_path) != NIL)
532 set_qpqual((Plan) plan,
533 list_concat(get_qpqual((Plan) plan),
534 get_actual_clauses(get_loc_restrictinfo(best_path))));
542 * Create an Append plan for 'best_path' and (recursively) plans
545 * Returns a Plan node.
548 create_append_plan(PlannerInfo *root, AppendPath *best_path)
551 List *tlist = build_relation_tlist(best_path->path.parent);
552 List *subplans = NIL;
556 * It is possible for the subplans list to contain only one entry, or even
557 * no entries. Handle these cases specially.
559 * XXX ideally, if there's just one entry, we'd not bother to generate an
560 * Append node but just return the single child. At the moment this does
561 * not work because the varno of the child scan plan won't match the
562 * parent-rel Vars it'll be asked to emit.
564 if (best_path->subpaths == NIL)
566 /* Generate a Result plan with constant-FALSE gating qual */
567 return (Plan *) make_result(root,
569 (Node *) list_make1(makeBoolConst(false,
574 /* Normal case with multiple subpaths */
575 foreach(subpaths, best_path->subpaths)
577 Path *subpath = (Path *) lfirst(subpaths);
579 subplans = lappend(subplans, create_plan(root, subpath));
582 plan = make_append(subplans, tlist);
584 return (Plan *) plan;
589 * Create a Result plan for 'best_path'.
590 * This is only used for the case of a query with an empty jointree.
592 * Returns a Plan node.
595 create_result_plan(PlannerInfo *root, ResultPath *best_path)
600 /* The tlist will be installed later, since we have no RelOptInfo */
601 Assert(best_path->path.parent == NULL);
604 /* best_path->quals is just bare clauses */
606 quals = order_qual_clauses(root, best_path->quals);
608 return make_result(root, tlist, (Node *) quals, NULL);
612 * create_material_plan
613 * Create a Material plan for 'best_path' and (recursively) plans
616 * Returns a Plan node.
619 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
624 subplan = create_plan(root, best_path->subpath);
626 /* We don't want any excess columns in the materialized tuples */
627 disuse_physical_tlist(subplan, best_path->subpath);
629 plan = make_material(subplan);
631 copy_path_costsize(&plan->plan, (Path *) best_path);
638 * Create a Unique plan for 'best_path' and (recursively) plans
641 * Returns a Plan node.
644 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
654 AttrNumber *groupColIdx;
658 subplan = create_plan(root, best_path->subpath);
660 /* Done if we don't need to do any actual unique-ifying */
661 if (best_path->umethod == UNIQUE_PATH_NOOP)
665 * As constructed, the subplan has a "flat" tlist containing just the Vars
666 * needed here and at upper levels. The values we are supposed to
667 * unique-ify may be expressions in these variables. We have to add any
668 * such expressions to the subplan's tlist.
670 * The subplan may have a "physical" tlist if it is a simple scan plan. If
671 * we're going to sort, this should be reduced to the regular tlist, so
672 * that we don't sort more data than we need to. For hashing, the tlist
673 * should be left as-is if we don't need to add any expressions; but if we
674 * do have to add expressions, then a projection step will be needed at
675 * runtime anyway, so we may as well remove unneeded items. Therefore
676 * newtlist starts from build_relation_tlist() not just a copy of the
677 * subplan's tlist; and we don't install it into the subplan unless we are
678 * sorting or stuff has to be added.
680 in_operators = best_path->in_operators;
681 uniq_exprs = best_path->uniq_exprs;
683 /* initialize modified subplan tlist as just the "required" vars */
684 newtlist = build_relation_tlist(best_path->path.parent);
685 nextresno = list_length(newtlist) + 1;
688 foreach(l, uniq_exprs)
690 Node *uniqexpr = lfirst(l);
693 tle = tlist_member(uniqexpr, newtlist);
696 tle = makeTargetEntry((Expr *) uniqexpr,
700 newtlist = lappend(newtlist, tle);
706 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
709 * If the top plan node can't do projections, we need to add a Result
710 * node to help it along.
712 if (!is_projection_capable_plan(subplan))
713 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
715 subplan->targetlist = newtlist;
719 * Build control information showing which subplan output columns are to
720 * be examined by the grouping step. Unfortunately we can't merge this
721 * with the previous loop, since we didn't then know which version of the
722 * subplan tlist we'd end up using.
724 newtlist = subplan->targetlist;
725 numGroupCols = list_length(uniq_exprs);
726 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
729 foreach(l, uniq_exprs)
731 Node *uniqexpr = lfirst(l);
734 tle = tlist_member(uniqexpr, newtlist);
735 if (!tle) /* shouldn't happen */
736 elog(ERROR, "failed to find unique expression in subplan tlist");
737 groupColIdx[groupColPos++] = tle->resno;
740 if (best_path->umethod == UNIQUE_PATH_HASH)
745 numGroups = (long) Min(best_path->rows, (double) LONG_MAX);
748 * Get the hashable equality operators for the Agg node to use.
749 * Normally these are the same as the IN clause operators, but if
750 * those are cross-type operators then the equality operators are the
751 * ones for the IN clause operators' RHS datatype.
753 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
755 foreach(l, in_operators)
757 Oid in_oper = lfirst_oid(l);
760 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
761 elog(ERROR, "could not find compatible hash operator for operator %u",
763 groupOperators[groupColPos++] = eq_oper;
767 * Since the Agg node is going to project anyway, we can give it the
768 * minimum output tlist, without any stuff we might have added to the
771 plan = (Plan *) make_agg(root,
772 build_relation_tlist(best_path->path.parent),
784 List *sortList = NIL;
786 /* Create an ORDER BY list to sort the input compatibly */
788 foreach(l, in_operators)
790 Oid in_oper = lfirst_oid(l);
793 SortGroupClause *sortcl;
795 sortop = get_ordering_op_for_equality_op(in_oper, false);
796 if (!OidIsValid(sortop)) /* shouldn't happen */
797 elog(ERROR, "could not find ordering operator for equality operator %u",
799 tle = get_tle_by_resno(subplan->targetlist,
800 groupColIdx[groupColPos]);
802 sortcl = makeNode(SortGroupClause);
803 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
804 subplan->targetlist);
805 sortcl->eqop = in_oper;
806 sortcl->sortop = sortop;
807 sortcl->nulls_first = false;
808 sortList = lappend(sortList, sortcl);
811 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
812 plan = (Plan *) make_unique(plan, sortList);
815 /* Adjust output size estimate (other fields should be OK already) */
816 plan->plan_rows = best_path->rows;
822 /*****************************************************************************
824 * BASE-RELATION SCAN METHODS
826 *****************************************************************************/
830 * create_seqscan_plan
831 * Returns a seqscan plan for the base relation scanned by 'best_path'
832 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
835 create_seqscan_plan(PlannerInfo *root, Path *best_path,
836 List *tlist, List *scan_clauses)
839 Index scan_relid = best_path->parent->relid;
841 /* it should be a base rel... */
842 Assert(scan_relid > 0);
843 Assert(best_path->parent->rtekind == RTE_RELATION);
845 /* Sort clauses into best execution order */
846 scan_clauses = order_qual_clauses(root, scan_clauses);
848 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
849 scan_clauses = extract_actual_clauses(scan_clauses, false);
851 scan_plan = make_seqscan(tlist,
855 copy_path_costsize(&scan_plan->plan, best_path);
861 * create_indexscan_plan
862 * Returns an indexscan plan for the base relation scanned by 'best_path'
863 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
865 * The indexquals list of the path contains implicitly-ANDed qual conditions.
866 * The list can be empty --- then no index restrictions will be applied during
870 create_indexscan_plan(PlannerInfo *root,
871 IndexPath *best_path,
875 List *indexquals = best_path->indexquals;
876 Index baserelid = best_path->path.parent->relid;
877 Oid indexoid = best_path->indexinfo->indexoid;
879 List *stripped_indexquals;
880 List *fixed_indexquals;
882 IndexScan *scan_plan;
884 /* it should be a base rel... */
885 Assert(baserelid > 0);
886 Assert(best_path->path.parent->rtekind == RTE_RELATION);
889 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
890 * executor as indexqualorig
892 stripped_indexquals = get_actual_clauses(indexquals);
895 * The executor needs a copy with the indexkey on the left of each clause
896 * and with index attr numbers substituted for table ones.
898 fixed_indexquals = fix_indexqual_references(indexquals, best_path);
901 * If this is an innerjoin scan, the indexclauses will contain join
902 * clauses that are not present in scan_clauses (since the passed-in value
903 * is just the rel's baserestrictinfo list). We must add these clauses to
904 * scan_clauses to ensure they get checked. In most cases we will remove
905 * the join clauses again below, but if a join clause contains a special
906 * operator, we need to make sure it gets into the scan_clauses.
908 * Note: pointer comparison should be enough to determine RestrictInfo
911 if (best_path->isjoininner)
912 scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
915 * The qpqual list must contain all restrictions not automatically handled
916 * by the index. All the predicates in the indexquals will be checked
917 * (either by the index itself, or by nodeIndexscan.c), but if there are
918 * any "special" operators involved then they must be included in qpqual.
919 * The upshot is that qpqual must contain scan_clauses minus whatever
920 * appears in indexquals.
922 * In normal cases simple pointer equality checks will be enough to spot
923 * duplicate RestrictInfos, so we try that first. In some situations
924 * (particularly with OR'd index conditions) we may have scan_clauses that
925 * are not equal to, but are logically implied by, the index quals; so we
926 * also try a predicate_implied_by() check to see if we can discard quals
927 * that way. (predicate_implied_by assumes its first input contains only
928 * immutable functions, so we have to check that.)
930 * We can also discard quals that are implied by a partial index's
931 * predicate, but only in a plain SELECT; when scanning a target relation
932 * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
933 * plan so that they'll be properly rechecked by EvalPlanQual testing.
936 foreach(l, scan_clauses)
938 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
940 Assert(IsA(rinfo, RestrictInfo));
941 if (rinfo->pseudoconstant)
942 continue; /* we may drop pseudoconstants here */
943 if (list_member_ptr(indexquals, rinfo))
945 if (!contain_mutable_functions((Node *) rinfo->clause))
947 List *clausel = list_make1(rinfo->clause);
949 if (predicate_implied_by(clausel, indexquals))
951 if (best_path->indexinfo->indpred)
953 if (baserelid != root->parse->resultRelation &&
954 get_rowmark(root->parse, baserelid) == NULL)
955 if (predicate_implied_by(clausel,
956 best_path->indexinfo->indpred))
960 qpqual = lappend(qpqual, rinfo);
963 /* Sort clauses into best execution order */
964 qpqual = order_qual_clauses(root, qpqual);
966 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
967 qpqual = extract_actual_clauses(qpqual, false);
969 /* Finally ready to build the plan node */
970 scan_plan = make_indexscan(tlist,
976 best_path->indexscandir);
978 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
979 /* use the indexscan-specific rows estimate, not the parent rel's */
980 scan_plan->scan.plan.plan_rows = best_path->rows;
986 * create_bitmap_scan_plan
987 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
988 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
990 static BitmapHeapScan *
991 create_bitmap_scan_plan(PlannerInfo *root,
992 BitmapHeapPath *best_path,
996 Index baserelid = best_path->path.parent->relid;
997 Plan *bitmapqualplan;
998 List *bitmapqualorig;
1002 BitmapHeapScan *scan_plan;
1004 /* it should be a base rel... */
1005 Assert(baserelid > 0);
1006 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1008 /* Process the bitmapqual tree into a Plan tree and qual lists */
1009 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1010 &bitmapqualorig, &indexquals);
1012 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1013 scan_clauses = extract_actual_clauses(scan_clauses, false);
1016 * If this is a innerjoin scan, the indexclauses will contain join clauses
1017 * that are not present in scan_clauses (since the passed-in value is just
1018 * the rel's baserestrictinfo list). We must add these clauses to
1019 * scan_clauses to ensure they get checked. In most cases we will remove
1020 * the join clauses again below, but if a join clause contains a special
1021 * operator, we need to make sure it gets into the scan_clauses.
1023 if (best_path->isjoininner)
1025 scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
1029 * The qpqual list must contain all restrictions not automatically handled
1030 * by the index. All the predicates in the indexquals will be checked
1031 * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
1032 * are any "special" operators involved then they must be added to qpqual.
1033 * The upshot is that qpqual must contain scan_clauses minus whatever
1034 * appears in indexquals.
1036 * In normal cases simple equal() checks will be enough to spot duplicate
1037 * clauses, so we try that first. In some situations (particularly with
1038 * OR'd index conditions) we may have scan_clauses that are not equal to,
1039 * but are logically implied by, the index quals; so we also try a
1040 * predicate_implied_by() check to see if we can discard quals that way.
1041 * (predicate_implied_by assumes its first input contains only immutable
1042 * functions, so we have to check that.)
1044 * Unlike create_indexscan_plan(), we need take no special thought here
1045 * for partial index predicates; this is because the predicate conditions
1046 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1047 * to do it that way because predicate conditions need to be rechecked if
1048 * the scan becomes lossy.
1051 foreach(l, scan_clauses)
1053 Node *clause = (Node *) lfirst(l);
1055 if (list_member(indexquals, clause))
1057 if (!contain_mutable_functions(clause))
1059 List *clausel = list_make1(clause);
1061 if (predicate_implied_by(clausel, indexquals))
1064 qpqual = lappend(qpqual, clause);
1067 /* Sort clauses into best execution order */
1068 qpqual = order_qual_clauses(root, qpqual);
1071 * When dealing with special operators, we will at this point have
1072 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
1073 * 'em from bitmapqualorig, since there's no point in making the tests
1076 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1078 /* Finally ready to build the plan node */
1079 scan_plan = make_bitmap_heapscan(tlist,
1085 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1086 /* use the indexscan-specific rows estimate, not the parent rel's */
1087 scan_plan->scan.plan.plan_rows = best_path->rows;
1093 * Given a bitmapqual tree, generate the Plan tree that implements it
1095 * As byproducts, we also return in *qual and *indexqual the qual lists
1096 * (in implicit-AND form, without RestrictInfos) describing the original index
1097 * conditions and the generated indexqual conditions. (These are the same in
1098 * simple cases, but when special index operators are involved, the former
1099 * list includes the special conditions while the latter includes the actual
1100 * indexable conditions derived from them.) Both lists include partial-index
1101 * predicates, because we have to recheck predicates as well as index
1102 * conditions if the bitmap scan becomes lossy.
1104 * Note: if you find yourself changing this, you probably need to change
1105 * make_restrictinfo_from_bitmapqual too.
1108 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1109 List **qual, List **indexqual)
1113 if (IsA(bitmapqual, BitmapAndPath))
1115 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1116 List *subplans = NIL;
1117 List *subquals = NIL;
1118 List *subindexquals = NIL;
1122 * There may well be redundant quals among the subplans, since a
1123 * top-level WHERE qual might have gotten used to form several
1124 * different index quals. We don't try exceedingly hard to eliminate
1125 * redundancies, but we do eliminate obvious duplicates by using
1126 * list_concat_unique.
1128 foreach(l, apath->bitmapquals)
1134 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1135 &subqual, &subindexqual);
1136 subplans = lappend(subplans, subplan);
1137 subquals = list_concat_unique(subquals, subqual);
1138 subindexquals = list_concat_unique(subindexquals, subindexqual);
1140 plan = (Plan *) make_bitmap_and(subplans);
1141 plan->startup_cost = apath->path.startup_cost;
1142 plan->total_cost = apath->path.total_cost;
1144 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1145 plan->plan_width = 0; /* meaningless */
1147 *indexqual = subindexquals;
1149 else if (IsA(bitmapqual, BitmapOrPath))
1151 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1152 List *subplans = NIL;
1153 List *subquals = NIL;
1154 List *subindexquals = NIL;
1155 bool const_true_subqual = false;
1156 bool const_true_subindexqual = false;
1160 * Here, we only detect qual-free subplans. A qual-free subplan would
1161 * cause us to generate "... OR true ..." which we may as well reduce
1162 * to just "true". We do not try to eliminate redundant subclauses
1163 * because (a) it's not as likely as in the AND case, and (b) we might
1164 * well be working with hundreds or even thousands of OR conditions,
1165 * perhaps from a long IN list. The performance of list_append_unique
1166 * would be unacceptable.
1168 foreach(l, opath->bitmapquals)
1174 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1175 &subqual, &subindexqual);
1176 subplans = lappend(subplans, subplan);
1178 const_true_subqual = true;
1179 else if (!const_true_subqual)
1180 subquals = lappend(subquals,
1181 make_ands_explicit(subqual));
1182 if (subindexqual == NIL)
1183 const_true_subindexqual = true;
1184 else if (!const_true_subindexqual)
1185 subindexquals = lappend(subindexquals,
1186 make_ands_explicit(subindexqual));
1190 * In the presence of ScalarArrayOpExpr quals, we might have built
1191 * BitmapOrPaths with just one subpath; don't add an OR step.
1193 if (list_length(subplans) == 1)
1195 plan = (Plan *) linitial(subplans);
1199 plan = (Plan *) make_bitmap_or(subplans);
1200 plan->startup_cost = opath->path.startup_cost;
1201 plan->total_cost = opath->path.total_cost;
1203 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1204 plan->plan_width = 0; /* meaningless */
1208 * If there were constant-TRUE subquals, the OR reduces to constant
1209 * TRUE. Also, avoid generating one-element ORs, which could happen
1210 * due to redundancy elimination or ScalarArrayOpExpr quals.
1212 if (const_true_subqual)
1214 else if (list_length(subquals) <= 1)
1217 *qual = list_make1(make_orclause(subquals));
1218 if (const_true_subindexqual)
1220 else if (list_length(subindexquals) <= 1)
1221 *indexqual = subindexquals;
1223 *indexqual = list_make1(make_orclause(subindexquals));
1225 else if (IsA(bitmapqual, IndexPath))
1227 IndexPath *ipath = (IndexPath *) bitmapqual;
1231 /* Use the regular indexscan plan build machinery... */
1232 iscan = create_indexscan_plan(root, ipath, NIL, NIL);
1233 /* then convert to a bitmap indexscan */
1234 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1237 iscan->indexqualorig);
1238 plan->startup_cost = 0.0;
1239 plan->total_cost = ipath->indextotalcost;
1241 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1242 plan->plan_width = 0; /* meaningless */
1243 *qual = get_actual_clauses(ipath->indexclauses);
1244 *indexqual = get_actual_clauses(ipath->indexquals);
1245 foreach(l, ipath->indexinfo->indpred)
1247 Expr *pred = (Expr *) lfirst(l);
1250 * We know that the index predicate must have been implied by the
1251 * query condition as a whole, but it may or may not be implied by
1252 * the conditions that got pushed into the bitmapqual. Avoid
1253 * generating redundant conditions.
1255 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1257 *qual = lappend(*qual, pred);
1258 *indexqual = lappend(*indexqual, pred);
1264 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1265 plan = NULL; /* keep compiler quiet */
1272 * create_tidscan_plan
1273 * Returns a tidscan plan for the base relation scanned by 'best_path'
1274 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1277 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1278 List *tlist, List *scan_clauses)
1281 Index scan_relid = best_path->path.parent->relid;
1284 /* it should be a base rel... */
1285 Assert(scan_relid > 0);
1286 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1288 /* Sort clauses into best execution order */
1289 scan_clauses = order_qual_clauses(root, scan_clauses);
1291 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1292 scan_clauses = extract_actual_clauses(scan_clauses, false);
1295 * Remove any clauses that are TID quals. This is a bit tricky since the
1296 * tidquals list has implicit OR semantics.
1298 ortidquals = best_path->tidquals;
1299 if (list_length(ortidquals) > 1)
1300 ortidquals = list_make1(make_orclause(ortidquals));
1301 scan_clauses = list_difference(scan_clauses, ortidquals);
1303 scan_plan = make_tidscan(tlist,
1306 best_path->tidquals);
1308 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1314 * create_subqueryscan_plan
1315 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1316 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1318 static SubqueryScan *
1319 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1320 List *tlist, List *scan_clauses)
1322 SubqueryScan *scan_plan;
1323 Index scan_relid = best_path->parent->relid;
1325 /* it should be a subquery base rel... */
1326 Assert(scan_relid > 0);
1327 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1329 /* Sort clauses into best execution order */
1330 scan_clauses = order_qual_clauses(root, scan_clauses);
1332 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1333 scan_clauses = extract_actual_clauses(scan_clauses, false);
1335 scan_plan = make_subqueryscan(tlist,
1338 best_path->parent->subplan,
1339 best_path->parent->subrtable,
1340 best_path->parent->subrowmark);
1342 copy_path_costsize(&scan_plan->scan.plan, best_path);
1348 * create_functionscan_plan
1349 * Returns a functionscan plan for the base relation scanned by 'best_path'
1350 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1352 static FunctionScan *
1353 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1354 List *tlist, List *scan_clauses)
1356 FunctionScan *scan_plan;
1357 Index scan_relid = best_path->parent->relid;
1360 /* it should be a function base rel... */
1361 Assert(scan_relid > 0);
1362 rte = planner_rt_fetch(scan_relid, root);
1363 Assert(rte->rtekind == RTE_FUNCTION);
1365 /* Sort clauses into best execution order */
1366 scan_clauses = order_qual_clauses(root, scan_clauses);
1368 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1369 scan_clauses = extract_actual_clauses(scan_clauses, false);
1371 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1373 rte->eref->colnames,
1375 rte->funccoltypmods);
1377 copy_path_costsize(&scan_plan->scan.plan, best_path);
1383 * create_valuesscan_plan
1384 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1385 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1388 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1389 List *tlist, List *scan_clauses)
1391 ValuesScan *scan_plan;
1392 Index scan_relid = best_path->parent->relid;
1395 /* it should be a values base rel... */
1396 Assert(scan_relid > 0);
1397 rte = planner_rt_fetch(scan_relid, root);
1398 Assert(rte->rtekind == RTE_VALUES);
1400 /* Sort clauses into best execution order */
1401 scan_clauses = order_qual_clauses(root, scan_clauses);
1403 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1404 scan_clauses = extract_actual_clauses(scan_clauses, false);
1406 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1409 copy_path_costsize(&scan_plan->scan.plan, best_path);
1415 * create_ctescan_plan
1416 * Returns a ctescan plan for the base relation scanned by 'best_path'
1417 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1420 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1421 List *tlist, List *scan_clauses)
1424 Index scan_relid = best_path->parent->relid;
1426 SubPlan *ctesplan = NULL;
1429 PlannerInfo *cteroot;
1434 Assert(scan_relid > 0);
1435 rte = planner_rt_fetch(scan_relid, root);
1436 Assert(rte->rtekind == RTE_CTE);
1437 Assert(!rte->self_reference);
1440 * Find the referenced CTE, and locate the SubPlan previously made for it.
1442 levelsup = rte->ctelevelsup;
1444 while (levelsup-- > 0)
1446 cteroot = cteroot->parent_root;
1447 if (!cteroot) /* shouldn't happen */
1448 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1452 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1453 * on planning the CTEs (ie, this is a side-reference from another CTE).
1454 * So we mustn't use forboth here.
1457 foreach(lc, cteroot->parse->cteList)
1459 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1461 if (strcmp(cte->ctename, rte->ctename) == 0)
1465 if (lc == NULL) /* shouldn't happen */
1466 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1467 if (ndx >= list_length(cteroot->cte_plan_ids))
1468 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1469 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1470 Assert(plan_id > 0);
1471 foreach(lc, cteroot->init_plans)
1473 ctesplan = (SubPlan *) lfirst(lc);
1474 if (ctesplan->plan_id == plan_id)
1477 if (lc == NULL) /* shouldn't happen */
1478 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1481 * We need the CTE param ID, which is the sole member of the SubPlan's
1484 cte_param_id = linitial_int(ctesplan->setParam);
1486 /* Sort clauses into best execution order */
1487 scan_clauses = order_qual_clauses(root, scan_clauses);
1489 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1490 scan_clauses = extract_actual_clauses(scan_clauses, false);
1492 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1493 plan_id, cte_param_id);
1495 copy_path_costsize(&scan_plan->scan.plan, best_path);
1501 * create_worktablescan_plan
1502 * Returns a worktablescan plan for the base relation scanned by 'best_path'
1503 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1505 static WorkTableScan *
1506 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1507 List *tlist, List *scan_clauses)
1509 WorkTableScan *scan_plan;
1510 Index scan_relid = best_path->parent->relid;
1513 PlannerInfo *cteroot;
1515 Assert(scan_relid > 0);
1516 rte = planner_rt_fetch(scan_relid, root);
1517 Assert(rte->rtekind == RTE_CTE);
1518 Assert(rte->self_reference);
1521 * We need to find the worktable param ID, which is in the plan level
1522 * that's processing the recursive UNION, which is one level *below* where
1523 * the CTE comes from.
1525 levelsup = rte->ctelevelsup;
1526 if (levelsup == 0) /* shouldn't happen */
1527 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1530 while (levelsup-- > 0)
1532 cteroot = cteroot->parent_root;
1533 if (!cteroot) /* shouldn't happen */
1534 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1536 if (cteroot->wt_param_id < 0) /* shouldn't happen */
1537 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1539 /* Sort clauses into best execution order */
1540 scan_clauses = order_qual_clauses(root, scan_clauses);
1542 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1543 scan_clauses = extract_actual_clauses(scan_clauses, false);
1545 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
1546 cteroot->wt_param_id);
1548 copy_path_costsize(&scan_plan->scan.plan, best_path);
1554 /*****************************************************************************
1558 *****************************************************************************/
1561 create_nestloop_plan(PlannerInfo *root,
1562 NestPath *best_path,
1566 List *tlist = build_relation_tlist(best_path->path.parent);
1567 List *joinrestrictclauses = best_path->joinrestrictinfo;
1570 NestLoop *join_plan;
1573 * If the inner path is a nestloop inner indexscan, it might be using some
1574 * of the join quals as index quals, in which case we don't have to check
1575 * them again at the join node. Remove any join quals that are redundant.
1577 joinrestrictclauses =
1578 select_nonredundant_join_clauses(root,
1579 joinrestrictclauses,
1580 best_path->innerjoinpath);
1582 /* Sort join qual clauses into best execution order */
1583 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
1585 /* Get the join qual clauses (in plain expression form) */
1586 /* Any pseudoconstant clauses are ignored here */
1587 if (IS_OUTER_JOIN(best_path->jointype))
1589 extract_actual_join_clauses(joinrestrictclauses,
1590 &joinclauses, &otherclauses);
1594 /* We can treat all clauses alike for an inner join */
1595 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
1599 join_plan = make_nestloop(tlist,
1604 best_path->jointype);
1606 copy_path_costsize(&join_plan->join.plan, &best_path->path);
1612 create_mergejoin_plan(PlannerInfo *root,
1613 MergePath *best_path,
1617 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1621 List *outerpathkeys;
1622 List *innerpathkeys;
1625 int *mergestrategies;
1626 bool *mergenullsfirst;
1627 MergeJoin *join_plan;
1633 /* Sort join qual clauses into best execution order */
1634 /* NB: do NOT reorder the mergeclauses */
1635 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1637 /* Get the join qual clauses (in plain expression form) */
1638 /* Any pseudoconstant clauses are ignored here */
1639 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1641 extract_actual_join_clauses(joinclauses,
1642 &joinclauses, &otherclauses);
1646 /* We can treat all clauses alike for an inner join */
1647 joinclauses = extract_actual_clauses(joinclauses, false);
1652 * Remove the mergeclauses from the list of join qual clauses, leaving the
1653 * list of quals that must be checked as qpquals.
1655 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
1656 joinclauses = list_difference(joinclauses, mergeclauses);
1659 * Rearrange mergeclauses, if needed, so that the outer variable is always
1660 * on the left; mark the mergeclause restrictinfos with correct
1661 * outer_is_left status.
1663 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
1664 best_path->jpath.outerjoinpath->parent->relids);
1667 * Create explicit sort nodes for the outer and inner join paths if
1668 * necessary. The sort cost was already accounted for in the path. Make
1669 * sure there are no excess columns in the inputs if sorting.
1671 if (best_path->outersortkeys)
1673 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1674 outer_plan = (Plan *)
1675 make_sort_from_pathkeys(root,
1677 best_path->outersortkeys,
1679 outerpathkeys = best_path->outersortkeys;
1682 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
1684 if (best_path->innersortkeys)
1686 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1687 inner_plan = (Plan *)
1688 make_sort_from_pathkeys(root,
1690 best_path->innersortkeys,
1692 innerpathkeys = best_path->innersortkeys;
1695 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
1698 * If inner plan is a sort that is expected to spill to disk, add a
1699 * materialize node to shield it from the need to handle mark/restore.
1700 * This will allow it to perform the last merge pass on-the-fly, while in
1701 * most cases not requiring the materialize to spill to disk.
1703 * XXX really, Sort oughta do this for itself, probably, to avoid the
1704 * overhead of a separate plan node.
1706 if (IsA(inner_plan, Sort) &&
1707 sort_exceeds_work_mem((Sort *) inner_plan))
1709 Plan *matplan = (Plan *) make_material(inner_plan);
1712 * We assume the materialize will not spill to disk, and therefore
1713 * charge just cpu_tuple_cost per tuple. (Keep this estimate in sync
1714 * with similar ones in cost_mergejoin and create_mergejoin_path.)
1716 copy_plan_costsize(matplan, inner_plan);
1717 matplan->total_cost += cpu_tuple_cost * matplan->plan_rows;
1719 inner_plan = matplan;
1723 * Compute the opfamily/strategy/nullsfirst arrays needed by the executor.
1724 * The information is in the pathkeys for the two inputs, but we need to
1725 * be careful about the possibility of mergeclauses sharing a pathkey
1726 * (compare find_mergeclauses_for_pathkeys()).
1728 nClauses = list_length(mergeclauses);
1729 Assert(nClauses == list_length(best_path->path_mergeclauses));
1730 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
1731 mergestrategies = (int *) palloc(nClauses * sizeof(int));
1732 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
1734 lop = list_head(outerpathkeys);
1735 lip = list_head(innerpathkeys);
1737 foreach(lc, best_path->path_mergeclauses)
1739 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1740 EquivalenceClass *oeclass;
1741 EquivalenceClass *ieclass;
1744 EquivalenceClass *opeclass;
1745 EquivalenceClass *ipeclass;
1748 /* fetch outer/inner eclass from mergeclause */
1749 Assert(IsA(rinfo, RestrictInfo));
1750 if (rinfo->outer_is_left)
1752 oeclass = rinfo->left_ec;
1753 ieclass = rinfo->right_ec;
1757 oeclass = rinfo->right_ec;
1758 ieclass = rinfo->left_ec;
1760 Assert(oeclass != NULL);
1761 Assert(ieclass != NULL);
1764 * For debugging purposes, we check that the eclasses match the
1765 * paths' pathkeys. In typical cases the merge clauses are one-to-one
1766 * with the pathkeys, but when dealing with partially redundant query
1767 * conditions, we might have clauses that re-reference earlier path
1768 * keys. The case that we need to reject is where a pathkey is
1769 * entirely skipped over.
1771 * lop and lip reference the first as-yet-unused pathkey elements;
1772 * it's okay to match them, or any element before them. If they're
1773 * NULL then we have found all pathkey elements to be used.
1777 opathkey = (PathKey *) lfirst(lop);
1778 opeclass = opathkey->pk_eclass;
1779 if (oeclass == opeclass)
1781 /* fast path for typical case */
1786 /* redundant clauses ... must match something before lop */
1787 foreach(l2, outerpathkeys)
1791 opathkey = (PathKey *) lfirst(l2);
1792 opeclass = opathkey->pk_eclass;
1793 if (oeclass == opeclass)
1796 if (oeclass != opeclass)
1797 elog(ERROR, "outer pathkeys do not match mergeclauses");
1802 /* redundant clauses ... must match some already-used pathkey */
1805 foreach(l2, outerpathkeys)
1807 opathkey = (PathKey *) lfirst(l2);
1808 opeclass = opathkey->pk_eclass;
1809 if (oeclass == opeclass)
1813 elog(ERROR, "outer pathkeys do not match mergeclauses");
1818 ipathkey = (PathKey *) lfirst(lip);
1819 ipeclass = ipathkey->pk_eclass;
1820 if (ieclass == ipeclass)
1822 /* fast path for typical case */
1827 /* redundant clauses ... must match something before lip */
1828 foreach(l2, innerpathkeys)
1832 ipathkey = (PathKey *) lfirst(l2);
1833 ipeclass = ipathkey->pk_eclass;
1834 if (ieclass == ipeclass)
1837 if (ieclass != ipeclass)
1838 elog(ERROR, "inner pathkeys do not match mergeclauses");
1843 /* redundant clauses ... must match some already-used pathkey */
1846 foreach(l2, innerpathkeys)
1848 ipathkey = (PathKey *) lfirst(l2);
1849 ipeclass = ipathkey->pk_eclass;
1850 if (ieclass == ipeclass)
1854 elog(ERROR, "inner pathkeys do not match mergeclauses");
1857 /* pathkeys should match each other too (more debugging) */
1858 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
1859 opathkey->pk_strategy != ipathkey->pk_strategy ||
1860 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
1861 elog(ERROR, "left and right pathkeys do not match in mergejoin");
1863 /* OK, save info for executor */
1864 mergefamilies[i] = opathkey->pk_opfamily;
1865 mergestrategies[i] = opathkey->pk_strategy;
1866 mergenullsfirst[i] = opathkey->pk_nulls_first;
1871 * Note: it is not an error if we have additional pathkey elements
1872 * (i.e., lop or lip isn't NULL here). The input paths might be
1873 * better-sorted than we need for the current mergejoin.
1877 * Now we can build the mergejoin node.
1879 join_plan = make_mergejoin(tlist,
1888 best_path->jpath.jointype);
1890 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1896 create_hashjoin_plan(PlannerInfo *root,
1897 HashPath *best_path,
1901 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1905 Oid skewTable = InvalidOid;
1906 AttrNumber skewColumn = InvalidAttrNumber;
1907 Oid skewColType = InvalidOid;
1908 int32 skewColTypmod = -1;
1909 HashJoin *join_plan;
1912 /* Sort join qual clauses into best execution order */
1913 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1914 /* There's no point in sorting the hash clauses ... */
1916 /* Get the join qual clauses (in plain expression form) */
1917 /* Any pseudoconstant clauses are ignored here */
1918 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1920 extract_actual_join_clauses(joinclauses,
1921 &joinclauses, &otherclauses);
1925 /* We can treat all clauses alike for an inner join */
1926 joinclauses = extract_actual_clauses(joinclauses, false);
1931 * Remove the hashclauses from the list of join qual clauses, leaving the
1932 * list of quals that must be checked as qpquals.
1934 hashclauses = get_actual_clauses(best_path->path_hashclauses);
1935 joinclauses = list_difference(joinclauses, hashclauses);
1938 * Rearrange hashclauses, if needed, so that the outer variable is always
1941 hashclauses = get_switched_clauses(best_path->path_hashclauses,
1942 best_path->jpath.outerjoinpath->parent->relids);
1944 /* We don't want any excess columns in the hashed tuples */
1945 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1947 /* If we expect batching, suppress excess columns in outer tuples too */
1948 if (best_path->num_batches > 1)
1949 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1952 * If there is a single join clause and we can identify the outer variable
1953 * as a simple column reference, supply its identity for possible use in
1954 * skew optimization. (Note: in principle we could do skew optimization
1955 * with multiple join clauses, but we'd have to be able to determine the
1956 * most common combinations of outer values, which we don't currently have
1957 * enough stats for.)
1959 if (list_length(hashclauses) == 1)
1961 OpExpr *clause = (OpExpr *) linitial(hashclauses);
1964 Assert(is_opclause(clause));
1965 node = (Node *) linitial(clause->args);
1966 if (IsA(node, RelabelType))
1967 node = (Node *) ((RelabelType *) node)->arg;
1970 Var *var = (Var *) node;
1973 rte = root->simple_rte_array[var->varno];
1974 if (rte->rtekind == RTE_RELATION)
1976 skewTable = rte->relid;
1977 skewColumn = var->varattno;
1978 skewColType = var->vartype;
1979 skewColTypmod = var->vartypmod;
1985 * Build the hash node and hash join node.
1987 hash_plan = make_hash(inner_plan,
1992 join_plan = make_hashjoin(tlist,
1998 best_path->jpath.jointype);
2000 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2006 /*****************************************************************************
2008 * SUPPORTING ROUTINES
2010 *****************************************************************************/
2013 * fix_indexqual_references
2014 * Adjust indexqual clauses to the form the executor's indexqual
2017 * We have three tasks here:
2018 * * Remove RestrictInfo nodes from the input clauses.
2019 * * Index keys must be represented by Var nodes with varattno set to the
2020 * index's attribute number, not the attribute number in the original rel.
2021 * * If the index key is on the right, commute the clause to put it on the
2024 * The result is a modified copy of the indexquals list --- the
2025 * original is not changed. Note also that the copy shares no substructure
2026 * with the original; this is needed in case there is a subplan in it (we need
2027 * two separate copies of the subplan tree, or things will go awry).
2030 fix_indexqual_references(List *indexquals, IndexPath *index_path)
2032 IndexOptInfo *index = index_path->indexinfo;
2033 List *fixed_indexquals;
2036 fixed_indexquals = NIL;
2039 * For each qual clause, commute if needed to put the indexkey operand on
2040 * the left, and then fix its varattno. (We do not need to change the
2041 * other side of the clause.)
2043 foreach(l, indexquals)
2045 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2048 Assert(IsA(rinfo, RestrictInfo));
2051 * Make a copy that will become the fixed clause.
2053 * We used to try to do a shallow copy here, but that fails if there
2054 * is a subplan in the arguments of the opclause. So just do a full
2057 clause = (Expr *) copyObject((Node *) rinfo->clause);
2059 if (IsA(clause, OpExpr))
2061 OpExpr *op = (OpExpr *) clause;
2063 if (list_length(op->args) != 2)
2064 elog(ERROR, "indexqual clause is not binary opclause");
2067 * Check to see if the indexkey is on the right; if so, commute
2068 * the clause. The indexkey should be the side that refers to
2069 * (only) the base relation.
2071 if (!bms_equal(rinfo->left_relids, index->rel->relids))
2075 * Now, determine which index attribute this is and change the
2076 * indexkey operand as needed.
2078 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2081 else if (IsA(clause, RowCompareExpr))
2083 RowCompareExpr *rc = (RowCompareExpr *) clause;
2087 * Check to see if the indexkey is on the right; if so, commute
2088 * the clause. The indexkey should be the side that refers to
2089 * (only) the base relation.
2091 if (!bms_overlap(pull_varnos(linitial(rc->largs)),
2092 index->rel->relids))
2093 CommuteRowCompareExpr(rc);
2096 * For each column in the row comparison, determine which index
2097 * attribute this is and change the indexkey operand as needed.
2099 foreach(lc, rc->largs)
2101 lfirst(lc) = fix_indexqual_operand(lfirst(lc),
2105 else if (IsA(clause, ScalarArrayOpExpr))
2107 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2109 /* Never need to commute... */
2112 * Determine which index attribute this is and change the indexkey
2113 * operand as needed.
2115 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2118 else if (IsA(clause, NullTest))
2120 NullTest *nt = (NullTest *) clause;
2122 Assert(nt->nulltesttype == IS_NULL);
2123 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
2127 elog(ERROR, "unsupported indexqual type: %d",
2128 (int) nodeTag(clause));
2130 fixed_indexquals = lappend(fixed_indexquals, clause);
2133 return fixed_indexquals;
2137 fix_indexqual_operand(Node *node, IndexOptInfo *index)
2140 * We represent index keys by Var nodes having the varno of the base table
2141 * but varattno equal to the index's attribute number (index column
2142 * position). This is a bit hokey ... would be cleaner to use a
2143 * special-purpose node type that could not be mistaken for a regular Var.
2144 * But it will do for now.
2148 ListCell *indexpr_item;
2151 * Remove any binary-compatible relabeling of the indexkey
2153 if (IsA(node, RelabelType))
2154 node = (Node *) ((RelabelType *) node)->arg;
2156 if (IsA(node, Var) &&
2157 ((Var *) node)->varno == index->rel->relid)
2159 /* Try to match against simple index columns */
2160 int varatt = ((Var *) node)->varattno;
2164 for (pos = 0; pos < index->ncolumns; pos++)
2166 if (index->indexkeys[pos] == varatt)
2168 result = (Var *) copyObject(node);
2169 result->varattno = pos + 1;
2170 return (Node *) result;
2176 /* Try to match against index expressions */
2177 indexpr_item = list_head(index->indexprs);
2178 for (pos = 0; pos < index->ncolumns; pos++)
2180 if (index->indexkeys[pos] == 0)
2184 if (indexpr_item == NULL)
2185 elog(ERROR, "too few entries in indexprs list");
2186 indexkey = (Node *) lfirst(indexpr_item);
2187 if (indexkey && IsA(indexkey, RelabelType))
2188 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2189 if (equal(node, indexkey))
2192 result = makeVar(index->rel->relid, pos + 1,
2193 exprType(lfirst(indexpr_item)), -1,
2195 return (Node *) result;
2197 indexpr_item = lnext(indexpr_item);
2202 elog(ERROR, "node is not an index attribute");
2203 return NULL; /* keep compiler quiet */
2207 * get_switched_clauses
2208 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2209 * extract the bare clauses, and rearrange the elements within the
2210 * clauses, if needed, so the outer join variable is on the left and
2211 * the inner is on the right. The original clause data structure is not
2212 * touched; a modified list is returned. We do, however, set the transient
2213 * outer_is_left field in each RestrictInfo to show which side was which.
2216 get_switched_clauses(List *clauses, Relids outerrelids)
2223 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
2224 OpExpr *clause = (OpExpr *) restrictinfo->clause;
2226 Assert(is_opclause(clause));
2227 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
2230 * Duplicate just enough of the structure to allow commuting the
2231 * clause without changing the original list. Could use
2232 * copyObject, but a complete deep copy is overkill.
2234 OpExpr *temp = makeNode(OpExpr);
2236 temp->opno = clause->opno;
2237 temp->opfuncid = InvalidOid;
2238 temp->opresulttype = clause->opresulttype;
2239 temp->opretset = clause->opretset;
2240 temp->args = list_copy(clause->args);
2241 temp->location = clause->location;
2242 /* Commute it --- note this modifies the temp node in-place. */
2243 CommuteOpExpr(temp);
2244 t_list = lappend(t_list, temp);
2245 restrictinfo->outer_is_left = false;
2249 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
2250 t_list = lappend(t_list, clause);
2251 restrictinfo->outer_is_left = true;
2258 * order_qual_clauses
2259 * Given a list of qual clauses that will all be evaluated at the same
2260 * plan node, sort the list into the order we want to check the quals
2263 * Ideally the order should be driven by a combination of execution cost and
2264 * selectivity, but it's not immediately clear how to account for both,
2265 * and given the uncertainty of the estimates the reliability of the decisions
2266 * would be doubtful anyway. So we just order by estimated per-tuple cost,
2267 * being careful not to change the order when (as is often the case) the
2268 * estimates are identical.
2270 * Although this will work on either bare clauses or RestrictInfos, it's
2271 * much faster to apply it to RestrictInfos, since it can re-use cost
2272 * information that is cached in RestrictInfos.
2274 * Note: some callers pass lists that contain entries that will later be
2275 * removed; this is the easiest way to let this routine see RestrictInfos
2276 * instead of bare clauses. It's OK because we only sort by cost, but
2277 * a cost/selectivity combination would likely do the wrong thing.
2280 order_qual_clauses(PlannerInfo *root, List *clauses)
2287 int nitems = list_length(clauses);
2293 /* No need to work hard for 0 or 1 clause */
2298 * Collect the items and costs into an array. This is to avoid repeated
2299 * cost_qual_eval work if the inputs aren't RestrictInfos.
2301 items = (QualItem *) palloc(nitems * sizeof(QualItem));
2303 foreach(lc, clauses)
2305 Node *clause = (Node *) lfirst(lc);
2308 cost_qual_eval_node(&qcost, clause, root);
2309 items[i].clause = clause;
2310 items[i].cost = qcost.per_tuple;
2315 * Sort. We don't use qsort() because it's not guaranteed stable for
2316 * equal keys. The expected number of entries is small enough that a
2317 * simple insertion sort should be good enough.
2319 for (i = 1; i < nitems; i++)
2321 QualItem newitem = items[i];
2324 /* insert newitem into the already-sorted subarray */
2325 for (j = i; j > 0; j--)
2327 if (newitem.cost >= items[j - 1].cost)
2329 items[j] = items[j - 1];
2334 /* Convert back to a list */
2336 for (i = 0; i < nitems; i++)
2337 result = lappend(result, items[i].clause);
2343 * Copy cost and size info from a Path node to the Plan node created from it.
2344 * The executor won't use this info, but it's needed by EXPLAIN.
2347 copy_path_costsize(Plan *dest, Path *src)
2351 dest->startup_cost = src->startup_cost;
2352 dest->total_cost = src->total_cost;
2353 dest->plan_rows = src->parent->rows;
2354 dest->plan_width = src->parent->width;
2358 dest->startup_cost = 0;
2359 dest->total_cost = 0;
2360 dest->plan_rows = 0;
2361 dest->plan_width = 0;
2366 * Copy cost and size info from a lower plan node to an inserted node.
2367 * This is not critical, since the decisions have already been made,
2368 * but it helps produce more reasonable-looking EXPLAIN output.
2369 * (Some callers alter the info after copying it.)
2372 copy_plan_costsize(Plan *dest, Plan *src)
2376 dest->startup_cost = src->startup_cost;
2377 dest->total_cost = src->total_cost;
2378 dest->plan_rows = src->plan_rows;
2379 dest->plan_width = src->plan_width;
2383 dest->startup_cost = 0;
2384 dest->total_cost = 0;
2385 dest->plan_rows = 0;
2386 dest->plan_width = 0;
2391 /*****************************************************************************
2393 * PLAN NODE BUILDING ROUTINES
2395 * Some of these are exported because they are called to build plan nodes
2396 * in contexts where we're not deriving the plan node from a path node.
2398 *****************************************************************************/
2401 make_seqscan(List *qptlist,
2405 SeqScan *node = makeNode(SeqScan);
2406 Plan *plan = &node->plan;
2408 /* cost should be inserted by caller */
2409 plan->targetlist = qptlist;
2410 plan->qual = qpqual;
2411 plan->lefttree = NULL;
2412 plan->righttree = NULL;
2413 node->scanrelid = scanrelid;
2419 make_indexscan(List *qptlist,
2424 List *indexqualorig,
2425 ScanDirection indexscandir)
2427 IndexScan *node = makeNode(IndexScan);
2428 Plan *plan = &node->scan.plan;
2430 /* cost should be inserted by caller */
2431 plan->targetlist = qptlist;
2432 plan->qual = qpqual;
2433 plan->lefttree = NULL;
2434 plan->righttree = NULL;
2435 node->scan.scanrelid = scanrelid;
2436 node->indexid = indexid;
2437 node->indexqual = indexqual;
2438 node->indexqualorig = indexqualorig;
2439 node->indexorderdir = indexscandir;
2444 static BitmapIndexScan *
2445 make_bitmap_indexscan(Index scanrelid,
2448 List *indexqualorig)
2450 BitmapIndexScan *node = makeNode(BitmapIndexScan);
2451 Plan *plan = &node->scan.plan;
2453 /* cost should be inserted by caller */
2454 plan->targetlist = NIL; /* not used */
2455 plan->qual = NIL; /* not used */
2456 plan->lefttree = NULL;
2457 plan->righttree = NULL;
2458 node->scan.scanrelid = scanrelid;
2459 node->indexid = indexid;
2460 node->indexqual = indexqual;
2461 node->indexqualorig = indexqualorig;
2466 static BitmapHeapScan *
2467 make_bitmap_heapscan(List *qptlist,
2470 List *bitmapqualorig,
2473 BitmapHeapScan *node = makeNode(BitmapHeapScan);
2474 Plan *plan = &node->scan.plan;
2476 /* cost should be inserted by caller */
2477 plan->targetlist = qptlist;
2478 plan->qual = qpqual;
2479 plan->lefttree = lefttree;
2480 plan->righttree = NULL;
2481 node->scan.scanrelid = scanrelid;
2482 node->bitmapqualorig = bitmapqualorig;
2488 make_tidscan(List *qptlist,
2493 TidScan *node = makeNode(TidScan);
2494 Plan *plan = &node->scan.plan;
2496 /* cost should be inserted by caller */
2497 plan->targetlist = qptlist;
2498 plan->qual = qpqual;
2499 plan->lefttree = NULL;
2500 plan->righttree = NULL;
2501 node->scan.scanrelid = scanrelid;
2502 node->tidquals = tidquals;
2508 make_subqueryscan(List *qptlist,
2515 SubqueryScan *node = makeNode(SubqueryScan);
2516 Plan *plan = &node->scan.plan;
2519 * Cost is figured here for the convenience of prepunion.c. Note this is
2520 * only correct for the case where qpqual is empty; otherwise caller
2521 * should overwrite cost with a better estimate.
2523 copy_plan_costsize(plan, subplan);
2524 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
2526 plan->targetlist = qptlist;
2527 plan->qual = qpqual;
2528 plan->lefttree = NULL;
2529 plan->righttree = NULL;
2530 node->scan.scanrelid = scanrelid;
2531 node->subplan = subplan;
2532 node->subrtable = subrtable;
2533 node->subrowmark = subrowmark;
2538 static FunctionScan *
2539 make_functionscan(List *qptlist,
2545 List *funccoltypmods)
2547 FunctionScan *node = makeNode(FunctionScan);
2548 Plan *plan = &node->scan.plan;
2550 /* cost should be inserted by caller */
2551 plan->targetlist = qptlist;
2552 plan->qual = qpqual;
2553 plan->lefttree = NULL;
2554 plan->righttree = NULL;
2555 node->scan.scanrelid = scanrelid;
2556 node->funcexpr = funcexpr;
2557 node->funccolnames = funccolnames;
2558 node->funccoltypes = funccoltypes;
2559 node->funccoltypmods = funccoltypmods;
2565 make_valuesscan(List *qptlist,
2570 ValuesScan *node = makeNode(ValuesScan);
2571 Plan *plan = &node->scan.plan;
2573 /* cost should be inserted by caller */
2574 plan->targetlist = qptlist;
2575 plan->qual = qpqual;
2576 plan->lefttree = NULL;
2577 plan->righttree = NULL;
2578 node->scan.scanrelid = scanrelid;
2579 node->values_lists = values_lists;
2585 make_ctescan(List *qptlist,
2591 CteScan *node = makeNode(CteScan);
2592 Plan *plan = &node->scan.plan;
2594 /* cost should be inserted by caller */
2595 plan->targetlist = qptlist;
2596 plan->qual = qpqual;
2597 plan->lefttree = NULL;
2598 plan->righttree = NULL;
2599 node->scan.scanrelid = scanrelid;
2600 node->ctePlanId = ctePlanId;
2601 node->cteParam = cteParam;
2606 static WorkTableScan *
2607 make_worktablescan(List *qptlist,
2612 WorkTableScan *node = makeNode(WorkTableScan);
2613 Plan *plan = &node->scan.plan;
2615 /* cost should be inserted by caller */
2616 plan->targetlist = qptlist;
2617 plan->qual = qpqual;
2618 plan->lefttree = NULL;
2619 plan->righttree = NULL;
2620 node->scan.scanrelid = scanrelid;
2621 node->wtParam = wtParam;
2627 make_append(List *appendplans, List *tlist)
2629 Append *node = makeNode(Append);
2630 Plan *plan = &node->plan;
2635 * Compute cost as sum of subplan costs. We charge nothing extra for the
2636 * Append itself, which perhaps is too optimistic, but since it doesn't do
2637 * any selection or projection, it is a pretty cheap node.
2639 plan->startup_cost = 0;
2640 plan->total_cost = 0;
2641 plan->plan_rows = 0;
2643 foreach(subnode, appendplans)
2645 Plan *subplan = (Plan *) lfirst(subnode);
2647 if (subnode == list_head(appendplans)) /* first node? */
2648 plan->startup_cost = subplan->startup_cost;
2649 plan->total_cost += subplan->total_cost;
2650 plan->plan_rows += subplan->plan_rows;
2651 total_size += subplan->plan_width * subplan->plan_rows;
2653 if (plan->plan_rows > 0)
2654 plan->plan_width = rint(total_size / plan->plan_rows);
2656 plan->plan_width = 0;
2658 plan->targetlist = tlist;
2660 plan->lefttree = NULL;
2661 plan->righttree = NULL;
2662 node->appendplans = appendplans;
2668 make_recursive_union(List *tlist,
2675 RecursiveUnion *node = makeNode(RecursiveUnion);
2676 Plan *plan = &node->plan;
2677 int numCols = list_length(distinctList);
2679 cost_recursive_union(plan, lefttree, righttree);
2681 plan->targetlist = tlist;
2683 plan->lefttree = lefttree;
2684 plan->righttree = righttree;
2685 node->wtParam = wtParam;
2688 * convert SortGroupClause list into arrays of attr indexes and equality
2689 * operators, as wanted by executor
2691 node->numCols = numCols;
2695 AttrNumber *dupColIdx;
2699 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
2700 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
2702 foreach(slitem, distinctList)
2704 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
2705 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
2708 dupColIdx[keyno] = tle->resno;
2709 dupOperators[keyno] = sortcl->eqop;
2710 Assert(OidIsValid(dupOperators[keyno]));
2713 node->dupColIdx = dupColIdx;
2714 node->dupOperators = dupOperators;
2716 node->numGroups = numGroups;
2722 make_bitmap_and(List *bitmapplans)
2724 BitmapAnd *node = makeNode(BitmapAnd);
2725 Plan *plan = &node->plan;
2727 /* cost should be inserted by caller */
2728 plan->targetlist = NIL;
2730 plan->lefttree = NULL;
2731 plan->righttree = NULL;
2732 node->bitmapplans = bitmapplans;
2738 make_bitmap_or(List *bitmapplans)
2740 BitmapOr *node = makeNode(BitmapOr);
2741 Plan *plan = &node->plan;
2743 /* cost should be inserted by caller */
2744 plan->targetlist = NIL;
2746 plan->lefttree = NULL;
2747 plan->righttree = NULL;
2748 node->bitmapplans = bitmapplans;
2754 make_nestloop(List *tlist,
2761 NestLoop *node = makeNode(NestLoop);
2762 Plan *plan = &node->join.plan;
2764 /* cost should be inserted by caller */
2765 plan->targetlist = tlist;
2766 plan->qual = otherclauses;
2767 plan->lefttree = lefttree;
2768 plan->righttree = righttree;
2769 node->join.jointype = jointype;
2770 node->join.joinqual = joinclauses;
2776 make_hashjoin(List *tlist,
2784 HashJoin *node = makeNode(HashJoin);
2785 Plan *plan = &node->join.plan;
2787 /* cost should be inserted by caller */
2788 plan->targetlist = tlist;
2789 plan->qual = otherclauses;
2790 plan->lefttree = lefttree;
2791 plan->righttree = righttree;
2792 node->hashclauses = hashclauses;
2793 node->join.jointype = jointype;
2794 node->join.joinqual = joinclauses;
2800 make_hash(Plan *lefttree,
2802 AttrNumber skewColumn,
2804 int32 skewColTypmod)
2806 Hash *node = makeNode(Hash);
2807 Plan *plan = &node->plan;
2809 copy_plan_costsize(plan, lefttree);
2812 * For plausibility, make startup & total costs equal total cost of input
2813 * plan; this only affects EXPLAIN display not decisions.
2815 plan->startup_cost = plan->total_cost;
2816 plan->targetlist = lefttree->targetlist;
2818 plan->lefttree = lefttree;
2819 plan->righttree = NULL;
2821 node->skewTable = skewTable;
2822 node->skewColumn = skewColumn;
2823 node->skewColType = skewColType;
2824 node->skewColTypmod = skewColTypmod;
2830 make_mergejoin(List *tlist,
2835 int *mergestrategies,
2836 bool *mergenullsfirst,
2841 MergeJoin *node = makeNode(MergeJoin);
2842 Plan *plan = &node->join.plan;
2844 /* cost should be inserted by caller */
2845 plan->targetlist = tlist;
2846 plan->qual = otherclauses;
2847 plan->lefttree = lefttree;
2848 plan->righttree = righttree;
2849 node->mergeclauses = mergeclauses;
2850 node->mergeFamilies = mergefamilies;
2851 node->mergeStrategies = mergestrategies;
2852 node->mergeNullsFirst = mergenullsfirst;
2853 node->join.jointype = jointype;
2854 node->join.joinqual = joinclauses;
2860 * make_sort --- basic routine to build a Sort plan node
2862 * Caller must have built the sortColIdx, sortOperators, and nullsFirst
2863 * arrays already. limit_tuples is as for cost_sort (in particular, pass
2867 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
2868 AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
2869 double limit_tuples)
2871 Sort *node = makeNode(Sort);
2872 Plan *plan = &node->plan;
2873 Path sort_path; /* dummy for result of cost_sort */
2875 copy_plan_costsize(plan, lefttree); /* only care about copying size */
2876 cost_sort(&sort_path, root, NIL,
2877 lefttree->total_cost,
2878 lefttree->plan_rows,
2879 lefttree->plan_width,
2881 plan->startup_cost = sort_path.startup_cost;
2882 plan->total_cost = sort_path.total_cost;
2883 plan->targetlist = lefttree->targetlist;
2885 plan->lefttree = lefttree;
2886 plan->righttree = NULL;
2887 node->numCols = numCols;
2888 node->sortColIdx = sortColIdx;
2889 node->sortOperators = sortOperators;
2890 node->nullsFirst = nullsFirst;
2896 * add_sort_column --- utility subroutine for building sort info arrays
2898 * We need this routine because the same column might be selected more than
2899 * once as a sort key column; if so, the extra mentions are redundant.
2901 * Caller is assumed to have allocated the arrays large enough for the
2902 * max possible number of columns. Return value is the new column count.
2905 add_sort_column(AttrNumber colIdx, Oid sortOp, bool nulls_first,
2906 int numCols, AttrNumber *sortColIdx,
2907 Oid *sortOperators, bool *nullsFirst)
2911 Assert(OidIsValid(sortOp));
2913 for (i = 0; i < numCols; i++)
2916 * Note: we check sortOp because it's conceivable that "ORDER BY foo
2917 * USING <, foo USING <<<" is not redundant, if <<< distinguishes
2918 * values that < considers equal. We need not check nulls_first
2919 * however because a lower-order column with the same sortop but
2920 * opposite nulls direction is redundant.
2922 if (sortColIdx[i] == colIdx &&
2923 sortOperators[numCols] == sortOp)
2925 /* Already sorting by this col, so extra sort key is useless */
2930 /* Add the column */
2931 sortColIdx[numCols] = colIdx;
2932 sortOperators[numCols] = sortOp;
2933 nullsFirst[numCols] = nulls_first;
2938 * make_sort_from_pathkeys
2939 * Create sort plan to sort according to given pathkeys
2941 * 'lefttree' is the node which yields input tuples
2942 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
2943 * 'limit_tuples' is the bound on the number of output tuples;
2946 * We must convert the pathkey information into arrays of sort key column
2947 * numbers and sort operator OIDs.
2949 * If the pathkeys include expressions that aren't simple Vars, we will
2950 * usually need to add resjunk items to the input plan's targetlist to
2951 * compute these expressions (since the Sort node itself won't do it).
2952 * If the input plan type isn't one that can do projections, this means
2953 * adding a Result node just to do the projection.
2956 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
2957 double limit_tuples)
2959 List *tlist = lefttree->targetlist;
2962 AttrNumber *sortColIdx;
2967 * We will need at most list_length(pathkeys) sort columns; possibly less
2969 numsortkeys = list_length(pathkeys);
2970 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2971 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2972 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2976 foreach(i, pathkeys)
2978 PathKey *pathkey = (PathKey *) lfirst(i);
2979 EquivalenceClass *ec = pathkey->pk_eclass;
2980 TargetEntry *tle = NULL;
2981 Oid pk_datatype = InvalidOid;
2985 if (ec->ec_has_volatile)
2988 * If the pathkey's EquivalenceClass is volatile, then it must
2989 * have come from an ORDER BY clause, and we have to match it to
2990 * that same targetlist entry.
2992 if (ec->ec_sortref == 0) /* can't happen */
2993 elog(ERROR, "volatile EquivalenceClass has no sortref");
2994 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
2996 Assert(list_length(ec->ec_members) == 1);
2997 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
3002 * Otherwise, we can sort by any non-constant expression listed in
3003 * the pathkey's EquivalenceClass. For now, we take the first one
3004 * that corresponds to an available item in the tlist. If there
3005 * isn't any, use the first one that is an expression in the
3006 * input's vars. (The non-const restriction only matters if the
3007 * EC is below_outer_join; but if it isn't, it won't contain
3008 * consts anyway, else we'd have discarded the pathkey as
3011 * XXX if we have a choice, is there any way of figuring out which
3012 * might be cheapest to execute? (For example, int4lt is likely
3013 * much cheaper to execute than numericlt, but both might appear
3014 * in the same equivalence class...) Not clear that we ever will
3015 * have an interesting choice in practice, so it may not matter.
3017 foreach(j, ec->ec_members)
3019 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3021 if (em->em_is_const || em->em_is_child)
3024 tle = tlist_member((Node *) em->em_expr, tlist);
3027 pk_datatype = em->em_datatype;
3028 break; /* found expr already in tlist */
3032 * We can also use it if the pathkey expression is a relabel
3033 * of the tlist entry, or vice versa. This is needed for
3034 * binary-compatible cases (cf. make_pathkey_from_sortinfo).
3035 * We prefer an exact match, though, so we do the basic search
3038 tle = tlist_member_ignore_relabel((Node *) em->em_expr, tlist);
3041 pk_datatype = em->em_datatype;
3042 break; /* found expr already in tlist */
3048 /* No matching tlist item; look for a computable expression */
3049 Expr *sortexpr = NULL;
3051 foreach(j, ec->ec_members)
3053 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3057 if (em->em_is_const || em->em_is_child)
3059 sortexpr = em->em_expr;
3060 exprvars = pull_var_clause((Node *) sortexpr,
3061 PVC_INCLUDE_PLACEHOLDERS);
3062 foreach(k, exprvars)
3064 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
3067 list_free(exprvars);
3070 pk_datatype = em->em_datatype;
3071 break; /* found usable expression */
3075 elog(ERROR, "could not find pathkey item to sort");
3078 * Do we need to insert a Result node?
3080 if (!is_projection_capable_plan(lefttree))
3082 /* copy needed so we don't modify input's tlist below */
3083 tlist = copyObject(tlist);
3084 lefttree = (Plan *) make_result(root, tlist, NULL,
3089 * Add resjunk entry to input's tlist
3091 tle = makeTargetEntry(sortexpr,
3092 list_length(tlist) + 1,
3095 tlist = lappend(tlist, tle);
3096 lefttree->targetlist = tlist; /* just in case NIL before */
3101 * Look up the correct sort operator from the PathKey's slightly
3102 * abstracted representation.
3104 sortop = get_opfamily_member(pathkey->pk_opfamily,
3107 pathkey->pk_strategy);
3108 if (!OidIsValid(sortop)) /* should not happen */
3109 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
3110 pathkey->pk_strategy, pk_datatype, pk_datatype,
3111 pathkey->pk_opfamily);
3114 * The column might already be selected as a sort key, if the pathkeys
3115 * contain duplicate entries. (This can happen in scenarios where
3116 * multiple mergejoinable clauses mention the same var, for example.)
3117 * So enter it only once in the sort arrays.
3119 numsortkeys = add_sort_column(tle->resno,
3121 pathkey->pk_nulls_first,
3123 sortColIdx, sortOperators, nullsFirst);
3126 Assert(numsortkeys > 0);
3128 return make_sort(root, lefttree, numsortkeys,
3129 sortColIdx, sortOperators, nullsFirst, limit_tuples);
3133 * make_sort_from_sortclauses
3134 * Create sort plan to sort according to given sortclauses
3136 * 'sortcls' is a list of SortGroupClauses
3137 * 'lefttree' is the node which yields input tuples
3140 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
3142 List *sub_tlist = lefttree->targetlist;
3145 AttrNumber *sortColIdx;
3150 * We will need at most list_length(sortcls) sort columns; possibly less
3152 numsortkeys = list_length(sortcls);
3153 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3154 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3155 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3161 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
3162 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
3165 * Check for the possibility of duplicate order-by clauses --- the
3166 * parser should have removed 'em, but no point in sorting
3169 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
3170 sortcl->nulls_first,
3172 sortColIdx, sortOperators, nullsFirst);
3175 Assert(numsortkeys > 0);
3177 return make_sort(root, lefttree, numsortkeys,
3178 sortColIdx, sortOperators, nullsFirst, -1.0);
3182 * make_sort_from_groupcols
3183 * Create sort plan to sort based on grouping columns
3185 * 'groupcls' is the list of SortGroupClauses
3186 * 'grpColIdx' gives the column numbers to use
3188 * This might look like it could be merged with make_sort_from_sortclauses,
3189 * but presently we *must* use the grpColIdx[] array to locate sort columns,
3190 * because the child plan's tlist is not marked with ressortgroupref info
3191 * appropriate to the grouping node. So, only the sort ordering info
3192 * is used from the SortGroupClause entries.
3195 make_sort_from_groupcols(PlannerInfo *root,
3197 AttrNumber *grpColIdx,
3200 List *sub_tlist = lefttree->targetlist;
3204 AttrNumber *sortColIdx;
3209 * We will need at most list_length(groupcls) sort columns; possibly less
3211 numsortkeys = list_length(groupcls);
3212 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3213 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3214 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3218 foreach(l, groupcls)
3220 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
3221 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
3224 * Check for the possibility of duplicate group-by clauses --- the
3225 * parser should have removed 'em, but no point in sorting
3228 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
3231 sortColIdx, sortOperators, nullsFirst);
3235 Assert(numsortkeys > 0);
3237 return make_sort(root, lefttree, numsortkeys,
3238 sortColIdx, sortOperators, nullsFirst, -1.0);
3242 make_material(Plan *lefttree)
3244 Material *node = makeNode(Material);
3245 Plan *plan = &node->plan;
3247 /* cost should be inserted by caller */
3248 plan->targetlist = lefttree->targetlist;
3250 plan->lefttree = lefttree;
3251 plan->righttree = NULL;
3257 * materialize_finished_plan: stick a Material node atop a completed plan
3259 * There are a couple of places where we want to attach a Material node
3260 * after completion of subquery_planner(). This currently requires hackery.
3261 * Since subquery_planner has already run SS_finalize_plan on the subplan
3262 * tree, we have to kluge up parameter lists for the Material node.
3263 * Possibly this could be fixed by postponing SS_finalize_plan processing
3264 * until setrefs.c is run?
3267 materialize_finished_plan(Plan *subplan)
3270 Path matpath; /* dummy for result of cost_material */
3272 matplan = (Plan *) make_material(subplan);
3275 cost_material(&matpath,
3276 subplan->startup_cost,
3277 subplan->total_cost,
3279 subplan->plan_width);
3280 matplan->startup_cost = matpath.startup_cost;
3281 matplan->total_cost = matpath.total_cost;
3282 matplan->plan_rows = subplan->plan_rows;
3283 matplan->plan_width = subplan->plan_width;
3285 /* parameter kluge --- see comments above */
3286 matplan->extParam = bms_copy(subplan->extParam);
3287 matplan->allParam = bms_copy(subplan->allParam);
3293 make_agg(PlannerInfo *root, List *tlist, List *qual,
3294 AggStrategy aggstrategy,
3295 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
3296 long numGroups, int numAggs,
3299 Agg *node = makeNode(Agg);
3300 Plan *plan = &node->plan;
3301 Path agg_path; /* dummy for result of cost_agg */
3304 node->aggstrategy = aggstrategy;
3305 node->numCols = numGroupCols;
3306 node->grpColIdx = grpColIdx;
3307 node->grpOperators = grpOperators;
3308 node->numGroups = numGroups;
3310 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3311 cost_agg(&agg_path, root,
3312 aggstrategy, numAggs,
3313 numGroupCols, numGroups,
3314 lefttree->startup_cost,
3315 lefttree->total_cost,
3316 lefttree->plan_rows);
3317 plan->startup_cost = agg_path.startup_cost;
3318 plan->total_cost = agg_path.total_cost;
3321 * We will produce a single output tuple if not grouping, and a tuple per
3324 if (aggstrategy == AGG_PLAIN)
3325 plan->plan_rows = 1;
3327 plan->plan_rows = numGroups;
3330 * We also need to account for the cost of evaluation of the qual (ie, the
3331 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
3332 * anything for Aggref nodes; this is okay since they are really
3333 * comparable to Vars.
3335 * See notes in grouping_planner about why only make_agg, make_windowagg
3336 * and make_group worry about tlist eval cost.
3340 cost_qual_eval(&qual_cost, qual, root);
3341 plan->startup_cost += qual_cost.startup;
3342 plan->total_cost += qual_cost.startup;
3343 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3345 cost_qual_eval(&qual_cost, tlist, root);
3346 plan->startup_cost += qual_cost.startup;
3347 plan->total_cost += qual_cost.startup;
3348 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3351 plan->targetlist = tlist;
3352 plan->lefttree = lefttree;
3353 plan->righttree = NULL;
3359 make_windowagg(PlannerInfo *root, List *tlist,
3360 int numWindowFuncs, Index winref,
3361 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
3362 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
3363 int frameOptions, Plan *lefttree)
3365 WindowAgg *node = makeNode(WindowAgg);
3366 Plan *plan = &node->plan;
3367 Path windowagg_path; /* dummy for result of cost_windowagg */
3370 node->winref = winref;
3371 node->partNumCols = partNumCols;
3372 node->partColIdx = partColIdx;
3373 node->partOperators = partOperators;
3374 node->ordNumCols = ordNumCols;
3375 node->ordColIdx = ordColIdx;
3376 node->ordOperators = ordOperators;
3377 node->frameOptions = frameOptions;
3379 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3380 cost_windowagg(&windowagg_path, root,
3381 numWindowFuncs, partNumCols, ordNumCols,
3382 lefttree->startup_cost,
3383 lefttree->total_cost,
3384 lefttree->plan_rows);
3385 plan->startup_cost = windowagg_path.startup_cost;
3386 plan->total_cost = windowagg_path.total_cost;
3389 * We also need to account for the cost of evaluation of the tlist.
3391 * See notes in grouping_planner about why only make_agg, make_windowagg
3392 * and make_group worry about tlist eval cost.
3394 cost_qual_eval(&qual_cost, tlist, root);
3395 plan->startup_cost += qual_cost.startup;
3396 plan->total_cost += qual_cost.startup;
3397 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3399 plan->targetlist = tlist;
3400 plan->lefttree = lefttree;
3401 plan->righttree = NULL;
3402 /* WindowAgg nodes never have a qual clause */
3409 make_group(PlannerInfo *root,
3413 AttrNumber *grpColIdx,
3418 Group *node = makeNode(Group);
3419 Plan *plan = &node->plan;
3420 Path group_path; /* dummy for result of cost_group */
3423 node->numCols = numGroupCols;
3424 node->grpColIdx = grpColIdx;
3425 node->grpOperators = grpOperators;
3427 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3428 cost_group(&group_path, root,
3429 numGroupCols, numGroups,
3430 lefttree->startup_cost,
3431 lefttree->total_cost,
3432 lefttree->plan_rows);
3433 plan->startup_cost = group_path.startup_cost;
3434 plan->total_cost = group_path.total_cost;
3436 /* One output tuple per estimated result group */
3437 plan->plan_rows = numGroups;
3440 * We also need to account for the cost of evaluation of the qual (ie, the
3441 * HAVING clause) and the tlist.
3443 * XXX this double-counts the cost of evaluation of any expressions used
3444 * for grouping, since in reality those will have been evaluated at a
3445 * lower plan level and will only be copied by the Group node. Worth
3448 * See notes in grouping_planner about why only make_agg, make_windowagg
3449 * and make_group worry about tlist eval cost.
3453 cost_qual_eval(&qual_cost, qual, root);
3454 plan->startup_cost += qual_cost.startup;
3455 plan->total_cost += qual_cost.startup;
3456 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3458 cost_qual_eval(&qual_cost, tlist, root);
3459 plan->startup_cost += qual_cost.startup;
3460 plan->total_cost += qual_cost.startup;
3461 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3464 plan->targetlist = tlist;
3465 plan->lefttree = lefttree;
3466 plan->righttree = NULL;
3472 * distinctList is a list of SortGroupClauses, identifying the targetlist items
3473 * that should be considered by the Unique filter. The input path must
3474 * already be sorted accordingly.
3477 make_unique(Plan *lefttree, List *distinctList)
3479 Unique *node = makeNode(Unique);
3480 Plan *plan = &node->plan;
3481 int numCols = list_length(distinctList);
3483 AttrNumber *uniqColIdx;
3487 copy_plan_costsize(plan, lefttree);
3490 * Charge one cpu_operator_cost per comparison per input tuple. We assume
3491 * all columns get compared at most of the tuples. (XXX probably this is
3494 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
3497 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
3498 * we assume the filter removes nothing. The caller must alter this if he
3499 * has a better idea.
3502 plan->targetlist = lefttree->targetlist;
3504 plan->lefttree = lefttree;
3505 plan->righttree = NULL;
3508 * convert SortGroupClause list into arrays of attr indexes and equality
3509 * operators, as wanted by executor
3511 Assert(numCols > 0);
3512 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3513 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3515 foreach(slitem, distinctList)
3517 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3518 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3520 uniqColIdx[keyno] = tle->resno;
3521 uniqOperators[keyno] = sortcl->eqop;
3522 Assert(OidIsValid(uniqOperators[keyno]));
3526 node->numCols = numCols;
3527 node->uniqColIdx = uniqColIdx;
3528 node->uniqOperators = uniqOperators;
3534 * distinctList is a list of SortGroupClauses, identifying the targetlist
3535 * items that should be considered by the SetOp filter. The input path must
3536 * already be sorted accordingly.
3539 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
3540 List *distinctList, AttrNumber flagColIdx, int firstFlag,
3541 long numGroups, double outputRows)
3543 SetOp *node = makeNode(SetOp);
3544 Plan *plan = &node->plan;
3545 int numCols = list_length(distinctList);
3547 AttrNumber *dupColIdx;
3551 copy_plan_costsize(plan, lefttree);
3552 plan->plan_rows = outputRows;
3555 * Charge one cpu_operator_cost per comparison per input tuple. We assume
3556 * all columns get compared at most of the tuples.
3558 plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
3560 plan->targetlist = lefttree->targetlist;
3562 plan->lefttree = lefttree;
3563 plan->righttree = NULL;
3566 * convert SortGroupClause list into arrays of attr indexes and equality
3567 * operators, as wanted by executor
3569 Assert(numCols > 0);
3570 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3571 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3573 foreach(slitem, distinctList)
3575 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3576 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3578 dupColIdx[keyno] = tle->resno;
3579 dupOperators[keyno] = sortcl->eqop;
3580 Assert(OidIsValid(dupOperators[keyno]));
3585 node->strategy = strategy;
3586 node->numCols = numCols;
3587 node->dupColIdx = dupColIdx;
3588 node->dupOperators = dupOperators;
3589 node->flagColIdx = flagColIdx;
3590 node->firstFlag = firstFlag;
3591 node->numGroups = numGroups;
3598 * Build a LockRows plan node
3601 make_lockrows(Plan *lefttree, List *rowMarks)
3603 LockRows *node = makeNode(LockRows);
3604 Plan *plan = &node->plan;
3606 copy_plan_costsize(plan, lefttree);
3608 /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
3609 plan->total_cost += cpu_tuple_cost * plan->plan_rows;
3611 plan->targetlist = lefttree->targetlist;
3613 plan->lefttree = lefttree;
3614 plan->righttree = NULL;
3616 node->rowMarks = rowMarks;
3622 * Note: offset_est and count_est are passed in to save having to repeat
3623 * work already done to estimate the values of the limitOffset and limitCount
3624 * expressions. Their values are as returned by preprocess_limit (0 means
3625 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
3626 * with that function!
3629 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
3630 int64 offset_est, int64 count_est)
3632 Limit *node = makeNode(Limit);
3633 Plan *plan = &node->plan;
3635 copy_plan_costsize(plan, lefttree);
3638 * Adjust the output rows count and costs according to the offset/limit.
3639 * This is only a cosmetic issue if we are at top level, but if we are
3640 * building a subquery then it's important to report correct info to the
3643 * When the offset or count couldn't be estimated, use 10% of the
3644 * estimated number of rows emitted from the subplan.
3646 if (offset_est != 0)
3651 offset_rows = (double) offset_est;
3653 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
3654 if (offset_rows > plan->plan_rows)
3655 offset_rows = plan->plan_rows;
3656 if (plan->plan_rows > 0)
3657 plan->startup_cost +=
3658 (plan->total_cost - plan->startup_cost)
3659 * offset_rows / plan->plan_rows;
3660 plan->plan_rows -= offset_rows;
3661 if (plan->plan_rows < 1)
3662 plan->plan_rows = 1;
3670 count_rows = (double) count_est;
3672 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
3673 if (count_rows > plan->plan_rows)
3674 count_rows = plan->plan_rows;
3675 if (plan->plan_rows > 0)
3676 plan->total_cost = plan->startup_cost +
3677 (plan->total_cost - plan->startup_cost)
3678 * count_rows / plan->plan_rows;
3679 plan->plan_rows = count_rows;
3680 if (plan->plan_rows < 1)
3681 plan->plan_rows = 1;
3684 plan->targetlist = lefttree->targetlist;
3686 plan->lefttree = lefttree;
3687 plan->righttree = NULL;
3689 node->limitOffset = limitOffset;
3690 node->limitCount = limitCount;
3697 * Build a Result plan node
3699 * If we have a subplan, assume that any evaluation costs for the gating qual
3700 * were already factored into the subplan's startup cost, and just copy the
3701 * subplan cost. If there's no subplan, we should include the qual eval
3702 * cost. In either case, tlist eval cost is not to be included here.
3705 make_result(PlannerInfo *root,
3707 Node *resconstantqual,
3710 Result *node = makeNode(Result);
3711 Plan *plan = &node->plan;
3714 copy_plan_costsize(plan, subplan);
3717 plan->startup_cost = 0;
3718 plan->total_cost = cpu_tuple_cost;
3719 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
3720 plan->plan_width = 0; /* XXX is it worth being smarter? */
3721 if (resconstantqual)
3725 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
3726 /* resconstantqual is evaluated once at startup */
3727 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
3728 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
3732 plan->targetlist = tlist;
3734 plan->lefttree = subplan;
3735 plan->righttree = NULL;
3736 node->resconstantqual = resconstantqual;
3743 * Build a ModifyTable plan node
3745 * Currently, we don't charge anything extra for the actual table modification
3746 * work, nor for the RETURNING expressions if any. It would only be window
3747 * dressing, since these are always top-level nodes and there is no way for
3748 * the costs to change any higher-level planning choices. But we might want
3749 * to make it look better sometime.
3752 make_modifytable(CmdType operation, List *resultRelations,
3753 List *subplans, List *returningLists)
3755 ModifyTable *node = makeNode(ModifyTable);
3756 Plan *plan = &node->plan;
3760 Assert(list_length(resultRelations) == list_length(subplans));
3761 Assert(returningLists == NIL ||
3762 list_length(resultRelations) == list_length(returningLists));
3765 * Compute cost as sum of subplan costs.
3767 plan->startup_cost = 0;
3768 plan->total_cost = 0;
3769 plan->plan_rows = 0;
3771 foreach(subnode, subplans)
3773 Plan *subplan = (Plan *) lfirst(subnode);
3775 if (subnode == list_head(subplans)) /* first node? */
3776 plan->startup_cost = subplan->startup_cost;
3777 plan->total_cost += subplan->total_cost;
3778 plan->plan_rows += subplan->plan_rows;
3779 total_size += subplan->plan_width * subplan->plan_rows;
3781 if (plan->plan_rows > 0)
3782 plan->plan_width = rint(total_size / plan->plan_rows);
3784 plan->plan_width = 0;
3786 node->plan.lefttree = NULL;
3787 node->plan.righttree = NULL;
3788 node->plan.qual = NIL;
3791 * Set up the visible plan targetlist as being the same as the first
3792 * RETURNING list. This is for the use of EXPLAIN; the executor won't
3793 * pay any attention to the targetlist.
3796 node->plan.targetlist = copyObject(linitial(returningLists));
3798 node->plan.targetlist = NIL;
3800 node->operation = operation;
3801 node->resultRelations = resultRelations;
3802 node->plans = subplans;
3803 node->returningLists = returningLists;
3809 * is_projection_capable_plan
3810 * Check whether a given Plan node is able to do projection.
3813 is_projection_capable_plan(Plan *plan)
3815 /* Most plan types can project, so just list the ones that can't */
3816 switch (nodeTag(plan))
3827 case T_RecursiveUnion: