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

Commit1332c1e

Browse files
committed
Change GEQO optimizer to release memory after each gene
is evaluated. This bounds memory usage to something reasonable evenwhen many tables are being joined.
1 parentc686be8 commit1332c1e

File tree

4 files changed

+84
-49
lines changed

4 files changed

+84
-49
lines changed

‎src/backend/optimizer/geqo/geqo_eval.c

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: geqo_eval.c,v 1.36 1999/05/16 19:45:00 tgl Exp $
8+
* $Id: geqo_eval.c,v 1.37 1999/05/17 00:25:34 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -36,6 +36,7 @@
3636

3737
#include"utils/palloc.h"
3838
#include"utils/elog.h"
39+
#include"utils/portal.h"
3940

4041
#include"optimizer/internal.h"
4142
#include"optimizer/paths.h"
@@ -48,6 +49,38 @@
4849
#include"optimizer/geqo_gene.h"
4950
#include"optimizer/geqo.h"
5051

52+
/*
53+
* Variables set by geqo_eval_startup for use within a single GEQO run
54+
*/
55+
staticMemoryContextgeqo_eval_context;
56+
57+
/*
58+
* geqo_eval_startup:
59+
* Must be called during geqo_main startup (before geqo_eval may be called)
60+
*
61+
* The main thing we need to do here is prepare a private memory context for
62+
* allocation of temp storage used while constructing a path in geqo_eval().
63+
* Since geqo_eval() will be called many times, we can't afford to let all
64+
* that memory go unreclaimed until end of statement. We use a special
65+
* named portal to hold the context, so that it will be freed even if
66+
* we abort via elog(ERROR). The working data is allocated in the portal's
67+
* heap memory context.
68+
*/
69+
void
70+
geqo_eval_startup(void)
71+
{
72+
#defineGEQO_PORTAL_NAME"<geqo workspace>"
73+
Portalgeqo_portal=GetPortalByName(GEQO_PORTAL_NAME);
74+
75+
if (!PortalIsValid(geqo_portal)) {
76+
/* First time through (within current transaction, that is) */
77+
geqo_portal=CreatePortal(GEQO_PORTAL_NAME);
78+
Assert(PortalIsValid(geqo_portal));
79+
}
80+
81+
geqo_eval_context= (MemoryContext)PortalGetHeapMemory(geqo_portal);
82+
}
83+
5184
/*
5285
* geqo_eval
5386
*
@@ -56,23 +89,30 @@
5689
Cost
5790
geqo_eval(Query*root,Gene*tour,intnum_gene)
5891
{
59-
RelOptInfo*joinrel;
60-
Costfitness;
61-
List*temp;
92+
MemoryContextoldcxt;
93+
RelOptInfo*joinrel;
94+
Costfitness;
95+
List*savelist;
96+
97+
/* preserve root->join_rel_list, which gimme_tree changes */
98+
savelist=root->join_rel_list;
6299

63-
/*remember root->join_rel_list ... */
64-
/* because root->join_rel_list will be changed during the following */
65-
temp=listCopy(root->join_rel_list);
100+
/*create a temporary allocation context for the path construction work */
101+
oldcxt=MemoryContextSwitchTo(geqo_eval_context);
102+
StartPortalAllocMode(DefaultAllocMode,0);
66103

67-
/*joinrel is readily processed query tree -- left-sided ! */
104+
/*construct the best path for the given combination of relations */
68105
joinrel=gimme_tree(root,tour,0,num_gene,NULL);
69106

70107
/* compute fitness */
71108
fitness= (Cost)joinrel->cheapestpath->path_cost;
72109

73-
root->join_rel_list=temp;
110+
/* restore join_rel_list */
111+
root->join_rel_list=savelist;
74112

75-
pfree(joinrel);
113+
/* release all the memory acquired within gimme_tree */
114+
EndPortalAllocMode();
115+
MemoryContextSwitchTo(oldcxt);
76116

77117
returnfitness;
78118
}

‎src/backend/optimizer/geqo/geqo_main.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: geqo_main.c,v 1.14 1999/02/18 04:55:54 momjian Exp $
9+
* $Id: geqo_main.c,v 1.15 1999/05/17 00:25:33 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -70,51 +70,41 @@ geqo(Query *root)
7070
Chromosome*momma;
7171
Chromosome*daddy;
7272
Chromosome*kid;
73-
73+
intnumber_of_rels;
74+
Pool*pool;
75+
intpool_size,
76+
number_generations,
77+
status_interval;
78+
Gene*best_tour;
79+
RelOptInfo*best_rel;
7480
#if defined(ERX)
7581
Edge*edge_table;/* list of edges */
7682
intedge_failures=0;
7783
floatdifference;
78-
7984
#endif
80-
8185
#if defined(CX)|| defined(PX)|| defined(OX1)|| defined(OX2)
8286
City*city_table;/* list of cities */
83-
8487
#endif
85-
8688
#if defined(CX)
8789
intcycle_diffs=0;
8890
intmutations=0;
89-
9091
#endif
9192

92-
93-
intnumber_of_rels;
94-
95-
Pool*pool;
96-
intpool_size,
97-
number_generations,
98-
status_interval;
99-
100-
Gene*best_tour;
101-
RelOptInfo*best_rel;
102-
103-
/*Plan *best_plan; */
104-
105-
10693
/* set tour size */
10794
number_of_rels=length(root->base_rel_list);
10895

10996
/* set GA parameters */
110-
geqo_params(number_of_rels);/*out of "$PGDATA/pg_geqo" file */
97+
geqo_params(number_of_rels);/*read "$PGDATA/pg_geqo" file */
11198
pool_size=PoolSize;
11299
number_generations=Generations;
113100
status_interval=10;
114101

115102
/* seed random number generator */
116103
srandom(RandomSeed);
117104

105+
/* initialize plan evaluator */
106+
geqo_eval_startup();
107+
118108
/* allocate genetic pool memory */
119109
pool=alloc_pool(pool_size,number_of_rels);
120110

‎src/backend/optimizer/geqo/geqo_params.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: geqo_params.c,v 1.14 1999/02/15 03:22:01 momjian Exp $
8+
* $Id: geqo_params.c,v 1.15 1999/05/17 00:25:33 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -45,6 +45,16 @@
4545

4646
#include"storage/fd.h"
4747

48+
/*
49+
* Parameter values read from the config file (or defaulted) are stored here
50+
* by geqo_params().
51+
*/
52+
intPoolSize;
53+
intGenerations;
54+
longRandomSeed;
55+
doubleSelectionBias;
56+
57+
4858
#definePOOL_TAG"Pool_Size"
4959
#defineTRIAL_TAG"Generations"
5060
#defineRAND_TAG"Random_Seed"
@@ -77,7 +87,7 @@ geqo_params(int string_length)
7787

7888
char*conf_file;
7989

80-
/* thesestatic variablesare used tosignal that a value has been set */
90+
/* theseflag variables signal that a value has been set from the file */
8191
intpool_size=0;
8292
intnumber_trials=0;
8393
intrandom_seed=0;

‎src/include/optimizer/geqo.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: geqo.h,v 1.13 1999/02/18 04:55:54 momjian Exp $
8+
* $Id: geqo.h,v 1.14 1999/05/17 00:25:32 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -56,27 +56,22 @@
5656
#defineSELECTION_BIAS 2.0/* selective pressure within population */
5757
/* should be 1.5 <= SELECTION_BIAS <= 2.0 */
5858

59-
intPoolSize;
60-
intGenerations;
59+
/* parameter values set in geqo_params.c */
60+
externintPoolSize;
61+
externintGenerations;
62+
externlongRandomSeed;
63+
externdoubleSelectionBias;
6164

62-
longRandomSeed;/* defaults to (long) time(NULL) in
63-
* geqo_params.c */
64-
doubleSelectionBias;
65-
66-
/* logarithmic base for rel->size decrease in case of long
67-
queries that cause an integer overflow; used in geqo_eval.c */
68-
69-
#defineGEQO_LOG_BASE 1.5/* should be 1.0 < GEQO_LOG_BASE <= 2.0 */
70-
/* ^^^*/
71-
72-
/* geqo prototypes */
65+
/* routines in geqo_main.c */
7366
externRelOptInfo*geqo(Query*root);
7467

68+
/* routines in geqo_params.c */
7569
externvoidgeqo_params(intstring_length);
7670

71+
/* routines in geqo_eval.c */
72+
externvoidgeqo_eval_startup(void);
7773
externCostgeqo_eval(Query*root,Gene*tour,intnum_gene);
7874
externRelOptInfo*gimme_tree(Query*root,Gene*tour,intrel_count,
7975
intnum_gene,RelOptInfo*old_rel);
8076

81-
8277
#endif/* GEQO_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp