Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
pseudorandomfuncs.c File Reference
#include "postgres.h"
#include <math.h>
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "utils/date.h"
#include "utils/fmgrprotos.h"
#include "utils/numeric.h"
#include "utils/timestamp.h"
Include dependency graph for pseudorandomfuncs.c:

Go to the source code of this file.

Macros

#define CHECK_RANGE_BOUNDS(rmin, rmax)
 

Functions

static void initialize_prng (void)
 
Datum setseed (PG_FUNCTION_ARGS)
 
Datum drandom (PG_FUNCTION_ARGS)
 
Datum drandom_normal (PG_FUNCTION_ARGS)
 
Datum int4random (PG_FUNCTION_ARGS)
 
Datum int8random (PG_FUNCTION_ARGS)
 
Datum numeric_random (PG_FUNCTION_ARGS)
 
Datum date_random (PG_FUNCTION_ARGS)
 
Datum timestamp_random (PG_FUNCTION_ARGS)
 
Datum timestamptz_random (PG_FUNCTION_ARGS)
 

Variables

static pg_prng_state prng_state
 
static bool prng_seed_set = false
 

Macro Definition Documentation

◆ CHECK_RANGE_BOUNDS

#define CHECK_RANGE_BOUNDS (   rmin,
  rmax 
)
Value:
do { \
if ((rmin) > (rmax)) \
ereport(ERROR, \
errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
errmsg("lower bound must be less than or equal to upper bound")); \
} while (0)
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39

Definition at line 33 of file pseudorandomfuncs.c.

Function Documentation

◆ date_random()

Datum date_random ( PG_FUNCTION_ARGS  )

Definition at line 203 of file pseudorandomfuncs.c.

204{
205 int32 rmin = (int32) PG_GETARG_DATEADT(0);
206 int32 rmax = (int32) PG_GETARG_DATEADT(1);
207 DateADT result;
208
209 CHECK_RANGE_BOUNDS(rmin, rmax);
210
211 if (DATE_IS_NOBEGIN(rmin) || DATE_IS_NOEND(rmax))
213 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
214 errmsg("lower and upper bounds must be finite"));
215
217
218 result = (DateADT) pg_prng_int64_range(&prng_state, rmin, rmax);
219
220 PG_RETURN_DATEADT(result);
221}
int32_t int32
Definition: c.h:535
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define PG_RETURN_DATEADT(x)
Definition: date.h:93
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:89
#define ereport(elevel,...)
Definition: elog.h:150
int64 pg_prng_int64_range(pg_prng_state *state, int64 rmin, int64 rmax)
Definition: pg_prng.c:192
static void initialize_prng(void)
static pg_prng_state prng_state
#define CHECK_RANGE_BOUNDS(rmin, rmax)

References CHECK_RANGE_BOUNDS, DATE_IS_NOBEGIN, DATE_IS_NOEND, ereport, errcode(), errmsg(), ERROR, initialize_prng(), PG_GETARG_DATEADT, pg_prng_int64_range(), PG_RETURN_DATEADT, and prng_state.

◆ drandom()

Datum drandom ( PG_FUNCTION_ARGS  )

Definition at line 97 of file pseudorandomfuncs.c.

98{
99 float8 result;
100
102
103 /* pg_prng_double produces desired result range [0.0, 1.0) */
104 result = pg_prng_double(&prng_state);
105
106 PG_RETURN_FLOAT8(result);
107}
double float8
Definition: c.h:636
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
double pg_prng_double(pg_prng_state *state)
Definition: pg_prng.c:268

References initialize_prng(), pg_prng_double(), PG_RETURN_FLOAT8, and prng_state.

◆ drandom_normal()

Datum drandom_normal ( PG_FUNCTION_ARGS  )

Definition at line 115 of file pseudorandomfuncs.c.

116{
117 float8 mean = PG_GETARG_FLOAT8(0);
118 float8 stddev = PG_GETARG_FLOAT8(1);
119 float8 result,
120 z;
121
123
124 /* Get random value from standard normal(mean = 0.0, stddev = 1.0) */
126 /* Transform the normal standard variable (z) */
127 /* using the target normal distribution parameters */
128 result = (stddev * z) + mean;
129
130 PG_RETURN_FLOAT8(result);
131}
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
double pg_prng_double_normal(pg_prng_state *state)
Definition: pg_prng.c:290

References initialize_prng(), PG_GETARG_FLOAT8, pg_prng_double_normal(), PG_RETURN_FLOAT8, and prng_state.

◆ initialize_prng()

static void initialize_prng ( void  )
static

Definition at line 47 of file pseudorandomfuncs.c.

48{
50 {
51 /*
52 * If possible, seed the PRNG using high-quality random bits. Should
53 * that fail for some reason, we fall back on a lower-quality seed
54 * based on current time and PID.
55 */
57 {
59 uint64 iseed;
60
61 /* Mix the PID with the most predictable bits of the timestamp */
62 iseed = (uint64) now ^ ((uint64) MyProcPid << 32);
63 pg_prng_seed(&prng_state, iseed);
64 }
65 prng_seed_set = true;
66 }
67}
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
uint64_t uint64
Definition: c.h:540
#define unlikely(x)
Definition: c.h:403
int64 TimestampTz
Definition: timestamp.h:39
int MyProcPid
Definition: globals.c:47
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:89
#define pg_prng_strong_seed(state)
Definition: pg_prng.h:46
static bool prng_seed_set

References GetCurrentTimestamp(), MyProcPid, now(), pg_prng_seed(), pg_prng_strong_seed, prng_seed_set, prng_state, and unlikely.

