postgresql/src/backend/utils/adt/dbsize.c

403 lines
8.3 KiB
C
Raw Normal View History

2005-08-02 16:07:27 +02:00
/*
* dbsize.c
* object size functions
2005-08-02 16:07:27 +02:00
*
* Copyright (c) 2002-2007, PostgreSQL Global Development Group
2005-08-02 16:07:27 +02:00
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.11 2007/02/27 23:48:07 tgl Exp $
2005-08-02 16:07:27 +02:00
*
*/
#include "postgres.h"
#include <sys/types.h>
#include <sys/stat.h>
#include "access/heapam.h"
#include "catalog/catalog.h"
2005-08-02 16:07:27 +02:00
#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;
2005-10-15 04:49:52 +02:00
struct dirent *direntry;
DIR *dirdesc;
char filename[MAXPGPATH];
2005-08-02 16:07:27 +02:00
dirdesc = AllocateDir(path);
if (!dirdesc)
2005-10-15 04:49:52 +02:00
return 0;
2005-08-02 16:07:27 +02:00
while ((direntry = ReadDir(dirdesc, path)) != NULL)
2005-08-02 16:07:27 +02:00
{
2005-10-15 04:49:52 +02:00
struct stat fst;
2005-08-02 16:07:27 +02:00
2005-10-15 04:49:52 +02:00
if (strcmp(direntry->d_name, ".") == 0 ||
2005-08-02 16:07:27 +02:00
strcmp(direntry->d_name, "..") == 0)
2005-10-15 04:49:52 +02:00
continue;
2005-08-02 16:07:27 +02:00
snprintf(filename, MAXPGPATH, "%s/%s", path, direntry->d_name);
if (stat(filename, &fst) < 0)
ereport(ERROR,
(errcode_for_file_access(),
2005-10-29 02:31:52 +02:00
errmsg("could not stat file \"%s\": %m", filename)));
2005-10-15 04:49:52 +02:00
dirsize += fst.st_size;
2005-08-02 16:07:27 +02:00
}
FreeDir(dirdesc);
return dirsize;
}
/*
* calculate size of database in all tablespaces
*/
static int64
calculate_database_size(Oid dbOid)
{
int64 totalsize;
2005-10-15 04:49:52 +02:00
DIR *dirdesc;
struct dirent *direntry;
char dirpath[MAXPGPATH];
char pathname[MAXPGPATH];
2005-08-02 16:07:27 +02:00
/* Shared storage in pg_global is not counted */
/* Include pg_default storage */
snprintf(pathname, MAXPGPATH, "base/%u", dbOid);
totalsize = db_dir_size(pathname);
2005-08-02 16:07:27 +02:00
/* Scan the non-default tablespaces */
snprintf(dirpath, MAXPGPATH, "pg_tblspc");
dirdesc = AllocateDir(dirpath);
2005-08-02 16:07:27 +02:00
if (!dirdesc)
2005-10-15 04:49:52 +02:00
ereport(ERROR,
2005-08-02 16:07:27 +02:00
(errcode_for_file_access(),
errmsg("could not open tablespace directory \"%s\": %m",
dirpath)));
2005-08-02 16:07:27 +02:00
while ((direntry = ReadDir(dirdesc, dirpath)) != NULL)
2005-08-02 16:07:27 +02:00
{
2005-10-15 04:49:52 +02:00
if (strcmp(direntry->d_name, ".") == 0 ||
2005-08-02 16:07:27 +02:00
strcmp(direntry->d_name, "..") == 0)
2005-10-15 04:49:52 +02:00
continue;
2005-08-02 16:07:27 +02:00
snprintf(pathname, MAXPGPATH, "pg_tblspc/%s/%u",
direntry->d_name, dbOid);
2005-08-02 16:07:27 +02:00
totalsize += db_dir_size(pathname);
}
FreeDir(dirdesc);
/* Complain if we found no trace of the DB at all */
if (!totalsize)
2005-10-15 04:49:52 +02:00
ereport(ERROR,
2005-08-02 16:07:27 +02:00
(ERRCODE_UNDEFINED_DATABASE,
errmsg("database with OID %u does not exist", dbOid)));
return totalsize;
}
Datum
pg_database_size_oid(PG_FUNCTION_ARGS)
{
2005-10-15 04:49:52 +02:00
Oid dbOid = PG_GETARG_OID(0);
2005-08-02 16:07:27 +02:00
PG_RETURN_INT64(calculate_database_size(dbOid));
}
Datum
pg_database_size_name(PG_FUNCTION_ARGS)
{
2005-10-15 04:49:52 +02:00
Name dbName = PG_GETARG_NAME(0);
Oid dbOid = get_database_oid(NameStr(*dbName));
2005-08-02 16:07:27 +02:00
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)
{
2005-10-15 04:49:52 +02:00
char tblspcPath[MAXPGPATH];
char pathname[MAXPGPATH];
int64 totalsize = 0;
DIR *dirdesc;
struct dirent *direntry;
2005-08-02 16:07:27 +02:00
if (tblspcOid == DEFAULTTABLESPACE_OID)
snprintf(tblspcPath, MAXPGPATH, "base");
2005-08-02 16:07:27 +02:00
else if (tblspcOid == GLOBALTABLESPACE_OID)
snprintf(tblspcPath, MAXPGPATH, "global");
2005-08-02 16:07:27 +02:00
else
snprintf(tblspcPath, MAXPGPATH, "pg_tblspc/%u", tblspcOid);
2005-08-02 16:07:27 +02:00
dirdesc = AllocateDir(tblspcPath);
if (!dirdesc)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not open tablespace directory \"%s\": %m",
tblspcPath)));
while ((direntry = ReadDir(dirdesc, tblspcPath)) != NULL)
2005-08-02 16:07:27 +02:00
{
2005-10-15 04:49:52 +02:00
struct stat fst;
2005-08-02 16:07:27 +02:00
2005-10-15 04:49:52 +02:00
if (strcmp(direntry->d_name, ".") == 0 ||
2005-08-02 16:07:27 +02:00
strcmp(direntry->d_name, "..") == 0)
2005-10-15 04:49:52 +02:00
continue;
2005-08-02 16:07:27 +02:00
snprintf(pathname, MAXPGPATH, "%s/%s", tblspcPath, direntry->d_name);
if (stat(pathname, &fst) < 0)
ereport(ERROR,
(errcode_for_file_access(),
2005-10-29 02:31:52 +02:00
errmsg("could not stat file \"%s\": %m", pathname)));
2005-08-02 16:07:27 +02:00
if (fst.st_mode & S_IFDIR)
2005-10-15 04:49:52 +02:00
totalsize += db_dir_size(pathname);
totalsize += fst.st_size;
2005-08-02 16:07:27 +02:00
}
FreeDir(dirdesc);
2005-10-15 04:49:52 +02:00
2005-08-02 16:07:27 +02:00
return totalsize;
}
Datum
pg_tablespace_size_oid(PG_FUNCTION_ARGS)
{
2005-10-15 04:49:52 +02:00
Oid tblspcOid = PG_GETARG_OID(0);
2005-08-02 16:07:27 +02:00
PG_RETURN_INT64(calculate_tablespace_size(tblspcOid));
}
Datum
pg_tablespace_size_name(PG_FUNCTION_ARGS)
{
2005-10-15 04:49:52 +02:00
Name tblspcName = PG_GETARG_NAME(0);
Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName));
2005-08-02 16:07:27 +02:00
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(RelFileNode *rfn)
2005-08-02 16:07:27 +02:00
{
int64 totalsize = 0;
char *relationpath;
char pathname[MAXPGPATH];
unsigned int segcount = 0;
2005-08-02 16:07:27 +02:00
relationpath = relpath(*rfn);
2005-08-02 16:07:27 +02:00
2005-10-15 04:49:52 +02:00
for (segcount = 0;; segcount++)
2005-08-02 16:07:27 +02:00
{
struct stat fst;
if (segcount == 0)
snprintf(pathname, MAXPGPATH, "%s",
relationpath);
2005-08-02 16:07:27 +02:00
else
snprintf(pathname, MAXPGPATH, "%s.%u",
relationpath, segcount);
2005-08-02 16:07:27 +02:00
if (stat(pathname, &fst) < 0)
{
if (errno == ENOENT)
break;
else
ereport(ERROR,
(errcode_for_file_access(),
2005-10-29 02:31:52 +02:00
errmsg("could not stat file \"%s\": %m", pathname)));
2005-08-02 16:07:27 +02:00
}
totalsize += fst.st_size;
}
return totalsize;
}
Datum
pg_relation_size_oid(PG_FUNCTION_ARGS)
{
2005-10-15 04:49:52 +02:00
Oid relOid = PG_GETARG_OID(0);
Relation rel;
int64 size;
2005-08-02 16:07:27 +02:00
rel = relation_open(relOid, AccessShareLock);
2005-08-02 16:07:27 +02:00
size = calculate_relation_size(&(rel->rd_node));
2005-08-02 16:07:27 +02:00
relation_close(rel, AccessShareLock);
2005-08-02 16:07:27 +02:00
PG_RETURN_INT64(size);
2005-08-02 16:07:27 +02:00
}
Datum
pg_relation_size_name(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_P(0);
RangeVar *relrv;
Relation rel;
int64 size;
2005-10-15 04:49:52 +02:00
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
rel = relation_openrv(relrv, AccessShareLock);
2005-10-15 04:49:52 +02:00
size = calculate_relation_size(&(rel->rd_node));
2005-10-15 04:49:52 +02:00
relation_close(rel, AccessShareLock);
2005-08-02 16:07:27 +02:00
PG_RETURN_INT64(size);
2005-08-02 16:07:27 +02:00
}
/*
2005-10-15 04:49:52 +02:00
* Compute the on-disk size of files for the relation according to the
* stat function, including heap data, index data, and toast data.
2005-08-02 16:07:27 +02:00
*/
static int64
calculate_total_relation_size(Oid Relid)
2005-08-02 16:07:27 +02:00
{
Relation heapRel;
Oid toastOid;
int64 size;
ListCell *cell;
heapRel = relation_open(Relid, AccessShareLock);
toastOid = heapRel->rd_rel->reltoastrelid;
/* Get the heap size */
size = calculate_relation_size(&(heapRel->rd_node));
/* Include any dependent indexes */
if (heapRel->rd_rel->relhasindex)
{
2005-10-15 04:49:52 +02:00
List *index_oids = RelationGetIndexList(heapRel);
foreach(cell, index_oids)
{
Oid idxOid = lfirst_oid(cell);
Relation iRel;
iRel = relation_open(idxOid, AccessShareLock);
size += calculate_relation_size(&(iRel->rd_node));
relation_close(iRel, AccessShareLock);
2005-08-02 16:07:27 +02:00
}
list_free(index_oids);
2005-08-02 16:07:27 +02:00
}
/* Recursively include toast table (and index) size */
if (OidIsValid(toastOid))
size += calculate_total_relation_size(toastOid);
2005-08-02 16:07:27 +02:00
relation_close(heapRel, AccessShareLock);
2005-08-02 16:07:27 +02:00
return size;
}
Datum
pg_total_relation_size_oid(PG_FUNCTION_ARGS)
2005-08-02 16:07:27 +02:00
{
2005-10-15 04:49:52 +02:00
Oid relid = PG_GETARG_OID(0);
2005-08-02 16:07:27 +02:00
PG_RETURN_INT64(calculate_total_relation_size(relid));
2005-08-02 16:07:27 +02:00
}
Datum
pg_total_relation_size_name(PG_FUNCTION_ARGS)
2005-08-02 16:07:27 +02:00
{
text *relname = PG_GETARG_TEXT_P(0);
RangeVar *relrv;
Oid relid;
2005-10-15 04:49:52 +02:00
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
relid = RangeVarGetRelid(relrv, false);
2005-10-15 04:49:52 +02:00
PG_RETURN_INT64(calculate_total_relation_size(relid));
2005-08-02 16:07:27 +02:00
}
/*
* formatting with size units
*/
Datum
pg_size_pretty(PG_FUNCTION_ARGS)
{
2005-10-15 04:49:52 +02:00
int64 size = PG_GETARG_INT64(0);
char *result = palloc(50 + VARHDRSZ);
int64 limit = 10 * 1024;
int64 mult = 1;
if (size < limit * mult)
2005-10-15 04:49:52 +02:00
snprintf(VARDATA(result), 50, INT64_FORMAT " bytes", size);
2005-08-02 16:07:27 +02:00
else
{
mult *= 1024;
if (size < limit * mult)
2005-10-15 04:49:52 +02:00
snprintf(VARDATA(result), 50, INT64_FORMAT " kB",
(size + mult / 2) / mult);
2005-08-02 16:07:27 +02:00
else
{
mult *= 1024;
if (size < limit * mult)
2005-10-15 04:49:52 +02:00
snprintf(VARDATA(result), 50, INT64_FORMAT " MB",
(size + mult / 2) / mult);
2005-08-02 16:07:27 +02:00
else
{
mult *= 1024;
if (size < limit * mult)
2005-10-15 04:49:52 +02:00
snprintf(VARDATA(result), 50, INT64_FORMAT " GB",
(size + mult / 2) / mult);
2005-08-02 16:07:27 +02:00
else
{
2005-10-15 04:49:52 +02:00
mult *= 1024;
snprintf(VARDATA(result), 50, INT64_FORMAT " TB",
(size + mult / 2) / mult);
2005-08-02 16:07:27 +02:00
}
}
}
}
SET_VARSIZE(result, strlen(VARDATA(result)) + VARHDRSZ);
2005-08-02 16:07:27 +02:00
PG_RETURN_TEXT_P(result);
}