PostgreSQL Source Code  git master
sampling.h File Reference
#include "storage/block.h"
Include dependency graph for sampling.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BlockSamplerData
 
struct  ReservoirStateData
 

Typedefs

typedef unsigned short SamplerRandomState[3]
 
typedef BlockSamplerDataBlockSampler
 
typedef ReservoirStateDataReservoirState
 

Functions

void sampler_random_init_state (long seed, SamplerRandomState randstate)
 
double sampler_random_fract (SamplerRandomState randstate)
 
BlockNumber BlockSampler_Init (BlockSampler bs, BlockNumber nblocks, int samplesize, long randseed)
 
bool BlockSampler_HasMore (BlockSampler bs)
 
BlockNumber BlockSampler_Next (BlockSampler bs)
 
void reservoir_init_selection_state (ReservoirState rs, int n)
 
double reservoir_get_next_S (ReservoirState rs, double t, int n)
 
double anl_random_fract (void)
 
double anl_init_selection_state (int n)
 
double anl_get_next_S (double t, int n, double *stateptr)
 

Typedef Documentation

◆ BlockSampler

Definition at line 38 of file sampling.h.

◆ ReservoirState

Definition at line 53 of file sampling.h.

◆ SamplerRandomState

typedef unsigned short SamplerRandomState[3]

Definition at line 20 of file sampling.h.

Function Documentation

◆ anl_get_next_S()

double anl_get_next_S ( double  t,
int  n,
double *  stateptr 
)

Definition at line 288 of file sampling.c.

References reservoir_get_next_S(), and ReservoirStateData::W.

289 {
290  double result;
291 
292  oldrs.W = *stateptr;
293  result = reservoir_get_next_S(&oldrs, t, n);
294  *stateptr = oldrs.W;
295  return result;
296 }
static ReservoirStateData oldrs
Definition: sampling.c:263
double reservoir_get_next_S(ReservoirState rs, double t, int n)
Definition: sampling.c:146

◆ anl_init_selection_state()

double anl_init_selection_state ( int  n)

Definition at line 277 of file sampling.c.

References random(), ReservoirStateData::randstate, sampler_random_fract(), and sampler_random_init_state().

278 {
279  /* initialize if first time through */
280  if (oldrs.randstate[0] == 0)
282 
283  /* Initial value of W (for use when Algorithm Z is first applied) */
284  return exp(-log(sampler_random_fract(oldrs.randstate)) / n);
285 }
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:233
long random(void)
Definition: random.c:22
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:242
static ReservoirStateData oldrs
Definition: sampling.c:263
SamplerRandomState randstate
Definition: sampling.h:50

◆ anl_random_fract()

double anl_random_fract ( void  )

Definition at line 266 of file sampling.c.

References random(), ReservoirStateData::randstate, sampler_random_fract(), and sampler_random_init_state().

267 {
268  /* initialize if first time through */
269  if (oldrs.randstate[0] == 0)
271 
272  /* and compute a random fraction */
274 }
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:233
long random(void)
Definition: random.c:22
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:242
static ReservoirStateData oldrs
Definition: sampling.c:263
SamplerRandomState randstate
Definition: sampling.h:50

◆ BlockSampler_HasMore()

bool BlockSampler_HasMore ( BlockSampler  bs)

Definition at line 58 of file sampling.c.

References BlockSamplerData::m, BlockSamplerData::N, BlockSamplerData::n, and BlockSamplerData::t.

Referenced by acquire_sample_rows(), and BlockSampler_Next().

59 {
60  return (bs->t < bs->N) && (bs->m < bs->n);
61 }
BlockNumber t
Definition: sampling.h:33
BlockNumber N
Definition: sampling.h:31

◆ BlockSampler_Init()

BlockNumber BlockSampler_Init ( BlockSampler  bs,
BlockNumber  nblocks,
int  samplesize,
long  randseed 
)

Definition at line 39 of file sampling.c.

References BlockSamplerData::m, Min, BlockSamplerData::N, BlockSamplerData::n, BlockSamplerData::randstate, sampler_random_init_state(), and BlockSamplerData::t.

Referenced by acquire_sample_rows().

41 {
42  bs->N = nblocks; /* measured table size */
43 
44  /*
45  * If we decide to reduce samplesize for tables that have less or not much
46  * more than samplesize blocks, here is the place to do it.
47  */
48  bs->n = samplesize;
49  bs->t = 0; /* blocks scanned so far */
50  bs->m = 0; /* blocks selected so far */
51 
52  sampler_random_init_state(randseed, bs->randstate);
53 
54  return Min(bs->n, bs->N);
55 }
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:233
#define Min(x, y)
Definition: c.h:911
BlockNumber t
Definition: sampling.h:33
BlockNumber N
Definition: sampling.h:31
SamplerRandomState randstate
Definition: sampling.h:35

◆ BlockSampler_Next()

BlockNumber BlockSampler_Next ( BlockSampler  bs)

Definition at line 64 of file sampling.c.

References Assert, BlockSampler_HasMore(), K, BlockSamplerData::m, BlockSamplerData::N, BlockSamplerData::n, BlockSamplerData::randstate, sampler_random_fract(), and BlockSamplerData::t.

Referenced by acquire_sample_rows().

65 {
66  BlockNumber K = bs->N - bs->t; /* remaining blocks */
67  int k = bs->n - bs->m; /* blocks still to sample */
68  double p; /* probability to skip block */
69  double V; /* random */
70 
71  Assert(BlockSampler_HasMore(bs)); /* hence K > 0 and k > 0 */
72 
73  if ((BlockNumber) k >= K)
74  {
75  /* need all the rest */
76  bs->m++;
77  return bs->t++;
78  }
79 
80  /*----------
81  * It is not obvious that this code matches Knuth's Algorithm S.
82  * Knuth says to skip the current block with probability 1 - k/K.
83  * If we are to skip, we should advance t (hence decrease K), and
84  * repeat the same probabilistic test for the next block. The naive
85  * implementation thus requires a sampler_random_fract() call for each
86  * block number. But we can reduce this to one sampler_random_fract()
87  * call per selected block, by noting that each time the while-test
88  * succeeds, we can reinterpret V as a uniform random number in the range
89  * 0 to p. Therefore, instead of choosing a new V, we just adjust p to be
90  * the appropriate fraction of its former value, and our next loop
91  * makes the appropriate probabilistic test.
92  *
93  * We have initially K > k > 0. If the loop reduces K to equal k,
94  * the next while-test must fail since p will become exactly zero
95  * (we assume there will not be roundoff error in the division).
96  * (Note: Knuth suggests a "<=" loop condition, but we use "<" just
97  * to be doubly sure about roundoff error.) Therefore K cannot become
98  * less than k, which means that we cannot fail to select enough blocks.
99  *----------
100  */
102  p = 1.0 - (double) k / (double) K;
103  while (V < p)
104  {
105  /* skip */
106  bs->t++;
107  K--; /* keep K == N - t */
108 
109  /* adjust p to be new cutoff point in reduced range */
110  p *= 1.0 - (double) k / (double) K;
111  }
112 
113  /* select */
114  bs->m++;
115  return bs->t++;
116 }
bool BlockSampler_HasMore(BlockSampler bs)
Definition: sampling.c:58
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:242
uint32 BlockNumber
Definition: block.h:31
BlockNumber t
Definition: sampling.h:33
#define K(t)
Definition: sha1.c:48
#define Assert(condition)
Definition: c.h:739
BlockNumber N
Definition: sampling.h:31
SamplerRandomState randstate
Definition: sampling.h:35

◆ reservoir_get_next_S()

double reservoir_get_next_S ( ReservoirState  rs,
double  t,
int  n 
)

Definition at line 146 of file sampling.c.

References ReservoirStateData::randstate, S, sampler_random_fract(), ReservoirStateData::W, and W.

Referenced by acquire_sample_rows(), analyze_row_processor(), anl_get_next_S(), and file_acquire_sample_rows().

147 {
148  double S;
149 
150  /* The magic constant here is T from Vitter's paper */
151  if (t <= (22.0 * n))
152  {
153  /* Process records using Algorithm X until t is large enough */
154  double V,
155  quot;
156 
157  V = sampler_random_fract(rs->randstate); /* Generate V */
158  S = 0;
159  t += 1;
160  /* Note: "num" in Vitter's code is always equal to t - n */
161  quot = (t - (double) n) / t;
162  /* Find min S satisfying (4.1) */
163  while (quot > V)
164  {
165  S += 1;
166  t += 1;
167  quot *= (t - (double) n) / t;
168  }
169  }
170  else
171  {
172  /* Now apply Algorithm Z */
173  double W = rs->W;
174  double term = t - (double) n + 1;
175 
176  for (;;)
177  {
178  double numer,
179  numer_lim,
180  denom;
181  double U,
182  X,
183  lhs,
184  rhs,
185  y,
186  tmp;
187 
188  /* Generate U and X */
190  X = t * (W - 1.0);
191  S = floor(X); /* S is tentatively set to floor(X) */
192  /* Test if U <= h(S)/cg(X) in the manner of (6.3) */
193  tmp = (t + 1) / term;
194  lhs = exp(log(((U * tmp * tmp) * (term + S)) / (t + X)) / n);
195  rhs = (((t + X) / (term + S)) * term) / t;
196  if (lhs <= rhs)
197  {
198  W = rhs / lhs;
199  break;
200  }
201  /* Test if U <= f(S)/cg(X) */
202  y = (((U * (t + 1)) / term) * (t + S + 1)) / (t + X);
203  if ((double) n < S)
204  {
205  denom = t;
206  numer_lim = term + S;
207  }
208  else
209  {
210  denom = t - (double) n + S;
211  numer_lim = t + 1;
212  }
213  for (numer = t + S; numer >= numer_lim; numer -= 1)
214  {
215  y *= numer / denom;
216  denom -= 1;
217  }
218  W = exp(-log(sampler_random_fract(rs->randstate)) / n); /* Generate W in advance */
219  if (exp(log(y) / n) <= (t + X) / t)
220  break;
221  }
222  rs->W = W;
223  }
224  return S;
225 }
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:242
#define W(n)
Definition: sha1.c:60
#define S(n, x)
Definition: sha1.c:55
SamplerRandomState randstate
Definition: sampling.h:50

◆ reservoir_init_selection_state()

void reservoir_init_selection_state ( ReservoirState  rs,
int  n 
)

Definition at line 133 of file sampling.c.

References random(), ReservoirStateData::randstate, sampler_random_fract(), sampler_random_init_state(), and ReservoirStateData::W.

Referenced by acquire_sample_rows(), file_acquire_sample_rows(), and postgresAcquireSampleRowsFunc().

134 {
135  /*
136  * Reservoir sampling is not used anywhere where it would need to return
137  * repeatable results so we can initialize it randomly.
138  */
140 
141  /* Initial value of W (for use when Algorithm Z is first applied) */
142  rs->W = exp(-log(sampler_random_fract(rs->randstate)) / n);
143 }
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:233
long random(void)
Definition: random.c:22
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:242
SamplerRandomState randstate
Definition: sampling.h:50

◆ sampler_random_fract()

double sampler_random_fract ( SamplerRandomState  randstate)

Definition at line 242 of file sampling.c.

References pg_erand48().

Referenced by acquire_sample_rows(), analyze_row_processor(), anl_init_selection_state(), anl_random_fract(), BlockSampler_Next(), file_acquire_sample_rows(), random_relative_prime(), reservoir_get_next_S(), reservoir_init_selection_state(), system_rows_nextsampleblock(), and system_time_nextsampleblock().

243 {
244  double res;
245 
246  /* pg_erand48 returns a value in [0.0 - 1.0), so we must reject 0 */
247  do
248  {
249  res = pg_erand48(randstate);
250  } while (res == 0.0);
251  return res;
252 }
double pg_erand48(unsigned short xseed[3])
Definition: erand48.c:88

◆ sampler_random_init_state()

void sampler_random_init_state ( long  seed,
SamplerRandomState  randstate 
)

Definition at line 233 of file sampling.c.

Referenced by anl_init_selection_state(), anl_random_fract(), BlockSampler_Init(), reservoir_init_selection_state(), system_rows_nextsampleblock(), and system_time_nextsampleblock().

234 {
235  randstate[0] = 0x330e; /* same as pg_erand48, but could be anything */
236  randstate[1] = (unsigned short) seed;
237  randstate[2] = (unsigned short) (seed >> 16);
238 }