Skip to content

Commit 98c5065

Browse files
committed
Increase the number of different values used when seeding random().
When a backend process is forked, we initialize the system's random number generator with srandom(). The seed used is derived from the backend's pid and the timestamp. However, we only used the microseconds part of the timestamp, and it was XORed with the pid, so the total range of different seed values chosen was 0-999999. That's quite limited. Change the code to also use the seconds part of the timestamp in the seed, and shift the microseconds so that all 32 bits of the seed are used. Honza Horak
1 parent 75fdcec commit 98c5065

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,9 +4033,9 @@ BackendRun(Port *port)
40334033
*/
40344034
random_seed = 0;
40354035
random_start_time.tv_usec = 0;
4036-
/* slightly hacky way to get integer microseconds part of timestamptz */
4036+
/* slightly hacky way to convert timestamptz into integers */
40374037
TimestampDifference(0, port->SessionStartTime, &secs, &usecs);
4038-
srandom((unsigned int) (MyProcPid ^ usecs));
4038+
srandom((unsigned int) (MyProcPid ^ (usecs << 12) ^ secs));
40394039

40404040
/*
40414041
* Now, build the argv vector that will be given to PostgresMain.

0 commit comments

Comments
 (0)