Skip to content

Commit cb0bcf4

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 c02bc63 commit cb0bcf4

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
@@ -473,39 +473,33 @@ pg_size_pretty(PG_FUNCTION_ARGS)
473473
int64 size = PG_GETARG_INT64(0);
474474
char buf[64];
475475
int64 limit = 10 * 1024;
476-
int64 mult = 1;
476+
int64 limit2 = limit * 2 - 1;
477477

478-
if (size < limit * mult)
478+
if (size < limit)
479479
snprintf(buf, sizeof(buf), INT64_FORMAT " bytes", size);
480480
else
481481
{
482-
mult *= 1024;
483-
if (size < limit * mult)
482+
size >>= 9; /* keep one extra bit for rounding */
483+
if (size < limit2)
484484
snprintf(buf, sizeof(buf), INT64_FORMAT " kB",
485-
(size + mult / 2) / mult);
485+
(size + 1) / 2);
486486
else
487487
{
488-
mult *= 1024;
489-
if (size < limit * mult)
488+
size >>= 10;
489+
if (size < limit2)
490490
snprintf(buf, sizeof(buf), INT64_FORMAT " MB",
491-
(size + mult / 2) / mult);
491+
(size + 1) / 2);
492492
else
493493
{
494-
mult *= 1024;
495-
if (size < limit * mult)
494+
size >>= 10;
495+
if (size < limit2)
496496
snprintf(buf, sizeof(buf), INT64_FORMAT " GB",
497-
(size + mult / 2) / mult);
497+
(size + 1) / 2);
498498
else
499499
{
500-
/* Here we have to worry about avoiding overflow */
501-
int64 val;
502-
503-
mult *= 1024;
504-
val = size / mult;
505-
if ((size % mult) >= (mult / 2))
506-
val++;
500+
size >>= 10;
507501
snprintf(buf, sizeof(buf), INT64_FORMAT " TB",
508-
val);
502+
(size + 1) / 2);
509503
}
510504
}
511505
}

0 commit comments

Comments
 (0)