Commit 5b0ce3ec authored by Thomas Munro's avatar Thomas Munro

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 98c50656 was unintentional.  Replace with code that
shifts the fast moving timestamp bits left, similar to the original
algorithm (though not the previous float-tolerating coding, which
is no longer necessary).

Author: Thomas Munro
Reported-by: Noah Misch
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/20181112083358.GA1073796%40rfd.leadboat.com
parent aa551830
...@@ -2525,8 +2525,16 @@ InitProcessGlobals(void) ...@@ -2525,8 +2525,16 @@ InitProcessGlobals(void)
random_start_time.tv_usec = 0; random_start_time.tv_usec = 0;
#endif #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(((unsigned int) MyProcPid) ^
((unsigned int) MyStartTimestamp << 12) ^
((unsigned int) MyStartTimestamp >> 20));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment