Skip to content

Commit ca2e447

Browse files
committed
Teach pg_size_pretty and pg_size_bytes about petabytes
There was talk about adding units all the way up to yottabytes but it seems quite far-fetched that anyone would need those. Since such large units are not exactly commonplace, it seems unlikely that having pg_size_pretty outputting unit any larger than petabytes would actually be helpful to anyone. Since petabytes are on the horizon, let's just add those only. Maybe one day we'll get to add additional units, but it will likely be a while before we'll need to think beyond petabytes in regards to the size of a database. Author: David Christensen Discussion: https://postgr.es/m/CAOxo6XKmHc_WZip-x5QwaOqFEiCq_SVD0B7sbTZQk+qqcn2qaw@mail.gmail.com
1 parent 0f80b47 commit ca2e447

File tree

3 files changed

+72
-64
lines changed

3 files changed

+72
-64
lines changed

src/backend/utils/adt/dbsize.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static const struct size_pretty_unit size_pretty_units[] = {
5252
{"MB", 20 * 1024 - 1, true, 20},
5353
{"GB", 20 * 1024 - 1, true, 30},
5454
{"TB", 20 * 1024 - 1, true, 40},
55+
{"PB", 20 * 1024 - 1, true, 50},
5556
{NULL, 0, false, 0}
5657
};
5758

@@ -811,7 +812,7 @@ pg_size_bytes(PG_FUNCTION_ARGS)
811812
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
812813
errmsg("invalid size: \"%s\"", text_to_cstring(arg)),
813814
errdetail("Invalid size unit: \"%s\".", strptr),
814-
errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", and \"TB\".")));
815+
errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\".")));
815816

816817
if (multiplier > 1)
817818
{

src/test/regress/expected/dbsize.out

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -53,79 +53,85 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM
5353
10994579406847 | 10239 GB | -10239 GB
5454
10994579406848 | 10 TB | -10 TB
5555
11258449312612351 | 10239 TB | -10239 TB
56-
11258449312612352 | 10240 TB | -10240 TB
56+
11258449312612352 | 10 PB | -10 PB
5757
(10 rows)
5858

5959
SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM
6060
(VALUES (10239::numeric), (10240::numeric),
6161
(10485247::numeric), (10485248::numeric),
6262
(10736893951::numeric), (10736893952::numeric),
6363
(10994579406847::numeric), (10994579406848::numeric),
64-
(11258449312612351::numeric), (11258449312612352::numeric)) x(size);
65-
size | pg_size_pretty | pg_size_pretty
66-
-------------------+----------------+----------------
67-
10239 | 10239 bytes | -10239 bytes
68-
10240 | 10 kB | -10 kB
69-
10485247 | 10239 kB | -10239 kB
70-
10485248 | 10 MB | -10 MB
71-
10736893951 | 10239 MB | -10239 MB
72-
10736893952 | 10 GB | -10 GB
73-
10994579406847 | 10239 GB | -10239 GB
74-
10994579406848 | 10 TB | -10 TB
75-
11258449312612351 | 10239 TB | -10239 TB
76-
11258449312612352 | 10240 TB | -10240 TB
77-
(10 rows)
64+
(11258449312612351::numeric), (11258449312612352::numeric),
65+
(11528652096115048447::numeric), (11528652096115048448::numeric)) x(size);
66+
size | pg_size_pretty | pg_size_pretty
67+
----------------------+----------------+----------------
68+
10239 | 10239 bytes | -10239 bytes
69+
10240 | 10 kB | -10 kB
70+
10485247 | 10239 kB | -10239 kB
71+
10485248 | 10 MB | -10 MB
72+
10736893951 | 10239 MB | -10239 MB
73+
10736893952 | 10 GB | -10 GB
74+
10994579406847 | 10239 GB | -10239 GB
75+
10994579406848 | 10 TB | -10 TB
76+
11258449312612351 | 10239 TB | -10239 TB
77+
11258449312612352 | 10 PB | -10 PB
78+
11528652096115048447 | 10239 PB | -10239 PB
79+
11528652096115048448 | 10240 PB | -10240 PB
80+
(12 rows)
7881

