Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.

Tom Lane <tgl@sss.pgh.pa.us>

Commit: 3804539e48e794781c6145c7f988f5d507418fa8
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: 2021-11-29T02:33:07Z
Releases: 15.0
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

Files

PathChange+/−
configure modified +0 −26
configure.ac modified +0 −2
contrib/amcheck/verify_nbtree.c modified +3 −2
contrib/auto_explain/auto_explain.c modified +2 −2
contrib/file_fdw/file_fdw.c modified +1 −1
contrib/postgres_fdw/postgres_fdw.c modified +1 −1
contrib/tablefunc/tablefunc.c modified +3 −2
contrib/tsm_system_rows/tsm_system_rows.c modified +6 −6
contrib/tsm_system_time/tsm_system_time.c modified +6 −6
src/backend/access/gin/ginget.c modified +2 −1
src/backend/access/gist/gistutil.c modified +3 −2
src/backend/access/nbtree/nbtinsert.c modified +2 −1
src/backend/access/spgist/spgdoinsert.c modified +4 −1
src/backend/access/transam/xact.c modified +2 −1
src/backend/commands/analyze.c modified +4 −3
src/backend/executor/nodeSamplescan.c modified +2 −1
src/backend/lib/bloomfilter.c modified +1 −2
src/backend/optimizer/geqo/geqo_random.c modified +14 −9
src/backend/optimizer/geqo/geqo_selection.c modified +5 −9
src/backend/postmaster/postmaster.c modified +15 −5
src/backend/storage/file/fd.c modified +3 −1
src/backend/storage/ipc/dsm.c modified +6 −3
src/backend/storage/lmgr/Makefile modified +3 −2
src/backend/storage/lmgr/s_lock.c modified +3 −2
src/backend/tcop/postgres.c modified +4 −3
src/backend/utils/adt/float.c modified +7 −13
src/backend/utils/misc/sampling.c modified +30 −22
src/bin/initdb/initdb.c modified +5 −3
src/bin/pgbench/pgbench.c modified +46 −76
src/bin/pgbench/t/001_pgbench_with_server.pl modified +16 −7
src/bin/pg_test_fsync/pg_test_fsync.c modified +4 −1
src/common/Makefile modified +1 −0
src/common/pg_prng.c added +247 −0
src/include/common/pg_prng.h added +60 −0
src/include/optimizer/geqo.h modified +2 −1
src/include/optimizer/geqo_random.h modified +2 −3
src/include/pg_config.h.in modified +0 −6
src/include/pg_config_manual.h modified +0 −11
src/include/port.h modified +0 −13
src/include/utils/sampling.h modified +7 −8
src/port/erand48.c deleted +0 −136
src/port/Makefile modified +0 −1
src/port/random.c deleted +0 −25
src/port/srandom.c deleted +0 −25
src/test/modules/test_bloomfilter/test_bloomfilter.c modified +2 −2
src/test/modules/test_integerset/test_integerset.c modified +3 −3
src/test/modules/test_rbtree/test_rbtree.c modified +3 −2
src/tools/msvc/Mkvcbuild.pm modified +6 −6
src/tools/msvc/Solution.pm modified +0 −2
src/tools/testint128.c modified +8 −21

Discussion