Re: [PATCH] expand the units that pg_size_pretty supports on output

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Cc: David Christensen <david(dot)christensen(at)crunchydata(dot)com>, Shinya11(dot)Kato(at)nttdata(dot)com, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCH] expand the units that pg_size_pretty supports on output
Date: 2021-07-06 12:14:47
Message-ID: CAApHDvpn46Lrk-sX7tg9d9A1nUZyAq8s_qBntPDCmYVWNMLxkA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, 6 Jul 2021 at 23:39, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> wrote:
> When I first read this:
>
> + /* half-round until we get down to unitBits */
> + while (rightshifts++ < unit->unitBits)
> + size = half_rounded(size);
>
> it looked to me like it would be invoking half_rounded() multiple
> times, which raised alarm bells because that would risk rounding the
> wrong way. Eventually I realised that by the time it reaches that,
> rightshifts will always equal unit->unitBits or unit->unitBits - 1, so
> it'll never do more than one half-round, which is important.

It's true that based on how the units table is set up now, it'll only
ever do it once for all but the first loop.

I wrote the attached .c file just to try to see if it ever goes wrong
and I didn't manage to find any inputs where it did. I always seem to
get the half rounded value either the same as the shifted value or 1
higher towards positive infinity

$ ./half_rounded -102 3
1. half_round(-102) == -51 :: -102 >> 1 = -51
2. half_round(-51) == -25 :: -51 >> 1 = -26
3. half_round(-25) == -12 :: -26 >> 1 = -13

$ ./half_rounded 6432 3
1. half_round(6432) == 3216 :: 6432 >> 1 = 3216
2. half_round(3216) == 1608 :: 3216 >> 1 = 1608
3. half_round(1608) == 804 :: 1608 >> 1 = 804

Can you give an example where calling half_rounded too many times will
give the wrong value? Keeping in mind we call half_rounded the number
of times that the passed in value would need to be left-shifted by to
get the equivalent truncated value.

David

Attachment Content-Type Size
half_rounded.c text/plain 506 bytes

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Matthias van de Meent 2021-07-06 12:29:51 Re: Removing redundant check for transaction in progress in check_safe_enum_use
Previous Message Greg Nancarrow 2021-07-06 12:06:04 Re: Remove useless int64 range checks on BIGINT sequence MINVALUE/MAXVALUE values