PostgreSQL Source Code  git master
dbsize.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include "access/htup_details.h"
#include "access/relation.h"
#include "catalog/catalog.h"
#include "catalog/namespace.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_tablespace.h"
#include "commands/dbcommands.h"
#include "commands/tablespace.h"
#include "miscadmin.h"
#include "storage/fd.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
#include "utils/rel.h"
#include "utils/relfilenodemap.h"
#include "utils/relmapper.h"
#include "utils/syscache.h"
Include dependency graph for dbsize.c:

Go to the source code of this file.

Macros

#define half_rounded(x)   (((x) + ((x) < 0 ? 0 : 1)) / 2)
 

Functions

static int64 db_dir_size (const char *path)
 
static int64 calculate_database_size (Oid dbOid)
 
Datum pg_database_size_oid (PG_FUNCTION_ARGS)
 
Datum pg_database_size_name (PG_FUNCTION_ARGS)
 
static int64 calculate_tablespace_size (Oid tblspcOid)
 
Datum pg_tablespace_size_oid (PG_FUNCTION_ARGS)
 
Datum pg_tablespace_size_name (PG_FUNCTION_ARGS)
 
static int64 calculate_relation_size (RelFileNode *rfn, BackendId backend, ForkNumber forknum)
 
Datum pg_relation_size (PG_FUNCTION_ARGS)
 
static int64 calculate_toast_table_size (Oid toastrelid)
 
static int64 calculate_table_size (Relation rel)
 
static int64 calculate_indexes_size (Relation rel)
 
Datum pg_table_size (PG_FUNCTION_ARGS)
 
Datum pg_indexes_size (PG_FUNCTION_ARGS)
 
static int64 calculate_total_relation_size (Relation rel)
 
Datum pg_total_relation_size (PG_FUNCTION_ARGS)
 
Datum pg_size_pretty (PG_FUNCTION_ARGS)
 
static char * numeric_to_cstring (Numeric n)
 
static bool numeric_is_less (Numeric a, Numeric b)
 
static Numeric numeric_absolute (Numeric n)
 
static Numeric numeric_half_rounded (Numeric n)
 
static Numeric numeric_shift_right (Numeric n, unsigned count)
 
Datum pg_size_pretty_numeric (PG_FUNCTION_ARGS)
 
Datum pg_size_bytes (PG_FUNCTION_ARGS)
 
Datum pg_relation_filenode (PG_FUNCTION_ARGS)
 
Datum pg_filenode_relation (PG_FUNCTION_ARGS)
 
Datum pg_relation_filepath (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ half_rounded

#define half_rounded (   x)    (((x) + ((x) < 0 ? 0 : 1)) / 2)

Definition at line 35 of file dbsize.c.

Referenced by pg_size_pretty().

Function Documentation

◆ calculate_database_size()

static int64 calculate_database_size ( Oid  dbOid)
static

Definition at line 83 of file dbsize.c.

References ACL_CONNECT, aclcheck_error(), ACLCHECK_OK, AllocateDir(), CHECK_FOR_INTERRUPTS, dirent::d_name, db_dir_size(), FreeDir(), get_database_name(), GetUserId(), is_member_of_role(), MAXPGPATH, OBJECT_DATABASE, pg_database_aclcheck(), ReadDir(), snprintf, and TABLESPACE_VERSION_DIRECTORY.

Referenced by pg_database_size_name(), and pg_database_size_oid().

84 {
85  int64 totalsize;
86  DIR *dirdesc;
87  struct dirent *direntry;
88  char dirpath[MAXPGPATH];
89  char pathname[MAXPGPATH + 21 + sizeof(TABLESPACE_VERSION_DIRECTORY)];
90  AclResult aclresult;
91 
92  /*
93  * User must have connect privilege for target database or be a member of
94  * pg_read_all_stats
95  */
96  aclresult = pg_database_aclcheck(dbOid, GetUserId(), ACL_CONNECT);
97  if (aclresult != ACLCHECK_OK &&
98  !is_member_of_role(GetUserId(), DEFAULT_ROLE_READ_ALL_STATS))
99  {
100  aclcheck_error(aclresult, OBJECT_DATABASE,
101  get_database_name(dbOid));
102  }
103 
104  /* Shared storage in pg_global is not counted */
105 
106  /* Include pg_default storage */
107  snprintf(pathname, sizeof(pathname), "base/%u", dbOid);
108  totalsize = db_dir_size(pathname);
109 
110  /* Scan the non-default tablespaces */
111  snprintf(dirpath, MAXPGPATH, "pg_tblspc");
112  dirdesc = AllocateDir(dirpath);
113 
114  while ((direntry = ReadDir(dirdesc, dirpath)) != NULL)
115  {
117 
118  if (strcmp(direntry->d_name, ".") == 0 ||
119  strcmp(direntry->d_name, "..") == 0)
120  continue;
121 
122  snprintf(pathname, sizeof(pathname), "pg_tblspc/%s/%s/%u",
123  direntry->d_name, TABLESPACE_VERSION_DIRECTORY, dbOid);
124  totalsize += db_dir_size(pathname);
125  }
126 
127  FreeDir(dirdesc);
128 
129  return totalsize;
130 }
Oid GetUserId(void)
Definition: miscinit.c:476
Definition: dirent.h:9
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
Definition: dirent.c:25
#define MAXPGPATH
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:26
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2583
#define ACL_CONNECT
Definition: parsenodes.h:86
AclResult
Definition: acl.h:177
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4916
AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4575
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2649
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:215
int FreeDir(DIR *dir)
Definition: fd.c:2701
static int64 db_dir_size(const char *path)
Definition: dbsize.c:39

◆ calculate_indexes_size()

static int64 calculate_indexes_size ( Relation  rel)
static

Definition at line 416 of file dbsize.c.

References AccessShareLock, calculate_relation_size(), lfirst_oid, list_free(), MAX_FORKNUM, RelationData::rd_backend, RelationData::rd_node, RelationData::rd_rel, relation_close(), relation_open(), and RelationGetIndexList().

Referenced by calculate_total_relation_size(), and pg_indexes_size().

417 {
418  int64 size = 0;
419 
420  /*
421  * Aggregate all indexes on the given relation
422  */
423  if (rel->rd_rel->relhasindex)
424  {
425  List *index_oids = RelationGetIndexList(rel);
426  ListCell *cell;
427 
428  foreach(cell, index_oids)
429  {
430  Oid idxOid = lfirst_oid(cell);
431  Relation idxRel;
432  ForkNumber forkNum;
433 
434  idxRel = relation_open(idxOid, AccessShareLock);
435 
436  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
437  size += calculate_relation_size(&(idxRel->rd_node),
438  idxRel->rd_backend,
439  forkNum);
440 
442  }
443 
444  list_free(index_oids);
445  }
446 
447  return size;
448 }
static int64 calculate_relation_size(RelFileNode *rfn, BackendId backend, ForkNumber forknum)
Definition: dbsize.c:273
#define AccessShareLock
Definition: lockdefs.h:36
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
ForkNumber
Definition: relpath.h:40
RelFileNode rd_node
Definition: rel.h:55
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
BackendId rd_backend
Definition: rel.h:59
#define MAX_FORKNUM
Definition: relpath.h:55
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4514
void list_free(List *list)
Definition: list.c:1376
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ calculate_relation_size()

static int64 calculate_relation_size ( RelFileNode rfn,
BackendId  backend,
ForkNumber  forknum 
)
static

Definition at line 273 of file dbsize.c.

References CHECK_FOR_INTERRUPTS, ereport, errcode_for_file_access(), errmsg(), ERROR, MAXPGPATH, relpathbackend, snprintf, stat::st_size, and stat.

Referenced by calculate_indexes_size(), calculate_table_size(), calculate_toast_table_size(), and pg_relation_size().

274 {
275  int64 totalsize = 0;
276  char *relationpath;
277  char pathname[MAXPGPATH];
278  unsigned int segcount = 0;
279 
280  relationpath = relpathbackend(*rfn, backend, forknum);
281 
282  for (segcount = 0;; segcount++)
283  {
284  struct stat fst;
285 
287 
288  if (segcount == 0)
289  snprintf(pathname, MAXPGPATH, "%s",
290  relationpath);
291  else
292  snprintf(pathname, MAXPGPATH, "%s.%u",
293  relationpath, segcount);
294 
295  if (stat(pathname, &fst) < 0)
296  {
297  if (errno == ENOENT)
298  break;
299  else
300  ereport(ERROR,
302  errmsg("could not stat file \"%s\": %m", pathname)));
303  }
304  totalsize += fst.st_size;
305  }
306 
307  return totalsize;
308 }
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:633
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define relpathbackend(rnode, backend, forknum)
Definition: relpath.h:78
#define snprintf
Definition: port.h:215
#define stat
Definition: win32_port.h:275

