aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/adt/float.c20
-rw-r--r--src/backend/utils/misc/sampling.c52
2 files changed, 37 insertions, 35 deletions
diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index 098bbb372bf..455e5d8cbea 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -21,6 +21,7 @@
#include "catalog/pg_type.h"
#include "common/int.h"
+#include "common/pg_prng.h"
#include "common/shortest_dec.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
@@ -65,7 +66,7 @@ float8 degree_c_one = 1.0;
/* State for drandom() and setseed() */
static bool drandom_seed_set = false;
-static unsigned short drandom_seed[3] = {0, 0, 0};
+static pg_prng_state drandom_seed;
/* Local function prototypes */
static double sind_q1(double x);
@@ -2762,22 +2763,20 @@ drandom(PG_FUNCTION_ARGS)
* Should that fail for some reason, we fall back on a lower-quality
* seed based on current time and PID.
*/
- if (!pg_strong_random(drandom_seed, sizeof(drandom_seed)))
+ if (unlikely(!pg_prng_strong_seed(&drandom_seed)))
{
TimestampTz now = GetCurrentTimestamp();
uint64 iseed;
/* Mix the PID with the most predictable bits of the timestamp */
iseed = (uint64) now ^ ((uint64) MyProcPid << 32);
- drandom_seed[0] = (unsigned short) iseed;
- drandom_seed[1] = (unsigned short) (iseed >> 16);
- drandom_seed[2] = (unsigned short) (iseed >> 32);
+ pg_prng_seed(&drandom_seed, iseed);
}
drandom_seed_set = true;
}
- /* pg_erand48 produces desired result range [0.0 - 1.0) */
- result = pg_erand48(drandom_seed);
+ /* pg_prng_double produces desired result range [0.0 - 1.0) */
+ result = pg_prng_double(&drandom_seed);
PG_RETURN_FLOAT8(result);
}
@@ -2790,7 +2789,6 @@ Datum
setseed(PG_FUNCTION_ARGS)
{
float8 seed = PG_GETARG_FLOAT8(0);
- uint64 iseed;
if (seed < -1 || seed > 1 || isnan(seed))
ereport(ERROR,
@@ -2798,11 +2796,7 @@ setseed(PG_FUNCTION_ARGS)
errmsg("setseed parameter %g is out of allowed range [-1,1]",
seed)));
- /* Use sign bit + 47 fractional bits to fill drandom_seed[] */
- iseed = (int64) (seed * (float8) UINT64CONST(0x7FFFFFFFFFFF));
- drandom_seed[0] = (unsigned short) iseed;
- drandom_seed[1] = (unsigned short) (iseed >> 16);
- drandom_seed[2] = (unsigned short) (iseed >> 32);
+ pg_prng_fseed(&drandom_seed, seed);
drandom_seed_set = true;
PG_RETURN_VOID();
diff --git a/src/backend/utils/misc/sampling.c b/src/backend/utils/misc/sampling.c
index 0c327e823f7..d1a2416e8b7 100644
--- a/src/backend/utils/misc/sampling.c
+++ b/src/backend/utils/misc/sampling.c
@@ -37,7 +37,7 @@
*/
BlockNumber
BlockSampler_Init(BlockSampler bs, BlockNumber nblocks, int samplesize,
- long randseed)
+ uint32 randseed)
{
bs->N = nblocks; /* measured table size */
@@ -49,7 +49,7 @@ BlockSampler_Init(BlockSampler bs, BlockNumber nblocks, int samplesize,
bs->t = 0; /* blocks scanned so far */
bs->m = 0; /* blocks selected so far */
- sampler_random_init_state(randseed, bs->randstate);
+ sampler_random_init_state(randseed, &bs->randstate);
return Min(bs->n, bs->N);
}
@@ -98,7 +98,7 @@ BlockSampler_Next(BlockSampler bs)
* less than k, which means that we cannot fail to select enough blocks.
*----------
*/
- V = sampler_random_fract(bs->randstate);
+ V = sampler_random_fract(&bs->randstate);
p = 1.0 - (double) k / (double) K;
while (V < p)
{
@@ -136,10 +136,11 @@ reservoir_init_selection_state(ReservoirState rs, int n)
* Reservoir sampling is not used anywhere where it would need to return
* repeatable results so we can initialize it randomly.
*/
- sampler_random_init_state(random(), rs->randstate);
+ sampler_random_init_state(pg_prng_uint32(&pg_global_prng_state),
+ &rs->randstate);
/* Initial value of W (for use when Algorithm Z is first applied) */
- rs->W = exp(-log(sampler_random_fract(rs->randstate)) / n);
+ rs->W = exp(-log(sampler_random_fract(&rs->randstate)) / n);
}
double
@@ -154,7 +155,7 @@ reservoir_get_next_S(ReservoirState rs, double t, int n)
double V,
quot;
- V = sampler_random_fract(rs->randstate); /* Generate V */
+ V = sampler_random_fract(&rs->randstate); /* Generate V */
S = 0;
t += 1;
/* Note: "num" in Vitter's code is always equal to t - n */
@@ -186,7 +187,7 @@ reservoir_get_next_S(ReservoirState rs, double t, int n)
tmp;
/* Generate U and X */
- U = sampler_random_fract(rs->randstate);
+ U = sampler_random_fract(&rs->randstate);
X = t * (W - 1.0);
S = floor(X); /* S is tentatively set to floor(X) */
/* Test if U <= h(S)/cg(X) in the manner of (6.3) */
@@ -215,7 +216,7 @@ reservoir_get_next_S(ReservoirState rs, double t, int n)
y *= numer / denom;
denom -= 1;
}
- W = exp(-log(sampler_random_fract(rs->randstate)) / n); /* Generate W in advance */
+ W = exp(-log(sampler_random_fract(&rs->randstate)) / n); /* Generate W in advance */
if (exp(log(y) / n) <= (t + X) / t)
break;
}
@@ -230,24 +231,22 @@ reservoir_get_next_S(ReservoirState rs, double t, int n)
*----------
*/
void
-sampler_random_init_state(long seed, SamplerRandomState randstate)
+sampler_random_init_state(uint32 seed, pg_prng_state *randstate)
{
- randstate[0] = 0x330e; /* same as pg_erand48, but could be anything */
- randstate[1] = (unsigned short) seed;
- randstate[2] = (unsigned short) (seed >> 16);
+ pg_prng_seed(randstate, (uint64) seed);
}
/* Select a random value R uniformly distributed in (0 - 1) */
double
-sampler_random_fract(SamplerRandomState randstate)
+sampler_random_fract(pg_prng_state *randstate)
{
double res;
- /* pg_erand48 returns a value in [0.0 - 1.0), so we must reject 0 */
+ /* pg_prng_double returns a value in [0.0 - 1.0), so we must reject 0.0 */
do
{
- res = pg_erand48(randstate);
- } while (res == 0.0);
+ res = pg_prng_double(randstate);
+ } while (unlikely(res == 0.0));
return res;
}
@@ -261,27 +260,36 @@ sampler_random_fract(SamplerRandomState randstate)
* except that a common random state is used across all callers.
*/
static ReservoirStateData oldrs;
+static bool oldrs_initialized = false;
double
anl_random_fract(void)
{
/* initialize if first time through */
- if (oldrs.randstate[0] == 0)
- sampler_random_init_state(random(), oldrs.randstate);
+ if (unlikely(!oldrs_initialized))
+ {
+ sampler_random_init_state(pg_prng_uint32(&pg_global_prng_state),
+ &oldrs.randstate);
+ oldrs_initialized = true;
+ }
/* and compute a random fraction */
- return sampler_random_fract(oldrs.randstate);
+ return sampler_random_fract(&oldrs.randstate);
}
double
anl_init_selection_state(int n)
{
/* initialize if first time through */
- if (oldrs.randstate[0] == 0)
- sampler_random_init_state(random(), oldrs.randstate);
+ if (unlikely(!oldrs_initialized))
+ {
+ sampler_random_init_state(pg_prng_uint32(&pg_global_prng_state),
+ &oldrs.randstate);
+ oldrs_initialized = true;
+ }
/* Initial value of W (for use when Algorithm Z is first applied) */
- return exp(-log(sampler_random_fract(oldrs.randstate)) / n);
+ return exp(-log(sampler_random_fract(&oldrs.randstate)) / n);
}
double