aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/init/miscinit.c
diff options
context:
space:
mode:
authorThomas Munro <tmunro@postgresql.org>2018-10-19 13:59:14 +1300
committerThomas Munro <tmunro@postgresql.org>2018-10-19 13:59:28 +1300
commit197e4af9d5da180190a0f2be851b095dba57d9cd (patch)
tree4121b3b50a5f020f982eb4cbbe754420d200ff31 /src/backend/utils/init/miscinit.c
parente74dd00f53cd6dc1887f76b9672e5f6dcf0fd8a2 (diff)
downloadpostgresql-197e4af9d5da180190a0f2be851b095dba57d9cd.tar.gz
postgresql-197e4af9d5da180190a0f2be851b095dba57d9cd.zip
Refactor pid, random seed and start time initialization.
Background workers, including parallel workers, were generating the same sequence of numbers in random(). This showed up as DSM handle collisions when Parallel Hash created multiple segments, but any code that calls random() in background workers could be affected if it cares about different backends generating different numbers. Repair by making sure that all new processes initialize the seed at the same time as they set MyProcPid and MyStartTime in a new function InitProcessGlobals(), called by the postmaster, its children and also standalone processes. Also add a new high resolution MyStartTimestamp as a potentially useful by-product, and remove SessionStartTime from struct Port as it is now redundant. No back-patch for now, as the known consequences so far are just a bunch of harmless shm_open(O_EXCL) collisions. Author: Thomas Munro Reviewed-by: Tom Lane Discussion: https://postgr.es/m/CAEepm%3D2eJj_6%3DB%2B2tEpGu2nf1BjthCf9nXXUouYvJJ4C5WSwhg%40mail.gmail.com
Diffstat (limited to 'src/backend/utils/init/miscinit.c')
-rw-r--r--src/backend/utils/init/miscinit.c16
1 files changed, 2 insertions, 14 deletions
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index f590ecb25e8..238fe1deec8 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -274,9 +274,7 @@ InitPostmasterChild(void)
{
IsUnderPostmaster = true; /* we are a postmaster subprocess now */
- MyProcPid = getpid(); /* reset MyProcPid */
-
- MyStartTime = time(NULL); /* set our start time in case we call elog */
+ InitProcessGlobals();
/*
* make sure stderr is in binary mode before anything can possibly be
@@ -321,17 +319,7 @@ InitStandaloneProcess(const char *argv0)
{
Assert(!IsPostmasterEnvironment);
- MyProcPid = getpid(); /* reset MyProcPid */
-
- 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));
+ InitProcessGlobals();
/* Initialize process-local latch support */
InitializeLatchSupport();