Skip site navigation (1) Skip section navigation (2)

Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64

From: Dave Page <dpage(at)postgresql(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Dave Page <dpage(at)postgresql(dot)org>, Alvaro Herrera <alvherre(at)commandprompt(dot)com>, pgsql-committers <pgsql-committers(at)postgresql(dot)org>
Subject: Re: pgsql: Fix pg_size_pretty() to avoid overflow for inputs close to INT64
Date: 2011-05-01 09:15:51
Message-ID: BANLkTinyNpGi5wUh=HnHE-FFo3yrRQ1cRQ@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-committerspgsql-hackers
Looks like you committed this before I got to it - moa is green, so I
guess it works.

Thanks.

On Thursday, April 28, 2011, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Please see if the attached version works.
>
>                         regards, tom lane
>
>
> Datum
> pg_size_pretty(PG_FUNCTION_ARGS)
> {
>         int64           size = PG_GETARG_INT64(0);
>         char            buf[64];
>         int64           limit = 10 * 1024;
>         int64           limit2 = limit * 2 - 1;
>
>         if (size < limit)
>                 snprintf(buf, sizeof(buf), INT64_FORMAT " bytes", size);
>         else
>         {
>                 size >>= 9;                             /* keep one extra bit for rounding */
>                 if (size < limit2)
>                         snprintf(buf, sizeof(buf), INT64_FORMAT " kB",
>                                          (size + 1) / 2);
>                 else
>                 {
>                         size >>= 10;
>                         if (size < limit2)
>                                 snprintf(buf, sizeof(buf), INT64_FORMAT " MB",
>                                                  (size + 1) / 2);
>                         else
>                         {
>                                 size >>= 10;
>                                 if (size < limit2)
>                                         snprintf(buf, sizeof(buf), INT64_FORMAT " GB",
>                                                          (size + 1) / 2);
>                                 else
>                                 {
>                                         size >>= 10;
>                                         snprintf(buf, sizeof(buf), INT64_FORMAT " TB",
>                                                          (size + 1) / 2);
>                                 }
>                         }
>                 }
>         }
>
>         PG_RETURN_TEXT_P(cstring_to_text(buf));
> }
>

-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

pgsql-hackers by date

Next:From: Pavel StehuleDate: 2011-05-01 09:36:12
Subject: strange view performance
Previous:From: Pavel StehuleDate: 2011-05-01 04:14:49
Subject: Re: increasing collapse_limits?

pgsql-committers by date

Next:From: Tom LaneDate: 2011-05-01 21:58:17
Subject: pgsql: Make CLUSTER lock the old table's toast table before copyingdat
Previous:From: Tom LaneDate: 2011-04-30 17:39:37
Subject: Re: Re: [COMMITTERS] pgsql: Update docs to say you need fsync to make sync rep work fast.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group