postgres.git / summary / log / commit / refs
commit 402da7054f34d2c5e3215ca72fc3d08ca2c98090
Author: Noah Misch <noah@leadboat.com>
Date: Mon Sep 24 05:56:39 2018 +0000
First released in: 12.0, 9.3.25, 9.4.20, 9.5.15, 9.6.11, 10.6, 11.0
Initialize random() in bootstrap/stand-alone postgres and in initdb.
This removes a difference between the standard IsUnderPostmaster
execution environment and that of --boot and --single. In a stand-alone
backend, "SELECT random()" always started at the same seed.
On a system capable of using posix shared memory, initdb could still
conclude "selecting dynamic shared memory implementation ... sysv".
Crashed --boot or --single postgres processes orphaned shared memory
objects having names that collided with the not-actually-random names
that initdb probed. The sysv fallback appeared after ten crashes of
--boot or --single postgres. Since --boot and --single are rare in
production use, systems used for PostgreSQL development are the
principal candidate to notice this symptom.
Back-patch to 9.3 (all supported versions). PostgreSQL 9.4 introduced
dynamic shared memory, but 9.3 does share the "SELECT random()" problem.
Reviewed by Tom Lane and Kyotaro HORIGUCHI.
Discussion: https://postgr.es/m/20180915221546.GA3159382@rfd.leadboat.com
src/backend/bootstrap/bootstrap.c | 8 ++++++++
src/backend/tcop/postgres.c | 8 ++++++++
2 files changed, 16 insertions(+)
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 5c0fe9b715b..cded57e045a 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -202,6 +202,14 @@ AuxiliaryProcessMain(int argc, char *argv[])
MyStartTime = time(NULL);
+ /*
+ * Initialize random() for the first time, like PostmasterMain() would.
+ * In a regular IsUnderPostmaster backend, BackendRun() computes a
+ * high-entropy seed before any user query. Fewer distinct initial seeds
+ * can occur here.
+ */
+ srandom((unsigned int) (MyProcPid ^ MyStartTime));
+
/* Compute paths, if we didn't inherit them from postmaster */
if (my_exec_path[0] == '\0')
{
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index cf4d4ec356b..47651c4ceac 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -3620,6 +3620,14 @@ PostgresMain(int argc, char *argv[],
MyProcPid = getpid();
MyStartTime = time(NULL);
+
+ /*
+ * Initialize random() for the first time, like PostmasterMain()
+ * would. In a regular IsUnderPostmaster backend, BackendRun()
+ * computes a high-entropy seed before any user query. Fewer distinct
+ * initial seeds can occur here.
+ */
+ srandom((unsigned int) (MyProcPid ^ MyStartTime));
}
SetProcessingMode(InitProcessing);
[parent: 00011a6ae9c7]