1 /*-------------------------------------------------------------------------
4 * Routines to find possible search paths for processing a query
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
20 #include "nodes/nodeFuncs.h"
21 #ifdef OPTIMIZER_DEBUG
22 #include "nodes/print.h"
24 #include "optimizer/clauses.h"
25 #include "optimizer/cost.h"
26 #include "optimizer/geqo.h"
27 #include "optimizer/pathnode.h"
28 #include "optimizer/paths.h"
29 #include "optimizer/plancat.h"
30 #include "optimizer/planner.h"
31 #include "optimizer/prep.h"
32 #include "optimizer/var.h"
33 #include "parser/parse_clause.h"
34 #include "parser/parsetree.h"
35 #include "rewrite/rewriteManip.h"
38 /* These parameters are set by GUC */
39 bool enable_geqo
= false; /* just in case GUC doesn't set it */
42 /* Hook for plugins to replace standard_join_search() */
43 join_search_hook_type join_search_hook
= NULL
;
46 static void set_base_rel_pathlists(PlannerInfo
*root
);
47 static void set_rel_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
48 Index rti
, RangeTblEntry
*rte
);
49 static void set_plain_rel_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
51 static void set_append_rel_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
52 Index rti
, RangeTblEntry
*rte
);
53 static void set_dummy_rel_pathlist(RelOptInfo
*rel
);
54 static void set_subquery_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
55 Index rti
, RangeTblEntry
*rte
);
56 static void set_function_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
58 static void set_values_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
60 static void set_cte_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
62 static void set_worktable_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
64 static RelOptInfo
*make_rel_from_joinlist(PlannerInfo
*root
, List
*joinlist
);
65 static bool subquery_is_pushdown_safe(Query
*subquery
, Query
*topquery
,
66 bool *differentTypes
);
67 static bool recurse_pushdown_safe(Node
*setOp
, Query
*topquery
,
68 bool *differentTypes
);
69 static void compare_tlist_datatypes(List
*tlist
, List
*colTypes
,
70 bool *differentTypes
);
71 static bool qual_is_pushdown_safe(Query
*subquery
, Index rti
, Node
*qual
,
72 bool *differentTypes
);
73 static void subquery_push_qual(Query
*subquery
,
74 RangeTblEntry
*rte
, Index rti
, Node
*qual
);
75 static void recurse_push_qual(Node
*setOp
, Query
*topquery
,
76 RangeTblEntry
*rte
, Index rti
, Node
*qual
);
81 * Finds all possible access paths for executing a query, returning a
82 * single rel that represents the join of all base rels in the query.
85 make_one_rel(PlannerInfo
*root
, List
*joinlist
)
90 * Generate access paths for the base rels.
92 set_base_rel_pathlists(root
);
95 * Generate access paths for the entire join tree.
97 rel
= make_rel_from_joinlist(root
, joinlist
);
100 * The result should join all and only the query's base rels.
102 #ifdef USE_ASSERT_CHECKING
104 int num_base_rels
= 0;
107 for (rti
= 1; rti
< root
->simple_rel_array_size
; rti
++)
109 RelOptInfo
*brel
= root
->simple_rel_array
[rti
];
114 Assert(brel
->relid
== rti
); /* sanity check on array */
116 /* ignore RTEs that are "other rels" */
117 if (brel
->reloptkind
!= RELOPT_BASEREL
)
120 Assert(bms_is_member(rti
, rel
->relids
));
124 Assert(bms_num_members(rel
->relids
) == num_base_rels
);
132 * set_base_rel_pathlists
133 * Finds all paths available for scanning each base-relation entry.
134 * Sequential scan and any available indices are considered.
135 * Each useful path is attached to its relation's 'pathlist' field.
138 set_base_rel_pathlists(PlannerInfo
*root
)
142 for (rti
= 1; rti
< root
->simple_rel_array_size
; rti
++)
144 RelOptInfo
*rel
= root
->simple_rel_array
[rti
];
146 /* there may be empty slots corresponding to non-baserel RTEs */
150 Assert(rel
->relid
== rti
); /* sanity check on array */
152 /* ignore RTEs that are "other rels" */
153 if (rel
->reloptkind
!= RELOPT_BASEREL
)
156 set_rel_pathlist(root
, rel
, rti
, root
->simple_rte_array
[rti
]);
162 * Build access paths for a base relation
165 set_rel_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
166 Index rti
, RangeTblEntry
*rte
)
170 /* It's an "append relation", process accordingly */
171 set_append_rel_pathlist(root
, rel
, rti
, rte
);
173 else if (rel
->rtekind
== RTE_SUBQUERY
)
175 /* Subquery --- generate a separate plan for it */
176 set_subquery_pathlist(root
, rel
, rti
, rte
);
178 else if (rel
->rtekind
== RTE_FUNCTION
)
180 /* RangeFunction --- generate a suitable path for it */
181 set_function_pathlist(root
, rel
, rte
);
183 else if (rel
->rtekind
== RTE_VALUES
)
185 /* Values list --- generate a suitable path for it */
186 set_values_pathlist(root
, rel
, rte
);
188 else if (rel
->rtekind
== RTE_CTE
)
190 /* CTE reference --- generate a suitable path for it */
191 if (rte
->self_reference
)
192 set_worktable_pathlist(root
, rel
, rte
);
194 set_cte_pathlist(root
, rel
, rte
);
199 Assert(rel
->rtekind
== RTE_RELATION
);
200 set_plain_rel_pathlist(root
, rel
, rte
);
203 #ifdef OPTIMIZER_DEBUG
204 debug_print_rel(root
, rel
);
209 * set_plain_rel_pathlist
210 * Build access paths for a plain relation (no subquery, no inheritance)
213 set_plain_rel_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
, RangeTblEntry
*rte
)
216 * If we can prove we don't need to scan the rel via constraint exclusion,
217 * set up a single dummy path for it. We only need to check for regular
218 * baserels; if it's an otherrel, CE was already checked in
219 * set_append_rel_pathlist().
221 if (rel
->reloptkind
== RELOPT_BASEREL
&&
222 relation_excluded_by_constraints(root
, rel
, rte
))
224 set_dummy_rel_pathlist(rel
);
229 * Test any partial indexes of rel for applicability. We must do this
230 * first since partial unique indexes can affect size estimates.
232 check_partial_indexes(root
, rel
);
234 /* Mark rel with estimated output rows, width, etc */
235 set_baserel_size_estimates(root
, rel
);
238 * Check to see if we can extract any restriction conditions from join
239 * quals that are OR-of-AND structures. If so, add them to the rel's
240 * restriction list, and redo the above steps.
242 if (create_or_index_quals(root
, rel
))
244 check_partial_indexes(root
, rel
);
245 set_baserel_size_estimates(root
, rel
);
249 * Generate paths and add them to the rel's pathlist.
251 * Note: add_path() will discard any paths that are dominated by another
252 * available path, keeping only those paths that are superior along at
253 * least one dimension of cost or sortedness.
256 /* Consider sequential scan */
257 add_path(rel
, create_seqscan_path(root
, rel
));
259 /* Consider index scans */
260 create_index_paths(root
, rel
);
262 /* Consider TID scans */
263 create_tidscan_paths(root
, rel
);
265 /* Now find the cheapest of the paths for this rel */
270 * set_append_rel_pathlist
271 * Build access paths for an "append relation"
273 * The passed-in rel and RTE represent the entire append relation. The
274 * relation's contents are computed by appending together the output of
275 * the individual member relations. Note that in the inheritance case,
276 * the first member relation is actually the same table as is mentioned in
277 * the parent RTE ... but it has a different RTE and RelOptInfo. This is
278 * a good thing because their outputs are not the same size.
281 set_append_rel_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
282 Index rti
, RangeTblEntry
*rte
)
284 int parentRTindex
= rti
;
285 List
*subpaths
= NIL
;
288 double *parent_attrsizes
;
293 * Initialize to compute size estimates for whole append relation.
295 * We handle width estimates by weighting the widths of different child
296 * rels proportionally to their number of rows. This is sensible because
297 * the use of width estimates is mainly to compute the total relation
298 * "footprint" if we have to sort or hash it. To do this, we sum the
299 * total equivalent size (in "double" arithmetic) and then divide by the
300 * total rowcount estimate. This is done separately for the total rel
301 * width and each attribute.
303 * Note: if you consider changing this logic, beware that child rels could
304 * have zero rows and/or width, if they were excluded by constraints.
308 nattrs
= rel
->max_attr
- rel
->min_attr
+ 1;
309 parent_attrsizes
= (double *) palloc0(nattrs
* sizeof(double));
312 * Generate access paths for each member relation, and pick the cheapest
315 foreach(l
, root
->append_rel_list
)
317 AppendRelInfo
*appinfo
= (AppendRelInfo
*) lfirst(l
);
319 RangeTblEntry
*childRTE
;
320 RelOptInfo
*childrel
;
322 ListCell
*parentvars
;
325 /* append_rel_list contains all append rels; ignore others */
326 if (appinfo
->parent_relid
!= parentRTindex
)
329 childRTindex
= appinfo
->child_relid
;
330 childRTE
= root
->simple_rte_array
[childRTindex
];
333 * The child rel's RelOptInfo was already created during
334 * add_base_rels_to_query.
336 childrel
= find_base_rel(root
, childRTindex
);
337 Assert(childrel
->reloptkind
== RELOPT_OTHER_MEMBER_REL
);
340 * We have to copy the parent's targetlist and quals to the child,
341 * with appropriate substitution of variables. However, only the
342 * baserestrictinfo quals are needed before we can check for
343 * constraint exclusion; so do that first and then check to see if we
344 * can disregard this child.
346 childrel
->baserestrictinfo
= (List
*)
347 adjust_appendrel_attrs((Node
*) rel
->baserestrictinfo
,
350 if (relation_excluded_by_constraints(root
, childrel
, childRTE
))
353 * This child need not be scanned, so we can omit it from the
354 * appendrel. Mark it with a dummy cheapest-path though, in case
355 * best_appendrel_indexscan() looks at it later.
357 set_dummy_rel_pathlist(childrel
);
361 /* CE failed, so finish copying targetlist and join quals */
362 childrel
->joininfo
= (List
*)
363 adjust_appendrel_attrs((Node
*) rel
->joininfo
,
365 childrel
->reltargetlist
= (List
*)
366 adjust_appendrel_attrs((Node
*) rel
->reltargetlist
,
370 * We have to make child entries in the EquivalenceClass data
371 * structures as well.
373 if (rel
->has_eclass_joins
)
375 add_child_rel_equivalences(root
, appinfo
, rel
, childrel
);
376 childrel
->has_eclass_joins
= true;
380 * Note: we could compute appropriate attr_needed data for the child's
381 * variables, by transforming the parent's attr_needed through the
382 * translated_vars mapping. However, currently there's no need
383 * because attr_needed is only examined for base relations not
384 * otherrels. So we just leave the child's attr_needed empty.
388 * Compute the child's access paths, and add the cheapest one to the
389 * Append path we are constructing for the parent.
391 * It's possible that the child is itself an appendrel, in which case
392 * we can "cut out the middleman" and just add its child paths to our
393 * own list. (We don't try to do this earlier because we need to
394 * apply both levels of transformation to the quals.)
396 set_rel_pathlist(root
, childrel
, childRTindex
, childRTE
);
398 childpath
= childrel
->cheapest_total_path
;
399 if (IsA(childpath
, AppendPath
))
400 subpaths
= list_concat(subpaths
,
401 ((AppendPath
*) childpath
)->subpaths
);
403 subpaths
= lappend(subpaths
, childpath
);
406 * Accumulate size information from each child.
408 if (childrel
->rows
> 0)
410 parent_rows
+= childrel
->rows
;
411 parent_size
+= childrel
->width
* childrel
->rows
;
413 forboth(parentvars
, rel
->reltargetlist
,
414 childvars
, childrel
->reltargetlist
)
416 Var
*parentvar
= (Var
*) lfirst(parentvars
);
417 Var
*childvar
= (Var
*) lfirst(childvars
);
420 * Accumulate per-column estimates too. Whole-row Vars and
421 * PlaceHolderVars can be ignored here.
423 if (IsA(parentvar
, Var
) &&
426 int pndx
= parentvar
->varattno
- rel
->min_attr
;
427 int cndx
= childvar
->varattno
- childrel
->min_attr
;
429 parent_attrsizes
[pndx
] += childrel
->attr_widths
[cndx
] * childrel
->rows
;
436 * Save the finished size estimates.
438 rel
->rows
= parent_rows
;
443 rel
->width
= rint(parent_size
/ parent_rows
);
444 for (i
= 0; i
< nattrs
; i
++)
445 rel
->attr_widths
[i
] = rint(parent_attrsizes
[i
] / parent_rows
);
448 rel
->width
= 0; /* attr_widths should be zero already */
451 * Set "raw tuples" count equal to "rows" for the appendrel; needed
452 * because some places assume rel->tuples is valid for any baserel.
454 rel
->tuples
= parent_rows
;
456 pfree(parent_attrsizes
);
459 * Finally, build Append path and install it as the only access path for
460 * the parent rel. (Note: this is correct even if we have zero or one
461 * live subpath due to constraint exclusion.)
463 add_path(rel
, (Path
*) create_append_path(rel
, subpaths
));
465 /* Select cheapest path (pretty easy in this case...) */
470 * set_dummy_rel_pathlist
471 * Build a dummy path for a relation that's been excluded by constraints
473 * Rather than inventing a special "dummy" path type, we represent this as an
474 * AppendPath with no members (see also IS_DUMMY_PATH macro).
477 set_dummy_rel_pathlist(RelOptInfo
*rel
)
479 /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
483 add_path(rel
, (Path
*) create_append_path(rel
, NIL
));
485 /* Select cheapest path (pretty easy in this case...) */
489 /* quick-and-dirty test to see if any joining is needed */
491 has_multiple_baserels(PlannerInfo
*root
)
493 int num_base_rels
= 0;
496 for (rti
= 1; rti
< root
->simple_rel_array_size
; rti
++)
498 RelOptInfo
*brel
= root
->simple_rel_array
[rti
];
503 /* ignore RTEs that are "other rels" */
504 if (brel
->reloptkind
== RELOPT_BASEREL
)
505 if (++num_base_rels
> 1)
512 * set_subquery_pathlist
513 * Build the (single) access path for a subquery RTE
516 set_subquery_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
,
517 Index rti
, RangeTblEntry
*rte
)
519 Query
*parse
= root
->parse
;
520 Query
*subquery
= rte
->subquery
;
521 bool *differentTypes
;
522 double tuple_fraction
;
523 PlannerInfo
*subroot
;
527 * Must copy the Query so that planning doesn't mess up the RTE contents
528 * (really really need to fix the planner to not scribble on its input,
531 subquery
= copyObject(subquery
);
533 /* We need a workspace for keeping track of set-op type coercions */
534 differentTypes
= (bool *)
535 palloc0((list_length(subquery
->targetList
) + 1) * sizeof(bool));
538 * If there are any restriction clauses that have been attached to the
539 * subquery relation, consider pushing them down to become WHERE or HAVING
540 * quals of the subquery itself. This transformation is useful because it
541 * may allow us to generate a better plan for the subquery than evaluating
542 * all the subquery output rows and then filtering them.
544 * There are several cases where we cannot push down clauses. Restrictions
545 * involving the subquery are checked by subquery_is_pushdown_safe().
546 * Restrictions on individual clauses are checked by
547 * qual_is_pushdown_safe(). Also, we don't want to push down
548 * pseudoconstant clauses; better to have the gating node above the
551 * Non-pushed-down clauses will get evaluated as qpquals of the
554 * XXX Are there any cases where we want to make a policy decision not to
555 * push down a pushable qual, because it'd result in a worse plan?
557 if (rel
->baserestrictinfo
!= NIL
&&
558 subquery_is_pushdown_safe(subquery
, subquery
, differentTypes
))
560 /* OK to consider pushing down individual quals */
561 List
*upperrestrictlist
= NIL
;
564 foreach(l
, rel
->baserestrictinfo
)
566 RestrictInfo
*rinfo
= (RestrictInfo
*) lfirst(l
);
567 Node
*clause
= (Node
*) rinfo
->clause
;
569 if (!rinfo
->pseudoconstant
&&
570 qual_is_pushdown_safe(subquery
, rti
, clause
, differentTypes
))
573 subquery_push_qual(subquery
, rte
, rti
, clause
);
577 /* Keep it in the upper query */
578 upperrestrictlist
= lappend(upperrestrictlist
, rinfo
);
581 rel
->baserestrictinfo
= upperrestrictlist
;
584 pfree(differentTypes
);
587 * We can safely pass the outer tuple_fraction down to the subquery if the
588 * outer level has no joining, aggregation, or sorting to do. Otherwise
589 * we'd better tell the subquery to plan for full retrieval. (XXX This
590 * could probably be made more intelligent ...)
592 if (parse
->hasAggs
||
593 parse
->groupClause
||
595 parse
->distinctClause
||
597 has_multiple_baserels(root
))
598 tuple_fraction
= 0.0; /* default case */
600 tuple_fraction
= root
->tuple_fraction
;
602 /* Generate the plan for the subquery */
603 rel
->subplan
= subquery_planner(root
->glob
, subquery
,
605 false, tuple_fraction
,
607 rel
->subrtable
= subroot
->parse
->rtable
;
609 /* Copy number of output rows from subplan */
610 rel
->tuples
= rel
->subplan
->plan_rows
;
612 /* Mark rel with estimated output rows, width, etc */
613 set_baserel_size_estimates(root
, rel
);
615 /* Convert subquery pathkeys to outer representation */
616 pathkeys
= convert_subquery_pathkeys(root
, rel
, subroot
->query_pathkeys
);
618 /* Generate appropriate path */
619 add_path(rel
, create_subqueryscan_path(rel
, pathkeys
));
621 /* Select cheapest path (pretty easy in this case...) */
626 * set_function_pathlist
627 * Build the (single) access path for a function RTE
630 set_function_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
, RangeTblEntry
*rte
)
632 /* Mark rel with estimated output rows, width, etc */
633 set_function_size_estimates(root
, rel
);
635 /* Generate appropriate path */
636 add_path(rel
, create_functionscan_path(root
, rel
));
638 /* Select cheapest path (pretty easy in this case...) */
643 * set_values_pathlist
644 * Build the (single) access path for a VALUES RTE
647 set_values_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
, RangeTblEntry
*rte
)
649 /* Mark rel with estimated output rows, width, etc */
650 set_values_size_estimates(root
, rel
);
652 /* Generate appropriate path */
653 add_path(rel
, create_valuesscan_path(root
, rel
));
655 /* Select cheapest path (pretty easy in this case...) */
661 * Build the (single) access path for a non-self-reference CTE RTE
664 set_cte_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
, RangeTblEntry
*rte
)
667 PlannerInfo
*cteroot
;
674 * Find the referenced CTE, and locate the plan previously made for it.
676 levelsup
= rte
->ctelevelsup
;
678 while (levelsup
-- > 0)
680 cteroot
= cteroot
->parent_root
;
681 if (!cteroot
) /* shouldn't happen */
682 elog(ERROR
, "bad levelsup for CTE \"%s\"", rte
->ctename
);
686 * Note: cte_plan_ids can be shorter than cteList, if we are still working
687 * on planning the CTEs (ie, this is a side-reference from another CTE).
688 * So we mustn't use forboth here.
691 foreach(lc
, cteroot
->parse
->cteList
)
693 CommonTableExpr
*cte
= (CommonTableExpr
*) lfirst(lc
);
695 if (strcmp(cte
->ctename
, rte
->ctename
) == 0)
699 if (lc
== NULL
) /* shouldn't happen */
700 elog(ERROR
, "could not find CTE \"%s\"", rte
->ctename
);
701 if (ndx
>= list_length(cteroot
->cte_plan_ids
))
702 elog(ERROR
, "could not find plan for CTE \"%s\"", rte
->ctename
);
703 plan_id
= list_nth_int(cteroot
->cte_plan_ids
, ndx
);
705 cteplan
= (Plan
*) list_nth(root
->glob
->subplans
, plan_id
- 1);
707 /* Mark rel with estimated output rows, width, etc */
708 set_cte_size_estimates(root
, rel
, cteplan
);
710 /* Generate appropriate path */
711 add_path(rel
, create_ctescan_path(root
, rel
));
713 /* Select cheapest path (pretty easy in this case...) */
718 * set_worktable_pathlist
719 * Build the (single) access path for a self-reference CTE RTE
722 set_worktable_pathlist(PlannerInfo
*root
, RelOptInfo
*rel
, RangeTblEntry
*rte
)
725 PlannerInfo
*cteroot
;
729 * We need to find the non-recursive term's plan, which is in the plan
730 * level that's processing the recursive UNION, which is one level *below*
731 * where the CTE comes from.
733 levelsup
= rte
->ctelevelsup
;
734 if (levelsup
== 0) /* shouldn't happen */
735 elog(ERROR
, "bad levelsup for CTE \"%s\"", rte
->ctename
);
738 while (levelsup
-- > 0)
740 cteroot
= cteroot
->parent_root
;
741 if (!cteroot
) /* shouldn't happen */
742 elog(ERROR
, "bad levelsup for CTE \"%s\"", rte
->ctename
);
744 cteplan
= cteroot
->non_recursive_plan
;
745 if (!cteplan
) /* shouldn't happen */
746 elog(ERROR
, "could not find plan for CTE \"%s\"", rte
->ctename
);
748 /* Mark rel with estimated output rows, width, etc */
749 set_cte_size_estimates(root
, rel
, cteplan
);
751 /* Generate appropriate path */
752 add_path(rel
, create_worktablescan_path(root
, rel
));
754 /* Select cheapest path (pretty easy in this case...) */
759 * make_rel_from_joinlist
760 * Build access paths using a "joinlist" to guide the join path search.
762 * See comments for deconstruct_jointree() for definition of the joinlist
766 make_rel_from_joinlist(PlannerInfo
*root
, List
*joinlist
)
773 * Count the number of child joinlist nodes. This is the depth of the
774 * dynamic-programming algorithm we must employ to consider all ways of
775 * joining the child nodes.
777 levels_needed
= list_length(joinlist
);
779 if (levels_needed
<= 0)
780 return NULL
; /* nothing to do? */
783 * Construct a list of rels corresponding to the child joinlist nodes.
784 * This may contain both base rels and rels constructed according to
788 foreach(jl
, joinlist
)
790 Node
*jlnode
= (Node
*) lfirst(jl
);
793 if (IsA(jlnode
, RangeTblRef
))
795 int varno
= ((RangeTblRef
*) jlnode
)->rtindex
;
797 thisrel
= find_base_rel(root
, varno
);
799 else if (IsA(jlnode
, List
))
801 /* Recurse to handle subproblem */
802 thisrel
= make_rel_from_joinlist(root
, (List
*) jlnode
);
806 elog(ERROR
, "unrecognized joinlist node type: %d",
807 (int) nodeTag(jlnode
));
808 thisrel
= NULL
; /* keep compiler quiet */
811 initial_rels
= lappend(initial_rels
, thisrel
);
814 if (levels_needed
== 1)
817 * Single joinlist node, so we're done.
819 return (RelOptInfo
*) linitial(initial_rels
);
824 * Consider the different orders in which we could join the rels,
825 * using a plugin, GEQO, or the regular join search code.
827 * We put the initial_rels list into a PlannerInfo field because
828 * has_legal_joinclause() needs to look at it (ugly :-().
830 root
->initial_rels
= initial_rels
;
832 if (join_search_hook
)
833 return (*join_search_hook
) (root
, levels_needed
, initial_rels
);
834 else if (enable_geqo
&& levels_needed
>= geqo_threshold
)
835 return geqo(root
, levels_needed
, initial_rels
);
837 return standard_join_search(root
, levels_needed
, initial_rels
);
842 * standard_join_search
843 * Find possible joinpaths for a query by successively finding ways
844 * to join component relations into join relations.
846 * 'levels_needed' is the number of iterations needed, ie, the number of
847 * independent jointree items in the query. This is > 1.
849 * 'initial_rels' is a list of RelOptInfo nodes for each independent
850 * jointree item. These are the components to be joined together.
851 * Note that levels_needed == list_length(initial_rels).
853 * Returns the final level of join relations, i.e., the relation that is
854 * the result of joining all the original relations together.
855 * At least one implementation path must be provided for this relation and
856 * all required sub-relations.
858 * To support loadable plugins that modify planner behavior by changing the
859 * join searching algorithm, we provide a hook variable that lets a plugin
860 * replace or supplement this function. Any such hook must return the same
861 * final join relation as the standard code would, but it might have a
862 * different set of implementation paths attached, and only the sub-joinrels
863 * needed for these paths need have been instantiated.
865 * Note to plugin authors: the functions invoked during standard_join_search()
866 * modify root->join_rel_list and root->join_rel_hash. If you want to do more
867 * than one join-order search, you'll probably need to save and restore the
868 * original states of those data structures. See geqo_eval() for an example.
871 standard_join_search(PlannerInfo
*root
, int levels_needed
, List
*initial_rels
)
878 * We employ a simple "dynamic programming" algorithm: we first find all
879 * ways to build joins of two jointree items, then all ways to build joins
880 * of three items (from two-item joins and single items), then four-item
881 * joins, and so on until we have considered all ways to join all the
882 * items into one rel.
884 * joinitems[j] is a list of all the j-item rels. Initially we set
885 * joinitems[1] to represent all the single-jointree-item relations.
887 joinitems
= (List
**) palloc0((levels_needed
+ 1) * sizeof(List
*));
889 joinitems
[1] = initial_rels
;
891 for (lev
= 2; lev
<= levels_needed
; lev
++)
896 * Determine all possible pairs of relations to be joined at this
897 * level, and build paths for making each one from every available
898 * pair of lower-level relations.
900 joinitems
[lev
] = join_search_one_level(root
, lev
, joinitems
);
903 * Do cleanup work on each just-processed rel.
905 foreach(x
, joinitems
[lev
])
907 rel
= (RelOptInfo
*) lfirst(x
);
909 /* Find and save the cheapest paths for this rel */
912 #ifdef OPTIMIZER_DEBUG
913 debug_print_rel(root
, rel
);
919 * We should have a single rel at the final level.
921 if (joinitems
[levels_needed
] == NIL
)
922 elog(ERROR
, "failed to build any %d-way joins", levels_needed
);
923 Assert(list_length(joinitems
[levels_needed
]) == 1);
925 rel
= (RelOptInfo
*) linitial(joinitems
[levels_needed
]);
930 /*****************************************************************************
931 * PUSHING QUALS DOWN INTO SUBQUERIES
932 *****************************************************************************/
935 * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
937 * subquery is the particular component query being checked. topquery
938 * is the top component of a set-operations tree (the same Query if no
939 * set-op is involved).
941 * Conditions checked here:
943 * 1. If the subquery has a LIMIT clause, we must not push down any quals,
944 * since that could change the set of rows returned.
946 * 2. If the subquery contains any window functions, we can't push quals
947 * into it, because that would change the results.
949 * 3. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
950 * quals into it, because that would change the results.
952 * 4. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
953 * push quals into each component query, but the quals can only reference
954 * subquery columns that suffer no type coercions in the set operation.
955 * Otherwise there are possible semantic gotchas. So, we check the
956 * component queries to see if any of them have different output types;
957 * differentTypes[k] is set true if column k has different type in any
961 subquery_is_pushdown_safe(Query
*subquery
, Query
*topquery
,
962 bool *differentTypes
)
964 SetOperationStmt
*topop
;
967 if (subquery
->limitOffset
!= NULL
|| subquery
->limitCount
!= NULL
)
971 if (subquery
->hasWindowFuncs
)
974 /* Are we at top level, or looking at a setop component? */
975 if (subquery
== topquery
)
977 /* Top level, so check any component queries */
978 if (subquery
->setOperations
!= NULL
)
979 if (!recurse_pushdown_safe(subquery
->setOperations
, topquery
,
985 /* Setop component must not have more components (too weird) */
986 if (subquery
->setOperations
!= NULL
)
988 /* Check whether setop component output types match top level */
989 topop
= (SetOperationStmt
*) topquery
->setOperations
;
990 Assert(topop
&& IsA(topop
, SetOperationStmt
));
991 compare_tlist_datatypes(subquery
->targetList
,
999 * Helper routine to recurse through setOperations tree
1002 recurse_pushdown_safe(Node
*setOp
, Query
*topquery
,
1003 bool *differentTypes
)
1005 if (IsA(setOp
, RangeTblRef
))
1007 RangeTblRef
*rtr
= (RangeTblRef
*) setOp
;
1008 RangeTblEntry
*rte
= rt_fetch(rtr
->rtindex
, topquery
->rtable
);
1009 Query
*subquery
= rte
->subquery
;
1011 Assert(subquery
!= NULL
);
1012 return subquery_is_pushdown_safe(subquery
, topquery
, differentTypes
);
1014 else if (IsA(setOp
, SetOperationStmt
))
1016 SetOperationStmt
*op
= (SetOperationStmt
*) setOp
;
1018 /* EXCEPT is no good */
1019 if (op
->op
== SETOP_EXCEPT
)
1022 if (!recurse_pushdown_safe(op
->larg
, topquery
, differentTypes
))
1024 if (!recurse_pushdown_safe(op
->rarg
, topquery
, differentTypes
))
1029 elog(ERROR
, "unrecognized node type: %d",
1030 (int) nodeTag(setOp
));
1036 * Compare tlist's datatypes against the list of set-operation result types.
1037 * For any items that are different, mark the appropriate element of
1038 * differentTypes[] to show that this column will have type conversions.
1040 * We don't have to care about typmods here: the only allowed difference
1041 * between set-op input and output typmods is input is a specific typmod
1042 * and output is -1, and that does not require a coercion.
1045 compare_tlist_datatypes(List
*tlist
, List
*colTypes
,
1046 bool *differentTypes
)
1049 ListCell
*colType
= list_head(colTypes
);
1053 TargetEntry
*tle
= (TargetEntry
*) lfirst(l
);
1056 continue; /* ignore resjunk columns */
1057 if (colType
== NULL
)
1058 elog(ERROR
, "wrong number of tlist entries");
1059 if (exprType((Node
*) tle
->expr
) != lfirst_oid(colType
))
1060 differentTypes
[tle
->resno
] = true;
1061 colType
= lnext(colType
);
1063 if (colType
!= NULL
)
1064 elog(ERROR
, "wrong number of tlist entries");
1068 * qual_is_pushdown_safe - is a particular qual safe to push down?
1070 * qual is a restriction clause applying to the given subquery (whose RTE
1071 * has index rti in the parent query).
1073 * Conditions checked here:
1075 * 1. The qual must not contain any subselects (mainly because I'm not sure
1076 * it will work correctly: sublinks will already have been transformed into
1077 * subplans in the qual, but not in the subquery).
1079 * 2. The qual must not refer to the whole-row output of the subquery
1080 * (since there is no easy way to name that within the subquery itself).
1082 * 3. The qual must not refer to any subquery output columns that were
1083 * found to have inconsistent types across a set operation tree by
1084 * subquery_is_pushdown_safe().
1086 * 4. If the subquery uses DISTINCT ON, we must not push down any quals that
1087 * refer to non-DISTINCT output columns, because that could change the set
1088 * of rows returned. (This condition is vacuous for DISTINCT, because then
1089 * there are no non-DISTINCT output columns, so we needn't check. But note
1090 * we are assuming that the qual can't distinguish values that the DISTINCT
1091 * operator sees as equal. This is a bit shaky but we have no way to test
1092 * for the case, and it's unlikely enough that we shouldn't refuse the
1093 * optimization just because it could theoretically happen.)
1095 * 5. We must not push down any quals that refer to subselect outputs that
1096 * return sets, else we'd introduce functions-returning-sets into the
1097 * subquery's WHERE/HAVING quals.
1099 * 6. We must not push down any quals that refer to subselect outputs that
1100 * contain volatile functions, for fear of introducing strange results due
1101 * to multiple evaluation of a volatile function.
1104 qual_is_pushdown_safe(Query
*subquery
, Index rti
, Node
*qual
,
1105 bool *differentTypes
)
1110 Bitmapset
*tested
= NULL
;
1112 /* Refuse subselects (point 1) */
1113 if (contain_subplans(qual
))
1117 * It would be unsafe to push down window function calls, but at least for
1118 * the moment we could never see any in a qual anyhow.
1120 Assert(!contain_window_function(qual
));
1123 * Examine all Vars used in clause; since it's a restriction clause, all
1124 * such Vars must refer to subselect output columns.
1126 vars
= pull_var_clause(qual
, PVC_INCLUDE_PLACEHOLDERS
);
1129 Var
*var
= (Var
*) lfirst(vl
);
1133 * XXX Punt if we find any PlaceHolderVars in the restriction clause.
1134 * It's not clear whether a PHV could safely be pushed down, and even
1135 * less clear whether such a situation could arise in any cases of
1136 * practical interest anyway. So for the moment, just refuse to push
1145 Assert(var
->varno
== rti
);
1148 if (var
->varattno
== 0)
1155 * We use a bitmapset to avoid testing the same attno more than once.
1156 * (NB: this only works because subquery outputs can't have negative
1159 if (bms_is_member(var
->varattno
, tested
))
1161 tested
= bms_add_member(tested
, var
->varattno
);
1164 if (differentTypes
[var
->varattno
])
1170 /* Must find the tlist element referenced by the Var */
1171 tle
= get_tle_by_resno(subquery
->targetList
, var
->varattno
);
1172 Assert(tle
!= NULL
);
1173 Assert(!tle
->resjunk
);
1175 /* If subquery uses DISTINCT ON, check point 4 */
1176 if (subquery
->hasDistinctOn
&&
1177 !targetIsInSortList(tle
, InvalidOid
, subquery
->distinctClause
))
1179 /* non-DISTINCT column, so fail */
1184 /* Refuse functions returning sets (point 5) */
1185 if (expression_returns_set((Node
*) tle
->expr
))
1191 /* Refuse volatile functions (point 6) */
1192 if (contain_volatile_functions((Node
*) tle
->expr
))
1206 * subquery_push_qual - push down a qual that we have determined is safe
1209 subquery_push_qual(Query
*subquery
, RangeTblEntry
*rte
, Index rti
, Node
*qual
)
1211 if (subquery
->setOperations
!= NULL
)
1213 /* Recurse to push it separately to each component query */
1214 recurse_push_qual(subquery
->setOperations
, subquery
,
1220 * We need to replace Vars in the qual (which must refer to outputs of
1221 * the subquery) with copies of the subquery's targetlist expressions.
1222 * Note that at this point, any uplevel Vars in the qual should have
1223 * been replaced with Params, so they need no work.
1225 * This step also ensures that when we are pushing into a setop tree,
1226 * each component query gets its own copy of the qual.
1228 qual
= ResolveNew(qual
, rti
, 0, rte
,
1229 subquery
->targetList
,
1233 * Now attach the qual to the proper place: normally WHERE, but if the
1234 * subquery uses grouping or aggregation, put it in HAVING (since the
1235 * qual really refers to the group-result rows).
1237 if (subquery
->hasAggs
|| subquery
->groupClause
|| subquery
->havingQual
)
1238 subquery
->havingQual
= make_and_qual(subquery
->havingQual
, qual
);
1240 subquery
->jointree
->quals
=
1241 make_and_qual(subquery
->jointree
->quals
, qual
);
1244 * We need not change the subquery's hasAggs or hasSublinks flags,
1245 * since we can't be pushing down any aggregates that weren't there
1246 * before, and we don't push down subselects at all.
1252 * Helper routine to recurse through setOperations tree
1255 recurse_push_qual(Node
*setOp
, Query
*topquery
,
1256 RangeTblEntry
*rte
, Index rti
, Node
*qual
)
1258 if (IsA(setOp
, RangeTblRef
))
1260 RangeTblRef
*rtr
= (RangeTblRef
*) setOp
;
1261 RangeTblEntry
*subrte
= rt_fetch(rtr
->rtindex
, topquery
->rtable
);
1262 Query
*subquery
= subrte
->subquery
;
1264 Assert(subquery
!= NULL
);
1265 subquery_push_qual(subquery
, rte
, rti
, qual
);
1267 else if (IsA(setOp
, SetOperationStmt
))
1269 SetOperationStmt
*op
= (SetOperationStmt
*) setOp
;
1271 recurse_push_qual(op
->larg
, topquery
, rte
, rti
, qual
);
1272 recurse_push_qual(op
->rarg
, topquery
, rte
, rti
, qual
);
1276 elog(ERROR
, "unrecognized node type: %d",
1277 (int) nodeTag(setOp
));
1281 /*****************************************************************************
1283 *****************************************************************************/
1285 #ifdef OPTIMIZER_DEBUG
1288 print_relids(Relids relids
)
1294 tmprelids
= bms_copy(relids
);
1295 while ((x
= bms_first_member(tmprelids
)) >= 0)
1302 bms_free(tmprelids
);
1306 print_restrictclauses(PlannerInfo
*root
, List
*clauses
)
1312 RestrictInfo
*c
= lfirst(l
);
1314 print_expr((Node
*) c
->clause
, root
->parse
->rtable
);
1321 print_path(PlannerInfo
*root
, Path
*path
, int indent
)
1325 Path
*subpath
= NULL
;
1328 switch (nodeTag(path
))
1336 case T_BitmapHeapPath
:
1337 ptype
= "BitmapHeapScan";
1339 case T_BitmapAndPath
:
1340 ptype
= "BitmapAndPath";
1342 case T_BitmapOrPath
:
1343 ptype
= "BitmapOrPath";
1354 case T_MaterialPath
:
1356 subpath
= ((MaterialPath
*) path
)->subpath
;
1360 subpath
= ((UniquePath
*) path
)->subpath
;
1367 ptype
= "MergeJoin";
1379 for (i
= 0; i
< indent
; i
++)
1381 printf("%s", ptype
);
1386 print_relids(path
->parent
->relids
);
1387 printf(") rows=%.0f", path
->parent
->rows
);
1389 printf(" cost=%.2f..%.2f\n", path
->startup_cost
, path
->total_cost
);
1393 for (i
= 0; i
< indent
; i
++)
1395 printf(" pathkeys: ");
1396 print_pathkeys(path
->pathkeys
, root
->parse
->rtable
);
1401 JoinPath
*jp
= (JoinPath
*) path
;
1403 for (i
= 0; i
< indent
; i
++)
1405 printf(" clauses: ");
1406 print_restrictclauses(root
, jp
->joinrestrictinfo
);
1409 if (IsA(path
, MergePath
))
1411 MergePath
*mp
= (MergePath
*) path
;
1413 if (mp
->outersortkeys
|| mp
->innersortkeys
)
1415 for (i
= 0; i
< indent
; i
++)
1417 printf(" sortouter=%d sortinner=%d\n",
1418 ((mp
->outersortkeys
) ? 1 : 0),
1419 ((mp
->innersortkeys
) ? 1 : 0));
1423 print_path(root
, jp
->outerjoinpath
, indent
+ 1);
1424 print_path(root
, jp
->innerjoinpath
, indent
+ 1);
1428 print_path(root
, subpath
, indent
+ 1);
1432 debug_print_rel(PlannerInfo
*root
, RelOptInfo
*rel
)
1436 printf("RELOPTINFO (");
1437 print_relids(rel
->relids
);
1438 printf("): rows=%.0f width=%d\n", rel
->rows
, rel
->width
);
1440 if (rel
->baserestrictinfo
)
1442 printf("\tbaserestrictinfo: ");
1443 print_restrictclauses(root
, rel
->baserestrictinfo
);
1449 printf("\tjoininfo: ");
1450 print_restrictclauses(root
, rel
->joininfo
);
1454 printf("\tpath list:\n");
1455 foreach(l
, rel
->pathlist
)
1456 print_path(root
, lfirst(l
), 1);
1457 printf("\n\tcheapest startup path:\n");
1458 print_path(root
, rel
->cheapest_startup_path
, 1);
1459 printf("\n\tcheapest total path:\n");
1460 print_path(root
, rel
->cheapest_total_path
, 1);
1465 #endif /* OPTIMIZER_DEBUG */