Skip to content

Commit ba2ea6e

Browse files
committed
Fix GEQO optimizer to work correctly with new outer-join-capable
query representation. Note that GEQO_RELS setting is now interpreted as the number of top-level items in the FROM list, not necessarily the number of relations in the query. This seems appropriate since we are only doing join-path searching over the top-level items.
1 parent 457ac03 commit ba2ea6e

File tree

6 files changed

+57
-48
lines changed

6 files changed

+57
-48
lines changed

src/backend/optimizer/geqo/geqo_eval.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: geqo_eval.c,v 1.54 2000/09/12 21:06:50 tgl Exp $
9+
* $Id: geqo_eval.c,v 1.55 2000/09/19 18:42:33 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -36,7 +36,7 @@
3636
* Returns cost of a query tree as an individual of the population.
3737
*/
3838
Cost
39-
geqo_eval(Query *root, Gene *tour, int num_gene)
39+
geqo_eval(Query *root, List *initial_rels, Gene *tour, int num_gene)
4040
{
4141
MemoryContext mycontext;
4242
MemoryContext oldcxt;
@@ -64,7 +64,7 @@ geqo_eval(Query *root, Gene *tour, int num_gene)
6464
savelist = root->join_rel_list;
6565

6666
/* construct the best path for the given combination of relations */
67-
joinrel = gimme_tree(root, tour, 0, num_gene, NULL);
67+
joinrel = gimme_tree(root, initial_rels, tour, num_gene, 0, NULL);
6868

6969
/*
7070
* compute fitness
@@ -86,35 +86,42 @@ geqo_eval(Query *root, Gene *tour, int num_gene)
8686

8787
/*
8888
* gimme_tree
89-
* this program presumes that only LEFT-SIDED TREES are considered!
89+
* this routine considers only LEFT-SIDED TREES!
9090
*
91-
* 'old_rel' is the preceding join
91+
* 'root' is the Query
92+
* 'initial_rels' is the list of initial relations (FROM-list items)
93+
* 'tour' is the proposed join order, of length 'num_gene'
94+
* 'rel_count' is number of initial_rels items already joined (initially 0)
95+
* 'old_rel' is the preceding join (initially NULL)
9296
*
9397
* Returns a new join relation incorporating all joins in a left-sided tree.
9498
*/
9599
RelOptInfo *
96-
gimme_tree(Query *root, Gene *tour, int rel_count, int num_gene,
97-
RelOptInfo *old_rel)
100+
gimme_tree(Query *root, List *initial_rels,
101+
Gene *tour, int num_gene,
102+
int rel_count, RelOptInfo *old_rel)
98103
{
99104
RelOptInfo *inner_rel; /* current relation */
100-
int base_rel_index;
105+
int init_rel_index;
101106

102107
if (rel_count < num_gene)
103-
{ /* tree not yet finished */
108+
{
109+
/* tree not yet finished */
110+
init_rel_index = (int) tour[rel_count];
104111

105-
/* tour[0] = 3; tour[1] = 1; tour[2] = 2 */
106-
base_rel_index = (int) tour[rel_count];
107-
108-
inner_rel = (RelOptInfo *) nth(base_rel_index - 1, root->base_rel_list);
112+
inner_rel = (RelOptInfo *) nth(init_rel_index - 1, initial_rels);
109113

110114
if (rel_count == 0)
111-
{ /* processing first join with
112-
* base_rel_index = (int) tour[0] */
115+
{
116+
/* processing first join with init_rel_index = (int) tour[0] */
113117
rel_count++;
114-
return gimme_tree(root, tour, rel_count, num_gene, inner_rel);
118+
return gimme_tree(root, initial_rels,
119+
tour, num_gene,
120+
rel_count, inner_rel);
115121
}
116122
else
117-
{ /* tree main part */
123+
{
124+
/* tree main part */
118125
List *acceptable_rels = lcons(inner_rel, NIL);
119126
List *new_rels;
120127
RelOptInfo *new_rel;
@@ -133,13 +140,14 @@ gimme_tree(Query *root, Gene *tour, int rel_count, int num_gene,
133140
}
134141
new_rel = (RelOptInfo *) lfirst(new_rels);
135142

136-
rel_count++;
137-
Assert(length(new_rel->relids) == rel_count);
138-
139143
/* Find and save the cheapest paths for this rel */
140144
set_cheapest(new_rel);
141145

142-
return gimme_tree(root, tour, rel_count, num_gene, new_rel);
146+
/* and recurse... */
147+
rel_count++;
148+
return gimme_tree(root, initial_rels,
149+
tour, num_gene,
150+
rel_count, new_rel);
143151
}
144152
}
145153

src/backend/optimizer/geqo/geqo_main.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: geqo_main.c,v 1.23 2000/08/07 00:51:23 tgl Exp $
10+
* $Id: geqo_main.c,v 1.24 2000/09/19 18:42:33 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -65,13 +65,12 @@ static int gimme_number_generations(int pool_size, int effort);
6565
*/
6666

6767
RelOptInfo *
68-
geqo(Query *root)
68+
geqo(Query *root, int number_of_rels, List *initial_rels)
6969
{
7070
int generation;
7171
Chromosome *momma;
7272
Chromosome *daddy;
7373
Chromosome *kid;
74-
int number_of_rels;
7574
Pool *pool;
7675
int pool_size,
7776
number_generations,
@@ -95,9 +94,6 @@ geqo(Query *root)
9594

9695
#endif
9796

98-
/* set tour size */
99-
number_of_rels = length(root->base_rel_list);
100-
10197
/* set GA parameters */
10298
pool_size = gimme_pool_size(number_of_rels);
10399
number_generations = gimme_number_generations(pool_size, Geqo_effort);
@@ -114,7 +110,7 @@ geqo(Query *root)
114110
pool = alloc_pool(pool_size, number_of_rels);
115111

116112
/* random initialization of the pool */
117-
random_init_pool(root, pool, 0, pool->size);
113+
random_init_pool(root, initial_rels, pool, 0, pool->size);
118114

119115
/* sort the pool according to cheapest path as fitness */
120116
sort_pool(pool); /* we have to do it only one time, since
@@ -204,7 +200,8 @@ geqo(Query *root)
204200

205201

206202
/* EVALUATE FITNESS */
207-
kid->worth = geqo_eval(root, kid->string, pool->string_length);
203+
kid->worth = geqo_eval(root, initial_rels,
204+
kid->string, pool->string_length);
208205

209206
/* push the kid into the wilderness of life according to its worth */
210207
spread_chromo(kid, pool);
@@ -247,9 +244,10 @@ geqo(Query *root)
247244

248245
best_tour = (Gene *) pool->data[0].string;
249246

250-
/* root->join_relation_list_ will be modified during this ! */
251-
best_rel = (RelOptInfo *) gimme_tree(root, best_tour, 0,
252-
pool->string_length, NULL);
247+
/* root->join_rel_list will be modified during this ! */
248+
best_rel = (RelOptInfo *) gimme_tree(root, initial_rels,
249+
best_tour, pool->string_length,
250+
0, NULL);
253251

254252
/* DBG: show the query plan
255253
print_plan(best_plan, root);

src/backend/optimizer/geqo/geqo_pool.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: geqo_pool.c,v 1.17 2000/01/26 05:56:33 momjian Exp $
9+
* $Id: geqo_pool.c,v 1.18 2000/09/19 18:42:33 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -84,18 +84,18 @@ free_pool(Pool *pool)
8484
* initialize genetic pool
8585
*/
8686
void
87-
random_init_pool(Query *root, Pool *pool, int strt, int stp)
87+
random_init_pool(Query *root, List *initial_rels,
88+
Pool *pool, int strt, int stp)
8889
{
8990
Chromosome *chromo = (Chromosome *) pool->data;
9091
int i;
9192

9293
for (i = strt; i < stp; i++)
9394
{
94-
init_tour(chromo[i].string, pool->string_length); /* from
95-
* "geqo_recombination.c"
96-
* */
97-
98-
pool->data[i].worth = geqo_eval(root, chromo[i].string, pool->string_length); /* "from geqo_eval.c" */
95+
init_tour(chromo[i].string, pool->string_length);
96+
pool->data[i].worth = geqo_eval(root, initial_rels,
97+
chromo[i].string,
98+
pool->string_length);
9999
}
100100
}
101101

src/backend/optimizer/path/allpaths.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.63 2000/09/12 21:06:52 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.64 2000/09/19 18:42:34 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -188,7 +188,7 @@ make_one_rel_by_joins(Query *root, int levels_needed, List *initial_rels)
188188
* rest will be skipped in case of GEQO *
189189
*******************************************/
190190
if (enable_geqo && levels_needed >= geqo_rels)
191-
return geqo(root);
191+
return geqo(root, levels_needed, initial_rels);
192192

193193
/*
194194
* We employ a simple "dynamic programming" algorithm: we first find

src/include/optimizer/geqo.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: geqo.h,v 1.20 2000/06/28 03:33:22 tgl Exp $
9+
* $Id: geqo.h,v 1.21 2000/09/19 18:42:32 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -62,11 +62,13 @@ extern int Geqo_random_seed; /* or negative to use current time */
6262

6363

6464
/* routines in geqo_main.c */
65-
extern RelOptInfo *geqo(Query *root);
65+
extern RelOptInfo *geqo(Query *root, int number_of_rels, List *initial_rels);
6666

6767
/* routines in geqo_eval.c */
68-
extern Cost geqo_eval(Query *root, Gene *tour, int num_gene);
69-
extern RelOptInfo *gimme_tree(Query *root, Gene *tour, int rel_count,
70-
int num_gene, RelOptInfo *old_rel);
68+
extern Cost geqo_eval(Query *root, List *initial_rels,
69+
Gene *tour, int num_gene);
70+
extern RelOptInfo *gimme_tree(Query *root, List *initial_rels,
71+
Gene *tour, int num_gene,
72+
int rel_count, RelOptInfo *old_rel);
7173

7274
#endif /* GEQO_H */

src/include/optimizer/geqo_pool.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: geqo_pool.h,v 1.9 2000/01/26 05:58:20 momjian Exp $
9+
* $Id: geqo_pool.h,v 1.10 2000/09/19 18:42:32 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -29,7 +29,8 @@
2929
extern Pool *alloc_pool(int pool_size, int string_length);
3030
extern void free_pool(Pool *pool);
3131

32-
extern void random_init_pool(Query *root, Pool *pool, int strt, int stop);
32+
extern void random_init_pool(Query *root, List *initial_rels,
33+
Pool *pool, int strt, int stop);
3334
extern Chromosome *alloc_chromo(int string_length);
3435
extern void free_chromo(Chromosome *chromo);
3536

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy