pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Honza Horak <hhorak@redhat.com>
To: pgsql-bugs@postgresql.org
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Heikki Linnakangas <hlinnakangas@vmware.com>
Subject: Re: random() generates collisions too early
Date: Tue, 22 Oct 2013 14:41:18 +0200
Message-ID: <5266726E.5030300@redhat.com> (raw)
In-Reply-To: <29392.1382368446@sss.pgh.pa.us>
References: <5261219D.3060205@redhat.com>
	<526537ED.2010607@vmware.com>
	<29392.1382368446@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 10/21/2013 05:14 PM, Tom Lane wrote:
> Heikki Linnakangas <hlinnakangas@vmware.com> writes:
>> On 18.10.2013 14:55, Honza Horak wrote:
>>> The results covered only 181383 distinct values, and 68 values
>>> repeated four or five times each. We should at least consider using a
>>> higher-entropy seed.
>
>> Interesting. PostgreSQL's random() function just calls the underlying
>> libc random() function. I assume you tested this on with Linux and glibc.
>
> I agree with the theory that this probably isn't the fault of the random()
> function as such, but with our code to reset the random seed when forking
> a postmaster child process.  Note that the test case is only examining the
> first random value created in each process.  So basically what this is
> measuring is the number of different seed values we use.
>
> 			regards, tom lane
>

After playing a bit more with random() value and the seed defined in 
src/backend/postmaster/postmaster.c:4038 it seems really like the seed 
doesn't have very good characteristic. The PID number used there ensures 
the value to be different in child processes, but when only xor-ed with 
usecs, it doesn't enlarge the total data domain of the seed, which stays 
1M values. Then having in mind the birthday paradox and probably not 
ideal PRNG, it doesn't seem unrealistic to get two same random numbers 
in only hundreds/thousands of tries.

In order to enlarge possible data domain of the seed we can include sec 
count as well and shift some xor-ed variables to use the whole range or 
unsigned int. The attached patch uses a way that gave me much better 
results (collision found approx. after a hundred thousands of values):

-       srandom((unsigned int) (MyProcPid ^ usecs));
+       srandom((unsigned int) (MyProcPid ^ (usecs << 12) ^ secs));

I'm also thinking of a reproducer -- does the test-suite allow to 
"reconnect" the client during a test?

Regards,
Honza

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

random-seed.patch (text/x-patch, 602B) download diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 351887b..a5bc5d1 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -4035,7 +4035,7 @@ BackendRun(Port *port) random_start_time.tv_usec = 0; /* slightly hacky way to get integer microseconds part of timestamptz */ 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.

view thread (11+ messages)  latest in thread

Message-ID: <5266726E.5030300@redhat.com>
Permalink:  ../5266726E.5030300@redhat.com/
Also on:    postgresql.org/message-id/5266726E.5030300@redhat.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: hhorak@redhat.com, tgl@sss.pgh.pa.us, hlinnakangas@vmware.com
  Subject: Re: random() generates collisions too early
  In-Reply-To: <5266726E.5030300@redhat.com>
  References: <5261219D.3060205@redhat.com> <526537ED.2010607@vmware.com> <29392.1382368446@sss.pgh.pa.us> <5266726E.5030300@redhat.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox