PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)) \
errmsg("lower bound must be less than or equal to upper bound")); \
} while (0)
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
static int fb(int x)

Definition at line 33 of file pseudorandomfuncs.c.

34 { \
35 if ((rmin) > (rmax)) \
38 errmsg("lower bound must be less than or equal to upper bound")); \
39 } while (0)

Function Documentation

◆ date_random()

Datum date_random ( PG_FUNCTION_ARGS  )

Definition at line 203 of file pseudorandomfuncs.c.

204{
207 DateADT result;
208
210
214 errmsg("lower and upper bounds must be finite"));
215
217
219
220 PG_RETURN_DATEADT(result);
221}
int32_t int32
Definition c.h:542
#define DATE_IS_NOEND(j)
Definition date.h:48
#define PG_RETURN_DATEADT(x)
Definition date.h:99
#define DATE_IS_NOBEGIN(j)
Definition date.h:46
int32 DateADT
Definition date.h:21
#define PG_GETARG_DATEADT(n)
Definition date.h:95
#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, fb(), 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:644
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
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:283
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 {
60
61 /* Mix the PID with the most predictable bits of the timestamp */
62 iseed = (uint64) now ^ ((uint64) MyProcPid << 32);
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:547
#define unlikely(x)
Definition c.h:412
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 fb(), 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{
143 int32 result;
144
146
148
150
151 PG_RETURN_INT32(result);
152}
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_INT32(n)
Definition fmgr.h:269

References CHECK_RANGE_BOUNDS, fb(), 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{
164 int64 result;
165
167
169
171
172 PG_RETURN_INT64(result);
173}
int64_t int64
Definition c.h:543
#define PG_RETURN_INT64(x)
Definition fmgr.h:370
#define PG_GETARG_INT64(n)
Definition fmgr.h:284

References CHECK_RANGE_BOUNDS, fb(), 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{
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:4204
#define PG_GETARG_NUMERIC(n)
Definition numeric.h:81
#define PG_RETURN_NUMERIC(x)
Definition numeric.h:83

References fb(), 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))
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:350
void pg_prng_fseed(pg_prng_state *state, double fseed)
Definition pg_prng.c:102

References ereport, errcode(), errmsg(), ERROR, fb(), 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{
233 Timestamp result;
234
236
240 errmsg("lower and upper bounds must be finite"));
241
243
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, fb(), 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  )

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