Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8a1fab3

Browse files
committed
pg_size_pretty: Format negative values similar to positive ones.
Previously, negative values were always displayed in bytes, regardlessof how large they were.Adrian Vondendriesch, reviewed by Julien Rouhaud and myself
1 parentdde5f09 commit8a1fab3

File tree

5 files changed

+89
-24
lines changed

5 files changed

+89
-24
lines changed

‎src/backend/utils/adt/dbsize.c

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include"utils/relmapper.h"
3232
#include"utils/syscache.h"
3333

34+
/* Divide by two and round towards positive infinity. */
35+
#definehalf_rounded(x) (((x) + ((x) < 0 ? 0 : 1)) / 2)
3436

3537
/* Return physical size of directory contents, or 0 if dir doesn't exist */
3638
staticint64
@@ -534,31 +536,31 @@ pg_size_pretty(PG_FUNCTION_ARGS)
534536
int64limit=10*1024;
535537
int64limit2=limit*2-1;
536538

537-
if (size<limit)
539+
if (Abs(size)<limit)
538540
snprintf(buf,sizeof(buf),INT64_FORMAT" bytes",size);
539541
else
540542
{
541543
size >>=9;/* keep one extra bit for rounding */
542-
if (size<limit2)
544+
if (Abs(size)<limit2)
543545
snprintf(buf,sizeof(buf),INT64_FORMAT" kB",
544-
(size+1) /2);
546+
half_rounded(size));
545547
else
546548
{
547549
size >>=10;
548-
if (size<limit2)
550+
if (Abs(size)<limit2)
549551
snprintf(buf,sizeof(buf),INT64_FORMAT" MB",
550-
(size+1) /2);
552+
half_rounded(size));
551553
else
552554
{
553555
size >>=10;
554-
if (size<limit2)
556+
if (Abs(size)<limit2)
555557
snprintf(buf,sizeof(buf),INT64_FORMAT" GB",
556-
(size+1) /2);
558+
half_rounded(size));
557559
else
558560
{
559561
size >>=10;
560562
snprintf(buf,sizeof(buf),INT64_FORMAT" TB",
561-
(size+1) /2);
563+
half_rounded(size));
562564
}
563565
}
564566
}
@@ -593,17 +595,34 @@ numeric_is_less(Numeric a, Numeric b)
593595
}
594596

595597
staticNumeric
596-
numeric_plus_one_over_two(Numericn)
598+
numeric_absolute(Numericn)
597599
{
598600
Datumd=NumericGetDatum(n);
601+
Datumresult;
602+
603+
result=DirectFunctionCall1(numeric_abs,d);
604+
returnDatumGetNumeric(result);
605+
}
606+
607+
staticNumeric
608+
numeric_half_rounded(Numericn)
609+
{
610+
Datumd=NumericGetDatum(n);
611+
Datumzero;
599612
Datumone;
600613
Datumtwo;
601614
Datumresult;
602615

616+
zero=DirectFunctionCall1(int8_numeric,Int64GetDatum(0));
603617
one=DirectFunctionCall1(int8_numeric,Int64GetDatum(1));
604618
two=DirectFunctionCall1(int8_numeric,Int64GetDatum(2));
605-
result=DirectFunctionCall2(numeric_add,d,one);
606-
result=DirectFunctionCall2(numeric_div_trunc,result,two);
619+
620+
if (DatumGetBool(DirectFunctionCall2(numeric_ge,d,zero)))
621+
d=DirectFunctionCall2(numeric_add,d,one);
622+
else
623+
d=DirectFunctionCall2(numeric_sub,d,one);
624+
625+
result=DirectFunctionCall2(numeric_div_trunc,d,two);
607626
returnDatumGetNumeric(result);
608627
}
609628

@@ -632,7 +651,7 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS)
632651
limit=int64_to_numeric(10*1024);
633652
limit2=int64_to_numeric(10*1024*2-1);
634653

635-
if (numeric_is_less(size,limit))
654+
if (numeric_is_less(numeric_absolute(size),limit))
636655
{
637656
result=psprintf("%s bytes",numeric_to_cstring(size));
638657
}
@@ -642,39 +661,35 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS)
642661
/* size >>= 9 */
643662
size=numeric_shift_right(size,9);
644663

