Skip to content

Commit 706a308

Browse files
committed
Add relation fork support to pg_relation_size() function. You can now pass
name of a fork ('main' or 'fsm', at the moment) to pg_relation_size() to get the size of a specific fork. Defaults to 'main', if none given. While we're at it, modify pg_relation_size to take a regclass as argument, instead of separate variants taking oid and name. This change is transparent to typical use where the table name is passed as a string literal, like pg_relation_size('table'), but will break queries like pg_relation_size(namecol), where namecol is of type name. text-type input still works, and using a non-schema-qualified table name is not very reliable anyway, so this is unlikely to break anyone's queries in practice.
1 parent 2cc1633 commit 706a308

File tree

6 files changed

+72
-72
lines changed

6 files changed

+72
-72
lines changed

doc/src/sgml/func.sgml

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.447 2008/09/11 17:32:33 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.448 2008/10/03 07:33:08 heikki Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -12417,7 +12417,7 @@ postgres=# select * from pg_xlogfile_name_offset(pg_stop_backup());
1241712417

1241812418
<tbody>
1241912419
<row>
12420-
<entry><function>pg_column_size</function>(<type>any</type>)</entry>
12420+
<entry><literal><function>pg_column_size</function>(<type>any</type>)</literal></entry>
1242112421
<entry><type>int</type></entry>
1242212422
<entry>Number of bytes used to store a particular value (possibly compressed)</entry>
1242312423
</row>
@@ -12437,19 +12437,22 @@ postgres=# select * from pg_xlogfile_name_offset(pg_stop_backup());
1243712437
</row>
1243812438
<row>
1243912439
<entry>
12440-
<literal><function>pg_relation_size</function>(<type>oid</type>)</literal>
12440+
<literal><function>pg_relation_size</function>(<parameter>relation</parameter> <type>regclass</type>, <parameter>fork</parameter> <type>text</type>)</literal>
1244112441
</entry>
1244212442
<entry><type>bigint</type></entry>
12443-
<entry>Disk space used by the table or index with the specified OID</entry>
12443+
<entry>
12444+
Disk space used by the specified fork, <literal>'main'</literal> or
12445+
<literal>'fsm'</literal>, of a table or index with the specified OID
12446+
or name. The table name can be qualified with a schema name
12447+
</entry>
1244412448
</row>
1244512449
<row>
1244612450
<entry>
12447-
<literal><function>pg_relation_size</function>(<type>text</type>)</literal>
12451+
<literal><function>pg_relation_size</function>(<parameter>relation</parameter> <type>regclass</type>)</literal>
1244812452
</entry>
1244912453
<entry><type>bigint</type></entry>
1245012454
<entry>
12451-
Disk space used by the table or index with the specified name.
12452-
The table name can be qualified with a schema name
12455+
Shorthand for <literal>pg_relation_size(..., 'main')</literal>
1245312456
</entry>
1245412457
</row>
1245512458
<row>
@@ -12475,21 +12478,11 @@ postgres=# select * from pg_xlogfile_name_offset(pg_stop_backup());
1247512478
</row>
1247612479
<row>
1247712480
<entry>
12478-
<literal><function>pg_total_relation_size</function>(<type>oid</type>)</literal>
12479-
</entry>
12480-
<entry><type>bigint</type></entry>
12481-
<entry>
12482-
Total disk space used by the table with the specified OID,
12483-
including indexes and toasted data
12484-
</entry>
12485-
</row>
12486-
<row>
12487-
<entry>
12488-
<literal><function>pg_total_relation_size</function>(<type>text</type>)</literal>
12481+
<literal><function>pg_total_relation_size</function>(<type>regclass</type>)</literal>
1248912482
</entry>
1249012483
<entry><type>bigint</type></entry>
1249112484
<entry>
12492-
Total disk space used by the table with the specified name,
12485+
Total disk space used by the table with the specified OID or name,
1249312486
including indexes and toasted data. The table name can be
1249412487
qualified with a schema name
1249512488
</entry>
@@ -12511,7 +12504,12 @@ postgres=# select * from pg_xlogfile_name_offset(pg_stop_backup());
1251112504

1251212505
<para>
1251312506
<function>pg_relation_size</> accepts the OID or name of a table, index or
12514-
toast table, and returns the size in bytes.
12507+
toast table, and returns the size in bytes. Specifying
12508+
<literal>'main'</literal> or leaving out the second argument returns the
12509+
size of the main data fork of the relation. Specifying
12510+
<literal>'fsm'</literal> returns the size of the
12511+
Free Space Map (see <xref linkend="storage-fsm">) associated with the
12512+
relation.
1251512513
</para>
1251612514

1251712515
<para>

src/backend/utils/adt/dbsize.c

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.20 2008/08/11 11:05:11 heikki Exp $
8+
* $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.21 2008/10/03 07:33:09 heikki Exp $
99
*
1010
*/
1111

