]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
pg_size_pretty(numeric)
authorRobert Haas <rhaas@postgresql.org>
Sat, 14 Apr 2012 12:04:11 +0000 (08:04 -0400)
committerRobert Haas <rhaas@postgresql.org>
Sat, 14 Apr 2012 12:07:25 +0000 (08:07 -0400)
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.

doc/src/sgml/func.sgml
src/backend/utils/adt/dbsize.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.h
src/include/utils/builtins.h

index 29d907ddd17a92fabdf1f288421668a0ef171354..82bac4d664141e356f4e6db9fccaebabf6d9fead 100644 (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>
index 26a8c01432c7048c0d15af798d401f49906f03a9..fd19de72cb678faf3fa43ccb41847115899122c8 100644 (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
  *
index d582015a5d55bcb89b96153ab352d43d9f79474a..a43491503a9fb0c3ceb591628f4fdedfae41aaa5 100644 (file)
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     201204131
+#define CATALOG_VERSION_NO     201204141
 
 #endif
index aa4d35072da3a7698a75b61e1aea2a04f1276bf9..079b0b212bcbbfec689227a6a1c72419d4e932c6 100644 (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_ ));
index 201b23ec9d78d5a4445f38825dd761ed8b9d37a1..f246f117ba3db02beb4e624010ef8ed6320757a4 100644 (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);