◆ calculate_table_size()

static int64 calculate_table_size ( Relation  rel)
static

Definition at line 389 of file dbsize.c.

References calculate_relation_size(), calculate_toast_table_size(), MAX_FORKNUM, OidIsValid, RelationData::rd_backend, RelationData::rd_node, and RelationData::rd_rel.

Referenced by calculate_total_relation_size(), and pg_table_size().

390 {
391  int64 size = 0;
392  ForkNumber forkNum;
393 
394  /*
395  * heap size, including FSM and VM
396  */
397  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
398  size += calculate_relation_size(&(rel->rd_node), rel->rd_backend,
399  forkNum);
400 
401  /*
402  * Size of toast relation
403  */
404  if (OidIsValid(rel->rd_rel->reltoastrelid))
405  size += calculate_toast_table_size(rel->rd_rel->reltoastrelid);
406 
407  return size;
408 }
static int64 calculate_relation_size(RelFileNode *rfn, BackendId backend, ForkNumber forknum)
Definition: dbsize.c:273
Form_pg_class rd_rel
Definition: rel.h:109
#define OidIsValid(objectId)
Definition: c.h:652
ForkNumber
Definition: relpath.h:40
static int64 calculate_toast_table_size(Oid toastrelid)
Definition: dbsize.c:343
RelFileNode rd_node
Definition: rel.h:55
BackendId rd_backend
Definition: rel.h:59
#define MAX_FORKNUM
Definition: relpath.h:55

◆ calculate_tablespace_size()

static int64 calculate_tablespace_size ( Oid  tblspcOid)
static

Definition at line 167 of file dbsize.c.

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, AllocateDir(), CHECK_FOR_INTERRUPTS, dirent::d_name, db_dir_size(), ereport, errcode_for_file_access(), errmsg(), ERROR, FreeDir(), get_tablespace_name(), GetUserId(), is_member_of_role(), MAXPGPATH, MyDatabaseTableSpace, OBJECT_TABLESPACE, pg_tablespace_aclcheck(), ReadDir(), S_ISDIR, snprintf, stat::st_mode, stat::st_size, stat, and TABLESPACE_VERSION_DIRECTORY.

Referenced by pg_tablespace_size_name(), and pg_tablespace_size_oid().

