diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index 9c39e7d3b3..daf9149be3 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -31,8 +31,8 @@ #include "utils/relmapper.h" #include "utils/syscache.h" -/* Divide by two and round towards positive infinity. */ -#define half_rounded(x) (((x) + ((x) < 0 ? 0 : 1)) / 2) +/* Divide by two and round away from zero */ +#define half_rounded(x) (((x) + ((x) < 0 ? -1 : 1)) / 2) /* Return physical size of directory contents, or 0 if dir doesn't exist */ static int64 @@ -542,25 +542,29 @@ pg_size_pretty(PG_FUNCTION_ARGS) snprintf(buf, sizeof(buf), INT64_FORMAT " bytes", size); else { - size >>= 9; /* keep one extra bit for rounding */ + /* + * We use divide instead of bit shifting so that behavior matches for + * both positive and negative size values. + */ + size /= (1 << 9); /* keep one extra bit for rounding */ if (Abs(size) < limit2) snprintf(buf, sizeof(buf), INT64_FORMAT " kB", half_rounded(size)); else { - size >>= 10; + size /= (1 << 10); if (Abs(size) < limit2) snprintf(buf, sizeof(buf), INT64_FORMAT " MB", half_rounded(size)); else { - size >>= 10; + size /= (1 << 10); if (Abs(size) < limit2) snprintf(buf, sizeof(buf), INT64_FORMAT " GB", half_rounded(size)); else { - size >>= 10; + size /= (1 << 10); snprintf(buf, sizeof(buf), INT64_FORMAT " TB", half_rounded(size)); }