Re: random() (was Re: New GUC to sample log queries)

Fabien COELHO <coelho@cri.ensmp.fr>

From: Fabien COELHO <coelho@cri.ensmp.fr>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Alvaro Herrera <alvherre@2ndquadrant.com>, Adrien Nayrat <adrien.nayrat@anayrat.info>, Thomas Munro <thomas.munro@enterprisedb.com>, Dmitry Dolgov <9erthalion6@gmail.com>, Tomas Vondra <tomas.vondra@2ndquadrant.com>, vik.fearing@2ndquadrant.com, Robert Haas <robertmhaas@gmail.com>, Michael Paquier <michael@paquier.xyz>, David Rowley <david.rowley@2ndquadrant.com>, Pg Hackers <pgsql-hackers@postgresql.org>
Date: 2018-12-28T08:44:05Z
Lists: pgsql-hackers
> About costs, not counting array accesses:
>
> - lrand48 (48 bits state as 3 uint16)        is 29 ops
>   (10 =, 8 *, 7 +, 4 >>)
> - xorshift+ (128 bits state as 2 uint64)     is 13 ops
>   ( 5 =, 0 *, 1 +, 3 >>, 4 ^)
> - xororshift128+ (idem)                      is 17 ops
>   ( 6 =, 0 *, 1 +, 5 >>, 3 ^, 2 |, less if rot in hardware)
> - WELL512 (512 bits state as 16 uint32)      is 38 ops
>   (11 =, 0 *, 3 +, 7 >>, 10 ^, 4 &)
>   probably much better, but probably slower than the current version
>
> I'd be of the (debatable) opinion that we could use xororshift128+, already 
> used by various languages, even if it fails some specialized tests.

After some more digging, the better choice seems to be the 64 bits 
optimized xoshiro256** (xoshiro = xor shift rotate):

  - xoshiro256** (256 bits states as 4 uint64) is 24 ops (18 if rot in hw)
    8 =, 2 *, 2 +, 5 <<, 5 ^, 2 |

See http://vigna.di.unimi.it/xorshift/

-- 
Fabien.


Commits

  1. Use pg_strong_random() to select each server process's random seed.

  2. Use a separate random seed for SQL random()/setseed() functions.

  3. Marginal performance hacking in erand48.c.

  4. Fix latent problem with pg_jrand48().

  5. Silence compiler warning

  6. Add log_statement_sample_rate parameter