168 {
169  char tblspcPath[MAXPGPATH];
170  char pathname[MAXPGPATH * 2];
171  int64 totalsize = 0;
172  DIR *dirdesc;
173  struct dirent *direntry;
174  AclResult aclresult;
175 
176  /*
177  * User must be a member of pg_read_all_stats or have CREATE privilege for
178  * target tablespace, either explicitly granted or implicitly because it
179  * is default for current database.
180  */
181  if (tblspcOid != MyDatabaseTableSpace &&
182  !is_member_of_role(GetUserId(), DEFAULT_ROLE_READ_ALL_STATS))
183  {
184  aclresult = pg_tablespace_aclcheck(tblspcOid, GetUserId(), ACL_CREATE);
185  if (aclresult != ACLCHECK_OK)
187  get_tablespace_name(tblspcOid));
188  }
189 
190  if (tblspcOid == DEFAULTTABLESPACE_OID)
191  snprintf(tblspcPath, MAXPGPATH, "base");
192  else if (tblspcOid == GLOBALTABLESPACE_OID)
193  snprintf(tblspcPath, MAXPGPATH, "global");
194  else
195  snprintf(tblspcPath, MAXPGPATH, "pg_tblspc/%u/%s", tblspcOid,
197 
198  dirdesc = AllocateDir(tblspcPath);
199 
200  if (!dirdesc)
201  return -1;
202 
203  while ((direntry = ReadDir(dirdesc, tblspcPath)) != NULL)
204  {
205  struct stat fst;
206 
208 
209  if (strcmp(direntry->d_name, ".") == 0 ||
210  strcmp(direntry->d_name, "..") == 0)
211  continue;
212 
213  snprintf(pathname, sizeof(pathname), "%s/%s", tblspcPath, direntry->d_name);
214 
215  if (stat(pathname, &fst) < 0)
216  {
217  if (errno == ENOENT)
218  continue;
219  else
220  ereport(ERROR,
222  errmsg("could not stat file \"%s\": %m", pathname)));
223  }
224 
225  if (S_ISDIR(fst.st_mode))
226  totalsize += db_dir_size(pathname);
227 
228  totalsize += fst.st_size;
229  }
230 
231  FreeDir(dirdesc);
232 
233  return totalsize;
234 }
AclResult pg_tablespace_aclcheck(Oid spc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4637
Oid GetUserId(void)
Definition: miscinit.c:476
Definition: dirent.h:9
Oid MyDatabaseTableSpace
Definition: globals.c:87
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:84
#define MAXPGPATH
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:26
int errcode_for_file_access(void)
Definition: elog.c:633
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2583
AclResult
Definition: acl.h:177
#define ereport(elevel,...)
Definition: elog.h:144
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4916
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2649
#define S_ISDIR(m)
Definition: win32_port.h:316
int errmsg(const char *fmt,...)
Definition: elog.c:821
char * get_tablespace_name(Oid spc_oid)
Definition: tablespace.c:1467
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:215
int FreeDir(DIR *dir)
Definition: fd.c:2701
static int64 db_dir_size(const char *path)
Definition: dbsize.c:39
#define stat
Definition: win32_port.h:275

◆ calculate_toast_table_size()

static int64 calculate_toast_table_size ( Oid  toastrelid)
static

Definition at line 343 of file dbsize.c.

References AccessShareLock, calculate_relation_size(), lfirst_oid, list_free(), MAX_FORKNUM, RelationData::rd_backend, RelationData::rd_node, relation_close(), relation_open(), and RelationGetIndexList().

Referenced by calculate_table_size().

344 {
345  int64 size = 0;
346  Relation toastRel;
347  ForkNumber forkNum;
348  ListCell *lc;
349  List *indexlist;
350 
351  toastRel = relation_open(toastrelid, AccessShareLock);
352 
353  /* toast heap size, including FSM and VM size */
354  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
355  size += calculate_relation_size(&(toastRel->rd_node),
356  toastRel->rd_backend, forkNum);
357 
358  /* toast index size, including FSM and VM size */
359  indexlist = RelationGetIndexList(toastRel);
360 
361  /* Size is calculated using all the indexes available */
362  foreach(lc, indexlist)
363  {
364  Relation toastIdxRel;
365 
366  toastIdxRel = relation_open(lfirst_oid(lc),
368  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
369  size += calculate_relation_size(&(toastIdxRel->rd_node),
370  toastIdxRel->rd_backend, forkNum);
371 
372  relation_close(toastIdxRel, AccessShareLock);
373  }
374  list_free(indexlist);
375  relation_close(toastRel, AccessShareLock);
376 
377  return size;
378 }
static int64 calculate_relation_size(RelFileNode *rfn, BackendId backend, ForkNumber forknum)
Definition: dbsize.c:273
#define AccessShareLock
Definition: lockdefs.h:36
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
ForkNumber
Definition: relpath.h:40
RelFileNode rd_node
Definition: rel.h:55
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
BackendId rd_backend
Definition: rel.h:59
#define MAX_FORKNUM
Definition: relpath.h:55
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4514
void list_free(List *list)
Definition: list.c:1376
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ calculate_total_relation_size()

static int64 calculate_total_relation_size ( Relation  rel)
static

Definition at line 493 of file dbsize.c.

References calculate_indexes_size(), and calculate_table_size().

Referenced by pg_total_relation_size().

494 {
495  int64 size;
496 
497  /*
498  * Aggregate the table size, this includes size of the heap, toast and
499  * toast index with free space and visibility map
500  */
501  size = calculate_table_size(rel);
502 
503  /*
504  * Add size of all attached indexes as well
505  */
506  size += calculate_indexes_size(rel);
507 
508  return size;
509 }
static int64 calculate_table_size(Relation rel)
Definition: dbsize.c:389
static int64 calculate_indexes_size(Relation rel)
Definition: dbsize.c:416

◆ db_dir_size()

static int64 db_dir_size ( const char *  path)
static

Definition at line 39 of file dbsize.c.

References AllocateDir(), CHECK_FOR_INTERRUPTS, dirent::d_name, ereport, errcode_for_file_access(), errmsg(), ERROR, filename, FreeDir(), MAXPGPATH, ReadDir(), snprintf, stat::st_size, and stat.

Referenced by calculate_database_size(), and calculate_tablespace_size().

40 {
41  int64 dirsize = 0;
42  struct dirent *direntry;
43  DIR *dirdesc;
44  char filename[MAXPGPATH * 2];
45 
46  dirdesc = AllocateDir(path);
47 
48  if (!dirdesc)
49  return 0;
50 
51  while ((direntry = ReadDir(dirdesc, path)) != NULL)
52  {
53  struct stat fst;
54 
56 
57  if (strcmp(direntry->d_name, ".") == 0 ||
58  strcmp(direntry->d_name, "..") == 0)
59  continue;
60 
61  snprintf(filename, sizeof(filename), "%s/%s", path, direntry->d_name);
62 
63  if (stat(filename, &fst) < 0)
64  {
65  if (errno == ENOENT)
66  continue;
67  else
68  ereport(ERROR,
70  errmsg("could not stat file \"%s\": %m", filename)));
71  }
72  dirsize += fst.st_size;
73  }
74 
75  FreeDir(dirdesc);
76  return dirsize;
77 }
Definition: dirent.h:9
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:633
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2583
#define ereport(elevel,...)
Definition: elog.h:144
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2649
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:215
int FreeDir(DIR *dir)
Definition: fd.c:2701
#define stat
Definition: win32_port.h:275

◆ numeric_absolute()

static Numeric numeric_absolute ( Numeric  n)
static

Definition at line 592 of file dbsize.c.

References DatumGetNumeric, DirectFunctionCall1, numeric_abs(), and NumericGetDatum.

Referenced by pg_size_pretty_numeric().

593 {
594  Datum d = NumericGetDatum(n);
595  Datum result;
596 
597  result = DirectFunctionCall1(numeric_abs, d);
598  return DatumGetNumeric(result);
599 }
#define NumericGetDatum(X)
Definition: numeric.h:51
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
Datum numeric_abs(PG_FUNCTION_ARGS)
Definition: numeric.c:1258
uintptr_t Datum
Definition: postgres.h:367
#define DatumGetNumeric(X)
Definition: numeric.h:49

◆ numeric_half_rounded()

static Numeric numeric_half_rounded ( Numeric  n)
static

Definition at line 602 of file dbsize.c.

References DatumGetBool, DatumGetNumeric, DirectFunctionCall2, int64_to_numeric(), numeric_add(), numeric_div_trunc(), numeric_ge(), numeric_sub(), and NumericGetDatum.

Referenced by pg_size_pretty_numeric().

603 {
604  Datum d = NumericGetDatum(n);
605  Datum zero;
606  Datum one;
607  Datum two;
608  Datum result;
609 
613 
615  d = DirectFunctionCall2(numeric_add, d, one);
616  else
617  d = DirectFunctionCall2(numeric_sub, d, one);
618 
619  result = DirectFunctionCall2(numeric_div_trunc, d, two);
620  return DatumGetNumeric(result);
621 }
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_div_trunc(PG_FUNCTION_ARGS)
Definition: numeric.c:3115
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4079
#define DatumGetBool(X)
Definition: postgres.h:393
uintptr_t Datum
Definition: postgres.h:367
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:2788
#define DatumGetNumeric(X)
Definition: numeric.h:49
Datum numeric_ge(PG_FUNCTION_ARGS)
Definition: numeric.c:2323
Datum numeric_add(PG_FUNCTION_ARGS)
Definition: numeric.c:2711
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ numeric_is_less()

static bool numeric_is_less ( Numeric  a,
Numeric  b 
)
static

Definition at line 583 of file dbsize.c.

References DatumGetBool, DirectFunctionCall2, numeric_lt(), and NumericGetDatum.

Referenced by pg_size_pretty_numeric().

584 {
585  Datum da = NumericGetDatum(a);
586  Datum db = NumericGetDatum(b);
587 
589 }
#define NumericGetDatum(X)
Definition: numeric.h:51
#define DatumGetBool(X)
Definition: postgres.h:393
uintptr_t Datum
Definition: postgres.h:367
Datum numeric_lt(PG_FUNCTION_ARGS)
Definition: numeric.c:2338
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ numeric_shift_right()

static Numeric numeric_shift_right ( Numeric  n,
unsigned  count 
)
static

Definition at line 624 of file dbsize.c.

References DatumGetNumeric, DirectFunctionCall2, int64_to_numeric(), numeric_div_trunc(), and NumericGetDatum.

Referenced by pg_size_pretty_numeric().

625 {
626  Datum d = NumericGetDatum(n);
627  Datum divisor_numeric;
628  Datum result;
629 
630  divisor_numeric = NumericGetDatum(int64_to_numeric(((int64) 1) << count));
631  result = DirectFunctionCall2(numeric_div_trunc, d, divisor_numeric);
632  return DatumGetNumeric(result);
633 }
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_div_trunc(PG_FUNCTION_ARGS)
Definition: numeric.c:3115
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4079
uintptr_t Datum
Definition: postgres.h:367
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ numeric_to_cstring()

static char* numeric_to_cstring ( Numeric  n)
static

Definition at line 575 of file dbsize.c.

References DatumGetCString, DirectFunctionCall1, numeric_out(), and NumericGetDatum.

Referenced by pg_size_pretty_numeric().

576 {
577  Datum d = NumericGetDatum(n);
578 
580 }
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:739
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
#define DatumGetCString(X)
Definition: postgres.h:566
uintptr_t Datum
Definition: postgres.h:367

◆ pg_database_size_name()

Datum pg_database_size_name ( PG_FUNCTION_ARGS  )

Definition at line 147 of file dbsize.c.

References calculate_database_size(), dbName, get_database_oid(), NameStr, PG_GETARG_NAME, PG_RETURN_INT64, and PG_RETURN_NULL.

148 {
150  Oid dbOid = get_database_oid(NameStr(*dbName), false);
151  int64 size;
152 
153  size = calculate_database_size(dbOid);
154 
155  if (size == 0)
156  PG_RETURN_NULL();
157 
158  PG_RETURN_INT64(size);
159 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
unsigned int Oid
Definition: postgres_ext.h:31
Definition: c.h:617
Oid get_database_oid(const char *dbname, bool missing_ok)
Definition: dbcommands.c:2108
static int64 calculate_database_size(Oid dbOid)
Definition: dbsize.c:83
char * dbName
Definition: pgbench.c:246
#define NameStr(name)
Definition: c.h:623
#define PG_RETURN_NULL()
Definition: fmgr.h:344
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_database_size_oid()

Datum pg_database_size_oid ( PG_FUNCTION_ARGS  )

Definition at line 133 of file dbsize.c.

References calculate_database_size(), PG_GETARG_OID, PG_RETURN_INT64, and PG_RETURN_NULL.

134 {
135  Oid dbOid = PG_GETARG_OID(0);
136  int64 size;
137 
138  size = calculate_database_size(dbOid);
139 
140  if (size == 0)
141  PG_RETURN_NULL();
142 
143  PG_RETURN_INT64(size);
144 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
static int64 calculate_database_size(Oid dbOid)
Definition: dbsize.c:83
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_filenode_relation()

Datum pg_filenode_relation ( PG_FUNCTION_ARGS  )

Definition at line 902 of file dbsize.c.

References InvalidOid, OidIsValid, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_OID, and RelidByRelfilenode().

903 {
904  Oid reltablespace = PG_GETARG_OID(0);
905  Oid relfilenode = PG_GETARG_OID(1);
906  Oid heaprel = InvalidOid;
907 
908  heaprel = RelidByRelfilenode(reltablespace, relfilenode);
909 
910  if (!OidIsValid(heaprel))
911  PG_RETURN_NULL();
912  else
913  PG_RETURN_OID(heaprel);
914 }
Oid RelidByRelfilenode(Oid reltablespace, Oid relfilenode)
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_RETURN_OID(x)
Definition: fmgr.h:359
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_indexes_size()

Datum pg_indexes_size ( PG_FUNCTION_ARGS  )

Definition at line 470 of file dbsize.c.

References AccessShareLock, calculate_indexes_size(), PG_GETARG_OID, PG_RETURN_INT64, PG_RETURN_NULL, relation_close(), and try_relation_open().

471 {
472  Oid relOid = PG_GETARG_OID(0);
473  Relation rel;
474  int64 size;
475 
476  rel = try_relation_open(relOid, AccessShareLock);
477 
478  if (rel == NULL)
479  PG_RETURN_NULL();
480 
481  size = calculate_indexes_size(rel);
482 
484 
485  PG_RETURN_INT64(size);
486 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344
static int64 calculate_indexes_size(Relation rel)
Definition: dbsize.c:416

◆ pg_relation_filenode()

Datum pg_relation_filenode ( PG_FUNCTION_ARGS  )

Definition at line 854 of file dbsize.c.

References GETSTRUCT, HeapTupleIsValid, InvalidOid, ObjectIdGetDatum, OidIsValid, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_OID, RelationMapOidToFilenode(), ReleaseSysCache(), RELOID, and SearchSysCache1().

855 {
856  Oid relid = PG_GETARG_OID(0);
857  Oid result;
858  HeapTuple tuple;
859  Form_pg_class relform;
860 
861  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
862  if (!HeapTupleIsValid(tuple))
863  PG_RETURN_NULL();
864  relform = (Form_pg_class) GETSTRUCT(tuple);
865 
866  if (RELKIND_HAS_STORAGE(relform->relkind))
867  {
868  if (relform->relfilenode)
869  result = relform->relfilenode;
870  else /* Consult the relation mapper */
871  result = RelationMapOidToFilenode(relid,
872  relform->relisshared);
873  }
874  else
875  {
876  /* no storage, return NULL */
877  result = InvalidOid;
878  }
879 
880  ReleaseSysCache(tuple);
881 
882  if (!OidIsValid(result))
883  PG_RETURN_NULL();
884 
885  PG_RETURN_OID(result);
886 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
Oid RelationMapOidToFilenode(Oid relationId, bool shared)
Definition: relmapper.c:159
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define PG_RETURN_OID(x)
Definition: fmgr.h:359
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_relation_filepath()

Datum pg_relation_filepath ( PG_FUNCTION_ARGS  )

Definition at line 922 of file dbsize.c.

References Assert, BackendIdForTempRelations, cstring_to_text(), RelFileNode::dbNode, elog, ERROR, GETSTRUCT, GetTempNamespaceBackendId(), HeapTupleIsValid, InvalidBackendId, InvalidOid, isTempOrTempToastNamespace(), MAIN_FORKNUM, MyDatabaseId, MyDatabaseTableSpace, ObjectIdGetDatum, OidIsValid, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TEXT_P, RelationMapOidToFilenode(), ReleaseSysCache(), RelFileNode::relNode, RELOID, relpathbackend, SearchSysCache1(), and RelFileNode::spcNode.

923 {
924  Oid relid = PG_GETARG_OID(0);
925  HeapTuple tuple;
926  Form_pg_class relform;
927  RelFileNode rnode;
928  BackendId backend;
929  char *path;
930 
931  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
932  if (!HeapTupleIsValid(tuple))
933  PG_RETURN_NULL();
934  relform = (Form_pg_class) GETSTRUCT(tuple);
935 
936  if (RELKIND_HAS_STORAGE(relform->relkind))
937  {
938  /* This logic should match RelationInitPhysicalAddr */
939  if (relform->reltablespace)
940  rnode.spcNode = relform->reltablespace;
941  else
943  if (rnode.spcNode == GLOBALTABLESPACE_OID)
944  rnode.dbNode = InvalidOid;
945  else
946  rnode.dbNode = MyDatabaseId;
947  if (relform->relfilenode)
948  rnode.relNode = relform->relfilenode;
949  else /* Consult the relation mapper */
950  rnode.relNode = RelationMapOidToFilenode(relid,
951  relform->relisshared);
952  }
953  else
954  {
955  /* no storage, return NULL */
956  rnode.relNode = InvalidOid;
957  /* some compilers generate warnings without these next two lines */
958  rnode.dbNode = InvalidOid;
959  rnode.spcNode = InvalidOid;
960  }
961 
962  if (!OidIsValid(rnode.relNode))
963  {
964  ReleaseSysCache(tuple);
965  PG_RETURN_NULL();
966  }
967 
968  /* Determine owning backend. */
969  switch (relform->relpersistence)
970  {
971  case RELPERSISTENCE_UNLOGGED:
972  case RELPERSISTENCE_PERMANENT:
973  backend = InvalidBackendId;
974  break;
975  case RELPERSISTENCE_TEMP:
976  if (isTempOrTempToastNamespace(relform->relnamespace))
977  backend = BackendIdForTempRelations();
978  else
979  {
980  /* Do it the hard way. */
981  backend = GetTempNamespaceBackendId(relform->relnamespace);
982  Assert(backend != InvalidBackendId);
983  }
984  break;
985  default:
986  elog(ERROR, "invalid relpersistence: %c", relform->relpersistence);
987  backend = InvalidBackendId; /* placate compiler */
988  break;
989  }
990 
991  ReleaseSysCache(tuple);
992 
993  path = relpathbackend(rnode, backend, MAIN_FORKNUM);
994 
996 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
bool isTempOrTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3180
int GetTempNamespaceBackendId(Oid namespaceId)
Definition: namespace.c:3273
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
Oid MyDatabaseTableSpace
Definition: globals.c:87
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define BackendIdForTempRelations()
Definition: backendid.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
#define InvalidBackendId
Definition: backendid.h:23
Oid RelationMapOidToFilenode(Oid relationId, bool shared)
Definition: relmapper.c:159
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
int BackendId
Definition: backendid.h:21
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
text * cstring_to_text(const char *s)
Definition: varlena.c:188
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:746
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define elog(elevel,...)
Definition: elog.h:214
#define relpathbackend(rnode, backend, forknum)
Definition: relpath.h:78
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_relation_size()

Datum pg_relation_size ( PG_FUNCTION_ARGS  )

Definition at line 311 of file dbsize.c.

References AccessShareLock, calculate_relation_size(), forkname_to_number(), PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_INT64, PG_RETURN_NULL, RelationData::rd_backend, RelationData::rd_node, relation_close(), text_to_cstring(), and try_relation_open().

312 {
313  Oid relOid = PG_GETARG_OID(0);
314  text *forkName = PG_GETARG_TEXT_PP(1);
315  Relation rel;
316  int64 size;
317 
318  rel = try_relation_open(relOid, AccessShareLock);
319 
320  /*
321  * Before 9.2, we used to throw an error if the relation didn't exist, but
322  * that makes queries like "SELECT pg_relation_size(oid) FROM pg_class"
323  * less robust, because while we scan pg_class with an MVCC snapshot,
324  * someone else might drop the table. It's better to return NULL for
325  * already-dropped tables than throw an error and abort the whole query.
326  */
327  if (rel == NULL)
328  PG_RETURN_NULL();
329 
330  size = calculate_relation_size(&(rel->rd_node), rel->rd_backend,
332 
334 
335  PG_RETURN_INT64(size);
336 }
static int64 calculate_relation_size(RelFileNode *rfn, BackendId backend, ForkNumber forknum)
Definition: dbsize.c:273
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:50
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
RelFileNode rd_node
Definition: rel.h:55
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
BackendId rd_backend
Definition: rel.h:59
char * text_to_cstring(const text *t)
Definition: varlena.c:221
Definition: c.h:563
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_size_bytes()

Datum pg_size_bytes ( PG_FUNCTION_ARGS  )

Definition at line 698 of file dbsize.c.

References arg, CStringGetDatum, DatumGetInt64, DatumGetNumeric, DirectFunctionCall1, DirectFunctionCall2, DirectFunctionCall3, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, Int32GetDatum, int64_to_numeric(), InvalidOid, numeric_in(), numeric_int8(), numeric_mul(), NumericGetDatum, ObjectIdGetDatum, PG_GETARG_TEXT_PP, PG_RETURN_INT64, pg_strcasecmp(), generate_unaccent_rules::str, text_to_cstring(), and VARSIZE_ANY_EXHDR.

699 {
700  text *arg = PG_GETARG_TEXT_PP(0);
701  char *str,
702  *strptr,
703  *endptr;
704  char saved_char;
705  Numeric num;
706  int64 result;
707  bool have_digits = false;
708 
709  str = text_to_cstring(arg);
710 
711  /* Skip leading whitespace */
712  strptr = str;
713  while (isspace((unsigned char) *strptr))
714  strptr++;
715 
716  /* Check that we have a valid number and determine where it ends */
717  endptr = strptr;
718 
719  /* Part (1): sign */
720  if (*endptr == '-' || *endptr == '+')
721  endptr++;
722 
723  /* Part (2): main digit string */
724  if (isdigit((unsigned char) *endptr))
725  {
726  have_digits = true;
727  do
728  endptr++;
729  while (isdigit((unsigned char) *endptr));
730  }
731 
732  /* Part (3): optional decimal point and fractional digits */
733  if (*endptr == '.')
734  {
735  endptr++;
736  if (isdigit((unsigned char) *endptr))
737  {
738  have_digits = true;
739  do
740  endptr++;
741  while (isdigit((unsigned char) *endptr));
742  }
743  }
744 
745  /* Complain if we don't have a valid number at this point */
746  if (!have_digits)
747  ereport(ERROR,
748  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
749  errmsg("invalid size: \"%s\"", str)));
750 
751  /* Part (4): optional exponent */
752  if (*endptr == 'e' || *endptr == 'E')
753  {
754  long exponent;
755  char *cp;
756 
757  /*
758  * Note we might one day support EB units, so if what follows 'E'
759  * isn't a number, just treat it all as a unit to be parsed.
760  */
761  exponent = strtol(endptr + 1, &cp, 10);
762  (void) exponent; /* Silence -Wunused-result warnings */
763  if (cp > endptr + 1)
764  endptr = cp;
765  }
766 
767  /*
768  * Parse the number, saving the next character, which may be the first
769  * character of the unit string.
770  */
771  saved_char = *endptr;
772  *endptr = '\0';
773 
775  CStringGetDatum(strptr),
777  Int32GetDatum(-1)));
778 
779  *endptr = saved_char;
780 
781  /* Skip whitespace between number and unit */
782  strptr = endptr;
783  while (isspace((unsigned char) *strptr))
784  strptr++;
785 
786  /* Handle possible unit */
787  if (*strptr != '\0')
788  {
789  int64 multiplier = 0;
790 
791  /* Trim any trailing whitespace */
792  endptr = str + VARSIZE_ANY_EXHDR(arg) - 1;
793 
794  while (isspace((unsigned char) *endptr))
795  endptr--;
796 
797  endptr++;
798  *endptr = '\0';
799 
800  /* Parse the unit case-insensitively */
801  if (pg_strcasecmp(strptr, "bytes") == 0)
802  multiplier = (int64) 1;
803  else if (pg_strcasecmp(strptr, "kb") == 0)
804  multiplier = (int64) 1024;
805  else if (pg_strcasecmp(strptr, "mb") == 0)
806  multiplier = ((int64) 1024) * 1024;
807 
808  else if (pg_strcasecmp(strptr, "gb") == 0)
809  multiplier = ((int64) 1024) * 1024 * 1024;
810 
811  else if (pg_strcasecmp(strptr, "tb") == 0)
812  multiplier = ((int64) 1024) * 1024 * 1024 * 1024;
813 
814  else
815  ereport(ERROR,
816  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
817  errmsg("invalid size: \"%s\"", text_to_cstring(arg)),
818  errdetail("Invalid size unit: \"%s\".", strptr),
819  errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", and \"TB\".")));
820 
821  if (multiplier > 1)
822  {
823  Numeric mul_num;
824 
825  mul_num = int64_to_numeric(multiplier);
826 
828  NumericGetDatum(mul_num),
829  NumericGetDatum(num)));
830  }
831  }
832 
834  NumericGetDatum(num)));
835 
836  PG_RETURN_INT64(result);
837 }
int errhint(const char *fmt,...)
Definition: elog.c:1068
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
#define NumericGetDatum(X)
Definition: numeric.h:51
Datum numeric_int8(PG_FUNCTION_ARGS)
Definition: numeric.c:4191
int errcode(int sqlerrcode)
Definition: elog.c:610
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define DatumGetInt64(X)
Definition: postgres.h:607
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4079
int errdetail(const char *fmt,...)
Definition: elog.c:954
#define CStringGetDatum(X)
Definition: postgres.h:578
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:621
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:628
Datum numeric_mul(PG_FUNCTION_ARGS)
Definition: numeric.c:2866
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define DatumGetNumeric(X)
Definition: numeric.h:49
char * text_to_cstring(const text *t)
Definition: varlena.c:221
#define Int32GetDatum(X)
Definition: postgres.h:479
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:821
void * arg
Definition: c.h:563
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ pg_size_pretty()

