Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z8fqi-0002By-AJ for pgsql-bugs@arkaria.postgresql.org; Sat, 27 Jun 2015 02:28:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1Z8fqh-0003Qg-QY for pgsql-bugs@arkaria.postgresql.org; Sat, 27 Jun 2015 02:28:07 +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 1Z8fpr-0002iQ-UY for pgsql-bugs@postgresql.org; Sat, 27 Jun 2015 02:27:16 +0000 Received: from mail-wi0-f170.google.com ([209.85.212.170]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Z8fpo-0001nl-1G for pgsql-bugs@postgresql.org; Sat, 27 Jun 2015 02:27:15 +0000 Received: by wiwl6 with SMTP id l6so58384955wiw.0 for ; Fri, 26 Jun 2015 19:27:10 -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 :subject:references:in-reply-to:content-type; bh=KfvTrwPZxHMJThnxB40fAoOAJKiH9wHZaKPzvuNTMg8=; b=QPEY5WIUb+MJkwJtKudKv3z1h1/44gYvKZbsThtr8nRPBp4Tf1VbIdp3WQt+otZr7H TKW0S1pfRpv6xLlViMTWbwGXXG+0qoZu+bAbCDRymNHxu24HX+VshIa+9fKkC9noJ6G+ bNrkXmkPJoXXP1uTC/lYyiBbdks2FHim+RXIyUMCf3lJcrlVUfYYqV6R5z2QBOFIhjQE 4OxFVIcjZ8HY1z9ZZam7aGHn0/z8eGTMyGYd3WeyUcCDOTXYK31h99srnxe3BSqqAD4T EABrbHOHpE8K2k9CZIOhAjihH/dA+kJV/zO2FG7wL+SdRfVaTv/mQTp5oxMXoTKHnP+w hYEg== X-Gm-Message-State: ALoCoQlrhM4OM8Nkp2irCOhuEEwfVRnLIRkOCOhpUc+nXu+LIhbgiwVQyOzBMfGmAUOyuAp15/lMbWl6pbig/sMgZOn3DEEAh5Sg3hHp1/ZVmkS2GJ7Ou16z1j9OCpAIon+evq4isLnDOTINwmOzgx9ufHikvdeo3acY0tZ9gIuOwCgNF8q71P4prsgcJF4uyf49dR2aZGBq X-Received: by 10.180.9.6 with SMTP id v6mr1841867wia.83.1435372030219; Fri, 26 Jun 2015 19:27:10 -0700 (PDT) Received: from [10.1.0.103] (190.84.broadband17.iol.cz. [109.80.84.190]) by mx.google.com with ESMTPSA id i5sm971635wic.10.2015.06.26.19.27.08 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Jun 2015 19:27:09 -0700 (PDT) Message-ID: <558E09FB.6010909@2ndquadrant.com> Date: Sat, 27 Jun 2015 04:27:07 +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: Michael Paquier , PostgreSQL mailing lists Subject: Re: sampling.c and potential divisions by 0 ang log(0) with tablesample and ANALYZE in 9.5 References: In-Reply-To: Content-Type: multipart/mixed; boundary="------------070801030007020208010802" X-Pg-Spam-Score: -2.6 (--) 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. --------------070801030007020208010802 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit 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 --------------070801030007020208010802 Content-Type: text/plain; charset=UTF-8; name="sampling_division_by_zero,diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="sampling_division_by_zero,diff" 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; --------------070801030007020208010802 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 --------------070801030007020208010802--