Referenced by date_random(), drandom(), drandom_normal(), int4random(), int8random(), numeric_random(), timestamp_random(), and timestamptz_random().

◆ int4random()

Datum int4random ( PG_FUNCTION_ARGS  )

Definition at line 139 of file pseudorandomfuncs.c.

140{
141 int32 rmin = PG_GETARG_INT32(0);
142 int32 rmax = PG_GETARG_INT32(1);
143 int32 result;
144
145 CHECK_RANGE_BOUNDS(rmin, rmax);
146
148
149 result = (int32) pg_prng_int64_range(&prng_state, rmin, rmax);
150
151 PG_RETURN_INT32(result);
152}
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269

References CHECK_RANGE_BOUNDS, initialize_prng(), PG_GETARG_INT32, pg_prng_int64_range(), PG_RETURN_INT32, and prng_state.

◆ int8random()

Datum int8random ( PG_FUNCTION_ARGS  )

Definition at line 160 of file pseudorandomfuncs.c.

161{
162 int64 rmin = PG_GETARG_INT64(0);
163 int64 rmax = PG_GETARG_INT64(1);
164 int64 result;
165
166 CHECK_RANGE_BOUNDS(rmin, rmax);
167
169
170 result = pg_prng_int64_range(&prng_state, rmin, rmax);
171
172 PG_RETURN_INT64(result);
173}
int64_t int64
Definition: c.h:536
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283

References CHECK_RANGE_BOUNDS, initialize_prng(), PG_GETARG_INT64, pg_prng_int64_range(), PG_RETURN_INT64, and prng_state.

◆ numeric_random()

Datum numeric_random ( PG_FUNCTION_ARGS  )

Definition at line 181 of file pseudorandomfuncs.c.

182{
183 Numeric rmin = PG_GETARG_NUMERIC(0);
184 Numeric rmax = PG_GETARG_NUMERIC(1);
185 Numeric result;
186
187 /* Leave range bound checking to random_numeric() */
188
190
191 result = random_numeric(&prng_state, rmin, rmax);
192
193 PG_RETURN_NUMERIC(result);
194}
Numeric random_numeric(pg_prng_state *state, Numeric rmin, Numeric rmax)
Definition: numeric.c:4205
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:81
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:83

References initialize_prng(), PG_GETARG_NUMERIC, PG_RETURN_NUMERIC, prng_state, and random_numeric().

◆ setseed()

Datum setseed ( PG_FUNCTION_ARGS  )

Definition at line 75 of file pseudorandomfuncs.c.

76{
77 float8 seed = PG_GETARG_FLOAT8(0);
78
79 if (seed < -1 || seed > 1 || isnan(seed))
81 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
82 errmsg("setseed parameter %g is out of allowed range [-1,1]",
83 seed));
84
86 prng_seed_set = true;
87
89}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pg_prng_fseed(pg_prng_state *state, double fseed)
Definition: pg_prng.c:102

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_FLOAT8, pg_prng_fseed(), PG_RETURN_VOID, prng_seed_set, and prng_state.

Referenced by assign_random_seed().

◆ timestamp_random()

Datum timestamp_random ( PG_FUNCTION_ARGS  )

Definition at line 229 of file pseudorandomfuncs.c.

230{
231 int64 rmin = (int64) PG_GETARG_TIMESTAMP(0);
232 int64 rmax = (int64) PG_GETARG_TIMESTAMP(1);
233 Timestamp result;
234
235 CHECK_RANGE_BOUNDS(rmin, rmax);
236
237 if (TIMESTAMP_IS_NOBEGIN(rmin) || TIMESTAMP_IS_NOEND(rmax))
239 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
240 errmsg("lower and upper bounds must be finite"));
241
243
244 result = (Timestamp) pg_prng_int64_range(&prng_state, rmin, rmax);
245
246 PG_RETURN_TIMESTAMP(result);
247}
int64 Timestamp
Definition: timestamp.h:38
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:167
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:162
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:63
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:67

References CHECK_RANGE_BOUNDS, ereport, errcode(), errmsg(), ERROR, initialize_prng(), PG_GETARG_TIMESTAMP, pg_prng_int64_range(), PG_RETURN_TIMESTAMP, prng_state, TIMESTAMP_IS_NOBEGIN, and TIMESTAMP_IS_NOEND.

◆ timestamptz_random()

Datum timestamptz_random ( PG_FUNCTION_ARGS  )

Definition at line 255 of file pseudorandomfuncs.c.

256{
259 TimestampTz result;
260
261 CHECK_RANGE_BOUNDS(rmin, rmax);
262
263 if (TIMESTAMP_IS_NOBEGIN(rmin) || TIMESTAMP_IS_NOEND(rmax))
265 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
266 errmsg("lower and upper bounds must be finite"));
267
269
270 result = (TimestampTz) pg_prng_int64_range(&prng_state, rmin, rmax);
271
272 PG_RETURN_TIMESTAMPTZ(result);
273}
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:64
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68

References CHECK_RANGE_BOUNDS, ereport, errcode(), errmsg(), ERROR, initialize_prng(), PG_GETARG_TIMESTAMPTZ, pg_prng_int64_range(), PG_RETURN_TIMESTAMPTZ, prng_state, TIMESTAMP_IS_NOBEGIN, and TIMESTAMP_IS_NOEND.

Variable Documentation

◆ prng_seed_set

bool prng_seed_set = false
static

Definition at line 27 of file pseudorandomfuncs.c.

Referenced by initialize_prng(), and setseed().

◆ prng_state