diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index b4a2c8d219..9b01d9e3fb 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -562,9 +562,13 @@ pg_size_pretty(PG_FUNCTION_ARGS) for (unit = size_pretty_units; unit->name != NULL; unit++) { uint8 bits; + uint64 abs_size = size < 0 ? 0 - (uint64) size : (uint64) size; - /* use this unit if there are no more units or we're below the limit */ - if (unit[1].name == NULL || Abs(size) < unit->limit) + /* + * Use this unit if there are no more units or the absolute size is + * below the limit for the current unit. + */ + if (unit[1].name == NULL || abs_size < unit->limit) { if (unit->round) size = half_rounded(size); diff --git a/src/test/regress/expected/dbsize.out b/src/test/regress/expected/dbsize.out index d8d6686b5f..b6d0f67893 100644 --- a/src/test/regress/expected/dbsize.out +++ b/src/test/regress/expected/dbsize.out @@ -79,6 +79,14 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM 11528652096115048448 | 10240 PB | -10240 PB (12 rows) +-- Ensure we get the expected results when passing the extremities of bigint +SELECT pg_size_pretty('-9223372036854775808'::bigint), + pg_size_pretty('9223372036854775807'::bigint); + pg_size_pretty | pg_size_pretty +----------------+---------------- + -8192 PB | 8192 PB +(1 row) + -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), diff --git a/src/test/regress/sql/dbsize.sql b/src/test/regress/sql/dbsize.sql index 7df865271b..2a4f9c4521 100644 --- a/src/test/regress/sql/dbsize.sql +++ b/src/test/regress/sql/dbsize.sql @@ -27,6 +27,10 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM (11258449312612351::numeric), (11258449312612352::numeric), (11528652096115048447::numeric), (11528652096115048448::numeric)) x(size); +-- Ensure we get the expected results when passing the extremities of bigint +SELECT pg_size_pretty('-9223372036854775808'::bigint), + pg_size_pretty('9223372036854775807'::bigint); + -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '),