Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 612b843

Browse files
committed
optimizer cleanup
1 parent 8ab72a3 commit 612b843

File tree

4 files changed

+46
-55
lines changed

4 files changed

+46
-55
lines changed

src/backend/optimizer/README

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ The optimizer generates optimial query plans by doing several steps:
77
it. Find each way of accessing the relation, called a Path, including
88
sequential and index scans, and add it to RelOptInfo.pathlist. Also
99
create RelOptInfo.joininfo that lists all the joins that involve this
10-
relation.
10+
relation. For example, the WHERE clause "tab1.col1 = tab2.col1"
11+
generates a JoinInfo for tab1 listing tab2 as an unjoined relation, and
12+
tab2's joininfo shows tab1 as an unjoined relation.
1113

1214
2) Join each RelOptInfo to other RelOptInfo as specified in
1315
RelOptInfo.joininfo. At this point each RelOptInfo is a single

src/backend/optimizer/path/joinpath.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinpath.c,v 1.29 1999/02/18 19:58:52 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinpath.c,v 1.30 1999/02/19 05:18:04 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -79,8 +79,8 @@ update_rels_pathlist_for_joins(Query *root, List *joinrels)
7979
List *pathlist = NIL;
8080

8181
/* flatten out relids later in this function */
82-
innerrelids = lsecond(joinrel->relids);
8382
outerrelids = lfirst(joinrel->relids);
83+
innerrelids = lsecond(joinrel->relids);
8484

