Skip to content

Commit fd2e2d0

Browse files
committed
Rewrite pg_size_pretty() to avoid compiler bug.
Convert it to use successive shifts right instead of increasing a divisor. This is probably a tad more efficient than the original coding, and it's nicer-looking than the previous patch because we don't need a special case to avoid overflow in the last branch. But the real reason to do it is to avoid a Solaris compiler bug, as per results from buildfarm member moa.
1 parent c49e4ae commit fd2e2d0

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/backend/utils/adt/dbsize.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -466,39 +466,33 @@ pg_size_pretty(PG_FUNCTION_ARGS)
466466
int64 size = PG_GETARG_INT64(0);
467467
char buf[64];
468468
int64 limit = 10 * 1024;
469-
int64 mult = 1;
469+
int64 limit2 = limit * 2 - 1;
470470

471-
if (size < limit * mult)
471+
if (size < limit)
472472
snprintf(buf, sizeof(buf), INT64_FORMAT " bytes", size);
473473
else
474474
{
475-
mult *= 1024;
476-
if (size < limit * mult)
475+
size >>= 9; /* keep one extra bit for rounding */
476+
if (size < limit2)
477477
snprintf(buf, sizeof(buf), INT64_FORMAT " kB",
478-
(size + mult / 2) / mult);
478+
(size + 1) / 2);
479479
else
480480
{
481-
mult *= 1024;
482-
if (size < limit * mult)
481+
size >>= 10;
482+
if (size < limit2)
483483
snprintf(buf, sizeof(buf), INT64_FORMAT " MB",
484-
(size + mult / 2) / mult);
484+
(size + 1) / 2);
485485
else
486486
{
487-
mult *= 1024;
488-
if (size < limit * mult)
487+
size >>= 10;
488+
if (size < limit2)
489489
snprintf(buf, sizeof(buf), INT64_FORMAT " GB",
490-
(size + mult / 2) / mult);
490+
(size + 1) / 2);
491491
else
492492
{
493-
/* Here we have to worry about avoiding overflow */
494-
int64 val;
495-
496-
mult *= 1024;
497-
val = size / mult;
498-
if ((size % mult) >= (mult / 2))
499-
val++;
493+
size >>= 10;
500494
snprintf(buf, sizeof(buf), INT64_FORMAT " TB",
501-
val);
495+
(size + 1) / 2);
502496
}
503497
}
504498
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy