Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit78a055a

Browse files
committed
Remove some recursion in optimizer and clean up some code there.
1 parent0808e65 commit78a055a

File tree

4 files changed

+128
-121
lines changed

4 files changed

+128
-121
lines changed

‎src/backend/optimizer/path/allpaths.c

Lines changed: 81 additions & 87 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/allpaths.c,v 1.13 1997/09/08 21:44:44 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.14 1997/12/21 05:18:18 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -44,7 +44,7 @@ int32_use_geqo_rels_ = GEQO_RELS;
4444

4545

4646
staticvoidfind_rel_paths(Query*root,List*rels);
47-
staticList*find_join_paths(Query*root,List*outer_rels,intlevels_left);
47+
staticList*find_join_paths(Query*root,List*outer_rels,intlevels_needed);
4848

4949
/*
5050
* find-paths--
@@ -56,28 +56,28 @@ static List *find_join_paths(Query *root, List *outer_rels, int levels_left);
5656
List*
5757
find_paths(Query*root,List*rels)
5858
{
59-
intlevels_left;
59+
intlevels_needed;
6060

6161
/*
6262
* Set the number of join (not nesting) levels yet to be processed.
6363
*/
64-
levels_left=length(rels);
64+
levels_needed=length(rels);
6565

66-
if (levels_left <=0)
66+
if (levels_needed <=0)
6767
returnNIL;
6868

6969
/*
7070
* Find the base relation paths.
7171
*/
7272
find_rel_paths(root,rels);
7373

74-
if (levels_left <=1)
74+
if (levels_needed <=1)
7575
{
7676

7777
/*
7878
* Unsorted single relation, no more processing is required.
7979
*/
80-
return(rels);
80+
returnrels;
8181
}
8282
else
8383
{
@@ -88,7 +88,7 @@ find_paths(Query *root, List *rels)
8888
*/
8989
set_rest_relselec(root,rels);
9090

91-
return(find_join_paths(root,rels,levels_left-1));
91+
returnfind_join_paths(root,rels,levels_needed);
9292
}
9393
}
9494

@@ -165,17 +165,16 @@ find_rel_paths(Query *root, List *rels)
165165
* 'outer-rels' is the current list of relations for which join paths
166166
*are to be found, i.e., he current list of relations that
167167
*have already been derived.
168-
* 'levels-left' is the current join level being processed, where '1' is
169-
*the "last" level
168+
* 'levels-needed' is the number of iterations needed
170169
*
171170
* Returns the final level of join relations, i.e., the relation that is
172-
* the result of joining all the original relationstogehter.
171+
* the result of joining all the original relationstogether.
173172
*/
174173
staticList*
175-
find_join_paths(Query*root,List*outer_rels,intlevels_left)
174+
find_join_paths(Query*root,List*outer_rels,intlevels_needed)
176175
{
177176
List*x;
178-
List*new_rels;
177+
List*new_rels=NIL;
179178
Rel*rel;
180179

181180
/*******************************************
@@ -190,89 +189,84 @@ find_join_paths(Query *root, List *outer_rels, int levels_left)
190189
* rest will be deprecated in case of GEQO *
191190
*******************************************/
192191

193-
/*
194-
* Determine all possible pairs of relations to be joined at this
195-
* level. Determine paths for joining these relation pairs and modify
196-
* 'new-rels' accordingly, then eliminate redundant join relations.
197-
*/
198-
new_rels=find_join_rels(root,outer_rels);
199-
200-
find_all_join_paths(root,new_rels);
201-
202-
new_rels=prune_joinrels(new_rels);
203-
192+
while (--levels_needed)
193+
{
194+
/*
195+
* Determine all possible pairs of relations to be joined at this
196+
* level. Determine paths for joining these relation pairs and modify
197+
* 'new-rels' accordingly, then eliminate redundant join relations.
198+
*/
199+
new_rels=find_join_rels(root,outer_rels);
200+
201+
find_all_join_paths(root,new_rels);
202+
203+
prune_joinrels(new_rels);
204+
204205
#if0
205-
206-
/*
207-
* * for each expensive predicate in each path in each distinct rel, *
208-
* consider doing pullup -- JMH
209-
*/
210-
if (XfuncMode!=XFUNC_NOPULL&&XfuncMode!=XFUNC_OFF)
211-
foreach(x,new_rels)
212-
xfunc_trypullup((Rel*)lfirst(x));
213-
#endif
214-
215-
prune_rel_paths(new_rels);
216-
217-
if (BushyPlanFlag)
218-
{
219-
206+
220207
/*
221-
* In case of bushy trees if there is still a join between a join
222-
* relation and another relation, add a new joininfo that involves
223-
* the join relation to the joininfo list of the other relation
208+
* * for each expensive predicate in each path in each distinct rel, *
209+
* consider doing pullup -- JMH
224210
*/
225-
add_new_joininfos(root,new_rels,outer_rels);
226-
}
227-
228-
foreach(x,new_rels)
229-
{
230-
rel= (Rel*)lfirst(x);
231-
if (rel->size <=0)
232-
rel->size=compute_rel_size(rel);
233-
rel->width=compute_rel_width(rel);
234-
235-
/*#define OPTIMIZER_DEBUG*/
211+
if (XfuncMode!=XFUNC_NOPULL&&XfuncMode!=XFUNC_OFF)
212+
foreach(x,new_rels)
213+
xfunc_trypullup((Rel*)lfirst(x));
214+
#endif
215+
216+
prune_rel_paths(new_rels);
217+
218+
if (BushyPlanFlag)
219+
{
220+
221+
/*
222+
* In case of bushy trees if there is still a join between a join
223+
* relation and another relation, add a new joininfo that involves
224+
* the join relation to the joininfo list of the other relation
225+
*/
226+
add_new_joininfos(root,new_rels,outer_rels);
227+
}
228+
229+
foreach(x,new_rels)
230+
{
231+
rel= (Rel*)lfirst(x);
232+
if (rel->size <=0)
233+
rel->size=compute_rel_size(rel);
234+
rel->width=compute_rel_width(rel);
235+
236+
/*#define OPTIMIZER_DEBUG*/
236237
#ifdefOPTIMIZER_DEBUG
237-
printf("levels left: %d\n",levels_left);
238-
debug_print_rel(root,rel);
238+
printf("levels left: %d\n",levels_left);
239+
debug_print_rel(root,rel);
239240
#endif
240-
}
241-
242-
if (BushyPlanFlag)
243-
{
244-
245-
/*
246-
* prune rels that have been completely incorporated into new join
247-
* rels
248-
*/
249-
outer_rels=prune_oldrels(outer_rels);
250-
251-
/*
252-
* merge join rels if then contain the same list of base rels
253-
*/
254-
outer_rels=merge_joinrels(new_rels,outer_rels);
255-
root->join_relation_list_=outer_rels;
256-
}
257-
else
258-
{
259-
root->join_relation_list_=new_rels;
260-
}
261-
262-
if (levels_left==1)
263-
{
241+
}
242+
264243
if (BushyPlanFlag)
265-
return (final_join_rels(outer_rels));
244+
{
245+
246+
/*
247+
* prune rels that have been completely incorporated into new join
248+
* rels
249+
*/
250+
outer_rels=prune_oldrels(outer_rels);
251+
252+
/*
253+
* merge join rels if then contain the same list of base rels
254+
*/
255+
outer_rels=merge_joinrels(new_rels,outer_rels);
256+
root->join_relation_list_=outer_rels;
257+
}
266258
else
267-
return (new_rels);
259+
{
260+
root->join_relation_list_=new_rels;
261+
}
262+
if (!BushyPlanFlag)
263+
outer_rels=new_rels;
268264
}
265+
266+
if (BushyPlanFlag)
267+
returnfinal_join_rels(outer_rels);
269268
else
270-
{
271-
if (BushyPlanFlag)
272-
return (find_join_paths(root,outer_rels,levels_left-1));
273-
else
274-
return (find_join_paths(root,new_rels,levels_left-1));
275-
}
269+
returnnew_rels;
276270
}
277271

278272
/*****************************************************************************

‎src/backend/optimizer/path/prune.c

Lines changed: 37 additions & 28 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/prune.c,v 1.6 1997/09/08 21:45:08 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/Attic/prune.c,v 1.7 1997/12/21 05:18:21 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -29,23 +29,21 @@ static List *prune_joinrel(Rel *rel, List *other_rels);
2929
/*
3030
* prune-joinrels--
3131
* Removes any redundant relation entries from a list of rel nodes
32-
* 'rel-list'.
32+
* 'rel-list'. Obviosly, the first relation can't be a duplicate.
3333
*
3434
* Returns the resulting list.
3535
*
3636
*/
37-
List*
37+
void
3838
prune_joinrels(List*rel_list)
3939
{
40-
List*temp_list=NIL;
40+
List*i;
4141

42-
if (rel_list!=NIL)
43-
{
44-
temp_list=lcons(lfirst(rel_list),
45-
prune_joinrels(prune_joinrel((Rel*)lfirst(rel_list),
46-
lnext(rel_list))));
47-
}
48-
return (temp_list);
42+
/*
43+
*rel_list can shorten while running as duplicate relations are deleted
44+
*/
45+
foreach(i,rel_list)
46+
lnext(i)=prune_joinrel((Rel*)lfirst(i),lnext(i));
4947
}
5048

5149
/*
@@ -62,28 +60,39 @@ prune_joinrels(List *rel_list)
6260
staticList*
6361
prune_joinrel(Rel*rel,List*other_rels)
6462
{
65-
List*i=NIL;
66-
List*t_list=NIL;
67-
List*temp_node=NIL;
68-
Rel*other_rel= (Rel*)NULL;
63+
List*cur=NIL;
64+
List*return_list=NIL;
6965

70-
foreach(i,other_rels)
66+
/* find first relation that doesn't match */
67+
foreach(cur,other_rels)
7168
{
72-
other_rel= (Rel*)lfirst(i);
73-
if (same(rel->relids,other_rel->relids))
74-
{
75-
rel->pathlist=add_pathlist(rel,
76-
rel->pathlist,
77-
other_rel->pathlist);
78-
t_list=nconc(t_list,NIL);/* XXX is this right ? */
79-
}
80-
else
69+
Rel*other_rel= (Rel*)lfirst(cur);
70+
71+
if (!same(rel->relids,other_rel->relids))
72+
break;
73+
}
74+
75+
/* we now know cur doesn't match, or is NIL */
76+
return_list=cur;
77+
78+
/* remove relations that do match, we use lnext so we can remove easily */
79+
if (cur!=NIL)
80+
{
81+
while (lnext(cur)!=NIL)
8182
{
82-
temp_node=lcons(other_rel,NIL);
83-
t_list=nconc(t_list,temp_node);
83+
Rel*other_rel= (Rel*)lfirst(lnext(cur));
84+
85+
if (same(rel->relids,other_rel->relids))
86+
{
87+
rel->pathlist=add_pathlist(rel,
88+
rel->pathlist,
89+
other_rel->pathlist);
90+
lnext(cur)=lnext(lnext(cur));/* delete it */
91+
}
92+
cur=lnext(cur);
8493
}
8594
}
86-
return(t_list);
95+
returnreturn_list;
8796
}
8897