8585
/*
8686
* base relation id is an integer and join relation relid is a
@@ -123,7 +123,7 @@ update_rels_pathlist_for_joins(Query *root, List *joinrels)
123123
outerrel,
124124
innerrel,
125125
outerrel->pathlist,
126-
(Path *) innerrel->cheapestpath,
126+
innerrel->cheapestpath,
127127
bestinnerjoin,
128128
mergeinfo_list));
129129

@@ -176,7 +176,6 @@ best_innerjoin(List *join_paths, Relids outer_relids)
176176
&& ((cheapest == NULL ||
177177
path_is_cheaper((Path *) lfirst(join_path), cheapest))))
178178
{
179-
180179
cheapest = (Path *) lfirst(join_path);
181180
}
182181
}
@@ -293,7 +292,7 @@ match_unsorted_outer(RelOptInfo *joinrel,
293292
List *clauses = NIL;
294293
List *matchedJoinKeys = NIL;
295294
List *matchedJoinClauses = NIL;
296-
MergeInfo *xmergeinfo = (MergeInfo *) NULL;
295+
MergeInfo *xmergeinfo = NULL;
297296

298297
outerpath = (Path *) lfirst(i);
299298

@@ -309,9 +308,8 @@ match_unsorted_outer(RelOptInfo *joinrel,
309308
if (clauses)
310309
{
311310
List *jmkeys = xmergeinfo->jmethod.jmkeys;
312-
List *clauses = xmergeinfo->jmethod.clauses;
313311

314-
matchedJoinKeys = match_pathkeys_joinkeys(outerpath->pathkeys,
312+
matchedJoinKeys = order_joinkeys_by_pathkeys(outerpath->pathkeys,
315313
jmkeys,
316314
clauses,
317315
OUTER,
@@ -339,19 +337,18 @@ match_unsorted_outer(RelOptInfo *joinrel,
339337
{
340338
bool path_is_cheaper_than_sort;
341339
List *varkeys = NIL;
342-
Path *mergeinnerpath = match_paths_joinkeys(matchedJoinKeys,
343-
outerpath_ordering,
344-
innerrel->pathlist,
345-
INNER);
340+
Path *mergeinnerpath = get_cheapest_path_for_joinkeys(
341+
matchedJoinKeys,
342+
outerpath_ordering,
343+
innerrel->pathlist,
344+
INNER);
346345

347346
/* Should we use the mergeinner, or sort the cheapest inner? */
348347
path_is_cheaper_than_sort = (bool) (mergeinnerpath &&
349-
(mergeinnerpath->path_cost <
350-
(cheapest_inner->path_cost +
351-
cost_sort(matchedJoinKeys,
352-
innerrel->size,
353-
innerrel->width,
354-
false))));
348+
(mergeinnerpath->path_cost <
349+
(cheapest_inner->path_cost +
350+
cost_sort(matchedJoinKeys, innerrel->size,
351+
innerrel->width, false))));
355352
if (!path_is_cheaper_than_sort)
356353
{
357354
varkeys = extract_path_keys(matchedJoinKeys,
@@ -451,11 +448,10 @@ match_unsorted_inner(RelOptInfo *joinrel,
451448
if (clauses)
452449
{
453450
List *jmkeys = xmergeinfo->jmethod.jmkeys;
454-
List *cls = xmergeinfo->jmethod.clauses;
455451

456-
matchedJoinKeys = match_pathkeys_joinkeys(innerpath->pathkeys,
452+
matchedJoinKeys = order_joinkeys_by_pathkeys(innerpath->pathkeys,
457453
jmkeys,
458-
cls,
454+
clauses,
459455
INNER,
460456
&matchedJoinClauses);
461457
}

src/backend/optimizer/path/pathkey.c

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/Attic/pathkey.c,v 1.2 1999/02/19 02:05:15 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/Attic/pathkey.c,v 1.3 1999/02/19 05:18:05 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -28,7 +28,7 @@
2828

2929
static int match_pathkey_joinkeys(List *pathkey, List *joinkeys,
3030
int outer_or_inner);
31-
static bool every_func(List *joinkeys, List *pathkey,
31+
static bool joinkeys_pathkeys_match(List *joinkeys, List *pathkey,
3232
int outer_or_inner);
3333
static List *new_join_pathkey(List *subkeys, List *considered_subkeys,
3434
List *join_rel_tlist, List *joinclauses);
@@ -40,7 +40,7 @@ static List *new_matching_subkeys(Var *subkey, List *considered_subkeys,
4040
****************************************************************************/
4141

4242
/*
43-
* match_pathkeys_joinkeys
43+
* order_joinkeys_by_pathkeys
4444
* Attempts to match the keys of a path against the keys of join clauses.
4545
* This is done by looking for a matching join key in 'joinkeys' for
4646
* every path key in the list 'path.keys'. If there is a matching join key
@@ -69,7 +69,7 @@ static List *new_matching_subkeys(Var *subkey, List *considered_subkeys,
6969
* in matchedJoinClausesPtr. - ay 11/94
7070
*/
7171
List *
72-
match_pathkeys_joinkeys(List *pathkeys,
72+
order_joinkeys_by_pathkeys(List *pathkeys,
7373
List *joinkeys,
7474
List *joinclauses,
7575
int outer_or_inner,
@@ -92,21 +92,18 @@ match_pathkeys_joinkeys(List *pathkeys,
9292
List *xjoinkey = nth(matched_joinkey_index, joinkeys);
9393
List *joinclause = nth(matched_joinkey_index, joinclauses);
9494

95-
matched_joinkeys = lcons(xjoinkey, matched_joinkeys);
96-
matched_joinclauses = lcons(joinclause, matched_joinclauses);
97-
98-
joinkeys = LispRemove(xjoinkey, joinkeys);
95+
matched_joinkeys = lappend(matched_joinkeys, xjoinkey);
96+
matched_joinclauses = lappend(matched_joinclauses, joinclause);
9997
}
10098
else
99+
{
100+
*matchedJoinClausesPtr = NIL;
101101
return NIL;
102-
102+
}
103103
}
104-
if (matched_joinkeys == NULL ||
105-
length(matched_joinkeys) != length(pathkeys))
106-
return NIL;
107104

108-
*matchedJoinClausesPtr = nreverse(matched_joinclauses);
109-
return nreverse(matched_joinkeys);
105+
*matchedJoinClausesPtr = matched_joinclauses;
106+
return matched_joinkeys;
110107
}
111108

112109

@@ -144,7 +141,7 @@ match_pathkey_joinkeys(List *pathkey,
144141

145142

146143
/*
147-
* match_paths_joinkeys
144+
* get_cheapest_path_for_joinkeys
148145
* Attempts to find a path in 'paths' whose keys match a set of join
149146
* keys 'joinkeys'. To match,
150147
* 1. the path node ordering must equal 'ordering'.
@@ -165,31 +162,27 @@ match_pathkey_joinkeys(List *pathkey,
165162
* Find the cheapest path that matches the join keys
166163
*/
167164
Path *
168-
match_paths_joinkeys(List *joinkeys,
169-
PathOrder *ordering,
170-
List *paths,
171-
int outer_or_inner)
165+
get_cheapest_path_for_joinkeys(List *joinkeys,
166+
PathOrder *ordering,
167+
List *paths,
168+
int outer_or_inner)
172169
{
173170
Path *matched_path = NULL;
174-
bool key_match = false;
175171
List *i = NIL;
176172

177173
foreach(i, paths)
178174
{
179175
Path *path = (Path *) lfirst(i);
180-
int better_sort;
176+
int better_sort, better_key;
181177

182-
key_match = every_func(joinkeys, path->pathkeys, outer_or_inner);
183-
184-
if (pathorder_match(ordering, path->pathorder, &better_sort) &&
185-
better_sort == 0 &&
186-
length(joinkeys) == length(path->pathkeys) && key_match)
178+
if (joinkeys_pathkeys_match(joinkeys, path->pathkeys, outer_or_inner) &&
179+
length(joinkeys) == length(path->pathkeys) &&
180+
pathorder_match(ordering, path->pathorder, &better_sort) &&
181+
better_sort == 0)
187182
{
188183
if (matched_path)
189-
{
190184
if (path->path_cost < matched_path->path_cost)
191185
matched_path = path;
192-
}
193186
else
194187
matched_path = path;
195188
}
@@ -253,10 +246,10 @@ extract_path_keys(List *joinkeys,
253246

254247

255248
/*
256-
* every_func
249+
* joinkeys_pathkeys_match
257250
*/
258251
static bool
259-
every_func(List *joinkeys, List *pathkey, int outer_or_inner)
252+
joinkeys_pathkeys_match(List *joinkeys, List *pathkey, int outer_or_inner)
260253
{
261254
JoinKey *xjoinkey;
262255
Var *temp;

src/include/optimizer/paths.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: paths.h,v 1.23 1999/02/19 02:05:20 momjian Exp $
10+
* $Id: paths.h,v 1.24 1999/02/19 05:18:06 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -54,13 +54,13 @@ extern List *group_clauses_by_hashop(List *restrictinfo_list,
5454
* joinutils.h
5555
* generic join method key/clause routines
5656
*/
57-
extern List *match_pathkeys_joinkeys(List *pathkeys,
57+
extern List *order_joinkeys_by_pathkeys(List *pathkeys,
5858
List *joinkeys, List *joinclauses, int outer_or_inner,
5959
List **matchedJoinClausesPtr);
6060
extern List *extract_path_keys(List *joinkeys, List *tlist,
6161
int outer_or_inner);
62-
extern Path *match_paths_joinkeys(List *joinkeys, PathOrder *ordering,
63-
List *paths, int outer_or_inner);
62+
extern Path *get_cheapest_path_for_joinkeys(List *joinkeys,
63+
PathOrder *ordering, List *paths, int outer_or_inner);
6464
extern List *new_join_pathkeys(List *outer_pathkeys,
6565
List *join_rel_tlist, List *joinclauses);
6666

0 commit comments

Comments
 (0)