Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a923602

Browse files
committed
Add pg_column_size() to return storage size of a column, including
possible compression. Mark Kirkwood
1 parent b9cb132 commit a923602

File tree

6 files changed

+114
-6
lines changed

6 files changed

+114
-6
lines changed

doc/src/sgml/func.sgml

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.262 2005/06/29 01:52:56 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.263 2005/07/06 19:02:52 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -2186,6 +2186,14 @@ PostgreSQL documentation
21862186
<entry><literal>5</literal></entry>
21872187
</row>
21882188

2189+
<row>
2190+
<entry><literal><function>pg_column_size</function>(<parameter>string</parameter>)</literal></entry>
2191+
<entry><type>integer</type></entry>
2192+
<entry>Number of bytes required to store the value, which might be compressed</entry>
2193+
<entry><literal>pg_column_size('jo\\000se'::bytea)</literal></entry>
2194+
<entry><literal>5</literal></entry>
2195+
</row>
2196+
21892197
<row>
21902198
<entry><literal><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</literal></entry>
21912199
<entry><type>integer</type></entry>

src/backend/access/heap/tuptoaster.c

+43-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.49 2005/03/21 01:23:58 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.50 2005/07/06 19:02:52 momjian Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1436,3 +1436,45 @@ toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
14361436

14371437
return result;
14381438
}
1439+
1440+
/* ----------
1441+
* toast_datum_size
1442+
*
1443+
* Show the (possibly compressed) size of a datum
1444+
* ----------
1445+
*/
1446+
Size
1447+
toast_datum_size(Datum value)
1448+
{
1449+
1450+
varattrib *attr = (varattrib *) DatumGetPointer(value);
1451+
Size result;
1452+
1453+
if (VARATT_IS_EXTERNAL(attr))
1454+
{
1455+
/*
1456+
* Attribute is stored externally - If it is compressed too,
1457+
* then we need to get the external datum and calculate its size,
1458+
* otherwise we just use the external rawsize.
1459+
*/
1460+
if (VARATT_IS_COMPRESSED(attr))
1461+
{
1462+
varattrib *attrext = toast_fetch_datum(attr);
1463+
result = VARSIZE(attrext);
1464+
pfree(attrext);
1465+
}
1466+
else
1467+
result = attr->va_content.va_external.va_rawsize;
1468+
}
1469+
else
1470+
{
1471+
/*
1472+
* Attribute is stored inline either compressed or not, just
1473+
* calculate the size of the datum in either case.
1474+
*/
1475+
result = VARSIZE(attr);
1476+
}
1477+
1478+
return result;
1479+
1480+
}

src/backend/utils/adt/varlena.c

+46-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.124 2005/07/04 18:56:44 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.125 2005/07/06 19:02:52 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -28,6 +28,7 @@
2828
#include "utils/builtins.h"
2929
#include "utils/lsyscache.h"
3030
#include "utils/pg_locale.h"
31+
#include "utils/syscache.h"
3132

3233

3334
typedef struct varlena unknown;
@@ -2348,3 +2349,47 @@ md5_bytea(PG_FUNCTION_ARGS)
23482349
result_text = PG_STR_GET_TEXT(hexsum);
23492350
PG_RETURN_TEXT_P(result_text);
23502351
}
2352+
2353+
/*
2354+
* Return the length of a datum, possibly compressed
2355+
*/
2356+
Datum
2357+
pg_column_size(PG_FUNCTION_ARGS)
2358+
{
2359+
Datum value = PG_GETARG_DATUM(0);
2360+
int result;
2361+
2362+
/* fn_extra stores the fixed column length, or -1 for varlena. */
2363+
if (fcinfo->flinfo->fn_extra == NULL) /* first call? */
2364+
{
2365+
/* On the first call lookup the datatype of the supplied argument */
2366+
Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
2367+
HeapTuple tp;
2368+
int typlen;
2369+
2370+
tp = SearchSysCache(TYPEOID,
2371+
ObjectIdGetDatum(argtypeid),
2372+
0, 0, 0);
2373+
if (!HeapTupleIsValid(tp))
2374+
{
2375+
/* Oid not in pg_type, should never happen. */
2376+
ereport(ERROR,
2377+
(errcode(ERRCODE_INTERNAL_ERROR),
2378+
errmsg("invalid typid: %u", argtypeid)));
2379+
}
2380+
2381+
typlen = ((Form_pg_type)GETSTRUCT(tp))->typlen;
2382+
ReleaseSysCache(tp);
2383+
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
2384+
sizeof(int));
2385+
*(int *)fcinfo->flinfo->fn_extra = typlen;
2386+
}
2387+
2388+
if (*(int *)fcinfo->flinfo->fn_extra != -1)
2389+
PG_RETURN_INT32(*(int *)fcinfo->flinfo->fn_extra);
2390+
else
2391+
{
2392+
result = toast_datum_size(value) - VARHDRSZ;
2393+
PG_RETURN_INT32(result);
2394+
}
2395+
}

src/include/access/tuptoaster.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
88
*
9-
* $PostgreSQL: pgsql/src/include/access/tuptoaster.h,v 1.22 2005/03/21 01:24:04 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/access/tuptoaster.h,v 1.23 2005/07/06 19:02:53 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -138,4 +138,12 @@ extern Datum toast_compress_datum(Datum value);
138138
*/
139139
extern Size toast_raw_datum_size(Datum value);
140140

141+
/* ----------
142+
* toast_datum_size -
143+
*
144+
* Return the storage size of a varlena datum
145+
* ----------
146+
*/
147+
extern Size toast_datum_size(Datum value);
148+
141149
#endif /* TUPTOASTER_H */

src/include/catalog/pg_proc.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.373 2005/07/01 19:19:03 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.374 2005/07/06 19:02:53 momjian Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3658,6 +3658,10 @@ DESCR("current value from last used sequence");
36583658
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_ ));
36593659
DESCR("postmaster start time");
36603660

3661+
/* Column storage size */
3662+
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_ ));
3663+
DESCR("bytes required to store the value, perhaps with compression");
3664+
36613665
/* new functions for Y-direction rtree opclasses */
36623666
DATA(insert OID = 2562 ( box_below PGNSP PGUID 12 f f t f i 2 16 "603 603" _null_ _null_ _null_ box_below - _null_ ));
36633667
DESCR("is below");

src/include/utils/builtins.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.258 2005/06/17 22:32:50 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.259 2005/07/06 19:02:54 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -601,6 +601,7 @@ extern Datum byteacat(PG_FUNCTION_ARGS);
601601
extern Datum byteapos(PG_FUNCTION_ARGS);
602602
extern Datum bytea_substr(PG_FUNCTION_ARGS);
603603
extern Datum bytea_substr_no_len(PG_FUNCTION_ARGS);
604+
extern Datum pg_column_size(PG_FUNCTION_ARGS);
604605

605606
/* version.c */
606607
extern Datum pgsql_version(PG_FUNCTION_ARGS);

0 commit comments

Comments
 (0)