postgres.git / summary / log / commit / refs

commit    98c50656cac2e6b873419fd09569a9119c02148c
Author:   Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date:     Thu Oct 24 13:55:22 2013 +0000

    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


src/backend/postmaster/postmaster.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 351887b1333..d294a5a47f8 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -4033,9 +4033,9 @@ BackendRun(Port *port) */ random_seed = 0; random_start_time.tv_usec = 0; - /* slightly hacky way to get integer microseconds part of timestamptz */ + /* slightly hacky way to convert timestamptz into integers */ TimestampDifference(0, port->SessionStartTime, &secs, &usecs); - srandom((unsigned int) (MyProcPid ^ usecs)); + srandom((unsigned int) (MyProcPid ^ (usecs << 12) ^ secs)); /* * Now, build the argv vector that will be given to PostgresMain. [parent: 75fdcec14543]