From: Petr Jelinek <petr@2ndquadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Michael Paquier <michael.paquier@gmail.com>
Cc: PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
Date: Tue, 30 Jun 2015 19:15:00 +0200
Message-ID: <5592CE94.1000208@2ndquadrant.com> (raw)
In-Reply-To: <6412.1435681288@sss.pgh.pa.us>
References: <CAB7nPqTL0YYPgGt00gV8mw+23U4ki8yXUKV0mfji3YVpAqR8sA@mail.gmail.com>
<558E09FB.6010909@2ndquadrant.com>
<6321.1435681035@sss.pgh.pa.us>
<6412.1435681288@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
On 2015-06-30 18:21, Tom Lane wrote:
> I wrote:
>> Petr Jelinek <petr@2ndquadrant.com> 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
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:sampling_division_by_zero_v2.diff (text/x-patch, 1.2K) downloaddiff --git a/src/backend/utils/misc/sampling.c b/src/backend/utils/misc/sampling.cindex 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.hindex 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.cindex 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)
{
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: petr@2ndquadrant.com, tgl@sss.pgh.pa.us, michael.paquier@gmail.com
Subject: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
In-Reply-To: <5592CE94.1000208@2ndquadrant.com>
References: <CAB7nPqTL0YYPgGt00gV8mw+23U4ki8yXUKV0mfji3YVpAqR8sA@mail.gmail.com> <558E09FB.6010909@2ndquadrant.com> <6321.1435681035@sss.pgh.pa.us> <6412.1435681288@sss.pgh.pa.us> <5592CE94.1000208@2ndquadrant.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