diff options
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/nodes/outfuncs.c | 5 | ||||
-rw-r--r-- | src/backend/optimizer/path/indxpath.c | 70 | ||||
-rw-r--r-- | src/backend/optimizer/path/joinpath.c | 39 |
3 files changed, 70 insertions, 44 deletions
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 841db47aebc..19a888bdb8c 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.261.2.1 2005/11/14 23:54:34 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.261.2.2 2007/05/22 01:40:52 tgl Exp $ * * NOTES * Every node type that can appear in stored rules' parsetrees *must* @@ -1262,7 +1262,8 @@ _outInnerIndexscanInfo(StringInfo str, InnerIndexscanInfo *node) WRITE_NODE_TYPE("INNERINDEXSCANINFO"); WRITE_BITMAPSET_FIELD(other_relids); WRITE_BOOL_FIELD(isouterjoin); - WRITE_NODE_FIELD(best_innerpath); + WRITE_NODE_FIELD(cheapest_startup_innerpath); + WRITE_NODE_FIELD(cheapest_total_innerpath); } static void diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c index 7ad89c16886..12ec16e2aec 100644 --- a/src/backend/optimizer/path/indxpath.c +++ b/src/backend/optimizer/path/indxpath.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.191.2.10 2007/04/17 20:03:16 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.191.2.11 2007/05/22 01:40:52 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1313,22 +1313,25 @@ matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel, Relids outer_relids) /* * best_inner_indexscan - * Finds the best available inner indexscan for a nestloop join + * Finds the best available inner indexscans for a nestloop join * with the given rel on the inside and the given outer_relids outside. - * May return NULL if there are no possible inner indexscans. - * - * We ignore ordering considerations (since a nestloop's inner scan's order - * is uninteresting). Also, we consider only total cost when deciding which - * of two possible paths is better --- this assumes that all indexpaths have - * negligible startup cost. (True today, but someday we might have to think - * harder.) Therefore, there is only one dimension of comparison and so it's - * sufficient to return a single "best" path. + * + * *cheapest_startup gets the path with least startup cost + * *cheapest_total gets the path with least total cost (often the same path) + * Both are set to NULL if there are no possible inner indexscans. + * + * We ignore ordering considerations, since a nestloop's inner scan's order + * is uninteresting. Hence startup cost and total cost are the only figures + * of merit to consider. + * + * Note: create_index_paths() must have been run previously for this rel, + * else the results will always be NULL. */ -Path * +void best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel, - Relids outer_relids, JoinType jointype) + Relids outer_relids, JoinType jointype, + Path **cheapest_startup, Path **cheapest_total) { - Path *cheapest; bool isouterjoin; List *clause_list; List *indexpaths; @@ -1337,6 +1340,9 @@ best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel, InnerIndexscanInfo *info; MemoryContext oldcontext; + /* Initialize results for failure returns */ + *cheapest_startup = *cheapest_total = NULL; + /* * Nestloop only supports inner, left, and IN joins. */ @@ -1351,14 +1357,14 @@ best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel, isouterjoin = true; break; default: - return NULL; + return; } /* * If there are no indexable joinclauses for this rel, exit quickly. */ if (bms_is_empty(rel->index_outer_relids)) - return NULL; + return; /* * Otherwise, we have to do path selection in the memory context of the @@ -1378,14 +1384,14 @@ best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel, { bms_free(outer_relids); MemoryContextSwitchTo(oldcontext); - return NULL; + return; } /* * Look to see if we already computed the result for this set of relevant * outerrels. (We include the isouterjoin status in the cache lookup key * for safety. In practice I suspect this is not necessary because it - * should always be the same for a given innerrel.) + * should always be the same for a given combination of rels.) */ foreach(l, rel->index_inner_paths) { @@ -1395,7 +1401,9 @@ best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel, { bms_free(outer_relids); MemoryContextSwitchTo(oldcontext); - return info->best_innerpath; + *cheapest_startup = info->cheapest_startup_innerpath; + *cheapest_total = info->cheapest_total_innerpath; + return; } } @@ -1450,28 +1458,32 @@ best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel, } /* - * Now choose the cheapest member of indexpaths. + * Now choose the cheapest members of indexpaths. */ - cheapest = NULL; - foreach(l, indexpaths) + if (indexpaths != NIL) { - Path *path = (Path *) lfirst(l); + *cheapest_startup = *cheapest_total = (Path *) linitial(indexpaths); + + for_each_cell(l, lnext(list_head(indexpaths))) + { + Path *path = (Path *) lfirst(l); - if (cheapest == NULL || - compare_path_costs(path, cheapest, TOTAL_COST) < 0) - cheapest = path; + if (compare_path_costs(path, *cheapest_startup, STARTUP_COST) < 0) + *cheapest_startup = path; + if (compare_path_costs(path, *cheapest_total, TOTAL_COST) < 0) + *cheapest_total = path; + } } - /* Cache the result --- whether positive or negative */ + /* Cache the results --- whether positive or negative */ info = makeNode(InnerIndexscanInfo); info->other_relids = outer_relids; info->isouterjoin = isouterjoin; - info->best_innerpath = cheapest; + info->cheapest_startup_innerpath = *cheapest_startup; + info->cheapest_total_innerpath = *cheapest_total; rel->index_inner_paths = lcons(info, rel->index_inner_paths); MemoryContextSwitchTo(oldcontext); - - return cheapest; } /* diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c index 215d03a9719..d94d46f52da 100644 --- a/src/backend/optimizer/path/joinpath.c +++ b/src/backend/optimizer/path/joinpath.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.97.2.1 2005/11/22 18:23:10 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.97.2.2 2007/05/22 01:40:53 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -295,10 +295,11 @@ sort_inner_and_outer(PlannerInfo *root, * only outer paths that are already ordered well enough for merging). * * We always generate a nestloop path for each available outer path. - * In fact we may generate as many as four: one on the cheapest-total-cost + * In fact we may generate as many as five: one on the cheapest-total-cost * inner path, one on the same with materialization, one on the - * cheapest-startup-cost inner path (if different), - * and one on the best inner-indexscan path (if any). + * cheapest-startup-cost inner path (if different), one on the + * cheapest-total inner-indexscan path (if any), and one on the + * cheapest-startup inner-indexscan path (if different). * * We also consider mergejoins if mergejoin clauses are available. We have * two ways to generate the inner path for a mergejoin: sort the cheapest @@ -334,7 +335,8 @@ match_unsorted_outer(PlannerInfo *root, Path *inner_cheapest_startup = innerrel->cheapest_startup_path; Path *inner_cheapest_total = innerrel->cheapest_total_path; Path *matpath = NULL; - Path *bestinnerjoin = NULL; + Path *index_cheapest_startup = NULL; + Path *index_cheapest_total = NULL; ListCell *l; /* @@ -392,11 +394,12 @@ match_unsorted_outer(PlannerInfo *root, create_material_path(innerrel, inner_cheapest_total); /* - * Get the best innerjoin indexpath (if any) for this outer rel. It's - * the same for all outer paths. + * Get the best innerjoin indexpaths (if any) for this outer rel. + * They're the same for all outer paths. */ - bestinnerjoin = best_inner_indexscan(root, innerrel, - outerrel->relids, jointype); + best_inner_indexscan(root, innerrel, outerrel->relids, jointype, + &index_cheapest_startup, + &index_cheapest_total); } foreach(l, outerrel->pathlist) @@ -437,8 +440,8 @@ match_unsorted_outer(PlannerInfo *root, * Always consider a nestloop join with this outer and * cheapest-total-cost inner. When appropriate, also consider * using the materialized form of the cheapest inner, the - * cheapest-startup-cost inner path, and the best innerjoin - * indexpath. + * cheapest-startup-cost inner path, and the cheapest innerjoin + * indexpaths. */ add_path(joinrel, (Path *) create_nestloop_path(root, @@ -466,13 +469,23 @@ match_unsorted_outer(PlannerInfo *root, inner_cheapest_startup, restrictlist, merge_pathkeys)); - if (bestinnerjoin != NULL) + if (index_cheapest_total != NULL) add_path(joinrel, (Path *) create_nestloop_path(root, joinrel, jointype, outerpath, - bestinnerjoin, + index_cheapest_total, + restrictlist, + merge_pathkeys)); + if (index_cheapest_startup != NULL && + index_cheapest_startup != index_cheapest_total) + add_path(joinrel, (Path *) + create_nestloop_path(root, + joinrel, + jointype, + outerpath, + index_cheapest_startup, restrictlist, merge_pathkeys)); } |