Re: Add support for unit "B" to pg_size_pretty() - Mailing list pgsql-hackers

From Dean Rasheed
Subject Re: Add support for unit "B" to pg_size_pretty()
Date
Msg-id CAEZATCVvKspBjEzxj-EuqFkvjWzdyu+ThBCh4a+QnWmFd3VRUQ@mail.gmail.com
Whole thread Raw
In response to Re: Add support for unit "B" to pg_size_pretty()  (David Rowley <dgrowleyml@gmail.com>)
Responses Re: Add support for unit "B" to pg_size_pretty()
List pgsql-hackers
On Thu, 2 Mar 2023 at 19:58, David Rowley <dgrowleyml@gmail.com> wrote:
>
> I think I'd prefer to see the size_bytes_unit_alias struct have an
> index into size_pretty_units[] array. i.e:
>
> struct size_bytes_unit_alias
> {
>     const char *alias; /* aliased unit name */
>     const int unit_index; /* corresponding size_pretty_units element */
> };
>
> then the pg_size_bytes code can be simplified to:
>
> /* If not found, look in the table of aliases */
> if (unit->name == NULL)
> {
>     for (const struct size_bytes_unit_alias *a = size_bytes_aliases;
> a->alias != NULL; a++)
>     {
>         if (pg_strcasecmp(strptr, a->alias) == 0)
>         {
>             unit = &size_pretty_units[a->unit_index];
>             break;
>         }
>     }
> }
>
> which saves having to have the additional and slower nested loop code.
>

Hmm, I think it would be easier to just have a separate table for
pg_size_bytes(), rather than reusing pg_size_pretty()'s table. I.e.,
size_bytes_units[], which would only need name and multiplier columns
(not round and limit). Done that way, it would be easier to add other
units later (e.g., non-base-2 units).

Also, it looks to me as though the doc change is for pg_size_pretty()
instead of pg_size_bytes().

Regards,
Dean



pgsql-hackers by date:

Previous
From: Alexander Korotkov
Date:
Subject: Re: POC: Lock updated tuples in tuple_update() and tuple_delete()
Next
From: Kirk Wolak
Date:
Subject: Re: proposal: psql: show current user in prompt