Datum pg_size_pretty ( PG_FUNCTION_ARGS  )

Definition at line 534 of file dbsize.c.

References Abs, buf, cstring_to_text(), half_rounded, INT64_FORMAT, PG_GETARG_INT64, PG_RETURN_TEXT_P, and snprintf.

535 {
536  int64 size = PG_GETARG_INT64(0);
537  char buf[64];
538  int64 limit = 10 * 1024;
539  int64 limit2 = limit * 2 - 1;
540 
541  if (Abs(size) < limit)
542  snprintf(buf, sizeof(buf), INT64_FORMAT " bytes", size);
543  else
544  {
545  size >>= 9; /* keep one extra bit for rounding */
546  if (Abs(size) < limit2)
547  snprintf(buf, sizeof(buf), INT64_FORMAT " kB",
548  half_rounded(size));
549  else
550  {
551  size >>= 10;
552  if (Abs(size) < limit2)
553  snprintf(buf, sizeof(buf), INT64_FORMAT " MB",
554  half_rounded(size));
555  else
556  {
557  size >>= 10;
558  if (Abs(size) < limit2)
559  snprintf(buf, sizeof(buf), INT64_FORMAT " GB",
560  half_rounded(size));
561  else
562  {
563  size >>= 10;
564  snprintf(buf, sizeof(buf), INT64_FORMAT " TB",
565  half_rounded(size));
566  }
567  }
568  }
569  }
570 
572 }
#define Abs(x)
Definition: c.h:934
#define half_rounded(x)
Definition: dbsize.c:35
static char * buf
Definition: pg_test_fsync.c:68
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
text * cstring_to_text(const char *s)
Definition: varlena.c:188
#define INT64_FORMAT
Definition: c.h:417
#define PG_GETARG_INT64(n)
Definition: fmgr.h:282
#define snprintf
Definition: port.h:215

