Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZddXq-0000Wn-Bc for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Sep 2015 12:16:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZddXo-0006tG-IQ for pgsql-bugs@arkaria.postgresql.org; Sun, 20 Sep 2015 12:16:36 +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 1ZddXm-0006t3-OD; Sun, 20 Sep 2015 12:16:35 +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 1ZddXe-0006lE-WB; Sun, 20 Sep 2015 12:16:33 +0000 Received: from gauss.credativ.com (localhost [127.0.0.1]) by gauss.credativ.com (Postfix) with ESMTP id 8FD0D145001D; Sun, 20 Sep 2015 14:16:23 +0200 (CEST) Received: from [192.168.1.126] (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 50E391B4292; Sun, 20 Sep 2015 14:16:23 +0200 (CEST) From: "Adrian.Vondendriesch" Subject: Re: BUG #12989: pg_size_pretty with negative values Cc: Tom Lane , david.g.johnston@gmail.com, Christian Almeida References: <20150406173026.2550.39773@wrigleys.postgresql.org> <31955.1428346338@sss.pgh.pa.us> X-Enigmail-Draft-Status: N1110 To: "pgsql-bugs@postgresql.org" , pgsql-hackers@postgresql.org Message-ID: <55FEA38F.40301@credativ.de> Date: Sun, 20 Sep 2015 14:16:15 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <31955.1428346338@sss.pgh.pa.us> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="OIfu3RtPSFABoDkahqaWBki3QWd24FiQT" 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 an OpenPGP/MIME signed message (RFC 4880 and 3156) --OIfu3RtPSFABoDkahqaWBki3QWd24FiQT Content-Type: multipart/mixed; boundary="------------040000050206050909090004" This is a multi-part message in MIME format. --------------040000050206050909090004 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi all, Am 06.04.2015 um 20:52 schrieb Tom Lane: > "David G. Johnston" writes: >> I'll let a hacker determine whether this is a bug or a feature request= >> though it is a POLA violation in either case. >=20 > I'd say it's a feature request --- a perfectly reasonable one, but I do= ubt > we'd alter the behavior of the function in the back branches. I was also wondering about the described behaviour. IMO pg_size_pretty should handle negative values the same way as positive values are handled= =2E I've attached a patch which implements the requested behaviour. The patch applies clean to HEAD (currently 85eda7e92). AFAICS the documentation doesn't say anything about pg_size_pretty and negative values. So, I didn't touch the documentation. If this is a oversight by me or should be documented after all, I will provide a additional documentation patch. Before the patch: > SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 100= 000000000000::bigint) foo(size); > pg_size_pretty | pg_size_pretty =20 > ----------------+------------------------ > 91 TB | -100000000000000 bytes > (1 row) > SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 100= 000000000000::numeric) foo(size); > pg_size_pretty | pg_size_pretty =20 > ----------------+------------------------ > 91 TB | -100000000000000 bytes > (1 row) After the patch: > SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 100= 000000000000::bigint) foo(size); > pg_size_pretty | pg_size_pretty=20 > ----------------+---------------- > 91 TB | -91 TB > (1 row) > SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 100= 000000000000::numeric) foo(size); > pg_size_pretty | pg_size_pretty=20 > ----------------+---------------- > 91 TB | -91 TB > (1 row) The patch contains two tests (pg_size_pretty_bigint and pg_size_pretty_numeric), to verify that positive and negative values return the same result (except sign). Greetings, - Adrian --------------040000050206050909090004 Content-Type: text/x-patch; name="0001-Make-pg_size_pretty-handle-negative-values-v1.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="0001-Make-pg_size_pretty-handle-negative-values-v1.patch" =46rom 08ae6cdeaf261e156ce5f7622f0d7426c1249485 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 | 61 +++++++++++-----= -- src/include/c.h | 6 ++ .../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 +++++++++ 6 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..97095bb 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -534,31 +534,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); + (size + Sign(size)) / 2); else { size >>=3D 10; - if (size < limit2) + if (Abs(size) < limit2) snprintf(buf, sizeof(buf), INT64_FORMAT " MB", - (size + 1) / 2); + (size + Sign(size)) / 2); else { size >>=3D 10; - if (size < limit2) + if (Abs(size) < limit2) snprintf(buf, sizeof(buf), INT64_FORMAT " GB", - (size + 1) / 2); + (size + Sign(size)) / 2); else { size >>=3D 10; snprintf(buf, sizeof(buf), INT64_FORMAT " TB", - (size + 1) / 2); + (size + Sign(size)) / 2); } } } @@ -593,16 +593,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_divide_by_two(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 +653,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 +663,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_divide_by_two(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_divide_by_two(size); result =3D psprintf("%s MB", numeric_to_cstring(size)); } else @@ -663,18 +682,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_divide_by_two(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_divide_by_two(size); result =3D psprintf("%s TB", numeric_to_cstring(size)); } } diff --git a/src/include/c.h b/src/include/c.h index 8163b00..1fb16f1 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -793,6 +793,12 @@ typedef NameData *Name; #define Abs(x) ((x) >=3D 0 ? (x) : -(x)) =20 /* + * Sign + * Return one if a number is positive 0 otherwise. + */ +#define Sign(x) ((x) >=3D 0 ? 1 : 0) + +/* * StrNCpy * Like standard library function strncpy(), except that result string * is guaranteed to be null-terminated --- that is, at most N-1 bytes 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.5.1 --------------040000050206050909090004-- --OIfu3RtPSFABoDkahqaWBki3QWd24FiQT 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 iQEcBAEBCAAGBQJV/qOPAAoJEJg+KbLX08eJdLAIAJRPjOvYR+KqRXGivM6GKQen C3PwvhWDvTJIriBOc204aZPowRDIIiQAUeP34gRXM88ZJ9NWGr705W0TXD9r1/WF 8KHnNTYXSllOSJrKZJH5HbmHTPeHfTlyl+UoAba5Ihga9KTuhhlb1EZy3hFGfnoe 9v3dxD0iVXgYchxtveQgbgbqHXUouxlmXx0+lTatfATOl7YbgOz+sQO38YStp/3u C+jeXCdzRXvErPEl6Tss1hPAVPAkkmhqJ/B8A8roHEOEpqO0qIhRlfmbxxRHoWOZ +HL2KqtgcSzjb2YG+LGYsYLTj6of7wc82snlP2jA7nXITe7oxRLRW1ih0BUAavA= =vcfs -----END PGP SIGNATURE----- --OIfu3RtPSFABoDkahqaWBki3QWd24FiQT--