pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
12+ messages / 3 participants
[nested] [flat]

* sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-06-25 08:01  Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 12+ messages in thread

From: Michael Paquier @ 2015-06-25 08:01 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: petr@2ndquadrant.com

Hi all,
(Petr in CC)

Coverity is pointing out that anl_random_fract and
sampler_random_fract can return 0, causing in some code paths math
errors, aka division by 0 or even log(0) in the case of TABLESAMPLE or
even ANALYZE.

In 9.4, anl_random_fract is careful enough to use random() + 1 to
prevent that, but that's not the case of 9.5 where we begin to use
pg_erand48, that returns a double in range [0.0,1.0).

I think that we should change the returned double to be (0.0,1.0]
instead like in the patch attached (bernouilli and system methods need
a brush-up as well). I haven't updated tsm_system_rows and
tsm_system_time but their regression diffs are attached.

This bug can be triggered when using TABLESAMPLE, now ANALYZE is more
worrying because it could happen during an auto-analyze.
Thoughts?
-- 
Michael

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

Attachments:

20150625_sampling_maths.patch (text/x-patch, 2.7K) download diff --git a/contrib/tsm_system_rows/expected/tsm_system_rows.out b/contrib/tsm_system_rows/expected/tsm_system_rows.out index 7e0f72b..4b8bcaa 100644 --- a/contrib/tsm_system_rows/expected/tsm_system_rows.out +++ b/contrib/tsm_system_rows/expected/tsm_system_rows.out @@ -11,14 +11,14 @@ SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (1000); SELECT id FROM test_tablesample TABLESAMPLE system_rows (8) REPEATABLE (5432); id ---- + 10 + 17 + 24 + 0 7 14 21 28 - 4 - 11 - 18 - 25 (8 rows) EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_rows (20) REPEATABLE (10); diff --git a/contrib/tsm_system_time/expected/tsm_system_time.out b/contrib/tsm_system_time/expected/tsm_system_time.out index 32ad03c..e07860a 100644 --- a/contrib/tsm_system_time/expected/tsm_system_time.out +++ b/contrib/tsm_system_time/expected/tsm_system_time.out @@ -11,6 +11,10 @@ SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (1000); SELECT id FROM test_tablesample TABLESAMPLE system_time (1000) REPEATABLE (5432); id ---- + 10 + 17 + 24 + 0 7 14 21 @@ -38,10 +42,6 @@ SELECT id FROM test_tablesample TABLESAMPLE system_time (1000) REPEATABLE (5432) 20 27 3 - 10 - 17 - 24 - 0 (31 rows) EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_time (100) REPEATABLE (10); diff --git a/src/backend/access/tablesample/bernoulli.c b/src/backend/access/tablesample/bernoulli.c index 0a53900..3fafcb4 100644 --- a/src/backend/access/tablesample/bernoulli.c +++ b/src/backend/access/tablesample/bernoulli.c @@ -146,7 +146,7 @@ tsm_bernoulli_nexttuple(PG_FUNCTION_ARGS) * * (This is our implementation of bernoulli trial) */ - while (sampler_random_fract(sampler->randstate) > probability) + while (1.0 - sampler_random_fract(sampler->randstate) > probability) { tupoffset++; diff --git a/src/backend/utils/misc/sampling.c b/src/backend/utils/misc/sampling.c index aaf1d6c..42e3147 100644 --- a/src/backend/utils/misc/sampling.c +++ b/src/backend/utils/misc/sampling.c @@ -94,7 +94,7 @@ BlockSampler_Next(BlockSampler bs) * less than k, which means that we cannot fail to select enough blocks. *---------- */ - V = sampler_random_fract(bs->randstate); + V = 1.0 - sampler_random_fract(bs->randstate); p = 1.0 - (double) k / (double) K; while (V < p) { @@ -233,11 +233,11 @@ sampler_random_init_state(long seed, SamplerRandomState randstate) randstate[2] = (unsigned short) (seed >> 16); } -/* Select a random value R uniformly distributed in (0 - 1) */ +/* Select a random value R uniformly distributed in (0.0,1.0] */ double sampler_random_fract(SamplerRandomState randstate) { - return pg_erand48(randstate); + return 1.0 - pg_erand48(randstate); }
^ permalink raw reply [nested|flat] 12+ messages in thread

* Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-06-27 02:27  Petr Jelinek <petr@2ndquadrant.com>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 2 replies; 12+ messages in thread

From: Petr Jelinek @ 2015-06-27 02:27 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; pgsql-bugs

On 2015-06-25 10:01, Michael Paquier wrote:
> Hi all,
> (Petr in CC)
>
> Coverity is pointing out that anl_random_fract and
> sampler_random_fract can return 0, causing in some code paths math
> errors, aka division by 0 or even log(0) in the case of TABLESAMPLE or
> even ANALYZE.
>
> In 9.4, anl_random_fract is careful enough to use random() + 1 to
> prevent that, but that's not the case of 9.5 where we begin to use
> pg_erand48, that returns a double in range [0.0,1.0).
>
> I think that we should change the returned double to be (0.0,1.0]

Agreed.

> instead like in the patch attached (bernouilli and system methods need
> a brush-up as well). I haven't updated tsm_system_rows and
> tsm_system_time but their regression diffs are attached.
>

I don't see need for the other 1 - x changes outside of the 
sampler_random_fract() tbh. Yes it changes the regression tests output 
but I don't think it changes correctness of the results.

-- 
  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-bugs

Attachments:

sampling_division_by_zero,diff (text/plain, 6.3K) download diff --git a/contrib/tsm_system_rows/expected/tsm_system_rows.out b/contrib/tsm_system_rows/expected/tsm_system_rows.out index 7e0f72b..4b8bcaa 100644 --- a/contrib/tsm_system_rows/expected/tsm_system_rows.out +++ b/contrib/tsm_system_rows/expected/tsm_system_rows.out @@ -11,14 +11,14 @@ SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (1000); SELECT id FROM test_tablesample TABLESAMPLE system_rows (8) REPEATABLE (5432); id ---- + 10 + 17 + 24 + 0 7 14 21 28 - 4 - 11 - 18 - 25 (8 rows) EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_rows (20) REPEATABLE (10); diff --git a/contrib/tsm_system_time/expected/tsm_system_time.out b/contrib/tsm_system_time/expected/tsm_system_time.out index 32ad03c..e07860a 100644 --- a/contrib/tsm_system_time/expected/tsm_system_time.out +++ b/contrib/tsm_system_time/expected/tsm_system_time.out @@ -11,6 +11,10 @@ SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (1000); SELECT id FROM test_tablesample TABLESAMPLE system_time (1000) REPEATABLE (5432); id ---- + 10 + 17 + 24 + 0 7 14 21 @@ -38,10 +42,6 @@ SELECT id FROM test_tablesample TABLESAMPLE system_time (1000) REPEATABLE (5432) 20 27 3 - 10 - 17 - 24 - 0 (31 rows) EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_time (100) REPEATABLE (10); diff --git a/src/backend/utils/misc/sampling.c b/src/backend/utils/misc/sampling.c index aaf1d6c..141dcfd 100644 --- a/src/backend/utils/misc/sampling.c +++ b/src/backend/utils/misc/sampling.c @@ -233,11 +233,11 @@ sampler_random_init_state(long seed, SamplerRandomState randstate) randstate[2] = (unsigned short) (seed >> 16); } -/* Select a random value R uniformly distributed in (0 - 1) */ +/* Select a random value R uniformly distributed in (0.0,1.0] */ double sampler_random_fract(SamplerRandomState randstate) { - return pg_erand48(randstate); + return 1 - pg_erand48(randstate); } diff --git a/src/test/regress/expected/rowsecurity.out b/src/test/regress/expected/rowsecurity.out index 0ae5557..b2dac30 100644 --- a/src/test/regress/expected/rowsecurity.out +++ b/src/test/regress/expected/rowsecurity.out @@ -102,15 +102,11 @@ NOTICE: f_leak => great manga (4 rows) SELECT * FROM document TABLESAMPLE BERNOULLI (50) REPEATABLE(1) WHERE f_leak(dtitle) ORDER BY did; -NOTICE: f_leak => my first novel -NOTICE: f_leak => my first manga -NOTICE: f_leak => great science fiction - did | cid | dlevel | dauthor | dtitle ------+-----+--------+-------------------+----------------------- - 1 | 11 | 1 | rls_regress_user1 | my first novel - 4 | 44 | 1 | rls_regress_user1 | my first manga - 6 | 22 | 1 | rls_regress_user2 | great science fiction -(3 rows) +NOTICE: f_leak => great manga + did | cid | dlevel | dauthor | dtitle +-----+-----+--------+-------------------+------------- + 8 | 44 | 1 | rls_regress_user2 | great manga +(1 row) -- viewpoint from rls_regress_user2 SET SESSION AUTHORIZATION rls_regress_user2; @@ -157,19 +153,15 @@ NOTICE: f_leak => great manga (8 rows) SELECT * FROM document TABLESAMPLE BERNOULLI (50) REPEATABLE(1) WHERE f_leak(dtitle) ORDER BY did; -NOTICE: f_leak => my first novel -NOTICE: f_leak => my second novel -NOTICE: f_leak => my first manga -NOTICE: f_leak => great science fiction -NOTICE: f_leak => great technology book - did | cid | dlevel | dauthor | dtitle ------+-----+--------+-------------------+----------------------- - 1 | 11 | 1 | rls_regress_user1 | my first novel - 2 | 11 | 2 | rls_regress_user1 | my second novel - 4 | 44 | 1 | rls_regress_user1 | my first manga - 6 | 22 | 1 | rls_regress_user2 | great science fiction - 7 | 33 | 2 | rls_regress_user2 | great technology book -(5 rows) +NOTICE: f_leak => my science fiction +NOTICE: f_leak => my second manga +NOTICE: f_leak => great manga + did | cid | dlevel | dauthor | dtitle +-----+-----+--------+-------------------+-------------------- + 3 | 22 | 2 | rls_regress_user1 | my science fiction + 5 | 44 | 2 | rls_regress_user1 | my second manga + 8 | 44 | 1 | rls_regress_user2 | great manga +(3 rows) EXPLAIN (COSTS OFF) SELECT * FROM document WHERE f_leak(dtitle); QUERY PLAN diff --git a/src/test/regress/expected/tablesample.out b/src/test/regress/expected/tablesample.out index 04e5eb8..bcd1aba 100644 --- a/src/test/regress/expected/tablesample.out +++ b/src/test/regress/expected/tablesample.out @@ -3,21 +3,21 @@ INSERT INTO test_tablesample SELECT i, repeat(i::text, 200) FROM generate_series SELECT t.id FROM test_tablesample AS t TABLESAMPLE SYSTEM (50) REPEATABLE (10); id ---- - 0 - 1 - 2 3 4 5 + 6 + 7 + 8 9 (7 rows) SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (100.0/11) REPEATABLE (9999); id ---- - 6 - 7 - 8 + 3 + 4 + 5 (3 rows) SELECT count(*) FROM test_tablesample TABLESAMPLE SYSTEM (100); @@ -32,28 +32,26 @@ SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (50) REPEATABLE (100); 0 1 2 - 6 - 7 - 8 + 3 + 4 + 5 9 (7 rows) SELECT id FROM test_tablesample TABLESAMPLE BERNOULLI (50) REPEATABLE (100); id ---- - 0 - 1 - 3 - 4 - 5 -(5 rows) + 2 + 6 + 7 + 8 +(4 rows) SELECT id FROM test_tablesample TABLESAMPLE BERNOULLI (5.5) REPEATABLE (1); id ---- - 0 - 5 -(2 rows) + 7 +(1 row) CREATE VIEW test_tablesample_v1 AS SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (10*2) REPEATABLE (2); CREATE VIEW test_tablesample_v2 AS SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (99); @@ -94,31 +92,31 @@ FETCH NEXT FROM tablesample_cur; SELECT id FROM test_tablesample TABLESAMPLE SYSTEM (50) REPEATABLE (10); id ---- - 0 - 1 - 2 3 4 5 + 6 + 7 + 8 9 (7 rows) FETCH NEXT FROM tablesample_cur; id ---- - 6 + 3 (1 row) FETCH NEXT FROM tablesample_cur; id ---- - 7 + 4 (1 row) FETCH NEXT FROM tablesample_cur; id ---- - 8 + 5 (1 row) FETCH FIRST FROM tablesample_cur; @@ -142,19 +140,19 @@ FETCH NEXT FROM tablesample_cur; FETCH NEXT FROM tablesample_cur; id ---- - 6 + 3 (1 row) FETCH NEXT FROM tablesample_cur; id ---- - 7 + 4 (1 row) FETCH NEXT FROM tablesample_cur; id ---- - 8 + 5 (1 row) CLOSE tablesample_cur;
^ permalink raw reply [nested|flat] 12+ messages in thread

* Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-06-27 05:46  Michael Paquier <michael.paquier@gmail.com>
  parent: Petr Jelinek <petr@2ndquadrant.com>
  1 sibling, 0 replies; 12+ messages in thread

From: Michael Paquier @ 2015-06-27 05:46 UTC (permalink / raw)
  To: Petr Jelinek <petr@2ndquadrant.com>; +Cc: pgsql-bugs

On Sat, Jun 27, 2015 at 11:27 AM, Petr Jelinek <petr@2ndquadrant.com> wrote:
> On 2015-06-25 10:01, Michael Paquier wrote:
>> instead like in the patch attached (bernouilli and system methods need
>> a brush-up as well). I haven't updated tsm_system_rows and
>> tsm_system_time but their regression diffs are attached.
>>
>
> I don't see need for the other 1 - x changes outside of the
> sampler_random_fract() tbh. Yes it changes the regression tests output but I
> don't think it changes correctness of the results.

OK, thanks for the confirmation. I thought first that things were
wanted this way in HEAD for the tablesample samplings, but if it does
not matter much, let's move on with your patch then it looks good to
me.
-- 
Michael


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



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-06-30 16:17  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Petr Jelinek <petr@2ndquadrant.com>
  1 sibling, 2 replies; 12+ messages in thread

From: Tom Lane @ 2015-06-30 16:17 UTC (permalink / raw)
  To: Petr Jelinek <petr@2ndquadrant.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; pgsql-bugs

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?

Another problem is that we consider anl_random_fract() to be an exported
API, and the very longstanding definition of that is that the result is
in (0,1), excluding both endpoints.  Whatever we do with
sampler_random_fract(), we'd better make sure that anl_random_fract()
preserves that behavior, else we are likely to break third-party modules.

A simple fix would be to adjust sampler_random_fract to disallow 0
as result, say by repeating the pg_erand48 call if it produces 0.
I'm not sure if that would throw off any of the math in the new
tablesample-related callers.  If it would, I'm inclined to fix the
problem call-site-by-call-site, rather than inventing a definition
of sampler_random_fract() that fails to satisfy the POLA.

			regards, tom lane


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



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-06-30 16:21  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 12+ messages in thread

From: Tom Lane @ 2015-06-30 16:21 UTC (permalink / raw)
  To: Petr Jelinek <petr@2ndquadrant.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; pgsql-bugs

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.

			regards, tom lane


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



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-06-30 17:15  Petr Jelinek <petr@2ndquadrant.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 12+ messages in thread

From: Petr Jelinek @ 2015-06-30 17:15 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael.paquier@gmail.com>; pgsql-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-bugs

Attachments:

sampling_division_by_zero_v2.diff (text/x-patch, 1.2K) download 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) {
^ permalink raw reply [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-06-30 18:52  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Petr Jelinek <petr@2ndquadrant.com>
  0 siblings, 2 replies; 12+ messages in thread

From: Tom Lane @ 2015-06-30 18:52 UTC (permalink / raw)
  To: Petr Jelinek <petr@2ndquadrant.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; pgsql-bugs

Petr Jelinek <petr@2ndquadrant.com> writes:
> 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.

I'm unimpressed with this coding because it presumes that MAX_RANDOM_VALUE
(which is defined as the maximum value returned by random()) has something
to do with the output range of pg_lrand48().  While that might
accidentally fail to fail on all known platforms, it's not good, because
those functions don't have the same provenance and so there's no good
reason to assume that they produce the same output range.

Rather than expanding the API of port.h still further, I continue to think
that the best answer is just to repeat if we get a zero from pg_erand48.

			regards, tom lane


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



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-06-30 19:17  Petr Jelinek <petr@2ndquadrant.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 12+ messages in thread

From: Petr Jelinek @ 2015-06-30 19:17 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael.paquier@gmail.com>; pgsql-bugs

On 2015-06-30 20:52, Tom Lane wrote:
> Petr Jelinek <petr@2ndquadrant.com> writes:
>> 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.
>
> I'm unimpressed with this coding because it presumes that MAX_RANDOM_VALUE
> (which is defined as the maximum value returned by random()) has something
> to do with the output range of pg_lrand48().  While that might
> accidentally fail to fail on all known platforms, it's not good, because
> those functions don't have the same provenance and so there's no good
> reason to assume that they produce the same output range.

Well, by this logic all the other places which are using 
MAX_RANDOM_VALUE would be broken as well as our port version of random() 
just calls pg_lrand48(). Not to mention that we hardwire 
MAX_RANDOM_VALUE to be same as PG_INT32_MAX (which we could use in the 
code I proposed instead of MAX_RANDOM_VALUE to be safe).

>
> Rather than expanding the API of port.h still further, I continue to think
> that the best answer is just to repeat if we get a zero from pg_erand48.
>

Looping over function call until you get value you like seems quite ugly 
to me tbh.

-- 
  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-bugs



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-07-01 07:56  Michael Paquier <michael.paquier@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 12+ messages in thread

From: Michael Paquier @ 2015-07-01 07:56 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Petr Jelinek <petr@2ndquadrant.com>; pgsql-bugs

On Wed, Jul 1, 2015 at 1:17 AM, Tom Lane <tgl@sss.pgh.pa.us> 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?
>
> Another problem is that we consider anl_random_fract() to be an exported
> API, and the very longstanding definition of that is that the result is
> in (0,1), excluding both endpoints.  Whatever we do with
> sampler_random_fract(), we'd better make sure that anl_random_fract()
> preserves that behavior, else we are likely to break third-party modules.

Wait a minute... Yes you are right I clearly missed the fact that in
~9.4 the range of values returned by anl_random_fract() does not
include 1. I thought it did, visibly I misread the code...

> A simple fix would be to adjust sampler_random_fract to disallow 0
> as result, say by repeating the pg_erand48 call if it produces 0.
> I'm not sure if that would throw off any of the math in the new
> tablesample-related callers.  If it would, I'm inclined to fix the
> problem call-site-by-call-site, rather than inventing a definition
> of sampler_random_fract() that fails to satisfy the POLA.

Agreed. Disallowing 0 in sampler_random_fract looks like a good answer
to that. Looking at the tablesample code, for the bernouilli trial I
recall that the range of probability success and failure is actually
(0,1), (if p is a success rate, the failure is 1 - p). I am not sure
for Knuth Algo S though for the system sampling but that looks OK from
a pure logical viewpoint.
-- 
Michael

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

Attachments:

20150701_tablesample_random.patch (text/x-patch, 542B) download diff --git a/src/backend/utils/misc/sampling.c b/src/backend/utils/misc/sampling.c index aaf1d6c..5356d29 100644 --- a/src/backend/utils/misc/sampling.c +++ b/src/backend/utils/misc/sampling.c @@ -237,7 +237,13 @@ sampler_random_init_state(long seed, SamplerRandomState randstate) double sampler_random_fract(SamplerRandomState randstate) { - return pg_erand48(randstate); + double res = pg_erand48(randstate); + + /* pg_erand48 returns a value in [0.0 - 1.0) */ + while (res == 0.0) + res = pg_erand48(randstate); + + return res; }
^ permalink raw reply [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-07-02 00:44  Michael Paquier <michael.paquier@gmail.com>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 12+ messages in thread

From: Michael Paquier @ 2015-07-02 00:44 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Petr Jelinek <petr@2ndquadrant.com>; pgsql-bugs

On Wed, Jul 1, 2015 at 4:56 PM, Michael Paquier wrote:
> On Wed, Jul 1, 2015 at 1:17 AM, Tom Lane wrote:
>> A simple fix would be to adjust sampler_random_fract to disallow 0
>> as result, say by repeating the pg_erand48 call if it produces 0.
>> I'm not sure if that would throw off any of the math in the new
>> tablesample-related callers.  If it would, I'm inclined to fix the
>> problem call-site-by-call-site, rather than inventing a definition
>> of sampler_random_fract() that fails to satisfy the POLA.
>
> Agreed. Disallowing 0 in sampler_random_fract looks like a good answer
> to that. Looking at the tablesample code, for the bernouilli trial I
> recall that the range of probability success and failure is actually
> (0,1), (if p is a success rate, the failure is 1 - p). I am not sure
> for Knuth Algo S though for the system sampling but that looks OK from
> a pure logical viewpoint.

For the sake of the archives, this has been fixed by commit d7c19d68.

(Thanks Tom for pushing it!)
-- 
Michael


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



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-07-02 00:51  Michael Paquier <michael.paquier@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 12+ messages in thread

From: Michael Paquier @ 2015-07-02 00:51 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Petr Jelinek <petr@2ndquadrant.com>; pgsql-bugs

On Wed, Jul 1, 2015 at 3:52 AM, Tom Lane wrote:
> Rather than expanding the API of port.h still further
> [...]

The current trend, as far as I am understanding it, is not to increase
the amount of code in src/port, but actually to reduce it and to rely
a maximum on what modern platforms offer. See for example the recent
discussion about rint and numeric rounding here:
http://www.postgresql.org/message-id/flat/20150320194337.2573.72944@wrigleys.postgresql.org#20150320...
We are keeping rint around because MSVC just added it recently in
MS2013 even if the POSIX spec has it for a long time, but I am sure
that we would drop it asap from the code tree if we could.
-- 
Michael


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



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
@ 2015-07-02 02:27  Petr Jelinek <petr@2ndquadrant.com>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 12+ messages in thread

From: Petr Jelinek @ 2015-07-02 02:27 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

On 2015-07-02 02:51, Michael Paquier wrote:
> On Wed, Jul 1, 2015 at 3:52 AM, Tom Lane wrote:
>> Rather than expanding the API of port.h still further
>> [...]
>
> The current trend, as far as I am understanding it, is not to increase
> the amount of code in src/port, but actually to reduce it and to rely
> a maximum on what modern platforms offer. See for example the recent
> discussion about rint and numeric rounding here:
> http://www.postgresql.org/message-id/flat/20150320194337.2573.72944@wrigleys.postgresql.org#20150320...
> We are keeping rint around because MSVC just added it recently in
> MS2013 even if the POSIX spec has it for a long time, but I am sure
> that we would drop it asap from the code tree if we could.
>

Which is completely moot point here because the sampling depends on 
pg_erand48 on every platform no matter what the platform provides.

-- 
  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-bugs



^ permalink  raw  reply  [nested|flat] 12+ messages in thread


end of thread, other threads:[~2015-07-02 02:27 UTC | newest]

Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-06-25 08:01 sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5 Michael Paquier <michael.paquier@gmail.com>
2015-06-27 02:27 ` Petr Jelinek <petr@2ndquadrant.com>
2015-06-27 05:46   ` Michael Paquier <michael.paquier@gmail.com>
2015-06-30 16:17   ` Tom Lane <tgl@sss.pgh.pa.us>
2015-06-30 16:21     ` Tom Lane <tgl@sss.pgh.pa.us>
2015-06-30 17:15       ` Petr Jelinek <petr@2ndquadrant.com>
2015-06-30 18:52         ` Tom Lane <tgl@sss.pgh.pa.us>
2015-06-30 19:17           ` Petr Jelinek <petr@2ndquadrant.com>
2015-07-02 00:51           ` Michael Paquier <michael.paquier@gmail.com>
2015-07-02 02:27             ` Petr Jelinek <petr@2ndquadrant.com>
2015-07-01 07:56     ` Michael Paquier <michael.paquier@gmail.com>
2015-07-02 00:44       ` Michael Paquier <michael.paquier@gmail.com>

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