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-2005, 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.191 2005/06/05 22:32:55 tgl Exp $
15 *-------------------------------------------------------------------------
21 #include "nodes/makefuncs.h"
22 #include "nodes/nodeFuncs.h"
23 #include "optimizer/clauses.h"
24 #include "optimizer/cost.h"
25 #include "optimizer/paths.h"
26 #include "optimizer/plancat.h"
27 #include "optimizer/planmain.h"
28 #include "optimizer/restrictinfo.h"
29 #include "optimizer/tlist.h"
30 #include "optimizer/var.h"
31 #include "parser/parsetree.h"
32 #include "parser/parse_clause.h"
33 #include "parser/parse_expr.h"
34 #include "utils/lsyscache.h"
35 #include "utils/syscache.h"
38 static Scan *create_scan_plan(PlannerInfo *root, Path *best_path);
39 static List *build_relation_tlist(RelOptInfo *rel);
40 static bool use_physical_tlist(RelOptInfo *rel);
41 static void disuse_physical_tlist(Plan *plan, Path *path);
42 static Join *create_join_plan(PlannerInfo *root, JoinPath *best_path);
43 static Append *create_append_plan(PlannerInfo *root, AppendPath *best_path);
44 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
45 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
46 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
47 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
48 List *tlist, List *scan_clauses);
49 static IndexScan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
50 List *tlist, List *scan_clauses,
51 List **nonlossy_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 NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
64 Plan *outer_plan, Plan *inner_plan);
65 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
66 Plan *outer_plan, Plan *inner_plan);
67 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
68 Plan *outer_plan, Plan *inner_plan);
69 static void fix_indexqual_references(List *indexquals, IndexPath *index_path,
70 List **fixed_indexquals,
71 List **nonlossy_indexquals,
74 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index,
76 static List *get_switched_clauses(List *clauses, Relids outerrelids);
77 static void copy_path_costsize(Plan *dest, Path *src);
78 static void copy_plan_costsize(Plan *dest, Plan *src);
79 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
80 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
81 Oid indexid, List *indexqual, List *indexqualorig,
82 List *indexstrategy, List *indexsubtype,
83 ScanDirection indexscandir);
84 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
89 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
94 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
96 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
98 static BitmapAnd *make_bitmap_and(List *bitmapplans);
99 static BitmapOr *make_bitmap_or(List *bitmapplans);
100 static NestLoop *make_nestloop(List *tlist,
101 List *joinclauses, List *otherclauses,
102 Plan *lefttree, Plan *righttree,
104 static HashJoin *make_hashjoin(List *tlist,
105 List *joinclauses, List *otherclauses,
107 Plan *lefttree, Plan *righttree,
109 static Hash *make_hash(Plan *lefttree);
110 static MergeJoin *make_mergejoin(List *tlist,
111 List *joinclauses, List *otherclauses,
113 Plan *lefttree, Plan *righttree,
115 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
116 AttrNumber *sortColIdx, Oid *sortOperators);
117 static Sort *make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree,
123 * Creates the access plan for a query by tracing backwards through the
124 * desired chain of pathnodes, starting at the node 'best_path'. For
125 * every pathnode found:
126 * (1) Create a corresponding plan node containing appropriate id,
127 * target list, and qualification information.
128 * (2) Modify qual clauses of join nodes so that subplan attributes are
129 * referenced using relative values.
130 * (3) Target lists are not modified, but will be in setrefs.c.
132 * best_path is the best access path
134 * Returns a Plan tree.
137 create_plan(PlannerInfo *root, Path *best_path)
141 switch (best_path->pathtype)
145 case T_BitmapHeapScan:
149 plan = (Plan *) create_scan_plan(root, best_path);
154 plan = (Plan *) create_join_plan(root,
155 (JoinPath *) best_path);
158 plan = (Plan *) create_append_plan(root,
159 (AppendPath *) best_path);
162 plan = (Plan *) create_result_plan(root,
163 (ResultPath *) best_path);
166 plan = (Plan *) create_material_plan(root,
167 (MaterialPath *) best_path);
170 plan = (Plan *) create_unique_plan(root,
171 (UniquePath *) best_path);
174 elog(ERROR, "unrecognized node type: %d",
175 (int) best_path->pathtype);
176 plan = NULL; /* keep compiler quiet */
185 * Create a scan plan for the parent relation of 'best_path'.
187 * Returns a Plan node.
190 create_scan_plan(PlannerInfo *root, Path *best_path)
192 RelOptInfo *rel = best_path->parent;
198 * For table scans, rather than using the relation targetlist (which
199 * is only those Vars actually needed by the query), we prefer to
200 * generate a tlist containing all Vars in order. This will allow the
201 * executor to optimize away projection of the table tuples, if
202 * possible. (Note that planner.c may replace the tlist we generate
203 * here, forcing projection to occur.)
205 if (use_physical_tlist(rel))
207 tlist = build_physical_tlist(root, rel);
208 /* if fail because of dropped cols, use regular method */
210 tlist = build_relation_tlist(rel);
213 tlist = build_relation_tlist(rel);
216 * Extract the relevant restriction clauses from the parent relation;
217 * the executor must apply all these restrictions during the scan.
219 scan_clauses = rel->baserestrictinfo;
221 switch (best_path->pathtype)
224 plan = (Scan *) create_seqscan_plan(root,
231 plan = (Scan *) create_indexscan_plan(root,
232 (IndexPath *) best_path,
238 case T_BitmapHeapScan:
239 plan = (Scan *) create_bitmap_scan_plan(root,
240 (BitmapHeapPath *) best_path,
246 plan = (Scan *) create_tidscan_plan(root,
247 (TidPath *) best_path,
253 plan = (Scan *) create_subqueryscan_plan(root,
260 plan = (Scan *) create_functionscan_plan(root,
267 elog(ERROR, "unrecognized node type: %d",
268 (int) best_path->pathtype);
269 plan = NULL; /* keep compiler quiet */
277 * Build a target list (ie, a list of TargetEntry) for a relation.
280 build_relation_tlist(RelOptInfo *rel)
286 foreach(v, rel->reltargetlist)
288 /* Do we really need to copy here? Not sure */
289 Var *var = (Var *) copyObject(lfirst(v));
291 tlist = lappend(tlist, makeTargetEntry((Expr *) var,
302 * Decide whether to use a tlist matching relation structure,
303 * rather than only those Vars actually referenced.
306 use_physical_tlist(RelOptInfo *rel)
311 * OK for subquery and function scans; otherwise, can't do it for
312 * anything except real relations.
314 if (rel->rtekind != RTE_RELATION)
316 if (rel->rtekind == RTE_SUBQUERY)
318 if (rel->rtekind == RTE_FUNCTION)
324 * Can't do it with inheritance cases either (mainly because Append
327 if (rel->reloptkind != RELOPT_BASEREL)
331 * Can't do it if any system columns are requested, either. (This
332 * could possibly be fixed but would take some fragile assumptions in
333 * setrefs.c, I think.)
335 for (i = rel->min_attr; i <= 0; i++)
337 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
344 * disuse_physical_tlist
345 * Switch a plan node back to emitting only Vars actually referenced.
347 * If the plan node immediately above a scan would prefer to get only
348 * needed Vars and not a physical tlist, it must call this routine to
349 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
350 * and Material nodes want this, so they don't have to store useless columns.
353 disuse_physical_tlist(Plan *plan, Path *path)
355 /* Only need to undo it for path types handled by create_scan_plan() */
356 switch (path->pathtype)
360 case T_BitmapHeapScan:
364 plan->targetlist = build_relation_tlist(path->parent);
373 * Create a join plan for 'best_path' and (recursively) plans for its
374 * inner and outer paths.
376 * Returns a Plan node.
379 create_join_plan(PlannerInfo *root, JoinPath *best_path)
385 outer_plan = create_plan(root, best_path->outerjoinpath);
386 inner_plan = create_plan(root, best_path->innerjoinpath);
388 switch (best_path->path.pathtype)
391 plan = (Join *) create_mergejoin_plan(root,
392 (MergePath *) best_path,
397 plan = (Join *) create_hashjoin_plan(root,
398 (HashPath *) best_path,
403 plan = (Join *) create_nestloop_plan(root,
404 (NestPath *) best_path,
409 elog(ERROR, "unrecognized node type: %d",
410 (int) best_path->path.pathtype);
411 plan = NULL; /* keep compiler quiet */
418 * * Expensive function pullups may have pulled local predicates *
419 * into this path node. Put them in the qpqual of the plan node. *
422 if (get_loc_restrictinfo(best_path) != NIL)
423 set_qpqual((Plan) plan,
424 list_concat(get_qpqual((Plan) plan),
425 get_actual_clauses(get_loc_restrictinfo(best_path))));
433 * Create an Append plan for 'best_path' and (recursively) plans
436 * Returns a Plan node.
439 create_append_plan(PlannerInfo *root, AppendPath *best_path)
442 List *tlist = build_relation_tlist(best_path->path.parent);
443 List *subplans = NIL;
446 foreach(subpaths, best_path->subpaths)
448 Path *subpath = (Path *) lfirst(subpaths);
450 subplans = lappend(subplans, create_plan(root, subpath));
453 plan = make_append(subplans, false, tlist);
460 * Create a Result plan for 'best_path' and (recursively) plans
463 * Returns a Plan node.
466 create_result_plan(PlannerInfo *root, ResultPath *best_path)
473 if (best_path->path.parent)
474 tlist = build_relation_tlist(best_path->path.parent);
476 tlist = NIL; /* will be filled in later */
478 if (best_path->subpath)
479 subplan = create_plan(root, best_path->subpath);
483 constclauses = order_qual_clauses(root, best_path->constantqual);
485 plan = make_result(tlist, (Node *) constclauses, subplan);
491 * create_material_plan
492 * Create a Material plan for 'best_path' and (recursively) plans
495 * Returns a Plan node.
498 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
503 subplan = create_plan(root, best_path->subpath);
505 /* We don't want any excess columns in the materialized tuples */
506 disuse_physical_tlist(subplan, best_path->subpath);
508 plan = make_material(subplan);
510 copy_path_costsize(&plan->plan, (Path *) best_path);
517 * Create a Unique plan for 'best_path' and (recursively) plans
520 * Returns a Plan node.
523 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
529 AttrNumber *groupColIdx;
536 subplan = create_plan(root, best_path->subpath);
539 * As constructed, the subplan has a "flat" tlist containing just the
540 * Vars needed here and at upper levels. The values we are supposed
541 * to unique-ify may be expressions in these variables. We have to
542 * add any such expressions to the subplan's tlist. We then build
543 * control information showing which subplan output columns are to be
544 * examined by the grouping step. (Since we do not remove any
545 * existing subplan outputs, not all the output columns may be used
548 * Note: the reason we don't remove any subplan outputs is that there
549 * are scenarios where a Var is needed at higher levels even though
550 * it is not one of the nominal outputs of an IN clause. Consider
551 * WHERE x IN (SELECT y FROM t1,t2 WHERE y = z)
552 * Implied equality deduction will generate an "x = z" clause, which may
553 * get used instead of "x = y" in the upper join step. Therefore the
554 * sub-select had better deliver both y and z in its targetlist.
555 * It is sufficient to unique-ify on y, however.
557 * To find the correct list of values to unique-ify, we look in the
558 * information saved for IN expressions. If this code is ever used in
559 * other scenarios, some other way of finding what to unique-ify will
563 uniq_exprs = NIL; /* just to keep compiler quiet */
564 foreach(l, root->in_info_list)
566 InClauseInfo *ininfo = (InClauseInfo *) lfirst(l);
568 if (bms_equal(ininfo->righthand, best_path->path.parent->relids))
570 uniq_exprs = ininfo->sub_targetlist;
574 if (l == NULL) /* fell out of loop? */
575 elog(ERROR, "could not find UniquePath in in_info_list");
577 /* set up to record positions of unique columns */
578 numGroupCols = list_length(uniq_exprs);
579 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
581 /* not sure if tlist might be shared with other nodes, so copy */
582 newtlist = copyObject(subplan->targetlist);
583 nextresno = list_length(newtlist) + 1;
586 foreach(l, uniq_exprs)
588 Node *uniqexpr = lfirst(l);
591 tle = tlist_member(uniqexpr, newtlist);
594 tle = makeTargetEntry((Expr *) uniqexpr,
598 newtlist = lappend(newtlist, tle);
602 groupColIdx[groupColPos++] = tle->resno;
608 * If the top plan node can't do projections, we need to add a
609 * Result node to help it along.
611 if (!is_projection_capable_plan(subplan))
612 subplan = (Plan *) make_result(newtlist, NULL, subplan);
614 subplan->targetlist = newtlist;
617 /* Done if we don't need to do any actual unique-ifying */
618 if (best_path->umethod == UNIQUE_PATH_NOOP)
621 if (best_path->umethod == UNIQUE_PATH_HASH)
625 numGroups = (long) Min(best_path->rows, (double) LONG_MAX);
627 plan = (Plan *) make_agg(root,
628 copyObject(subplan->targetlist),
639 List *sortList = NIL;
641 for (groupColPos = 0; groupColPos < numGroupCols; groupColPos++)
645 tle = get_tle_by_resno(subplan->targetlist,
646 groupColIdx[groupColPos]);
648 sortList = addTargetToSortList(NULL, tle,
649 sortList, subplan->targetlist,
650 SORTBY_ASC, NIL, false);
652 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
653 plan = (Plan *) make_unique(plan, sortList);
656 /* Adjust output size estimate (other fields should be OK already) */
657 plan->plan_rows = best_path->rows;
663 /*****************************************************************************
665 * BASE-RELATION SCAN METHODS
667 *****************************************************************************/
671 * create_seqscan_plan
672 * Returns a seqscan plan for the base relation scanned by 'best_path'
673 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
676 create_seqscan_plan(PlannerInfo *root, Path *best_path,
677 List *tlist, List *scan_clauses)
680 Index scan_relid = best_path->parent->relid;
682 /* it should be a base rel... */
683 Assert(scan_relid > 0);
684 Assert(best_path->parent->rtekind == RTE_RELATION);
686 /* Reduce RestrictInfo list to bare expressions */
687 scan_clauses = get_actual_clauses(scan_clauses);
689 /* Sort clauses into best execution order */
690 scan_clauses = order_qual_clauses(root, scan_clauses);
692 scan_plan = make_seqscan(tlist,
696 copy_path_costsize(&scan_plan->plan, best_path);
702 * create_indexscan_plan
703 * Returns an indexscan plan for the base relation scanned by 'best_path'
704 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
706 * The indexquals list of the path contains implicitly-ANDed qual conditions.
707 * The list can be empty --- then no index restrictions will be applied during
710 * If nonlossy_clauses isn't NULL, *nonlossy_clauses receives a list of the
711 * nonlossy indexquals.
714 create_indexscan_plan(PlannerInfo *root,
715 IndexPath *best_path,
718 List **nonlossy_clauses)
720 List *indexquals = best_path->indexquals;
721 Index baserelid = best_path->path.parent->relid;
722 Oid indexoid = best_path->indexinfo->indexoid;
724 List *stripped_indexquals;
725 List *fixed_indexquals;
726 List *nonlossy_indexquals;
730 IndexScan *scan_plan;
732 /* it should be a base rel... */
733 Assert(baserelid > 0);
734 Assert(best_path->path.parent->rtekind == RTE_RELATION);
737 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
738 * executor as indexqualorig
740 stripped_indexquals = get_actual_clauses(indexquals);
743 * The executor needs a copy with the indexkey on the left of each
744 * clause and with index attr numbers substituted for table ones. This
745 * pass also gets strategy info and looks for "lossy" operators.
747 fix_indexqual_references(indexquals, best_path,
749 &nonlossy_indexquals,
753 /* pass back nonlossy quals if caller wants 'em */
754 if (nonlossy_clauses)
755 *nonlossy_clauses = nonlossy_indexquals;
758 * If this is an innerjoin scan, the indexclauses will contain join
759 * clauses that are not present in scan_clauses (since the passed-in
760 * value is just the rel's baserestrictinfo list). We must add these
761 * clauses to scan_clauses to ensure they get checked. In most cases
762 * we will remove the join clauses again below, but if a join clause
763 * contains a special operator, we need to make sure it gets into the
766 * Note: pointer comparison should be enough to determine RestrictInfo
769 if (best_path->isjoininner)
770 scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
773 * The qpqual list must contain all restrictions not automatically
774 * handled by the index. All the predicates in the indexquals will be
775 * checked (either by the index itself, or by nodeIndexscan.c), but if
776 * there are any "special" operators involved then they must be included
777 * in qpqual. Also, any lossy index operators must be rechecked in
778 * the qpqual. The upshot is that qpqual must contain scan_clauses
779 * minus whatever appears in nonlossy_indexquals.
781 * In normal cases simple pointer equality checks will be enough to
782 * spot duplicate RestrictInfos, so we try that first. In some situations
783 * (particularly with OR'd index conditions) we may have scan_clauses
784 * that are not equal to, but are logically implied by, the index quals;
785 * so we also try a pred_test() check to see if we can discard quals
788 * While at it, we strip off the RestrictInfos to produce a list of
792 foreach(l, scan_clauses)
794 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
796 Assert(IsA(rinfo, RestrictInfo));
797 if (list_member_ptr(nonlossy_indexquals, rinfo))
799 if (pred_test(list_make1(rinfo->clause), nonlossy_indexquals))
801 qpqual = lappend(qpqual, rinfo->clause);
804 /* Sort clauses into best execution order */
805 qpqual = order_qual_clauses(root, qpqual);
807 /* Finally ready to build the plan node */
808 scan_plan = make_indexscan(tlist,
816 best_path->indexscandir);
818 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
819 /* use the indexscan-specific rows estimate, not the parent rel's */
820 scan_plan->scan.plan.plan_rows = best_path->rows;
826 * create_bitmap_scan_plan
827 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
828 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
830 static BitmapHeapScan *
831 create_bitmap_scan_plan(PlannerInfo *root,
832 BitmapHeapPath *best_path,
836 Index baserelid = best_path->path.parent->relid;
837 Plan *bitmapqualplan;
838 List *bitmapqualorig;
842 BitmapHeapScan *scan_plan;
844 /* it should be a base rel... */
845 Assert(baserelid > 0);
846 Assert(best_path->path.parent->rtekind == RTE_RELATION);
848 /* Process the bitmapqual tree into a Plan tree and qual lists */
849 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
850 &bitmapqualorig, &indexquals);
852 /* Reduce RestrictInfo list to bare expressions */
853 scan_clauses = get_actual_clauses(scan_clauses);
856 * If this is a innerjoin scan, the indexclauses will contain join
857 * clauses that are not present in scan_clauses (since the passed-in
858 * value is just the rel's baserestrictinfo list). We must add these
859 * clauses to scan_clauses to ensure they get checked. In most cases
860 * we will remove the join clauses again below, but if a join clause
861 * contains a special operator, we need to make sure it gets into the
864 if (best_path->isjoininner)
866 scan_clauses = list_union(scan_clauses, bitmapqualorig);
870 * The qpqual list must contain all restrictions not automatically
871 * handled by the index. All the predicates in the indexquals will be
872 * checked (either by the index itself, or by nodeBitmapHeapscan.c),
873 * but if there are any "special" or lossy operators involved then they
874 * must be added to qpqual. The upshot is that qpquals must contain
875 * scan_clauses minus whatever appears in indexquals.
877 * In normal cases simple equal() checks will be enough to spot duplicate
878 * clauses, so we try that first. In some situations (particularly with
879 * OR'd index conditions) we may have scan_clauses that are not equal to,
880 * but are logically implied by, the index quals; so we also try a
881 * pred_test() check to see if we can discard quals that way.
884 foreach(l, scan_clauses)
886 Node *clause = (Node *) lfirst(l);
888 if (list_member(indexquals, clause))
890 if (pred_test(list_make1(clause), indexquals))
892 qpqual = lappend(qpqual, clause);
895 /* Sort clauses into best execution order */
896 qpqual = order_qual_clauses(root, qpqual);
899 * When dealing with special or lossy operators, we will at this point
900 * have duplicate clauses in qpqual and bitmapqualorig. We may as well
901 * drop 'em from bitmapqualorig, since there's no point in making the
904 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
906 /* Finally ready to build the plan node */
907 scan_plan = make_bitmap_heapscan(tlist,
913 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
914 /* use the indexscan-specific rows estimate, not the parent rel's */
915 scan_plan->scan.plan.plan_rows = best_path->rows;
921 * Given a bitmapqual tree, generate the Plan tree that implements it
923 * As byproducts, we also return in *qual and *indexqual the qual lists
924 * (in implicit-AND form, without RestrictInfos) describing the original index
925 * conditions and the generated indexqual conditions. The latter is made to
926 * exclude lossy index operators.
929 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
930 List **qual, List **indexqual)
934 if (IsA(bitmapqual, BitmapAndPath))
936 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
937 List *subplans = NIL;
938 List *subquals = NIL;
939 List *subindexquals = NIL;
942 foreach(l, apath->bitmapquals)
948 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
949 &subqual, &subindexqual);
950 subplans = lappend(subplans, subplan);
951 subquals = list_concat(subquals, subqual);
952 subindexquals = list_concat(subindexquals, subindexqual);
954 plan = (Plan *) make_bitmap_and(subplans);
955 plan->startup_cost = apath->path.startup_cost;
956 plan->total_cost = apath->path.total_cost;
958 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
959 plan->plan_width = 0; /* meaningless */
961 *indexqual = subindexquals;
963 else if (IsA(bitmapqual, BitmapOrPath))
965 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
966 List *subplans = NIL;
967 List *subquals = NIL;
968 List *subindexquals = NIL;
971 foreach(l, opath->bitmapquals)
977 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
978 &subqual, &subindexqual);
979 subplans = lappend(subplans, subplan);
980 subquals = lappend(subquals,
981 make_ands_explicit(subqual));
982 subindexquals = lappend(subindexquals,
983 make_ands_explicit(subindexqual));
985 plan = (Plan *) make_bitmap_or(subplans);
986 plan->startup_cost = opath->path.startup_cost;
987 plan->total_cost = opath->path.total_cost;
989 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
990 plan->plan_width = 0; /* meaningless */
991 *qual = list_make1(make_orclause(subquals));
992 *indexqual = list_make1(make_orclause(subindexquals));
994 else if (IsA(bitmapqual, IndexPath))
996 IndexPath *ipath = (IndexPath *) bitmapqual;
998 List *nonlossy_clauses;
1000 /* Use the regular indexscan plan build machinery... */
1001 iscan = create_indexscan_plan(root, ipath, NIL, NIL,
1003 /* then convert to a bitmap indexscan */
1004 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1007 iscan->indexqualorig,
1008 iscan->indexstrategy,
1009 iscan->indexsubtype);
1010 plan->startup_cost = 0.0;
1011 plan->total_cost = ipath->indextotalcost;
1013 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1014 plan->plan_width = 0; /* meaningless */
1015 *qual = get_actual_clauses(ipath->indexclauses);
1016 *indexqual = get_actual_clauses(nonlossy_clauses);
1020 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1021 plan = NULL; /* keep compiler quiet */
1028 * create_tidscan_plan
1029 * Returns a tidscan plan for the base relation scanned by 'best_path'
1030 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1033 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1034 List *tlist, List *scan_clauses)
1037 Index scan_relid = best_path->path.parent->relid;
1039 /* it should be a base rel... */
1040 Assert(scan_relid > 0);
1041 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1043 /* Reduce RestrictInfo list to bare expressions */
1044 scan_clauses = get_actual_clauses(scan_clauses);
1046 /* Sort clauses into best execution order */
1047 scan_clauses = order_qual_clauses(root, scan_clauses);
1049 scan_plan = make_tidscan(tlist,
1052 best_path->tideval);
1054 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1060 * create_subqueryscan_plan
1061 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1062 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1064 static SubqueryScan *
1065 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1066 List *tlist, List *scan_clauses)
1068 SubqueryScan *scan_plan;
1069 Index scan_relid = best_path->parent->relid;
1071 /* it should be a subquery base rel... */
1072 Assert(scan_relid > 0);
1073 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1075 /* Reduce RestrictInfo list to bare expressions */
1076 scan_clauses = get_actual_clauses(scan_clauses);
1078 /* Sort clauses into best execution order */
1079 scan_clauses = order_qual_clauses(root, scan_clauses);
1081 scan_plan = make_subqueryscan(tlist,
1084 best_path->parent->subplan);
1086 copy_path_costsize(&scan_plan->scan.plan, best_path);
1092 * create_functionscan_plan
1093 * Returns a functionscan plan for the base relation scanned by 'best_path'
1094 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1096 static FunctionScan *
1097 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1098 List *tlist, List *scan_clauses)
1100 FunctionScan *scan_plan;
1101 Index scan_relid = best_path->parent->relid;
1103 /* it should be a function base rel... */
1104 Assert(scan_relid > 0);
1105 Assert(best_path->parent->rtekind == RTE_FUNCTION);
1107 /* Reduce RestrictInfo list to bare expressions */
1108 scan_clauses = get_actual_clauses(scan_clauses);
1110 /* Sort clauses into best execution order */
1111 scan_clauses = order_qual_clauses(root, scan_clauses);
1113 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid);
1115 copy_path_costsize(&scan_plan->scan.plan, best_path);
1120 /*****************************************************************************
1124 *****************************************************************************/
1127 create_nestloop_plan(PlannerInfo *root,
1128 NestPath *best_path,
1132 List *tlist = build_relation_tlist(best_path->path.parent);
1133 List *joinrestrictclauses = best_path->joinrestrictinfo;
1136 NestLoop *join_plan;
1138 if (IsA(best_path->innerjoinpath, IndexPath))
1141 * An index is being used to reduce the number of tuples scanned
1142 * in the inner relation. If there are join clauses being used
1143 * with the index, we may remove those join clauses from the list
1144 * of clauses that have to be checked as qpquals at the join node.
1146 * We can also remove any join clauses that are redundant with those
1147 * being used in the index scan; prior redundancy checks will not
1148 * have caught this case because the join clauses would never have
1149 * been put in the same joininfo list.
1151 * We can skip this if the index path is an ordinary indexpath and
1152 * not a special innerjoin path.
1154 IndexPath *innerpath = (IndexPath *) best_path->innerjoinpath;
1156 if (innerpath->isjoininner)
1158 joinrestrictclauses =
1159 select_nonredundant_join_clauses(root,
1160 joinrestrictclauses,
1161 innerpath->indexclauses,
1162 IS_OUTER_JOIN(best_path->jointype));
1165 else if (IsA(best_path->innerjoinpath, BitmapHeapPath))
1168 * Same deal for bitmapped index scans.
1170 BitmapHeapPath *innerpath = (BitmapHeapPath *) best_path->innerjoinpath;
1172 if (innerpath->isjoininner)
1174 List *bitmapclauses;
1177 make_restrictinfo_from_bitmapqual(innerpath->bitmapqual,
1179 joinrestrictclauses =
1180 select_nonredundant_join_clauses(root,
1181 joinrestrictclauses,
1183 IS_OUTER_JOIN(best_path->jointype));
1187 /* Get the join qual clauses (in plain expression form) */
1188 if (IS_OUTER_JOIN(best_path->jointype))
1190 get_actual_join_clauses(joinrestrictclauses,
1191 &joinclauses, &otherclauses);
1195 /* We can treat all clauses alike for an inner join */
1196 joinclauses = get_actual_clauses(joinrestrictclauses);
1200 /* Sort clauses into best execution order */
1201 joinclauses = order_qual_clauses(root, joinclauses);
1202 otherclauses = order_qual_clauses(root, otherclauses);
1204 join_plan = make_nestloop(tlist,
1209 best_path->jointype);
1211 copy_path_costsize(&join_plan->join.plan, &best_path->path);
1217 create_mergejoin_plan(PlannerInfo *root,
1218 MergePath *best_path,
1222 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1226 MergeJoin *join_plan;
1228 /* Get the join qual clauses (in plain expression form) */
1229 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1231 get_actual_join_clauses(best_path->jpath.joinrestrictinfo,
1232 &joinclauses, &otherclauses);
1236 /* We can treat all clauses alike for an inner join */
1237 joinclauses = get_actual_clauses(best_path->jpath.joinrestrictinfo);
1242 * Remove the mergeclauses from the list of join qual clauses, leaving
1243 * the list of quals that must be checked as qpquals.
1245 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
1246 joinclauses = list_difference(joinclauses, mergeclauses);
1249 * Rearrange mergeclauses, if needed, so that the outer variable is
1250 * always on the left.
1252 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
1253 best_path->jpath.outerjoinpath->parent->relids);
1255 /* Sort clauses into best execution order */
1256 /* NB: do NOT reorder the mergeclauses */
1257 joinclauses = order_qual_clauses(root, joinclauses);
1258 otherclauses = order_qual_clauses(root, otherclauses);
1261 * Create explicit sort nodes for the outer and inner join paths if
1262 * necessary. The sort cost was already accounted for in the path.
1263 * Make sure there are no excess columns in the inputs if sorting.
1265 if (best_path->outersortkeys)
1267 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1268 outer_plan = (Plan *)
1269 make_sort_from_pathkeys(root,
1271 best_path->outersortkeys);
1274 if (best_path->innersortkeys)
1276 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1277 inner_plan = (Plan *)
1278 make_sort_from_pathkeys(root,
1280 best_path->innersortkeys);
1284 * Now we can build the mergejoin node.
1286 join_plan = make_mergejoin(tlist,
1292 best_path->jpath.jointype);
1294 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1300 create_hashjoin_plan(PlannerInfo *root,
1301 HashPath *best_path,
1305 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1309 HashJoin *join_plan;
1312 /* Get the join qual clauses (in plain expression form) */
1313 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1315 get_actual_join_clauses(best_path->jpath.joinrestrictinfo,
1316 &joinclauses, &otherclauses);
1320 /* We can treat all clauses alike for an inner join */
1321 joinclauses = get_actual_clauses(best_path->jpath.joinrestrictinfo);
1326 * Remove the hashclauses from the list of join qual clauses, leaving
1327 * the list of quals that must be checked as qpquals.
1329 hashclauses = get_actual_clauses(best_path->path_hashclauses);
1330 joinclauses = list_difference(joinclauses, hashclauses);
1333 * Rearrange hashclauses, if needed, so that the outer variable is
1334 * always on the left.
1336 hashclauses = get_switched_clauses(best_path->path_hashclauses,
1337 best_path->jpath.outerjoinpath->parent->relids);
1339 /* Sort clauses into best execution order */
1340 joinclauses = order_qual_clauses(root, joinclauses);
1341 otherclauses = order_qual_clauses(root, otherclauses);
1342 hashclauses = order_qual_clauses(root, hashclauses);
1344 /* We don't want any excess columns in the hashed tuples */
1345 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1348 * Build the hash node and hash join node.
1350 hash_plan = make_hash(inner_plan);
1351 join_plan = make_hashjoin(tlist,
1357 best_path->jpath.jointype);
1359 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1365 /*****************************************************************************
1367 * SUPPORTING ROUTINES
1369 *****************************************************************************/
1372 * fix_indexqual_references
1373 * Adjust indexqual clauses to the form the executor's indexqual
1374 * machinery needs, and check for recheckable (lossy) index conditions.
1376 * We have five tasks here:
1377 * * Remove RestrictInfo nodes from the input clauses.
1378 * * Index keys must be represented by Var nodes with varattno set to the
1379 * index's attribute number, not the attribute number in the original rel.
1380 * * If the index key is on the right, commute the clause to put it on the
1382 * * We must construct lists of operator strategy numbers and subtypes
1383 * for the top-level operators of each index clause.
1384 * * We must detect any lossy index operators. The API is that we return
1385 * a list of the input clauses whose operators are NOT lossy.
1387 * fixed_indexquals receives a modified copy of the indexquals list --- the
1388 * original is not changed. Note also that the copy shares no substructure
1389 * with the original; this is needed in case there is a subplan in it (we need
1390 * two separate copies of the subplan tree, or things will go awry).
1392 * nonlossy_indexquals receives a list of the original input clauses (with
1393 * RestrictInfos) that contain non-lossy operators.
1395 * indexstrategy receives an integer list of strategy numbers.
1396 * indexsubtype receives an OID list of strategy subtypes.
1399 fix_indexqual_references(List *indexquals, IndexPath *index_path,
1400 List **fixed_indexquals,
1401 List **nonlossy_indexquals,
1402 List **indexstrategy,
1403 List **indexsubtype)
1405 IndexOptInfo *index = index_path->indexinfo;
1408 *fixed_indexquals = NIL;
1409 *nonlossy_indexquals = NIL;
1410 *indexstrategy = NIL;
1411 *indexsubtype = NIL;
1414 * For each qual clause, commute if needed to put the indexkey operand on
1415 * the left, and then fix its varattno. (We do not need to change the
1416 * other side of the clause.) Then determine the operator's strategy
1417 * number and subtype number, and check for lossy index behavior.
1419 foreach(l, indexquals)
1421 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1429 Assert(IsA(rinfo, RestrictInfo));
1430 clause = (OpExpr *) rinfo->clause;
1431 if (!IsA(clause, OpExpr) ||
1432 list_length(clause->args) != 2)
1433 elog(ERROR, "indexqual clause is not binary opclause");
1436 * Make a copy that will become the fixed clause.
1438 * We used to try to do a shallow copy here, but that fails if there
1439 * is a subplan in the arguments of the opclause. So just do a
1442 newclause = (OpExpr *) copyObject((Node *) clause);
1445 * Check to see if the indexkey is on the right; if so, commute
1446 * the clause. The indexkey should be the side that refers to
1447 * (only) the base relation.
1449 if (!bms_equal(rinfo->left_relids, index->rel->relids))
1450 CommuteClause(newclause);
1453 * Now, determine which index attribute this is, change the
1454 * indexkey operand as needed, and get the index opclass.
1456 linitial(newclause->args) =
1457 fix_indexqual_operand(linitial(newclause->args),
1461 *fixed_indexquals = lappend(*fixed_indexquals, newclause);
1464 * Look up the (possibly commuted) operator in the operator class
1465 * to get its strategy numbers and the recheck indicator. This
1466 * also double-checks that we found an operator matching the
1469 get_op_opclass_properties(newclause->opno, opclass,
1470 &stratno, &stratsubtype, &recheck);
1472 *indexstrategy = lappend_int(*indexstrategy, stratno);
1473 *indexsubtype = lappend_oid(*indexsubtype, stratsubtype);
1475 /* If it's not lossy, add to nonlossy_indexquals */
1477 *nonlossy_indexquals = lappend(*nonlossy_indexquals, rinfo);
1482 fix_indexqual_operand(Node *node, IndexOptInfo *index, Oid *opclass)
1485 * We represent index keys by Var nodes having the varno of the base
1486 * table but varattno equal to the index's attribute number (index
1487 * column position). This is a bit hokey ... would be cleaner to use
1488 * a special-purpose node type that could not be mistaken for a
1489 * regular Var. But it will do for now.
1493 ListCell *indexpr_item;
1496 * Remove any binary-compatible relabeling of the indexkey
1498 if (IsA(node, RelabelType))
1499 node = (Node *) ((RelabelType *) node)->arg;
1501 if (IsA(node, Var) &&
1502 ((Var *) node)->varno == index->rel->relid)
1504 /* Try to match against simple index columns */
1505 int varatt = ((Var *) node)->varattno;
1509 for (pos = 0; pos < index->ncolumns; pos++)
1511 if (index->indexkeys[pos] == varatt)
1513 result = (Var *) copyObject(node);
1514 result->varattno = pos + 1;
1515 /* return the correct opclass, too */
1516 *opclass = index->classlist[pos];
1517 return (Node *) result;
1523 /* Try to match against index expressions */
1524 indexpr_item = list_head(index->indexprs);
1525 for (pos = 0; pos < index->ncolumns; pos++)
1527 if (index->indexkeys[pos] == 0)
1531 if (indexpr_item == NULL)
1532 elog(ERROR, "too few entries in indexprs list");
1533 indexkey = (Node *) lfirst(indexpr_item);
1534 if (indexkey && IsA(indexkey, RelabelType))
1535 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
1536 if (equal(node, indexkey))
1539 result = makeVar(index->rel->relid, pos + 1,
1540 exprType(lfirst(indexpr_item)), -1,
1542 /* return the correct opclass, too */
1543 *opclass = index->classlist[pos];
1544 return (Node *) result;
1546 indexpr_item = lnext(indexpr_item);
1551 elog(ERROR, "node is not an index attribute");
1552 return NULL; /* keep compiler quiet */
1556 * get_switched_clauses
1557 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
1558 * extract the bare clauses, and rearrange the elements within the
1559 * clauses, if needed, so the outer join variable is on the left and
1560 * the inner is on the right. The original data structure is not touched;
1561 * a modified list is returned.
1564 get_switched_clauses(List *clauses, Relids outerrelids)
1571 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
1572 OpExpr *clause = (OpExpr *) restrictinfo->clause;
1574 Assert(is_opclause(clause));
1575 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
1578 * Duplicate just enough of the structure to allow commuting
1579 * the clause without changing the original list. Could use
1580 * copyObject, but a complete deep copy is overkill.
1582 OpExpr *temp = makeNode(OpExpr);
1584 temp->opno = clause->opno;
1585 temp->opfuncid = InvalidOid;
1586 temp->opresulttype = clause->opresulttype;
1587 temp->opretset = clause->opretset;
1588 temp->args = list_copy(clause->args);
1589 /* Commute it --- note this modifies the temp node in-place. */
1590 CommuteClause(temp);
1591 t_list = lappend(t_list, temp);
1594 t_list = lappend(t_list, clause);
1600 * order_qual_clauses
1601 * Given a list of qual clauses that will all be evaluated at the same
1602 * plan node, sort the list into the order we want to check the quals
1605 * Ideally the order should be driven by a combination of execution cost and
1606 * selectivity, but unfortunately we have so little information about
1607 * execution cost of operators that it's really hard to do anything smart.
1608 * For now, we just move any quals that contain SubPlan references (but not
1609 * InitPlan references) to the end of the list.
1612 order_qual_clauses(PlannerInfo *root, List *clauses)
1618 /* No need to work hard if the query is subselect-free */
1619 if (!root->parse->hasSubLinks)
1626 Node *clause = (Node *) lfirst(l);
1628 if (contain_subplans(clause))
1629 withsubplans = lappend(withsubplans, clause);
1631 nosubplans = lappend(nosubplans, clause);
1634 return list_concat(nosubplans, withsubplans);
1638 * Copy cost and size info from a Path node to the Plan node created from it.
1639 * The executor won't use this info, but it's needed by EXPLAIN.
1642 copy_path_costsize(Plan *dest, Path *src)
1646 dest->startup_cost = src->startup_cost;
1647 dest->total_cost = src->total_cost;
1648 dest->plan_rows = src->parent->rows;
1649 dest->plan_width = src->parent->width;
1653 dest->startup_cost = 0;
1654 dest->total_cost = 0;
1655 dest->plan_rows = 0;
1656 dest->plan_width = 0;
1661 * Copy cost and size info from a lower plan node to an inserted node.
1662 * This is not critical, since the decisions have already been made,
1663 * but it helps produce more reasonable-looking EXPLAIN output.
1664 * (Some callers alter the info after copying it.)
1667 copy_plan_costsize(Plan *dest, Plan *src)
1671 dest->startup_cost = src->startup_cost;
1672 dest->total_cost = src->total_cost;
1673 dest->plan_rows = src->plan_rows;
1674 dest->plan_width = src->plan_width;
1678 dest->startup_cost = 0;
1679 dest->total_cost = 0;
1680 dest->plan_rows = 0;
1681 dest->plan_width = 0;
1686 /*****************************************************************************
1688 * PLAN NODE BUILDING ROUTINES
1690 * Some of these are exported because they are called to build plan nodes
1691 * in contexts where we're not deriving the plan node from a path node.
1693 *****************************************************************************/
1696 make_seqscan(List *qptlist,
1700 SeqScan *node = makeNode(SeqScan);
1701 Plan *plan = &node->plan;
1703 /* cost should be inserted by caller */
1704 plan->targetlist = qptlist;
1705 plan->qual = qpqual;
1706 plan->lefttree = NULL;
1707 plan->righttree = NULL;
1708 node->scanrelid = scanrelid;
1714 make_indexscan(List *qptlist,
1719 List *indexqualorig,
1720 List *indexstrategy,
1722 ScanDirection indexscandir)
1724 IndexScan *node = makeNode(IndexScan);
1725 Plan *plan = &node->scan.plan;
1727 /* cost should be inserted by caller */
1728 plan->targetlist = qptlist;
1729 plan->qual = qpqual;
1730 plan->lefttree = NULL;
1731 plan->righttree = NULL;
1732 node->scan.scanrelid = scanrelid;
1733 node->indexid = indexid;
1734 node->indexqual = indexqual;
1735 node->indexqualorig = indexqualorig;
1736 node->indexstrategy = indexstrategy;
1737 node->indexsubtype = indexsubtype;
1738 node->indexorderdir = indexscandir;
1743 static BitmapIndexScan *
1744 make_bitmap_indexscan(Index scanrelid,
1747 List *indexqualorig,
1748 List *indexstrategy,
1751 BitmapIndexScan *node = makeNode(BitmapIndexScan);
1752 Plan *plan = &node->scan.plan;
1754 /* cost should be inserted by caller */
1755 plan->targetlist = NIL; /* not used */
1756 plan->qual = NIL; /* not used */
1757 plan->lefttree = NULL;
1758 plan->righttree = NULL;
1759 node->scan.scanrelid = scanrelid;
1760 node->indexid = indexid;
1761 node->indexqual = indexqual;
1762 node->indexqualorig = indexqualorig;
1763 node->indexstrategy = indexstrategy;
1764 node->indexsubtype = indexsubtype;
1769 static BitmapHeapScan *
1770 make_bitmap_heapscan(List *qptlist,
1773 List *bitmapqualorig,
1776 BitmapHeapScan *node = makeNode(BitmapHeapScan);
1777 Plan *plan = &node->scan.plan;
1779 /* cost should be inserted by caller */
1780 plan->targetlist = qptlist;
1781 plan->qual = qpqual;
1782 plan->lefttree = lefttree;
1783 plan->righttree = NULL;
1784 node->scan.scanrelid = scanrelid;
1785 node->bitmapqualorig = bitmapqualorig;
1791 make_tidscan(List *qptlist,
1796 TidScan *node = makeNode(TidScan);
1797 Plan *plan = &node->scan.plan;
1799 /* cost should be inserted by caller */
1800 plan->targetlist = qptlist;
1801 plan->qual = qpqual;
1802 plan->lefttree = NULL;
1803 plan->righttree = NULL;
1804 node->scan.scanrelid = scanrelid;
1805 node->tideval = tideval;
1811 make_subqueryscan(List *qptlist,
1816 SubqueryScan *node = makeNode(SubqueryScan);
1817 Plan *plan = &node->scan.plan;
1820 * Cost is figured here for the convenience of prepunion.c. Note this
1821 * is only correct for the case where qpqual is empty; otherwise
1822 * caller should overwrite cost with a better estimate.
1824 copy_plan_costsize(plan, subplan);
1825 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
1827 plan->targetlist = qptlist;
1828 plan->qual = qpqual;
1829 plan->lefttree = NULL;
1830 plan->righttree = NULL;
1831 node->scan.scanrelid = scanrelid;
1832 node->subplan = subplan;
1837 static FunctionScan *
1838 make_functionscan(List *qptlist,
1842 FunctionScan *node = makeNode(FunctionScan);
1843 Plan *plan = &node->scan.plan;
1845 /* cost should be inserted by caller */
1846 plan->targetlist = qptlist;
1847 plan->qual = qpqual;
1848 plan->lefttree = NULL;
1849 plan->righttree = NULL;
1850 node->scan.scanrelid = scanrelid;
1856 make_append(List *appendplans, bool isTarget, List *tlist)
1858 Append *node = makeNode(Append);
1859 Plan *plan = &node->plan;
1863 * Compute cost as sum of subplan costs. We charge nothing extra for
1864 * the Append itself, which perhaps is too optimistic, but since it
1865 * doesn't do any selection or projection, it is a pretty cheap node.
1867 plan->startup_cost = 0;
1868 plan->total_cost = 0;
1869 plan->plan_rows = 0;
1870 plan->plan_width = 0;
1871 foreach(subnode, appendplans)
1873 Plan *subplan = (Plan *) lfirst(subnode);
1875 if (subnode == list_head(appendplans)) /* first node? */
1876 plan->startup_cost = subplan->startup_cost;
1877 plan->total_cost += subplan->total_cost;
1878 plan->plan_rows += subplan->plan_rows;
1879 if (plan->plan_width < subplan->plan_width)
1880 plan->plan_width = subplan->plan_width;
1883 plan->targetlist = tlist;
1885 plan->lefttree = NULL;
1886 plan->righttree = NULL;
1887 node->appendplans = appendplans;
1888 node->isTarget = isTarget;
1894 make_bitmap_and(List *bitmapplans)
1896 BitmapAnd *node = makeNode(BitmapAnd);
1897 Plan *plan = &node->plan;
1899 /* cost should be inserted by caller */
1900 plan->targetlist = NIL;
1902 plan->lefttree = NULL;
1903 plan->righttree = NULL;
1904 node->bitmapplans = bitmapplans;
1910 make_bitmap_or(List *bitmapplans)
1912 BitmapOr *node = makeNode(BitmapOr);
1913 Plan *plan = &node->plan;
1915 /* cost should be inserted by caller */
1916 plan->targetlist = NIL;
1918 plan->lefttree = NULL;
1919 plan->righttree = NULL;
1920 node->bitmapplans = bitmapplans;
1926 make_nestloop(List *tlist,
1933 NestLoop *node = makeNode(NestLoop);
1934 Plan *plan = &node->join.plan;
1936 /* cost should be inserted by caller */
1937 plan->targetlist = tlist;
1938 plan->qual = otherclauses;
1939 plan->lefttree = lefttree;
1940 plan->righttree = righttree;
1941 node->join.jointype = jointype;
1942 node->join.joinqual = joinclauses;
1948 make_hashjoin(List *tlist,
1956 HashJoin *node = makeNode(HashJoin);
1957 Plan *plan = &node->join.plan;
1959 /* cost should be inserted by caller */
1960 plan->targetlist = tlist;
1961 plan->qual = otherclauses;
1962 plan->lefttree = lefttree;
1963 plan->righttree = righttree;
1964 node->hashclauses = hashclauses;
1965 node->join.jointype = jointype;
1966 node->join.joinqual = joinclauses;
1972 make_hash(Plan *lefttree)
1974 Hash *node = makeNode(Hash);
1975 Plan *plan = &node->plan;
1977 copy_plan_costsize(plan, lefttree);
1980 * For plausibility, make startup & total costs equal total cost of
1981 * input plan; this only affects EXPLAIN display not decisions.
1983 plan->startup_cost = plan->total_cost;
1984 plan->targetlist = copyObject(lefttree->targetlist);
1986 plan->lefttree = lefttree;
1987 plan->righttree = NULL;
1993 make_mergejoin(List *tlist,
2001 MergeJoin *node = makeNode(MergeJoin);
2002 Plan *plan = &node->join.plan;
2004 /* cost should be inserted by caller */
2005 plan->targetlist = tlist;
2006 plan->qual = otherclauses;
2007 plan->lefttree = lefttree;
2008 plan->righttree = righttree;
2009 node->mergeclauses = mergeclauses;
2010 node->join.jointype = jointype;
2011 node->join.joinqual = joinclauses;
2017 * make_sort --- basic routine to build a Sort plan node
2019 * Caller must have built the sortColIdx and sortOperators arrays already.
2022 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
2023 AttrNumber *sortColIdx, Oid *sortOperators)
2025 Sort *node = makeNode(Sort);
2026 Plan *plan = &node->plan;
2027 Path sort_path; /* dummy for result of cost_sort */
2029 copy_plan_costsize(plan, lefttree); /* only care about copying size */
2030 cost_sort(&sort_path, root, NIL,
2031 lefttree->total_cost,
2032 lefttree->plan_rows,
2033 lefttree->plan_width);
2034 plan->startup_cost = sort_path.startup_cost;
2035 plan->total_cost = sort_path.total_cost;
2036 plan->targetlist = copyObject(lefttree->targetlist);
2038 plan->lefttree = lefttree;
2039 plan->righttree = NULL;
2040 node->numCols = numCols;
2041 node->sortColIdx = sortColIdx;
2042 node->sortOperators = sortOperators;
2048 * add_sort_column --- utility subroutine for building sort info arrays
2050 * We need this routine because the same column might be selected more than
2051 * once as a sort key column; if so, the extra mentions are redundant.
2053 * Caller is assumed to have allocated the arrays large enough for the
2054 * max possible number of columns. Return value is the new column count.
2057 add_sort_column(AttrNumber colIdx, Oid sortOp,
2058 int numCols, AttrNumber *sortColIdx, Oid *sortOperators)
2062 for (i = 0; i < numCols; i++)
2064 if (sortColIdx[i] == colIdx)
2066 /* Already sorting by this col, so extra sort key is useless */
2071 /* Add the column */
2072 sortColIdx[numCols] = colIdx;
2073 sortOperators[numCols] = sortOp;
2078 * make_sort_from_pathkeys
2079 * Create sort plan to sort according to given pathkeys
2081 * 'lefttree' is the node which yields input tuples
2082 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
2084 * We must convert the pathkey information into arrays of sort key column
2085 * numbers and sort operator OIDs.
2087 * If the pathkeys include expressions that aren't simple Vars, we will
2088 * usually need to add resjunk items to the input plan's targetlist to
2089 * compute these expressions (since the Sort node itself won't do it).
2090 * If the input plan type isn't one that can do projections, this means
2091 * adding a Result node just to do the projection.
2094 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys)
2096 List *tlist = lefttree->targetlist;
2099 AttrNumber *sortColIdx;
2103 * We will need at most list_length(pathkeys) sort columns; possibly
2106 numsortkeys = list_length(pathkeys);
2107 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2108 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2112 foreach(i, pathkeys)
2114 List *keysublist = (List *) lfirst(i);
2115 PathKeyItem *pathkey = NULL;
2116 TargetEntry *tle = NULL;
2120 * We can sort by any one of the sort key items listed in this
2121 * sublist. For now, we take the first one that corresponds to an
2122 * available Var in the tlist. If there isn't any, use the first
2123 * one that is an expression in the input's vars.
2125 * XXX if we have a choice, is there any way of figuring out which
2126 * might be cheapest to execute? (For example, int4lt is likely
2127 * much cheaper to execute than numericlt, but both might appear
2128 * in the same pathkey sublist...) Not clear that we ever will
2129 * have a choice in practice, so it may not matter.
2131 foreach(j, keysublist)
2133 pathkey = (PathKeyItem *) lfirst(j);
2134 Assert(IsA(pathkey, PathKeyItem));
2135 tle = tlist_member(pathkey->key, tlist);
2141 /* No matching Var; look for a computable expression */
2142 foreach(j, keysublist)
2147 pathkey = (PathKeyItem *) lfirst(j);
2148 exprvars = pull_var_clause(pathkey->key, false);
2149 foreach(k, exprvars)
2151 if (!tlist_member(lfirst(k), tlist))
2154 list_free(exprvars);
2156 break; /* found usable expression */
2159 elog(ERROR, "could not find pathkey item to sort");
2162 * Do we need to insert a Result node?
2164 if (!is_projection_capable_plan(lefttree))
2166 tlist = copyObject(tlist);
2167 lefttree = (Plan *) make_result(tlist, NULL, lefttree);
2171 * Add resjunk entry to input's tlist
2173 tle = makeTargetEntry((Expr *) pathkey->key,
2174 list_length(tlist) + 1,
2177 tlist = lappend(tlist, tle);
2178 lefttree->targetlist = tlist; /* just in case NIL before */
2182 * The column might already be selected as a sort key, if the
2183 * pathkeys contain duplicate entries. (This can happen in
2184 * scenarios where multiple mergejoinable clauses mention the same
2185 * var, for example.) So enter it only once in the sort arrays.
2187 numsortkeys = add_sort_column(tle->resno, pathkey->sortop,
2188 numsortkeys, sortColIdx, sortOperators);
2191 Assert(numsortkeys > 0);
2193 return make_sort(root, lefttree, numsortkeys,
2194 sortColIdx, sortOperators);
2198 * make_sort_from_sortclauses
2199 * Create sort plan to sort according to given sortclauses
2201 * 'sortcls' is a list of SortClauses
2202 * 'lefttree' is the node which yields input tuples
2205 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
2207 List *sub_tlist = lefttree->targetlist;
2210 AttrNumber *sortColIdx;
2214 * We will need at most list_length(sortcls) sort columns; possibly
2217 numsortkeys = list_length(sortcls);
2218 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2219 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2225 SortClause *sortcl = (SortClause *) lfirst(l);
2226 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
2229 * Check for the possibility of duplicate order-by clauses --- the
2230 * parser should have removed 'em, but no point in sorting
2233 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
2234 numsortkeys, sortColIdx, sortOperators);
2237 Assert(numsortkeys > 0);
2239 return make_sort(root, lefttree, numsortkeys,
2240 sortColIdx, sortOperators);
2244 * make_sort_from_groupcols
2245 * Create sort plan to sort based on grouping columns
2247 * 'groupcls' is the list of GroupClauses
2248 * 'grpColIdx' gives the column numbers to use
2250 * This might look like it could be merged with make_sort_from_sortclauses,
2251 * but presently we *must* use the grpColIdx[] array to locate sort columns,
2252 * because the child plan's tlist is not marked with ressortgroupref info
2253 * appropriate to the grouping node. So, only the sortop is used from the
2254 * GroupClause entries.
2257 make_sort_from_groupcols(PlannerInfo *root,
2259 AttrNumber *grpColIdx,
2262 List *sub_tlist = lefttree->targetlist;
2266 AttrNumber *sortColIdx;
2270 * We will need at most list_length(groupcls) sort columns; possibly
2273 numsortkeys = list_length(groupcls);
2274 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2275 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2279 foreach(l, groupcls)
2281 GroupClause *grpcl = (GroupClause *) lfirst(l);
2282 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
2285 * Check for the possibility of duplicate group-by clauses --- the
2286 * parser should have removed 'em, but no point in sorting
2289 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
2290 numsortkeys, sortColIdx, sortOperators);
2294 Assert(numsortkeys > 0);
2296 return make_sort(root, lefttree, numsortkeys,
2297 sortColIdx, sortOperators);
2301 make_material(Plan *lefttree)
2303 Material *node = makeNode(Material);
2304 Plan *plan = &node->plan;
2306 /* cost should be inserted by caller */
2307 plan->targetlist = copyObject(lefttree->targetlist);
2309 plan->lefttree = lefttree;
2310 plan->righttree = NULL;
2316 * materialize_finished_plan: stick a Material node atop a completed plan
2318 * There are a couple of places where we want to attach a Material node
2319 * after completion of subquery_planner(). This currently requires hackery.
2320 * Since subquery_planner has already run SS_finalize_plan on the subplan
2321 * tree, we have to kluge up parameter lists for the Material node.
2322 * Possibly this could be fixed by postponing SS_finalize_plan processing
2323 * until setrefs.c is run?
2326 materialize_finished_plan(Plan *subplan)
2329 Path matpath; /* dummy for result of cost_material */
2331 matplan = (Plan *) make_material(subplan);
2334 cost_material(&matpath,
2335 subplan->total_cost,
2337 subplan->plan_width);
2338 matplan->startup_cost = matpath.startup_cost;
2339 matplan->total_cost = matpath.total_cost;
2340 matplan->plan_rows = subplan->plan_rows;
2341 matplan->plan_width = subplan->plan_width;
2343 /* parameter kluge --- see comments above */
2344 matplan->extParam = bms_copy(subplan->extParam);
2345 matplan->allParam = bms_copy(subplan->allParam);
2351 make_agg(PlannerInfo *root, List *tlist, List *qual,
2352 AggStrategy aggstrategy,
2353 int numGroupCols, AttrNumber *grpColIdx,
2354 long numGroups, int numAggs,
2357 Agg *node = makeNode(Agg);
2358 Plan *plan = &node->plan;
2359 Path agg_path; /* dummy for result of cost_agg */
2362 node->aggstrategy = aggstrategy;
2363 node->numCols = numGroupCols;
2364 node->grpColIdx = grpColIdx;
2365 node->numGroups = numGroups;
2367 copy_plan_costsize(plan, lefttree); /* only care about copying size */
2368 cost_agg(&agg_path, root,
2369 aggstrategy, numAggs,
2370 numGroupCols, numGroups,
2371 lefttree->startup_cost,
2372 lefttree->total_cost,
2373 lefttree->plan_rows);
2374 plan->startup_cost = agg_path.startup_cost;
2375 plan->total_cost = agg_path.total_cost;
2378 * We will produce a single output tuple if not grouping, and a tuple
2379 * per group otherwise.
2381 if (aggstrategy == AGG_PLAIN)
2382 plan->plan_rows = 1;
2384 plan->plan_rows = numGroups;
2387 * We also need to account for the cost of evaluation of the qual (ie,
2388 * the HAVING clause) and the tlist. Note that cost_qual_eval doesn't
2389 * charge anything for Aggref nodes; this is okay since they are
2390 * really comparable to Vars.
2392 * See notes in grouping_planner about why this routine and make_group
2393 * are the only ones in this file that worry about tlist eval cost.
2397 cost_qual_eval(&qual_cost, qual);
2398 plan->startup_cost += qual_cost.startup;
2399 plan->total_cost += qual_cost.startup;
2400 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
2402 cost_qual_eval(&qual_cost, tlist);
2403 plan->startup_cost += qual_cost.startup;
2404 plan->total_cost += qual_cost.startup;
2405 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
2408 plan->targetlist = tlist;
2409 plan->lefttree = lefttree;
2410 plan->righttree = NULL;
2416 make_group(PlannerInfo *root,
2420 AttrNumber *grpColIdx,
2424 Group *node = makeNode(Group);
2425 Plan *plan = &node->plan;
2426 Path group_path; /* dummy for result of cost_group */
2429 node->numCols = numGroupCols;
2430 node->grpColIdx = grpColIdx;
2432 copy_plan_costsize(plan, lefttree); /* only care about copying size */
2433 cost_group(&group_path, root,
2434 numGroupCols, numGroups,
2435 lefttree->startup_cost,
2436 lefttree->total_cost,
2437 lefttree->plan_rows);
2438 plan->startup_cost = group_path.startup_cost;
2439 plan->total_cost = group_path.total_cost;
2441 /* One output tuple per estimated result group */
2442 plan->plan_rows = numGroups;
2445 * We also need to account for the cost of evaluation of the qual (ie,
2446 * the HAVING clause) and the tlist.
2448 * XXX this double-counts the cost of evaluation of any expressions used
2449 * for grouping, since in reality those will have been evaluated at a
2450 * lower plan level and will only be copied by the Group node. Worth
2453 * See notes in grouping_planner about why this routine and make_agg are
2454 * the only ones in this file that worry about tlist eval cost.
2458 cost_qual_eval(&qual_cost, qual);
2459 plan->startup_cost += qual_cost.startup;
2460 plan->total_cost += qual_cost.startup;
2461 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
2463 cost_qual_eval(&qual_cost, tlist);
2464 plan->startup_cost += qual_cost.startup;
2465 plan->total_cost += qual_cost.startup;
2466 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
2469 plan->targetlist = tlist;
2470 plan->lefttree = lefttree;
2471 plan->righttree = NULL;
2477 * distinctList is a list of SortClauses, identifying the targetlist items
2478 * that should be considered by the Unique filter.
2481 make_unique(Plan *lefttree, List *distinctList)
2483 Unique *node = makeNode(Unique);
2484 Plan *plan = &node->plan;
2485 int numCols = list_length(distinctList);
2487 AttrNumber *uniqColIdx;
2490 copy_plan_costsize(plan, lefttree);
2493 * Charge one cpu_operator_cost per comparison per input tuple. We
2494 * assume all columns get compared at most of the tuples. (XXX
2495 * probably this is an overestimate.)
2497 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
2500 * plan->plan_rows is left as a copy of the input subplan's plan_rows;
2501 * ie, we assume the filter removes nothing. The caller must alter
2502 * this if he has a better idea.
2505 plan->targetlist = copyObject(lefttree->targetlist);
2507 plan->lefttree = lefttree;
2508 plan->righttree = NULL;
2511 * convert SortClause list into array of attr indexes, as wanted by
2514 Assert(numCols > 0);
2515 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
2517 foreach(slitem, distinctList)
2519 SortClause *sortcl = (SortClause *) lfirst(slitem);
2520 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
2522 uniqColIdx[keyno++] = tle->resno;
2525 node->numCols = numCols;
2526 node->uniqColIdx = uniqColIdx;
2532 * distinctList is a list of SortClauses, identifying the targetlist items
2533 * that should be considered by the SetOp filter.
2537 make_setop(SetOpCmd cmd, Plan *lefttree,
2538 List *distinctList, AttrNumber flagColIdx)
2540 SetOp *node = makeNode(SetOp);
2541 Plan *plan = &node->plan;
2542 int numCols = list_length(distinctList);
2544 AttrNumber *dupColIdx;
2547 copy_plan_costsize(plan, lefttree);
2550 * Charge one cpu_operator_cost per comparison per input tuple. We
2551 * assume all columns get compared at most of the tuples.
2553 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
2556 * We make the unsupported assumption that there will be 10% as many
2557 * tuples out as in. Any way to do better?
2559 plan->plan_rows *= 0.1;
2560 if (plan->plan_rows < 1)
2561 plan->plan_rows = 1;
2563 plan->targetlist = copyObject(lefttree->targetlist);
2565 plan->lefttree = lefttree;
2566 plan->righttree = NULL;
2569 * convert SortClause list into array of attr indexes, as wanted by
2572 Assert(numCols > 0);
2573 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
2575 foreach(slitem, distinctList)
2577 SortClause *sortcl = (SortClause *) lfirst(slitem);
2578 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
2580 dupColIdx[keyno++] = tle->resno;
2584 node->numCols = numCols;
2585 node->dupColIdx = dupColIdx;
2586 node->flagColIdx = flagColIdx;
2592 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
2594 Limit *node = makeNode(Limit);
2595 Plan *plan = &node->plan;
2597 copy_plan_costsize(plan, lefttree);
2600 * If offset/count are constants, adjust the output rows count and
2601 * costs accordingly. This is only a cosmetic issue if we are at top
2602 * level, but if we are building a subquery then it's important to
2603 * report correct info to the outer planner.
2605 if (limitOffset && IsA(limitOffset, Const))
2607 Const *limito = (Const *) limitOffset;
2608 int32 offset = DatumGetInt32(limito->constvalue);
2610 if (!limito->constisnull && offset > 0)
2612 if (offset > plan->plan_rows)
2613 offset = (int32) plan->plan_rows;
2614 if (plan->plan_rows > 0)
2615 plan->startup_cost +=
2616 (plan->total_cost - plan->startup_cost)
2617 * ((double) offset) / plan->plan_rows;
2618 plan->plan_rows -= offset;
2619 if (plan->plan_rows < 1)
2620 plan->plan_rows = 1;
2623 if (limitCount && IsA(limitCount, Const))
2625 Const *limitc = (Const *) limitCount;
2626 int32 count = DatumGetInt32(limitc->constvalue);
2628 if (!limitc->constisnull && count >= 0)
2630 if (count > plan->plan_rows)
2631 count = (int32) plan->plan_rows;
2632 if (plan->plan_rows > 0)
2633 plan->total_cost = plan->startup_cost +
2634 (plan->total_cost - plan->startup_cost)
2635 * ((double) count) / plan->plan_rows;
2636 plan->plan_rows = count;
2637 if (plan->plan_rows < 1)
2638 plan->plan_rows = 1;
2642 plan->targetlist = copyObject(lefttree->targetlist);
2644 plan->lefttree = lefttree;
2645 plan->righttree = NULL;
2647 node->limitOffset = limitOffset;
2648 node->limitCount = limitCount;
2654 make_result(List *tlist,
2655 Node *resconstantqual,
2658 Result *node = makeNode(Result);
2659 Plan *plan = &node->plan;
2662 copy_plan_costsize(plan, subplan);
2665 plan->startup_cost = 0;
2666 plan->total_cost = cpu_tuple_cost;
2667 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
2668 plan->plan_width = 0; /* XXX try to be smarter? */
2671 if (resconstantqual)
2675 cost_qual_eval(&qual_cost, (List *) resconstantqual);
2676 /* resconstantqual is evaluated once at startup */
2677 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
2678 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
2681 plan->targetlist = tlist;
2683 plan->lefttree = subplan;
2684 plan->righttree = NULL;
2685 node->resconstantqual = resconstantqual;
2691 * is_projection_capable_plan
2692 * Check whether a given Plan node is able to do projection.
2695 is_projection_capable_plan(Plan *plan)
2697 /* Most plan types can project, so just list the ones that can't */
2698 switch (nodeTag(plan))