2002-02-23 02:05:35 +03:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2002-04-02 05:17:28 +04:00
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/catalog.h"
|
|
|
|
#include "catalog/namespace.h"
|
2002-08-09 20:45:16 +04:00
|
|
|
#include "commands/dbcommands.h"
|
2002-04-02 05:17:28 +04:00
|
|
|
#include "fmgr.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
2002-02-23 02:05:35 +03:00
|
|
|
|
|
|
|
static char *
|
|
|
|
psnprintf(size_t len, const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = palloc(len);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, len, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SQL function: database_size(name) returns bigint
|
|
|
|
*/
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(database_size);
|
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
Datum database_size(PG_FUNCTION_ARGS);
|
2002-04-02 05:17:28 +04:00
|
|
|
|
2002-02-23 02:05:35 +03:00
|
|
|
Datum
|
|
|
|
database_size(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Name dbname = PG_GETARG_NAME(0);
|
|
|
|
|
|
|
|
Oid dbid;
|
|
|
|
char *dbpath;
|
|
|
|
DIR *dirdesc;
|
|
|
|
struct dirent *direntry;
|
|
|
|
int64 totalsize;
|
|
|
|
|
2002-08-09 20:45:16 +04:00
|
|
|
dbid = get_database_oid(NameStr(*dbname));
|
|
|
|
if (!OidIsValid(dbid))
|
2002-02-23 02:05:35 +03:00
|
|
|
elog(ERROR, "database %s does not exist", NameStr(*dbname));
|
|
|
|
|
|
|
|
dbpath = GetDatabasePath(dbid);
|
|
|
|
|
|
|
|
dirdesc = opendir(dbpath);
|
|
|
|
if (!dirdesc)
|
|
|
|
elog(ERROR, "could not open directory %s: %s", dbpath, strerror(errno));
|
|
|
|
|
|
|
|
totalsize = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char *fullname;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
direntry = readdir(dirdesc);
|
|
|
|
if (!direntry)
|
|
|
|
{
|
|
|
|
if (errno)
|
|
|
|
elog(ERROR, "error reading directory: %s", strerror(errno));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fullname = psnprintf(strlen(dbpath) + 1 + strlen(direntry->d_name) + 1,
|
|
|
|
"%s/%s", dbpath, direntry->d_name);
|
|
|
|
if (stat(fullname, &statbuf) == -1)
|
|
|
|
elog(ERROR, "could not stat %s: %s", fullname, strerror(errno));
|
|
|
|
totalsize += statbuf.st_size;
|
|
|
|
pfree(fullname);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirdesc);
|
|
|
|
|
|
|
|
PG_RETURN_INT64(totalsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-04-02 05:17:28 +04:00
|
|
|
* SQL function: relation_size(text) returns bigint
|
2002-02-23 02:05:35 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(relation_size);
|
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
Datum relation_size(PG_FUNCTION_ARGS);
|
2002-04-02 05:17:28 +04:00
|
|
|
|
2002-02-23 02:05:35 +03:00
|
|
|
Datum
|
|
|
|
relation_size(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-04-02 05:17:28 +04:00
|
|
|
text *relname = PG_GETARG_TEXT_P(0);
|
2002-02-23 02:05:35 +03:00
|
|
|
|
2002-04-02 05:17:28 +04:00
|
|
|
RangeVar *relrv;
|
2002-02-23 02:05:35 +03:00
|
|
|
Relation relation;
|
|
|
|
Oid relnode;
|
|
|
|
int64 totalsize;
|
|
|
|
unsigned int segcount;
|
|
|
|
|
2002-04-02 05:17:28 +04:00
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname,
|
2002-09-05 00:31:48 +04:00
|
|
|
"relation_size"));
|
2002-08-29 04:17:06 +04:00
|
|
|
relation = heap_openrv(relrv, AccessShareLock);
|
2002-02-23 02:05:35 +03:00
|
|
|
|
2002-04-02 05:17:28 +04:00
|
|
|
relnode = relation->rd_rel->relfilenode;
|
2002-02-23 02:05:35 +03:00
|
|
|
|
|
|
|
totalsize = 0;
|
|
|
|
segcount = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char *fullname;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
if (segcount == 0)
|
|
|
|
fullname = psnprintf(25, "%u", (unsigned) relnode);
|
|
|
|
else
|
|
|
|
fullname = psnprintf(50, "%u.%u", (unsigned) relnode, segcount);
|
|
|
|
|
|
|
|
if (stat(fullname, &statbuf) == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT)
|
|
|
|
break;
|
|
|
|
else
|
2002-04-02 05:17:28 +04:00
|
|
|
elog(ERROR, "could not stat %s: %m", fullname);
|
2002-02-23 02:05:35 +03:00
|
|
|
}
|
|
|
|
totalsize += statbuf.st_size;
|
|
|
|
pfree(fullname);
|
|
|
|
segcount++;
|
|
|
|
}
|
|
|
|
|
2002-08-29 04:17:06 +04:00
|
|
|
heap_close(relation, AccessShareLock);
|
2002-04-02 05:17:28 +04:00
|
|
|
|
2002-02-23 02:05:35 +03:00
|
|
|
PG_RETURN_INT64(totalsize);
|
|
|
|
}
|