Add pg_column_size() to return storage size of a column, including
authorBruce Momjian
Wed, 6 Jul 2005 19:02:54 +0000 (19:02 +0000)
committerBruce Momjian
Wed, 6 Jul 2005 19:02:54 +0000 (19:02 +0000)
possible compression.

Mark Kirkwood

doc/src/sgml/func.sgml
src/backend/access/heap/tuptoaster.c
src/backend/utils/adt/varlena.c
src/include/access/tuptoaster.h
src/include/catalog/pg_proc.h
src/include/utils/builtins.h

index d837219421e61d0842a21c0b32520a3b0acba0d4..4ff434aca876d1cef2cf4bb2da5970c7601f166c 100644 (file)
@@ -1,5 +1,5 @@
 
 
@@ -2186,6 +2186,14 @@ PostgreSQL documentation
        5
       
 
+      
+       pg_column_size(string)
+       integer
+       Number of bytes required to store the value, which might be compressed
+       pg_column_size('jo\\000se'::bytea)
+       5
+      
+
       
        position(substring in string)
        integer
index 3a3e2d301b6313ffaa96dabb702d0d1470d4b809..77caf79dd190b0144f510fd02fe18f9fd50c3e58 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.49 2005/03/21 01:23:58 tgl Exp $
+ *   $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.50 2005/07/06 19:02:52 momjian Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -1436,3 +1436,45 @@ toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
 
    return result;
 }
+
+/* ----------
+ * toast_datum_size
+ *
+ * Show the (possibly compressed) size of a datum
+ * ----------
+ */
+Size 
+toast_datum_size(Datum value)
+{
+
+   varattrib   *attr = (varattrib *) DatumGetPointer(value);
+   Size        result;
+
+   if (VARATT_IS_EXTERNAL(attr))
+   {
+       /*
+        * Attribute is stored externally - If it is compressed too, 
+        * then we need to get the external datum and calculate its size,
+        * otherwise we just use the external rawsize.
+        */
+       if (VARATT_IS_COMPRESSED(attr))
+       {
+           varattrib       *attrext = toast_fetch_datum(attr);
+           result = VARSIZE(attrext);
+           pfree(attrext);
+       }
+       else
+           result = attr->va_content.va_external.va_rawsize;
+   }
+   else
+   {
+       /*
+        * Attribute is stored inline either compressed or not, just
+        * calculate the size of the datum in either case.
+        */
+       result = VARSIZE(attr);
+   }
+
+   return result;
+   
+}
index f114f96cf7fc302e731ca14f3fd576cf000a3f94..0568c2eea7a4bc114cd1de87f30e80d0651986b5 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.124 2005/07/04 18:56:44 momjian Exp $
+ *   $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.125 2005/07/06 19:02:52 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -28,6 +28,7 @@
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
 #include "utils/pg_locale.h"
+#include "utils/syscache.h"
 
 
 typedef struct varlena unknown;
@@ -2348,3 +2349,47 @@ md5_bytea(PG_FUNCTION_ARGS)
    result_text = PG_STR_GET_TEXT(hexsum);
    PG_RETURN_TEXT_P(result_text);
 }
+
+/* 
+ * Return the length of a datum, possibly compressed
+ */
+Datum
+pg_column_size(PG_FUNCTION_ARGS)
+{
+   Datum           value = PG_GETARG_DATUM(0);
+   int             result;
+
+   /*  fn_extra stores the fixed column length, or -1 for varlena. */
+   if (fcinfo->flinfo->fn_extra == NULL)   /* first call? */
+   {
+       /* On the first call lookup the datatype of the supplied argument */
+       Oid             argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
+       HeapTuple       tp;
+       int             typlen;
+
+       tp = SearchSysCache(TYPEOID,
+                           ObjectIdGetDatum(argtypeid),
+                           0, 0, 0);
+       if (!HeapTupleIsValid(tp))
+       {
+           /* Oid not in pg_type, should never happen. */
+           ereport(ERROR,
+                   (errcode(ERRCODE_INTERNAL_ERROR),
+                    errmsg("invalid typid: %u", argtypeid)));
+       }
+       
+       typlen = ((Form_pg_type)GETSTRUCT(tp))->typlen;
+       ReleaseSysCache(tp);
+       fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
+                                                     sizeof(int));
+       *(int *)fcinfo->flinfo->fn_extra = typlen;
+   }
+
+   if (*(int *)fcinfo->flinfo->fn_extra != -1)
+       PG_RETURN_INT32(*(int *)fcinfo->flinfo->fn_extra);
+   else
+   {
+       result = toast_datum_size(value) - VARHDRSZ;
+       PG_RETURN_INT32(result);
+   }
+}
index 1cf3b4debe64e09e1373dd6a796110c3337a423d..b45df277b1e79526c0fa60bbe388c119cbf06956 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 2000-2005, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/include/access/tuptoaster.h,v 1.22 2005/03/21 01:24:04 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/tuptoaster.h,v 1.23 2005/07/06 19:02:53 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -138,4 +138,12 @@ extern Datum toast_compress_datum(Datum value);
  */
 extern Size toast_raw_datum_size(Datum value);
 
+/* ----------
+ * toast_datum_size -
+ *
+ * Return the storage size of a varlena datum
+ * ----------
+ */
+extern Size toast_datum_size(Datum value);
+
 #endif   /* TUPTOASTER_H */
index 340052792f76cda8d3448c3e6fd635d0ba7b6039..0a213f64be177bf9d0d0d21bd4ffd10333838be4 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.373 2005/07/01 19:19:03 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.374 2005/07/06 19:02:53 momjian Exp $
  *
  * NOTES
  *   The script catalog/genbki.sh reads this file and generates .bki
@@ -3658,6 +3658,10 @@ DESCR("current value from last used sequence");
 DATA(insert OID = 2560 (  pg_postmaster_start_time PGNSP PGUID 12 f f t f s 0 1184 "" _null_ _null_ _null_ pgsql_postmaster_start_time - _null_ ));
 DESCR("postmaster start time");
 
+/* Column storage size */
+DATA(insert OID = 1269 (  pg_column_size      PGNSP PGUID 12 f f t f i 1 23 "2276" _null_ _null_ _null_  pg_column_size - _null_ ));
+DESCR("bytes required to store the value, perhaps with compression");
+
 /* new functions for Y-direction rtree opclasses */
 DATA(insert OID = 2562 (  box_below           PGNSP PGUID 12 f f t f i 2 16 "603 603" _null_ _null_ _null_ box_below - _null_ ));
 DESCR("is below");
index 37423dc73c2e14976d009ef45e6589bcd923f55f..fea496aaaeab9f9495e8d82a35b04fefe51989d6 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.258 2005/06/17 22:32:50 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.259 2005/07/06 19:02:54 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -601,6 +601,7 @@ extern Datum byteacat(PG_FUNCTION_ARGS);
 extern Datum byteapos(PG_FUNCTION_ARGS);
 extern Datum bytea_substr(PG_FUNCTION_ARGS);
 extern Datum bytea_substr_no_len(PG_FUNCTION_ARGS);
+extern Datum pg_column_size(PG_FUNCTION_ARGS);
 
 /* version.c */
 extern Datum pgsql_version(PG_FUNCTION_ARGS);