3 * object size functions
5 * Copyright (c) 2002-2007, PostgreSQL Global Development Group
8 * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.10 2007/01/05 22:19:40 momjian Exp $
14 #include <sys/types.h>
17 #include "access/heapam.h"
18 #include "catalog/catalog.h"
19 #include "catalog/namespace.h"
20 #include "catalog/pg_tablespace.h"
21 #include "commands/dbcommands.h"
22 #include "commands/tablespace.h"
23 #include "miscadmin.h"
24 #include "storage/fd.h"
25 #include "utils/builtins.h"
26 #include "utils/syscache.h"
27 #include "utils/relcache.h"
30 /* Return physical size of directory contents, or 0 if dir doesn't exist */
32 db_dir_size(const char *path)
35 struct dirent *direntry;
37 char filename[MAXPGPATH];
39 dirdesc = AllocateDir(path);
44 while ((direntry = ReadDir(dirdesc, path)) != NULL)
48 if (strcmp(direntry->d_name, ".") == 0 ||
49 strcmp(direntry->d_name, "..") == 0)
52 snprintf(filename, MAXPGPATH, "%s/%s", path, direntry->d_name);
54 if (stat(filename, &fst) < 0)
56 (errcode_for_file_access(),
57 errmsg("could not stat file \"%s\": %m", filename)));
59 dirsize += fst.st_size;
67 * calculate size of database in all tablespaces
70 calculate_database_size(Oid dbOid)
74 struct dirent *direntry;
75 char dirpath[MAXPGPATH];
76 char pathname[MAXPGPATH];
78 /* Shared storage in pg_global is not counted */
80 /* Include pg_default storage */
81 snprintf(pathname, MAXPGPATH, "base/%u", dbOid);
82 totalsize = db_dir_size(pathname);
84 /* Scan the non-default tablespaces */
85 snprintf(dirpath, MAXPGPATH, "pg_tblspc");
86 dirdesc = AllocateDir(dirpath);
89 (errcode_for_file_access(),
90 errmsg("could not open tablespace directory \"%s\": %m",
93 while ((direntry = ReadDir(dirdesc, dirpath)) != NULL)
95 if (strcmp(direntry->d_name, ".") == 0 ||
96 strcmp(direntry->d_name, "..") == 0)
99 snprintf(pathname, MAXPGPATH, "pg_tblspc/%s/%u",
100 direntry->d_name, dbOid);
101 totalsize += db_dir_size(pathname);
106 /* Complain if we found no trace of the DB at all */
109 (ERRCODE_UNDEFINED_DATABASE,
110 errmsg("database with OID %u does not exist", dbOid)));
116 pg_database_size_oid(PG_FUNCTION_ARGS)
118 Oid dbOid = PG_GETARG_OID(0);
120 PG_RETURN_INT64(calculate_database_size(dbOid));
124 pg_database_size_name(PG_FUNCTION_ARGS)
126 Name dbName = PG_GETARG_NAME(0);
127 Oid dbOid = get_database_oid(NameStr(*dbName));
129 if (!OidIsValid(dbOid))
131 (errcode(ERRCODE_UNDEFINED_DATABASE),
132 errmsg("database \"%s\" does not exist",
135 PG_RETURN_INT64(calculate_database_size(dbOid));
140 * calculate total size of tablespace
143 calculate_tablespace_size(Oid tblspcOid)
145 char tblspcPath[MAXPGPATH];
146 char pathname[MAXPGPATH];
149 struct dirent *direntry;
151 if (tblspcOid == DEFAULTTABLESPACE_OID)
152 snprintf(tblspcPath, MAXPGPATH, "base");
153 else if (tblspcOid == GLOBALTABLESPACE_OID)
154 snprintf(tblspcPath, MAXPGPATH, "global");
156 snprintf(tblspcPath, MAXPGPATH, "pg_tblspc/%u", tblspcOid);
158 dirdesc = AllocateDir(tblspcPath);
162 (errcode_for_file_access(),
163 errmsg("could not open tablespace directory \"%s\": %m",
166 while ((direntry = ReadDir(dirdesc, tblspcPath)) != NULL)
170 if (strcmp(direntry->d_name, ".") == 0 ||
171 strcmp(direntry->d_name, "..") == 0)
174 snprintf(pathname, MAXPGPATH, "%s/%s", tblspcPath, direntry->d_name);
176 if (stat(pathname, &fst) < 0)
178 (errcode_for_file_access(),
179 errmsg("could not stat file \"%s\": %m", pathname)));
181 if (fst.st_mode & S_IFDIR)
182 totalsize += db_dir_size(pathname);
184 totalsize += fst.st_size;
193 pg_tablespace_size_oid(PG_FUNCTION_ARGS)
195 Oid tblspcOid = PG_GETARG_OID(0);
197 PG_RETURN_INT64(calculate_tablespace_size(tblspcOid));
201 pg_tablespace_size_name(PG_FUNCTION_ARGS)
203 Name tblspcName = PG_GETARG_NAME(0);
204 Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName));
206 if (!OidIsValid(tblspcOid))
208 (errcode(ERRCODE_UNDEFINED_OBJECT),
209 errmsg("tablespace \"%s\" does not exist",
210 NameStr(*tblspcName))));
212 PG_RETURN_INT64(calculate_tablespace_size(tblspcOid));
217 * calculate size of a relation
220 calculate_relation_size(RelFileNode *rfn)
224 char pathname[MAXPGPATH];
225 unsigned int segcount = 0;
227 relationpath = relpath(*rfn);
229 for (segcount = 0;; segcount++)
234 snprintf(pathname, MAXPGPATH, "%s",
237 snprintf(pathname, MAXPGPATH, "%s.%u",
238 relationpath, segcount);
240 if (stat(pathname, &fst) < 0)
246 (errcode_for_file_access(),
247 errmsg("could not stat file \"%s\": %m", pathname)));
249 totalsize += fst.st_size;
256 pg_relation_size_oid(PG_FUNCTION_ARGS)
258 Oid relOid = PG_GETARG_OID(0);
262 rel = relation_open(relOid, AccessShareLock);
264 size = calculate_relation_size(&(rel->rd_node));
266 relation_close(rel, AccessShareLock);
268 PG_RETURN_INT64(size);
272 pg_relation_size_name(PG_FUNCTION_ARGS)
274 text *relname = PG_GETARG_TEXT_P(0);
279 relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
280 rel = relation_openrv(relrv, AccessShareLock);
282 size = calculate_relation_size(&(rel->rd_node));
284 relation_close(rel, AccessShareLock);
286 PG_RETURN_INT64(size);
291 * Compute the on-disk size of files for the relation according to the
292 * stat function, including heap data, index data, and toast data.
295 calculate_total_relation_size(Oid Relid)
302 heapRel = relation_open(Relid, AccessShareLock);
303 toastOid = heapRel->rd_rel->reltoastrelid;
305 /* Get the heap size */
306 size = calculate_relation_size(&(heapRel->rd_node));
308 /* Include any dependent indexes */
309 if (heapRel->rd_rel->relhasindex)
311 List *index_oids = RelationGetIndexList(heapRel);
313 foreach(cell, index_oids)
315 Oid idxOid = lfirst_oid(cell);
318 iRel = relation_open(idxOid, AccessShareLock);
320 size += calculate_relation_size(&(iRel->rd_node));
322 relation_close(iRel, AccessShareLock);
325 list_free(index_oids);
328 /* Recursively include toast table (and index) size */
329 if (OidIsValid(toastOid))
330 size += calculate_total_relation_size(toastOid);
332 relation_close(heapRel, AccessShareLock);
338 pg_total_relation_size_oid(PG_FUNCTION_ARGS)
340 Oid relid = PG_GETARG_OID(0);
342 PG_RETURN_INT64(calculate_total_relation_size(relid));
346 pg_total_relation_size_name(PG_FUNCTION_ARGS)
348 text *relname = PG_GETARG_TEXT_P(0);
352 relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
353 relid = RangeVarGetRelid(relrv, false);
355 PG_RETURN_INT64(calculate_total_relation_size(relid));
359 * formatting with size units
362 pg_size_pretty(PG_FUNCTION_ARGS)
364 int64 size = PG_GETARG_INT64(0);
365 char *result = palloc(50 + VARHDRSZ);
366 int64 limit = 10 * 1024;
369 if (size < limit * mult)
370 snprintf(VARDATA(result), 50, INT64_FORMAT " bytes", size);
374 if (size < limit * mult)
375 snprintf(VARDATA(result), 50, INT64_FORMAT " kB",
376 (size + mult / 2) / mult);
380 if (size < limit * mult)
381 snprintf(VARDATA(result), 50, INT64_FORMAT " MB",
382 (size + mult / 2) / mult);
386 if (size < limit * mult)
387 snprintf(VARDATA(result), 50, INT64_FORMAT " GB",
388 (size + mult / 2) / mult);
392 snprintf(VARDATA(result), 50, INT64_FORMAT " TB",
393 (size + mult / 2) / mult);
399 VARATT_SIZEP(result) = strlen(VARDATA(result)) + VARHDRSZ;
401 PG_RETURN_TEXT_P(result);