@@ -248,15 +248,14 @@ pg_tablespace_size_name(PG_FUNCTION_ARGS)
248248
* calculate size of a relation
249249
*/
250250
static int64
251-
calculate_relation_size(RelFileNode *rfn)
251+
calculate_relation_size(RelFileNode *rfn, ForkNumber forknum)
252252
{
253253
int64 totalsize = 0;
254254
char *relationpath;
255255
char pathname[MAXPGPATH];
256256
unsigned int segcount = 0;
257257

258-
/* XXX: This ignores the other forks. */
259-
relationpath = relpath(*rfn, MAIN_FORKNUM);
258+
relationpath = relpath(*rfn, forknum);
260259

261260
for (segcount = 0;; segcount++)
262261
{
@@ -284,34 +283,47 @@ calculate_relation_size(RelFileNode *rfn)
284283
return totalsize;
285284
}
286285

287-
Datum
288-
pg_relation_size_oid(PG_FUNCTION_ARGS)
289-
{
290-
Oid relOid = PG_GETARG_OID(0);
291-
Relation rel;
292-
int64 size;
293286

294-
rel = relation_open(relOid, AccessShareLock);
287+
/*
288+
* XXX: Consider making this global and moving elsewhere. But currently
289+
* there's no other users for this.
290+
*
291+
* Remember to also update the errhint below if you add entries, and the
292+
* documentation for pg_relation_size().
293+
*/
294+
static char *forkNames[] = {
295+
"main", /* MAIN_FORKNUM */
296+
"fsm" /* FSM_FORKNUM */
297+
};
295298

296-
size = calculate_relation_size(&(rel->rd_node));
299+
static ForkNumber
300+
forkname_to_number(char *forkName)
301+
{
302+
ForkNumber forkNum;
297303

298-
relation_close(rel, AccessShareLock);
304+
for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
305+
if (strcmp(forkNames[forkNum], forkName) == 0)
306+
return forkNum;
299307

300-
PG_RETURN_INT64(size);
308+
ereport(ERROR,
309+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
310+
errmsg("invalid fork name"),
311+
errhint("Valid fork names are 'main' and 'fsm'")));
312+
return InvalidForkNumber; /* keep compiler quiet */
301313
}
302314

303315
Datum
304-
pg_relation_size_name(PG_FUNCTION_ARGS)
316+
pg_relation_size(PG_FUNCTION_ARGS)
305317
{
306-
text *relname = PG_GETARG_TEXT_P(0);
307-
RangeVar *relrv;
318+
Oid relOid = PG_GETARG_OID(0);
319+
text *forkName = PG_GETARG_TEXT_P(1);
308320
Relation rel;
309321
int64 size;
310322

311-
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
312-
rel = relation_openrv(relrv, AccessShareLock);
323+
rel = relation_open(relOid, AccessShareLock);
313324

314-
size = calculate_relation_size(&(rel->rd_node));
325+
size = calculate_relation_size(&(rel->rd_node),
326+
forkname_to_number(text_to_cstring(forkName)));
315327

316328
relation_close(rel, AccessShareLock);
317329

@@ -330,12 +342,15 @@ calculate_total_relation_size(Oid Relid)
330342
Oid toastOid;
331343
int64 size;
332344
ListCell *cell;
345+
ForkNumber forkNum;
333346

334347
heapRel = relation_open(Relid, AccessShareLock);
335348
toastOid = heapRel->rd_rel->reltoastrelid;
336349

337350
/* Get the heap size */
338-
size = calculate_relation_size(&(heapRel->rd_node));
351+
size = 0;
352+
for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
353+
size += calculate_relation_size(&(heapRel->rd_node), forkNum);
339354

340355
/* Include any dependent indexes */
341356
if (heapRel->rd_rel->relhasindex)
@@ -349,7 +364,8 @@ calculate_total_relation_size(Oid Relid)
349364

350365
iRel = relation_open(idxOid, AccessShareLock);
351366

352-
size += calculate_relation_size(&(iRel->rd_node));
367+
for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
368+
size += calculate_relation_size(&(iRel->rd_node), forkNum);
353369

354370
relation_close(iRel, AccessShareLock);
355371
}
@@ -367,26 +383,13 @@ calculate_total_relation_size(Oid Relid)
367383
}
368384

369385
Datum
370-
pg_total_relation_size_oid(PG_FUNCTION_ARGS)
386+
pg_total_relation_size(PG_FUNCTION_ARGS)
371387
{
372388
Oid relid = PG_GETARG_OID(0);
373389

374390
PG_RETURN_INT64(calculate_total_relation_size(relid));
375391
}
376392

377-
Datum
378-
pg_total_relation_size_name(PG_FUNCTION_ARGS)
379-
{
380-
text *relname = PG_GETARG_TEXT_P(0);
381-
RangeVar *relrv;
382-
Oid relid;
383-
384-
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
385-
relid = RangeVarGetRelid(relrv, false);
386-
387-
PG_RETURN_INT64(calculate_total_relation_size(relid));
388-
}
389-
390393
/*
391394
* formatting with size units
392395
*/

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.490 2008/09/30 11:11:28 heikki Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.491 2008/10/03 07:33:09 heikki Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200809301
56+
#define CATALOG_VERSION_NO 200810031
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, 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.515 2008/09/19 19:03:40 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.516 2008/10/03 07:33:09 heikki Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3591,13 +3591,11 @@ DATA(insert OID = 2324 ( pg_database_size PGNSP PGUID 12 1 0 0 f f t f v 1 20 "
35913591
DESCR("total disk space usage for the specified database");
35923592
DATA(insert OID = 2168 ( pg_database_size PGNSP PGUID 12 1 0 0 f f t f v 1 20 "19" _null_ _null_ _null_ pg_database_size_name _null_ _null_ _null_ ));
35933593
DESCR("total disk space usage for the specified database");
3594-
DATA(insert OID = 2325 ( pg_relation_size PGNSP PGUID 12 1 0 0 f f t f v 1 20 "26" _null_ _null_ _null_ pg_relation_size_oid _null_ _null_ _null_ ));
3594+
DATA(insert OID = 2325 ( pg_relation_size PGNSP PGUID 14 1 0 0 f f t f v 1 20 "2205" _null_ _null_ _null_ "select pg_catalog.pg_relation_size($1, ''main'')" _null_ _null_ _null_ ));
35953595
DESCR("disk space usage for the specified table or index");
3596-
DATA(insert OID = 2289 ( pg_relation_size PGNSP PGUID 12 1 0 0 f f t f v 1 20 "25" _null_ _null_ _null_ pg_relation_size_name _null_ _null_ _null_ ));
3597-
DESCR("disk space usage for the specified table or index");
3598-
DATA(insert OID = 2286 ( pg_total_relation_size PGNSP PGUID 12 1 0 0 f f t f v 1 20 "26" _null_ _null_ _null_ pg_total_relation_size_oid _null_ _null_ _null_ ));
3599-
DESCR("total disk space usage for the specified table and associated indexes and toast tables");
3600-
DATA(insert OID = 2287 ( pg_total_relation_size PGNSP PGUID 12 1 0 0 f f t f v 1 20 "25" _null_ _null_ _null_ pg_total_relation_size_name _null_ _null_ _null_ ));
3596+
DATA(insert OID = 2332 ( pg_relation_size PGNSP PGUID 12 1 0 0 f f t f v 2 20 "2205 25" _null_ _null_ _null_ pg_relation_size _null_ _null_ _null_ ));
3597+
DESCR("disk space usage for the specified fork of a table or index");
3598+
DATA(insert OID = 2286 ( pg_total_relation_size PGNSP PGUID 12 1 0 0 f f t f v 1 20 "2205" _null_ _null_ _null_ pg_total_relation_size _null_ _null_ _null_ ));
36013599
DESCR("total disk space usage for the specified table and associated indexes and toast tables");
36023600
DATA(insert OID = 2288 ( pg_size_pretty PGNSP PGUID 12 1 0 0 f f t f v 1 25 "20" _null_ _null_ _null_ pg_size_pretty _null_ _null_ _null_ ));
36033601
DESCR("convert a long int to a human readable text using size units");

src/include/storage/relfilenode.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/relfilenode.h,v 1.17 2008/09/30 10:52:14 heikki Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/relfilenode.h,v 1.18 2008/10/03 07:33:10 heikki Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -25,7 +25,10 @@ typedef enum ForkNumber
2525
InvalidForkNumber = -1,
2626
MAIN_FORKNUM = 0,
2727
FSM_FORKNUM
28-
/* NOTE: change MAX_FORKNUM below when you add new forks */
28+
/*
29+
* NOTE: if you add a new fork, change MAX_FORKNUM below and update the
30+
* name to number mapping in utils/adt/dbsize.c
31+
*/
2932
} ForkNumber;
3033

3134
#define MAX_FORKNUM FSM_FORKNUM

src/include/utils/builtins.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, 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.320 2008/09/06 00:01:25 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.321 2008/10/03 07:33:10 heikki Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -389,10 +389,8 @@ extern Datum pg_tablespace_size_oid(PG_FUNCTION_ARGS);
389389
extern Datum pg_tablespace_size_name(PG_FUNCTION_ARGS);
390390
extern Datum pg_database_size_oid(PG_FUNCTION_ARGS);
391391
extern Datum pg_database_size_name(PG_FUNCTION_ARGS);
392-
extern Datum pg_relation_size_oid(PG_FUNCTION_ARGS);
393-
extern Datum pg_relation_size_name(PG_FUNCTION_ARGS);
394-
extern Datum pg_total_relation_size_oid(PG_FUNCTION_ARGS);
395-
extern Datum pg_total_relation_size_name(PG_FUNCTION_ARGS);
392+
extern Datum pg_relation_size(PG_FUNCTION_ARGS);
393+
extern Datum pg_total_relation_size(PG_FUNCTION_ARGS);
396394
extern Datum pg_size_pretty(PG_FUNCTION_ARGS);
397395

398396
/* genfile.c */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy