pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix incorrect return value in pg_size_pretty(bigint)
Date: Fri, 09 Jul 2021 02:06:52 +0000
Message-ID: <E1m1fum-0002bk-S8@gemulon.postgresql.org> (raw)

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
------
REL_11_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/45bad6a398ced09b16a3fa47a2a39c37bbc73222

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(-)



view thread (9+ messages)  latest in thread

Message-ID: <E1m1fum-0002bk-S8@gemulon.postgresql.org>
Permalink:  ../E1m1fum-0002bk-S8@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1m1fum-0002bk-S8@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: drowley@postgresql.org
  Subject: Re: pgsql: Fix incorrect return value in pg_size_pretty(bigint)
  In-Reply-To: <E1m1fum-0002bk-S8@gemulon.postgresql.org>
  References: <E1m1fum-0002bk-S8@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox