summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/geqo/geqo_random.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2009-07-16 20:55:44 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2009-07-16 20:55:44 +0000
commitf5bc74192d2ffb32952a06c62b3458d28ff7f98f (patch)
treed582b83c6ba2ff21d2970660806d353c6ac496ee /src/backend/optimizer/geqo/geqo_random.c
parentc43feefa806c81d68115ed03a7f723720cefad31 (diff)
downloadpostgresql-f5bc74192d2ffb32952a06c62b3458d28ff7f98f.tar.gz
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
Diffstat (limited to 'src/backend/optimizer/geqo/geqo_random.c')
-rw-r--r--src/backend/optimizer/geqo/geqo_random.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/backend/optimizer/geqo/geqo_random.c b/src/backend/optimizer/geqo/geqo_random.c
new file mode 100644
index 0000000000..025407e2d3
--- /dev/null
+++ b/src/backend/optimizer/geqo/geqo_random.c
@@ -0,0 +1,40 @@
+/*------------------------------------------------------------------------
+ *
+ * geqo_random.c
+ * random number generator
+ *
+ * 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_random.c,v 1.1 2009/07/16 20:55:44 tgl Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "optimizer/geqo_random.h"
+
+
+void
+geqo_set_seed(PlannerInfo *root, double seed)
+{
+ GeqoPrivateData *private = (GeqoPrivateData *) root->join_search_private;
+
+ /*
+ * XXX. This seeding algorithm could certainly be improved - but
+ * it is not critical to do so.
+ */
+ memset(private->random_state, 0, sizeof(private->random_state));
+ memcpy(private->random_state,
+ &seed,
+ Min(sizeof(private->random_state), sizeof(seed)));
+}
+
+double
+geqo_rand(PlannerInfo *root)
+{
+ GeqoPrivateData *private = (GeqoPrivateData *) root->join_search_private;
+
+ return erand48(private->random_state);
+}