PostgreSQL Source Code  git master
sampling.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * sampling.h
4  * definitions for sampling functions
5  *
6  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/utils/sampling.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef SAMPLING_H
14 #define SAMPLING_H
15 
16 #include "common/pg_prng.h"
17 #include "storage/block.h" /* for typedef BlockNumber */
18 
19 
20 /* Random generator for sampling code */
21 extern void sampler_random_init_state(uint32 seed,
22  pg_prng_state *randstate);
23 extern double sampler_random_fract(pg_prng_state *randstate);
24 
25 /* Block sampling methods */
26 
27 /* Data structure for Algorithm S from Knuth 3.4.2 */
28 typedef struct
29 {
30  BlockNumber N; /* number of blocks, known in advance */
31  int n; /* desired sample size */
32  BlockNumber t; /* current block number */
33  int m; /* blocks selected so far */
34  pg_prng_state randstate; /* random generator state */
36 
38 
40  int samplesize, uint32 randseed);
41 extern bool BlockSampler_HasMore(BlockSampler bs);
43 
44 /* Reservoir sampling methods */
45 
46 typedef struct
47 {
48  double W;
49  pg_prng_state randstate; /* random generator state */
51 
53 
54 extern void reservoir_init_selection_state(ReservoirState rs, int n);
55 extern double reservoir_get_next_S(ReservoirState rs, double t, int n);
56 
57 /* Old API, still in use by assorted FDWs */
58 /* For backwards compatibility, these declarations are duplicated in vacuum.h */
59 
60 extern double anl_random_fract(void);
61 extern double anl_init_selection_state(int n);
62 extern double anl_get_next_S(double t, int n, double *stateptr);
63 
64 #endif /* SAMPLING_H */
uint32 BlockNumber
Definition: block.h:31
unsigned int uint32
Definition: c.h:493
BlockNumber BlockSampler_Init(BlockSampler bs, BlockNumber nblocks, int samplesize, uint32 randseed)
Definition: sampling.c:39
void reservoir_init_selection_state(ReservoirState rs, int n)
Definition: sampling.c:133
double anl_get_next_S(double t, int n, double *stateptr)
Definition: sampling.c:296
double sampler_random_fract(pg_prng_state *randstate)
Definition: sampling.c:241
bool BlockSampler_HasMore(BlockSampler bs)
Definition: sampling.c:58
ReservoirStateData * ReservoirState
Definition: sampling.h:52
BlockNumber BlockSampler_Next(BlockSampler bs)
Definition: sampling.c:64
double anl_init_selection_state(int n)
Definition: sampling.c:281
void sampler_random_init_state(uint32 seed, pg_prng_state *randstate)
Definition: sampling.c:234
BlockSamplerData * BlockSampler
Definition: sampling.h:37
double anl_random_fract(void)
Definition: sampling.c:266
double reservoir_get_next_S(ReservoirState rs, double t, int n)
Definition: sampling.c:147
BlockNumber N
Definition: sampling.h:30
pg_prng_state randstate
Definition: sampling.h:34
BlockNumber t
Definition: sampling.h:32
pg_prng_state randstate
Definition: sampling.h:49