7982
-- pg_size_bytes() tests
8083
SELECT size, pg_size_bytes(size) FROM
8184
(VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '),
82-
('1TB'), ('3000 TB'), ('1e6 MB')) x(size);
83-
size | pg_size_bytes
84-
----------+------------------
85-
1 | 1
86-
123bytes | 123
87-
1kB | 1024
88-
1MB | 1048576
89-
1 GB | 1073741824
90-
1.5 GB | 1610612736
91-
1TB | 1099511627776
92-
3000 TB | 3298534883328000
93-
1e6 MB | 1048576000000
94-
(9 rows)
85+
('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size);
86+
size | pg_size_bytes
87+
----------+--------------------
88+
1 | 1
89+
123bytes | 123
90+
1kB | 1024
91+
1MB | 1048576
92+
1 GB | 1073741824
93+
1.5 GB | 1610612736
94+
1TB | 1099511627776
95+
3000 TB | 3298534883328000
96+
1e6 MB | 1048576000000
97+
99 PB | 111464090777419776
98+
(10 rows)
9599

96100
-- case-insensitive units are supported
97101
SELECT size, pg_size_bytes(size) FROM
98102
(VALUES ('1'), ('123bYteS'), ('1kb'), ('1mb'), (' 1 Gb'), ('1.5 gB '),
99-
('1tb'), ('3000 tb'), ('1e6 mb')) x(size);
100-
size | pg_size_bytes
101-
----------+------------------
102-
1 | 1
103-
123bYteS | 123
104-
1kb | 1024
105-
1mb | 1048576
106-
1 Gb | 1073741824
107-
1.5 gB | 1610612736
108-
1tb | 1099511627776
109-
3000 tb | 3298534883328000
110-
1e6 mb | 1048576000000
111-
(9 rows)
103+
('1tb'), ('3000 tb'), ('1e6 mb'), ('99 pb')) x(size);
104+
size | pg_size_bytes
105+
----------+--------------------
106+
1 | 1
107+
123bYteS | 123
108+
1kb | 1024
109+
1mb | 1048576
110+
1 Gb | 1073741824
111+
1.5 gB | 1610612736
112+
1tb | 1099511627776
113+
3000 tb | 3298534883328000
114+
1e6 mb | 1048576000000
115+
99 pb | 111464090777419776
116+
(10 rows)
112117

113118
-- negative numbers are supported
114119
SELECT size, pg_size_bytes(size) FROM
115120
(VALUES ('-1'), ('-123bytes'), ('-1kb'), ('-1mb'), (' -1 Gb'), ('-1.5 gB '),
116-
('-1tb'), ('-3000 TB'), ('-10e-1 MB')) x(size);
117-
size | pg_size_bytes
118-
-----------+-------------------
119-
-1 | -1
120-
-123bytes | -123
121-
-1kb | -1024
122-
-1mb | -1048576
123-
-1 Gb | -1073741824
124-
-1.5 gB | -1610612736
125-
-1tb | -1099511627776
126-
-3000 TB | -3298534883328000
127-
-10e-1 MB | -1048576
128-
(9 rows)
121+
('-1tb'), ('-3000 TB'), ('-10e-1 MB'), ('-99 PB')) x(size);
122+
size | pg_size_bytes
123+
-----------+---------------------
124+
-1 | -1
125+
-123bytes | -123
126+
-1kb | -1024
127+
-1mb | -1048576
128+
-1 Gb | -1073741824
129+
-1.5 gB | -1610612736
130+
-1tb | -1099511627776
131+
-3000 TB | -3298534883328000
132+
-10e-1 MB | -1048576
133+
-99 PB | -111464090777419776
134+
(10 rows)
129135

