postgrespro.git / summary / log / commit / refs
commit d18f6674bd60e923bcdbf0fd916685b0a250c60f
Author: Noah Misch <noah@leadboat.com>
Date: Mon Sep 24 05:56:39 2018 +0000
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/utils/init/miscinit.c | 8 ++++++++
src/bin/initdb/initdb.c | 3 +++
2 files changed, 11 insertions(+)
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index 865119d272..f590ecb25e 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -325,6 +325,14 @@ InitStandaloneProcess(const char *argv0)
MyStartTime = time(NULL); /* set our start time in case we call elog */
+ /*
+ * 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));
+
/* Initialize process-local latch support */
InitializeLatchSupport();
MyLatch = &LocalLatchData;
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index cb8c7450d9..e155c6ae2f 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -923,6 +923,9 @@ choose_dsm_implementation(void)
#ifdef HAVE_SHM_OPEN
int ntries = 10;
+ /* Initialize random(); this function is its only user in this program. */
+ srandom((unsigned int) (getpid() ^ time(NULL)));
+
while (ntries > 0)
{
uint32 handle;
[parent: 73a60051379b]