diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2005-06-10 03:32:25 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2005-06-10 03:32:25 +0000 |
commit | a87ee007edfa3d71e31cea4adad396888adbcd5a (patch) | |
tree | c2eeadb582711700d0de21d01ab3a994f8bfb716 /src/backend/optimizer/path/allpaths.c | |
parent | 453d74b99c9ba6e5e75d214b0d7bec13553ded89 (diff) | |
download | postgresql-a87ee007edfa3d71e31cea4adad396888adbcd5a.tar.gz postgresql-a87ee007edfa3d71e31cea4adad396888adbcd5a.zip |
Quick hack to allow the outer query's tuple_fraction to be passed down
to a subquery if the outer query is simple enough that the LIMIT can
be reflected directly to the subquery. This didn't use to be very
interesting, because a subquery that couldn't have been flattened into
the upper query was usually not going to be very responsive to
tuple_fraction anyway. But with new code that allows UNION ALL subqueries
to pay attention to tuple_fraction, this is useful to do. In particular
this lets the optimization occur when the UNION ALL is directly inside
a view.
Diffstat (limited to 'src/backend/optimizer/path/allpaths.c')
-rw-r--r-- | src/backend/optimizer/path/allpaths.c | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 4b09f03e6e7..25bd55dadf3 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.133 2005/06/09 04:18:59 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.134 2005/06/10 03:32:21 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -353,6 +353,28 @@ set_inherited_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, set_cheapest(rel); } +/* quick-and-dirty test to see if any joining is needed */ +static bool +has_multiple_baserels(PlannerInfo *root) +{ + int num_base_rels = 0; + Index rti; + + for (rti = 1; rti < root->base_rel_array_size; rti++) + { + RelOptInfo *brel = root->base_rel_array[rti]; + + if (brel == NULL) + continue; + + /* ignore RTEs that are "other rels" */ + if (brel->reloptkind == RELOPT_BASEREL) + if (++num_base_rels > 1) + return true; + } + return false; +} + /* * set_subquery_pathlist * Build the (single) access path for a subquery RTE @@ -361,8 +383,10 @@ static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte) { + Query *parse = root->parse; Query *subquery = rte->subquery; bool *differentTypes; + double tuple_fraction; List *pathkeys; List *subquery_pathkeys; @@ -416,8 +440,24 @@ set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel, pfree(differentTypes); + /* + * We can safely pass the outer tuple_fraction down to the subquery + * if the outer level has no joining, aggregation, or sorting to do. + * Otherwise we'd better tell the subquery to plan for full retrieval. + * (XXX This could probably be made more intelligent ...) + */ + if (parse->hasAggs || + parse->groupClause || + parse->havingQual || + parse->distinctClause || + parse->sortClause || + has_multiple_baserels(root)) + tuple_fraction = 0.0; /* default case */ + else + tuple_fraction = root->tuple_fraction; + /* Generate the plan for the subquery */ - rel->subplan = subquery_planner(subquery, 0.0 /* default case */, + rel->subplan = subquery_planner(subquery, tuple_fraction, &subquery_pathkeys); /* Copy number of output rows from subplan */ |