◆ pg_size_pretty_numeric()

Datum pg_size_pretty_numeric ( PG_FUNCTION_ARGS  )

Definition at line 636 of file dbsize.c.

References cstring_to_text(), int64_to_numeric(), numeric_absolute(), numeric_half_rounded(), numeric_is_less(), numeric_shift_right(), numeric_to_cstring(), PG_GETARG_NUMERIC, PG_RETURN_TEXT_P, and psprintf().

637 {
638  Numeric size = PG_GETARG_NUMERIC(0);
639  Numeric limit,
640  limit2;
641  char *result;
642 
643  limit = int64_to_numeric(10 * 1024);
644  limit2 = int64_to_numeric(10 * 1024 * 2 - 1);
645 
646  if (numeric_is_less(numeric_absolute(size), limit))
647  {
648  result = psprintf("%s bytes", numeric_to_cstring(size));
649  }
650  else
651  {
652  /* keep one extra bit for rounding */
653  /* size >>= 9 */
654  size = numeric_shift_right(size, 9);
655 
656  if (numeric_is_less(numeric_absolute(size), limit2))
657  {
658  size = numeric_half_rounded(size);
659  result = psprintf("%s kB", numeric_to_cstring(size));
660  }
661  else
662  {
663  /* size >>= 10 */
664  size = numeric_shift_right(size, 10);
665  if (numeric_is_less(numeric_absolute(size), limit2))
666  {
667  size = numeric_half_rounded(size);
668  result = psprintf("%s MB", numeric_to_cstring(size));
669  }
670  else
671  {
672  /* size >>= 10 */
673  size = numeric_shift_right(size, 10);
674 
675  if (numeric_is_less(numeric_absolute(size), limit2))
676  {
677  size = numeric_half_rounded(size);
678  result = psprintf("%s GB", numeric_to_cstring(size));
679  }
680  else
681  {
682  /* size >>= 10 */
683  size = numeric_shift_right(size, 10);
684  size = numeric_half_rounded(size);
685  result = psprintf("%s TB", numeric_to_cstring(size));
686  }
687  }
688  }
689  }
690 
692 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static Numeric numeric_shift_right(Numeric n, unsigned count)
Definition: dbsize.c:624
static char * numeric_to_cstring(Numeric n)
Definition: dbsize.c:575
static Numeric numeric_absolute(Numeric n)
Definition: dbsize.c:592
static Numeric numeric_half_rounded(Numeric n)
Definition: dbsize.c:602
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4079
static bool numeric_is_less(Numeric a, Numeric b)
Definition: dbsize.c:583
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
text * cstring_to_text(const char *s)
Definition: varlena.c:188
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52

◆ pg_table_size()

Datum pg_table_size ( PG_FUNCTION_ARGS  )

Definition at line 451 of file dbsize.c.

References AccessShareLock, calculate_table_size(), PG_GETARG_OID, PG_RETURN_INT64, PG_RETURN_NULL, relation_close(), and try_relation_open().

452 {
453  Oid relOid = PG_GETARG_OID(0);
454  Relation rel;
455  int64 size;
456 
457  rel = try_relation_open(relOid, AccessShareLock);
458 
459  if (rel == NULL)
460  PG_RETURN_NULL();
461 
462  size = calculate_table_size(rel);
463 
465 
466  PG_RETURN_INT64(size);
467 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
static int64 calculate_table_size(Relation rel)
Definition: dbsize.c:389
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_tablespace_size_name()

Datum pg_tablespace_size_name ( PG_FUNCTION_ARGS  )

Definition at line 251 of file dbsize.c.

References calculate_tablespace_size(), get_tablespace_oid(), NameStr, PG_GETARG_NAME, PG_RETURN_INT64, and PG_RETURN_NULL.

252 {
253  Name tblspcName = PG_GETARG_NAME(0);
254  Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName), false);
255  int64 size;
256 
257  size = calculate_tablespace_size(tblspcOid);
258 
259  if (size < 0)
260  PG_RETURN_NULL();
261 
262  PG_RETURN_INT64(size);
263 }
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
Definition: tablespace.c:1421
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
unsigned int Oid
Definition: postgres_ext.h:31
Definition: c.h:617
static int64 calculate_tablespace_size(Oid tblspcOid)
Definition: dbsize.c:167
#define NameStr(name)
Definition: c.h:623
#define PG_RETURN_NULL()
Definition: fmgr.h:344
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_tablespace_size_oid()

