pgsql: Fix incorrect return value in pg_size_pretty(bigint) - Mailing list pgsql-committers

From David Rowley
Subject pgsql: Fix incorrect return value in pg_size_pretty(bigint)
Date
Msg-id E1m1ftt-0002aC-81@gemulon.postgresql.org
Whole thread Raw
Responses Re: pgsql: Fix incorrect return value in pg_size_pretty(bigint)  (David Rowley <dgrowleyml@gmail.com>)
List pgsql-committers
Fix incorrect return value in pg_size_pretty(bigint)

Due to how pg_size_pretty(bigint) was implemented, it's possible that when
given a negative number of bytes that the returning value would not match
the equivalent positive return value when given the equivalent positive
number of bytes.  This was due to two separate issues.

1. The function used bit shifting to convert the number of bytes into
larger units.  The rounding performed by bit shifting is not the same as
dividing.  For example -3 >> 1 = -2, but -3 / 2 = -1.  These two
operations are only equivalent with positive numbers.

2. The half_rounded() macro rounded towards positive infinity.  This meant
that negative numbers rounded towards zero and positive numbers rounded
away from zero.

Here we fix #1 by dividing the values instead of bit shifting.  We fix #2
by adjusting the half_rounded macro always to round away from zero.

Additionally, adjust the pg_size_pretty(numeric) function to be more
explicit that it's using division rather than bit shifting.  A casual
observer might have believed bit shifting was used due to a static
function being named numeric_shift_right.  However, that function was
calculating the divisor from the number of bits and performed division.
Here we make that more clear.  This change is just cosmetic and does not
affect the return value of the numeric version of the function.

Here we also add a set of regression tests both versions of
pg_size_pretty() which test the values directly before and after the
function switches to the next unit.

This bug was introduced in 8a1fab36a. Prior to that negative values were
always displayed in bytes.

Author: Dean Rasheed, David Rowley
Discussion: https://postgr.es/m/CAEZATCXnNW4HsmZnxhfezR5FuiGgp+mkY4AzcL5eRGO4fuadWg@mail.gmail.com
Backpatch-through: 9.6, where the bug was introduced.

Branch
------
REL9_6_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/674ee3b7669444dbfd9a6ec47aa49ead8444349e

Modified Files
--------------
src/backend/utils/adt/dbsize.c       | 39 +++++++++++++++++----------------
src/test/regress/expected/dbsize.out | 42 ++++++++++++++++++++++++++++++++++++
src/test/regress/sql/dbsize.sql      | 16 ++++++++++++++
3 files changed, 79 insertions(+), 18 deletions(-)


pgsql-committers by date:

Previous
From: Bruce Momjian
Date:
Subject: pgsql: doc: minor PG 14 relnote changes
Next
From: David Rowley
Date:
Subject: pgsql: Fix incorrect return value in pg_size_pretty(bigint)