LCOV - code coverage report
Current view: top level - src/backend/utils/adt - pseudorandomfuncs.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 93.5 % 93 87
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 10 10
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 60.9 % 64 39

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pseudorandomfuncs.c
       4                 :             :  *        Functions giving SQL access to a pseudorandom number generator.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *        src/backend/utils/adt/pseudorandomfuncs.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : #include "postgres.h"
      15                 :             : 
      16                 :             : #include <math.h>
      17                 :             : 
      18                 :             : #include "common/pg_prng.h"
      19                 :             : #include "miscadmin.h"
      20                 :             : #include "utils/date.h"
      21                 :             : #include "utils/fmgrprotos.h"
      22                 :             : #include "utils/numeric.h"
      23                 :             : #include "utils/timestamp.h"
      24                 :             : 
      25                 :             : /* Shared PRNG state used by all the random functions */
      26                 :             : static pg_prng_state prng_state;
      27                 :             : static bool prng_seed_set = false;
      28                 :             : 
      29                 :             : /*
      30                 :             :  * Macro for checking the range bounds of random(min, max) functions. Throws
      31                 :             :  * an error if they're the wrong way round.
      32                 :             :  */
      33                 :             : #define CHECK_RANGE_BOUNDS(rmin, rmax) \
      34                 :             :         do { \
      35                 :             :                 if ((rmin) > (rmax)) \
      36                 :             :                         ereport(ERROR, \
      37                 :             :                                         errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
      38                 :             :                                         errmsg("lower bound must be less than or equal to upper bound")); \
      39                 :             :         } while (0)
      40                 :             : 
      41                 :             : /*
      42                 :             :  * initialize_prng() -
      43                 :             :  *
      44                 :             :  *      Initialize (seed) the PRNG, if not done yet in this process.
      45                 :             :  */
      46                 :             : static void
      47                 :       54470 : initialize_prng(void)
      48                 :             : {
      49         [ +  + ]:       54470 :         if (unlikely(!prng_seed_set))
      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                 :             :                  */
      56   [ +  -  +  - ]:          24 :                 if (unlikely(!pg_prng_strong_seed(&prng_state)))
      57                 :             :                 {
      58                 :           0 :                         TimestampTz now = GetCurrentTimestamp();
      59                 :           0 :                         uint64          iseed;
      60                 :             : 
      61                 :             :                         /* Mix the PID with the most predictable bits of the timestamp */
      62                 :           0 :                         iseed = (uint64) now ^ ((uint64) MyProcPid << 32);
      63                 :           0 :                         pg_prng_seed(&prng_state, iseed);
      64                 :           0 :                 }
      65                 :          24 :                 prng_seed_set = true;
      66                 :          24 :         }
      67                 :       54470 : }
      68                 :             : 
      69                 :             : /*
      70                 :             :  * setseed() -
      71                 :             :  *
      72                 :             :  *      Seed the PRNG from a specified value in the range [-1.0, 1.0].
      73                 :             :  */
      74                 :             : Datum
      75                 :           1 : setseed(PG_FUNCTION_ARGS)
      76                 :             : {
      77                 :           1 :         float8          seed = PG_GETARG_FLOAT8(0);
      78                 :             : 
      79   [ +  -  -  +  :           1 :         if (seed < -1 || seed > 1 || isnan(seed))
                   +  - ]
      80   [ #  #  #  # ]:           0 :                 ereport(ERROR,
      81                 :             :                                 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
      82                 :             :                                 errmsg("setseed parameter %g is out of allowed range [-1,1]",
      83                 :             :                                            seed));
      84                 :             : 
      85                 :           1 :         pg_prng_fseed(&prng_state, seed);
      86                 :           1 :         prng_seed_set = true;
      87                 :             : 
      88                 :           1 :         PG_RETURN_VOID();
      89                 :           1 : }
      90                 :             : 
      91                 :             : /*
      92                 :             :  * drandom() -
      93                 :             :  *
      94                 :             :  *      Returns a random number chosen uniformly in the range [0.0, 1.0).
      95                 :             :  */
      96                 :             : Datum
      97                 :       28628 : drandom(PG_FUNCTION_ARGS)
      98                 :             : {
      99                 :       28628 :         float8          result;
     100                 :             : 
     101                 :       28628 :         initialize_prng();
     102                 :             : 
     103                 :             :         /* pg_prng_double produces desired result range [0.0, 1.0) */
     104                 :       28628 :         result = pg_prng_double(&prng_state);
     105                 :             : 
     106                 :       57256 :         PG_RETURN_FLOAT8(result);
     107                 :       28628 : }
     108                 :             : 
     109                 :             : /*
     110                 :             :  * drandom_normal() -
     111                 :             :  *
     112                 :             :  *      Returns a random number from a normal distribution.
     113                 :             :  */
     114                 :             : Datum
     115                 :        4220 : drandom_normal(PG_FUNCTION_ARGS)
     116                 :             : {
     117                 :        4220 :         float8          mean = PG_GETARG_FLOAT8(0);
     118                 :        4220 :         float8          stddev = PG_GETARG_FLOAT8(1);
     119                 :        4220 :         float8          result,
     120                 :             :                                 z;
     121                 :             : 
     122                 :        4220 :         initialize_prng();
     123                 :             : 
     124                 :             :         /* Get random value from standard normal(mean = 0.0, stddev = 1.0) */
     125                 :        4220 :         z = pg_prng_double_normal(&prng_state);
     126                 :             :         /* Transform the normal standard variable (z) */
     127                 :             :         /* using the target normal distribution parameters */
     128                 :        4220 :         result = (stddev * z) + mean;
     129                 :             : 
     130                 :        8440 :         PG_RETURN_FLOAT8(result);
     131                 :        4220 : }
     132                 :             : 
     133                 :             : /*
     134                 :             :  * int4random() -
     135                 :             :  *
     136                 :             :  *      Returns a random 32-bit integer chosen uniformly in the specified range.
     137                 :             :  */
     138                 :             : Datum
     139                 :        8524 : int4random(PG_FUNCTION_ARGS)
     140                 :             : {
     141                 :        8524 :         int32           rmin = PG_GETARG_INT32(0);
     142                 :        8524 :         int32           rmax = PG_GETARG_INT32(1);
     143                 :        8524 :         int32           result;
     144                 :             : 
     145   [ +  +  +  -  :        8524 :         CHECK_RANGE_BOUNDS(rmin, rmax);
                   +  - ]
     146                 :             : 
     147                 :        8523 :         initialize_prng();
     148                 :             : 
     149                 :        8523 :         result = (int32) pg_prng_int64_range(&prng_state, rmin, rmax);
     150                 :             : 
     151                 :       17046 :         PG_RETURN_INT32(result);
     152                 :        8523 : }
     153                 :             : 
     154                 :             : /*
     155                 :             :  * int8random() -
     156                 :             :  *
     157                 :             :  *      Returns a random 64-bit integer chosen uniformly in the specified range.
     158                 :             :  */
     159                 :             : Datum
     160                 :        7512 : int8random(PG_FUNCTION_ARGS)
     161                 :             : {
     162                 :        7512 :         int64           rmin = PG_GETARG_INT64(0);
     163                 :        7512 :         int64           rmax = PG_GETARG_INT64(1);
     164                 :        7512 :         int64           result;
     165                 :             : 
     166   [ +  +  +  -  :        7512 :         CHECK_RANGE_BOUNDS(rmin, rmax);
                   +  - ]
     167                 :             : 
     168                 :        7511 :         initialize_prng();
     169                 :             : 
     170                 :        7511 :         result = pg_prng_int64_range(&prng_state, rmin, rmax);
     171                 :             : 
     172                 :       15022 :         PG_RETURN_INT64(result);
     173                 :        7511 : }
     174                 :             : 
     175                 :             : /*
     176                 :             :  * numeric_random() -
     177                 :             :  *
     178                 :             :  *      Returns a random numeric value chosen uniformly in the specified range.
     179                 :             :  */
     180                 :             : Datum
     181                 :        5577 : numeric_random(PG_FUNCTION_ARGS)
     182                 :             : {
     183                 :        5577 :         Numeric         rmin = PG_GETARG_NUMERIC(0);
     184                 :        5577 :         Numeric         rmax = PG_GETARG_NUMERIC(1);
     185                 :        5577 :         Numeric         result;
     186                 :             : 
     187                 :             :         /* Leave range bound checking to random_numeric() */
     188                 :             : 
     189                 :        5577 :         initialize_prng();
     190                 :             : 
     191                 :        5577 :         result = random_numeric(&prng_state, rmin, rmax);
     192                 :             : 
     193                 :       11154 :         PG_RETURN_NUMERIC(result);
     194                 :        5577 : }
     195                 :             : 
     196                 :             : 
     197                 :             : /*
     198                 :             :  * date_random() -
     199                 :             :  *
     200                 :             :  *      Returns a random date chosen uniformly in the specified range.
     201                 :             :  */
     202                 :             : Datum
     203                 :           6 : date_random(PG_FUNCTION_ARGS)
     204                 :             : {
     205                 :           6 :         int32           rmin = (int32) PG_GETARG_DATEADT(0);
     206                 :           6 :         int32           rmax = (int32) PG_GETARG_DATEADT(1);
     207                 :           6 :         DateADT         result;
     208                 :             : 
     209   [ +  +  +  -  :           6 :         CHECK_RANGE_BOUNDS(rmin, rmax);
                   +  - ]
     210                 :             : 
     211         [ +  + ]:           5 :         if (DATE_IS_NOBEGIN(rmin) || DATE_IS_NOEND(rmax))
     212   [ +  -  +  - ]:           2 :                 ereport(ERROR,
     213                 :             :                                 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     214                 :             :                                 errmsg("lower and upper bounds must be finite"));
     215                 :             : 
     216                 :           3 :         initialize_prng();
     217                 :             : 
     218                 :           3 :         result = (DateADT) pg_prng_int64_range(&prng_state, rmin, rmax);
     219                 :             : 
     220                 :           6 :         PG_RETURN_DATEADT(result);
     221                 :           3 : }
     222                 :             : 
     223                 :             : /*
     224                 :             :  * timestamp_random() -
     225                 :             :  *
     226                 :             :  *      Returns a random timestamp chosen uniformly in the specified range.
     227                 :             :  */
     228                 :             : Datum
     229                 :           7 : timestamp_random(PG_FUNCTION_ARGS)
     230                 :             : {
     231                 :           7 :         int64           rmin = (int64) PG_GETARG_TIMESTAMP(0);
     232                 :           7 :         int64           rmax = (int64) PG_GETARG_TIMESTAMP(1);
     233                 :           7 :         Timestamp       result;
     234                 :             : 
     235   [ +  +  +  -  :           7 :         CHECK_RANGE_BOUNDS(rmin, rmax);
                   +  - ]
     236                 :             : 
     237         [ +  + ]:           6 :         if (TIMESTAMP_IS_NOBEGIN(rmin) || TIMESTAMP_IS_NOEND(rmax))
     238   [ +  -  +  - ]:           2 :                 ereport(ERROR,
     239                 :             :                                 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     240                 :             :                                 errmsg("lower and upper bounds must be finite"));
     241                 :             : 
     242                 :           4 :         initialize_prng();
     243                 :             : 
     244                 :           4 :         result = (Timestamp) pg_prng_int64_range(&prng_state, rmin, rmax);
     245                 :             : 
     246                 :           8 :         PG_RETURN_TIMESTAMP(result);
     247                 :           4 : }
     248                 :             : 
     249                 :             : /*
     250                 :             :  * timestamptz_random() -
     251                 :             :  *
     252                 :             :  *      Returns a random timestamptz chosen uniformly in the specified range.
     253                 :             :  */
     254                 :             : Datum
     255                 :           7 : timestamptz_random(PG_FUNCTION_ARGS)
     256                 :             : {
     257                 :           7 :         int64           rmin = (int64) PG_GETARG_TIMESTAMPTZ(0);
     258                 :           7 :         int64           rmax = (int64) PG_GETARG_TIMESTAMPTZ(1);
     259                 :           7 :         TimestampTz result;
     260                 :             : 
     261   [ +  +  +  -  :           7 :         CHECK_RANGE_BOUNDS(rmin, rmax);
                   +  - ]
     262                 :             : 
     263         [ +  + ]:           6 :         if (TIMESTAMP_IS_NOBEGIN(rmin) || TIMESTAMP_IS_NOEND(rmax))
     264   [ +  -  +  - ]:           2 :                 ereport(ERROR,
     265                 :             :                                 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     266                 :             :                                 errmsg("lower and upper bounds must be finite"));
     267                 :             : 
     268                 :           4 :         initialize_prng();
     269                 :             : 
     270                 :           4 :         result = (TimestampTz) pg_prng_int64_range(&prng_state, rmin, rmax);
     271                 :             : 
     272                 :           8 :         PG_RETURN_TIMESTAMPTZ(result);
     273                 :           4 : }
        

Generated by: LCOV version 2.3.2-1