size_pretty_int4_v1.patch

application/octet-stream

Filename: size_pretty_int4_v1.patch
Type: application/octet-stream
Part: 0
Message: Re: No, pg_size_pretty(numeric) was not such a hot idea

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: context
Series: patch v1
File+
doc/src/sgml/func.sgml 10 0
src/backend/utils/adt/dbsize.c 41 0
src/include/catalog/pg_proc.h 2 0
src/include/utils/builtins.h 1 0
*** a/doc/src/sgml/func.sgml
--- b/doc/src/sgml/func.sgml
***************
*** 14978,14983 **** postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
--- 14978,14993 ----
        </row>
        <row>
         <entry>
+         <literal><function>pg_size_pretty(<type>int</type>)</function></literal>
+         </entry>
+        <entry><type>text</type></entry>
+        <entry>
+          Converts a size in bytes expressed as a 32-bit integer into a
+          human-readable format with size units
+        </entry>
+       </row>
+       <row>
+        <entry>
          <literal><function>pg_size_pretty(<type>bigint</type>)</function></literal>
          </entry>
         <entry><type>text</type></entry>
*** a/src/backend/utils/adt/dbsize.c
--- b/src/backend/utils/adt/dbsize.c
***************
*** 551,556 **** pg_size_pretty(PG_FUNCTION_ARGS)
--- 551,597 ----
  	PG_RETURN_TEXT_P(cstring_to_text(buf));
  }
  
+ Datum
+ pg_size_pretty_int4(PG_FUNCTION_ARGS)
+ {
+ 	int32		size = PG_GETARG_INT32(0);
+ 	char		buf[64];
+ 	int32		limit = 10 * 1024;
+ 	int32		limit2 = limit * 2 - 1;
+ 
+ 	if (size < limit)
+ 		snprintf(buf, sizeof(buf), "%d bytes", size);
+ 	else
+ 	{
+ 		size >>= 9;				/* keep one extra bit for rounding */
+ 		if (size < limit2)
+ 			snprintf(buf, sizeof(buf), "%d kB",
+ 					 (size + 1) / 2);
+ 		else
+ 		{
+ 			size >>= 10;
+ 			if (size < limit2)
+ 				snprintf(buf, sizeof(buf), "%d MB",
+ 						 (size + 1) / 2);
+ 			else
+ 			{
+ 				size >>= 10;
+ 				if (size < limit2)
+ 					snprintf(buf, sizeof(buf), "%d GB",
+ 							 (size + 1) / 2);
+ 				else
+ 				{
+ 					size >>= 10;
+ 					snprintf(buf, sizeof(buf), "%d TB",
+ 							 (size + 1) / 2);
+ 				}
+ 			}
+ 		}
+ 	}
+ 
+ 	PG_RETURN_TEXT_P(cstring_to_text(buf));
+ }
+ 
  static char *
  numeric_to_cstring(Numeric n)
  {
*** a/src/include/catalog/pg_proc.h
--- b/src/include/catalog/pg_proc.h
***************
*** 3410,3415 **** DATA(insert OID = 2286 ( pg_total_relation_size PGNSP PGUID 12 1 0 0 0 f f f f t
--- 3410,3417 ----
  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 = 3167 ( pg_size_pretty			PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 25 "23" _null_ _null_ _null_ _null_ pg_size_pretty_int4 _null_ _null_ _null_ ));
+ DESCR("convert an 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_ ));
*** a/src/include/utils/builtins.h
--- b/src/include/utils/builtins.h
***************
*** 453,458 **** extern Datum pg_database_size_name(PG_FUNCTION_ARGS);
--- 453,459 ----
  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_int4(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);