From 573200c188ce825a812ab477c7caa9c0ed851b6a Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 7 Dec 2022 15:28:11 -0800 Subject: [PATCH] Use pg_prng_uint32 for Postgres 15 --- src/ivfbuild.c | 3 +-- src/ivfflat.h | 14 ++++++++++++++ src/ivfkmeans.c | 13 +------------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/ivfbuild.c b/src/ivfbuild.c index d8d00c4..408883a 100644 --- a/src/ivfbuild.c +++ b/src/ivfbuild.c @@ -5,7 +5,6 @@ #include "catalog/index.h" #include "ivfflat.h" #include "miscadmin.h" -#include "port.h" #include "storage/bufmgr.h" #if PG_VERSION_NUM >= 140000 @@ -108,7 +107,7 @@ SampleRows(IvfflatBuildState * buildstate) buildstate->rowstoskip = -1; - BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, random()); + BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt()); reservoir_init_selection_state(&buildstate->rstate, targsamples); while (BlockSampler_HasMore(&buildstate->bs)) diff --git a/src/ivfflat.h b/src/ivfflat.h index a64c28c..5780db8 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -10,6 +10,12 @@ #include "utils/tuplesort.h" #include "vector.h" +#if PG_VERSION_NUM >= 150000 +#include "common/pg_prng.h" +#else +#include "port.h" +#endif + #ifdef IVFFLAT_BENCH #include "portability/instr_time.h" #endif @@ -62,6 +68,14 @@ #define IvfflatBench(name, code) (code) #endif +#if PG_VERSION_NUM >= 150000 +#define RandomDouble() pg_prng_double(&pg_global_prng_state) +#define RandomInt() pg_prng_uint32(&pg_global_prng_state) +#else +#define RandomDouble() (((double) random()) / MAX_RANDOM_VALUE) +#define RandomInt() random() +#endif + /* Variables */ extern int ivfflat_probes; diff --git a/src/ivfkmeans.c b/src/ivfkmeans.c index ebb45b1..40d8afc 100644 --- a/src/ivfkmeans.c +++ b/src/ivfkmeans.c @@ -4,17 +4,6 @@ #include "ivfflat.h" #include "miscadmin.h" -#include "port.h" - -#if PG_VERSION_NUM >= 150000 -#include "common/pg_prng.h" -#endif - -#if PG_VERSION_NUM >= 150000 -#define RandomDouble() pg_prng_double(&pg_global_prng_state) -#else -#define RandomDouble() (((double) random()) / MAX_RANDOM_VALUE) -#endif /* * Initialize with kmeans++ @@ -40,7 +29,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, float *low collation = index->rd_indcollation[0]; /* Choose an initial center uniformly at random */ - VectorArraySet(centers, 0, VectorArrayGet(samples, random() % samples->length)); + VectorArraySet(centers, 0, VectorArrayGet(samples, RandomInt() % samples->length)); centers->length++; for (j = 0; j < numSamples; j++)