Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0001e98

Browse files
committed
Code and docs review for pg_column_size() patch.
1 parentf023fc6 commit0001e98

File tree

4 files changed

+130
-108
lines changed

4 files changed

+130
-108
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.275 2005/07/30 22:53:15 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.276 2005/08/02 16:11:56 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -2206,14 +2206,6 @@ PostgreSQL documentation
22062206
<entry><literal>5</literal></entry>
22072207
</row>
22082208

2209-
<row>
2210-
<entry><literal><function>pg_column_size</function>(<parameter>string</parameter>)</literal></entry>
2211-
<entry><type>integer</type></entry>
2212-
<entry>Number of bytes required to store the value, which might be compressed</entry>
2213-
<entry><literal>pg_column_size('jo\\000se'::bytea)</literal></entry>
2214-
<entry><literal>5</literal></entry>
2215-
</row>
2216-
22172209
<row>
22182210
<entry><literal><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</literal></entry>
22192211
<entry><type>integer</type></entry>
@@ -9181,11 +9173,29 @@ SELECT set_config('log_statement_stats', 'off', false);
91819173
</para>
91829174

91839175
<para>
9184-
The functions shown in <xref
9185-
linkend="functions-admin-dbsize"> calculate the actual disk space
9186-
usage of database objects.
9176+
The functions shown in <xref linkend="functions-admin-dbsize"> calculate
9177+
the actual disk space usage of database objects.
91879178
</para>
91889179

9180+
<indexterm zone="functions-admin">
9181+
<primary>pg_column_size</primary>
9182+
</indexterm>
9183+
<indexterm zone="functions-admin">
9184+
<primary>pg_tablespace_size</primary>
9185+
</indexterm>
9186+
<indexterm zone="functions-admin">
9187+
<primary>pg_database_size</primary>
9188+
</indexterm>
9189+
<indexterm zone="functions-admin">
9190+
<primary>pg_relation_size</primary>
9191+
</indexterm>
9192+
<indexterm zone="functions-admin">
9193+
<primary>pg_complete_relation_size</primary>
9194+
</indexterm>
9195+
<indexterm zone="functions-admin">
9196+
<primary>pg_size_pretty</primary>
9197+
</indexterm>
9198+
91899199
<table id="functions-admin-dbsize">
91909200
<title>Database Object Size Functions</title>
91919201
<tgroup cols="3">
@@ -9195,97 +9205,108 @@ SELECT set_config('log_statement_stats', 'off', false);
91959205
</thead>
91969206

91979207
<tbody>
9208+
<row>
9209+
<entry><function>pg_column_size</function>(<parameter>any</parameter>)</entry>
9210+
<entry><type>integer</type></entry>
9211+
<entry>Number of bytes used to store a particular value (possibly compressed)</entry>
9212+
</row>
91989213
<row>
91999214
<entry>
92009215
<literal><function>pg_tablespace_size</function>(<parameter>oid</parameter>)</literal>
92019216
</entry>
9202-
<entry><type>int8</type></entry>
9203-
<entry>Calculates the total disk space used by the tablespace with the specified OID</entry>
9217+
<entry><type>bigint</type></entry>
9218+
<entry>Total disk space used by the tablespace with the specified OID</entry>
92049219
</row>
92059220
<row>
92069221
<entry>
92079222
<literal><function>pg_tablespace_size</function>(<parameter>name</parameter>)</literal>
92089223
</entry>
9209-
<entry><type>int8</type></entry>
9210-
<entry>Calculates the total disk space used by the tablespace with the specified name</entry>
9224+
<entry><type>bigint</type></entry>
9225+
<entry>Total disk space used by the tablespace with the specified name</entry>
92119226
</row>
92129227
<row>
92139228
<entry>
92149229
<literal><function>pg_database_size</function>(<parameter>oid</parameter>)</literal>
92159230
</entry>
9216-
<entry><type>int8</type></entry>
9217-
<entry>Calculates the total disk space used by the database with the specified OID</entry>
9231+
<entry><type>bigint</type></entry>
9232+
<entry>Total disk space used by the database with the specified OID</entry>
92189233
</row>
92199234
<row>
92209235
<entry>
92219236
<literal><function>pg_database_size</function>(<parameter>name</parameter>)</literal>
92229237
</entry>
9223-
<entry><type>int8</type></entry>
9224-
<entry>Calculates the total disk space used by the database with the specified name</entry>
9238+
<entry><type>bigint</type></entry>
9239+
<entry>Total disk space used by the database with the specified name</entry>
92259240
</row>
92269241
<row>
92279242
<entry>
92289243
<literal><function>pg_relation_size</function>(<parameter>oid</parameter>)</literal>
92299244
</entry>
9230-
<entry><type>int8</type></entry>
9231-
<entry>Calculates the disk space used by the table or index with the specified OID</entry>
9245+
<entry><type>bigint</type></entry>
9246+
<entry>Disk space used by the table or index with the specified OID</entry>
92329247
</row>
92339248
<row>
92349249
<entry>
92359250
<literal><function>pg_relation_size</function>(<parameter>text</parameter>)</literal>
92369251
</entry>
9237-
<entry><type>int8</type></entry>
9238-
<entry>Calculates the diskspace used by theindex ortable with the specified name.
9239-
The name may beprefixed with a schema name if required</entry>
9252+
<entry><type>bigint</type></entry>
9253+
<entry>Diskspace used by thetable orindex with the specified name.
9254+
The name may bequalified with a schema name</entry>
92409255
</row>
92419256
<row>
92429257
<entry>
92439258
<literal><function>pg_complete_relation_size</function>(<parameter>oid</parameter>)</literal>
92449259
</entry>
9245-
<entry><type>int8</type></entry>
9246-
<entry>Calculates the total disk space used by the table with the specified OID,
9260+
<entry><type>bigint</type></entry>
9261+
<entry>Total disk space used by the table with the specified OID,
92479262
including indexes and toasted data</entry>
92489263
</row>
92499264
<row>
92509265
<entry>
92519266
<literal><function>pg_complete_relation_size</function>(<parameter>text</parameter>)</literal>
92529267
</entry>
9253-
<entry><type>int8</type></entry>
9254-
<entry>Calculates the total disk space used by the table with the specified name,
9255-
including indexes and toasted data. The name may be prefixed with a schema name if
9256-
required</entry>
9268+
<entry><type>bigint</type></entry>
9269+
<entry>Total disk space used by the table with the specified name,
9270+
including indexes and toasted data.
9271+
The table name may be qualified with a schema name</entry>
92579272
</row>
92589273
<row>
92599274
<entry>
9260-
<literal><function>pg_size_pretty</function>(<parameter>int8</parameter>)</literal>
9275+
<literal><function>pg_size_pretty</function>(<parameter>bigint</parameter>)</literal>
92619276
</entry>
92629277
<entry><type>text</type></entry>
9263-
<entry>Formats the sizevalue (in bytes) into a humanreadable format with size units</entry>
9278+
<entry>Converts a size in bytes into a human-readable format with size units</entry>
92649279
</row>
92659280
</tbody>
92669281
</tgroup>
92679282
</table>
92689283

92699284
<para>
9270-
<function>pg_tablespace_size</>and <function>pg_database_size</> accept an
9271-
oid or name of a tablespace or database, and return the disk space usage of the specified object.
9285+
<function>pg_column_size</>shows the space used to store any individual
9286+
data value.
92729287
</para>
92739288

9274-
<indexterm zone="functions-admin">
9275-
<primary>pg_relation_size</primary>
9276-
</indexterm>
92779289
<para>
9278-
<function>pg_relation_size</> accepts the oid or name of a table, index or
9290+
<function>pg_tablespace_size</> and <function>pg_database_size</> accept
9291+
the OID or name of a tablespace or database, and return the total disk
9292+
space used therein.
9293+
</para>
9294+
9295+
<para>
9296+
<function>pg_relation_size</> accepts the OID or name of a table, index or
92799297
toast table, and returns the size in bytes.
92809298
</para>
9299+
92819300
<para>
9282-
<function>pg_complete_relation_size</> accepts theoid or name of a table or
9283-
toast table, and returns the size in bytes of the data and all associated
9284-
indexes and toast tables.
9301+
<function>pg_complete_relation_size</> accepts theOID or name of a table
9302+
ortoast table, and returns the size in bytes of the data and all
9303+
associatedindexes and toast tables.
92859304
</para>
9305+
92869306
<para>
9287-
<function>pg_size_pretty</> can be used to format the size of the
9288-
database objects in a human readable way, using kB, MB, GB or TB as appropriate.
9307+
<function>pg_size_pretty</> can be used to format the result of one of
9308+
the other functions in a human-readable way, using kB, MB, GB or TB as
9309+
appropriate.
92899310
</para>
92909311

92919312
</sect1>

‎src/backend/access/heap/tuptoaster.c

Lines changed: 33 additions & 43 deletions
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.50 2005/07/06 19:02:52 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.51 2005/08/02 16:11:57 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -268,6 +268,38 @@ toast_raw_datum_size(Datum value)
268268
returnresult;
269269
}
270270

271+
/* ----------
272+
* toast_datum_size
273+
*
274+
*Return the physical storage size (possibly compressed) of a varlena datum
275+
* ----------
276+
*/
277+
Size
278+
toast_datum_size(Datumvalue)
279+
{
280+
varattrib*attr= (varattrib*)DatumGetPointer(value);
281+
Sizeresult;
282+
283+
if (VARATT_IS_EXTERNAL(attr))
284+
{
285+
/*
286+
* Attribute is stored externally - return the extsize whether
287+
* compressed or not. We do not count the size of the toast
288+
* pointer ... should we?
289+
*/
290+
result=attr->va_content.va_external.va_extsize;
291+
}
292+
else
293+
{
294+
/*
295+
* Attribute is stored inline either compressed or not, just
296+
* calculate the size of the datum in either case.
297+
*/
298+
result=VARSIZE(attr);
299+
}
300+
returnresult;
301+
}
302+
271303