Datum pg_tablespace_size_oid ( PG_FUNCTION_ARGS  )

Definition at line 237 of file dbsize.c.

References calculate_tablespace_size(), PG_GETARG_OID, PG_RETURN_INT64, and PG_RETURN_NULL.

238 {
239  Oid tblspcOid = PG_GETARG_OID(0);
240  int64 size;
241 
242  size = calculate_tablespace_size(tblspcOid);
243 
244  if (size < 0)
245  PG_RETURN_NULL();
246 
247  PG_RETURN_INT64(size);
248 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
static int64 calculate_tablespace_size(Oid tblspcOid)
Definition: dbsize.c:167
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_total_relation_size()

Datum pg_total_relation_size ( PG_FUNCTION_ARGS  )

Definition at line 512 of file dbsize.c.

References AccessShareLock, calculate_total_relation_size(), PG_GETARG_OID, PG_RETURN_INT64, PG_RETURN_NULL, relation_close(), and try_relation_open().

513 {
514  Oid relOid = PG_GETARG_OID(0);
515  Relation rel;
516  int64 size;
517 
518  rel = try_relation_open(relOid, AccessShareLock);
519 
520  if (rel == NULL)
521  PG_RETURN_NULL();
522 
523  size = calculate_total_relation_size(rel);
524 
526 
527  PG_RETURN_INT64(size);
528 }
static int64 calculate_total_relation_size(Relation rel)
Definition: dbsize.c:493
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344