Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VYbHB-0000cB-Ii for pgsql-bugs@arkaria.postgresql.org; Tue, 22 Oct 2013 12:41:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VYbHB-00089W-2j for pgsql-bugs@arkaria.postgresql.org; Tue, 22 Oct 2013 12:41:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VYbHA-00089P-36 for pgsql-bugs@postgresql.org; Tue, 22 Oct 2013 12:41:32 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VYbH1-0007Vn-Ob for pgsql-bugs@postgresql.org; Tue, 22 Oct 2013 12:41:28 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r9MCfKjq012075 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 22 Oct 2013 08:41:20 -0400 Received: from hhorak-t510.usersys.redhat.com (unused-4-233.brq.redhat.com [10.34.4.233]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r9MCfI8l031369; Tue, 22 Oct 2013 08:41:19 -0400 Message-ID: <5266726E.5030300@redhat.com> Date: Tue, 22 Oct 2013 14:41:18 +0200 From: Honza Horak User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130923 Thunderbird/17.0.9 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org CC: Tom Lane , Heikki Linnakangas Subject: Re: random() generates collisions too early References: <5261219D.3060205@redhat.com> <526537ED.2010607@vmware.com> <29392.1382368446@sss.pgh.pa.us> In-Reply-To: <29392.1382368446@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------030607040803010606050104" X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Pg-Spam-Score: -7.3 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------030607040803010606050104 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 10/21/2013 05:14 PM, Tom Lane wrote: > Heikki Linnakangas 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 --------------030607040803010606050104 Content-Type: text/x-patch; name="random-seed.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="random-seed.patch" 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. --------------030607040803010606050104 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------030607040803010606050104--