head	1.6;
access;
symbols
	REL7_4_29:1.2
	REL8_0_25:1.4
	REL7_4_28:1.2
	REL8_0_24:1.4
	REL7_4_27:1.2
	REL8_0_23:1.4
	REL7_4_26:1.2
	REL8_0_22:1.4
	REL7_4_25:1.2
	REL8_0_21:1.4
	REL7_4_24:1.2
	REL8_0_20:1.4
	REL7_4_23:1.2
	REL8_0_19:1.4
	REL7_4_22:1.2
	REL8_0_18:1.4
	REL7_4_21:1.2
	REL8_0_17:1.4
	REL7_4_20:1.2
	REL8_0_16:1.4
	REL7_3_21:1.2
	REL7_4_19:1.2
	REL8_0_15:1.4
	REL7_3_20:1.2
	REL7_4_18:1.2
	REL8_0_14:1.4
	REL7_3_19:1.2
	REL7_4_17:1.2
	REL8_0_13:1.4
	REL8_0_12:1.4
	REL7_3_18:1.2
	REL7_4_16:1.2
	REL8_0_11:1.4
	REL8_0_10:1.4
	REL7_4_15:1.2
	REL7_3_17:1.2
	REL8_0_9:1.4
	REL7_4_14:1.2
	REL7_3_16:1.2
	REL7_3_15:1.2
	REL7_4_13:1.2
	REL8_0_8:1.4
	REL7_3_14:1.2
	REL7_4_12:1.2
	REL8_0_7:1.4
	REL7_3_13:1.2
	REL7_4_11:1.2
	REL8_0_6:1.4
	REL7_3_12:1.2
	REL7_4_10:1.2
	REL8_0_5:1.4
	REL7_3_11:1.2
	REL7_4_9:1.2
	REL8_0_4:1.4
	REL7_3_10:1.2
	REL7_4_8:1.2
	REL8_0_3:1.4
	REL8_0_2:1.4
	REL7_3_9:1.2
	REL7_4_7:1.2
	REL8_0_1:1.4
	REL8_0_STABLE:1.4.0.4
	REL8_0_0:1.4.0.2
	REL8_0_0RC5:1.4
	REL8_0_0RC4:1.4
	REL8_0_0RC3:1.4
	REL8_0_0RC2:1.4
	REL8_0_0RC1:1.4
	REL8_0_0BETA5:1.4
	REL8_0_0BETA4:1.4
	REL7_4_6:1.2
	REL7_3_8:1.2
	REL8_0_0BETA3:1.3
	REL8_0_0BETA2:1.2
	REL7_4_5:1.2
	REL7_3_7:1.2
	REL7_4_4:1.2
	REL8_0_0BETA1:1.2
	REL7_4_3:1.2
	REL7_4_2:1.2
	REL7_3_6:1.2
	REL7_4_1:1.2
	REL7_3_5:1.2
	REL7_4:1.2
	REL7_4_RC2:1.2
	REL7_4_STABLE:1.2.0.6
	REL7_4_RC1:1.2
	REL7_4_BETA5:1.2
	REL7_4_BETA4:1.2
	REL7_4_BETA3:1.2
	REL7_4_BETA2:1.2
	WIN32_DEV:1.2.0.4
	REL7_4_BETA1:1.2
	REL7_3_4:1.2
	REL7_3_2:1.2
	REL7_3_STABLE:1.2.0.2;
locks; strict;
comment	@# @;


1.6
date	2005.07.29.14.46.55;	author momjian;	state dead;
branches;
next	1.5;

1.5
date	2005.02.26.23.31.15;	author momjian;	state Exp;
branches;
next	1.4;

1.4
date	2004.09.28.19.35.43;	author tgl;	state Exp;
branches;
next	1.3;

1.3
date	2004.09.08.19.35.33;	author momjian;	state Exp;
branches;
next	1.2;

1.2
date	2002.06.23.20.09.23;	author momjian;	state Exp;
branches;
next	1.1;

1.1
date	2002.02.22.23.05.35;	author momjian;	state Exp;
branches;
next	;


desc
@@


1.6
log
@Move dbsize functions into the backend.  New functions:

	pg_tablespace_size
	pg_database_size
	pg_relation_size
	pg_complete_relation_size
	pg_size_pretty

Remove /contrib/dbsize.

Dave Page
@
text
@This module contains several functions that report the on-disk size of a 
given database object in bytes:

	int8 database_size(name)
	int8 relation_size(text)
	int8 indexes_size(text)
	int8 total_relation_size(text)

	int8 pg_database_size(oid)
	int8 pg_relation_size(oid)
	int8 pg_tablespace_size(oid)

	text pg_size_pretty(int8)

	setof record relation_size_components(text)

The first four functions take the name of the object (possibly 
schema-qualified for the latter three) and returns the size of the
on-disk files in bytes.

	SELECT database_size('template1');
	SELECT relation_size('pg_class');
	SELECT indexes_size('pg_class');
	SELECT total_relation_size('pg_class');

These functions take object OIDs:
	
	SELECT pg_database_size(1);         -- template1 database
	SELECT pg_relation_size(1259);      -- pg_class table size
	SELECT pg_tablespace_size(1663);    -- pg_default tablespace

The indexes_size() function returns the total size of the indices for a 
relation, including any toasted indices.

The total_relation_size() function returns the total size of the relation,
all its indices, and any toasted data.  

Please note that relation_size and pg_relation_size report only the size of
the selected relation itself; any related indexes or toast tables are not
counted.  To obtain the total size of a table including all indices and
toasted data, use total_relation_size().

The last function, relation_size_components(), returns a set of rows
showing the sizes of the component relations constituting the input 
relation.  

Examples
========

I've loaded the following table with a little less than 3 MB of data for 
illustration:

    create table fat ( id serial, data varchar );
    create index fat_uidx on fat (id);
    create index fat_idx on fat (data);

You can retrieve a rowset containing constituent sizes as follows:

# SELECT relation_size_components('fat');
              relation_size_components              
----------------------------------------------------
 (2088960,65536,2891776,fat,r,59383,59383)
 (32768,704512,737280,pg_toast_59383,t,59386,59386)
 (0,32768,32768,pg_toast_59383_index,i,59388,59388)
 (0,2039808,2039808,fat_idx,i,59389,59389)
 (0,49152,49152,fat_uidx,i,59911,59911)
(5 rows)

To see a more readable output of the rowset:

    SELECT * 
    FROM relation_size_components('fat') AS (idxsize BIGINT, 
                                             datasize BIGINT, 
                                             totalsize BIGINT, 
                                             relname NAME, 
                                             kind "char", 
                                             relid OID, 
                                             node OID) 
    ORDER BY totalsize;

 idxsize | datasize | totalsize |       relname        | kind | relid | node  
---------+----------+-----------+----------------------+------+-------+-------
       0 |    32768 |     32768 | pg_toast_59383_index | i    | 59388 | 59388
       0 |    49152 |     49152 | fat_uidx             | i    | 59911 | 59911
   32768 |   704512 |    737280 | pg_toast_59383       | t    | 59386 | 59386
       0 |  2039808 |   2039808 | fat_idx              | i    | 59389 | 59389
 2088960 |    65536 |   2891776 | fat                  | r    | 59383 | 59383
(5 rows)

To see the sum total size of a relation:

# select total_relation_size('fat');
 total_relation_size 
-------------------------
                 2891776
(1 row)

To see just the size of the uncompressed relation data:

# select relation_size('fat');
 relation_size 
---------------
         65536
(1 row)

To see the size of all related indices:

# select indexes_size('fat');
 indexes_size 
--------------
      2088960
(1 row)

To install, just run make; make install.  Then load the functions
into any database using dbsize.sql.
@


1.5
log
@Add routines to dbsize to return the index size and total relation size.
Improve documentation.

Ed L.
@
text
@@


1.4
log
@Code review for recent dbsize changes.  Fix some thinkos, enforce coding
style and message style standards, improve documentation.
@
text
@d1 2
a2 2
This module contains several functions that report the size of a given
database object:
d6 2
d15 5
a19 1
The first two functions:
d23 2
d26 1
a26 2
take the name of the object (possibly schema-qualified, for relation_size),
while these functions take object OIDs:
d32 6
d39 74
a112 23
the selected relation itself; any subsidiary indexes or toast tables are not
counted.  To obtain the total size of a table including all helper files
you'd have to do something like:

SELECT *,
    pg_size_pretty(tablesize+indexsize+toastsize+toastindexsize) AS totalsize
FROM
(SELECT pg_relation_size(cl.oid) AS tablesize,
        COALESCE((SELECT SUM(pg_relation_size(indexrelid))::bigint
                  FROM pg_index WHERE cl.oid=indrelid), 0) AS indexsize,
        CASE WHEN reltoastrelid=0 THEN 0
             ELSE pg_relation_size(reltoastrelid)
        END AS toastsize,
        CASE WHEN reltoastrelid=0 THEN 0
             ELSE pg_relation_size((SELECT reltoastidxid FROM pg_class ct
                                    WHERE ct.oid = cl.reltoastrelid))
        END AS toastindexsize
 FROM pg_class cl
 WHERE relname = 'foo') ss;

This sample query utilizes the helper function pg_size_pretty(int8),
which formats the number of bytes into a convenient string using KB, MB,
GB.  It is also contained in this module.
@


1.3
log
@Improve README with mention of new functions.
@
text
@d8 1
a9 1
	int8 pg_relation_size(oid)
d18 2
a19 5
take the name of the object, and support databases and tables. Please
note that relation_size() only reports table file usage and not the
space used by indexes and toast tables.

Functions using oids are:
d22 1
a23 6
	SELECT pg_relation_size(1259);      -- pg_class table size

pg_relation_size() will report the size of the table, index and toast
table OIDs, but they must be requested individually. To obtain the total
size of a table including all helper files you'd have to do something
like:
d25 20
a44 14
XXX This query does not work, syntax error XXX
	
	SELECT pg_relation_size(cl.oid) AS tablesize,
	       CASE WHEN reltoastrelid=0 THEN 0
	            ELSE pg_relation_size(reltoastrelid) END AS toastsize,
	       SUM(pg_relation_size(indexrelid)) AS indexsize,
	       pg_size_pretty(pg_relation_size(cl.oid)
	                    + pg_relation_size(reltoastrelid)
	                    + SUM(pg_relation_size(indexrelid))::int8) 
								AS totalsize
	  FROM pg_class cl
	  JOIN pg_index ON cl.oid=indrelid
	 WHERE relname = 'pg_rewrite'
	 GROUP BY 1,2
d50 1
a50 1
To install, just run make; make install.  Finally, load the functions
a51 1

@


1.2
log
@Update dbsize documentation with:

Copy this directory to contrib/dbsize in your PostgreSQL source tree.
Then just run make; make install.  Finally, load the functions into any
database using dbsize.sql.

When computing the size of a table, it does not include TOAST or index
disk space.
@
text
@d1 2
a2 2
This module contains two functions that report the size of a given
database or relation.  E.g.,
d4 2
a5 2
SELECT database_size('template1');
SELECT relation_size('pg_class');
d7 3
a9 2
These functions report the actual file system space.  Thus, users can
avoid digging through the details of the database directories.
d11 43
a53 3
Copy this directory to contrib/dbsize in your PostgreSQL source tree.
Then just run make; make install.  Finally, load the functions into any
database using dbsize.sql.
a54 2
When computing the size of a table, it does not include TOAST or index
disk space.
@


1.1
log
@I've written on like that a while ago:

http://webmail.postgresql.org/~petere/dbsize.html

The tarball can be rolled into contrib -- now that I think of it I don't
know why I never did that.

Never imagined this would have anything to do with that TODO item,
though.
I figured oid2name accomplished that.

Peter Eisentraut   peter_e@@gmx.net
@
text
@d11 5
a15 1
Then just run make; make install.
@
