From 358a897fa1d7ff49c350f2cb7a0372c0f67dfb88 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Fri, 29 Jul 2005 14:47:04 +0000 Subject: [PATCH] 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 --- contrib/Makefile | 3 +- contrib/dbsize/Makefile | 13 - contrib/dbsize/README.dbsize | 115 --- contrib/dbsize/dbsize.sql.in | 132 --- doc/src/sgml/diskusage.sgml | 23 +- doc/src/sgml/func.sgml | 111 ++- src/backend/utils/adt/Makefile | 4 +- .../dbsize => src/backend/utils/adt}/dbsize.c | 796 ++++++++++-------- src/include/catalog/pg_proc.h | 25 +- src/include/utils/builtins.h | 13 +- 10 files changed, 620 insertions(+), 615 deletions(-) delete mode 100644 contrib/dbsize/Makefile delete mode 100644 contrib/dbsize/README.dbsize delete mode 100644 contrib/dbsize/dbsize.sql.in rename {contrib/dbsize => src/backend/utils/adt}/dbsize.c (50%) diff --git a/contrib/Makefile b/contrib/Makefile index 460fe8a97f..d569569895 100644 --- a/contrib/Makefile +++ b/contrib/Makefile @@ -1,4 +1,4 @@ -# $PostgreSQL: pgsql/contrib/Makefile,v 1.57 2005/07/01 19:23:04 tgl Exp $ +# $PostgreSQL: pgsql/contrib/Makefile,v 1.58 2005/07/29 14:46:55 momjian Exp $ subdir = contrib top_builddir = .. @@ -11,7 +11,6 @@ WANTED_DIRS = \ dbase \ dblink \ dbmirror \ - dbsize \ earthdistance \ fulltextindex \ fuzzystrmatch \ diff --git a/contrib/dbsize/Makefile b/contrib/dbsize/Makefile deleted file mode 100644 index 7beb6ba564..0000000000 --- a/contrib/dbsize/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -MODULES = dbsize -DATA_built = dbsize.sql -DOCS = README.dbsize - -ifdef USE_PGXS -PGXS = $(shell pg_config --pgxs) -include $(PGXS) -else -subdir = contrib/dbsize -top_builddir = ../.. -include $(top_builddir)/src/Makefile.global -include $(top_srcdir)/contrib/contrib-global.mk -endif diff --git a/contrib/dbsize/README.dbsize b/contrib/dbsize/README.dbsize deleted file mode 100644 index e4b92553a4..0000000000 --- a/contrib/dbsize/README.dbsize +++ /dev/null @@ -1,115 +0,0 @@ -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. diff --git a/contrib/dbsize/dbsize.sql.in b/contrib/dbsize/dbsize.sql.in deleted file mode 100644 index 6d08d289c0..0000000000 --- a/contrib/dbsize/dbsize.sql.in +++ /dev/null @@ -1,132 +0,0 @@ -CREATE FUNCTION database_size (name) RETURNS bigint - AS 'MODULE_PATHNAME', 'database_size' - LANGUAGE C STRICT; - -CREATE FUNCTION relation_size (text) RETURNS bigint - AS 'MODULE_PATHNAME', 'relation_size' - LANGUAGE C STRICT; - -CREATE FUNCTION pg_tablespace_size(oid) RETURNS bigint - AS 'MODULE_PATHNAME', 'pg_tablespace_size' - LANGUAGE C STRICT; - -CREATE FUNCTION pg_database_size(oid) RETURNS bigint - AS 'MODULE_PATHNAME', 'pg_database_size' - LANGUAGE C STRICT; - -CREATE FUNCTION pg_relation_size(oid) RETURNS bigint - AS 'MODULE_PATHNAME', 'pg_relation_size' - LANGUAGE C STRICT; - -CREATE FUNCTION pg_size_pretty(bigint) RETURNS text - AS 'MODULE_PATHNAME', 'pg_size_pretty' - LANGUAGE C STRICT; - -CREATE FUNCTION total_relation_size (text) RETURNS bigint AS ' -SELECT pg_relation_size(r.oid) - + COALESCE(pg_relation_size(t.oid), 0)::bigint - + COALESCE(pg_relation_size(ti.oid), 0)::bigint - + COALESCE(SUM(pg_relation_size(i.indexrelid)), 0)::bigint - + COALESCE(SUM(pg_relation_size(it.oid)), 0)::bigint - + COALESCE(SUM(pg_relation_size(iti.oid)), 0)::bigint AS bytes -FROM pg_class r - LEFT JOIN pg_class t ON (r.reltoastrelid = t.oid) - LEFT JOIN pg_class ti ON (t.reltoastidxid = ti.oid) - LEFT JOIN pg_index i ON (r.oid = i.indrelid) - LEFT JOIN pg_class ir ON (ir.oid = i.indexrelid) - LEFT JOIN pg_class it ON (ir.reltoastrelid = it.oid) - LEFT JOIN pg_class iti ON (it.reltoastidxid = iti.oid) -WHERE r.relname = \$1 -GROUP BY r.oid, t.oid, ti.oid -' LANGUAGE SQL; - -CREATE FUNCTION indexes_size (text) RETURNS bigint - AS ' -SELECT COALESCE(SUM(pg_relation_size(ir.oid)), 0)::bigint - + COALESCE(SUM(pg_relation_size(it.oid)), 0)::bigint - + COALESCE(SUM(pg_relation_size(iti.oid)), 0)::bigint AS bytes -FROM pg_class r - LEFT JOIN pg_index i ON (r.oid = i.indrelid) - LEFT JOIN pg_class ir ON (ir.oid = i.indexrelid) - LEFT JOIN pg_class it ON (ir.reltoastrelid = it.oid) - LEFT JOIN pg_class iti ON (it.reltoastidxid = iti.oid) -WHERE r.relname = \$1 -' LANGUAGE SQL; - -CREATE FUNCTION relation_size_components (text) RETURNS SETOF RECORD - AS ' --- relation size -SELECT indexes_size(r.relname) AS indexes_size, - relation_size(r.relname) AS data_size, - total_relation_size(r.relname) AS total_size, - r.relname, r.relkind, r.oid AS relid, r.relfilenode -FROM pg_class r -WHERE r.relname = \$1 - -UNION ALL - --- relation toast size -SELECT indexes_size(toast.relname) AS indexes_size, - relation_size(''pg_toast.''||toast.relname) AS data_size, - total_relation_size(toast.relname) AS total_size, - toast.relname, toast.relkind, toast.oid AS relid, toast.relfilenode -FROM pg_class r, pg_class toast -WHERE r.reltoastrelid = toast.oid - AND r.relname = \$1 - -UNION ALL - --- relation toast index size -SELECT indexes_size(toastidxr.relname) AS indexes_size, - relation_size(''pg_toast.''||toastidxr.relname) AS data_size, - total_relation_size(toastidxr.relname) AS total_size, - toastidxr.relname, toastidxr.relkind, - toastidxr.oid AS relid, toastidxr.relfilenode -FROM pg_class r, pg_index toastidx, pg_class toastidxr -WHERE r.relname = \$1 - AND r.reltoastrelid = toastidx.indrelid - AND toastidx.indexrelid = toastidxr.oid - -UNION ALL - --- relation indices size -SELECT indexes_size(idxr.relname) AS indexes_size, - relation_size(idxr.relname) AS data_size, - total_relation_size(idxr.relname) AS total_size, - idxr.relname, idxr.relkind, idxr.oid AS relid, idxr.relfilenode -FROM pg_class r, pg_class idxr, pg_index idx -WHERE r.relname = \$1 - AND r.oid = idx.indrelid - AND idx.indexrelid = idxr.oid - -UNION ALL - --- relation indices toast size -SELECT indexes_size(idxtoastr.relname) AS indexes_size, - relation_size(''pg_toast.''||idxtoastr.relname) AS data_size, - total_relation_size(idxtoastr.relname) AS total_size, - idxtoastr.relname, idxtoastr.relkind, idxtoastr.oid AS relid, - idxtoastr.relfilenode -FROM pg_class r, pg_class idxr, pg_index idx, pg_class idxtoastr -WHERE r.relname = \$1 - AND r.oid = idx.indrelid - AND idx.indexrelid = idxr.oid - AND idxr.reltoastrelid = idxtoastr.oid - -UNION ALL - --- relation indices toast index size -SELECT indexes_size(idxtoastidxr.relname) AS indexes_size, - relation_size(''pg_toast.''||idxtoastidxr.relname) AS data_size, - total_relation_size(idxtoastidxr.relname) AS total_size, - idxtoastidxr.relname, idxtoastidxr.relkind, - idxtoastidxr.oid AS relid, idxtoastidxr.relfilenode -FROM pg_class r, pg_class idxr, pg_index idx, pg_class idxtoast, - pg_class idxtoastidxr -WHERE r.relname = \$1 - AND r.oid = idx.indrelid - AND idx.indexrelid = idxr.oid - AND idxr.reltoastrelid = idxtoast.oid - AND idxtoast.reltoastrelid = idxtoastidxr.oid -' LANGUAGE SQL; - diff --git a/doc/src/sgml/diskusage.sgml b/doc/src/sgml/diskusage.sgml index 385fd93ef1..634b9ab698 100644 --- a/doc/src/sgml/diskusage.sgml +++ b/doc/src/sgml/diskusage.sgml @@ -1,5 +1,5 @@ @@ -31,11 +31,16 @@ $PostgreSQL: pgsql/doc/src/sgml/diskusage.sgml,v 1.14 2005/01/10 00:04:38 tgl Ex - You can monitor disk space from three places: from - psql using VACUUM information, from - psql using the tools in contrib/dbsize, and from - the command line using the tools in contrib/oid2name. Using - psql on a recently vacuumed or analyzed database, + You can monitor disk space from three ways: using + SQL functions listed in , + using VACUUM information, and from the command line + using the tools in contrib/oid2name. The SQL functions + are the easiest to use and report information about tables, tables with + indexes and long value storage (TOAST), databases, and tablespaces. + + + + Using psql on a recently vacuumed or analyzed database, you can issue queries to see the disk usage of any table: SELECT relfilenode, relpages FROM pg_class WHERE relname = 'customer'; @@ -101,12 +106,6 @@ SELECT relname, relpages FROM pg_class ORDER BY relpages DESC; - - contrib/dbsize loads functions into your database that allow - you to find the size of a table or database from inside - psql without the need for VACUUM or ANALYZE. - - You can also use contrib/oid2name to show disk usage. See README.oid2name in that directory for examples. It includes a script that diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 0fdcb1d0df..5c6092e424 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,5 +1,5 @@ @@ -9161,6 +9161,115 @@ SELECT set_config('log_statement_stats', 'off', false); For details about proper usage of these functions, see . + + + The functions shown in calculate the actual disk space + usage of database objects. + + + + Database Object Size Functions + + + Name Return Type Description + + + + + + + pg_tablespace_size(oid) + + int8 + Calculates the total disk space used by the tablespace with the specified OID + + + + pg_tablespace_size(name) + + int8 + Calculates the total disk space used by the tablespace with the specified name + + + + pg_database_size(oid) + + int8 + Calculates the total disk space used by the database with the specified OID + + + + pg_database_size(name) + + int8 + Calculates the total disk space used by the database with the specified name + + + + pg_relation_size(oid) + + int8 + Calculates the disk space used by the table or index with the specified OID + + + + pg_relation_size(text) + + int8 + Calculates the disk space used by the index or table with the specified name. + The name may be prefixed with a schema name if required + + + + pg_complete_relation_size(oid) + + int8 + Calculates the total disk space used by the table with the specified OID, + including indexes and toasted data + + + + pg_complete_relation_size(text) + + int8 + Calculates the total disk space used by the table with the specified name, + including indexes and toasted data. The name may be prefixed with a schema name if + required + + + + pg_size_pretty(int8) + + text + Formats the size value (in bytes) into a human readable format with size units + + + +
+ + + pg_tablespace_size and pg_database_size accept an + oid or name of a tablespace or database, and return the disk space usage of the specified object. + + + + pg_relation_size + + + pg_relation_size accepts the oid or name of a table, index or + toast table, and returns the size in bytes. + + + pg_complete_relation_size accepts the oid or name of a table or + toast table, and returns the size in bytes of the data and all associated + indexes and toast tables. + + + pg_size_pretty can be used to format the size of the + database objects in a human readable way, using kB, MB, GB or TB as appropriate. + +
diff --git a/src/backend/utils/adt/Makefile b/src/backend/utils/adt/Makefile index 427d6334cd..a0b4bf7dfd 100644 --- a/src/backend/utils/adt/Makefile +++ b/src/backend/utils/adt/Makefile @@ -1,7 +1,7 @@ # # Makefile for utils/adt # -# $PostgreSQL: pgsql/src/backend/utils/adt/Makefile,v 1.57 2004/04/01 21:28:45 tgl Exp $ +# $PostgreSQL: pgsql/src/backend/utils/adt/Makefile,v 1.58 2005/07/29 14:46:57 momjian Exp $ # subdir = src/backend/utils/adt @@ -24,7 +24,7 @@ OBJS = acl.o arrayfuncs.o array_userfuncs.o arrayutils.o bool.o \ tid.o timestamp.o varbit.o varchar.o varlena.o version.o xid.o \ network.o mac.o inet_net_ntop.o inet_net_pton.o \ ri_triggers.o pg_lzcompress.o pg_locale.o formatting.o \ - ascii.o quote.o pgstatfuncs.o encode.o + ascii.o quote.o pgstatfuncs.o encode.o dbsize.o like.o: like.c like_match.c diff --git a/contrib/dbsize/dbsize.c b/src/backend/utils/adt/dbsize.c similarity index 50% rename from contrib/dbsize/dbsize.c rename to src/backend/utils/adt/dbsize.c index ac5e4c74ea..158d2cf402 100644 --- a/contrib/dbsize/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -1,335 +1,461 @@ -/* - * dbsize.c - * object size functions - * - * Copyright (c) 2002-2005, PostgreSQL Global Development Group - * - * IDENTIFICATION - * $PostgreSQL: pgsql/contrib/dbsize/dbsize.c,v 1.19 2005/07/04 04:51:43 tgl Exp $ - * - */ - -#include "postgres.h" - -#include -#include - -#include "access/heapam.h" -#include "catalog/namespace.h" -#include "catalog/pg_tablespace.h" -#include "commands/dbcommands.h" -#include "miscadmin.h" -#include "storage/fd.h" -#include "utils/builtins.h" -#include "utils/syscache.h" - - -Datum pg_tablespace_size(PG_FUNCTION_ARGS); -Datum pg_database_size(PG_FUNCTION_ARGS); -Datum pg_relation_size(PG_FUNCTION_ARGS); -Datum pg_size_pretty(PG_FUNCTION_ARGS); - -Datum database_size(PG_FUNCTION_ARGS); -Datum relation_size(PG_FUNCTION_ARGS); - -PG_FUNCTION_INFO_V1(pg_tablespace_size); -PG_FUNCTION_INFO_V1(pg_database_size); -PG_FUNCTION_INFO_V1(pg_relation_size); -PG_FUNCTION_INFO_V1(pg_size_pretty); - -PG_FUNCTION_INFO_V1(database_size); -PG_FUNCTION_INFO_V1(relation_size); - - -/* Return physical size of directory contents, or 0 if dir doesn't exist */ -static int64 -db_dir_size(const char *path) -{ - int64 dirsize = 0; - struct dirent *direntry; - DIR *dirdesc; - char filename[MAXPGPATH]; - - dirdesc = AllocateDir(path); - - if (!dirdesc) - return 0; - - while ((direntry = ReadDir(dirdesc, path)) != NULL) - { - struct stat fst; - - if (strcmp(direntry->d_name, ".") == 0 || - strcmp(direntry->d_name, "..") == 0) - continue; - - snprintf(filename, MAXPGPATH, "%s/%s", path, direntry->d_name); - - if (stat(filename, &fst) < 0) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not stat \"%s\": %m", filename))); - dirsize += fst.st_size; - } - - FreeDir(dirdesc); - return dirsize; -} - - -static int64 -calculate_database_size(Oid dbOid) -{ - int64 totalsize = 0; - DIR *dirdesc; - struct dirent *direntry; - char pathname[MAXPGPATH]; - - /* Shared storage in pg_global is not counted */ - - /* Include pg_default storage */ - snprintf(pathname, MAXPGPATH, "base/%u", dbOid); - totalsize += db_dir_size(pathname); - - /* Scan the non-default tablespaces */ - snprintf(pathname, MAXPGPATH, "pg_tblspc"); - dirdesc = AllocateDir(pathname); - - while ((direntry = ReadDir(dirdesc, pathname)) != NULL) - { - if (strcmp(direntry->d_name, ".") == 0 || - strcmp(direntry->d_name, "..") == 0) - continue; - - snprintf(pathname, MAXPGPATH, "pg_tblspc/%s/%u", - direntry->d_name, dbOid); - totalsize += db_dir_size(pathname); - } - - FreeDir(dirdesc); - - /* Complain if we found no trace of the DB at all */ - if (!totalsize) - ereport(ERROR, - (ERRCODE_UNDEFINED_DATABASE, - errmsg("database with OID %u does not exist", dbOid))); - - return totalsize; -} - -/* - * calculate total size of tablespace - */ -Datum -pg_tablespace_size(PG_FUNCTION_ARGS) -{ - Oid tblspcOid = PG_GETARG_OID(0); - char tblspcPath[MAXPGPATH]; - char pathname[MAXPGPATH]; - int64 totalsize=0; - DIR *dirdesc; - struct dirent *direntry; - - if (tblspcOid == DEFAULTTABLESPACE_OID) - snprintf(tblspcPath, MAXPGPATH, "base"); - else if (tblspcOid == GLOBALTABLESPACE_OID) - snprintf(tblspcPath, MAXPGPATH, "global"); - else - snprintf(tblspcPath, MAXPGPATH, "pg_tblspc/%u", tblspcOid); - - dirdesc = AllocateDir(tblspcPath); - - while ((direntry = ReadDir(dirdesc, tblspcPath)) != NULL) - { - struct stat fst; - - if (strcmp(direntry->d_name, ".") == 0 || - strcmp(direntry->d_name, "..") == 0) - continue; - - snprintf(pathname, MAXPGPATH, "%s/%s", tblspcPath, direntry->d_name); - - if (stat(pathname, &fst) < 0) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not stat \"%s\": %m", pathname))); - totalsize += fst.st_size; - - if (fst.st_mode & S_IFDIR) - totalsize += db_dir_size(pathname); - } - - FreeDir(dirdesc); - - PG_RETURN_INT64(totalsize); -} - - -/* - * calculate size of database in all tablespaces - */ -Datum -pg_database_size(PG_FUNCTION_ARGS) -{ - Oid dbOid = PG_GETARG_OID(0); - - PG_RETURN_INT64(calculate_database_size(dbOid)); -} - -Datum -database_size(PG_FUNCTION_ARGS) -{ - Name dbName = PG_GETARG_NAME(0); - Oid dbOid = get_database_oid(NameStr(*dbName)); - - if (!OidIsValid(dbOid)) - ereport(ERROR, - (errcode(ERRCODE_UNDEFINED_DATABASE), - errmsg("database \"%s\" does not exist", - NameStr(*dbName)))); - - PG_RETURN_INT64(calculate_database_size(dbOid)); -} - - -/* Calculate relation size given tablespace and relation OIDs */ -static int64 -calculate_relation_size(Oid tblspcOid, Oid relnodeOid) -{ - int64 totalsize=0; - unsigned int segcount=0; - char dirpath[MAXPGPATH]; - char pathname[MAXPGPATH]; - - if (!tblspcOid) - tblspcOid = MyDatabaseTableSpace; - - if (tblspcOid == DEFAULTTABLESPACE_OID) - snprintf(dirpath, MAXPGPATH, "base/%u", MyDatabaseId); - else if (tblspcOid == GLOBALTABLESPACE_OID) - snprintf(dirpath, MAXPGPATH, "global"); - else - snprintf(dirpath, MAXPGPATH, "pg_tblspc/%u/%u", - tblspcOid, MyDatabaseId); - - for (segcount = 0 ;; segcount++) - { - struct stat fst; - - if (segcount == 0) - snprintf(pathname, MAXPGPATH, "%s/%u", - dirpath, relnodeOid); - else - snprintf(pathname, MAXPGPATH, "%s/%u.%u", - dirpath, relnodeOid, segcount); - - if (stat(pathname, &fst) < 0) - { - if (errno == ENOENT) - break; - else - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not stat \"%s\": %m", pathname))); - } - totalsize += fst.st_size; - } - - return totalsize; -} - -/* - * calculate size of relation - */ -Datum -pg_relation_size(PG_FUNCTION_ARGS) -{ - Oid relOid=PG_GETARG_OID(0); - HeapTuple tuple; - Form_pg_class pg_class; - Oid relnodeOid; - Oid tblspcOid; - - tuple = SearchSysCache(RELOID, - ObjectIdGetDatum(relOid), - 0, 0, 0); - if (!HeapTupleIsValid(tuple)) - ereport(ERROR, - (ERRCODE_UNDEFINED_TABLE, - errmsg("relation with OID %u does not exist", relOid))); - - pg_class = (Form_pg_class) GETSTRUCT(tuple); - relnodeOid = pg_class->relfilenode; - tblspcOid = pg_class->reltablespace; - - ReleaseSysCache(tuple); - - PG_RETURN_INT64(calculate_relation_size(tblspcOid, relnodeOid)); -} - -Datum -relation_size(PG_FUNCTION_ARGS) -{ - text *relname = PG_GETARG_TEXT_P(0); - RangeVar *relrv; - Relation relation; - Oid relnodeOid; - Oid tblspcOid; - - relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); - relation = relation_openrv(relrv, AccessShareLock); - - tblspcOid = relation->rd_rel->reltablespace; - relnodeOid = relation->rd_rel->relfilenode; - - relation_close(relation, AccessShareLock); - - PG_RETURN_INT64(calculate_relation_size(tblspcOid, relnodeOid)); -} - -/* - * formatting with size units - */ -Datum -pg_size_pretty(PG_FUNCTION_ARGS) -{ - int64 size=PG_GETARG_INT64(0); - char *result=palloc(50+VARHDRSZ); - int64 limit = 10*1024; - int64 mult=1; - - if (size < limit*mult) - snprintf(VARDATA(result), 50, INT64_FORMAT" bytes", - size); - else - { - mult *= 1024; - if (size < limit*mult) - snprintf(VARDATA(result), 50, INT64_FORMAT " kB", - (size+mult/2) / mult); - else - { - mult *= 1024; - if (size < limit*mult) - snprintf(VARDATA(result), 50, INT64_FORMAT " MB", - (size+mult/2) / mult); - else - { - mult *= 1024; - if (size < limit*mult) - snprintf(VARDATA(result), 50, INT64_FORMAT " GB", - (size+mult/2) / mult); - else - { - mult *= 1024; - snprintf(VARDATA(result), 50, INT64_FORMAT " TB", - (size+mult/2) / mult); - } - } - } - } - - VARATT_SIZEP(result) = strlen(VARDATA(result)) + VARHDRSZ; - - PG_RETURN_TEXT_P(result); -} +/* + * dbsize.c + * object size functions + * + * Copyright (c) 2002-2005, PostgreSQL Global Development Group + * + * IDENTIFICATION + * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.1 2005/07/29 14:46:57 momjian Exp $ + * + */ + +#include "postgres.h" + +#include +#include + +#include "access/heapam.h" +#include "catalog/namespace.h" +#include "catalog/pg_tablespace.h" +#include "commands/dbcommands.h" +#include "commands/tablespace.h" +#include "miscadmin.h" +#include "storage/fd.h" +#include "utils/builtins.h" +#include "utils/syscache.h" +#include "utils/relcache.h" + + +/* Return physical size of directory contents, or 0 if dir doesn't exist */ +static int64 +db_dir_size(const char *path) +{ + int64 dirsize = 0; + struct dirent *direntry; + DIR *dirdesc; + char filename[MAXPGPATH]; + + dirdesc = AllocateDir(path); + + if (!dirdesc) + return 0; + + while ((direntry = readdir(dirdesc)) != NULL) + { + struct stat fst; + + if (strcmp(direntry->d_name, ".") == 0 || + strcmp(direntry->d_name, "..") == 0) + continue; + + snprintf(filename, MAXPGPATH, "%s/%s", path, direntry->d_name); + + if (stat(filename, &fst) < 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not stat \"%s\": %m", filename))); + + dirsize += fst.st_size; + } + + FreeDir(dirdesc); + return dirsize; +} + +/* + * calculate size of database in all tablespaces + */ +static int64 +calculate_database_size(Oid dbOid) +{ + int64 totalsize = 0; + DIR *dirdesc; + struct dirent *direntry; + char pathname[MAXPGPATH]; + + /* Shared storage in pg_global is not counted */ + + /* Include pg_default storage */ + snprintf(pathname, MAXPGPATH, "%s/base/%u", DataDir, dbOid); + totalsize += db_dir_size(pathname); + + /* Scan the non-default tablespaces */ + snprintf(pathname, MAXPGPATH, "%s/pg_tblspc", DataDir); + dirdesc = AllocateDir(pathname); + if (!dirdesc) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open tablespace directory \"%s\": %m", + pathname))); + + while ((direntry = readdir(dirdesc)) != NULL) + { + if (strcmp(direntry->d_name, ".") == 0 || + strcmp(direntry->d_name, "..") == 0) + continue; + + snprintf(pathname, MAXPGPATH, "%s/pg_tblspc/%s/%u", + DataDir, direntry->d_name, dbOid); + totalsize += db_dir_size(pathname); + } + + FreeDir(dirdesc); + + /* Complain if we found no trace of the DB at all */ + if (!totalsize) + ereport(ERROR, + (ERRCODE_UNDEFINED_DATABASE, + errmsg("database with OID %u does not exist", dbOid))); + + return totalsize; +} + +Datum +pg_database_size_oid(PG_FUNCTION_ARGS) +{ + Oid dbOid = PG_GETARG_OID(0); + + PG_RETURN_INT64(calculate_database_size(dbOid)); +} + +Datum +pg_database_size_name(PG_FUNCTION_ARGS) +{ + Name dbName = PG_GETARG_NAME(0); + Oid dbOid = get_database_oid(NameStr(*dbName)); + + if (!OidIsValid(dbOid)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_DATABASE), + errmsg("database \"%s\" does not exist", + NameStr(*dbName)))); + + PG_RETURN_INT64(calculate_database_size(dbOid)); +} + + +/* + * calculate total size of tablespace + */ +static int64 +calculate_tablespace_size(Oid tblspcOid) +{ + char tblspcPath[MAXPGPATH]; + char pathname[MAXPGPATH]; + int64 totalsize=0; + DIR *dirdesc; + struct dirent *direntry; + + if (tblspcOid == DEFAULTTABLESPACE_OID) + snprintf(tblspcPath, MAXPGPATH, "%s/base", DataDir); + else if (tblspcOid == GLOBALTABLESPACE_OID) + snprintf(tblspcPath, MAXPGPATH, "%s/global", DataDir); + else + snprintf(tblspcPath, MAXPGPATH, "%s/pg_tblspc/%u", DataDir, tblspcOid); + + dirdesc = AllocateDir(tblspcPath); + + if (!dirdesc) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open tablespace directory \"%s\": %m", + tblspcPath))); + + while ((direntry = readdir(dirdesc)) != NULL) + { + struct stat fst; + + if (strcmp(direntry->d_name, ".") == 0 || + strcmp(direntry->d_name, "..") == 0) + continue; + + snprintf(pathname, MAXPGPATH, "%s/%s", tblspcPath, direntry->d_name); + + if (stat(pathname, &fst) < 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not stat \"%s\": %m", pathname))); + + if (fst.st_mode & S_IFDIR) + totalsize += db_dir_size(pathname); + + totalsize += fst.st_size; + } + + FreeDir(dirdesc); + + return totalsize; +} + +Datum +pg_tablespace_size_oid(PG_FUNCTION_ARGS) +{ + Oid tblspcOid = PG_GETARG_OID(0); + + PG_RETURN_INT64(calculate_tablespace_size(tblspcOid)); +} + +Datum +pg_tablespace_size_name(PG_FUNCTION_ARGS) +{ + Name tblspcName = PG_GETARG_NAME(0); + Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName)); + + if (!OidIsValid(tblspcOid)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("tablespace \"%s\" does not exist", + NameStr(*tblspcName)))); + + PG_RETURN_INT64(calculate_tablespace_size(tblspcOid)); +} + + +/* + * calculate size of a relation + */ +static int64 +calculate_relation_size(Oid tblspcOid, Oid relnodeOid) +{ + int64 totalsize=0; + unsigned int segcount=0; + char dirpath[MAXPGPATH]; + char pathname[MAXPGPATH]; + + if (!tblspcOid) + tblspcOid = MyDatabaseTableSpace; + + if (tblspcOid == DEFAULTTABLESPACE_OID) + snprintf(dirpath, MAXPGPATH, "%s/base/%u", DataDir, MyDatabaseId); + else if (tblspcOid == GLOBALTABLESPACE_OID) + snprintf(dirpath, MAXPGPATH, "%s/global", DataDir); + else + snprintf(dirpath, MAXPGPATH, "%s/pg_tblspc/%u/%u", + DataDir, tblspcOid, MyDatabaseId); + + for (segcount = 0 ;; segcount++) + { + struct stat fst; + + if (segcount == 0) + snprintf(pathname, MAXPGPATH, "%s/%u", + dirpath, relnodeOid); + else + snprintf(pathname, MAXPGPATH, "%s/%u.%u", + dirpath, relnodeOid, segcount); + + if (stat(pathname, &fst) < 0) + { + if (errno == ENOENT) + break; + else + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not stat \"%s\": %m", pathname))); + } + totalsize += fst.st_size; + } + + return totalsize; +} + +Datum +pg_relation_size_oid(PG_FUNCTION_ARGS) +{ + Oid relOid=PG_GETARG_OID(0); + HeapTuple tuple; + Form_pg_class pg_class; + Oid relnodeOid; + Oid tblspcOid; + + tuple = SearchSysCache(RELOID, + ObjectIdGetDatum(relOid), + 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + ereport(ERROR, + (ERRCODE_UNDEFINED_TABLE, + errmsg("relation with OID %u does not exist", relOid))); + + pg_class = (Form_pg_class) GETSTRUCT(tuple); + relnodeOid = pg_class->relfilenode; + tblspcOid = pg_class->reltablespace; + + ReleaseSysCache(tuple); + + PG_RETURN_INT64(calculate_relation_size(tblspcOid, relnodeOid)); +} + +Datum +pg_relation_size_name(PG_FUNCTION_ARGS) +{ + text *relname = PG_GETARG_TEXT_P(0); + RangeVar *relrv; + Relation relation; + Oid relnodeOid; + Oid tblspcOid; + + relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); + relation = relation_openrv(relrv, AccessShareLock); + + tblspcOid = relation->rd_rel->reltablespace; + relnodeOid = relation->rd_rel->relfilenode; + + relation_close(relation, AccessShareLock); + + PG_RETURN_INT64(calculate_relation_size(tblspcOid, relnodeOid)); +} + + +/* + * Compute on-disk size of files for 'relation' according to the stat function, + * optionally including heap data, index data, and/or toast data. + */ +static int64 +calculate_complete_relation_size(Oid tblspcOid, Oid relnodeOid) +{ + Relation heapRelation; + Relation idxRelation; + Relation toastRelation; + Oid idxOid; + Oid idxTblspcOid; + Oid toastOid; + Oid toastTblspcOid; + bool hasIndices; + int64 size = 0; + List *indexoidlist; + ListCell *idx; + + heapRelation = relation_open(relnodeOid, AccessShareLock); + toastOid = heapRelation->rd_rel->reltoastrelid; + hasIndices = heapRelation->rd_rel->relhasindex; + + /* Get the heap size */ + size += calculate_relation_size(tblspcOid, relnodeOid); + + /* Get Index size */ + if ( hasIndices ) { + /* recursively include any dependent indexes ... */ + indexoidlist = RelationGetIndexList(heapRelation); + + foreach(idx, indexoidlist) { + idxOid = lfirst_oid(idx); + idxRelation = relation_open(idxOid, AccessShareLock); + idxTblspcOid = idxRelation->rd_rel->reltablespace; + size += calculate_relation_size(idxTblspcOid, idxOid); + relation_close(idxRelation, AccessShareLock); + } + list_free(indexoidlist); + } + + /* Close heapReleation now we no longer need it */ + relation_close(heapRelation, AccessShareLock); + + /* Get toast table size */ + if ( toastOid != 0 ) { + + /* recursively include any toast relations ... */ + toastRelation = relation_open(toastOid, AccessShareLock); + toastTblspcOid = toastRelation->rd_rel->reltablespace; + size += calculate_relation_size(toastTblspcOid, toastOid); + relation_close(toastRelation, AccessShareLock); + } + + return size; +} + +/* + * Compute on-disk size of files for 'relation' including + * heap data, index data, and toasted data. + */ +Datum +pg_complete_relation_size_oid(PG_FUNCTION_ARGS) +{ + Oid relOid=PG_GETARG_OID(0); + HeapTuple tuple; + Form_pg_class pg_class; + Oid relnodeOid; + Oid tblspcOid; + + tuple = SearchSysCache(RELOID, + ObjectIdGetDatum(relOid), + 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + ereport(ERROR, + (ERRCODE_UNDEFINED_TABLE, + errmsg("relation with OID %u does not exist", relOid))); + + pg_class = (Form_pg_class) GETSTRUCT(tuple); + relnodeOid = pg_class->relfilenode; + tblspcOid = pg_class->reltablespace; + + ReleaseSysCache(tuple); + + PG_RETURN_INT64(calculate_complete_relation_size(tblspcOid, relnodeOid)); +} + +Datum +pg_complete_relation_size_name(PG_FUNCTION_ARGS) +{ + text *relname = PG_GETARG_TEXT_P(0); + RangeVar *relrv; + Relation relation; + Oid relnodeOid; + Oid tblspcOid; + + relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); + relation = relation_openrv(relrv, AccessShareLock); + + tblspcOid = relation->rd_rel->reltablespace; + relnodeOid = relation->rd_rel->relfilenode; + + relation_close(relation, AccessShareLock); + + PG_RETURN_INT64(calculate_complete_relation_size(tblspcOid, relnodeOid)); +} + +/* + * formatting with size units + */ +Datum +pg_size_pretty(PG_FUNCTION_ARGS) +{ + int64 size=PG_GETARG_INT64(0); + char *result=palloc(50+VARHDRSZ); + int64 limit = 10*1024; + int64 mult=1; + + if (size < limit*mult) + snprintf(VARDATA(result), 50, INT64_FORMAT" bytes", + size); + else + { + mult *= 1024; + if (size < limit*mult) + snprintf(VARDATA(result), 50, INT64_FORMAT " kB", + (size+mult/2) / mult); + else + { + mult *= 1024; + if (size < limit*mult) + snprintf(VARDATA(result), 50, INT64_FORMAT " MB", + (size+mult/2) / mult); + else + { + mult *= 1024; + if (size < limit*mult) + snprintf(VARDATA(result), 50, INT64_FORMAT " GB", + (size+mult/2) / mult); + else + { + mult *= 1024; + snprintf(VARDATA(result), 50, INT64_FORMAT " TB", + (size+mult/2) / mult); + } + } + } + } + + VARATT_SIZEP(result) = strlen(VARDATA(result)) + VARHDRSZ; + + PG_RETURN_TEXT_P(result); +} diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index d69cdc122d..72e23c3f0b 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.378 2005/07/26 00:04:19 tgl Exp $ + * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.379 2005/07/29 14:47:01 momjian Exp $ * * NOTES * The script catalog/genbki.sh reads this file and generates .bki @@ -1573,6 +1573,11 @@ DESCR("matches regex., case-insensitive"); DATA(insert OID = 1241 ( nameicregexne PGNSP PGUID 12 f f t f i 2 16 "19 25" _null_ _null_ _null_ nameicregexne - _null_ )); DESCR("does not match regex., case-insensitive"); +DATA(insert OID = 2322 ( pg_tablespace_size PGNSP PGUID 12 f f t f v 1 20 "26" _null_ _null_ _null_ pg_tablespace_size_oid - _null_ )); +DESCR("Calculate total disk space usage for the specified tablespace"); +DATA(insert OID = 2323 ( pg_tablespace_size PGNSP PGUID 12 f f t f v 1 20 "19" _null_ _null_ _null_ pg_tablespace_size_name - _null_ )); +DESCR("Calculate total disk space usage for the specified tablespace"); + DATA(insert OID = 1251 ( int4abs PGNSP PGUID 12 f f t f i 1 23 "23" _null_ _null_ _null_ int4abs - _null_ )); DESCR("absolute value"); DATA(insert OID = 1253 ( int2abs PGNSP PGUID 12 f f t f i 1 21 "21" _null_ _null_ _null_ int2abs - _null_ )); @@ -1581,6 +1586,9 @@ DESCR("absolute value"); DATA(insert OID = 1263 ( interval PGNSP PGUID 12 f f t f s 1 1186 "25" _null_ _null_ _null_ text_interval - _null_ )); DESCR("convert text to interval"); +DATA(insert OID = 2324 ( pg_database_size PGNSP PGUID 12 f f t f v 1 20 "26" _null_ _null_ _null_ pg_database_size_oid - _null_ )); +DESCR("Calculate total disk space usage for the specified database"); + DATA(insert OID = 1271 ( overlaps PGNSP PGUID 12 f f f f i 4 16 "1266 1266 1266 1266" _null_ _null_ _null_ overlaps_timetz - _null_ )); DESCR("SQL92 interval comparison"); DATA(insert OID = 1272 ( datetime_pl PGNSP PGUID 12 f f t f i 2 1114 "1082 1083" _null_ _null_ _null_ datetime_timestamp - _null_ )); @@ -1624,6 +1632,9 @@ DESCR("latest tid of a tuple"); DATA(insert OID = 1294 ( currtid2 PGNSP PGUID 12 f f t f v 2 27 "25 27" _null_ _null_ _null_ currtid_byrelname - _null_ )); DESCR("latest tid of a tuple"); +DATA(insert OID = 2168 ( pg_database_size PGNSP PGUID 12 f f t f v 1 20 "19" _null_ _null_ _null_ pg_database_size_name - _null_ )); +DESCR("Calculate total disk space usage for the specified database"); + DATA(insert OID = 1296 ( timedate_pl PGNSP PGUID 14 f f t f i 2 1114 "1083 1082" _null_ _null_ _null_ "select ($2 + $1)" - _null_ )); DESCR("convert time and date to timestamp"); DATA(insert OID = 1297 ( datetimetz_pl PGNSP PGUID 12 f f t f i 2 1184 "1082 1266" _null_ _null_ _null_ datetimetz_timestamptz - _null_ )); @@ -3038,7 +3049,6 @@ DESCR("Prepare for taking an online backup"); DATA(insert OID = 2173 ( pg_stop_backup PGNSP PGUID 12 f f t f v 0 25 "" _null_ _null_ _null_ pg_stop_backup - _null_ )); DESCR("Finish taking an online backup"); - /* Aggregates (moved here from pg_aggregate for 7.3) */ DATA(insert OID = 2100 ( avg PGNSP PGUID 12 t f f f i 1 1700 "20" _null_ _null_ _null_ aggregate_dummy - _null_ )); @@ -3227,6 +3237,17 @@ DESCR("current user privilege on schema by schema name"); DATA(insert OID = 2273 ( has_schema_privilege PGNSP PGUID 12 f f t f s 2 16 "26 25" _null_ _null_ _null_ has_schema_privilege_id - _null_ )); DESCR("current user privilege on schema by schema oid"); +DATA(insert OID = 2325 ( pg_relation_size PGNSP PGUID 12 f f t f v 1 20 "26" _null_ _null_ _null_ pg_relation_size_oid - _null_ )); +DESCR("Calculate disk space usage for the specified table or index"); +DATA(insert OID = 2289 ( pg_relation_size PGNSP PGUID 12 f f t f v 1 20 "25" _null_ _null_ _null_ pg_relation_size_name - _null_ )); +DESCR("Calculate disk space usage for the specified table or index"); +DATA(insert OID = 2286 ( pg_complete_relation_size PGNSP PGUID 12 f f t f v 1 20 "26" _null_ _null_ _null_ pg_complete_relation_size_oid - _null_ )); +DESCR("Calculate total disk space usage for the specified table and associated indexes and toast tables"); +DATA(insert OID = 2287 ( pg_complete_relation_size PGNSP PGUID 12 f f t f v 1 20 "25" _null_ _null_ _null_ pg_complete_relation_size_name - _null_ )); +DESCR("Calculate total disk space usage for the specified table and associated indexes and toast tables"); +DATA(insert OID = 2288 ( pg_size_pretty PGNSP PGUID 12 f f t f v 1 25 "20" _null_ _null_ _null_ pg_size_pretty - _null_ )); +DESCR("Convert a long int to a human readable text using size units"); + DATA(insert OID = 2390 ( has_tablespace_privilege PGNSP PGUID 12 f f t f s 3 16 "19 25 25" _null_ _null_ _null_ has_tablespace_privilege_name_name - _null_ )); DESCR("user privilege on tablespace by username, tablespace name"); DATA(insert OID = 2391 ( has_tablespace_privilege PGNSP PGUID 12 f f t f s 3 16 "19 26 25" _null_ _null_ _null_ has_tablespace_privilege_name_id - _null_ )); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index 4f2ac7aae3..96681e0850 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.261 2005/07/26 00:04:19 tgl Exp $ + * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.262 2005/07/29 14:47:04 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -363,6 +363,17 @@ extern Datum float84le(PG_FUNCTION_ARGS); extern Datum float84gt(PG_FUNCTION_ARGS); extern Datum float84ge(PG_FUNCTION_ARGS); +/* dbsize.c */ +extern Datum pg_tablespace_size_oid(PG_FUNCTION_ARGS); +extern Datum pg_tablespace_size_name(PG_FUNCTION_ARGS); +extern Datum pg_database_size_oid(PG_FUNCTION_ARGS); +extern Datum pg_database_size_name(PG_FUNCTION_ARGS); +extern Datum pg_relation_size_oid(PG_FUNCTION_ARGS); +extern Datum pg_relation_size_name(PG_FUNCTION_ARGS); +extern Datum pg_complete_relation_size_oid(PG_FUNCTION_ARGS); +extern Datum pg_complete_relation_size_name(PG_FUNCTION_ARGS); +extern Datum pg_size_pretty(PG_FUNCTION_ARGS); + /* misc.c */ extern Datum nullvalue(PG_FUNCTION_ARGS); extern Datum nonnullvalue(PG_FUNCTION_ARGS); -- 2.40.0