272304
/* ----------
273305
* toast_delete -
@@ -1436,45 +1468,3 @@ toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
14361468

14371469
returnresult;
14381470
}
1439-
1440-
/* ----------
1441-
* toast_datum_size
1442-
*
1443-
*Show the (possibly compressed) size of a datum
1444-
* ----------
1445-
*/
1446-
Size
1447-
toast_datum_size(Datumvalue)
1448-
{
1449-
1450-
varattrib*attr= (varattrib*)DatumGetPointer(value);
1451-
Sizeresult;
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-
returnresult;
1479-
1480-
}

‎src/backend/utils/adt/varlena.c

Lines changed: 30 additions & 19 deletions
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.130 2005/07/29 03:17:55 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.131 2005/08/02 16:11:57 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2574,38 +2574,49 @@ md5_bytea(PG_FUNCTION_ARGS)
25742574
}
25752575

25762576
/*
2577-
* Return the length of a datum, possibly compressed
2577+
* Return the size of a datum, possibly compressed
2578+
*
2579+
* Works on any data type
25782580
*/
25792581
Datum
25802582
pg_column_size(PG_FUNCTION_ARGS)
25812583
{
2582-
Datumvalue=PG_GETARG_DATUM(0);
2583-
intresult;
2584+
Datumvalue=PG_GETARG_DATUM(0);
2585+
int32result;
2586+
inttyplen;
25842587

2585-
/*fn_extra storesthefixed column length, or -1 for varlena. */
2586-
if (fcinfo->flinfo->fn_extra==NULL)/* first call? */
2588+
/* On first call, gettheinput type's typlen, and save at *fn_extra */
2589+
if (fcinfo->flinfo->fn_extra==NULL)
25872590
{
2588-
/* On the first call lookup the datatype of the supplied argument */
2589-
Oidargtypeid=get_fn_expr_argtype(fcinfo->flinfo,0);
2590-
inttyplen=get_typlen(argtypeid);
2591+
/* Lookup the datatype of the supplied argument */
2592+
Oidargtypeid=get_fn_expr_argtype(fcinfo->flinfo,0);
25912593

2592-
2593-
if (typlen==0)
2594-
{
2595-
/* Oid not in pg_type, should never happen. */
2594+
typlen=get_typlen(argtypeid);
2595+
if (typlen==0)/* should not happen */
25962596
elog(ERROR,"cache lookup failed for type %u",argtypeid);
2597-
}
25982597

25992598
fcinfo->flinfo->fn_extra=MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
26002599
sizeof(int));
2601-
*(int*)fcinfo->flinfo->fn_extra=typlen;
2600+
*((int*)fcinfo->flinfo->fn_extra)=typlen;
26022601
}
2602+
else
2603+
typlen=*((int*)fcinfo->flinfo->fn_extra);
26032604

2604-
if (*(int*)fcinfo->flinfo->fn_extra!=-1)
2605-
PG_RETURN_INT32(*(int*)fcinfo->flinfo->fn_extra);
2605+
if (typlen==-1)
2606+
{
2607+
/* varlena type, possibly toasted */
2608+
result=toast_datum_size(value);
2609+
}
2610+
elseif (typlen==-2)
2611+
{
2612+
/* cstring */
2613+
result=strlen(DatumGetCString(value))+1;
2614+
}
26062615
else
26072616
{
2608-
result=toast_datum_size(value)-VARHDRSZ;
2609-
PG_RETURN_INT32(result);
2617+
/* ordinary fixed-width type */
2618+
result=typlen;
26102619
}
2620+
2621+
PG_RETURN_INT32(result);
26112622
}

‎src/include/catalog/pg_proc.h

Lines changed: 2 additions & 2 deletions
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.379 2005/07/29 14:47:01 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.380 2005/08/02 16:11:57 tgl Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3701,7 +3701,7 @@ DATA(insert OID = 2560 ( pg_postmaster_start_time PGNSP PGUID 12 f f t f s 0 11
37013701
DESCR("postmaster start time");
37023702

37033703
/* Column storage size */
3704-
DATA(insertOID=1269 (pg_column_sizePGNSPPGUID12fftfi123"2276"_null__null__null_pg_column_size-_null_ ));
3704+
DATA(insertOID=1269 (pg_column_sizePGNSPPGUID12fftfs123"2276"_null__null__null_pg_column_size-_null_ ));
37053705
DESCR("bytes required to store the value, perhaps with compression");
37063706

37073707
/* new functions for Y-direction rtree opclasses */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp