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

From David Rowley
Subject Re: Add support for unit "B" to pg_size_pretty()
Date
Msg-id CAApHDvpzxuo5F8-zbDoK7ZFmVjMbvVK3XQjNcZZuGcMhBc0V5w@mail.gmail.com
Whole thread Raw
In response to Re: Add support for unit "B" to pg_size_pretty()  (Peter Eisentraut <peter.eisentraut@enterprisedb.com>)
Responses Re: Add support for unit "B" to pg_size_pretty()  (Dean Rasheed <dean.a.rasheed@gmail.com>)
Re: Add support for unit "B" to pg_size_pretty()  (Peter Eisentraut <peter.eisentraut@enterprisedb.com>)
List pgsql-hackers
On Mon, 27 Feb 2023 at 21:34, Peter Eisentraut
<peter.eisentraut@enterprisedb.com> wrote:
>
> On 22.02.23 03:39, David Rowley wrote:
> > I think you'll need to find another way to make the aliases work.
> > Maybe another array with the name and an int to reference the
> > corresponding index in size_pretty_units.
>
> Ok, here is a new patch with a separate table of aliases.  (Might look
> like overkill, but I think the "PiB" etc. example you had could actually
> be a good use case for this as well.)

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.

Apart from that, the patch looks fine.

David



pgsql-hackers by date:

Previous
From: Jeff Davis
Date:
Subject: Re: Minimal logical decoding on standbys
Next
From: Jacob Champion
Date:
Subject: Re: Auth extensions, with an LDAP/SCRAM example [was: Proposal: Support custom authentication methods using hooks]