645-
if (numeric_is_less(size,limit2))
664+
if (numeric_is_less(numeric_absolute(size),limit2))
646665
{
647-
/* size = (size + 1) / 2 */
648-
size=numeric_plus_one_over_two(size);
666+
size=numeric_half_rounded(size);
649667
result=psprintf("%s kB",numeric_to_cstring(size));
650668
}
651669
else
652670
{
653671
/* size >>= 10 */
654672
size=numeric_shift_right(size,10);
655-
if (numeric_is_less(size,limit2))
673+
if (numeric_is_less(numeric_absolute(size),limit2))
656674
{
657-
/* size = (size + 1) / 2 */
658-
size=numeric_plus_one_over_two(size);
675+
size=numeric_half_rounded(size);
659676
result=psprintf("%s MB",numeric_to_cstring(size));
660677
}
661678
else
662679
{
663680
/* size >>= 10 */
664681
size=numeric_shift_right(size,10);
665682

666-
if (numeric_is_less(size,limit2))
683+
if (numeric_is_less(numeric_absolute(size),limit2))
667684
{
668-
/* size = (size + 1) / 2 */
669-
size=numeric_plus_one_over_two(size);
685+
size=numeric_half_rounded(size);
670686
result=psprintf("%s GB",numeric_to_cstring(size));
671687
}
672688
else
673689
{
674690
/* size >>= 10 */
675691
size=numeric_shift_right(size,10);
676-
/* size = (size + 1) / 2 */
677-
size=numeric_plus_one_over_two(size);
692+
size=numeric_half_rounded(size);
678693
result=psprintf("%s TB",numeric_to_cstring(size));
679694
}
680695
}

‎src/test/regress/expected/dbsize.out

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM
2+
(VALUES (10::bigint), (1000::bigint), (1000000::bigint),
3+
(1000000000::bigint), (1000000000000::bigint),
4+
(1000000000000000::bigint)) x(size);
5+
size | pg_size_pretty | pg_size_pretty
6+
------------------+----------------+----------------
7+
10 | 10 bytes | -10 bytes
8+
1000 | 1000 bytes | -1000 bytes
9+
1000000 | 977 kB | -977 kB
10+
1000000000 | 954 MB | -954 MB
11+
1000000000000 | 931 GB | -931 GB
12+
1000000000000000 | 909 TB | -909 TB
13+
(6 rows)
14+
15+
SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM
16+
(VALUES (10::numeric), (1000::numeric), (1000000::numeric),
17+
(1000000000::numeric), (1000000000000::numeric),
18+
(1000000000000000::numeric),
19+
(10.5::numeric), (1000.5::numeric), (1000000.5::numeric),
20+
(1000000000.5::numeric), (1000000000000.5::numeric),
21+
(1000000000000000.5::numeric)) x(size);
22+
size | pg_size_pretty | pg_size_pretty
23+
--------------------+----------------+----------------
24+
10 | 10 bytes | -10 bytes
25+
1000 | 1000 bytes | -1000 bytes
26+
1000000 | 977 kB | -977 kB
27+
1000000000 | 954 MB | -954 MB
28+
1000000000000 | 931 GB | -931 GB
29+
1000000000000000 | 909 TB | -909 TB
30+
10.5 | 10.5 bytes | -10.5 bytes
31+
1000.5 | 1000.5 bytes | -1000.5 bytes
32+
1000000.5 | 977 kB | -977 kB
33+
1000000000.5 | 954 MB | -954 MB
34+
1000000000000.5 | 931 GB | -931 GB
35+
1000000000000000.5 | 909 TB | -909 TB
36+
(12 rows)
37+

‎src/test/regress/parallel_schedule

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ test: brin gin gist spgist privileges security_label collate matview lock replic
8989
# ----------
9090
# Another group of parallel tests
9191
# ----------
92-
test: alter_generic alter_operator misc psql async
92+
test: alter_generic alter_operator misc psql async dbsize
9393

9494
# rules cannot run concurrently with any test that creates a view
9595
test: rules

‎src/test/regress/serial_schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ test: alter_operator
117117
test: misc
118118
test: psql
119119
test: async
120+
test: dbsize
120121
test: rules
121122
test: select_views
122123
test: portals_p2

‎src/test/regress/sql/dbsize.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
SELECT size, pg_size_pretty(size), pg_size_pretty(-1* size)FROM
2+
(VALUES (10::bigint), (1000::bigint), (1000000::bigint),
3+
(1000000000::bigint), (1000000000000::bigint),
4+
(1000000000000000::bigint)) x(size);
5+
6+
SELECT size, pg_size_pretty(size), pg_size_pretty(-1* size)FROM
7+
(VALUES (10::numeric), (1000::numeric), (1000000::numeric),
8+
(1000000000::numeric), (1000000000000::numeric),
9+
(1000000000000000::numeric),
10+
(10.5::numeric), (1000.5::numeric), (1000000.5::numeric),
11+
(1000000000.5::numeric), (1000000000000.5::numeric),
12+
(1000000000000000.5::numeric)) x(size);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp