1 /*-------------------------------------------------------------------------
2  *
3  * sampling.h
4  *	  definitions for sampling functions
5  *
6  * Portions Copyright (c) 1996-2017, 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 "storage/block.h"		/* for typedef BlockNumber */
17 
18 
19 /* Random generator for sampling code */
20 typedef unsigned short SamplerRandomState[3];
21 
22 extern void sampler_random_init_state(long seed,
23 						  SamplerRandomState randstate);
24 extern double sampler_random_fract(SamplerRandomState randstate);
25 
26 /* Block sampling methods */
27 
28 /* Data structure for Algorithm S from Knuth 3.4.2 */
29 typedef struct
30 {
31 	BlockNumber N;				/* number of blocks, known in advance */
32 	int			n;				/* desired sample size */
33 	BlockNumber t;				/* current block number */
34 	int			m;				/* blocks selected so far */
35 	SamplerRandomState randstate;	/* random generator state */
36 } BlockSamplerData;
37 
38 typedef BlockSamplerData *BlockSampler;
39 
40 extern void BlockSampler_Init(BlockSampler bs, BlockNumber nblocks,
41 				  int samplesize, long randseed);
42 extern bool BlockSampler_HasMore(BlockSampler bs);
43 extern BlockNumber BlockSampler_Next(BlockSampler bs);
44 
45 /* Reservoir sampling methods */
46 
47 typedef struct
48 {
49 	double		W;
50 	SamplerRandomState randstate;	/* random generator state */
51 } ReservoirStateData;
52 
53 typedef ReservoirStateData *ReservoirState;
54 
55 extern void reservoir_init_selection_state(ReservoirState rs, int n);
56 extern double reservoir_get_next_S(ReservoirState rs, double t, int n);
57 
58 /* Old API, still in use by assorted FDWs */
59 /* For backwards compatibility, these declarations are duplicated in vacuum.h */
60 
61 extern double anl_random_fract(void);
62 extern double anl_init_selection_state(int n);
63 extern double anl_get_next_S(double t, int n, double *stateptr);
64 
65 #endif							/* SAMPLING_H */
66