Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZuRwk-0004TY-1O for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Nov 2015 21:19:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZuRwj-0000sZ-JO for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Nov 2015 21:19:49 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZuRwi-0000ph-A3; Thu, 05 Nov 2015 21:19:48 +0000 Received: from gauss.credativ.com ([93.94.130.89]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZuRwe-0005QC-7p; Thu, 05 Nov 2015 21:19:46 +0000 Received: from gauss.credativ.com (localhost [127.0.0.1]) by gauss.credativ.com (Postfix) with ESMTP id 8527B1450037; Thu, 5 Nov 2015 22:19:41 +0100 (CET) Received: from [192.168.1.135] (ip-109-91-229-217.hsi12.unitymediagroup.de [109.91.229.217]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: avo@gauss.credativ.com) by gauss.credativ.com (Postfix) with ESMTPSA id 44C061450036; Thu, 5 Nov 2015 22:19:41 +0100 (CET) Subject: Re: [BUGS] BUG #12989: pg_size_pretty with negative values To: Robert Haas , Julien Rouhaud References: <20150406173026.2550.39773@wrigleys.postgresql.org> <31955.1428346338@sss.pgh.pa.us> <55FEA38F.40301@credativ.de> <56350782.3040400@dalibo.com> Cc: "pgsql-bugs@postgresql.org" , "pgsql-hackers@postgresql.org" , Tom Lane , "David G. Johnston" , Christian Almeida From: "Adrian.Vondendriesch" X-Enigmail-Draft-Status: N1110 Message-ID: <563BC7E6.9060101@credativ.de> Date: Thu, 5 Nov 2015 22:19:34 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="C2nQfsfk4kGEOMa2Oso9wwgefH8Xavud9" X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --C2nQfsfk4kGEOMa2Oso9wwgefH8Xavud9 Content-Type: multipart/mixed; boundary="------------050409040701060702010406" This is a multi-part message in MIME format. --------------050409040701060702010406 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable New patch attached and rebased on HEAD (8c75ad436f75fc629b61f601ba884c8f9313c9af). Am 03.11.2015 um 04:06 schrieb Robert Haas: > On Sat, Oct 31, 2015 at 2:25 PM, Julien Rouhaud > wrote: >> I just reviewed your patch, everything looks fine for me. Maybe some >> minor cosmetic changes could be made to avoid declaring too many vars,= >> but I think a committer would have a better idea on this, so I mark >> this patch as ready for committer. >=20 > I don't think we should define Sign(x) as a macro in c.h. c.h is > really only supposed to contain stuff that's pretty generic and > universal, and the fact that we haven't needed it up until now > suggests that Sign(x) isn't. I'd suggest instead defining something > like: >=20 > #define half_rounded(x) (((x) + (x) < 0 ? 0 : 1) / 2) I removed the Sign macro and introduced half_rounded. It's placed it in dbsize.c. Please let me know if that's the wrong place. >=20 > Maybe rename numeric_divide_by_two to numeric_half_rounded. > Generally, let's try to make the numeric and int64 code paths look as > similar as possible. I renamed numeric_divide_by_two. >=20 > Recomputing numeric_absolute(x) multiple times should be avoided. > Compute it once and save the answer. Because "size" is shifted multiple times within pg_size_pretty and pg_size_pretty_numeric the absolute values needs to be recomputed. Please let me know if I oversee something. I changed the status back to "needs review". Regards, - Adrian --------------050409040701060702010406 Content-Type: text/x-diff; name="0001-Make-pg_size_pretty-handle-negative-values-v2.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="0001-Make-pg_size_pretty-handle-negative-values-v2.patch" =46rom c06d1463cf21957260327c47217050e334058422 Mon Sep 17 00:00:00 2001 From: Adrian Vondendriesch Date: Sat, 19 Sep 2015 15:54:13 +0200 Subject: [PATCH] Make pg_size_pretty handle negative values. Make pg_size_pretty(bigint) and pg_size_pretty(numeric) handle negative values the same way as positive values are. This commit introduces a new macro "Sign", which is used within pg_size_pretty(bigint). Also numeric_plus_one_over_two is renamed to numeric_divide_by_two. numeric_divide_by_two now handles negative values the same way as positive values are handled. To get the absolute value of a Numeric variable, a new static function called numeric_absolute is introduced. --- src/backend/utils/adt/dbsize.c | 67 ++++++++++++----= --- .../regress/expected/pg_size_pretty_bigint.out | 39 +++++++++++ .../regress/expected/pg_size_pretty_numeric.out | 75 ++++++++++++++++= ++++++ src/test/regress/sql/pg_size_pretty_bigint.sql | 15 +++++ src/test/regress/sql/pg_size_pretty_numeric.sql | 29 +++++++++ 5 files changed, 203 insertions(+), 22 deletions(-) create mode 100644 src/test/regress/expected/pg_size_pretty_bigint.out create mode 100644 src/test/regress/expected/pg_size_pretty_numeric.out create mode 100644 src/test/regress/sql/pg_size_pretty_bigint.sql create mode 100644 src/test/regress/sql/pg_size_pretty_numeric.sql diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsiz= e.c index 82311b4..84c67d3 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -31,6 +31,12 @@ #include "utils/relmapper.h" #include "utils/syscache.h" =20 +/* + * half_rounded + * Return (x / 2) if x is smaller than 0 + * ((x + 1) / 2) otherwise. + */ +#define half_rounded(x) (((x) + ((x) < 0 ? 0 : 1)) / 2) =20 /* Return physical size of directory contents, or 0 if dir doesn't exist= */ static int64 @@ -534,31 +540,31 @@ pg_size_pretty(PG_FUNCTION_ARGS) int64 limit =3D 10 * 1024; int64 limit2 =3D limit * 2 - 1; =20 - if (size < limit) + if (Abs(size) < limit) snprintf(buf, sizeof(buf), INT64_FORMAT " bytes", size); else { size >>=3D 9; /* keep one extra bit for rounding */ - if (size < limit2) + if (Abs(size) < limit2) snprintf(buf, sizeof(buf), INT64_FORMAT " kB", - (size + 1) / 2); + half_rounded(size)); else { size >>=3D 10; - if (size < limit2) + if (Abs(size) < limit2) snprintf(buf, sizeof(buf), INT64_FORMAT " MB", - (size + 1) / 2); + half_rounded(size)); else { size >>=3D 10; - if (size < limit2) + if (Abs(size) < limit2) snprintf(buf, sizeof(buf), INT64_FORMAT " GB", - (size + 1) / 2); + half_rounded(size)); else { size >>=3D 10; snprintf(buf, sizeof(buf), INT64_FORMAT " TB", - (size + 1) / 2); + half_rounded(size)); } } } @@ -593,16 +599,37 @@ numeric_is_less(Numeric a, Numeric b) } =20 static Numeric -numeric_plus_one_over_two(Numeric n) +numeric_absolute(Numeric n) +{ + Datum d =3D NumericGetDatum(n); + Datum result; + + result =3D DirectFunctionCall1(numeric_abs, d); + return DatumGetNumeric(result); +} + +static Numeric +numeric_half_rounded(Numeric n) { Datum d =3D NumericGetDatum(n); + Datum zero; Datum one; Datum two; Datum result; + bool greater_or_equal_zero; =20 + zero =3D DirectFunctionCall1(int8_numeric, Int64GetDatum(0)); one =3D DirectFunctionCall1(int8_numeric, Int64GetDatum(1)); two =3D DirectFunctionCall1(int8_numeric, Int64GetDatum(2)); - result =3D DirectFunctionCall2(numeric_add, d, one); + + result =3D DirectFunctionCall2(numeric_ge, d, zero); + greater_or_equal_zero =3D DatumGetBool(result); + + if (greater_or_equal_zero) + result =3D DirectFunctionCall2(numeric_add, d, one); + else + result =3D DirectFunctionCall2(numeric_sub, d, one); + result =3D DirectFunctionCall2(numeric_div_trunc, result, two); return DatumGetNumeric(result); } @@ -632,7 +659,7 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS) limit =3D int64_to_numeric(10 * 1024); limit2 =3D int64_to_numeric(10 * 1024 * 2 - 1); =20 - if (numeric_is_less(size, limit)) + if (numeric_is_less(numeric_absolute(size), limit)) { result =3D psprintf("%s bytes", numeric_to_cstring(size)); } @@ -642,20 +669,18 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS) /* size >>=3D 9 */ size =3D numeric_shift_right(size, 9); =20 - if (numeric_is_less(size, limit2)) + if (numeric_is_less(numeric_absolute(size), limit2)) { - /* size =3D (size + 1) / 2 */ - size =3D numeric_plus_one_over_two(size); + size =3D numeric_half_rounded(size); result =3D psprintf("%s kB", numeric_to_cstring(size)); } else { /* size >>=3D 10 */ size =3D numeric_shift_right(size, 10); - if (numeric_is_less(size, limit2)) + if (numeric_is_less(numeric_absolute(size), limit2)) { - /* size =3D (size + 1) / 2 */ - size =3D numeric_plus_one_over_two(size); + size =3D numeric_half_rounded(size); result =3D psprintf("%s MB", numeric_to_cstring(size)); } else @@ -663,18 +688,16 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS) /* size >>=3D 10 */ size =3D numeric_shift_right(size, 10); =20 - if (numeric_is_less(size, limit2)) + if (numeric_is_less(numeric_absolute(size), limit2)) { - /* size =3D (size + 1) / 2 */ - size =3D numeric_plus_one_over_two(size); + size =3D numeric_half_rounded(size); result =3D psprintf("%s GB", numeric_to_cstring(size)); } else { /* size >>=3D 10 */ size =3D numeric_shift_right(size, 10); - /* size =3D (size + 1) / 2 */ - size =3D numeric_plus_one_over_two(size); + size =3D numeric_half_rounded(size); result =3D psprintf("%s TB", numeric_to_cstring(size)); } } diff --git a/src/test/regress/expected/pg_size_pretty_bigint.out b/src/te= st/regress/expected/pg_size_pretty_bigint.out new file mode 100644 index 0000000..f669e35 --- /dev/null +++ b/src/test/regress/expected/pg_size_pretty_bigint.out @@ -0,0 +1,39 @@ +-- +-- pg_size_pretty_bigint +-- +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 10::= bigint) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 10 bytes | -10 bytes +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= ::bigint) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 1000 bytes | -1000 bytes +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000::bigint) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 977 kB | -977 kB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000::bigint) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 954 MB | -954 MB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000::bigint) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 931 GB | -931 GB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000000::bigint) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 909 TB | -909 TB +(1 row) + diff --git a/src/test/regress/expected/pg_size_pretty_numeric.out b/src/t= est/regress/expected/pg_size_pretty_numeric.out new file mode 100644 index 0000000..ce34952 --- /dev/null +++ b/src/test/regress/expected/pg_size_pretty_numeric.out @@ -0,0 +1,75 @@ +-- +-- pg_size_pretty_numeric +-- +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 10::= numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 10 bytes | -10 bytes +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= ::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 1000 bytes | -1000 bytes +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 977 kB | -977 kB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 954 MB | -954 MB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 931 GB | -931 GB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000000::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 909 TB | -909 TB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 10.5= ::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 10.5 bytes | -10.5 bytes +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= =2E5::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 1000.5 bytes | -1000.5 bytes +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000.5::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 977 kB | -977 kB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000.5::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 954 MB | -954 MB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000.5::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 931 GB | -931 GB +(1 row) + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000000.5::numeric) pg_size_pretty(size); + pg_size_pretty | pg_size_pretty=20 +----------------+---------------- + 909 TB | -909 TB +(1 row) + diff --git a/src/test/regress/sql/pg_size_pretty_bigint.sql b/src/test/re= gress/sql/pg_size_pretty_bigint.sql new file mode 100644 index 0000000..a232203 --- /dev/null +++ b/src/test/regress/sql/pg_size_pretty_bigint.sql @@ -0,0 +1,15 @@ +-- +-- pg_size_pretty_bigint +-- + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 10::= bigint) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= ::bigint) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000::bigint) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000::bigint) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000::bigint) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000000::bigint) pg_size_pretty(size); diff --git a/src/test/regress/sql/pg_size_pretty_numeric.sql b/src/test/r= egress/sql/pg_size_pretty_numeric.sql new file mode 100644 index 0000000..c1b626e --- /dev/null +++ b/src/test/regress/sql/pg_size_pretty_numeric.sql @@ -0,0 +1,29 @@ + +-- +-- pg_size_pretty_numeric +-- + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 10::= numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= ::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000000::numeric) pg_size_pretty(size); + + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 10.5= ::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= =2E5::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000.5::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000.5::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000.5::numeric) pg_size_pretty(size); + +SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 1000= 000000000000.5::numeric) pg_size_pretty(size); --=20 2.6.2 --------------050409040701060702010406-- --C2nQfsfk4kGEOMa2Oso9wwgefH8Xavud9 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQEcBAEBCAAGBQJWO8fsAAoJEJg+KbLX08eJaU0H/AubxtlvIob/l+naskP5Sblj CdJCLo53F+29Cj5EGjnK617qRIEua351ZiY1QVNfzFgxhDoimp9nf2xx8hdBqng5 zbLpVG68BubhHz6bxKPdl7eGfx+X2fwSb8Qw/My5K/0iSb5iD0cnJNT/Gwsjdpcf J+TQXCXmwE2vtNupMJLZ1aFicd9CaaPXfmwO0roaSVyjNdth5x5E2fe0+k9xf23p Bno9m7fpKml4yryM9BNzhwt+xKBH/ToIGRqhvEnFXV6Ib5/W3xHQpX5zHXbicvWA vdymMUkngTBzgKRE4Xbwz33xajzyAQThm1FrqwrgJjiPKM5t37oWNcu4+bTNACU= =tuye -----END PGP SIGNATURE----- --C2nQfsfk4kGEOMa2Oso9wwgefH8Xavud9--