mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Add pg_column_size() to return storage size of a column, including
possible compression. Mark Kirkwood
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user