14 #include <sys/types.h>
35 #define half_rounded(x) (((x) + ((x) < 0 ? 0 : 1)) / 2)
57 if (strcmp(direntry->
d_name,
".") == 0 ||
58 strcmp(direntry->
d_name,
"..") == 0)
63 if (stat(filename, &fst) < 0)
70 errmsg(
"could not stat file \"%s\": %m", filename)));
72 dirsize += fst.st_size;
110 errmsg(
"could not open tablespace directory \"%s\": %m",
113 while ((direntry =
ReadDir(dirdesc, dirpath)) !=
NULL)
117 if (strcmp(direntry->
d_name,
".") == 0 ||
118 strcmp(direntry->
d_name,
"..") == 0)
201 while ((direntry =
ReadDir(dirdesc, tblspcPath)) !=
NULL)
207 if (strcmp(direntry->
d_name,
".") == 0 ||
208 strcmp(direntry->
d_name,
"..") == 0)
213 if (stat(pathname, &fst) < 0)
220 errmsg(
"could not stat file \"%s\": %m", pathname)));
223 if (S_ISDIR(fst.st_mode))
226 totalsize += fst.st_size;
276 unsigned int segcount = 0;
280 for (segcount = 0;; segcount++)
291 relationpath, segcount);
293 if (stat(pathname, &fst) < 0)
300 errmsg(
"could not stat file \"%s\": %m", pathname)));
302 totalsize += fst.st_size;
352 for (forkNum = 0; forkNum <=
MAX_FORKNUM; forkNum++)
360 foreach(lc, indexlist)
366 for (forkNum = 0; forkNum <=
MAX_FORKNUM; forkNum++)
395 for (forkNum = 0; forkNum <=
MAX_FORKNUM; forkNum++)
421 if (rel->
rd_rel->relhasindex)
426 foreach(cell, index_oids)
434 for (forkNum = 0; forkNum <=
MAX_FORKNUM; forkNum++)
536 int64 limit = 10 * 1024;
537 int64 limit2 = limit * 2 - 1;
539 if (
Abs(size) < limit)
544 if (
Abs(size) < limit2)
550 if (
Abs(size) < limit2)
556 if (
Abs(size) < limit2)
634 Datum divisor_numeric;
715 bool have_digits =
false;
721 while (isspace((
unsigned char) *strptr))
728 if (*endptr ==
'-' || *endptr ==
'+')
732 if (isdigit((
unsigned char) *endptr))
737 while (isdigit((
unsigned char) *endptr));
744 if (isdigit((
unsigned char) *endptr))
749 while (isdigit((
unsigned char) *endptr));
756 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
757 errmsg(
"invalid size: \"%s\"", str)));
760 if (*endptr ==
'e' || *endptr ==
'E')
769 exponent = strtol(endptr + 1, &cp, 10);
775 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
776 errmsg(
"invalid size: \"%s\"", str)));
785 saved_char = *endptr;
793 *endptr = saved_char;
797 while (isspace((
unsigned char) *strptr))
803 int64 multiplier = 0;
808 while (isspace((
unsigned char) *endptr))
816 multiplier = (int64) 1;
818 multiplier = (int64) 1024;
820 multiplier = (int64) 1024 * 1024;
822 multiplier = (int64) 1024 * 1024 * 1024;
824 multiplier = (int64) 1024 * 1024 * 1024 * 1024;
827 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
829 errdetail(
"Invalid size unit: \"%s\".", strptr),
830 errhint(
"Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", and \"TB\".")));
878 switch (relform->relkind)
886 if (relform->relfilenode)
887 result = relform->relfilenode;
890 relform->relisshared);
955 switch (relform->relkind)
965 if (relform->reltablespace)
966 rnode.
spcNode = relform->reltablespace;
973 if (relform->relfilenode)
974 rnode.
relNode = relform->relfilenode;
977 relform->relisshared);
996 switch (relform->relpersistence)
1013 elog(
ERROR,
"invalid relpersistence: %c", relform->relpersistence);
Datum pg_filenode_relation(PG_FUNCTION_ARGS)
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
AclResult pg_tablespace_aclcheck(Oid spc_oid, Oid roleid, AclMode mode)
int errhint(const char *fmt,...)
static int64 calculate_total_relation_size(Relation rel)
#define RELPERSISTENCE_UNLOGGED
Datum pg_relation_filepath(PG_FUNCTION_ARGS)
static int64 calculate_relation_size(RelFileNode *rfn, BackendId backend, ForkNumber forknum)
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
#define PG_RETURN_INT64(x)
bool isTempOrTempToastNamespace(Oid namespaceId)
char * psprintf(const char *fmt,...)
#define NumericGetDatum(X)
Oid RelidByRelfilenode(Oid reltablespace, Oid relfilenode)
Datum numeric_out(PG_FUNCTION_ARGS)
#define GLOBALTABLESPACE_OID
Datum numeric_int8(PG_FUNCTION_ARGS)
int errcode(int sqlerrcode)
void relation_close(Relation relation, LOCKMODE lockmode)
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
int GetTempNamespaceBackendId(Oid namespaceId)
#define NUMERIC_MAX_PRECISION
#define DirectFunctionCall1(func, arg1)
int pg_strcasecmp(const char *s1, const char *s2)
static Numeric numeric_shift_right(Numeric n, unsigned count)
Datum int8_numeric(PG_FUNCTION_ARGS)
#define OidIsValid(objectId)
Datum numeric_div_trunc(PG_FUNCTION_ARGS)
Datum pg_total_relation_size(PG_FUNCTION_ARGS)
ForkNumber forkname_to_number(const char *forkName)
#define SearchSysCache1(cacheId, key1)
#define PG_GETARG_TEXT_PP(n)
#define RELPERSISTENCE_PERMANENT
Datum pg_relation_filenode(PG_FUNCTION_ARGS)
static char * numeric_to_cstring(Numeric n)
static Numeric numeric_absolute(Numeric n)
Datum numeric_abs(PG_FUNCTION_ARGS)
#define ObjectIdGetDatum(X)
#define DatumGetCString(X)
char * get_database_name(Oid dbid)
static Numeric int64_to_numeric(int64 v)
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
static Numeric numeric_half_rounded(Numeric n)
#define DEFAULTTABLESPACE_OID
int errdetail(const char *fmt,...)
int errcode_for_file_access(void)
#define CStringGetDatum(X)
DIR * AllocateDir(const char *dirname)
Datum numeric_in(PG_FUNCTION_ARGS)
static bool numeric_is_less(Numeric a, Numeric b)
Datum Int64GetDatum(int64 X)
Datum pg_table_size(PG_FUNCTION_ARGS)
Datum pg_size_bytes(PG_FUNCTION_ARGS)
#define ereport(elevel, rest)
static int64 calculate_table_size(Relation rel)
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Datum numeric_mul(PG_FUNCTION_ARGS)
Datum pg_database_size_name(PG_FUNCTION_ARGS)
#define RELKIND_TOASTVALUE
Oid RelationMapOidToFilenode(Oid relationId, bool shared)
void ReleaseSysCache(HeapTuple tuple)
Datum numeric_sub(PG_FUNCTION_ARGS)
Datum numeric_lt(PG_FUNCTION_ARGS)
Oid get_database_oid(const char *dbname, bool missing_ok)
Datum pg_size_pretty_numeric(PG_FUNCTION_ARGS)
#define PG_RETURN_TEXT_P(x)
static int64 calculate_toast_table_size(Oid toastrelid)
static int64 calculate_database_size(Oid dbOid)
text * cstring_to_text(const char *s)
#define PG_GETARG_NUMERIC(n)
#define DatumGetNumeric(X)
#define HeapTupleIsValid(tuple)
AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode)
#define Assert(condition)
Datum pg_tablespace_size_name(PG_FUNCTION_ARGS)
struct dirent * ReadDir(DIR *dir, const char *dirname)
Datum pg_size_pretty(PG_FUNCTION_ARGS)
Datum pg_indexes_size(PG_FUNCTION_ARGS)
#define TABLESPACE_VERSION_DIRECTORY
List * RelationGetIndexList(Relation relation)
FormData_pg_class * Form_pg_class
char * text_to_cstring(const text *t)
Datum pg_tablespace_size_oid(PG_FUNCTION_ARGS)
Datum pg_database_size_oid(PG_FUNCTION_ARGS)
#define VARSIZE_ANY_EXHDR(PTR)
int errmsg(const char *fmt,...)
#define PG_GETARG_TEXT_P(n)
static int64 calculate_tablespace_size(Oid tblspcOid)
char * get_tablespace_name(Oid spc_oid)
void list_free(List *list)
Relation relation_open(Oid relationId, LOCKMODE lockmode)
#define CHECK_FOR_INTERRUPTS()
#define RELPERSISTENCE_TEMP
Datum numeric_ge(PG_FUNCTION_ARGS)
#define PG_GETARG_INT64(n)
#define relpathbackend(rnode, backend, forknum)
Datum pg_relation_size(PG_FUNCTION_ARGS)
Datum numeric_add(PG_FUNCTION_ARGS)
#define DirectFunctionCall2(func, arg1, arg2)
static int64 db_dir_size(const char *path)
#define PG_GETARG_NAME(n)
static int64 calculate_indexes_size(Relation rel)