From 7518c2b42afd21e7fcedcefb3e4dda65b9e9920d Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Wed, 14 Nov 2018 19:36:14 +1300 Subject: [PATCH] Increase the number of possible random seeds per time period. Commit 197e4af9 refactored the initialization of the libc random() seed, but reduced the number of possible seeds values that could be chosen in a given time period. This negation of the effects of commit 98c50656c was unintentional. Replace with code that shifts the fast moving timestamp bits left, like the earlier code. Author: Thomas Munro Reported-by: Noah Misch Reviewed-by: Discussion: https://postgr.es/m/20181112083358.GA1073796%40rfd.leadboat.com --- src/backend/postmaster/postmaster.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 688f462e7d..d64d554022 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -2525,8 +2525,14 @@ InitProcessGlobals(void) random_start_time.tv_usec = 0; #endif - /* Set a different seed for random() in every backend. */ - srandom((unsigned int) MyProcPid ^ (unsigned int) MyStartTimestamp); + /* + * Set a different seed for random() in every backend. Since PIDs and + * timestamps tend to change more frequently in their least significant + * bits, shift the timestamp left to allow a larger total number of seeds + * in a given time period. Since that would leave only 20 bits of the + * timestamp that cycle every ~1 second, also mix in some higher bits. + */ + srandom(MyProcPid ^ (MyStartTimestamp << 12) ^ (MyStartTimestamp >> 20)); } -- 2.19.1