pg_size_pretty(numeric)

The output of the new pg_xlog_location_diff function is of type numeric,
since it could theoretically overflow an int8 due to signedness; this
provides a convenient way to format such values.

Fujii Masao, with some beautification by me.
This commit is contained in:
Robert Haas 2012-04-14 08:04:11 -04:00
parent d4db16d303
commit 4a2d7ad76f
5 changed files with 150 additions and 2 deletions

View File

@ -14981,7 +14981,20 @@ postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
<literal><function>pg_size_pretty(<type>bigint</type>)</function></literal>
</entry>
<entry><type>text</type></entry>
<entry>Converts a size in bytes into a human-readable format with size units</entry>
<entry>
Converts a size in bytes expressed as a 64-bit integer into a
human-readable format with size units
</entry>
</row>
<row>
<entry>
<literal><function>pg_size_pretty(<type>numeric</type>)</function></literal>
</entry>
<entry><type>text</type></entry>
<entry>
Converts a size in bytes expressed as a numeric value into a
human-readable format with size units
</entry>
</row>
<row>
<entry>

View File

@ -24,6 +24,7 @@
#include "storage/fd.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
#include "utils/rel.h"
#include "utils/relmapper.h"
#include "utils/syscache.h"
@ -550,6 +551,137 @@ pg_size_pretty(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(cstring_to_text(buf));
}
static char *
numeric_to_cstring(Numeric n)
{
Datum d = NumericGetDatum(n);
return DatumGetCString(DirectFunctionCall1(numeric_out, d));
}
static Numeric
int64_to_numeric(int64 v)
{
Datum d = Int64GetDatum(v);
return DatumGetNumeric(DirectFunctionCall1(int8_numeric, d));
}
static bool
numeric_is_less(Numeric a, Numeric b)
{
Datum da = NumericGetDatum(a);
Datum db = NumericGetDatum(b);
return DatumGetBool(DirectFunctionCall2(numeric_lt, da, db));
}
static Numeric
numeric_plus_one_over_two(Numeric n)
{
Datum d = NumericGetDatum(n);
Datum one;
Datum two;
Datum result;
one = DirectFunctionCall1(int8_numeric, Int64GetDatum(1));
two = DirectFunctionCall1(int8_numeric, Int64GetDatum(2));
result = DirectFunctionCall2(numeric_add, d, one);
result = DirectFunctionCall2(numeric_div_trunc, result, two);
return DatumGetNumeric(result);
}
static Numeric
numeric_shift_right(Numeric n, unsigned count)
{
Datum d = NumericGetDatum(n);
Datum divisor_int64;
Datum divisor_numeric;
Datum result;
divisor_int64 = Int64GetDatum((int64) (1 << count));
divisor_numeric = DirectFunctionCall1(int8_numeric, divisor_int64);
result = DirectFunctionCall2(numeric_div_trunc, d, divisor_numeric);
return DatumGetNumeric(result);
}
Datum
pg_size_pretty_numeric(PG_FUNCTION_ARGS)
{
Numeric size = PG_GETARG_NUMERIC(0);
Numeric limit,
limit2;
char *buf,
*result;
limit = int64_to_numeric(10 * 1024);
limit2 = int64_to_numeric(10 * 1024 * 2 - 1);
if (numeric_is_less(size, limit))
{
buf = numeric_to_cstring(size);
result = palloc(strlen(buf) + 7);
strcpy(result, buf);
strcat(result, " bytes");
}
else
{
/* keep one extra bit for rounding */
/* size >>= 9 */
size = numeric_shift_right(size, 9);
if (numeric_is_less(size, limit2))
{
/* size = (size + 1) / 2 */
size = numeric_plus_one_over_two(size);
buf = numeric_to_cstring(size);
result = palloc(strlen(buf) + 4);
strcpy(result, buf);
strcat(result, " kB");
}
else
{
/* size >>= 10 */
size = numeric_shift_right(size, 10);
if (numeric_is_less(size, limit2))
{
/* size = (size + 1) / 2 */
size = numeric_plus_one_over_two(size);
buf = numeric_to_cstring(size);
result = palloc(strlen(buf) + 4);
strcpy(result, buf);
strcat(result, " MB");
}
else
{
/* size >>= 10 */
size = numeric_shift_right(size, 10);
if (numeric_is_less(size, limit2))
{
/* size = (size + 1) / 2 */
size = numeric_plus_one_over_two(size);
buf = numeric_to_cstring(size);
result = palloc(strlen(buf) + 4);
strcpy(result, buf);
strcat(result, " GB");
}
else
{
/* size >>= 10 */
size = numeric_shift_right(size, 10);
/* size = (size + 1) / 2 */
size = numeric_plus_one_over_two(size);
buf = numeric_to_cstring(size);
result = palloc(strlen(buf) + 4);
strcpy(result, buf);
strcat(result, " TB");
}
}
}
}
PG_RETURN_TEXT_P(cstring_to_text(result));
}
/*
* Get the filenode of a relation
*

View File

@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 201204131
#define CATALOG_VERSION_NO 201204141
#endif

View File

@ -3410,6 +3410,8 @@ DATA(insert OID = 2286 ( pg_total_relation_size PGNSP PGUID 12 1 0 0 0 f f f f t
DESCR("total disk space usage for the specified table and associated indexes");
DATA(insert OID = 2288 ( pg_size_pretty PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 25 "20" _null_ _null_ _null_ _null_ pg_size_pretty _null_ _null_ _null_ ));
DESCR("convert a long int to a human readable text using size units");
DATA(insert OID = 3166 ( pg_size_pretty PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 25 "1700" _null_ _null_ _null_ _null_ pg_size_pretty_numeric _null_ _null_ _null_ ));
DESCR("convert a numeric to a human readable text using size units");
DATA(insert OID = 2997 ( pg_table_size PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 20 "2205" _null_ _null_ _null_ _null_ pg_table_size _null_ _null_ _null_ ));
DESCR("disk space usage for the specified table, including TOAST, free space and visibility map");
DATA(insert OID = 2998 ( pg_indexes_size PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 20 "2205" _null_ _null_ _null_ _null_ pg_indexes_size _null_ _null_ _null_ ));

View File

@ -453,6 +453,7 @@ extern Datum pg_database_size_name(PG_FUNCTION_ARGS);
extern Datum pg_relation_size(PG_FUNCTION_ARGS);
extern Datum pg_total_relation_size(PG_FUNCTION_ARGS);
extern Datum pg_size_pretty(PG_FUNCTION_ARGS);
extern Datum pg_size_pretty_numeric(PG_FUNCTION_ARGS);
extern Datum pg_table_size(PG_FUNCTION_ARGS);
extern Datum pg_indexes_size(PG_FUNCTION_ARGS);
extern Datum pg_relation_filenode(PG_FUNCTION_ARGS);