Re: BUG #12989: pg_size_pretty with negative values

From: "Adrian(dot)Vondendriesch" <Adrian(dot)Vondendriesch(at)credativ(dot)de>
To: "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>, pgsql-hackers(at)postgresql(dot)org
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, david(dot)g(dot)johnston(at)gmail(dot)com, Christian Almeida <cbalmeida(at)gmail(dot)com>
Subject: Re: BUG #12989: pg_size_pretty with negative values
Date: 2015-09-20 12:16:15
Message-ID: 55FEA38F.40301@credativ.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

Hi all,

Am 06.04.2015 um 20:52 schrieb Tom Lane:
> "David G. Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> 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.
>
> I'd say it's a feature request --- a perfectly reasonable one, but I doubt
> 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.

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 100000000000000::bigint) foo(size);
> pg_size_pretty | pg_size_pretty
> ----------------+------------------------
> 91 TB | -100000000000000 bytes
> (1 row)

> SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 100000000000000::numeric) foo(size);
> pg_size_pretty | pg_size_pretty
> ----------------+------------------------
> 91 TB | -100000000000000 bytes
> (1 row)

After the patch:

> SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 100000000000000::bigint) foo(size);
> pg_size_pretty | pg_size_pretty
> ----------------+----------------
> 91 TB | -91 TB
> (1 row)

> SELECT pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (SELECT 100000000000000::numeric) foo(size);
> pg_size_pretty | pg_size_pretty
> ----------------+----------------
> 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

Attachment Content-Type Size
0001-Make-pg_size_pretty-handle-negative-values-v1.patch text/x-patch 12.8 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message manoj.sagar 2015-09-21 07:49:00 BUG #13627: System Hangs During Update Query
Previous Message rysiek 2015-09-20 02:37:54 BUG #13625: LDAP connections via UNIX sockets

Browse pgsql-hackers by date

  From Date Subject
Next Message Xiong He 2015-09-20 13:06:48 Re: A better translation version of Chinese forpsql/po/zh_CN.po file
Previous Message Pavel Stehule 2015-09-20 05:31:04 Re: [patch] Proposal for \rotate in psql