From: Petr Jelinek <petr@2ndquadrant.com>
To: Michael Paquier <michael.paquier@gmail.com>
To: PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5
Date: Sat, 27 Jun 2015 04:27:07 +0200
Message-ID: <558E09FB.6010909@2ndquadrant.com> (raw)
In-Reply-To: <CAB7nPqTL0YYPgGt00gV8mw+23U4ki8yXUKV0mfji3YVpAqR8sA@mail.gmail.com>
References: <CAB7nPqTL0YYPgGt00gV8mw+23U4ki8yXUKV0mfji3YVpAqR8sA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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-bugsAttachments:sampling_division_by_zero,diff (text/plain, 6.3K) downloaddiff --git a/contrib/tsm_system_rows/expected/tsm_system_rows.out b/contrib/tsm_system_rows/expected/tsm_system_rows.outindex 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.outindex 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.cindex 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.outindex 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.outindex 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;
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, 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: <558E09FB.6010909@2ndquadrant.com>
References: <CAB7nPqTL0YYPgGt00gV8mw+23U4ki8yXUKV0mfji3YVpAqR8sA@mail.gmail.com> <558E09FB.6010909@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