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-2008, 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.237 2008/01/01 19:45:50 momjian Exp $
15 *-------------------------------------------------------------------------
21 #include "access/skey.h"
22 #include "nodes/makefuncs.h"
23 #include "optimizer/clauses.h"
24 #include "optimizer/cost.h"
25 #include "optimizer/plancat.h"
26 #include "optimizer/planmain.h"
27 #include "optimizer/predtest.h"
28 #include "optimizer/restrictinfo.h"
29 #include "optimizer/tlist.h"
30 #include "optimizer/var.h"
31 #include "parser/parse_clause.h"
32 #include "parser/parse_expr.h"
33 #include "parser/parsetree.h"
34 #include "utils/lsyscache.h"
37 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
38 static List *build_relation_tlist(RelOptInfo *rel);
39 static bool use_physical_tlist(RelOptInfo *rel);
40 static void disuse_physical_tlist(Plan *plan, Path *path);
41 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
42 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
43 static Plan *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 ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
64 List *tlist, List *scan_clauses);
65 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
66 Plan *outer_plan, Plan *inner_plan);
67 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
68 Plan *outer_plan, Plan *inner_plan);
69 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
70 Plan *outer_plan, Plan *inner_plan);
71 static void fix_indexqual_references(List *indexquals, IndexPath *index_path,
72 List **fixed_indexquals,
73 List **nonlossy_indexquals,
76 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index,
78 static List *get_switched_clauses(List *clauses, Relids outerrelids);
79 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
80 static void copy_path_costsize(Plan *dest, Path *src);
81 static void copy_plan_costsize(Plan *dest, Plan *src);
82 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
83 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
84 Oid indexid, List *indexqual, List *indexqualorig,
85 List *indexstrategy, List *indexsubtype,
86 ScanDirection indexscandir);
87 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
92 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
97 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
99 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
100 Index scanrelid, Node *funcexpr, List *funccolnames,
101 List *funccoltypes, List *funccoltypmods);
102 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
103 Index scanrelid, List *values_lists);
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);
116 static MergeJoin *make_mergejoin(List *tlist,
117 List *joinclauses, List *otherclauses,
120 int *mergestrategies,
121 bool *mergenullsfirst,
122 Plan *lefttree, Plan *righttree,
124 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
125 AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
126 double limit_tuples);
131 * Creates the access plan for a query by tracing backwards through the
132 * desired chain of pathnodes, starting at the node 'best_path'. For
133 * every pathnode found, we create a corresponding plan node containing
134 * appropriate id, target list, and qualification information.
136 * The tlists and quals in the plan tree are still in planner format,
137 * ie, Vars still correspond to the parser's numbering. This will be
138 * fixed later by setrefs.c.
140 * best_path is the best access path
142 * Returns a Plan tree.
145 create_plan(PlannerInfo *root, Path *best_path)
149 switch (best_path->pathtype)
153 case T_BitmapHeapScan:
158 plan = create_scan_plan(root, best_path);
163 plan = create_join_plan(root,
164 (JoinPath *) best_path);
167 plan = create_append_plan(root,
168 (AppendPath *) best_path);
171 plan = (Plan *) create_result_plan(root,
172 (ResultPath *) best_path);
175 plan = (Plan *) create_material_plan(root,
176 (MaterialPath *) best_path);
179 plan = create_unique_plan(root,
180 (UniquePath *) best_path);
183 elog(ERROR, "unrecognized node type: %d",
184 (int) best_path->pathtype);
185 plan = NULL; /* keep compiler quiet */
194 * Create a scan plan for the parent relation of 'best_path'.
197 create_scan_plan(PlannerInfo *root, Path *best_path)
199 RelOptInfo *rel = best_path->parent;
205 * For table scans, rather than using the relation targetlist (which is
206 * only those Vars actually needed by the query), we prefer to generate a
207 * tlist containing all Vars in order. This will allow the executor to
208 * optimize away projection of the table tuples, if possible. (Note that
209 * planner.c may replace the tlist we generate here, forcing projection to
212 if (use_physical_tlist(rel))
214 tlist = build_physical_tlist(root, rel);
215 /* if fail because of dropped cols, use regular method */
217 tlist = build_relation_tlist(rel);
220 tlist = build_relation_tlist(rel);
223 * Extract the relevant restriction clauses from the parent relation. The
224 * executor must apply all these restrictions during the scan, except for
225 * pseudoconstants which we'll take care of below.
227 scan_clauses = rel->baserestrictinfo;
229 switch (best_path->pathtype)
232 plan = (Plan *) create_seqscan_plan(root,
239 plan = (Plan *) create_indexscan_plan(root,
240 (IndexPath *) best_path,
246 case T_BitmapHeapScan:
247 plan = (Plan *) create_bitmap_scan_plan(root,
248 (BitmapHeapPath *) best_path,
254 plan = (Plan *) create_tidscan_plan(root,
255 (TidPath *) best_path,
261 plan = (Plan *) create_subqueryscan_plan(root,
268 plan = (Plan *) create_functionscan_plan(root,
275 plan = (Plan *) create_valuesscan_plan(root,
282 elog(ERROR, "unrecognized node type: %d",
283 (int) best_path->pathtype);
284 plan = NULL; /* keep compiler quiet */
289 * If there are any pseudoconstant clauses attached to this node, insert a
290 * gating Result node that evaluates the pseudoconstants as one-time
293 if (root->hasPseudoConstantQuals)
294 plan = create_gating_plan(root, plan, scan_clauses);
300 * Build a target list (ie, a list of TargetEntry) for a relation.
303 build_relation_tlist(RelOptInfo *rel)
309 foreach(v, rel->reltargetlist)
311 /* Do we really need to copy here? Not sure */
312 Var *var = (Var *) copyObject(lfirst(v));
314 tlist = lappend(tlist, makeTargetEntry((Expr *) var,
325 * Decide whether to use a tlist matching relation structure,
326 * rather than only those Vars actually referenced.
329 use_physical_tlist(RelOptInfo *rel)
334 * We can do this for real relation scans, subquery scans, function scans,
335 * and values scans (but not for, eg, joins).
337 if (rel->rtekind != RTE_RELATION &&
338 rel->rtekind != RTE_SUBQUERY &&
339 rel->rtekind != RTE_FUNCTION &&
340 rel->rtekind != RTE_VALUES)
344 * Can't do it with inheritance cases either (mainly because Append
347 if (rel->reloptkind != RELOPT_BASEREL)
351 * Can't do it if any system columns or whole-row Vars are requested,
352 * either. (This could possibly be fixed but would take some fragile
353 * assumptions in setrefs.c, I think.)
355 for (i = rel->min_attr; i <= 0; i++)
357 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
365 * disuse_physical_tlist
366 * Switch a plan node back to emitting only Vars actually referenced.
368 * If the plan node immediately above a scan would prefer to get only
369 * needed Vars and not a physical tlist, it must call this routine to
370 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
371 * and Material nodes want this, so they don't have to store useless columns.
374 disuse_physical_tlist(Plan *plan, Path *path)
376 /* Only need to undo it for path types handled by create_scan_plan() */
377 switch (path->pathtype)
381 case T_BitmapHeapScan:
386 plan->targetlist = build_relation_tlist(path->parent);
395 * Deal with pseudoconstant qual clauses
397 * If the node's quals list includes any pseudoconstant quals, put them
398 * into a gating Result node atop the already-built plan. Otherwise,
399 * return the plan as-is.
401 * Note that we don't change cost or size estimates when doing gating.
402 * The costs of qual eval were already folded into the plan's startup cost.
403 * Leaving the size alone amounts to assuming that the gating qual will
404 * succeed, which is the conservative estimate for planning upper queries.
405 * We certainly don't want to assume the output size is zero (unless the
406 * gating qual is actually constant FALSE, and that case is dealt with in
407 * clausesel.c). Interpolating between the two cases is silly, because
408 * it doesn't reflect what will really happen at runtime, and besides which
409 * in most cases we have only a very bad idea of the probability of the gating
413 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
415 List *pseudoconstants;
417 /* Sort into desirable execution order while still in RestrictInfo form */
418 quals = order_qual_clauses(root, quals);
420 /* Pull out any pseudoconstant quals from the RestrictInfo list */
421 pseudoconstants = extract_actual_clauses(quals, true);
423 if (!pseudoconstants)
426 return (Plan *) make_result(root,
428 (Node *) pseudoconstants,
434 * Create a join plan for 'best_path' and (recursively) plans for its
435 * inner and outer paths.
438 create_join_plan(PlannerInfo *root, JoinPath *best_path)
444 outer_plan = create_plan(root, best_path->outerjoinpath);
445 inner_plan = create_plan(root, best_path->innerjoinpath);
447 switch (best_path->path.pathtype)
450 plan = (Plan *) create_mergejoin_plan(root,
451 (MergePath *) best_path,
456 plan = (Plan *) create_hashjoin_plan(root,
457 (HashPath *) best_path,
462 plan = (Plan *) create_nestloop_plan(root,
463 (NestPath *) best_path,
468 elog(ERROR, "unrecognized node type: %d",
469 (int) best_path->path.pathtype);
470 plan = NULL; /* keep compiler quiet */
475 * If there are any pseudoconstant clauses attached to this node, insert a
476 * gating Result node that evaluates the pseudoconstants as one-time
479 if (root->hasPseudoConstantQuals)
480 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
485 * * Expensive function pullups may have pulled local predicates * into
486 * this path node. Put them in the qpqual of the plan node. * JMH,
489 if (get_loc_restrictinfo(best_path) != NIL)
490 set_qpqual((Plan) plan,
491 list_concat(get_qpqual((Plan) plan),
492 get_actual_clauses(get_loc_restrictinfo(best_path))));
500 * Create an Append plan for 'best_path' and (recursively) plans
503 * Returns a Plan node.
506 create_append_plan(PlannerInfo *root, AppendPath *best_path)
509 List *tlist = build_relation_tlist(best_path->path.parent);
510 List *subplans = NIL;
514 * It is possible for the subplans list to contain only one entry, or even
515 * no entries. Handle these cases specially.
517 * XXX ideally, if there's just one entry, we'd not bother to generate an
518 * Append node but just return the single child. At the moment this does
519 * not work because the varno of the child scan plan won't match the
520 * parent-rel Vars it'll be asked to emit.
522 if (best_path->subpaths == NIL)
524 /* Generate a Result plan with constant-FALSE gating qual */
525 return (Plan *) make_result(root,
527 (Node *) list_make1(makeBoolConst(false,
532 /* Normal case with multiple subpaths */
533 foreach(subpaths, best_path->subpaths)
535 Path *subpath = (Path *) lfirst(subpaths);
537 subplans = lappend(subplans, create_plan(root, subpath));
540 plan = make_append(subplans, false, tlist);
542 return (Plan *) plan;
547 * Create a Result plan for 'best_path'.
548 * This is only used for the case of a query with an empty jointree.
550 * Returns a Plan node.
553 create_result_plan(PlannerInfo *root, ResultPath *best_path)
558 /* The tlist will be installed later, since we have no RelOptInfo */
559 Assert(best_path->path.parent == NULL);
562 /* best_path->quals is just bare clauses */
564 quals = order_qual_clauses(root, best_path->quals);
566 return make_result(root, tlist, (Node *) quals, NULL);
570 * create_material_plan
571 * Create a Material plan for 'best_path' and (recursively) plans
574 * Returns a Plan node.
577 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
582 subplan = create_plan(root, best_path->subpath);
584 /* We don't want any excess columns in the materialized tuples */
585 disuse_physical_tlist(subplan, best_path->subpath);
587 plan = make_material(subplan);
589 copy_path_costsize(&plan->plan, (Path *) best_path);
596 * Create a Unique plan for 'best_path' and (recursively) plans
599 * Returns a Plan node.
602 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
612 AttrNumber *groupColIdx;
616 subplan = create_plan(root, best_path->subpath);
618 /* Done if we don't need to do any actual unique-ifying */
619 if (best_path->umethod == UNIQUE_PATH_NOOP)
623 * As constructed, the subplan has a "flat" tlist containing just the
624 * Vars needed here and at upper levels. The values we are supposed
625 * to unique-ify may be expressions in these variables. We have to
626 * add any such expressions to the subplan's tlist.
628 * The subplan may have a "physical" tlist if it is a simple scan plan.
629 * This should be left as-is if we don't need to add any expressions;
630 * but if we do have to add expressions, then a projection step will be
631 * needed at runtime anyway, and so we may as well remove unneeded items.
632 * Therefore newtlist starts from build_relation_tlist() not just a
633 * copy of the subplan's tlist; and we don't install it into the subplan
634 * unless stuff has to be added.
636 * To find the correct list of values to unique-ify, we look in the
637 * information saved for IN expressions. If this code is ever used in
638 * other scenarios, some other way of finding what to unique-ify will
639 * be needed. The IN clause's operators are needed too, since they
640 * determine what the meaning of "unique" is in this context.
643 uniq_exprs = NIL; /* just to keep compiler quiet */
645 foreach(l, root->in_info_list)
647 InClauseInfo *ininfo = (InClauseInfo *) lfirst(l);
649 if (bms_equal(ininfo->righthand, best_path->path.parent->relids))
651 uniq_exprs = ininfo->sub_targetlist;
652 in_operators = ininfo->in_operators;
656 if (l == NULL) /* fell out of loop? */
657 elog(ERROR, "could not find UniquePath in in_info_list");
659 /* initialize modified subplan tlist as just the "required" vars */
660 newtlist = build_relation_tlist(best_path->path.parent);
661 nextresno = list_length(newtlist) + 1;
664 foreach(l, uniq_exprs)
666 Node *uniqexpr = lfirst(l);
669 tle = tlist_member(uniqexpr, newtlist);
672 tle = makeTargetEntry((Expr *) uniqexpr,
676 newtlist = lappend(newtlist, tle);
685 * If the top plan node can't do projections, we need to add a Result
686 * node to help it along.
688 if (!is_projection_capable_plan(subplan))
689 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
691 subplan->targetlist = newtlist;
695 * Build control information showing which subplan output columns are to
696 * be examined by the grouping step. Unfortunately we can't merge this
697 * with the previous loop, since we didn't then know which version of the
698 * subplan tlist we'd end up using.
700 newtlist = subplan->targetlist;
701 numGroupCols = list_length(uniq_exprs);
702 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
705 foreach(l, uniq_exprs)
707 Node *uniqexpr = lfirst(l);
710 tle = tlist_member(uniqexpr, newtlist);
711 if (!tle) /* shouldn't happen */
712 elog(ERROR, "failed to find unique expression in subplan tlist");
713 groupColIdx[groupColPos++] = tle->resno;
716 if (best_path->umethod == UNIQUE_PATH_HASH)
721 numGroups = (long) Min(best_path->rows, (double) LONG_MAX);
724 * Get the hashable equality operators for the Agg node to use.
725 * Normally these are the same as the IN clause operators, but if
726 * those are cross-type operators then the equality operators are the
727 * ones for the IN clause operators' RHS datatype.
729 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
731 foreach(l, in_operators)
733 Oid in_oper = lfirst_oid(l);
736 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
737 elog(ERROR, "could not find compatible hash operator for operator %u",
739 groupOperators[groupColPos++] = eq_oper;
743 * Since the Agg node is going to project anyway, we can give it the
744 * minimum output tlist, without any stuff we might have added to the
747 plan = (Plan *) make_agg(root,
748 build_relation_tlist(best_path->path.parent),
760 List *sortList = NIL;
762 /* Create an ORDER BY list to sort the input compatibly */
764 foreach(l, in_operators)
766 Oid in_oper = lfirst_oid(l);
771 sortop = get_ordering_op_for_equality_op(in_oper, false);
772 if (!OidIsValid(sortop)) /* shouldn't happen */
773 elog(ERROR, "could not find ordering operator for equality operator %u",
775 tle = get_tle_by_resno(subplan->targetlist,
776 groupColIdx[groupColPos]);
778 sortcl = makeNode(SortClause);
779 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
780 subplan->targetlist);
781 sortcl->sortop = sortop;
782 sortcl->nulls_first = false;
783 sortList = lappend(sortList, sortcl);
786 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
787 plan = (Plan *) make_unique(plan, sortList);
790 /* Adjust output size estimate (other fields should be OK already) */
791 plan->plan_rows = best_path->rows;
797 /*****************************************************************************
799 * BASE-RELATION SCAN METHODS
801 *****************************************************************************/
805 * create_seqscan_plan
806 * Returns a seqscan plan for the base relation scanned by 'best_path'
807 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
810 create_seqscan_plan(PlannerInfo *root, Path *best_path,
811 List *tlist, List *scan_clauses)
814 Index scan_relid = best_path->parent->relid;
816 /* it should be a base rel... */
817 Assert(scan_relid > 0);
818 Assert(best_path->parent->rtekind == RTE_RELATION);
820 /* Sort clauses into best execution order */
821 scan_clauses = order_qual_clauses(root, scan_clauses);
823 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
824 scan_clauses = extract_actual_clauses(scan_clauses, false);
826 scan_plan = make_seqscan(tlist,
830 copy_path_costsize(&scan_plan->plan, best_path);
836 * create_indexscan_plan
837 * Returns an indexscan plan for the base relation scanned by 'best_path'
838 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
840 * The indexquals list of the path contains implicitly-ANDed qual conditions.
841 * The list can be empty --- then no index restrictions will be applied during
844 * If nonlossy_clauses isn't NULL, *nonlossy_clauses receives a list of the
845 * nonlossy indexquals.
848 create_indexscan_plan(PlannerInfo *root,
849 IndexPath *best_path,
852 List **nonlossy_clauses)
854 List *indexquals = best_path->indexquals;
855 Index baserelid = best_path->path.parent->relid;
856 Oid indexoid = best_path->indexinfo->indexoid;
858 List *stripped_indexquals;
859 List *fixed_indexquals;
860 List *nonlossy_indexquals;
864 IndexScan *scan_plan;
866 /* it should be a base rel... */
867 Assert(baserelid > 0);
868 Assert(best_path->path.parent->rtekind == RTE_RELATION);
871 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
872 * executor as indexqualorig
874 stripped_indexquals = get_actual_clauses(indexquals);
877 * The executor needs a copy with the indexkey on the left of each clause
878 * and with index attr numbers substituted for table ones. This pass also
879 * gets strategy info and looks for "lossy" operators.
881 fix_indexqual_references(indexquals, best_path,
883 &nonlossy_indexquals,
887 /* pass back nonlossy quals if caller wants 'em */
888 if (nonlossy_clauses)
889 *nonlossy_clauses = nonlossy_indexquals;
892 * If this is an innerjoin scan, the indexclauses will contain join
893 * clauses that are not present in scan_clauses (since the passed-in value
894 * is just the rel's baserestrictinfo list). We must add these clauses to
895 * scan_clauses to ensure they get checked. In most cases we will remove
896 * the join clauses again below, but if a join clause contains a special
897 * operator, we need to make sure it gets into the scan_clauses.
899 * Note: pointer comparison should be enough to determine RestrictInfo
902 if (best_path->isjoininner)
903 scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
906 * The qpqual list must contain all restrictions not automatically handled
907 * by the index. All the predicates in the indexquals will be checked
908 * (either by the index itself, or by nodeIndexscan.c), but if there are
909 * any "special" operators involved then they must be included in qpqual.
910 * Also, any lossy index operators must be rechecked in the qpqual. The
911 * upshot is that qpqual must contain scan_clauses minus whatever appears
912 * in nonlossy_indexquals.
914 * In normal cases simple pointer equality checks will be enough to spot
915 * duplicate RestrictInfos, so we try that first. In some situations
916 * (particularly with OR'd index conditions) we may have scan_clauses that
917 * are not equal to, but are logically implied by, the index quals; so we
918 * also try a predicate_implied_by() check to see if we can discard quals
919 * that way. (predicate_implied_by assumes its first input contains only
920 * immutable functions, so we have to check that.)
922 * We can also discard quals that are implied by a partial index's
923 * predicate, but only in a plain SELECT; when scanning a target relation
924 * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
925 * plan so that they'll be properly rechecked by EvalPlanQual testing.
928 foreach(l, scan_clauses)
930 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
932 Assert(IsA(rinfo, RestrictInfo));
933 if (rinfo->pseudoconstant)
934 continue; /* we may drop pseudoconstants here */
935 if (list_member_ptr(nonlossy_indexquals, rinfo))
937 if (!contain_mutable_functions((Node *) rinfo->clause))
939 List *clausel = list_make1(rinfo->clause);
941 if (predicate_implied_by(clausel, nonlossy_indexquals))
943 if (best_path->indexinfo->indpred)
945 if (baserelid != root->parse->resultRelation &&
946 get_rowmark(root->parse, baserelid) == NULL)
947 if (predicate_implied_by(clausel,
948 best_path->indexinfo->indpred))
952 qpqual = lappend(qpqual, rinfo);
955 /* Sort clauses into best execution order */
956 qpqual = order_qual_clauses(root, qpqual);
958 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
959 qpqual = extract_actual_clauses(qpqual, false);
961 /* Finally ready to build the plan node */
962 scan_plan = make_indexscan(tlist,
970 best_path->indexscandir);
972 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
973 /* use the indexscan-specific rows estimate, not the parent rel's */
974 scan_plan->scan.plan.plan_rows = best_path->rows;
980 * create_bitmap_scan_plan
981 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
982 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
984 static BitmapHeapScan *
985 create_bitmap_scan_plan(PlannerInfo *root,
986 BitmapHeapPath *best_path,
990 Index baserelid = best_path->path.parent->relid;
991 Plan *bitmapqualplan;
992 List *bitmapqualorig;
996 BitmapHeapScan *scan_plan;
998 /* it should be a base rel... */
999 Assert(baserelid > 0);
1000 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1002 /* Process the bitmapqual tree into a Plan tree and qual lists */
1003 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1004 &bitmapqualorig, &indexquals);
1006 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1007 scan_clauses = extract_actual_clauses(scan_clauses, false);
1010 * If this is a innerjoin scan, the indexclauses will contain join clauses
1011 * that are not present in scan_clauses (since the passed-in value is just
1012 * the rel's baserestrictinfo list). We must add these clauses to
1013 * scan_clauses to ensure they get checked. In most cases we will remove
1014 * the join clauses again below, but if a join clause contains a special
1015 * operator, we need to make sure it gets into the scan_clauses.
1017 if (best_path->isjoininner)
1019 scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
1023 * The qpqual list must contain all restrictions not automatically handled
1024 * by the index. All the predicates in the indexquals will be checked
1025 * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
1026 * are any "special" or lossy operators involved then they must be added
1027 * to qpqual. The upshot is that qpqual must contain scan_clauses minus
1028 * whatever appears in indexquals.
1030 * In normal cases simple equal() checks will be enough to spot duplicate
1031 * clauses, so we try that first. In some situations (particularly with
1032 * OR'd index conditions) we may have scan_clauses that are not equal to,
1033 * but are logically implied by, the index quals; so we also try a
1034 * predicate_implied_by() check to see if we can discard quals that way.
1035 * (predicate_implied_by assumes its first input contains only immutable
1036 * functions, so we have to check that.)
1038 * Unlike create_indexscan_plan(), we need take no special thought here
1039 * for partial index predicates; this is because the predicate conditions
1040 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1041 * to do it that way because predicate conditions need to be rechecked if
1042 * the scan becomes lossy.
1045 foreach(l, scan_clauses)
1047 Node *clause = (Node *) lfirst(l);
1049 if (list_member(indexquals, clause))
1051 if (!contain_mutable_functions(clause))
1053 List *clausel = list_make1(clause);
1055 if (predicate_implied_by(clausel, indexquals))
1058 qpqual = lappend(qpqual, clause);
1061 /* Sort clauses into best execution order */
1062 qpqual = order_qual_clauses(root, qpqual);
1065 * When dealing with special or lossy operators, we will at this point
1066 * have duplicate clauses in qpqual and bitmapqualorig. We may as well
1067 * drop 'em from bitmapqualorig, since there's no point in making the
1070 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1072 /* Finally ready to build the plan node */
1073 scan_plan = make_bitmap_heapscan(tlist,
1079 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1080 /* use the indexscan-specific rows estimate, not the parent rel's */
1081 scan_plan->scan.plan.plan_rows = best_path->rows;
1087 * Given a bitmapqual tree, generate the Plan tree that implements it
1089 * As byproducts, we also return in *qual and *indexqual the qual lists
1090 * (in implicit-AND form, without RestrictInfos) describing the original index
1091 * conditions and the generated indexqual conditions. The latter is made to
1092 * exclude lossy index operators. Both lists include partial-index predicates,
1093 * because we have to recheck predicates as well as index conditions if the
1094 * bitmap scan becomes lossy.
1096 * Note: if you find yourself changing this, you probably need to change
1097 * make_restrictinfo_from_bitmapqual too.
1100 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1101 List **qual, List **indexqual)
1105 if (IsA(bitmapqual, BitmapAndPath))
1107 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1108 List *subplans = NIL;
1109 List *subquals = NIL;
1110 List *subindexquals = NIL;
1114 * There may well be redundant quals among the subplans, since a
1115 * top-level WHERE qual might have gotten used to form several
1116 * different index quals. We don't try exceedingly hard to eliminate
1117 * redundancies, but we do eliminate obvious duplicates by using
1118 * list_concat_unique.
1120 foreach(l, apath->bitmapquals)
1126 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1127 &subqual, &subindexqual);
1128 subplans = lappend(subplans, subplan);
1129 subquals = list_concat_unique(subquals, subqual);
1130 subindexquals = list_concat_unique(subindexquals, subindexqual);
1132 plan = (Plan *) make_bitmap_and(subplans);
1133 plan->startup_cost = apath->path.startup_cost;
1134 plan->total_cost = apath->path.total_cost;
1136 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1137 plan->plan_width = 0; /* meaningless */
1139 *indexqual = subindexquals;
1141 else if (IsA(bitmapqual, BitmapOrPath))
1143 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1144 List *subplans = NIL;
1145 List *subquals = NIL;
1146 List *subindexquals = NIL;
1147 bool const_true_subqual = false;
1148 bool const_true_subindexqual = false;
1152 * Here, we only detect qual-free subplans. A qual-free subplan would
1153 * cause us to generate "... OR true ..." which we may as well reduce
1154 * to just "true". We do not try to eliminate redundant subclauses
1155 * because (a) it's not as likely as in the AND case, and (b) we might
1156 * well be working with hundreds or even thousands of OR conditions,
1157 * perhaps from a long IN list. The performance of list_append_unique
1158 * would be unacceptable.
1160 foreach(l, opath->bitmapquals)
1166 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1167 &subqual, &subindexqual);
1168 subplans = lappend(subplans, subplan);
1170 const_true_subqual = true;
1171 else if (!const_true_subqual)
1172 subquals = lappend(subquals,
1173 make_ands_explicit(subqual));
1174 if (subindexqual == NIL)
1175 const_true_subindexqual = true;
1176 else if (!const_true_subindexqual)
1177 subindexquals = lappend(subindexquals,
1178 make_ands_explicit(subindexqual));
1182 * In the presence of ScalarArrayOpExpr quals, we might have built
1183 * BitmapOrPaths with just one subpath; don't add an OR step.
1185 if (list_length(subplans) == 1)
1187 plan = (Plan *) linitial(subplans);
1191 plan = (Plan *) make_bitmap_or(subplans);
1192 plan->startup_cost = opath->path.startup_cost;
1193 plan->total_cost = opath->path.total_cost;
1195 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1196 plan->plan_width = 0; /* meaningless */
1200 * If there were constant-TRUE subquals, the OR reduces to constant
1201 * TRUE. Also, avoid generating one-element ORs, which could happen
1202 * due to redundancy elimination or ScalarArrayOpExpr quals.
1204 if (const_true_subqual)
1206 else if (list_length(subquals) <= 1)
1209 *qual = list_make1(make_orclause(subquals));
1210 if (const_true_subindexqual)
1212 else if (list_length(subindexquals) <= 1)
1213 *indexqual = subindexquals;
1215 *indexqual = list_make1(make_orclause(subindexquals));
1217 else if (IsA(bitmapqual, IndexPath))
1219 IndexPath *ipath = (IndexPath *) bitmapqual;
1221 List *nonlossy_clauses;
1224 /* Use the regular indexscan plan build machinery... */
1225 iscan = create_indexscan_plan(root, ipath, NIL, NIL,
1227 /* then convert to a bitmap indexscan */
1228 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1231 iscan->indexqualorig,
1232 iscan->indexstrategy,
1233 iscan->indexsubtype);
1234 plan->startup_cost = 0.0;
1235 plan->total_cost = ipath->indextotalcost;
1237 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1238 plan->plan_width = 0; /* meaningless */
1239 *qual = get_actual_clauses(ipath->indexclauses);
1240 *indexqual = get_actual_clauses(nonlossy_clauses);
1241 foreach(l, ipath->indexinfo->indpred)
1243 Expr *pred = (Expr *) lfirst(l);
1246 * We know that the index predicate must have been implied by the
1247 * query condition as a whole, but it may or may not be implied by
1248 * the conditions that got pushed into the bitmapqual. Avoid
1249 * generating redundant conditions.
1251 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1253 *qual = lappend(*qual, pred);
1254 *indexqual = lappend(*indexqual, pred);
1260 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1261 plan = NULL; /* keep compiler quiet */
1268 * create_tidscan_plan
1269 * Returns a tidscan plan for the base relation scanned by 'best_path'
1270 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1273 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1274 List *tlist, List *scan_clauses)
1277 Index scan_relid = best_path->path.parent->relid;
1280 /* it should be a base rel... */
1281 Assert(scan_relid > 0);
1282 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1284 /* Sort clauses into best execution order */
1285 scan_clauses = order_qual_clauses(root, scan_clauses);
1287 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1288 scan_clauses = extract_actual_clauses(scan_clauses, false);
1291 * Remove any clauses that are TID quals. This is a bit tricky since the
1292 * tidquals list has implicit OR semantics.
1294 ortidquals = best_path->tidquals;
1295 if (list_length(ortidquals) > 1)
1296 ortidquals = list_make1(make_orclause(ortidquals));
1297 scan_clauses = list_difference(scan_clauses, ortidquals);
1299 scan_plan = make_tidscan(tlist,
1302 best_path->tidquals);
1304 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1310 * create_subqueryscan_plan
1311 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1312 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1314 static SubqueryScan *
1315 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1316 List *tlist, List *scan_clauses)
1318 SubqueryScan *scan_plan;
1319 Index scan_relid = best_path->parent->relid;
1321 /* it should be a subquery base rel... */
1322 Assert(scan_relid > 0);
1323 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1325 /* Sort clauses into best execution order */
1326 scan_clauses = order_qual_clauses(root, scan_clauses);
1328 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1329 scan_clauses = extract_actual_clauses(scan_clauses, false);
1331 scan_plan = make_subqueryscan(tlist,
1334 best_path->parent->subplan,
1335 best_path->parent->subrtable);
1337 copy_path_costsize(&scan_plan->scan.plan, best_path);
1343 * create_functionscan_plan
1344 * Returns a functionscan plan for the base relation scanned by 'best_path'
1345 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1347 static FunctionScan *
1348 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1349 List *tlist, List *scan_clauses)
1351 FunctionScan *scan_plan;
1352 Index scan_relid = best_path->parent->relid;
1355 /* it should be a function base rel... */
1356 Assert(scan_relid > 0);
1357 rte = planner_rt_fetch(scan_relid, root);
1358 Assert(rte->rtekind == RTE_FUNCTION);
1360 /* Sort clauses into best execution order */
1361 scan_clauses = order_qual_clauses(root, scan_clauses);
1363 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1364 scan_clauses = extract_actual_clauses(scan_clauses, false);
1366 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1368 rte->eref->colnames,
1370 rte->funccoltypmods);
1372 copy_path_costsize(&scan_plan->scan.plan, best_path);
1378 * create_valuesscan_plan
1379 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1380 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1383 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1384 List *tlist, List *scan_clauses)
1386 ValuesScan *scan_plan;
1387 Index scan_relid = best_path->parent->relid;
1390 /* it should be a values base rel... */
1391 Assert(scan_relid > 0);
1392 rte = planner_rt_fetch(scan_relid, root);
1393 Assert(rte->rtekind == RTE_VALUES);
1395 /* Sort clauses into best execution order */
1396 scan_clauses = order_qual_clauses(root, scan_clauses);
1398 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1399 scan_clauses = extract_actual_clauses(scan_clauses, false);
1401 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1404 copy_path_costsize(&scan_plan->scan.plan, best_path);
1409 /*****************************************************************************
1413 *****************************************************************************/
1416 create_nestloop_plan(PlannerInfo *root,
1417 NestPath *best_path,
1421 List *tlist = build_relation_tlist(best_path->path.parent);
1422 List *joinrestrictclauses = best_path->joinrestrictinfo;
1425 NestLoop *join_plan;
1427 if (IsA(best_path->innerjoinpath, IndexPath))
1430 * An index is being used to reduce the number of tuples scanned in
1431 * the inner relation. If there are join clauses being used with the
1432 * index, we may remove those join clauses from the list of clauses
1433 * that have to be checked as qpquals at the join node.
1435 * We can also remove any join clauses that are redundant with those
1436 * being used in the index scan; this check is needed because
1437 * find_eclass_clauses_for_index_join() may emit different clauses
1438 * than generate_join_implied_equalities() did.
1440 * We can skip this if the index path is an ordinary indexpath and not
1441 * a special innerjoin path, since it then wouldn't be using any join
1444 IndexPath *innerpath = (IndexPath *) best_path->innerjoinpath;
1446 if (innerpath->isjoininner)
1447 joinrestrictclauses =
1448 select_nonredundant_join_clauses(root,
1449 joinrestrictclauses,
1450 innerpath->indexclauses);
1452 else if (IsA(best_path->innerjoinpath, BitmapHeapPath))
1455 * Same deal for bitmapped index scans.
1457 * Note: both here and above, we ignore any implicit index
1458 * restrictions associated with the use of partial indexes. This is
1459 * OK because we're only trying to prove we can dispense with some
1460 * join quals; failing to prove that doesn't result in an incorrect
1461 * plan. It is the right way to proceed because adding more quals to
1462 * the stuff we got from the original query would just make it harder
1463 * to detect duplication. (Also, to change this we'd have to be wary
1464 * of UPDATE/DELETE/SELECT FOR UPDATE target relations; see notes
1465 * above about EvalPlanQual.)
1467 BitmapHeapPath *innerpath = (BitmapHeapPath *) best_path->innerjoinpath;
1469 if (innerpath->isjoininner)
1471 List *bitmapclauses;
1474 make_restrictinfo_from_bitmapqual(innerpath->bitmapqual,
1477 joinrestrictclauses =
1478 select_nonredundant_join_clauses(root,
1479 joinrestrictclauses,
1484 /* Sort join qual clauses into best execution order */
1485 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
1487 /* Get the join qual clauses (in plain expression form) */
1488 /* Any pseudoconstant clauses are ignored here */
1489 if (IS_OUTER_JOIN(best_path->jointype))
1491 extract_actual_join_clauses(joinrestrictclauses,
1492 &joinclauses, &otherclauses);
1496 /* We can treat all clauses alike for an inner join */
1497 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
1501 join_plan = make_nestloop(tlist,
1506 best_path->jointype);
1508 copy_path_costsize(&join_plan->join.plan, &best_path->path);
1514 create_mergejoin_plan(PlannerInfo *root,
1515 MergePath *best_path,
1519 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1523 List *outerpathkeys;
1524 List *innerpathkeys;
1527 int *mergestrategies;
1528 bool *mergenullsfirst;
1529 MergeJoin *join_plan;
1531 EquivalenceClass *lastoeclass;
1532 EquivalenceClass *lastieclass;
1539 /* Sort join qual clauses into best execution order */
1540 /* NB: do NOT reorder the mergeclauses */
1541 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1543 /* Get the join qual clauses (in plain expression form) */
1544 /* Any pseudoconstant clauses are ignored here */
1545 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1547 extract_actual_join_clauses(joinclauses,
1548 &joinclauses, &otherclauses);
1552 /* We can treat all clauses alike for an inner join */
1553 joinclauses = extract_actual_clauses(joinclauses, false);
1558 * Remove the mergeclauses from the list of join qual clauses, leaving the
1559 * list of quals that must be checked as qpquals.
1561 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
1562 joinclauses = list_difference(joinclauses, mergeclauses);
1565 * Rearrange mergeclauses, if needed, so that the outer variable is always
1566 * on the left; mark the mergeclause restrictinfos with correct
1567 * outer_is_left status.
1569 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
1570 best_path->jpath.outerjoinpath->parent->relids);
1573 * Create explicit sort nodes for the outer and inner join paths if
1574 * necessary. The sort cost was already accounted for in the path. Make
1575 * sure there are no excess columns in the inputs if sorting.
1577 if (best_path->outersortkeys)
1579 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1580 outer_plan = (Plan *)
1581 make_sort_from_pathkeys(root,
1583 best_path->outersortkeys,
1585 outerpathkeys = best_path->outersortkeys;
1588 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
1590 if (best_path->innersortkeys)
1592 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1593 inner_plan = (Plan *)
1594 make_sort_from_pathkeys(root,
1596 best_path->innersortkeys,
1598 innerpathkeys = best_path->innersortkeys;
1601 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
1604 * If inner plan is a sort that is expected to spill to disk, add a
1605 * materialize node to shield it from the need to handle mark/restore.
1606 * This will allow it to perform the last merge pass on-the-fly, while in
1607 * most cases not requiring the materialize to spill to disk.
1609 * XXX really, Sort oughta do this for itself, probably, to avoid the
1610 * overhead of a separate plan node.
1612 if (IsA(inner_plan, Sort) &&
1613 sort_exceeds_work_mem((Sort *) inner_plan))
1615 Plan *matplan = (Plan *) make_material(inner_plan);
1618 * We assume the materialize will not spill to disk, and therefore
1619 * charge just cpu_tuple_cost per tuple.
1621 copy_plan_costsize(matplan, inner_plan);
1622 matplan->total_cost += cpu_tuple_cost * matplan->plan_rows;
1624 inner_plan = matplan;
1628 * Compute the opfamily/strategy/nullsfirst arrays needed by the executor.
1629 * The information is in the pathkeys for the two inputs, but we need to
1630 * be careful about the possibility of mergeclauses sharing a pathkey
1631 * (compare find_mergeclauses_for_pathkeys()).
1633 nClauses = list_length(mergeclauses);
1634 Assert(nClauses == list_length(best_path->path_mergeclauses));
1635 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
1636 mergestrategies = (int *) palloc(nClauses * sizeof(int));
1637 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
1643 lop = list_head(outerpathkeys);
1644 lip = list_head(innerpathkeys);
1646 foreach(lc, best_path->path_mergeclauses)
1648 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1649 EquivalenceClass *oeclass;
1650 EquivalenceClass *ieclass;
1652 /* fetch outer/inner eclass from mergeclause */
1653 Assert(IsA(rinfo, RestrictInfo));
1654 if (rinfo->outer_is_left)
1656 oeclass = rinfo->left_ec;
1657 ieclass = rinfo->right_ec;
1661 oeclass = rinfo->right_ec;
1662 ieclass = rinfo->left_ec;
1664 Assert(oeclass != NULL);
1665 Assert(ieclass != NULL);
1667 /* should match current or next pathkeys */
1668 /* we check this carefully for debugging reasons */
1669 if (oeclass != lastoeclass)
1672 elog(ERROR, "too few pathkeys for mergeclauses");
1673 opathkey = (PathKey *) lfirst(lop);
1675 lastoeclass = opathkey->pk_eclass;
1676 if (oeclass != lastoeclass)
1677 elog(ERROR, "outer pathkeys do not match mergeclause");
1679 if (ieclass != lastieclass)
1682 elog(ERROR, "too few pathkeys for mergeclauses");
1683 ipathkey = (PathKey *) lfirst(lip);
1685 lastieclass = ipathkey->pk_eclass;
1686 if (ieclass != lastieclass)
1687 elog(ERROR, "inner pathkeys do not match mergeclause");
1689 /* pathkeys should match each other too (more debugging) */
1690 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
1691 opathkey->pk_strategy != ipathkey->pk_strategy ||
1692 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
1693 elog(ERROR, "left and right pathkeys do not match in mergejoin");
1695 /* OK, save info for executor */
1696 mergefamilies[i] = opathkey->pk_opfamily;
1697 mergestrategies[i] = opathkey->pk_strategy;
1698 mergenullsfirst[i] = opathkey->pk_nulls_first;
1704 * Now we can build the mergejoin node.
1706 join_plan = make_mergejoin(tlist,
1715 best_path->jpath.jointype);
1717 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1723 create_hashjoin_plan(PlannerInfo *root,
1724 HashPath *best_path,
1728 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1732 HashJoin *join_plan;
1735 /* Sort join qual clauses into best execution order */
1736 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1737 /* There's no point in sorting the hash clauses ... */
1739 /* Get the join qual clauses (in plain expression form) */
1740 /* Any pseudoconstant clauses are ignored here */
1741 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1743 extract_actual_join_clauses(joinclauses,
1744 &joinclauses, &otherclauses);
1748 /* We can treat all clauses alike for an inner join */
1749 joinclauses = extract_actual_clauses(joinclauses, false);
1754 * Remove the hashclauses from the list of join qual clauses, leaving the
1755 * list of quals that must be checked as qpquals.
1757 hashclauses = get_actual_clauses(best_path->path_hashclauses);
1758 joinclauses = list_difference(joinclauses, hashclauses);
1761 * Rearrange hashclauses, if needed, so that the outer variable is always
1764 hashclauses = get_switched_clauses(best_path->path_hashclauses,
1765 best_path->jpath.outerjoinpath->parent->relids);
1767 /* We don't want any excess columns in the hashed tuples */
1768 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1771 * Build the hash node and hash join node.
1773 hash_plan = make_hash(inner_plan);
1774 join_plan = make_hashjoin(tlist,
1780 best_path->jpath.jointype);
1782 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1788 /*****************************************************************************
1790 * SUPPORTING ROUTINES
1792 *****************************************************************************/
1795 * fix_indexqual_references
1796 * Adjust indexqual clauses to the form the executor's indexqual
1797 * machinery needs, and check for recheckable (lossy) index conditions.
1799 * We have five tasks here:
1800 * * Remove RestrictInfo nodes from the input clauses.
1801 * * Index keys must be represented by Var nodes with varattno set to the
1802 * index's attribute number, not the attribute number in the original rel.
1803 * * If the index key is on the right, commute the clause to put it on the
1805 * * We must construct lists of operator strategy numbers and subtypes
1806 * for the top-level operators of each index clause.
1807 * * We must detect any lossy index operators. The API is that we return
1808 * a list of the input clauses whose operators are NOT lossy.
1810 * fixed_indexquals receives a modified copy of the indexquals list --- the
1811 * original is not changed. Note also that the copy shares no substructure
1812 * with the original; this is needed in case there is a subplan in it (we need
1813 * two separate copies of the subplan tree, or things will go awry).
1815 * nonlossy_indexquals receives a list of the original input clauses (with
1816 * RestrictInfos) that contain non-lossy operators.
1818 * indexstrategy receives an integer list of strategy numbers.
1819 * indexsubtype receives an OID list of strategy subtypes.
1822 fix_indexqual_references(List *indexquals, IndexPath *index_path,
1823 List **fixed_indexquals,
1824 List **nonlossy_indexquals,
1825 List **indexstrategy,
1826 List **indexsubtype)
1828 IndexOptInfo *index = index_path->indexinfo;
1831 *fixed_indexquals = NIL;
1832 *nonlossy_indexquals = NIL;
1833 *indexstrategy = NIL;
1834 *indexsubtype = NIL;
1837 * For each qual clause, commute if needed to put the indexkey operand on
1838 * the left, and then fix its varattno. (We do not need to change the
1839 * other side of the clause.) Then determine the operator's strategy
1840 * number and subtype number, and check for lossy index behavior.
1842 foreach(l, indexquals)
1844 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1852 bool is_null_op = false;
1854 Assert(IsA(rinfo, RestrictInfo));
1857 * Make a copy that will become the fixed clause.
1859 * We used to try to do a shallow copy here, but that fails if there
1860 * is a subplan in the arguments of the opclause. So just do a full
1863 clause = (Expr *) copyObject((Node *) rinfo->clause);
1865 if (IsA(clause, OpExpr))
1867 OpExpr *op = (OpExpr *) clause;
1869 if (list_length(op->args) != 2)
1870 elog(ERROR, "indexqual clause is not binary opclause");
1873 * Check to see if the indexkey is on the right; if so, commute
1874 * the clause. The indexkey should be the side that refers to
1875 * (only) the base relation.
1877 if (!bms_equal(rinfo->left_relids, index->rel->relids))
1881 * Now, determine which index attribute this is, change the
1882 * indexkey operand as needed, and get the index opfamily.
1884 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
1887 clause_op = op->opno;
1889 else if (IsA(clause, RowCompareExpr))
1891 RowCompareExpr *rc = (RowCompareExpr *) clause;
1895 * Check to see if the indexkey is on the right; if so, commute
1896 * the clause. The indexkey should be the side that refers to
1897 * (only) the base relation.
1899 if (!bms_overlap(pull_varnos(linitial(rc->largs)),
1900 index->rel->relids))
1901 CommuteRowCompareExpr(rc);
1904 * For each column in the row comparison, determine which index
1905 * attribute this is and change the indexkey operand as needed.
1907 * Save the index opfamily for only the first column. We will
1908 * return the operator and opfamily info for just the first column
1909 * of the row comparison; the executor will have to look up the
1910 * rest if it needs them.
1912 foreach(lc, rc->largs)
1916 lfirst(lc) = fix_indexqual_operand(lfirst(lc),
1919 if (lc == list_head(rc->largs))
1920 opfamily = tmp_opfamily;
1922 clause_op = linitial_oid(rc->opnos);
1924 else if (IsA(clause, ScalarArrayOpExpr))
1926 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
1928 /* Never need to commute... */
1931 * Now, determine which index attribute this is, change the
1932 * indexkey operand as needed, and get the index opfamily.
1934 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
1937 clause_op = saop->opno;
1939 else if (IsA(clause, NullTest))
1941 NullTest *nt = (NullTest *) clause;
1943 Assert(nt->nulltesttype == IS_NULL);
1944 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
1948 clause_op = InvalidOid; /* keep compiler quiet */
1952 elog(ERROR, "unsupported indexqual type: %d",
1953 (int) nodeTag(clause));
1954 continue; /* keep compiler quiet */
1957 *fixed_indexquals = lappend(*fixed_indexquals, clause);
1961 /* IS NULL doesn't have a clause_op */
1962 stratno = InvalidStrategy;
1963 stratrighttype = InvalidOid;
1964 /* We assume it's non-lossy ... might need more work someday */
1970 * Look up the (possibly commuted) operator in the operator family
1971 * to get its strategy number and the recheck indicator. This also
1972 * double-checks that we found an operator matching the index.
1974 get_op_opfamily_properties(clause_op, opfamily,
1981 *indexstrategy = lappend_int(*indexstrategy, stratno);
1982 *indexsubtype = lappend_oid(*indexsubtype, stratrighttype);
1984 /* If it's not lossy, add to nonlossy_indexquals */
1986 *nonlossy_indexquals = lappend(*nonlossy_indexquals, rinfo);
1991 fix_indexqual_operand(Node *node, IndexOptInfo *index, Oid *opfamily)
1994 * We represent index keys by Var nodes having the varno of the base table
1995 * but varattno equal to the index's attribute number (index column
1996 * position). This is a bit hokey ... would be cleaner to use a
1997 * special-purpose node type that could not be mistaken for a regular Var.
1998 * But it will do for now.
2002 ListCell *indexpr_item;
2005 * Remove any binary-compatible relabeling of the indexkey
2007 if (IsA(node, RelabelType))
2008 node = (Node *) ((RelabelType *) node)->arg;
2010 if (IsA(node, Var) &&
2011 ((Var *) node)->varno == index->rel->relid)
2013 /* Try to match against simple index columns */
2014 int varatt = ((Var *) node)->varattno;
2018 for (pos = 0; pos < index->ncolumns; pos++)
2020 if (index->indexkeys[pos] == varatt)
2022 result = (Var *) copyObject(node);
2023 result->varattno = pos + 1;
2024 /* return the correct opfamily, too */
2025 *opfamily = index->opfamily[pos];
2026 return (Node *) result;
2032 /* Try to match against index expressions */
2033 indexpr_item = list_head(index->indexprs);
2034 for (pos = 0; pos < index->ncolumns; pos++)
2036 if (index->indexkeys[pos] == 0)
2040 if (indexpr_item == NULL)
2041 elog(ERROR, "too few entries in indexprs list");
2042 indexkey = (Node *) lfirst(indexpr_item);
2043 if (indexkey && IsA(indexkey, RelabelType))
2044 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2045 if (equal(node, indexkey))
2048 result = makeVar(index->rel->relid, pos + 1,
2049 exprType(lfirst(indexpr_item)), -1,
2051 /* return the correct opfamily, too */
2052 *opfamily = index->opfamily[pos];
2053 return (Node *) result;
2055 indexpr_item = lnext(indexpr_item);
2060 elog(ERROR, "node is not an index attribute");
2061 *opfamily = InvalidOid; /* keep compiler quiet */
2066 * get_switched_clauses
2067 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2068 * extract the bare clauses, and rearrange the elements within the
2069 * clauses, if needed, so the outer join variable is on the left and
2070 * the inner is on the right. The original clause data structure is not
2071 * touched; a modified list is returned. We do, however, set the transient
2072 * outer_is_left field in each RestrictInfo to show which side was which.
2075 get_switched_clauses(List *clauses, Relids outerrelids)
2082 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
2083 OpExpr *clause = (OpExpr *) restrictinfo->clause;
2085 Assert(is_opclause(clause));
2086 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
2089 * Duplicate just enough of the structure to allow commuting the
2090 * clause without changing the original list. Could use
2091 * copyObject, but a complete deep copy is overkill.
2093 OpExpr *temp = makeNode(OpExpr);
2095 temp->opno = clause->opno;
2096 temp->opfuncid = InvalidOid;
2097 temp->opresulttype = clause->opresulttype;
2098 temp->opretset = clause->opretset;
2099 temp->args = list_copy(clause->args);
2100 /* Commute it --- note this modifies the temp node in-place. */
2101 CommuteOpExpr(temp);
2102 t_list = lappend(t_list, temp);
2103 restrictinfo->outer_is_left = false;
2107 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
2108 t_list = lappend(t_list, clause);
2109 restrictinfo->outer_is_left = true;
2116 * order_qual_clauses
2117 * Given a list of qual clauses that will all be evaluated at the same
2118 * plan node, sort the list into the order we want to check the quals
2121 * Ideally the order should be driven by a combination of execution cost and
2122 * selectivity, but it's not immediately clear how to account for both,
2123 * and given the uncertainty of the estimates the reliability of the decisions
2124 * would be doubtful anyway. So we just order by estimated per-tuple cost,
2125 * being careful not to change the order when (as is often the case) the
2126 * estimates are identical.
2128 * Although this will work on either bare clauses or RestrictInfos, it's
2129 * much faster to apply it to RestrictInfos, since it can re-use cost
2130 * information that is cached in RestrictInfos.
2132 * Note: some callers pass lists that contain entries that will later be
2133 * removed; this is the easiest way to let this routine see RestrictInfos
2134 * instead of bare clauses. It's OK because we only sort by cost, but
2135 * a cost/selectivity combination would likely do the wrong thing.
2138 order_qual_clauses(PlannerInfo *root, List *clauses)
2145 int nitems = list_length(clauses);
2151 /* No need to work hard for 0 or 1 clause */
2156 * Collect the items and costs into an array. This is to avoid repeated
2157 * cost_qual_eval work if the inputs aren't RestrictInfos.
2159 items = (QualItem *) palloc(nitems * sizeof(QualItem));
2161 foreach(lc, clauses)
2163 Node *clause = (Node *) lfirst(lc);
2166 cost_qual_eval_node(&qcost, clause, root);
2167 items[i].clause = clause;
2168 items[i].cost = qcost.per_tuple;
2173 * Sort. We don't use qsort() because it's not guaranteed stable for
2174 * equal keys. The expected number of entries is small enough that a
2175 * simple insertion sort should be good enough.
2177 for (i = 1; i < nitems; i++)
2179 QualItem newitem = items[i];
2182 /* insert newitem into the already-sorted subarray */
2183 for (j = i; j > 0; j--)
2185 if (newitem.cost >= items[j - 1].cost)
2187 items[j] = items[j - 1];
2192 /* Convert back to a list */
2194 for (i = 0; i < nitems; i++)
2195 result = lappend(result, items[i].clause);
2201 * Copy cost and size info from a Path node to the Plan node created from it.
2202 * The executor won't use this info, but it's needed by EXPLAIN.
2205 copy_path_costsize(Plan *dest, Path *src)
2209 dest->startup_cost = src->startup_cost;
2210 dest->total_cost = src->total_cost;
2211 dest->plan_rows = src->parent->rows;
2212 dest->plan_width = src->parent->width;
2216 dest->startup_cost = 0;
2217 dest->total_cost = 0;
2218 dest->plan_rows = 0;
2219 dest->plan_width = 0;
2224 * Copy cost and size info from a lower plan node to an inserted node.
2225 * This is not critical, since the decisions have already been made,
2226 * but it helps produce more reasonable-looking EXPLAIN output.
2227 * (Some callers alter the info after copying it.)
2230 copy_plan_costsize(Plan *dest, Plan *src)
2234 dest->startup_cost = src->startup_cost;
2235 dest->total_cost = src->total_cost;
2236 dest->plan_rows = src->plan_rows;
2237 dest->plan_width = src->plan_width;
2241 dest->startup_cost = 0;
2242 dest->total_cost = 0;
2243 dest->plan_rows = 0;
2244 dest->plan_width = 0;
2249 /*****************************************************************************
2251 * PLAN NODE BUILDING ROUTINES
2253 * Some of these are exported because they are called to build plan nodes
2254 * in contexts where we're not deriving the plan node from a path node.
2256 *****************************************************************************/
2259 make_seqscan(List *qptlist,
2263 SeqScan *node = makeNode(SeqScan);
2264 Plan *plan = &node->plan;
2266 /* cost should be inserted by caller */
2267 plan->targetlist = qptlist;
2268 plan->qual = qpqual;
2269 plan->lefttree = NULL;
2270 plan->righttree = NULL;
2271 node->scanrelid = scanrelid;
2277 make_indexscan(List *qptlist,
2282 List *indexqualorig,
2283 List *indexstrategy,
2285 ScanDirection indexscandir)
2287 IndexScan *node = makeNode(IndexScan);
2288 Plan *plan = &node->scan.plan;
2290 /* cost should be inserted by caller */
2291 plan->targetlist = qptlist;
2292 plan->qual = qpqual;
2293 plan->lefttree = NULL;
2294 plan->righttree = NULL;
2295 node->scan.scanrelid = scanrelid;
2296 node->indexid = indexid;
2297 node->indexqual = indexqual;
2298 node->indexqualorig = indexqualorig;
2299 node->indexstrategy = indexstrategy;
2300 node->indexsubtype = indexsubtype;
2301 node->indexorderdir = indexscandir;
2306 static BitmapIndexScan *
2307 make_bitmap_indexscan(Index scanrelid,
2310 List *indexqualorig,
2311 List *indexstrategy,
2314 BitmapIndexScan *node = makeNode(BitmapIndexScan);
2315 Plan *plan = &node->scan.plan;
2317 /* cost should be inserted by caller */
2318 plan->targetlist = NIL; /* not used */
2319 plan->qual = NIL; /* not used */
2320 plan->lefttree = NULL;
2321 plan->righttree = NULL;
2322 node->scan.scanrelid = scanrelid;
2323 node->indexid = indexid;
2324 node->indexqual = indexqual;
2325 node->indexqualorig = indexqualorig;
2326 node->indexstrategy = indexstrategy;
2327 node->indexsubtype = indexsubtype;
2332 static BitmapHeapScan *
2333 make_bitmap_heapscan(List *qptlist,
2336 List *bitmapqualorig,
2339 BitmapHeapScan *node = makeNode(BitmapHeapScan);
2340 Plan *plan = &node->scan.plan;
2342 /* cost should be inserted by caller */
2343 plan->targetlist = qptlist;
2344 plan->qual = qpqual;
2345 plan->lefttree = lefttree;
2346 plan->righttree = NULL;
2347 node->scan.scanrelid = scanrelid;
2348 node->bitmapqualorig = bitmapqualorig;
2354 make_tidscan(List *qptlist,
2359 TidScan *node = makeNode(TidScan);
2360 Plan *plan = &node->scan.plan;
2362 /* cost should be inserted by caller */
2363 plan->targetlist = qptlist;
2364 plan->qual = qpqual;
2365 plan->lefttree = NULL;
2366 plan->righttree = NULL;
2367 node->scan.scanrelid = scanrelid;
2368 node->tidquals = tidquals;
2374 make_subqueryscan(List *qptlist,
2380 SubqueryScan *node = makeNode(SubqueryScan);
2381 Plan *plan = &node->scan.plan;
2384 * Cost is figured here for the convenience of prepunion.c. Note this is
2385 * only correct for the case where qpqual is empty; otherwise caller
2386 * should overwrite cost with a better estimate.
2388 copy_plan_costsize(plan, subplan);
2389 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
2391 plan->targetlist = qptlist;
2392 plan->qual = qpqual;
2393 plan->lefttree = NULL;
2394 plan->righttree = NULL;
2395 node->scan.scanrelid = scanrelid;
2396 node->subplan = subplan;
2397 node->subrtable = subrtable;
2402 static FunctionScan *
2403 make_functionscan(List *qptlist,
2409 List *funccoltypmods)
2411 FunctionScan *node = makeNode(FunctionScan);
2412 Plan *plan = &node->scan.plan;
2414 /* cost should be inserted by caller */
2415 plan->targetlist = qptlist;
2416 plan->qual = qpqual;
2417 plan->lefttree = NULL;
2418 plan->righttree = NULL;
2419 node->scan.scanrelid = scanrelid;
2420 node->funcexpr = funcexpr;
2421 node->funccolnames = funccolnames;
2422 node->funccoltypes = funccoltypes;
2423 node->funccoltypmods = funccoltypmods;
2429 make_valuesscan(List *qptlist,
2434 ValuesScan *node = makeNode(ValuesScan);
2435 Plan *plan = &node->scan.plan;
2437 /* cost should be inserted by caller */
2438 plan->targetlist = qptlist;
2439 plan->qual = qpqual;
2440 plan->lefttree = NULL;
2441 plan->righttree = NULL;
2442 node->scan.scanrelid = scanrelid;
2443 node->values_lists = values_lists;
2449 make_append(List *appendplans, bool isTarget, List *tlist)
2451 Append *node = makeNode(Append);
2452 Plan *plan = &node->plan;
2456 * Compute cost as sum of subplan costs. We charge nothing extra for the
2457 * Append itself, which perhaps is too optimistic, but since it doesn't do
2458 * any selection or projection, it is a pretty cheap node.
2460 plan->startup_cost = 0;
2461 plan->total_cost = 0;
2462 plan->plan_rows = 0;
2463 plan->plan_width = 0;
2464 foreach(subnode, appendplans)
2466 Plan *subplan = (Plan *) lfirst(subnode);
2468 if (subnode == list_head(appendplans)) /* first node? */
2469 plan->startup_cost = subplan->startup_cost;
2470 plan->total_cost += subplan->total_cost;
2471 plan->plan_rows += subplan->plan_rows;
2472 if (plan->plan_width < subplan->plan_width)
2473 plan->plan_width = subplan->plan_width;
2476 plan->targetlist = tlist;
2478 plan->lefttree = NULL;
2479 plan->righttree = NULL;
2480 node->appendplans = appendplans;
2481 node->isTarget = isTarget;
2487 make_bitmap_and(List *bitmapplans)
2489 BitmapAnd *node = makeNode(BitmapAnd);
2490 Plan *plan = &node->plan;
2492 /* cost should be inserted by caller */
2493 plan->targetlist = NIL;
2495 plan->lefttree = NULL;
2496 plan->righttree = NULL;
2497 node->bitmapplans = bitmapplans;
2503 make_bitmap_or(List *bitmapplans)
2505 BitmapOr *node = makeNode(BitmapOr);
2506 Plan *plan = &node->plan;
2508 /* cost should be inserted by caller */
2509 plan->targetlist = NIL;
2511 plan->lefttree = NULL;
2512 plan->righttree = NULL;
2513 node->bitmapplans = bitmapplans;
2519 make_nestloop(List *tlist,
2526 NestLoop *node = makeNode(NestLoop);
2527 Plan *plan = &node->join.plan;
2529 /* cost should be inserted by caller */
2530 plan->targetlist = tlist;
2531 plan->qual = otherclauses;
2532 plan->lefttree = lefttree;
2533 plan->righttree = righttree;
2534 node->join.jointype = jointype;
2535 node->join.joinqual = joinclauses;
2541 make_hashjoin(List *tlist,
2549 HashJoin *node = makeNode(HashJoin);
2550 Plan *plan = &node->join.plan;
2552 /* cost should be inserted by caller */
2553 plan->targetlist = tlist;
2554 plan->qual = otherclauses;
2555 plan->lefttree = lefttree;
2556 plan->righttree = righttree;
2557 node->hashclauses = hashclauses;
2558 node->join.jointype = jointype;
2559 node->join.joinqual = joinclauses;
2565 make_hash(Plan *lefttree)
2567 Hash *node = makeNode(Hash);
2568 Plan *plan = &node->plan;
2570 copy_plan_costsize(plan, lefttree);
2573 * For plausibility, make startup & total costs equal total cost of input
2574 * plan; this only affects EXPLAIN display not decisions.
2576 plan->startup_cost = plan->total_cost;
2577 plan->targetlist = lefttree->targetlist;
2579 plan->lefttree = lefttree;
2580 plan->righttree = NULL;
2586 make_mergejoin(List *tlist,
2591 int *mergestrategies,
2592 bool *mergenullsfirst,
2597 MergeJoin *node = makeNode(MergeJoin);
2598 Plan *plan = &node->join.plan;
2600 /* cost should be inserted by caller */
2601 plan->targetlist = tlist;
2602 plan->qual = otherclauses;
2603 plan->lefttree = lefttree;
2604 plan->righttree = righttree;
2605 node->mergeclauses = mergeclauses;
2606 node->mergeFamilies = mergefamilies;
2607 node->mergeStrategies = mergestrategies;
2608 node->mergeNullsFirst = mergenullsfirst;
2609 node->join.jointype = jointype;
2610 node->join.joinqual = joinclauses;
2616 * make_sort --- basic routine to build a Sort plan node
2618 * Caller must have built the sortColIdx, sortOperators, and nullsFirst
2619 * arrays already. limit_tuples is as for cost_sort (in particular, pass
2623 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
2624 AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
2625 double limit_tuples)
2627 Sort *node = makeNode(Sort);
2628 Plan *plan = &node->plan;
2629 Path sort_path; /* dummy for result of cost_sort */
2631 copy_plan_costsize(plan, lefttree); /* only care about copying size */
2632 cost_sort(&sort_path, root, NIL,
2633 lefttree->total_cost,
2634 lefttree->plan_rows,
2635 lefttree->plan_width,
2637 plan->startup_cost = sort_path.startup_cost;
2638 plan->total_cost = sort_path.total_cost;
2639 plan->targetlist = lefttree->targetlist;
2641 plan->lefttree = lefttree;
2642 plan->righttree = NULL;
2643 node->numCols = numCols;
2644 node->sortColIdx = sortColIdx;
2645 node->sortOperators = sortOperators;
2646 node->nullsFirst = nullsFirst;
2652 * add_sort_column --- utility subroutine for building sort info arrays
2654 * We need this routine because the same column might be selected more than
2655 * once as a sort key column; if so, the extra mentions are redundant.
2657 * Caller is assumed to have allocated the arrays large enough for the
2658 * max possible number of columns. Return value is the new column count.
2661 add_sort_column(AttrNumber colIdx, Oid sortOp, bool nulls_first,
2662 int numCols, AttrNumber *sortColIdx,
2663 Oid *sortOperators, bool *nullsFirst)
2667 for (i = 0; i < numCols; i++)
2670 * Note: we check sortOp because it's conceivable that "ORDER BY foo
2671 * USING <, foo USING <<<" is not redundant, if <<< distinguishes
2672 * values that < considers equal. We need not check nulls_first
2673 * however because a lower-order column with the same sortop but
2674 * opposite nulls direction is redundant.
2676 if (sortColIdx[i] == colIdx &&
2677 sortOperators[numCols] == sortOp)
2679 /* Already sorting by this col, so extra sort key is useless */
2684 /* Add the column */
2685 sortColIdx[numCols] = colIdx;
2686 sortOperators[numCols] = sortOp;
2687 nullsFirst[numCols] = nulls_first;
2692 * make_sort_from_pathkeys
2693 * Create sort plan to sort according to given pathkeys
2695 * 'lefttree' is the node which yields input tuples
2696 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
2697 * 'limit_tuples' is the bound on the number of output tuples;
2700 * We must convert the pathkey information into arrays of sort key column
2701 * numbers and sort operator OIDs.
2703 * If the pathkeys include expressions that aren't simple Vars, we will
2704 * usually need to add resjunk items to the input plan's targetlist to
2705 * compute these expressions (since the Sort node itself won't do it).
2706 * If the input plan type isn't one that can do projections, this means
2707 * adding a Result node just to do the projection.
2710 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
2711 double limit_tuples)
2713 List *tlist = lefttree->targetlist;
2716 AttrNumber *sortColIdx;
2721 * We will need at most list_length(pathkeys) sort columns; possibly less
2723 numsortkeys = list_length(pathkeys);
2724 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2725 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2726 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2730 foreach(i, pathkeys)
2732 PathKey *pathkey = (PathKey *) lfirst(i);
2733 EquivalenceClass *ec = pathkey->pk_eclass;
2734 TargetEntry *tle = NULL;
2735 Oid pk_datatype = InvalidOid;
2739 if (ec->ec_has_volatile)
2742 * If the pathkey's EquivalenceClass is volatile, then it must
2743 * have come from an ORDER BY clause, and we have to match it to
2744 * that same targetlist entry.
2746 if (ec->ec_sortref == 0) /* can't happen */
2747 elog(ERROR, "volatile EquivalenceClass has no sortref");
2748 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
2750 Assert(list_length(ec->ec_members) == 1);
2751 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
2756 * Otherwise, we can sort by any non-constant expression listed in
2757 * the pathkey's EquivalenceClass. For now, we take the first one
2758 * that corresponds to an available item in the tlist. If there
2759 * isn't any, use the first one that is an expression in the
2760 * input's vars. (The non-const restriction only matters if the
2761 * EC is below_outer_join; but if it isn't, it won't contain
2762 * consts anyway, else we'd have discarded the pathkey as
2765 * XXX if we have a choice, is there any way of figuring out which
2766 * might be cheapest to execute? (For example, int4lt is likely
2767 * much cheaper to execute than numericlt, but both might appear
2768 * in the same equivalence class...) Not clear that we ever will
2769 * have an interesting choice in practice, so it may not matter.
2771 foreach(j, ec->ec_members)
2773 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
2775 if (em->em_is_const || em->em_is_child)
2778 tle = tlist_member((Node *) em->em_expr, tlist);
2781 pk_datatype = em->em_datatype;
2782 break; /* found expr already in tlist */
2786 * We can also use it if the pathkey expression is a relabel
2787 * of the tlist entry, or vice versa. This is needed for
2788 * binary-compatible cases (cf. make_pathkey_from_sortinfo).
2789 * We prefer an exact match, though, so we do the basic search
2792 tle = tlist_member_ignore_relabel((Node *) em->em_expr, tlist);
2795 pk_datatype = em->em_datatype;
2796 break; /* found expr already in tlist */
2802 /* No matching tlist item; look for a computable expression */
2803 Expr *sortexpr = NULL;
2805 foreach(j, ec->ec_members)
2807 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
2811 if (em->em_is_const || em->em_is_child)
2813 sortexpr = em->em_expr;
2814 exprvars = pull_var_clause((Node *) sortexpr, false);
2815 foreach(k, exprvars)
2817 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
2820 list_free(exprvars);
2823 pk_datatype = em->em_datatype;
2824 break; /* found usable expression */
2828 elog(ERROR, "could not find pathkey item to sort");
2831 * Do we need to insert a Result node?
2833 if (!is_projection_capable_plan(lefttree))
2835 /* copy needed so we don't modify input's tlist below */
2836 tlist = copyObject(tlist);
2837 lefttree = (Plan *) make_result(root, tlist, NULL,
2842 * Add resjunk entry to input's tlist
2844 tle = makeTargetEntry(sortexpr,
2845 list_length(tlist) + 1,
2848 tlist = lappend(tlist, tle);
2849 lefttree->targetlist = tlist; /* just in case NIL before */
2854 * Look up the correct sort operator from the PathKey's slightly
2855 * abstracted representation.
2857 sortop = get_opfamily_member(pathkey->pk_opfamily,
2860 pathkey->pk_strategy);
2861 if (!OidIsValid(sortop)) /* should not happen */
2862 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
2863 pathkey->pk_strategy, pk_datatype, pk_datatype,
2864 pathkey->pk_opfamily);
2867 * The column might already be selected as a sort key, if the pathkeys
2868 * contain duplicate entries. (This can happen in scenarios where
2869 * multiple mergejoinable clauses mention the same var, for example.)
2870 * So enter it only once in the sort arrays.
2872 numsortkeys = add_sort_column(tle->resno,
2874 pathkey->pk_nulls_first,
2876 sortColIdx, sortOperators, nullsFirst);
2879 Assert(numsortkeys > 0);
2881 return make_sort(root, lefttree, numsortkeys,
2882 sortColIdx, sortOperators, nullsFirst, limit_tuples);
2886 * make_sort_from_sortclauses
2887 * Create sort plan to sort according to given sortclauses
2889 * 'sortcls' is a list of SortClauses
2890 * 'lefttree' is the node which yields input tuples
2893 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
2895 List *sub_tlist = lefttree->targetlist;
2898 AttrNumber *sortColIdx;
2903 * We will need at most list_length(sortcls) sort columns; possibly less
2905 numsortkeys = list_length(sortcls);
2906 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2907 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2908 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2914 SortClause *sortcl = (SortClause *) lfirst(l);
2915 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
2918 * Check for the possibility of duplicate order-by clauses --- the
2919 * parser should have removed 'em, but no point in sorting
2922 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
2923 sortcl->nulls_first,
2925 sortColIdx, sortOperators, nullsFirst);
2928 Assert(numsortkeys > 0);
2930 return make_sort(root, lefttree, numsortkeys,
2931 sortColIdx, sortOperators, nullsFirst, -1.0);
2935 * make_sort_from_groupcols
2936 * Create sort plan to sort based on grouping columns
2938 * 'groupcls' is the list of GroupClauses
2939 * 'grpColIdx' gives the column numbers to use
2941 * This might look like it could be merged with make_sort_from_sortclauses,
2942 * but presently we *must* use the grpColIdx[] array to locate sort columns,
2943 * because the child plan's tlist is not marked with ressortgroupref info
2944 * appropriate to the grouping node. So, only the sort ordering info
2945 * is used from the GroupClause entries.
2948 make_sort_from_groupcols(PlannerInfo *root,
2950 AttrNumber *grpColIdx,
2953 List *sub_tlist = lefttree->targetlist;
2957 AttrNumber *sortColIdx;
2962 * We will need at most list_length(groupcls) sort columns; possibly less
2964 numsortkeys = list_length(groupcls);
2965 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2966 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2967 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2971 foreach(l, groupcls)
2973 GroupClause *grpcl = (GroupClause *) lfirst(l);
2974 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
2977 * Check for the possibility of duplicate group-by clauses --- the
2978 * parser should have removed 'em, but no point in sorting
2981 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
2984 sortColIdx, sortOperators, nullsFirst);
2988 Assert(numsortkeys > 0);
2990 return make_sort(root, lefttree, numsortkeys,
2991 sortColIdx, sortOperators, nullsFirst, -1.0);
2995 make_material(Plan *lefttree)
2997 Material *node = makeNode(Material);
2998 Plan *plan = &node->plan;
3000 /* cost should be inserted by caller */
3001 plan->targetlist = lefttree->targetlist;
3003 plan->lefttree = lefttree;
3004 plan->righttree = NULL;
3010 * materialize_finished_plan: stick a Material node atop a completed plan
3012 * There are a couple of places where we want to attach a Material node
3013 * after completion of subquery_planner(). This currently requires hackery.
3014 * Since subquery_planner has already run SS_finalize_plan on the subplan
3015 * tree, we have to kluge up parameter lists for the Material node.
3016 * Possibly this could be fixed by postponing SS_finalize_plan processing
3017 * until setrefs.c is run?
3020 materialize_finished_plan(Plan *subplan)
3023 Path matpath; /* dummy for result of cost_material */
3025 matplan = (Plan *) make_material(subplan);
3028 cost_material(&matpath,
3029 subplan->total_cost,
3031 subplan->plan_width);
3032 matplan->startup_cost = matpath.startup_cost;
3033 matplan->total_cost = matpath.total_cost;
3034 matplan->plan_rows = subplan->plan_rows;
3035 matplan->plan_width = subplan->plan_width;
3037 /* parameter kluge --- see comments above */
3038 matplan->extParam = bms_copy(subplan->extParam);
3039 matplan->allParam = bms_copy(subplan->allParam);
3045 make_agg(PlannerInfo *root, List *tlist, List *qual,
3046 AggStrategy aggstrategy,
3047 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
3048 long numGroups, int numAggs,
3051 Agg *node = makeNode(Agg);
3052 Plan *plan = &node->plan;
3053 Path agg_path; /* dummy for result of cost_agg */
3056 node->aggstrategy = aggstrategy;
3057 node->numCols = numGroupCols;
3058 node->grpColIdx = grpColIdx;
3059 node->grpOperators = grpOperators;
3060 node->numGroups = numGroups;
3062 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3063 cost_agg(&agg_path, root,
3064 aggstrategy, numAggs,
3065 numGroupCols, numGroups,
3066 lefttree->startup_cost,
3067 lefttree->total_cost,
3068 lefttree->plan_rows);
3069 plan->startup_cost = agg_path.startup_cost;
3070 plan->total_cost = agg_path.total_cost;
3073 * We will produce a single output tuple if not grouping, and a tuple per
3076 if (aggstrategy == AGG_PLAIN)
3077 plan->plan_rows = 1;
3079 plan->plan_rows = numGroups;
3082 * We also need to account for the cost of evaluation of the qual (ie, the
3083 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
3084 * anything for Aggref nodes; this is okay since they are really
3085 * comparable to Vars.
3087 * See notes in grouping_planner about why this routine and make_group are
3088 * the only ones in this file that worry about tlist eval cost.
3092 cost_qual_eval(&qual_cost, qual, root);
3093 plan->startup_cost += qual_cost.startup;
3094 plan->total_cost += qual_cost.startup;
3095 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3097 cost_qual_eval(&qual_cost, tlist, root);
3098 plan->startup_cost += qual_cost.startup;
3099 plan->total_cost += qual_cost.startup;
3100 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3103 plan->targetlist = tlist;
3104 plan->lefttree = lefttree;
3105 plan->righttree = NULL;
3111 make_group(PlannerInfo *root,
3115 AttrNumber *grpColIdx,
3120 Group *node = makeNode(Group);
3121 Plan *plan = &node->plan;
3122 Path group_path; /* dummy for result of cost_group */
3125 node->numCols = numGroupCols;
3126 node->grpColIdx = grpColIdx;
3127 node->grpOperators = grpOperators;
3129 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3130 cost_group(&group_path, root,
3131 numGroupCols, numGroups,
3132 lefttree->startup_cost,
3133 lefttree->total_cost,
3134 lefttree->plan_rows);
3135 plan->startup_cost = group_path.startup_cost;
3136 plan->total_cost = group_path.total_cost;
3138 /* One output tuple per estimated result group */
3139 plan->plan_rows = numGroups;
3142 * We also need to account for the cost of evaluation of the qual (ie, the
3143 * HAVING clause) and the tlist.
3145 * XXX this double-counts the cost of evaluation of any expressions used
3146 * for grouping, since in reality those will have been evaluated at a
3147 * lower plan level and will only be copied by the Group node. Worth
3150 * See notes in grouping_planner about why this routine and make_agg are
3151 * the only ones in this file that worry about tlist eval cost.
3155 cost_qual_eval(&qual_cost, qual, root);
3156 plan->startup_cost += qual_cost.startup;
3157 plan->total_cost += qual_cost.startup;
3158 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3160 cost_qual_eval(&qual_cost, tlist, root);
3161 plan->startup_cost += qual_cost.startup;
3162 plan->total_cost += qual_cost.startup;
3163 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3166 plan->targetlist = tlist;
3167 plan->lefttree = lefttree;
3168 plan->righttree = NULL;
3174 * distinctList is a list of SortClauses, identifying the targetlist items
3175 * that should be considered by the Unique filter. The input path must
3176 * already be sorted accordingly.
3179 make_unique(Plan *lefttree, List *distinctList)
3181 Unique *node = makeNode(Unique);
3182 Plan *plan = &node->plan;
3183 int numCols = list_length(distinctList);
3185 AttrNumber *uniqColIdx;
3189 copy_plan_costsize(plan, lefttree);
3192 * Charge one cpu_operator_cost per comparison per input tuple. We assume
3193 * all columns get compared at most of the tuples. (XXX probably this is
3196 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
3199 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
3200 * we assume the filter removes nothing. The caller must alter this if he
3201 * has a better idea.
3204 plan->targetlist = lefttree->targetlist;
3206 plan->lefttree = lefttree;
3207 plan->righttree = NULL;
3210 * convert SortClause list into arrays of attr indexes and equality
3211 * operators, as wanted by executor
3213 Assert(numCols > 0);
3214 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3215 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3217 foreach(slitem, distinctList)
3219 SortClause *sortcl = (SortClause *) lfirst(slitem);
3220 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3222 uniqColIdx[keyno] = tle->resno;
3223 uniqOperators[keyno] = get_equality_op_for_ordering_op(sortcl->sortop);
3224 if (!OidIsValid(uniqOperators[keyno])) /* shouldn't happen */
3225 elog(ERROR, "could not find equality operator for ordering operator %u",
3230 node->numCols = numCols;
3231 node->uniqColIdx = uniqColIdx;
3232 node->uniqOperators = uniqOperators;
3238 * distinctList is a list of SortClauses, identifying the targetlist items
3239 * that should be considered by the SetOp filter. The input path must
3240 * already be sorted accordingly.
3243 make_setop(SetOpCmd cmd, Plan *lefttree,
3244 List *distinctList, AttrNumber flagColIdx)
3246 SetOp *node = makeNode(SetOp);
3247 Plan *plan = &node->plan;
3248 int numCols = list_length(distinctList);
3250 AttrNumber *dupColIdx;
3254 copy_plan_costsize(plan, lefttree);
3257 * Charge one cpu_operator_cost per comparison per input tuple. We assume
3258 * all columns get compared at most of the tuples.
3260 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
3263 * We make the unsupported assumption that there will be 10% as many
3264 * tuples out as in. Any way to do better?
3266 plan->plan_rows *= 0.1;
3267 if (plan->plan_rows < 1)
3268 plan->plan_rows = 1;
3270 plan->targetlist = lefttree->targetlist;
3272 plan->lefttree = lefttree;
3273 plan->righttree = NULL;
3276 * convert SortClause list into arrays of attr indexes and equality
3277 * operators, as wanted by executor
3279 Assert(numCols > 0);
3280 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3281 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3283 foreach(slitem, distinctList)
3285 SortClause *sortcl = (SortClause *) lfirst(slitem);
3286 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3288 dupColIdx[keyno] = tle->resno;
3289 dupOperators[keyno] = get_equality_op_for_ordering_op(sortcl->sortop);
3290 if (!OidIsValid(dupOperators[keyno])) /* shouldn't happen */
3291 elog(ERROR, "could not find equality operator for ordering operator %u",
3297 node->numCols = numCols;
3298 node->dupColIdx = dupColIdx;
3299 node->dupOperators = dupOperators;
3300 node->flagColIdx = flagColIdx;
3306 * Note: offset_est and count_est are passed in to save having to repeat
3307 * work already done to estimate the values of the limitOffset and limitCount
3308 * expressions. Their values are as returned by preprocess_limit (0 means
3309 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
3310 * with that function!
3313 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
3314 int64 offset_est, int64 count_est)
3316 Limit *node = makeNode(Limit);
3317 Plan *plan = &node->plan;
3319 copy_plan_costsize(plan, lefttree);
3322 * Adjust the output rows count and costs according to the offset/limit.
3323 * This is only a cosmetic issue if we are at top level, but if we are
3324 * building a subquery then it's important to report correct info to the
3327 * When the offset or count couldn't be estimated, use 10% of the
3328 * estimated number of rows emitted from the subplan.
3330 if (offset_est != 0)
3335 offset_rows = (double) offset_est;
3337 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
3338 if (offset_rows > plan->plan_rows)
3339 offset_rows = plan->plan_rows;
3340 if (plan->plan_rows > 0)
3341 plan->startup_cost +=
3342 (plan->total_cost - plan->startup_cost)
3343 * offset_rows / plan->plan_rows;
3344 plan->plan_rows -= offset_rows;
3345 if (plan->plan_rows < 1)
3346 plan->plan_rows = 1;
3354 count_rows = (double) count_est;
3356 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
3357 if (count_rows > plan->plan_rows)
3358 count_rows = plan->plan_rows;
3359 if (plan->plan_rows > 0)
3360 plan->total_cost = plan->startup_cost +
3361 (plan->total_cost - plan->startup_cost)
3362 * count_rows / plan->plan_rows;
3363 plan->plan_rows = count_rows;
3364 if (plan->plan_rows < 1)
3365 plan->plan_rows = 1;
3368 plan->targetlist = lefttree->targetlist;
3370 plan->lefttree = lefttree;
3371 plan->righttree = NULL;
3373 node->limitOffset = limitOffset;
3374 node->limitCount = limitCount;
3381 * Build a Result plan node
3383 * If we have a subplan, assume that any evaluation costs for the gating qual
3384 * were already factored into the subplan's startup cost, and just copy the
3385 * subplan cost. If there's no subplan, we should include the qual eval
3386 * cost. In either case, tlist eval cost is not to be included here.
3389 make_result(PlannerInfo *root,
3391 Node *resconstantqual,
3394 Result *node = makeNode(Result);
3395 Plan *plan = &node->plan;
3398 copy_plan_costsize(plan, subplan);
3401 plan->startup_cost = 0;
3402 plan->total_cost = cpu_tuple_cost;
3403 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
3404 plan->plan_width = 0; /* XXX is it worth being smarter? */
3405 if (resconstantqual)
3409 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
3410 /* resconstantqual is evaluated once at startup */
3411 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
3412 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
3416 plan->targetlist = tlist;
3418 plan->lefttree = subplan;
3419 plan->righttree = NULL;
3420 node->resconstantqual = resconstantqual;
3426 * is_projection_capable_plan
3427 * Check whether a given Plan node is able to do projection.
3430 is_projection_capable_plan(Plan *plan)
3432 /* Most plan types can project, so just list the ones that can't */
3433 switch (nodeTag(plan))