mirror of
https://github.com/postgres/postgres.git
synced 2025-04-27 22:56:53 +03:00
dbsize modification to support tablespaces
Gavin Sherry
This commit is contained in:
parent
a583675108
commit
676bb9a6b4
@ -6,13 +6,18 @@
|
|||||||
|
|
||||||
#include "access/heapam.h"
|
#include "access/heapam.h"
|
||||||
#include "catalog/catalog.h"
|
#include "catalog/catalog.h"
|
||||||
|
#include "catalog/catname.h"
|
||||||
#include "catalog/namespace.h"
|
#include "catalog/namespace.h"
|
||||||
|
#include "catalog/pg_tablespace.h"
|
||||||
#include "commands/dbcommands.h"
|
#include "commands/dbcommands.h"
|
||||||
#include "fmgr.h"
|
#include "fmgr.h"
|
||||||
#include "storage/fd.h"
|
#include "storage/fd.h"
|
||||||
#include "utils/builtins.h"
|
#include "utils/builtins.h"
|
||||||
|
|
||||||
|
|
||||||
|
static int64
|
||||||
|
get_tablespace_size(Oid dbid, Oid spcid, bool baddirOK);
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
psnprintf(size_t len, const char *fmt,...)
|
psnprintf(size_t len, const char *fmt,...)
|
||||||
{
|
{
|
||||||
@ -44,10 +49,12 @@ database_size(PG_FUNCTION_ARGS)
|
|||||||
Name dbname = PG_GETARG_NAME(0);
|
Name dbname = PG_GETARG_NAME(0);
|
||||||
|
|
||||||
Oid dbid;
|
Oid dbid;
|
||||||
char *dbpath;
|
|
||||||
DIR *dirdesc;
|
|
||||||
struct dirent *direntry;
|
|
||||||
int64 totalsize;
|
int64 totalsize;
|
||||||
|
#ifdef SYMLINK
|
||||||
|
Relation dbrel;
|
||||||
|
HeapScanDesc scan;
|
||||||
|
HeapTuple tuple;
|
||||||
|
#endif
|
||||||
|
|
||||||
dbid = get_database_oid(NameStr(*dbname));
|
dbid = get_database_oid(NameStr(*dbname));
|
||||||
if (!OidIsValid(dbid))
|
if (!OidIsValid(dbid))
|
||||||
@ -55,14 +62,55 @@ database_size(PG_FUNCTION_ARGS)
|
|||||||
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
||||||
errmsg("database \"%s\" does not exist", NameStr(*dbname))));
|
errmsg("database \"%s\" does not exist", NameStr(*dbname))));
|
||||||
|
|
||||||
dbpath = GetDatabasePath(dbid);
|
#ifdef SYMLINK
|
||||||
|
|
||||||
|
dbrel = heap_openr(TableSpaceRelationName, AccessShareLock);
|
||||||
|
scan = heap_beginscan(dbrel, SnapshotNow, 0, (ScanKey) NULL);
|
||||||
|
|
||||||
|
totalsize = 0;
|
||||||
|
|
||||||
|
while((tuple = heap_getnext(scan, ForwardScanDirection)))
|
||||||
|
{
|
||||||
|
Oid spcid = HeapTupleGetOid(tuple);
|
||||||
|
if(spcid != GLOBALTABLESPACE_OID)
|
||||||
|
totalsize += get_tablespace_size(dbid, spcid, true);
|
||||||
|
}
|
||||||
|
heap_endscan(scan);
|
||||||
|
heap_close(dbrel, AccessShareLock);
|
||||||
|
#else
|
||||||
|
/* Same as always */
|
||||||
|
totalsize = get_tablespace_size(dbid, DEFAULTTABLESPACE_OID, false);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We need to keep in mind that we may not be called from the database
|
||||||
|
* whose size we're reporting so, we need to look in every tablespace
|
||||||
|
* to see if our database has data in there
|
||||||
|
*/
|
||||||
|
|
||||||
|
PG_RETURN_INT64(totalsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int64
|
||||||
|
get_tablespace_size(Oid dbid, Oid spcid, bool baddirOK)
|
||||||
|
{
|
||||||
|
char *dbpath;
|
||||||
|
DIR *dirdesc;
|
||||||
|
struct dirent *direntry;
|
||||||
|
int64 totalsize;
|
||||||
|
|
||||||
|
dbpath = GetDatabasePath(dbid, spcid);
|
||||||
|
|
||||||
dirdesc = AllocateDir(dbpath);
|
dirdesc = AllocateDir(dbpath);
|
||||||
if (!dirdesc)
|
if (!dirdesc)
|
||||||
ereport(ERROR,
|
{
|
||||||
|
if(baddirOK)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
ereport(ERROR,
|
||||||
(errcode_for_file_access(),
|
(errcode_for_file_access(),
|
||||||
errmsg("could not open directory \"%s\": %m", dbpath)));
|
errmsg("could not open directory \"%s\": %m", dbpath)));
|
||||||
|
}
|
||||||
totalsize = 0;
|
totalsize = 0;
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
@ -87,18 +135,14 @@ database_size(PG_FUNCTION_ARGS)
|
|||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode_for_file_access(),
|
(errcode_for_file_access(),
|
||||||
errmsg("could not stat \"%s\": %m", fullname)));
|
errmsg("could not stat \"%s\": %m", fullname)));
|
||||||
|
|
||||||
totalsize += statbuf.st_size;
|
totalsize += statbuf.st_size;
|
||||||
pfree(fullname);
|
pfree(fullname);
|
||||||
}
|
}
|
||||||
|
|
||||||
FreeDir(dirdesc);
|
FreeDir(dirdesc);
|
||||||
|
return (totalsize);
|
||||||
PG_RETURN_INT64(totalsize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* SQL function: relation_size(text) returns bigint
|
* SQL function: relation_size(text) returns bigint
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user