1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-19 13:42:17 +03:00

Make GEQO's planning deterministic by having it start from a predictable

random number seed each time.  This is how it used to work years ago, but
we got rid of the seed reset because it was resetting the main random()
sequence and thus having undesirable effects on the rest of the system.
To fix, establish a private random number state for each execution of
geqo(), and initialize the state using the new GUC variable geqo_seed.
People who want to experiment with different random searches can do so
by changing geqo_seed, but you'll always get the same plan for the same
value of geqo_seed (if holding all other planner inputs constant, of course).

The new state is kept in PlannerInfo by adding a "void *" field reserved
for use by join_search hooks.  Most of the rather bulky code changes in
this commit are just arranging to pass PlannerInfo around to all the GEQO
functions (many of which formerly didn't receive it).

Andres Freund, with some editorialization by Tom
This commit is contained in:
Tom Lane
2009-07-16 20:55:44 +00:00
parent c43feefa80
commit f5bc74192d
27 changed files with 305 additions and 202 deletions

View File

@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/backend/optimizer/geqo/geqo_pool.c,v 1.33 2009/01/01 17:23:43 momjian Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/geqo/geqo_pool.c,v 1.34 2009/07/16 20:55:44 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -39,7 +39,7 @@ static int compare(const void *arg1, const void *arg2);
* allocates memory for GA pool
*/
Pool *
alloc_pool(int pool_size, int string_length)
alloc_pool(PlannerInfo *root, int pool_size, int string_length)
{
Pool *new_pool;
Chromosome *chromo;
@@ -66,7 +66,7 @@ alloc_pool(int pool_size, int string_length)
* deallocates memory for GA pool
*/
void
free_pool(Pool *pool)
free_pool(PlannerInfo *root, Pool *pool)
{
Chromosome *chromo;
int i;
@@ -88,7 +88,7 @@ free_pool(Pool *pool)
* initialize genetic pool
*/
void
random_init_pool(Pool *pool, GeqoEvalData *evaldata)
random_init_pool(PlannerInfo *root, Pool *pool)
{
Chromosome *chromo = (Chromosome *) pool->data;
int i;
@@ -105,10 +105,9 @@ random_init_pool(Pool *pool, GeqoEvalData *evaldata)
i = 0;
while (i < pool->size)
{
init_tour(chromo[i].string, pool->string_length);
pool->data[i].worth = geqo_eval(chromo[i].string,
pool->string_length,
evaldata);
init_tour(root, chromo[i].string, pool->string_length);
pool->data[i].worth = geqo_eval(root, chromo[i].string,
pool->string_length);
if (pool->data[i].worth < DBL_MAX)
i++;
else
@@ -133,7 +132,7 @@ random_init_pool(Pool *pool, GeqoEvalData *evaldata)
* maybe you have to change compare() for different ordering ...
*/
void
sort_pool(Pool *pool)
sort_pool(PlannerInfo *root, Pool *pool)
{
qsort(pool->data, pool->size, sizeof(Chromosome), compare);
}
@@ -160,7 +159,7 @@ compare(const void *arg1, const void *arg2)
* allocates a chromosome and string space
*/
Chromosome *
alloc_chromo(int string_length)
alloc_chromo(PlannerInfo *root, int string_length)
{
Chromosome *chromo;
@@ -174,7 +173,7 @@ alloc_chromo(int string_length)
* deallocates a chromosome and string space
*/
void
free_chromo(Chromosome *chromo)
free_chromo(PlannerInfo *root, Chromosome *chromo)
{
pfree(chromo->string);
pfree(chromo);
@@ -185,7 +184,7 @@ free_chromo(Chromosome *chromo)
* assumes best->worst = smallest->largest
*/
void
spread_chromo(Chromosome *chromo, Pool *pool)
spread_chromo(PlannerInfo *root, Chromosome *chromo, Pool *pool)
{
int top,
mid,
@@ -247,7 +246,7 @@ spread_chromo(Chromosome *chromo, Pool *pool)
* copy new gene into pool storage; always replace worst gene in pool
*/
geqo_copy(&pool->data[pool->size - 1], chromo, pool->string_length);
geqo_copy(root, &pool->data[pool->size - 1], chromo, pool->string_length);
swap_chromo.string = pool->data[pool->size - 1].string;
swap_chromo.worth = pool->data[pool->size - 1].worth;