mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.
Standardize on xoroshiro128** as our basic PRNG algorithm, eliminating a bunch of platform dependencies as well as fundamentally-obsolete PRNG code. In addition, this API replacement will ease replacing the algorithm again in future, should that become necessary. xoroshiro128** is a few percent slower than the drand48 family, but it can produce full-width 64-bit random values not only 48-bit, and it should be much more trustworthy. It's likely to be noticeably faster than the platform's random(), depending on which platform you are thinking about; and we can have non-global state vectors easily, unlike with random(). It is not cryptographically strong, but neither are the functions it replaces. Fabien Coelho, reviewed by Dean Rasheed, Aleksander Alekseev, and myself Discussion: https://postgr.es/m/alpine.DEB.2.22.394.2105241211230.165418@pseudo
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "access/gin_private.h"
|
||||
#include "access/relscan.h"
|
||||
#include "common/pg_prng.h"
|
||||
#include "miscadmin.h"
|
||||
#include "storage/predicate.h"
|
||||
#include "utils/datum.h"
|
||||
@@ -787,7 +788,7 @@ entryLoadMoreItems(GinState *ginstate, GinScanEntry entry,
|
||||
}
|
||||
}
|
||||
|
||||
#define gin_rand() (((double) random()) / ((double) MAX_RANDOM_VALUE))
|
||||
#define gin_rand() pg_prng_double(&pg_global_prng_state)
|
||||
#define dropItem(e) ( gin_rand() > ((double)GinFuzzySearchLimit)/((double)((e)->predictNumberResult)) )
|
||||
|
||||
/*
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "access/htup_details.h"
|
||||
#include "access/reloptions.h"
|
||||
#include "catalog/pg_opclass.h"
|
||||
#include "common/pg_prng.h"
|
||||
#include "storage/indexfsm.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "utils/float.h"
|
||||
@@ -507,7 +508,7 @@ gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
|
||||
if (keep_current_best == -1)
|
||||
{
|
||||
/* we didn't make the random choice yet for this old best */
|
||||
keep_current_best = (random() <= (MAX_RANDOM_VALUE / 2)) ? 1 : 0;
|
||||
keep_current_best = pg_prng_bool(&pg_global_prng_state) ? 1 : 0;
|
||||
}
|
||||
if (keep_current_best == 0)
|
||||
{
|
||||
@@ -529,7 +530,7 @@ gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
|
||||
if (keep_current_best == -1)
|
||||
{
|
||||
/* we didn't make the random choice yet for this old best */
|
||||
keep_current_best = (random() <= (MAX_RANDOM_VALUE / 2)) ? 1 : 0;
|
||||
keep_current_best = pg_prng_bool(&pg_global_prng_state) ? 1 : 0;
|
||||
}
|
||||
if (keep_current_best == 1)
|
||||
break;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "access/nbtxlog.h"
|
||||
#include "access/transam.h"
|
||||
#include "access/xloginsert.h"
|
||||
#include "common/pg_prng.h"
|
||||
#include "lib/qunique.h"
|
||||
#include "miscadmin.h"
|
||||
#include "storage/lmgr.h"
|
||||
@@ -965,7 +966,7 @@ _bt_findinsertloc(Relation rel,
|
||||
|
||||
if (P_RIGHTMOST(opaque) ||
|
||||
_bt_compare(rel, itup_key, page, P_HIKEY) != 0 ||
|
||||
random() <= (MAX_RANDOM_VALUE / 100))
|
||||
pg_prng_uint32(&pg_global_prng_state) <= (PG_UINT32_MAX / 100))
|
||||
break;
|
||||
|
||||
_bt_stepright(rel, insertstate, stack);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "access/spgist_private.h"
|
||||
#include "access/spgxlog.h"
|
||||
#include "access/xloginsert.h"
|
||||
#include "common/pg_prng.h"
|
||||
#include "miscadmin.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "utils/rel.h"
|
||||
@@ -2210,7 +2211,9 @@ spgdoinsert(Relation index, SpGistState *state,
|
||||
if (out.resultType == spgAddNode)
|
||||
elog(ERROR, "cannot add a node to an allTheSame inner tuple");
|
||||
else if (out.resultType == spgMatchNode)
|
||||
out.result.matchNode.nodeN = random() % innerTuple->nNodes;
|
||||
out.result.matchNode.nodeN =
|
||||
pg_prng_uint64_range(&pg_global_prng_state,
|
||||
0, innerTuple->nNodes - 1);
|
||||
}
|
||||
|
||||
switch (out.resultType)
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include "commands/async.h"
|
||||
#include "commands/tablecmds.h"
|
||||
#include "commands/trigger.h"
|
||||
#include "common/pg_prng.h"
|
||||
#include "executor/spi.h"
|
||||
#include "libpq/be-fsstubs.h"
|
||||
#include "libpq/pqsignal.h"
|
||||
@@ -1990,7 +1991,7 @@ StartTransaction(void)
|
||||
/* Determine if statements are logged in this transaction */
|
||||
xact_is_sampled = log_xact_sample_rate != 0 &&
|
||||
(log_xact_sample_rate == 1 ||
|
||||
random() <= log_xact_sample_rate * MAX_RANDOM_VALUE);
|
||||
pg_prng_double(&pg_global_prng_state) <= log_xact_sample_rate);
|
||||
|
||||
/*
|
||||
* initialize current transaction state fields
|
||||
|
||||
Reference in New Issue
Block a user