1 /*-------------------------------------------------------------------------
4 * Routines to find possible search paths for processing a query
6 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.134 2005/06/10 03:32:21 tgl Exp $
13 *-------------------------------------------------------------------------
18 #ifdef OPTIMIZER_DEBUG
19 #include "nodes/print.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/geqo.h"
24 #include "optimizer/pathnode.h"
25 #include "optimizer/paths.h"
26 #include "optimizer/plancat.h"
27 #include "optimizer/planner.h"
28 #include "optimizer/prep.h"
29 #include "optimizer/var.h"
30 #include "parser/parsetree.h"
31 #include "parser/parse_clause.h"
32 #include "parser/parse_expr.h"
33 #include "rewrite/rewriteManip.h"
36 /* These parameters are set by GUC */
37 bool enable_geqo = false; /* just in case GUC doesn't set it */
41 static void set_base_rel_pathlists(PlannerInfo *root);
42 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
44 static void set_inherited_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
45 Index rti, RangeTblEntry *rte,
47 static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
48 Index rti, RangeTblEntry *rte);
49 static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
51 static RelOptInfo *make_one_rel_by_joins(PlannerInfo *root, int levels_needed,
53 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
54 bool *differentTypes);
55 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
56 bool *differentTypes);
57 static void compare_tlist_datatypes(List *tlist, List *colTypes,
58 bool *differentTypes);
59 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
60 bool *differentTypes);
61 static void subquery_push_qual(Query *subquery,
62 RangeTblEntry *rte, Index rti, Node *qual);
63 static void recurse_push_qual(Node *setOp, Query *topquery,
64 RangeTblEntry *rte, Index rti, Node *qual);
69 * Finds all possible access paths for executing a query, returning a
70 * single rel that represents the join of all base rels in the query.
73 make_one_rel(PlannerInfo *root)
78 * Generate access paths for the base rels.
80 set_base_rel_pathlists(root);
83 * Generate access paths for the entire join tree.
85 Assert(root->parse->jointree != NULL &&
86 IsA(root->parse->jointree, FromExpr));
88 rel = make_fromexpr_rel(root, root->parse->jointree);
91 * The result should join all and only the query's base rels.
93 #ifdef USE_ASSERT_CHECKING
95 int num_base_rels = 0;
98 for (rti = 1; rti < root->base_rel_array_size; rti++)
100 RelOptInfo *brel = root->base_rel_array[rti];
105 Assert(brel->relid == rti); /* sanity check on array */
107 /* ignore RTEs that are "other rels" */
108 if (brel->reloptkind != RELOPT_BASEREL)
111 Assert(bms_is_member(rti, rel->relids));
115 Assert(bms_num_members(rel->relids) == num_base_rels);
123 * set_base_rel_pathlists
124 * Finds all paths available for scanning each base-relation entry.
125 * Sequential scan and any available indices are considered.
126 * Each useful path is attached to its relation's 'pathlist' field.
129 set_base_rel_pathlists(PlannerInfo *root)
134 * Note: because we call expand_inherited_rtentry inside the loop,
135 * it's quite possible for the base_rel_array to be enlarged while
136 * the loop runs. Hence don't try to optimize the loop.
138 for (rti = 1; rti < root->base_rel_array_size; rti++)
140 RelOptInfo *rel = root->base_rel_array[rti];
144 /* there may be empty slots corresponding to non-baserel RTEs */
148 Assert(rel->relid == rti); /* sanity check on array */
150 /* ignore RTEs that are "other rels" */
151 if (rel->reloptkind != RELOPT_BASEREL)
154 rte = rt_fetch(rti, root->parse->rtable);
156 if (rel->rtekind == RTE_SUBQUERY)
158 /* Subquery --- generate a separate plan for it */
159 set_subquery_pathlist(root, rel, rti, rte);
161 else if (rel->rtekind == RTE_FUNCTION)
163 /* RangeFunction --- generate a separate plan for it */
164 set_function_pathlist(root, rel, rte);
166 else if ((inheritlist = expand_inherited_rtentry(root, rti)) != NIL)
168 /* Relation is root of an inheritance tree, process specially */
169 set_inherited_rel_pathlist(root, rel, rti, rte, inheritlist);
174 set_plain_rel_pathlist(root, rel, rte);
177 #ifdef OPTIMIZER_DEBUG
178 debug_print_rel(root, rel);
184 * set_plain_rel_pathlist
185 * Build access paths for a plain relation (no subquery, no inheritance)
188 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
190 /* Mark rel with estimated output rows, width, etc */
191 set_baserel_size_estimates(root, rel);
193 /* Test any partial indexes of rel for applicability */
194 check_partial_indexes(root, rel);
197 * Check to see if we can extract any restriction conditions from join
198 * quals that are OR-of-AND structures. If so, add them to the rel's
199 * restriction list, and recompute the size estimates.
201 if (create_or_index_quals(root, rel))
202 set_baserel_size_estimates(root, rel);
205 * Generate paths and add them to the rel's pathlist.
207 * Note: add_path() will discard any paths that are dominated by another
208 * available path, keeping only those paths that are superior along at
209 * least one dimension of cost or sortedness.
212 /* Consider sequential scan */
213 add_path(rel, create_seqscan_path(root, rel));
215 /* Consider index scans */
216 create_index_paths(root, rel);
218 /* Consider TID scans */
219 create_tidscan_paths(root, rel);
221 /* Now find the cheapest of the paths for this rel */
226 * set_inherited_rel_pathlist
227 * Build access paths for a inheritance tree rooted at rel
229 * inheritlist is a list of RT indexes of all tables in the inheritance tree,
230 * including a duplicate of the parent itself. Note we will not come here
231 * unless there's at least one child in addition to the parent.
233 * NOTE: the passed-in rel and RTE will henceforth represent the appended
234 * result of the whole inheritance tree. The members of inheritlist represent
235 * the individual tables --- in particular, the inheritlist member that is a
236 * duplicate of the parent RTE represents the parent table alone.
237 * We will generate plans to scan the individual tables that refer to
238 * the inheritlist RTEs, whereas Vars elsewhere in the plan tree that
239 * refer to the original RTE are taken to refer to the append output.
240 * In particular, this means we have separate RelOptInfos for the parent
241 * table and for the append output, which is a good thing because they're
245 set_inherited_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
246 Index rti, RangeTblEntry *rte,
249 int parentRTindex = rti;
250 Oid parentOID = rte->relid;
251 List *subpaths = NIL;
255 * XXX for now, can't handle inherited expansion of FOR UPDATE/SHARE;
258 if (list_member_int(root->parse->rowMarks, parentRTindex))
260 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
261 errmsg("SELECT FOR UPDATE/SHARE is not supported for inheritance queries")));
264 * Initialize to compute size estimates for whole inheritance tree
270 * Generate access paths for each table in the tree (parent AND
271 * children), and pick the cheapest path for each table.
273 foreach(il, inheritlist)
275 int childRTindex = lfirst_int(il);
276 RangeTblEntry *childrte;
278 RelOptInfo *childrel;
279 ListCell *parentvars;
282 childrte = rt_fetch(childRTindex, root->parse->rtable);
283 childOID = childrte->relid;
286 * Make a RelOptInfo for the child so we can do planning.
287 * Mark it as an "other rel" since it will not be part of the
290 childrel = build_other_rel(root, childRTindex);
293 * Copy the parent's targetlist and restriction quals to the
294 * child, with attribute-number adjustment as needed. We don't
295 * bother to copy the join quals, since we can't do any joining of
296 * the individual tables. Also, we just zap attr_needed rather
297 * than trying to adjust it; it won't be looked at in the child.
299 childrel->reltargetlist = (List *)
300 adjust_inherited_attrs((Node *) rel->reltargetlist,
305 childrel->attr_needed = NULL;
306 childrel->baserestrictinfo = (List *)
307 adjust_inherited_attrs((Node *) rel->baserestrictinfo,
314 * Now compute child access paths, and save the cheapest.
316 set_plain_rel_pathlist(root, childrel, childrte);
318 subpaths = lappend(subpaths, childrel->cheapest_total_path);
321 * Propagate size information from the child back to the parent.
322 * For simplicity, we use the largest widths from any child as the
325 rel->rows += childrel->rows;
326 if (childrel->width > rel->width)
327 rel->width = childrel->width;
329 forboth(parentvars, rel->reltargetlist,
330 childvars, childrel->reltargetlist)
332 Var *parentvar = (Var *) lfirst(parentvars);
333 Var *childvar = (Var *) lfirst(childvars);
335 if (IsA(parentvar, Var) &&IsA(childvar, Var))
337 int pndx = parentvar->varattno - rel->min_attr;
338 int cndx = childvar->varattno - childrel->min_attr;
340 if (childrel->attr_widths[cndx] > rel->attr_widths[pndx])
341 rel->attr_widths[pndx] = childrel->attr_widths[cndx];
347 * Finally, build Append path and install it as the only access path
348 * for the parent rel.
350 add_path(rel, (Path *) create_append_path(rel, subpaths));
352 /* Select cheapest path (pretty easy in this case...) */
356 /* quick-and-dirty test to see if any joining is needed */
358 has_multiple_baserels(PlannerInfo *root)
360 int num_base_rels = 0;
363 for (rti = 1; rti < root->base_rel_array_size; rti++)
365 RelOptInfo *brel = root->base_rel_array[rti];
370 /* ignore RTEs that are "other rels" */
371 if (brel->reloptkind == RELOPT_BASEREL)
372 if (++num_base_rels > 1)
379 * set_subquery_pathlist
380 * Build the (single) access path for a subquery RTE
383 set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
384 Index rti, RangeTblEntry *rte)
386 Query *parse = root->parse;
387 Query *subquery = rte->subquery;
388 bool *differentTypes;
389 double tuple_fraction;
391 List *subquery_pathkeys;
393 /* We need a workspace for keeping track of set-op type coercions */
394 differentTypes = (bool *)
395 palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
398 * If there are any restriction clauses that have been attached to the
399 * subquery relation, consider pushing them down to become WHERE or
400 * HAVING quals of the subquery itself. This transformation is useful
401 * because it may allow us to generate a better plan for the subquery
402 * than evaluating all the subquery output rows and then filtering them.
404 * There are several cases where we cannot push down clauses.
405 * Restrictions involving the subquery are checked by
406 * subquery_is_pushdown_safe(). Restrictions on individual clauses
407 * are checked by qual_is_pushdown_safe().
409 * Non-pushed-down clauses will get evaluated as qpquals of the
412 * XXX Are there any cases where we want to make a policy decision not to
413 * push down a pushable qual, because it'd result in a worse plan?
415 if (rel->baserestrictinfo != NIL &&
416 subquery_is_pushdown_safe(subquery, subquery, differentTypes))
418 /* OK to consider pushing down individual quals */
419 List *upperrestrictlist = NIL;
422 foreach(l, rel->baserestrictinfo)
424 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
425 Node *clause = (Node *) rinfo->clause;
427 if (qual_is_pushdown_safe(subquery, rti, clause, differentTypes))
430 subquery_push_qual(subquery, rte, rti, clause);
434 /* Keep it in the upper query */
435 upperrestrictlist = lappend(upperrestrictlist, rinfo);
438 rel->baserestrictinfo = upperrestrictlist;
441 pfree(differentTypes);
444 * We can safely pass the outer tuple_fraction down to the subquery
445 * if the outer level has no joining, aggregation, or sorting to do.
446 * Otherwise we'd better tell the subquery to plan for full retrieval.
447 * (XXX This could probably be made more intelligent ...)
449 if (parse->hasAggs ||
450 parse->groupClause ||
452 parse->distinctClause ||
454 has_multiple_baserels(root))
455 tuple_fraction = 0.0; /* default case */
457 tuple_fraction = root->tuple_fraction;
459 /* Generate the plan for the subquery */
460 rel->subplan = subquery_planner(subquery, tuple_fraction,
463 /* Copy number of output rows from subplan */
464 rel->tuples = rel->subplan->plan_rows;
466 /* Mark rel with estimated output rows, width, etc */
467 set_baserel_size_estimates(root, rel);
469 /* Convert subquery pathkeys to outer representation */
470 pathkeys = convert_subquery_pathkeys(root, rel, subquery_pathkeys);
472 /* Generate appropriate path */
473 add_path(rel, create_subqueryscan_path(rel, pathkeys));
475 /* Select cheapest path (pretty easy in this case...) */
480 * set_function_pathlist
481 * Build the (single) access path for a function RTE
484 set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
486 /* Mark rel with estimated output rows, width, etc */
487 set_function_size_estimates(root, rel);
489 /* Generate appropriate path */
490 add_path(rel, create_functionscan_path(root, rel));
492 /* Select cheapest path (pretty easy in this case...) */
498 * Build access paths for a FromExpr jointree node.
501 make_fromexpr_rel(PlannerInfo *root, FromExpr *from)
504 List *initial_rels = NIL;
508 * Count the number of child jointree nodes. This is the depth of the
509 * dynamic-programming algorithm we must employ to consider all ways
510 * of joining the child nodes.
512 levels_needed = list_length(from->fromlist);
514 if (levels_needed <= 0)
515 return NULL; /* nothing to do? */
518 * Construct a list of rels corresponding to the child jointree nodes.
519 * This may contain both base rels and rels constructed according to
520 * explicit JOIN directives.
522 foreach(jt, from->fromlist)
524 Node *jtnode = (Node *) lfirst(jt);
526 initial_rels = lappend(initial_rels,
527 make_jointree_rel(root, jtnode));
530 if (levels_needed == 1)
533 * Single jointree node, so we're done.
535 return (RelOptInfo *) linitial(initial_rels);
540 * Consider the different orders in which we could join the rels,
541 * using either GEQO or regular optimizer.
543 if (enable_geqo && levels_needed >= geqo_threshold)
544 return geqo(root, levels_needed, initial_rels);
546 return make_one_rel_by_joins(root, levels_needed, initial_rels);
551 * make_one_rel_by_joins
552 * Find all possible joinpaths for a query by successively finding ways
553 * to join component relations into join relations.
555 * 'levels_needed' is the number of iterations needed, ie, the number of
556 * independent jointree items in the query. This is > 1.
558 * 'initial_rels' is a list of RelOptInfo nodes for each independent
559 * jointree item. These are the components to be joined together.
561 * Returns the final level of join relations, i.e., the relation that is
562 * the result of joining all the original relations together.
565 make_one_rel_by_joins(PlannerInfo *root, int levels_needed, List *initial_rels)
572 * We employ a simple "dynamic programming" algorithm: we first find
573 * all ways to build joins of two jointree items, then all ways to
574 * build joins of three items (from two-item joins and single items),
575 * then four-item joins, and so on until we have considered all ways
576 * to join all the items into one rel.
578 * joinitems[j] is a list of all the j-item rels. Initially we set
579 * joinitems[1] to represent all the single-jointree-item relations.
581 joinitems = (List **) palloc0((levels_needed + 1) * sizeof(List *));
583 joinitems[1] = initial_rels;
585 for (lev = 2; lev <= levels_needed; lev++)
590 * Determine all possible pairs of relations to be joined at this
591 * level, and build paths for making each one from every available
592 * pair of lower-level relations.
594 joinitems[lev] = make_rels_by_joins(root, lev, joinitems);
597 * Do cleanup work on each just-processed rel.
599 foreach(x, joinitems[lev])
601 rel = (RelOptInfo *) lfirst(x);
603 /* Find and save the cheapest paths for this rel */
606 #ifdef OPTIMIZER_DEBUG
607 debug_print_rel(root, rel);
613 * We should have a single rel at the final level.
615 if (joinitems[levels_needed] == NIL)
616 elog(ERROR, "failed to build any %d-way joins", levels_needed);
617 Assert(list_length(joinitems[levels_needed]) == 1);
619 rel = (RelOptInfo *) linitial(joinitems[levels_needed]);
624 /*****************************************************************************
625 * PUSHING QUALS DOWN INTO SUBQUERIES
626 *****************************************************************************/
629 * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
631 * subquery is the particular component query being checked. topquery
632 * is the top component of a set-operations tree (the same Query if no
633 * set-op is involved).
635 * Conditions checked here:
637 * 1. If the subquery has a LIMIT clause, we must not push down any quals,
638 * since that could change the set of rows returned.
640 * 2. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
641 * quals into it, because that would change the results.
643 * 3. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
644 * push quals into each component query, but the quals can only reference
645 * subquery columns that suffer no type coercions in the set operation.
646 * Otherwise there are possible semantic gotchas. So, we check the
647 * component queries to see if any of them have different output types;
648 * differentTypes[k] is set true if column k has different type in any
652 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
653 bool *differentTypes)
655 SetOperationStmt *topop;
658 if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
661 /* Are we at top level, or looking at a setop component? */
662 if (subquery == topquery)
664 /* Top level, so check any component queries */
665 if (subquery->setOperations != NULL)
666 if (!recurse_pushdown_safe(subquery->setOperations, topquery,
672 /* Setop component must not have more components (too weird) */
673 if (subquery->setOperations != NULL)
675 /* Check whether setop component output types match top level */
676 topop = (SetOperationStmt *) topquery->setOperations;
677 Assert(topop && IsA(topop, SetOperationStmt));
678 compare_tlist_datatypes(subquery->targetList,
686 * Helper routine to recurse through setOperations tree
689 recurse_pushdown_safe(Node *setOp, Query *topquery,
690 bool *differentTypes)
692 if (IsA(setOp, RangeTblRef))
694 RangeTblRef *rtr = (RangeTblRef *) setOp;
695 RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
696 Query *subquery = rte->subquery;
698 Assert(subquery != NULL);
699 return subquery_is_pushdown_safe(subquery, topquery, differentTypes);
701 else if (IsA(setOp, SetOperationStmt))
703 SetOperationStmt *op = (SetOperationStmt *) setOp;
705 /* EXCEPT is no good */
706 if (op->op == SETOP_EXCEPT)
709 if (!recurse_pushdown_safe(op->larg, topquery, differentTypes))
711 if (!recurse_pushdown_safe(op->rarg, topquery, differentTypes))
716 elog(ERROR, "unrecognized node type: %d",
717 (int) nodeTag(setOp));
723 * Compare tlist's datatypes against the list of set-operation result types.
724 * For any items that are different, mark the appropriate element of
725 * differentTypes[] to show that this column will have type conversions.
728 compare_tlist_datatypes(List *tlist, List *colTypes,
729 bool *differentTypes)
732 ListCell *colType = list_head(colTypes);
736 TargetEntry *tle = (TargetEntry *) lfirst(l);
739 continue; /* ignore resjunk columns */
741 elog(ERROR, "wrong number of tlist entries");
742 if (exprType((Node *) tle->expr) != lfirst_oid(colType))
743 differentTypes[tle->resno] = true;
744 colType = lnext(colType);
747 elog(ERROR, "wrong number of tlist entries");
751 * qual_is_pushdown_safe - is a particular qual safe to push down?
753 * qual is a restriction clause applying to the given subquery (whose RTE
754 * has index rti in the parent query).
756 * Conditions checked here:
758 * 1. The qual must not contain any subselects (mainly because I'm not sure
759 * it will work correctly: sublinks will already have been transformed into
760 * subplans in the qual, but not in the subquery).
762 * 2. The qual must not refer to any subquery output columns that were
763 * found to have inconsistent types across a set operation tree by
764 * subquery_is_pushdown_safe().
766 * 3. If the subquery uses DISTINCT ON, we must not push down any quals that
767 * refer to non-DISTINCT output columns, because that could change the set
768 * of rows returned. This condition is vacuous for DISTINCT, because then
769 * there are no non-DISTINCT output columns, but unfortunately it's fairly
770 * expensive to tell the difference between DISTINCT and DISTINCT ON in the
771 * parsetree representation. It's cheaper to just make sure all the Vars
772 * in the qual refer to DISTINCT columns.
774 * 4. We must not push down any quals that refer to subselect outputs that
775 * return sets, else we'd introduce functions-returning-sets into the
776 * subquery's WHERE/HAVING quals.
779 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
780 bool *differentTypes)
785 Bitmapset *tested = NULL;
787 /* Refuse subselects (point 1) */
788 if (contain_subplans(qual))
792 * Examine all Vars used in clause; since it's a restriction clause,
793 * all such Vars must refer to subselect output columns.
795 vars = pull_var_clause(qual, false);
798 Var *var = (Var *) lfirst(vl);
801 Assert(var->varno == rti);
804 * We use a bitmapset to avoid testing the same attno more than
805 * once. (NB: this only works because subquery outputs can't have
808 if (bms_is_member(var->varattno, tested))
810 tested = bms_add_member(tested, var->varattno);
813 if (differentTypes[var->varattno])
819 /* Must find the tlist element referenced by the Var */
820 tle = get_tle_by_resno(subquery->targetList, var->varattno);
822 Assert(!tle->resjunk);
824 /* If subquery uses DISTINCT or DISTINCT ON, check point 3 */
825 if (subquery->distinctClause != NIL &&
826 !targetIsInSortList(tle, subquery->distinctClause))
828 /* non-DISTINCT column, so fail */
833 /* Refuse functions returning sets (point 4) */
834 if (expression_returns_set((Node *) tle->expr))
848 * subquery_push_qual - push down a qual that we have determined is safe
851 subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
853 if (subquery->setOperations != NULL)
855 /* Recurse to push it separately to each component query */
856 recurse_push_qual(subquery->setOperations, subquery,
862 * We need to replace Vars in the qual (which must refer to
863 * outputs of the subquery) with copies of the subquery's
864 * targetlist expressions. Note that at this point, any uplevel
865 * Vars in the qual should have been replaced with Params, so they
868 * This step also ensures that when we are pushing into a setop tree,
869 * each component query gets its own copy of the qual.
871 qual = ResolveNew(qual, rti, 0, rte,
872 subquery->targetList,
876 * Now attach the qual to the proper place: normally WHERE, but
877 * if the subquery uses grouping or aggregation, put it in HAVING
878 * (since the qual really refers to the group-result rows).
880 if (subquery->hasAggs || subquery->groupClause || subquery->havingQual)
881 subquery->havingQual = make_and_qual(subquery->havingQual, qual);
883 subquery->jointree->quals =
884 make_and_qual(subquery->jointree->quals, qual);
887 * We need not change the subquery's hasAggs or hasSublinks flags,
888 * since we can't be pushing down any aggregates that weren't
889 * there before, and we don't push down subselects at all.
895 * Helper routine to recurse through setOperations tree
898 recurse_push_qual(Node *setOp, Query *topquery,
899 RangeTblEntry *rte, Index rti, Node *qual)
901 if (IsA(setOp, RangeTblRef))
903 RangeTblRef *rtr = (RangeTblRef *) setOp;
904 RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
905 Query *subquery = subrte->subquery;
907 Assert(subquery != NULL);
908 subquery_push_qual(subquery, rte, rti, qual);
910 else if (IsA(setOp, SetOperationStmt))
912 SetOperationStmt *op = (SetOperationStmt *) setOp;
914 recurse_push_qual(op->larg, topquery, rte, rti, qual);
915 recurse_push_qual(op->rarg, topquery, rte, rti, qual);
919 elog(ERROR, "unrecognized node type: %d",
920 (int) nodeTag(setOp));
924 /*****************************************************************************
926 *****************************************************************************/
928 #ifdef OPTIMIZER_DEBUG
931 print_relids(Relids relids)
937 tmprelids = bms_copy(relids);
938 while ((x = bms_first_member(tmprelids)) >= 0)
949 print_restrictclauses(PlannerInfo *root, List *clauses)
955 RestrictInfo *c = lfirst(l);
957 print_expr((Node *) c->clause, root->parse->rtable);
964 print_path(PlannerInfo *root, Path *path, int indent)
968 Path *subpath = NULL;
971 switch (nodeTag(path))
979 case T_BitmapHeapPath:
980 ptype = "BitmapHeapScan";
982 case T_BitmapAndPath:
983 ptype = "BitmapAndPath";
986 ptype = "BitmapOrPath";
996 subpath = ((ResultPath *) path)->subpath;
1000 subpath = ((MaterialPath *) path)->subpath;
1004 subpath = ((UniquePath *) path)->subpath;
1011 ptype = "MergeJoin";
1023 for (i = 0; i < indent; i++)
1025 printf("%s", ptype);
1030 print_relids(path->parent->relids);
1031 printf(") rows=%.0f", path->parent->rows);
1033 printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
1037 for (i = 0; i < indent; i++)
1039 printf(" pathkeys: ");
1040 print_pathkeys(path->pathkeys, root->parse->rtable);
1045 JoinPath *jp = (JoinPath *) path;
1047 for (i = 0; i < indent; i++)
1049 printf(" clauses: ");
1050 print_restrictclauses(root, jp->joinrestrictinfo);
1053 if (IsA(path, MergePath))
1055 MergePath *mp = (MergePath *) path;
1057 if (mp->outersortkeys || mp->innersortkeys)
1059 for (i = 0; i < indent; i++)
1061 printf(" sortouter=%d sortinner=%d\n",
1062 ((mp->outersortkeys) ? 1 : 0),
1063 ((mp->innersortkeys) ? 1 : 0));
1067 print_path(root, jp->outerjoinpath, indent + 1);
1068 print_path(root, jp->innerjoinpath, indent + 1);
1072 print_path(root, subpath, indent + 1);
1076 debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
1080 printf("RELOPTINFO (");
1081 print_relids(rel->relids);
1082 printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
1084 if (rel->baserestrictinfo)
1086 printf("\tbaserestrictinfo: ");
1087 print_restrictclauses(root, rel->baserestrictinfo);
1093 printf("\tjoininfo: ");
1094 print_restrictclauses(root, rel->joininfo);
1098 printf("\tpath list:\n");
1099 foreach(l, rel->pathlist)
1100 print_path(root, lfirst(l), 1);
1101 printf("\n\tcheapest startup path:\n");
1102 print_path(root, rel->cheapest_startup_path, 1);
1103 printf("\n\tcheapest total path:\n");
1104 print_path(root, rel->cheapest_total_path, 1);
1109 #endif /* OPTIMIZER_DEBUG */