aboutsummaryrefslogtreecommitdiff
path: root/src/backend/postmaster/postmaster.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/postmaster/postmaster.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/postmaster/postmaster.c')
-rw-r--r--src/backend/postmaster/postmaster.c60
1 files changed, 29 insertions, 31 deletions
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 41de140ae01..688f462e7d0 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -129,6 +129,7 @@
#include "utils/pidfile.h"
#include "utils/ps_status.h"
#include "utils/timeout.h"
+#include "utils/timestamp.h"
#include "utils/varlena.h"
#ifdef EXEC_BACKEND
@@ -581,9 +582,9 @@ PostmasterMain(int argc, char *argv[])
int i;
char *output_config_variable = NULL;
- MyProcPid = PostmasterPid = getpid();
+ InitProcessGlobals();
- MyStartTime = time(NULL);
+ PostmasterPid = MyProcPid;
IsPostmasterEnvironment = true;
@@ -598,16 +599,6 @@ PostmasterMain(int argc, char *argv[])
umask(PG_MODE_MASK_OWNER);
/*
- * Initialize random(3) so we don't get the same values in every run.
- *
- * Note: the seed is pretty predictable from externally-visible facts such
- * as postmaster start time, so avoid using random() for security-critical
- * random values during postmaster startup. At the time of first
- * connection, PostmasterRandom will select a hopefully-more-random seed.
- */
- srandom((unsigned int) (MyProcPid ^ MyStartTime));
-
- /*
* By default, palloc() requests in the postmaster will be allocated in
* the PostmasterContext, which is space that can be recycled by backends.
* Allocated data that needs to be available to backends should be
@@ -2514,6 +2505,32 @@ ClosePostmasterPorts(bool am_syslogger)
/*
+ * InitProcessGlobals -- set MyProcPid, MyStartTime[stamp], random seeds
+ *
+ * Called early in every backend.
+ */
+void
+InitProcessGlobals(void)
+{
+ MyProcPid = getpid();
+ MyStartTimestamp = GetCurrentTimestamp();
+ MyStartTime = timestamptz_to_time_t(MyStartTimestamp);
+
+ /*
+ * Don't want backend to be able to see the postmaster random number
+ * generator state. We have to clobber the static random_seed.
+ */
+#ifndef HAVE_STRONG_RANDOM
+ random_seed = 0;
+ random_start_time.tv_usec = 0;
+#endif
+
+ /* Set a different seed for random() in every backend. */
+ srandom((unsigned int) MyProcPid ^ (unsigned int) MyStartTimestamp);
+}
+
+
+/*
* reset_shared -- reset shared memory and semaphores
*/
static void
@@ -4154,10 +4171,6 @@ BackendInitialize(Port *port)
/* This flag will remain set until InitPostgres finishes authentication */
ClientAuthInProgress = true; /* limit visibility of log messages */
- /* save process start time */
- port->SessionStartTime = GetCurrentTimestamp();
- MyStartTime = timestamptz_to_time_t(port->SessionStartTime);
-
/* set these to empty in case they are needed before we set them up */
port->remote_host = "";
port->remote_port = "";
@@ -4315,24 +4328,9 @@ BackendRun(Port *port)
char **av;
int maxac;
int ac;
- long secs;
- int usecs;
int i;
/*
- * Don't want backend to be able to see the postmaster random number
- * generator state. We have to clobber the static random_seed *and* start
- * a new random sequence in the random() library function.
- */
-#ifndef HAVE_STRONG_RANDOM
- random_seed = 0;
- random_start_time.tv_usec = 0;
-#endif
- /* slightly hacky way to convert timestamptz into integers */
- TimestampDifference(0, port->SessionStartTime, &secs, &usecs);
- srandom((unsigned int) (MyProcPid ^ (usecs << 12) ^ secs));
-
- /*
* Now, build the argv vector that will be given to PostgresMain.
*
* The maximum possible number of commandline arguments that could come