8998
/*

‎src/backend/optimizer/prep/prepunion.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.11 1997/12/20 07:59:33 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.12 1997/12/21 05:18:28 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -236,9 +236,13 @@ plan_union_query(List *relids,
236236
new_root->uniqueFlag=NULL;
237237
new_root->sortClause=NULL;
238238
new_root->groupClause=NULL;
239-
new_root->qry_numAgg=0;
240-
new_root->qry_aggs=NULL;
241-
del_agg_tlist_references(new_root->targetList);
239+
if (new_root->qry_numAgg!=0)
240+
{
241+
new_root->qry_numAgg=0;
242+
pfree(new_root->qry_aggs);
243+
new_root->qry_aggs=NULL;
244+
del_agg_tlist_references(new_root->targetList);
245+
}
242246
fix_parsetree_attnums(rt_index,
243247
rt_entry->relid,
244248
relid,

‎src/include/optimizer/paths.h

Lines changed: 2 additions & 2 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.5 1997/11/26 01:13:47 momjian Exp $
10+
* $Id: paths.h,v 1.6 1997/12/21 05:18:48 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -92,7 +92,7 @@ extern List *final_join_rels(List *join_rel_list);
9292
/*
9393
* prototypes for path/prune.c
9494
*/
95-
externList*prune_joinrels(List*rel_list);
95+
externvoidprune_joinrels(List*rel_list);
9696
externvoidprune_rel_paths(List*rel_list);
9797
externPath*prune_rel_path(Rel*rel,Path*unorderedpath);
9898
externList*merge_joinrels(List*rel_list1,List*rel_list2);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp