Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z9z7o-00057t-5h for pgsql-bugs@arkaria.postgresql.org; Tue, 30 Jun 2015 17:15:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1Z9z7n-0004gf-F6 for pgsql-bugs@arkaria.postgresql.org; Tue, 30 Jun 2015 17:15:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Z9z7m-0004gK-CW for pgsql-bugs@postgresql.org; Tue, 30 Jun 2015 17:15:10 +0000 Received: from mail-wg0-f45.google.com ([74.125.82.45]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Z9z7i-00086w-JT for pgsql-bugs@postgresql.org; Tue, 30 Jun 2015 17:15:09 +0000 Received: by wgck11 with SMTP id k11so15596465wgc.0 for ; Tue, 30 Jun 2015 10:15:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type; bh=OSNknJquoaKCallh3Kdh6BSdtJgT0zjBEgMMAbWe/5A=; b=EcYjzHAEiJdHT8GUYQIEj7DdGDKH/nrFo9GIR80Q32ssdqo8G5wM2M/dcj1ue3ySlJ 9RBoKvs235vkvUlTzQ/Ty9Vu4y3qd/XsBtXV4WdttA4QOkg4r2/lQas61MN8bvDrOX2N t1TJ6ZreqAwHCNaxKoehfBoZfZBfDqxqZQvNCoMKvONs0whdwbV4NOZvQygD9a1TX08H BQKDnlSJExrIuu2ndVeB5IYoG5bG/whY0BMBkJcnDaZDZZ+jjSFFK8MDARr88VL1sjG4 2LCs07rkGSLbzxXkxIHpnwzFzDlLQQH1yETdiduLg1nONbxJA1WGFg+3IgoR78xjvAsp +PFQ== X-Gm-Message-State: ALoCoQlxl7pHlmwYbNUnDbcWKQVuY2NnIN1wu5Z9ZwIwsnjyxOyhy9h0iRc0nZiJQ0ATHf4+ZwVSQw+zV1NKu3bCxn1qqGLRr2RP1eQyTJg5MeasdZykTQPjoEx1f61RtLYOf+XI6S2wPglvxl49O7Nyf8FbtzX/y7FxgN0s+SImzJgcszPgQnECfTbY9LEohcTyNzrhKLDF X-Received: by 10.180.160.210 with SMTP id xm18mr34519477wib.93.1435684504854; Tue, 30 Jun 2015 10:15:04 -0700 (PDT) Received: from ?IPv6:2a00:1028:9941:52fa:d464:fc43:f845:4d31? (dynamic-2a00-1028-9941-52fa-d464-fc43-f845-4d31.ipv6.broadband.iol.cz. [2a00:1028:9941:52fa:d464:fc43:f845:4d31]) by mx.google.com with ESMTPSA id x10sm15729783wjr.25.2015.06.30.10.15.02 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Jun 2015 10:15:03 -0700 (PDT) Message-ID: <5592CE94.1000208@2ndquadrant.com> Date: Tue, 30 Jun 2015 19:15:00 +0200 From: Petr Jelinek User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Tom Lane CC: Michael Paquier , PostgreSQL mailing lists Subject: Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5 References: <558E09FB.6010909@2ndquadrant.com> <6321.1435681035@sss.pgh.pa.us> <6412.1435681288@sss.pgh.pa.us> In-Reply-To: <6412.1435681288@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------070108080905040003070203" X-Pg-Spam-Score: -1.9 (-) 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. --------------070108080905040003070203 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 2015-06-30 18:21, Tom Lane wrote: > I wrote: >> Petr Jelinek writes: >>> On 2015-06-25 10:01, Michael Paquier wrote: >>>> I think that we should change the returned double to be (0.0,1.0] > >>> Agreed. > >> I find this to be a pretty bad idea. That definition is simply weird; >> where else in the world will you find a random number generator that does >> that? What are the odds that any callers are actually designed for that >> behavior? > > And, in fact, a bit of looking quickly finds a counterexample, in > analyze.c: > > int k = (int) (targrows * sampler_random_fract(rstate.randstate)); > > Assert(k >= 0 && k < targrows); > > You can't just whack this around to satisfy some new call sites without > considering the behavior of existing use-cases. > Right, very good point. So, we used to protect against this problem by using long value and doing: ((double) random() + 1) / ((double) MAX_RANDOM_VALUE + 2) Maybe best solution is to have pg_lrand48() variant which accepts seed as parameter same way pg_erand48() does and use the same logic we used to have before sampling was added. The attached patch does that and it does not even need changes in the regression tests output which I consider to be a nice bonus. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------070108080905040003070203 Content-Type: text/x-patch; name="sampling_division_by_zero_v2.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="sampling_division_by_zero_v2.diff" diff --git a/src/backend/utils/misc/sampling.c b/src/backend/utils/misc/sampling.c index aaf1d6c..21234f9 100644 --- a/src/backend/utils/misc/sampling.c +++ b/src/backend/utils/misc/sampling.c @@ -237,7 +237,7 @@ sampler_random_init_state(long seed, SamplerRandomState randstate) double sampler_random_fract(SamplerRandomState randstate) { - return pg_erand48(randstate); + return ((double) pg_lrand48_seed(randstate) + 1) / ((double) MAX_RANDOM_VALUE + 2); } diff --git a/src/include/port.h b/src/include/port.h index 71113c0..6172a65 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -363,6 +363,7 @@ extern off_t ftello(FILE *stream); extern double pg_erand48(unsigned short xseed[3]); extern long pg_lrand48(void); +extern long pg_lrand48_seed(unsigned short xseed[3]); extern void pg_srand48(long seed); #ifndef HAVE_FLS diff --git a/src/port/erand48.c b/src/port/erand48.c index 12efd81..22e86ef 100644 --- a/src/port/erand48.c +++ b/src/port/erand48.c @@ -88,6 +88,13 @@ pg_lrand48(void) return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1); } +long +pg_lrand48_seed(unsigned short xseed[3]) +{ + _dorand48(xseed); + return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); +} + void pg_srand48(long seed) { --------------070108080905040003070203 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 --------------070108080905040003070203--