void
pgstat_bestart(void)
{
- TimestampTz proc_start_timestamp;
SockAddr clientaddr;
volatile PgBackendStatus *beentry;
/*
* To minimize the time spent modifying the PgBackendStatus entry, fetch
* all the needed data first.
- *
- * If we have a MyProcPort, use its session start time (for consistency,
- * and to save a kernel call).
*/
- if (MyProcPort)
- proc_start_timestamp = MyProcPort->SessionStartTime;
- else
- proc_start_timestamp = GetCurrentTimestamp();
/*
* We may not have a MyProcPort (eg, if this is the autovacuum process).
} while ((beentry->st_changecount & 1) == 0);
beentry->st_procpid = MyProcPid;
- beentry->st_proc_start_timestamp = proc_start_timestamp;
+ beentry->st_proc_start_timestamp = MyStartTimestamp;
beentry->st_activity_start_timestamp = 0;
beentry->st_state_start_timestamp = 0;
beentry->st_xact_start_timestamp = 0;
#include "utils/pidfile.h"
#include "utils/ps_status.h"
#include "utils/timeout.h"
+#include "utils/timestamp.h"
#include "utils/varlena.h"
#ifdef EXEC_BACKEND
int i;
char *output_config_variable = NULL;
- MyProcPid = PostmasterPid = getpid();
+ InitProcessGlobals();
- MyStartTime = time(NULL);
+ PostmasterPid = MyProcPid;
IsPostmasterEnvironment = true;
*/
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.
}
+/*
+ * 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
*/
/* 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 = "";
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.
*
{
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
{
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();