1 /*-------------------------------------------------------------------------
4 * Routines to find possible search paths for processing a query
6 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/optimizer/path/allpaths.c
13 *-------------------------------------------------------------------------
20 #include "catalog/pg_class.h"
21 #include "foreign/fdwapi.h"
22 #include "nodes/nodeFuncs.h"
23 #ifdef OPTIMIZER_DEBUG
24 #include "nodes/print.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/cost.h"
28 #include "optimizer/geqo.h"
29 #include "optimizer/pathnode.h"
30 #include "optimizer/paths.h"
31 #include "optimizer/plancat.h"
32 #include "optimizer/planner.h"
33 #include "optimizer/prep.h"
34 #include "optimizer/restrictinfo.h"
35 #include "optimizer/var.h"
36 #include "parser/parse_clause.h"
37 #include "parser/parsetree.h"
38 #include "rewrite/rewriteManip.h"
39 #include "utils/lsyscache.h"
42 /* These parameters are set by GUC */
43 bool enable_geqo = false; /* just in case GUC doesn't set it */
46 /* Hook for plugins to replace standard_join_search() */
47 join_search_hook_type join_search_hook = NULL;
50 static void set_base_rel_sizes(PlannerInfo *root);
51 static void set_base_rel_pathlists(PlannerInfo *root);
52 static void set_rel_size(PlannerInfo *root, RelOptInfo *rel,
53 Index rti, RangeTblEntry *rte);
54 static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
55 Index rti, RangeTblEntry *rte);
56 static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
58 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
60 static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
62 static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
64 static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
65 Index rti, RangeTblEntry *rte);
66 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
67 Index rti, RangeTblEntry *rte);
68 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
70 List *all_child_pathkeys,
71 Relids required_outer);
72 static List *accumulate_append_subpath(List *subpaths, Path *path);
73 static void set_dummy_rel_pathlist(RelOptInfo *rel);
74 static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
75 Index rti, RangeTblEntry *rte);
76 static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
78 static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
80 static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
82 static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
84 static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
85 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
86 bool *differentTypes);
87 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
88 bool *differentTypes);
89 static void compare_tlist_datatypes(List *tlist, List *colTypes,
90 bool *differentTypes);
91 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
92 bool *differentTypes);
93 static void subquery_push_qual(Query *subquery,
94 RangeTblEntry *rte, Index rti, Node *qual);
95 static void recurse_push_qual(Node *setOp, Query *topquery,
96 RangeTblEntry *rte, Index rti, Node *qual);
101 * Finds all possible access paths for executing a query, returning a
102 * single rel that represents the join of all base rels in the query.
105 make_one_rel(PlannerInfo *root, List *joinlist)
111 * Construct the all_baserels Relids set.
113 root->all_baserels = NULL;
114 for (rti = 1; rti < root->simple_rel_array_size; rti++)
116 RelOptInfo *brel = root->simple_rel_array[rti];
118 /* there may be empty slots corresponding to non-baserel RTEs */
122 Assert(brel->relid == rti); /* sanity check on array */
124 /* ignore RTEs that are "other rels" */
125 if (brel->reloptkind != RELOPT_BASEREL)
128 root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
132 * Generate access paths for the base rels.
134 set_base_rel_sizes(root);
135 set_base_rel_pathlists(root);
138 * Generate access paths for the entire join tree.
140 rel = make_rel_from_joinlist(root, joinlist);
143 * The result should join all and only the query's base rels.
145 Assert(bms_equal(rel->relids, root->all_baserels));
152 * Set the size estimates (rows and widths) for each base-relation entry.
154 * We do this in a separate pass over the base rels so that rowcount
155 * estimates are available for parameterized path generation.
158 set_base_rel_sizes(PlannerInfo *root)
162 for (rti = 1; rti < root->simple_rel_array_size; rti++)
164 RelOptInfo *rel = root->simple_rel_array[rti];
166 /* there may be empty slots corresponding to non-baserel RTEs */
170 Assert(rel->relid == rti); /* sanity check on array */
172 /* ignore RTEs that are "other rels" */
173 if (rel->reloptkind != RELOPT_BASEREL)
176 set_rel_size(root, rel, rti, root->simple_rte_array[rti]);
181 * set_base_rel_pathlists
182 * Finds all paths available for scanning each base-relation entry.
183 * Sequential scan and any available indices are considered.
184 * Each useful path is attached to its relation's 'pathlist' field.
187 set_base_rel_pathlists(PlannerInfo *root)
191 for (rti = 1; rti < root->simple_rel_array_size; rti++)
193 RelOptInfo *rel = root->simple_rel_array[rti];
195 /* there may be empty slots corresponding to non-baserel RTEs */
199 Assert(rel->relid == rti); /* sanity check on array */
201 /* ignore RTEs that are "other rels" */
202 if (rel->reloptkind != RELOPT_BASEREL)
205 set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
211 * Set size estimates for a base relation
214 set_rel_size(PlannerInfo *root, RelOptInfo *rel,
215 Index rti, RangeTblEntry *rte)
217 if (rel->reloptkind == RELOPT_BASEREL &&
218 relation_excluded_by_constraints(root, rel, rte))
221 * We proved we don't need to scan the rel via constraint exclusion,
222 * so set up a single dummy path for it. Here we only check this for
223 * regular baserels; if it's an otherrel, CE was already checked in
224 * set_append_rel_pathlist().
226 * In this case, we go ahead and set up the relation's path right away
227 * instead of leaving it for set_rel_pathlist to do. This is because
228 * we don't have a convention for marking a rel as dummy except by
229 * assigning a dummy path to it.
231 set_dummy_rel_pathlist(rel);
235 /* It's an "append relation", process accordingly */
236 set_append_rel_size(root, rel, rti, rte);
240 switch (rel->rtekind)
243 if (rte->relkind == RELKIND_FOREIGN_TABLE)
246 set_foreign_size(root, rel, rte);
251 set_plain_rel_size(root, rel, rte);
256 * Subqueries don't support parameterized paths, so just go
257 * ahead and build their paths immediately.
259 set_subquery_pathlist(root, rel, rti, rte);
262 set_function_size_estimates(root, rel);
265 set_values_size_estimates(root, rel);
269 * CTEs don't support parameterized paths, so just go ahead
270 * and build their paths immediately.
272 if (rte->self_reference)
273 set_worktable_pathlist(root, rel, rte);
275 set_cte_pathlist(root, rel, rte);
278 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
286 * Build access paths for a base relation
289 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
290 Index rti, RangeTblEntry *rte)
292 if (IS_DUMMY_REL(rel))
294 /* We already proved the relation empty, so nothing more to do */
298 /* It's an "append relation", process accordingly */
299 set_append_rel_pathlist(root, rel, rti, rte);
303 switch (rel->rtekind)
306 if (rte->relkind == RELKIND_FOREIGN_TABLE)
309 set_foreign_pathlist(root, rel, rte);
314 set_plain_rel_pathlist(root, rel, rte);
318 /* Subquery --- fully handled during set_rel_size */
322 set_function_pathlist(root, rel, rte);
326 set_values_pathlist(root, rel, rte);
329 /* CTE reference --- fully handled during set_rel_size */
332 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
337 #ifdef OPTIMIZER_DEBUG
338 debug_print_rel(root, rel);
344 * Set size estimates for a plain relation (no subquery, no inheritance)
347 set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
350 * Test any partial indexes of rel for applicability. We must do this
351 * first since partial unique indexes can affect size estimates.
353 check_partial_indexes(root, rel);
355 /* Mark rel with estimated output rows, width, etc */
356 set_baserel_size_estimates(root, rel);
359 * Check to see if we can extract any restriction conditions from join
360 * quals that are OR-of-AND structures. If so, add them to the rel's
361 * restriction list, and redo the above steps.
363 if (create_or_index_quals(root, rel))
365 check_partial_indexes(root, rel);
366 set_baserel_size_estimates(root, rel);
371 * set_plain_rel_pathlist
372 * Build access paths for a plain relation (no subquery, no inheritance)
375 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
377 /* Consider sequential scan */
378 add_path(rel, create_seqscan_path(root, rel));
380 /* Consider index scans */
381 create_index_paths(root, rel);
383 /* Consider TID scans */
384 create_tidscan_paths(root, rel);
386 /* Now find the cheapest of the paths for this rel */
392 * Set size estimates for a foreign table RTE
395 set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
397 /* Mark rel with estimated output rows, width, etc */
398 set_foreign_size_estimates(root, rel);
400 /* Get FDW routine pointers for the rel */
401 rel->fdwroutine = GetFdwRoutineByRelId(rte->relid);
403 /* Let FDW adjust the size estimates, if it can */
404 rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
408 * set_foreign_pathlist
409 * Build access paths for a foreign table RTE
412 set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
414 /* Call the FDW's GetForeignPaths function to generate path(s) */
415 rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
417 /* Select cheapest path */
422 * set_append_rel_size
423 * Set size estimates for an "append relation"
425 * The passed-in rel and RTE represent the entire append relation. The
426 * relation's contents are computed by appending together the output of
427 * the individual member relations. Note that in the inheritance case,
428 * the first member relation is actually the same table as is mentioned in
429 * the parent RTE ... but it has a different RTE and RelOptInfo. This is
430 * a good thing because their outputs are not the same size.
433 set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
434 Index rti, RangeTblEntry *rte)
436 int parentRTindex = rti;
439 double *parent_attrsizes;
444 * Initialize to compute size estimates for whole append relation.
446 * We handle width estimates by weighting the widths of different child
447 * rels proportionally to their number of rows. This is sensible because
448 * the use of width estimates is mainly to compute the total relation
449 * "footprint" if we have to sort or hash it. To do this, we sum the
450 * total equivalent size (in "double" arithmetic) and then divide by the
451 * total rowcount estimate. This is done separately for the total rel
452 * width and each attribute.
454 * Note: if you consider changing this logic, beware that child rels could
455 * have zero rows and/or width, if they were excluded by constraints.
459 nattrs = rel->max_attr - rel->min_attr + 1;
460 parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
462 foreach(l, root->append_rel_list)
464 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
466 RangeTblEntry *childRTE;
467 RelOptInfo *childrel;
470 ListCell *parentvars;
473 /* append_rel_list contains all append rels; ignore others */
474 if (appinfo->parent_relid != parentRTindex)
477 childRTindex = appinfo->child_relid;
478 childRTE = root->simple_rte_array[childRTindex];
481 * The child rel's RelOptInfo was already created during
482 * add_base_rels_to_query.
484 childrel = find_base_rel(root, childRTindex);
485 Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
488 * We have to copy the parent's targetlist and quals to the child,
489 * with appropriate substitution of variables. However, only the
490 * baserestrictinfo quals are needed before we can check for
491 * constraint exclusion; so do that first and then check to see if we
492 * can disregard this child.
494 * As of 8.4, the child rel's targetlist might contain non-Var
495 * expressions, which means that substitution into the quals could
496 * produce opportunities for const-simplification, and perhaps even
497 * pseudoconstant quals. To deal with this, we strip the RestrictInfo
498 * nodes, do the substitution, do const-simplification, and then
499 * reconstitute the RestrictInfo layer.
501 childquals = get_all_actual_clauses(rel->baserestrictinfo);
502 childquals = (List *) adjust_appendrel_attrs(root,
505 childqual = eval_const_expressions(root, (Node *)
506 make_ands_explicit(childquals));
507 if (childqual && IsA(childqual, Const) &&
508 (((Const *) childqual)->constisnull ||
509 !DatumGetBool(((Const *) childqual)->constvalue)))
512 * Restriction reduces to constant FALSE or constant NULL after
513 * substitution, so this child need not be scanned.
515 set_dummy_rel_pathlist(childrel);
518 childquals = make_ands_implicit((Expr *) childqual);
519 childquals = make_restrictinfos_from_actual_clauses(root,
521 childrel->baserestrictinfo = childquals;
523 if (relation_excluded_by_constraints(root, childrel, childRTE))
526 * This child need not be scanned, so we can omit it from the
529 set_dummy_rel_pathlist(childrel);
534 * CE failed, so finish copying/modifying targetlist and join quals.
536 * Note: the resulting childrel->reltargetlist may contain arbitrary
537 * expressions, which normally would not occur in a reltargetlist.
538 * That is okay because nothing outside of this routine will look at
539 * the child rel's reltargetlist. We do have to cope with the case
540 * while constructing attr_widths estimates below, though.
542 childrel->joininfo = (List *)
543 adjust_appendrel_attrs(root,
544 (Node *) rel->joininfo,
546 childrel->reltargetlist = (List *)
547 adjust_appendrel_attrs(root,
548 (Node *) rel->reltargetlist,
552 * We have to make child entries in the EquivalenceClass data
553 * structures as well. This is needed either if the parent
554 * participates in some eclass joins (because we will want to consider
555 * inner-indexscan joins on the individual children) or if the parent
556 * has useful pathkeys (because we should try to build MergeAppend
557 * paths that produce those sort orderings).
559 if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
560 add_child_rel_equivalences(root, appinfo, rel, childrel);
561 childrel->has_eclass_joins = rel->has_eclass_joins;
564 * Note: we could compute appropriate attr_needed data for the child's
565 * variables, by transforming the parent's attr_needed through the
566 * translated_vars mapping. However, currently there's no need
567 * because attr_needed is only examined for base relations not
568 * otherrels. So we just leave the child's attr_needed empty.
572 * Compute the child's size.
574 set_rel_size(root, childrel, childRTindex, childRTE);
577 * It is possible that constraint exclusion detected a contradiction
578 * within a child subquery, even though we didn't prove one above.
579 * If so, we can skip this child.
581 if (IS_DUMMY_REL(childrel))
585 * Accumulate size information from each live child.
587 if (childrel->rows > 0)
589 parent_rows += childrel->rows;
590 parent_size += childrel->width * childrel->rows;
593 * Accumulate per-column estimates too. We need not do anything
594 * for PlaceHolderVars in the parent list. If child expression
595 * isn't a Var, or we didn't record a width estimate for it, we
596 * have to fall back on a datatype-based estimate.
598 * By construction, child's reltargetlist is 1-to-1 with parent's.
600 forboth(parentvars, rel->reltargetlist,
601 childvars, childrel->reltargetlist)
603 Var *parentvar = (Var *) lfirst(parentvars);
604 Node *childvar = (Node *) lfirst(childvars);
606 if (IsA(parentvar, Var))
608 int pndx = parentvar->varattno - rel->min_attr;
609 int32 child_width = 0;
611 if (IsA(childvar, Var))
613 int cndx = ((Var *) childvar)->varattno - childrel->min_attr;
615 child_width = childrel->attr_widths[cndx];
617 if (child_width <= 0)
618 child_width = get_typavgwidth(exprType(childvar),
619 exprTypmod(childvar));
620 Assert(child_width > 0);
621 parent_attrsizes[pndx] += child_width * childrel->rows;
628 * Save the finished size estimates.
630 rel->rows = parent_rows;
635 rel->width = rint(parent_size / parent_rows);
636 for (i = 0; i < nattrs; i++)
637 rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
640 rel->width = 0; /* attr_widths should be zero already */
643 * Set "raw tuples" count equal to "rows" for the appendrel; needed
644 * because some places assume rel->tuples is valid for any baserel.
646 rel->tuples = parent_rows;
648 pfree(parent_attrsizes);
652 * set_append_rel_pathlist
653 * Build access paths for an "append relation"
656 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
657 Index rti, RangeTblEntry *rte)
659 int parentRTindex = rti;
660 List *live_childrels = NIL;
661 List *subpaths = NIL;
662 List *all_child_pathkeys = NIL;
663 List *all_child_outers = NIL;
667 * Generate access paths for each member relation, and remember the
668 * cheapest path for each one. Also, identify all pathkeys (orderings)
669 * and parameterizations (required_outer sets) available for the member
672 foreach(l, root->append_rel_list)
674 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
676 RangeTblEntry *childRTE;
677 RelOptInfo *childrel;
680 /* append_rel_list contains all append rels; ignore others */
681 if (appinfo->parent_relid != parentRTindex)
684 /* Re-locate the child RTE and RelOptInfo */
685 childRTindex = appinfo->child_relid;
686 childRTE = root->simple_rte_array[childRTindex];
687 childrel = root->simple_rel_array[childRTindex];
690 * Compute the child's access paths.
692 set_rel_pathlist(root, childrel, childRTindex, childRTE);
695 * If child is dummy, ignore it.
697 if (IS_DUMMY_REL(childrel))
701 * Child is live, so add its cheapest access path to the Append path
702 * we are constructing for the parent.
704 subpaths = accumulate_append_subpath(subpaths,
705 childrel->cheapest_total_path);
707 /* Remember which childrels are live, for logic below */
708 live_childrels = lappend(live_childrels, childrel);
711 * Collect lists of all the available path orderings and
712 * parameterizations for all the children. We use these as a
713 * heuristic to indicate which sort orderings and parameterizations we
714 * should build Append and MergeAppend paths for.
716 foreach(lcp, childrel->pathlist)
718 Path *childpath = (Path *) lfirst(lcp);
719 List *childkeys = childpath->pathkeys;
720 Relids childouter = childpath->required_outer;
722 /* Unsorted paths don't contribute to pathkey list */
723 if (childkeys != NIL)
728 /* Have we already seen this ordering? */
729 foreach(lpk, all_child_pathkeys)
731 List *existing_pathkeys = (List *) lfirst(lpk);
733 if (compare_pathkeys(existing_pathkeys,
734 childkeys) == PATHKEYS_EQUAL)
742 /* No, so add it to all_child_pathkeys */
743 all_child_pathkeys = lappend(all_child_pathkeys,
748 /* Unparameterized paths don't contribute to param-set list */
754 /* Have we already seen this param set? */
755 foreach(lco, all_child_outers)
757 Relids existing_outers = (Relids) lfirst(lco);
759 if (bms_equal(existing_outers, childouter))
767 /* No, so add it to all_child_outers */
768 all_child_outers = lappend(all_child_outers,
776 * Next, build an unordered, unparameterized Append path for the rel.
777 * (Note: this is correct even if we have zero or one live subpath due to
778 * constraint exclusion.)
780 add_path(rel, (Path *) create_append_path(rel, subpaths));
783 * Build unparameterized MergeAppend paths based on the collected list of
786 generate_mergeappend_paths(root, rel, live_childrels,
787 all_child_pathkeys, NULL);
790 * Build Append and MergeAppend paths for each parameterization seen
791 * among the child rels. (This may look pretty expensive, but in most
792 * cases of practical interest, the child relations will tend to expose
793 * the same parameterizations and pathkeys, so that not that many cases
794 * actually get considered here.)
796 foreach(l, all_child_outers)
798 Relids required_outer = (Relids) lfirst(l);
801 /* Select the child paths for an Append with this parameterization */
803 foreach(lcr, live_childrels)
805 RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
806 Path *cheapest_total;
809 get_cheapest_path_for_pathkeys(childrel->pathlist,
813 Assert(cheapest_total != NULL);
815 subpaths = accumulate_append_subpath(subpaths, cheapest_total);
817 add_path(rel, (Path *) create_append_path(rel, subpaths));
819 /* And build parameterized MergeAppend paths */
820 generate_mergeappend_paths(root, rel, live_childrels,
821 all_child_pathkeys, required_outer);
824 /* Select cheapest paths */
829 * generate_mergeappend_paths
830 * Generate MergeAppend paths for an append relation
832 * Generate a path for each ordering (pathkey list) appearing in
833 * all_child_pathkeys. If required_outer isn't NULL, accept paths having
834 * those relations as required outer relations.
836 * We consider both cheapest-startup and cheapest-total cases, ie, for each
837 * interesting ordering, collect all the cheapest startup subpaths and all the
838 * cheapest total paths, and build a MergeAppend path for each case.
841 generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
842 List *live_childrels,
843 List *all_child_pathkeys,
844 Relids required_outer)
848 foreach(lcp, all_child_pathkeys)
850 List *pathkeys = (List *) lfirst(lcp);
851 List *startup_subpaths = NIL;
852 List *total_subpaths = NIL;
853 bool startup_neq_total = false;
856 /* Select the child paths for this ordering... */
857 foreach(lcr, live_childrels)
859 RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
860 Path *cheapest_startup,
863 /* Locate the right paths, if they are available. */
865 get_cheapest_path_for_pathkeys(childrel->pathlist,
870 get_cheapest_path_for_pathkeys(childrel->pathlist,
876 * If we can't find any paths with the right order just use the
877 * cheapest-total path; we'll have to sort it later. We can
878 * use the cheapest path for the parameterization, though.
880 if (cheapest_startup == NULL || cheapest_total == NULL)
883 cheapest_startup = cheapest_total =
884 get_cheapest_path_for_pathkeys(childrel->pathlist,
889 cheapest_startup = cheapest_total =
890 childrel->cheapest_total_path;
891 Assert(cheapest_total != NULL);
895 * Notice whether we actually have different paths for the
896 * "cheapest" and "total" cases; frequently there will be no point
897 * in two create_merge_append_path() calls.
899 if (cheapest_startup != cheapest_total)
900 startup_neq_total = true;
903 accumulate_append_subpath(startup_subpaths, cheapest_startup);
905 accumulate_append_subpath(total_subpaths, cheapest_total);
908 /* ... and build the MergeAppend paths */
909 add_path(rel, (Path *) create_merge_append_path(root,
913 if (startup_neq_total)
914 add_path(rel, (Path *) create_merge_append_path(root,
922 * accumulate_append_subpath
923 * Add a subpath to the list being built for an Append or MergeAppend
925 * It's possible that the child is itself an Append path, in which case
926 * we can "cut out the middleman" and just add its child paths to our
927 * own list. (We don't try to do this earlier because we need to
928 * apply both levels of transformation to the quals.)
931 accumulate_append_subpath(List *subpaths, Path *path)
933 if (IsA(path, AppendPath))
935 AppendPath *apath = (AppendPath *) path;
937 /* list_copy is important here to avoid sharing list substructure */
938 return list_concat(subpaths, list_copy(apath->subpaths));
941 return lappend(subpaths, path);
945 * set_dummy_rel_pathlist
946 * Build a dummy path for a relation that's been excluded by constraints
948 * Rather than inventing a special "dummy" path type, we represent this as an
949 * AppendPath with no members (see also IS_DUMMY_PATH/IS_DUMMY_REL macros).
952 set_dummy_rel_pathlist(RelOptInfo *rel)
954 /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
958 /* Discard any pre-existing paths; no further need for them */
961 add_path(rel, (Path *) create_append_path(rel, NIL));
963 /* Select cheapest path (pretty easy in this case...) */
967 /* quick-and-dirty test to see if any joining is needed */
969 has_multiple_baserels(PlannerInfo *root)
971 int num_base_rels = 0;
974 for (rti = 1; rti < root->simple_rel_array_size; rti++)
976 RelOptInfo *brel = root->simple_rel_array[rti];
981 /* ignore RTEs that are "other rels" */
982 if (brel->reloptkind == RELOPT_BASEREL)
983 if (++num_base_rels > 1)
990 * set_subquery_pathlist
991 * Build the (single) access path for a subquery RTE
993 * There's no need for a separate set_subquery_size phase, since we don't
994 * support parameterized paths for subqueries.
997 set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
998 Index rti, RangeTblEntry *rte)
1000 Query *parse = root->parse;
1001 Query *subquery = rte->subquery;
1002 bool *differentTypes;
1003 double tuple_fraction;
1004 PlannerInfo *subroot;
1008 * Must copy the Query so that planning doesn't mess up the RTE contents
1009 * (really really need to fix the planner to not scribble on its input,
1012 subquery = copyObject(subquery);
1014 /* We need a workspace for keeping track of set-op type coercions */
1015 differentTypes = (bool *)
1016 palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
1019 * If there are any restriction clauses that have been attached to the
1020 * subquery relation, consider pushing them down to become WHERE or HAVING
1021 * quals of the subquery itself. This transformation is useful because it
1022 * may allow us to generate a better plan for the subquery than evaluating
1023 * all the subquery output rows and then filtering them.
1025 * There are several cases where we cannot push down clauses. Restrictions
1026 * involving the subquery are checked by subquery_is_pushdown_safe().
1027 * Restrictions on individual clauses are checked by
1028 * qual_is_pushdown_safe(). Also, we don't want to push down
1029 * pseudoconstant clauses; better to have the gating node above the
1032 * Also, if the sub-query has "security_barrier" flag, it means the
1033 * sub-query originated from a view that must enforce row-level security.
1034 * We must not push down quals in order to avoid information leaks, either
1035 * via side-effects or error output.
1037 * Non-pushed-down clauses will get evaluated as qpquals of the
1038 * SubqueryScan node.
1040 * XXX Are there any cases where we want to make a policy decision not to
1041 * push down a pushable qual, because it'd result in a worse plan?
1043 if (rel->baserestrictinfo != NIL &&
1044 subquery_is_pushdown_safe(subquery, subquery, differentTypes))
1046 /* OK to consider pushing down individual quals */
1047 List *upperrestrictlist = NIL;
1050 foreach(l, rel->baserestrictinfo)
1052 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1053 Node *clause = (Node *) rinfo->clause;
1055 if (!rinfo->pseudoconstant &&
1056 (!rte->security_barrier ||
1057 !contain_leaky_functions(clause)) &&
1058 qual_is_pushdown_safe(subquery, rti, clause, differentTypes))
1061 subquery_push_qual(subquery, rte, rti, clause);
1065 /* Keep it in the upper query */
1066 upperrestrictlist = lappend(upperrestrictlist, rinfo);
1069 rel->baserestrictinfo = upperrestrictlist;
1072 pfree(differentTypes);
1075 * We can safely pass the outer tuple_fraction down to the subquery if the
1076 * outer level has no joining, aggregation, or sorting to do. Otherwise
1077 * we'd better tell the subquery to plan for full retrieval. (XXX This
1078 * could probably be made more intelligent ...)
1080 if (parse->hasAggs ||
1081 parse->groupClause ||
1082 parse->havingQual ||
1083 parse->distinctClause ||
1084 parse->sortClause ||
1085 has_multiple_baserels(root))
1086 tuple_fraction = 0.0; /* default case */
1088 tuple_fraction = root->tuple_fraction;
1090 /* Generate the plan for the subquery */
1091 rel->subplan = subquery_planner(root->glob, subquery,
1093 false, tuple_fraction,
1095 rel->subroot = subroot;
1098 * It's possible that constraint exclusion proved the subquery empty.
1099 * If so, it's convenient to turn it back into a dummy path so that we
1100 * will recognize appropriate optimizations at this level.
1102 if (is_dummy_plan(rel->subplan))
1104 set_dummy_rel_pathlist(rel);
1108 /* Mark rel with estimated output rows, width, etc */
1109 set_subquery_size_estimates(root, rel);
1111 /* Convert subquery pathkeys to outer representation */
1112 pathkeys = convert_subquery_pathkeys(root, rel, subroot->query_pathkeys);
1114 /* Generate appropriate path */
1115 add_path(rel, create_subqueryscan_path(rel, pathkeys));
1117 /* Select cheapest path (pretty easy in this case...) */
1122 * set_function_pathlist
1123 * Build the (single) access path for a function RTE
1126 set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1128 /* Generate appropriate path */
1129 add_path(rel, create_functionscan_path(root, rel));
1131 /* Select cheapest path (pretty easy in this case...) */
1136 * set_values_pathlist
1137 * Build the (single) access path for a VALUES RTE
1140 set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1142 /* Generate appropriate path */
1143 add_path(rel, create_valuesscan_path(root, rel));
1145 /* Select cheapest path (pretty easy in this case...) */
1151 * Build the (single) access path for a non-self-reference CTE RTE
1153 * There's no need for a separate set_cte_size phase, since we don't
1154 * support parameterized paths for CTEs.
1157 set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1160 PlannerInfo *cteroot;
1167 * Find the referenced CTE, and locate the plan previously made for it.
1169 levelsup = rte->ctelevelsup;
1171 while (levelsup-- > 0)
1173 cteroot = cteroot->parent_root;
1174 if (!cteroot) /* shouldn't happen */
1175 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1179 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1180 * on planning the CTEs (ie, this is a side-reference from another CTE).
1181 * So we mustn't use forboth here.
1184 foreach(lc, cteroot->parse->cteList)
1186 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1188 if (strcmp(cte->ctename, rte->ctename) == 0)
1192 if (lc == NULL) /* shouldn't happen */
1193 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1194 if (ndx >= list_length(cteroot->cte_plan_ids))
1195 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1196 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1197 Assert(plan_id > 0);
1198 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
1200 /* Mark rel with estimated output rows, width, etc */
1201 set_cte_size_estimates(root, rel, cteplan);
1203 /* Generate appropriate path */
1204 add_path(rel, create_ctescan_path(root, rel));
1206 /* Select cheapest path (pretty easy in this case...) */
1211 * set_worktable_pathlist
1212 * Build the (single) access path for a self-reference CTE RTE
1214 * There's no need for a separate set_worktable_size phase, since we don't
1215 * support parameterized paths for CTEs.
1218 set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1221 PlannerInfo *cteroot;
1225 * We need to find the non-recursive term's plan, which is in the plan
1226 * level that's processing the recursive UNION, which is one level *below*
1227 * where the CTE comes from.
1229 levelsup = rte->ctelevelsup;
1230 if (levelsup == 0) /* shouldn't happen */
1231 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1234 while (levelsup-- > 0)
1236 cteroot = cteroot->parent_root;
1237 if (!cteroot) /* shouldn't happen */
1238 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1240 cteplan = cteroot->non_recursive_plan;
1241 if (!cteplan) /* shouldn't happen */
1242 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1244 /* Mark rel with estimated output rows, width, etc */
1245 set_cte_size_estimates(root, rel, cteplan);
1247 /* Generate appropriate path */
1248 add_path(rel, create_worktablescan_path(root, rel));
1250 /* Select cheapest path (pretty easy in this case...) */
1255 * make_rel_from_joinlist
1256 * Build access paths using a "joinlist" to guide the join path search.
1258 * See comments for deconstruct_jointree() for definition of the joinlist
1262 make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
1269 * Count the number of child joinlist nodes. This is the depth of the
1270 * dynamic-programming algorithm we must employ to consider all ways of
1271 * joining the child nodes.
1273 levels_needed = list_length(joinlist);
1275 if (levels_needed <= 0)
1276 return NULL; /* nothing to do? */
1279 * Construct a list of rels corresponding to the child joinlist nodes.
1280 * This may contain both base rels and rels constructed according to
1284 foreach(jl, joinlist)
1286 Node *jlnode = (Node *) lfirst(jl);
1287 RelOptInfo *thisrel;
1289 if (IsA(jlnode, RangeTblRef))
1291 int varno = ((RangeTblRef *) jlnode)->rtindex;
1293 thisrel = find_base_rel(root, varno);
1295 else if (IsA(jlnode, List))
1297 /* Recurse to handle subproblem */
1298 thisrel = make_rel_from_joinlist(root, (List *) jlnode);
1302 elog(ERROR, "unrecognized joinlist node type: %d",
1303 (int) nodeTag(jlnode));
1304 thisrel = NULL; /* keep compiler quiet */
1307 initial_rels = lappend(initial_rels, thisrel);
1310 if (levels_needed == 1)
1313 * Single joinlist node, so we're done.
1315 return (RelOptInfo *) linitial(initial_rels);
1320 * Consider the different orders in which we could join the rels,
1321 * using a plugin, GEQO, or the regular join search code.
1323 * We put the initial_rels list into a PlannerInfo field because
1324 * has_legal_joinclause() needs to look at it (ugly :-().
1326 root->initial_rels = initial_rels;
1328 if (join_search_hook)
1329 return (*join_search_hook) (root, levels_needed, initial_rels);
1330 else if (enable_geqo && levels_needed >= geqo_threshold)
1331 return geqo(root, levels_needed, initial_rels);
1333 return standard_join_search(root, levels_needed, initial_rels);
1338 * standard_join_search
1339 * Find possible joinpaths for a query by successively finding ways
1340 * to join component relations into join relations.
1342 * 'levels_needed' is the number of iterations needed, ie, the number of
1343 * independent jointree items in the query. This is > 1.
1345 * 'initial_rels' is a list of RelOptInfo nodes for each independent
1346 * jointree item. These are the components to be joined together.
1347 * Note that levels_needed == list_length(initial_rels).
1349 * Returns the final level of join relations, i.e., the relation that is
1350 * the result of joining all the original relations together.
1351 * At least one implementation path must be provided for this relation and
1352 * all required sub-relations.
1354 * To support loadable plugins that modify planner behavior by changing the
1355 * join searching algorithm, we provide a hook variable that lets a plugin
1356 * replace or supplement this function. Any such hook must return the same
1357 * final join relation as the standard code would, but it might have a
1358 * different set of implementation paths attached, and only the sub-joinrels
1359 * needed for these paths need have been instantiated.
1361 * Note to plugin authors: the functions invoked during standard_join_search()
1362 * modify root->join_rel_list and root->join_rel_hash. If you want to do more
1363 * than one join-order search, you'll probably need to save and restore the
1364 * original states of those data structures. See geqo_eval() for an example.
1367 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
1373 * This function cannot be invoked recursively within any one planning
1374 * problem, so join_rel_level[] can't be in use already.
1376 Assert(root->join_rel_level == NULL);
1379 * We employ a simple "dynamic programming" algorithm: we first find all
1380 * ways to build joins of two jointree items, then all ways to build joins
1381 * of three items (from two-item joins and single items), then four-item
1382 * joins, and so on until we have considered all ways to join all the
1383 * items into one rel.
1385 * root->join_rel_level[j] is a list of all the j-item rels. Initially we
1386 * set root->join_rel_level[1] to represent all the single-jointree-item
1389 root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
1391 root->join_rel_level[1] = initial_rels;
1393 for (lev = 2; lev <= levels_needed; lev++)
1398 * Determine all possible pairs of relations to be joined at this
1399 * level, and build paths for making each one from every available
1400 * pair of lower-level relations.
1402 join_search_one_level(root, lev);
1405 * Do cleanup work on each just-processed rel.
1407 foreach(lc, root->join_rel_level[lev])
1409 rel = (RelOptInfo *) lfirst(lc);
1411 /* Find and save the cheapest paths for this rel */
1414 #ifdef OPTIMIZER_DEBUG
1415 debug_print_rel(root, rel);
1421 * We should have a single rel at the final level.
1423 if (root->join_rel_level[levels_needed] == NIL)
1424 elog(ERROR, "failed to build any %d-way joins", levels_needed);
1425 Assert(list_length(root->join_rel_level[levels_needed]) == 1);
1427 rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
1429 root->join_rel_level = NULL;
1434 /*****************************************************************************
1435 * PUSHING QUALS DOWN INTO SUBQUERIES
1436 *****************************************************************************/
1439 * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
1441 * subquery is the particular component query being checked. topquery
1442 * is the top component of a set-operations tree (the same Query if no
1443 * set-op is involved).
1445 * Conditions checked here:
1447 * 1. If the subquery has a LIMIT clause, we must not push down any quals,
1448 * since that could change the set of rows returned.
1450 * 2. If the subquery contains any window functions, we can't push quals
1451 * into it, because that could change the results.
1453 * 3. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
1454 * quals into it, because that could change the results.
1456 * 4. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
1457 * push quals into each component query, but the quals can only reference
1458 * subquery columns that suffer no type coercions in the set operation.
1459 * Otherwise there are possible semantic gotchas. So, we check the
1460 * component queries to see if any of them have different output types;
1461 * differentTypes[k] is set true if column k has different type in any
1465 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
1466 bool *differentTypes)
1468 SetOperationStmt *topop;
1471 if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
1475 if (subquery->hasWindowFuncs)
1478 /* Are we at top level, or looking at a setop component? */
1479 if (subquery == topquery)
1481 /* Top level, so check any component queries */
1482 if (subquery->setOperations != NULL)
1483 if (!recurse_pushdown_safe(subquery->setOperations, topquery,
1489 /* Setop component must not have more components (too weird) */
1490 if (subquery->setOperations != NULL)
1492 /* Check whether setop component output types match top level */
1493 topop = (SetOperationStmt *) topquery->setOperations;
1494 Assert(topop && IsA(topop, SetOperationStmt));
1495 compare_tlist_datatypes(subquery->targetList,
1503 * Helper routine to recurse through setOperations tree
1506 recurse_pushdown_safe(Node *setOp, Query *topquery,
1507 bool *differentTypes)
1509 if (IsA(setOp, RangeTblRef))
1511 RangeTblRef *rtr = (RangeTblRef *) setOp;
1512 RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
1513 Query *subquery = rte->subquery;
1515 Assert(subquery != NULL);
1516 return subquery_is_pushdown_safe(subquery, topquery, differentTypes);
1518 else if (IsA(setOp, SetOperationStmt))
1520 SetOperationStmt *op = (SetOperationStmt *) setOp;
1522 /* EXCEPT is no good */
1523 if (op->op == SETOP_EXCEPT)
1526 if (!recurse_pushdown_safe(op->larg, topquery, differentTypes))
1528 if (!recurse_pushdown_safe(op->rarg, topquery, differentTypes))
1533 elog(ERROR, "unrecognized node type: %d",
1534 (int) nodeTag(setOp));
1540 * Compare tlist's datatypes against the list of set-operation result types.
1541 * For any items that are different, mark the appropriate element of
1542 * differentTypes[] to show that this column will have type conversions.
1544 * We don't have to care about typmods here: the only allowed difference
1545 * between set-op input and output typmods is input is a specific typmod
1546 * and output is -1, and that does not require a coercion.
1549 compare_tlist_datatypes(List *tlist, List *colTypes,
1550 bool *differentTypes)
1553 ListCell *colType = list_head(colTypes);
1557 TargetEntry *tle = (TargetEntry *) lfirst(l);
1560 continue; /* ignore resjunk columns */
1561 if (colType == NULL)
1562 elog(ERROR, "wrong number of tlist entries");
1563 if (exprType((Node *) tle->expr) != lfirst_oid(colType))
1564 differentTypes[tle->resno] = true;
1565 colType = lnext(colType);
1567 if (colType != NULL)
1568 elog(ERROR, "wrong number of tlist entries");
1572 * qual_is_pushdown_safe - is a particular qual safe to push down?
1574 * qual is a restriction clause applying to the given subquery (whose RTE
1575 * has index rti in the parent query).
1577 * Conditions checked here:
1579 * 1. The qual must not contain any subselects (mainly because I'm not sure
1580 * it will work correctly: sublinks will already have been transformed into
1581 * subplans in the qual, but not in the subquery).
1583 * 2. The qual must not refer to the whole-row output of the subquery
1584 * (since there is no easy way to name that within the subquery itself).
1586 * 3. The qual must not refer to any subquery output columns that were
1587 * found to have inconsistent types across a set operation tree by
1588 * subquery_is_pushdown_safe().
1590 * 4. If the subquery uses DISTINCT ON, we must not push down any quals that
1591 * refer to non-DISTINCT output columns, because that could change the set
1592 * of rows returned. (This condition is vacuous for DISTINCT, because then
1593 * there are no non-DISTINCT output columns, so we needn't check. But note
1594 * we are assuming that the qual can't distinguish values that the DISTINCT
1595 * operator sees as equal. This is a bit shaky but we have no way to test
1596 * for the case, and it's unlikely enough that we shouldn't refuse the
1597 * optimization just because it could theoretically happen.)
1599 * 5. We must not push down any quals that refer to subselect outputs that
1600 * return sets, else we'd introduce functions-returning-sets into the
1601 * subquery's WHERE/HAVING quals.
1603 * 6. We must not push down any quals that refer to subselect outputs that
1604 * contain volatile functions, for fear of introducing strange results due
1605 * to multiple evaluation of a volatile function.
1608 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
1609 bool *differentTypes)
1614 Bitmapset *tested = NULL;
1616 /* Refuse subselects (point 1) */
1617 if (contain_subplans(qual))
1621 * It would be unsafe to push down window function calls, but at least for
1622 * the moment we could never see any in a qual anyhow. (The same applies
1623 * to aggregates, which we check for in pull_var_clause below.)
1625 Assert(!contain_window_function(qual));
1628 * Examine all Vars used in clause; since it's a restriction clause, all
1629 * such Vars must refer to subselect output columns.
1631 vars = pull_var_clause(qual,
1632 PVC_REJECT_AGGREGATES,
1633 PVC_INCLUDE_PLACEHOLDERS);
1636 Var *var = (Var *) lfirst(vl);
1640 * XXX Punt if we find any PlaceHolderVars in the restriction clause.
1641 * It's not clear whether a PHV could safely be pushed down, and even
1642 * less clear whether such a situation could arise in any cases of
1643 * practical interest anyway. So for the moment, just refuse to push
1652 Assert(var->varno == rti);
1655 if (var->varattno == 0)
1662 * We use a bitmapset to avoid testing the same attno more than once.
1663 * (NB: this only works because subquery outputs can't have negative
1666 if (bms_is_member(var->varattno, tested))
1668 tested = bms_add_member(tested, var->varattno);
1671 if (differentTypes[var->varattno])
1677 /* Must find the tlist element referenced by the Var */
1678 tle = get_tle_by_resno(subquery->targetList, var->varattno);
1679 Assert(tle != NULL);
1680 Assert(!tle->resjunk);
1682 /* If subquery uses DISTINCT ON, check point 4 */
1683 if (subquery->hasDistinctOn &&
1684 !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
1686 /* non-DISTINCT column, so fail */
1691 /* Refuse functions returning sets (point 5) */
1692 if (expression_returns_set((Node *) tle->expr))
1698 /* Refuse volatile functions (point 6) */
1699 if (contain_volatile_functions((Node *) tle->expr))
1713 * subquery_push_qual - push down a qual that we have determined is safe
1716 subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
1718 if (subquery->setOperations != NULL)
1720 /* Recurse to push it separately to each component query */
1721 recurse_push_qual(subquery->setOperations, subquery,
1727 * We need to replace Vars in the qual (which must refer to outputs of
1728 * the subquery) with copies of the subquery's targetlist expressions.
1729 * Note that at this point, any uplevel Vars in the qual should have
1730 * been replaced with Params, so they need no work.
1732 * This step also ensures that when we are pushing into a setop tree,
1733 * each component query gets its own copy of the qual.
1735 qual = ResolveNew(qual, rti, 0, rte,
1736 subquery->targetList,
1738 &subquery->hasSubLinks);
1741 * Now attach the qual to the proper place: normally WHERE, but if the
1742 * subquery uses grouping or aggregation, put it in HAVING (since the
1743 * qual really refers to the group-result rows).
1745 if (subquery->hasAggs || subquery->groupClause || subquery->havingQual)
1746 subquery->havingQual = make_and_qual(subquery->havingQual, qual);
1748 subquery->jointree->quals =
1749 make_and_qual(subquery->jointree->quals, qual);
1752 * We need not change the subquery's hasAggs or hasSublinks flags,
1753 * since we can't be pushing down any aggregates that weren't there
1754 * before, and we don't push down subselects at all.
1760 * Helper routine to recurse through setOperations tree
1763 recurse_push_qual(Node *setOp, Query *topquery,
1764 RangeTblEntry *rte, Index rti, Node *qual)
1766 if (IsA(setOp, RangeTblRef))
1768 RangeTblRef *rtr = (RangeTblRef *) setOp;
1769 RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
1770 Query *subquery = subrte->subquery;
1772 Assert(subquery != NULL);
1773 subquery_push_qual(subquery, rte, rti, qual);
1775 else if (IsA(setOp, SetOperationStmt))
1777 SetOperationStmt *op = (SetOperationStmt *) setOp;
1779 recurse_push_qual(op->larg, topquery, rte, rti, qual);
1780 recurse_push_qual(op->rarg, topquery, rte, rti, qual);
1784 elog(ERROR, "unrecognized node type: %d",
1785 (int) nodeTag(setOp));
1789 /*****************************************************************************
1791 *****************************************************************************/
1793 #ifdef OPTIMIZER_DEBUG
1796 print_relids(Relids relids)
1802 tmprelids = bms_copy(relids);
1803 while ((x = bms_first_member(tmprelids)) >= 0)
1810 bms_free(tmprelids);
1814 print_restrictclauses(PlannerInfo *root, List *clauses)
1820 RestrictInfo *c = lfirst(l);
1822 print_expr((Node *) c->clause, root->parse->rtable);
1829 print_path(PlannerInfo *root, Path *path, int indent)
1833 Path *subpath = NULL;
1836 switch (nodeTag(path))
1844 case T_BitmapHeapPath:
1845 ptype = "BitmapHeapScan";
1847 case T_BitmapAndPath:
1848 ptype = "BitmapAndPath";
1850 case T_BitmapOrPath:
1851 ptype = "BitmapOrPath";
1857 ptype = "ForeignScan";
1862 case T_MergeAppendPath:
1863 ptype = "MergeAppend";
1868 case T_MaterialPath:
1870 subpath = ((MaterialPath *) path)->subpath;
1874 subpath = ((UniquePath *) path)->subpath;
1881 ptype = "MergeJoin";
1893 for (i = 0; i < indent; i++)
1895 printf("%s", ptype);
1900 print_relids(path->parent->relids);
1901 printf(") rows=%.0f", path->parent->rows);
1903 printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
1907 for (i = 0; i < indent; i++)
1909 printf(" pathkeys: ");
1910 print_pathkeys(path->pathkeys, root->parse->rtable);
1915 JoinPath *jp = (JoinPath *) path;
1917 for (i = 0; i < indent; i++)
1919 printf(" clauses: ");
1920 print_restrictclauses(root, jp->joinrestrictinfo);
1923 if (IsA(path, MergePath))
1925 MergePath *mp = (MergePath *) path;
1927 for (i = 0; i < indent; i++)
1929 printf(" sortouter=%d sortinner=%d materializeinner=%d\n",
1930 ((mp->outersortkeys) ? 1 : 0),
1931 ((mp->innersortkeys) ? 1 : 0),
1932 ((mp->materialize_inner) ? 1 : 0));
1935 print_path(root, jp->outerjoinpath, indent + 1);
1936 print_path(root, jp->innerjoinpath, indent + 1);
1940 print_path(root, subpath, indent + 1);
1944 debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
1948 printf("RELOPTINFO (");
1949 print_relids(rel->relids);
1950 printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
1952 if (rel->baserestrictinfo)
1954 printf("\tbaserestrictinfo: ");
1955 print_restrictclauses(root, rel->baserestrictinfo);
1961 printf("\tjoininfo: ");
1962 print_restrictclauses(root, rel->joininfo);
1966 printf("\tpath list:\n");
1967 foreach(l, rel->pathlist)
1968 print_path(root, lfirst(l), 1);
1969 printf("\n\tcheapest startup path:\n");
1970 print_path(root, rel->cheapest_startup_path, 1);
1971 printf("\n\tcheapest total path:\n");
1972 print_path(root, rel->cheapest_total_path, 1);
1977 #endif /* OPTIMIZER_DEBUG */