130136
-- different cases with allowed points
131137
SELECT size, pg_size_bytes(size) FROM
@@ -147,15 +153,15 @@ SELECT size, pg_size_bytes(size) FROM
147153
SELECT pg_size_bytes('1 AB');
148154
ERROR: invalid size: "1 AB"
149155
DETAIL: Invalid size unit: "AB".
150-
HINT: Valid units are "bytes", "kB", "MB", "GB", and "TB".
156+
HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB".
151157
SELECT pg_size_bytes('1 AB A');
152158
ERROR: invalid size: "1 AB A"
153159
DETAIL: Invalid size unit: "AB A".
154-
HINT: Valid units are "bytes", "kB", "MB", "GB", and "TB".
160+
HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB".
155161
SELECT pg_size_bytes('1 AB A ');
156162
ERROR: invalid size: "1 AB A "
157163
DETAIL: Invalid size unit: "AB A".
158-
HINT: Valid units are "bytes", "kB", "MB", "GB", and "TB".
164+
HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB".
159165
SELECT pg_size_bytes('9223372036854775807.9');
160166
ERROR: bigint out of range
161167
SELECT pg_size_bytes('1e100');
@@ -165,7 +171,7 @@ ERROR: value overflows numeric format
165171
SELECT pg_size_bytes('1 byte'); -- the singular "byte" is not supported
166172
ERROR: invalid size: "1 byte"
167173
DETAIL: Invalid size unit: "byte".
168-
HINT: Valid units are "bytes", "kB", "MB", "GB", and "TB".
174+
HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB".
169175
SELECT pg_size_bytes('');
170176
ERROR: invalid size: ""
171177
SELECT pg_size_bytes('kb');
@@ -183,6 +189,6 @@ ERROR: invalid size: ".+912"
183189
SELECT pg_size_bytes('+912+ kB');
184190
ERROR: invalid size: "+912+ kB"
185191
DETAIL: Invalid size unit: "+ kB".
186-
HINT: Valid units are "bytes", "kB", "MB", "GB", and "TB".
192+
HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB".
187193
SELECT pg_size_bytes('++123 kB');
188194
ERROR: invalid size: "++123 kB"

src/test/regress/sql/dbsize.sql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,23 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM
2424
(10485247::numeric), (10485248::numeric),
2525
(10736893951::numeric), (10736893952::numeric),
2626
(10994579406847::numeric), (10994579406848::numeric),
27-
(11258449312612351::numeric), (11258449312612352::numeric)) x(size);
27+
(11258449312612351::numeric), (11258449312612352::numeric),
28+
(11528652096115048447::numeric), (11528652096115048448::numeric)) x(size);
2829

2930
-- pg_size_bytes() tests
3031
SELECT size, pg_size_bytes(size) FROM
3132
(VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '),
32-
('1TB'), ('3000 TB'), ('1e6 MB')) x(size);
33+
('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size);
3334

3435
-- case-insensitive units are supported
3536
SELECT size, pg_size_bytes(size) FROM
3637
(VALUES ('1'), ('123bYteS'), ('1kb'), ('1mb'), (' 1 Gb'), ('1.5 gB '),
37-
('1tb'), ('3000 tb'), ('1e6 mb')) x(size);
38+
('1tb'), ('3000 tb'), ('1e6 mb'), ('99 pb')) x(size);
3839

3940
-- negative numbers are supported
4041
SELECT size, pg_size_bytes(size) FROM
4142
(VALUES ('-1'), ('-123bytes'), ('-1kb'), ('-1mb'), (' -1 Gb'), ('-1.5 gB '),
42-
('-1tb'), ('-3000 TB'), ('-10e-1 MB')) x(size);
43+
('-1tb'), ('-3000 TB'), ('-10e-1 MB'), ('-99 PB')) x(size);
4344

4445
-- different cases with allowed points
4546
SELECT size, pg_size_bytes(size) FROM

0 commit comments

Comments
 (0)