]> granicus.if.org Git - postgresql/blob - doc/src/sgml/geqo.sgml
Add port information for NS32532.
[postgresql] / doc / src / sgml / geqo.sgml
1 <Chapter Id="geqo">
2 <DocInfo>
3 <Author>
4 <FirstName>Martin</FirstName>
5 <SurName>Utesch</SurName>
6 <Affiliation>
7 <Orgname>
8 University of Mining and Technology
9 </Orgname>
10 <Orgdiv>
11 Institute of Automatic Control
12 </Orgdiv>
13 <Address>
14 <City>
15 Freiberg
16 </City>
17 <Country>
18 Germany
19 </Country>
20 </Address>
21 </Affiliation>
22 </Author>
23 <Date>1997-10-02</Date>
24 </DocInfo>
25
26 <Title>Genetic Query Optimization in Database Systems</Title>
27
28 <Para>
29 <Note>
30 <Title>Author</Title>
31 <Para>
32 Written by <ULink url="utesch@aut.tu-freiberg.de">Martin Utesch</ULink>
33 for the Institute of Automatic Control at the University of Mining and Technology in Freiberg, Germany.
34 </Para>
35 </Note>
36
37 <Sect1>
38 <Title>Query Handling as a Complex Optimization Problem</Title>
39
40 <Para>
41    Among all relational operators the most difficult one to process and
42 optimize is the <FirstTerm>join</FirstTerm>. The number of alternative plans to answer a query
43 grows exponentially with the number of <Command>join</Command>s included in it. Further
44 optimization effort is caused by the support of a variety of <FirstTerm>join methods</FirstTerm>
45  (e.g., nested loop, index scan, merge join in <ProductName>Postgres</ProductName>) to
46 process individual <Command>join</Command>s and a diversity of <FirstTerm>indices</FirstTerm> (e.g., r-tree,
47 b-tree, hash in <ProductName>Postgres</ProductName>) as access paths for relations.
48
49 <Para>
50    The current <ProductName>Postgres</ProductName> optimizer implementation performs a <FirstTerm>near-
51 exhaustive search</FirstTerm> over the space of alternative strategies. This query
52 optimization technique is inadequate to support database application
53 domains that involve the need for extensive queries, such as artificial
54 intelligence.
55
56 <Para>
57    The Institute of Automatic Control at the University of Mining and
58 Technology, in Freiberg, Germany, encountered the described problems as its
59 folks wanted to take the <ProductName>Postgres</ProductName> DBMS as the backend for a decision
60 support knowledge based system for the maintenance of an electrical
61 power grid. The DBMS needed to handle large <Command>join</Command> queries for the
62 inference machine of the knowledge based system.
63
64 <Para>
65    Performance difficulties within exploring the space of possible query
66 plans arose the demand for a new optimization technique being developed.
67
68 <Para>
69    In the following we propose the implementation of a <FirstTerm>Genetic Algorithm</FirstTerm>
70  as an option for the database query optimization problem.
71
72
73 <Sect1>
74 <Title>Genetic Algorithms (<Acronym>GA</Acronym>)</Title>
75
76 <Para>
77    The <Acronym>GA</Acronym> is a heuristic optimization method which operates through 
78 determined, randomized search. The set of possible solutions for the
79 optimization problem is considered as a <FirstTerm>population</FirstTerm> of <FirstTerm>individuals</FirstTerm>.
80 The degree of adaption of an individual to its environment is specified
81 by its <FirstTerm>fitness</FirstTerm>.
82
83 <Para>
84    The coordinates of an individual in the search space are represented
85 by <FirstTerm>chromosomes</FirstTerm>, in essence a set of character strings. A <FirstTerm>gene</FirstTerm> is a
86 subsection of a chromosome which encodes the value of a single parameter
87 being optimized. Typical encodings for a gene could be <FirstTerm>binary</FirstTerm> or
88 <FirstTerm>integer</FirstTerm>.
89
90 <Para>
91    Through simulation of the evolutionary operations <FirstTerm>recombination</FirstTerm>,
92 <FirstTerm>mutation</FirstTerm>, and <FirstTerm>selection</FirstTerm> new generations of search points are found
93 that show a higher average fitness than their ancestors.
94
95 <Para>
96    According to the "comp.ai.genetic" <Acronym>FAQ</Acronym> it cannot be stressed too
97 strongly that a <Acronym>GA</Acronym> is not a pure random search for a solution to a
98 problem. A <Acronym>GA</Acronym> uses stochastic processes, but the result is distinctly
99 non-random (better than random). 
100
101 <ProgramListing>
102 Structured Diagram of a <Acronym>GA</Acronym>:
103 ---------------------------
104
105 P(t)    generation of ancestors at a time t
106 P''(t)  generation of descendants at a time t
107
108 +=========================================+
109 |>>>>>>>>>>>  Algorithm GA  <<<<<<<<<<<<<<|
110 +=========================================+
111 | INITIALIZE t := 0                       |
112 +=========================================+
113 | INITIALIZE P(t)                         |
114 +=========================================+
115 | evalute FITNESS of P(t)                 |
116 +=========================================+
117 | while not STOPPING CRITERION do         |
118 |   +-------------------------------------+
119 |   | P'(t)  := RECOMBINATION{P(t)}       |
120 |   +-------------------------------------+
121 |   | P''(t) := MUTATION{P'(t)}           |
122 |   +-------------------------------------+
123 |   | P(t+1) := SELECTION{P''(t) + P(t)}  |
124 |   +-------------------------------------+
125 |   | evalute FITNESS of P''(t)           |
126 |   +-------------------------------------+
127 |   | t := t + 1                          |
128 +===+=====================================+
129 </ProgramListing>
130
131 <Sect1>
132 <Title>Genetic Query Optimization (<Acronym>GEQO</Acronym>) in Postgres</Title>
133
134 <Para>
135    The <Acronym>GEQO</Acronym> module is intended for the solution of the query
136 optimization problem similar to a traveling salesman problem (<Acronym>TSP</Acronym>).
137 Possible query plans are encoded as integer strings. Each string
138 represents the <Command>join</Command> order from one relation of the query to the next.
139 E. g., the query tree
140 <ProgramListing>
141        /\
142       /\ 2
143      /\ 3
144     4  1
145 </ProgramListing>
146 is encoded by the integer string '4-1-3-2',
147 which means, first join relation '4' and '1', then '3', and
148 then '2', where 1, 2, 3, 4 are relids in <ProductName>Postgres</ProductName>.
149
150 <Para>
151    Parts of the <Acronym>GEQO</Acronym> module are adapted from D. Whitley's Genitor
152 algorithm.
153
154 <Para>
155    Specific characteristics of the <Acronym>GEQO</Acronym> implementation in <ProductName>Postgres</ProductName>
156 are:
157
158 <ItemizedList Mark="bullet" Spacing="compact">
159 <ListItem>
160 <Para>
161 Usage of a <FirstTerm>steady state</FirstTerm> <Acronym>GA</Acronym> (replacement of the least fit
162    individuals in a population, not whole-generational replacement)
163    allows fast convergence towards improved query plans. This is
164    essential for query handling with reasonable time;
165 </Para>
166 </ListItem>
167
168 <ListItem>
169 <Para>
170 Usage of <FirstTerm>edge recombination crossover</FirstTerm> which is especially suited
171    to keep edge losses low for the solution of the <Acronym>TSP</Acronym> by means of a <Acronym>GA</Acronym>;
172 </Para>
173 </ListItem>
174
175 <ListItem>
176 <Para>
177 Mutation as genetic operator is deprecated so that no repair
178    mechanisms are needed to generate legal <Acronym>TSP</Acronym> tours.
179 </Para>
180 </ListItem>
181 </ItemizedList>
182
183 <Para>
184    The <Acronym>GEQO</Acronym> module gives the following benefits to the <ProductName>Postgres</ProductName> DBMS
185 compared to the <ProductName>Postgres</ProductName> query optimizer implementation:
186
187 <ItemizedList Mark="bullet" Spacing="compact">
188 <ListItem>
189 <Para>
190 Handling of large <Command>join</Command> queries through non-exhaustive search;
191 </Para>
192 </ListItem>
193
194 <ListItem>
195 <Para>
196 Improved cost size approximation of query plans since no longer
197    plan merging is needed (the <Acronym>GEQO</Acronym> module evaluates the cost for a
198    query plan as an individual).
199 </Para>
200 </ListItem>
201 </ItemizedList>
202
203 </Sect1>
204
205 <Sect1>
206 <Title>Future Implementation Tasks for <ProductName>Postgres</ProductName> <Acronym>GEQO</Acronym></Title>
207
208 <Sect2>
209 <Title>Basic Improvements</Title>
210
211 <Sect3>
212 <Title>Improve freeing of memory when query is already processed</Title>
213
214 <Para>
215 With large <Command>join</Command> queries the computing time spent for the genetic query
216 optimization seems to be a mere <Emphasis>fraction</Emphasis> of the time
217  <ProductName>Postgres</ProductName>
218 needs for freeing memory via routine <Function>MemoryContextFree</Function>,
219 file <FileName>backend/utils/mmgr/mcxt.c</FileName>.
220 Debugging showed that it get stucked in a loop of routine
221 <Function>OrderedElemPop</Function>, file <FileName>backend/utils/mmgr/oset.c</FileName>.
222 The same problems arise with long queries when using the normal
223 <ProductName>Postgres</ProductName> query optimization algorithm.
224
225 <Sect3>
226 <Title>Improve genetic algorithm parameter settings</Title>
227
228 <Para>
229 In file <FileName>backend/optimizer/geqo/geqo_params.c</FileName>, routines
230 <Function>gimme_pool_size</Function> and <Function>gimme_number_generations</Function>,
231 we have to find a compromise for the parameter settings
232 to satisfy two competing demands:
233 <ItemizedList Spacing="compact">
234 <ListItem>
235 <Para>
236 Optimality of the query plan
237 </Para>
238 </ListItem>
239 <ListItem>
240 <Para>
241 Computing time
242 </Para>
243 </ListItem>
244 </ItemizedList>
245
246 <Sect3>
247 <Title>Find better solution for integer overflow</Title>
248
249 <Para>
250 In file <FileName>backend/optimizer/geqo/geqo_eval.c</FileName>, routine
251 <Function>geqo_joinrel_size</Function>,
252 the present hack for MAXINT overflow is to set the <ProductName>Postgres</ProductName> integer
253 value of <StructField>rel->size</StructField> to its logarithm.
254 Modifications of <StructName>Rel</StructName> in <FileName>backend/nodes/relation.h</FileName> will
255 surely have severe impacts on the whole <ProductName>Postgres</ProductName> implementation.
256
257 <Sect3>
258 <Title>Find solution for exhausted memory</Title>
259
260 <Para>
261 Memory exhaustion may occur with more than 10 relations involved in a query.
262 In file <FileName>backend/optimizer/geqo/geqo_eval.c</FileName>, routine
263 <Function>gimme_tree</Function> is recursively called.
264 Maybe I forgot something to be freed correctly, but I dunno what.
265 Of course the <StructName>rel</StructName> data structure of the <Command>join</Command> keeps growing and
266 growing the more relations are packed into it.
267 Suggestions are welcome :-(
268
269
270 <Sect2>
271 <Title>Further Improvements</Title>
272
273 <Para>
274 Enable bushy query tree processing within <ProductName>Postgres</ProductName>;
275 that may improve the quality of query plans.
276
277 <BIBLIOGRAPHY Id="geqo-references">
278 <TITLE>
279 References
280 </TITLE>
281 <PARA>Reference information for <Acronym>GEQ</Acronym> algorithms.
282 </PARA>
283 <BIBLIOENTRY>
284
285 <BOOKBIBLIO>
286 <TITLE>
287 The Hitch-Hiker's Guide to Evolutionary Computation
288 </TITLE>
289 <AUTHORGROUP>
290 <AUTHOR>
291 <FIRSTNAME>J&ouml;rg</FIRSTNAME>
292 <SURNAME>Heitk&ouml;tter</SURNAME>
293 </AUTHOR>
294 <AUTHOR>
295 <FIRSTNAME>David</FIRSTNAME>
296 <SURNAME>Beasley</SURNAME>
297 </AUTHOR>
298 </AUTHORGROUP>
299 <PUBLISHER>
300 <PUBLISHERNAME>
301 InterNet resource
302 </PUBLISHERNAME>
303 </PUBLISHER>
304 <ABSTRACT>
305 <Para>
306 FAQ in <ULink url="news://comp.ai.genetic">comp.ai.genetic</ULink>
307 is available at <ULink url="ftp://ftp.Germany.EU.net/pub/research/softcomp/EC/Welcome.html">Encore</ULink>.
308 </Para>
309 </ABSTRACT>
310 </BOOKBIBLIO>
311
312 <BOOKBIBLIO>
313 <TITLE>
314 The Design and Implementation of the Postgres Query Optimizer
315 </TITLE>
316 <AUTHORGROUP>
317 <AUTHOR>
318 <FIRSTNAME>Z.</FIRSTNAME>
319 <SURNAME>Fong</SURNAME>
320 </AUTHOR>
321 </AUTHORGROUP>
322 <PUBLISHER>
323 <PUBLISHERNAME>
324 University of California, Berkeley Computer Science Department
325 </PUBLISHERNAME>
326 </PUBLISHER>
327 <ABSTRACT>
328 <Para>
329 File <FileName>planner/Report.ps</FileName> in the 'postgres-papers' distribution.
330 </Para>
331 </ABSTRACT>
332 </BOOKBIBLIO>
333
334 <BOOKBIBLIO>
335 <TITLE>
336 Fundamentals of Database Systems
337 </TITLE>
338 <AUTHORGROUP>
339 <AUTHOR>
340 <FIRSTNAME>R.</FIRSTNAME>
341 <SURNAME>Elmasri</SURNAME>
342 </AUTHOR>
343 <AUTHOR>
344 <FIRSTNAME>S.</FIRSTNAME>
345 <SURNAME>Navathe</SURNAME>
346 </AUTHOR>
347 </AUTHORGROUP>
348 <PUBLISHER>
349 <PUBLISHERNAME>
350 The Benjamin/Cummings Pub., Inc.
351 </PUBLISHERNAME>
352 </PUBLISHER>
353 </BOOKBIBLIO>
354
355 </BIBLIOENTRY>
356 </BIBLIOGRAPHY>
357
358 </Chapter>