|
| 1 | +#include "postgres.h" |
| 2 | +#include "fmgr.h" |
| 3 | + |
| 4 | +#include "access/heapam.h" |
| 5 | +#include "catalog/catalog.h" |
| 6 | +#include "catalog/catname.h" |
| 7 | +#include "catalog/pg_database.h" |
| 8 | +#include "utils/fmgroids.h" |
| 9 | + |
| 10 | +#include <stdlib.h> |
| 11 | +#include <sys/types.h> |
| 12 | +#include <dirent.h> |
| 13 | +#include <sys/stat.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <errno.h> |
| 16 | + |
| 17 | + |
| 18 | +static char * |
| 19 | +psnprintf(size_t len, const char *fmt,...) |
| 20 | +{ |
| 21 | + va_list ap; |
| 22 | + char *buf; |
| 23 | + |
| 24 | + buf = palloc(len); |
| 25 | + |
| 26 | + va_start(ap, fmt); |
| 27 | + vsnprintf(buf, len, fmt, ap); |
| 28 | + va_end(ap); |
| 29 | + |
| 30 | + return buf; |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +/* |
| 36 | + * SQL function: database_size(name) returns bigint |
| 37 | + */ |
| 38 | + |
| 39 | +PG_FUNCTION_INFO_V1(database_size); |
| 40 | + |
| 41 | +Datum |
| 42 | +database_size(PG_FUNCTION_ARGS) |
| 43 | +{ |
| 44 | + Name dbname = PG_GETARG_NAME(0); |
| 45 | + |
| 46 | + HeapTuple tuple; |
| 47 | + Relation relation; |
| 48 | + ScanKeyData scanKey; |
| 49 | + HeapScanDesc scan; |
| 50 | + Oid dbid; |
| 51 | + char *dbpath; |
| 52 | + DIR *dirdesc; |
| 53 | + struct dirent *direntry; |
| 54 | + int64 totalsize; |
| 55 | + |
| 56 | + relation = heap_openr(DatabaseRelationName, AccessShareLock); |
| 57 | + ScanKeyEntryInitialize(&scanKey, 0, Anum_pg_database_datname, |
| 58 | + F_NAMEEQ, NameGetDatum(dbname)); |
| 59 | + scan = heap_beginscan(relation, 0, SnapshotNow, 1, &scanKey); |
| 60 | + tuple = heap_getnext(scan, 0); |
| 61 | + |
| 62 | + if (!HeapTupleIsValid(tuple)) |
| 63 | + elog(ERROR, "database %s does not exist", NameStr(*dbname)); |
| 64 | + |
| 65 | + dbid = tuple->t_data->t_oid; |
| 66 | + if (dbid == InvalidOid) |
| 67 | + elog(ERROR, "invalid database id"); |
| 68 | + |
| 69 | + heap_endscan(scan); |
| 70 | + heap_close(relation, NoLock); |
| 71 | + |
| 72 | + dbpath = GetDatabasePath(dbid); |
| 73 | + |
| 74 | + dirdesc = opendir(dbpath); |
| 75 | + if (!dirdesc) |
| 76 | + elog(ERROR, "could not open directory %s: %s", dbpath, strerror(errno)); |
| 77 | + |
| 78 | + totalsize = 0; |
| 79 | + for (;;) |
| 80 | + { |
| 81 | + char *fullname; |
| 82 | + struct stat statbuf; |
| 83 | + |
| 84 | + errno = 0; |
| 85 | + direntry = readdir(dirdesc); |
| 86 | + if (!direntry) |
| 87 | + { |
| 88 | + if (errno) |
| 89 | + elog(ERROR, "error reading directory: %s", strerror(errno)); |
| 90 | + else |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + fullname = psnprintf(strlen(dbpath) + 1 + strlen(direntry->d_name) + 1, |
| 95 | + "%s/%s", dbpath, direntry->d_name); |
| 96 | + if (stat(fullname, &statbuf) == -1) |
| 97 | + elog(ERROR, "could not stat %s: %s", fullname, strerror(errno)); |
| 98 | + totalsize += statbuf.st_size; |
| 99 | + pfree(fullname); |
| 100 | + } |
| 101 | + |
| 102 | + closedir(dirdesc); |
| 103 | + |
| 104 | + PG_RETURN_INT64(totalsize); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +/* |
| 110 | + * SQL function: relation_size(name) returns bigint |
| 111 | + */ |
| 112 | + |
| 113 | +PG_FUNCTION_INFO_V1(relation_size); |
| 114 | + |
| 115 | +Datum |
| 116 | +relation_size(PG_FUNCTION_ARGS) |
| 117 | +{ |
| 118 | + Name relname = PG_GETARG_NAME(0); |
| 119 | + |
| 120 | + HeapTuple tuple; |
| 121 | + Relation relation; |
| 122 | + ScanKeyData scanKey; |
| 123 | + HeapScanDesc scan; |
| 124 | + Oid relnode; |
| 125 | + int64 totalsize; |
| 126 | + unsigned int segcount; |
| 127 | + |
| 128 | + relation = heap_openr(RelationRelationName, AccessShareLock); |
| 129 | + ScanKeyEntryInitialize(&scanKey, 0, Anum_pg_class_relname, |
| 130 | + F_NAMEEQ, NameGetDatum(relname)); |
| 131 | + scan = heap_beginscan(relation, 0, SnapshotNow, 1, &scanKey); |
| 132 | + tuple = heap_getnext(scan, 0); |
| 133 | + |
| 134 | + if (!HeapTupleIsValid(tuple)) |
| 135 | + elog(ERROR, "relation %s does not exist", NameStr(*relname)); |
| 136 | + |
| 137 | + relnode = ((Form_pg_class) GETSTRUCT(tuple))->relfilenode; |
| 138 | + if (relnode == InvalidOid) |
| 139 | + elog(ERROR, "invalid relation node id"); |
| 140 | + |
| 141 | + heap_endscan(scan); |
| 142 | + heap_close(relation, NoLock); |
| 143 | + |
| 144 | + totalsize = 0; |
| 145 | + segcount = 0; |
| 146 | + for (;;) |
| 147 | + { |
| 148 | + char *fullname; |
| 149 | + struct stat statbuf; |
| 150 | + |
| 151 | + if (segcount == 0) |
| 152 | + fullname = psnprintf(25, "%u", (unsigned) relnode); |
| 153 | + else |
| 154 | + fullname = psnprintf(50, "%u.%u", (unsigned) relnode, segcount); |
| 155 | + |
| 156 | + if (stat(fullname, &statbuf) == -1) |
| 157 | + { |
| 158 | + if (errno == ENOENT) |
| 159 | + break; |
| 160 | + else |
| 161 | + elog(ERROR, "could not stat %s: %s", fullname, strerror(errno)); |
| 162 | + } |
| 163 | + totalsize += statbuf.st_size; |
| 164 | + pfree(fullname); |
| 165 | + segcount++; |
| 166 | + } |
| 167 | + |
| 168 | + PG_RETURN_INT64(totalsize); |
| 169 | +} |
0 commit comments