Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 29 Nov 2021 02:32:36 +0000 (21:32 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 29 Nov 2021 02:33:07 +0000 (21:33 -0500)
commit3804539e48e794781c6145c7f988f5d507418fa8
tree317904b43ca8c1d510b23cb8fdd7b05a75e971bc
parentf44ceb46ec2d8da48f6e145bf462d5620c25e079
Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.

Standardize on xoroshiro128** as our basic PRNG algorithm, eliminating
a bunch of platform dependencies as well as fundamentally-obsolete PRNG
code.  In addition, this API replacement will ease replacing the
algorithm again in future, should that become necessary.

xoroshiro128** is a few percent slower than the drand48 family,
but it can produce full-width 64-bit random values not only 48-bit,
and it should be much more trustworthy.  It's likely to be noticeably
faster than the platform's random(), depending on which platform you
are thinking about; and we can have non-global state vectors easily,
unlike with random().  It is not cryptographically strong, but neither
are the functions it replaces.

Fabien Coelho, reviewed by Dean Rasheed, Aleksander Alekseev, and myself

Discussion: https://postgr.es/m/alpine.DEB.2.22.394.2105241211230.165418@pseudo
50 files changed:
configure
configure.ac
contrib/amcheck/verify_nbtree.c
contrib/auto_explain/auto_explain.c
contrib/file_fdw/file_fdw.c
contrib/postgres_fdw/postgres_fdw.c
contrib/tablefunc/tablefunc.c
contrib/tsm_system_rows/tsm_system_rows.c
contrib/tsm_system_time/tsm_system_time.c
src/backend/access/gin/ginget.c
src/backend/access/gist/gistutil.c
src/backend/access/nbtree/nbtinsert.c
src/backend/access/spgist/spgdoinsert.c
src/backend/access/transam/xact.c
src/backend/commands/analyze.c
src/backend/executor/nodeSamplescan.c
src/backend/lib/bloomfilter.c
src/backend/optimizer/geqo/geqo_random.c
src/backend/optimizer/geqo/geqo_selection.c
src/backend/postmaster/postmaster.c
src/backend/storage/file/fd.c
src/backend/storage/ipc/dsm.c
src/backend/storage/lmgr/Makefile
src/backend/storage/lmgr/s_lock.c
src/backend/tcop/postgres.c
src/backend/utils/adt/float.c
src/backend/utils/misc/sampling.c
src/bin/initdb/initdb.c
src/bin/pg_test_fsync/pg_test_fsync.c
src/bin/pgbench/pgbench.c
src/bin/pgbench/t/001_pgbench_with_server.pl
src/common/Makefile
src/common/pg_prng.c [new file with mode: 0644]
src/include/common/pg_prng.h [new file with mode: 0644]
src/include/optimizer/geqo.h
src/include/optimizer/geqo_random.h
src/include/pg_config.h.in
src/include/pg_config_manual.h
src/include/port.h
src/include/utils/sampling.h
src/port/Makefile
src/port/erand48.c [deleted file]
src/port/random.c [deleted file]
src/port/srandom.c [deleted file]
src/test/modules/test_bloomfilter/test_bloomfilter.c
src/test/modules/test_integerset/test_integerset.c
src/test/modules/test_rbtree/test_rbtree.c
src/tools/msvc/Mkvcbuild.pm
src/tools/msvc/Solution.pm
src/tools/testint128.c