PostgreSQL Source Code  git master
dbsize.c
Go to the documentation of this file.
1 /*
2  * dbsize.c
3  * Database object size functions, and related inquiries
4  *
5  * Copyright (c) 2002-2023, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/backend/utils/adt/dbsize.c
9  *
10  */
11 
12 #include "postgres.h"
13 
14 #include <sys/stat.h>
15 
16 #include "access/htup_details.h"
17 #include "access/relation.h"
18 #include "catalog/catalog.h"
19 #include "catalog/namespace.h"
20 #include "catalog/pg_authid.h"
21 #include "catalog/pg_database.h"
22 #include "catalog/pg_tablespace.h"
23 #include "commands/dbcommands.h"
24 #include "commands/tablespace.h"
25 #include "miscadmin.h"
26 #include "storage/fd.h"
27 #include "utils/acl.h"
28 #include "utils/builtins.h"
29 #include "utils/numeric.h"
30 #include "utils/rel.h"
31 #include "utils/relfilenumbermap.h"
32 #include "utils/relmapper.h"
33 #include "utils/syscache.h"
34 
35 /* Divide by two and round away from zero */
36 #define half_rounded(x) (((x) + ((x) < 0 ? -1 : 1)) / 2)
37 
38 /* Units used in pg_size_pretty functions. All units must be powers of 2 */
40 {
41  const char *name; /* bytes, kB, MB, GB etc */
42  uint32 limit; /* upper limit, prior to half rounding after
43  * converting to this unit. */
44  bool round; /* do half rounding for this unit */
45  uint8 unitbits; /* (1 << unitbits) bytes to make 1 of this
46  * unit */
47 };
48 
49 /* When adding units here also update the docs and the error message in pg_size_bytes */
50 static const struct size_pretty_unit size_pretty_units[] = {
51  {"bytes", 10 * 1024, false, 0},
52  {"kB", 20 * 1024 - 1, true, 10},
53  {"MB", 20 * 1024 - 1, true, 20},
54  {"GB", 20 * 1024 - 1, true, 30},
55  {"TB", 20 * 1024 - 1, true, 40},
56  {"PB", 20 * 1024 - 1, true, 50},
57  {NULL, 0, false, 0}
58 };
59 
60 /* Additional unit aliases accepted by pg_size_bytes */
62 {
63  const char *alias;
64  int unit_index; /* corresponding size_pretty_units element */
65 };
66 
67 /* When adding units here also update the docs and the error message in pg_size_bytes */
68 static const struct size_bytes_unit_alias size_bytes_aliases[] = {
69  {"B", 0},
70  {NULL}
71 };
72 
73 /* Return physical size of directory contents, or 0 if dir doesn't exist */
74 static int64
75 db_dir_size(const char *path)
76 {
77  int64 dirsize = 0;
78  struct dirent *direntry;
79  DIR *dirdesc;
80  char filename[MAXPGPATH * 2];
81 
82  dirdesc = AllocateDir(path);
83 
84  if (!dirdesc)
85  return 0;
86 
87  while ((direntry = ReadDir(dirdesc, path)) != NULL)
88  {
89  struct stat fst;
90 
92 
93  if (strcmp(direntry->d_name, ".") == 0 ||
94  strcmp(direntry->d_name, "..") == 0)
95  continue;
96 
97  snprintf(filename, sizeof(filename), "%s/%s", path, direntry->d_name);
98 
99  if (stat(filename, &fst) < 0)
100  {
101  if (errno == ENOENT)
102  continue;
103  else
104  ereport(ERROR,
106  errmsg("could not stat file \"%s\": %m", filename)));
107  }
108  dirsize += fst.st_size;
109  }
110 
111  FreeDir(dirdesc);
112  return dirsize;
113 }
114 
115 /*
116  * calculate size of database in all tablespaces
117  */
118 static int64
120 {
121  int64 totalsize;
122  DIR *dirdesc;
123  struct dirent *direntry;
124  char dirpath[MAXPGPATH];
125  char pathname[MAXPGPATH + 21 + sizeof(TABLESPACE_VERSION_DIRECTORY)];
126  AclResult aclresult;
127 
128  /*
129  * User must have connect privilege for target database or have privileges
130  * of pg_read_all_stats
131  */
132  aclresult = object_aclcheck(DatabaseRelationId, dbOid, GetUserId(), ACL_CONNECT);
133  if (aclresult != ACLCHECK_OK &&
134  !has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS))
135  {
136  aclcheck_error(aclresult, OBJECT_DATABASE,
137  get_database_name(dbOid));
138  }
139 
140  /* Shared storage in pg_global is not counted */
141 
142  /* Include pg_default storage */
143  snprintf(pathname, sizeof(pathname), "base/%u", dbOid);
144  totalsize = db_dir_size(pathname);
145 
146  /* Scan the non-default tablespaces */
147  snprintf(dirpath, MAXPGPATH, "pg_tblspc");
148  dirdesc = AllocateDir(dirpath);
149 
150  while ((direntry = ReadDir(dirdesc, dirpath)) != NULL)
151  {
153 
154  if (strcmp(direntry->d_name, ".") == 0 ||
155  strcmp(direntry->d_name, "..") == 0)
156  continue;
157 
158  snprintf(pathname, sizeof(pathname), "pg_tblspc/%s/%s/%u",
159  direntry->d_name, TABLESPACE_VERSION_DIRECTORY, dbOid);
160  totalsize += db_dir_size(pathname);
161  }
162 
163  FreeDir(dirdesc);
164 
165  return totalsize;
166 }
167 
168 Datum
170 {
171  Oid dbOid = PG_GETARG_OID(0);
172  int64 size;
173 
174  size = calculate_database_size(dbOid);
175 
176  if (size == 0)
177  PG_RETURN_NULL();
178 
179  PG_RETURN_INT64(size);
180 }
181 
182 Datum
184 {
186  Oid dbOid = get_database_oid(NameStr(*dbName), false);
187  int64 size;
188 
189  size = calculate_database_size(dbOid);
190 
191  if (size == 0)
192  PG_RETURN_NULL();
193 
194  PG_RETURN_INT64(size);
195 }
196 
197 
198 /*
199  * Calculate total size of tablespace. Returns -1 if the tablespace directory
200  * cannot be found.
201  */
202 static int64
204 {
205  char tblspcPath[MAXPGPATH];
206  char pathname[MAXPGPATH * 2];
207  int64 totalsize = 0;
208  DIR *dirdesc;
209  struct dirent *direntry;
210  AclResult aclresult;
211 
212  /*
213  * User must have privileges of pg_read_all_stats or have CREATE privilege
214  * for target tablespace, either explicitly granted or implicitly because
215  * it is default for current database.
216  */
217  if (tblspcOid != MyDatabaseTableSpace &&
218  !has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS))
219  {
220  aclresult = object_aclcheck(TableSpaceRelationId, tblspcOid, GetUserId(), ACL_CREATE);
221  if (aclresult != ACLCHECK_OK)
223  get_tablespace_name(tblspcOid));
224  }
225 
226  if (tblspcOid == DEFAULTTABLESPACE_OID)
227  snprintf(tblspcPath, MAXPGPATH, "base");
228  else if (tblspcOid == GLOBALTABLESPACE_OID)
229  snprintf(tblspcPath, MAXPGPATH, "global");
230  else
231  snprintf(tblspcPath, MAXPGPATH, "pg_tblspc/%u/%s", tblspcOid,
233 
234  dirdesc = AllocateDir(tblspcPath);
235 
236  if (!dirdesc)
237  return -1;
238 
239  while ((direntry = ReadDir(dirdesc, tblspcPath)) != NULL)
240  {
241  struct stat fst;
242 
244 
245  if (strcmp(direntry->d_name, ".") == 0 ||
246  strcmp(direntry->d_name, "..") == 0)
247  continue;
248 
249  snprintf(pathname, sizeof(pathname), "%s/%s", tblspcPath, direntry->d_name);
250 
251  if (stat(pathname, &fst) < 0)
252  {
253  if (errno == ENOENT)
254  continue;
255  else
256  ereport(ERROR,
258  errmsg("could not stat file \"%s\": %m", pathname)));
259  }
260 
261  if (S_ISDIR(fst.st_mode))
262  totalsize += db_dir_size(pathname);
263 
264  totalsize += fst.st_size;
265  }
266 
267  FreeDir(dirdesc);
268 
269  return totalsize;
270 }
271 
272 Datum
274 {
275  Oid tblspcOid = PG_GETARG_OID(0);
276  int64 size;
277 
278  size = calculate_tablespace_size(tblspcOid);
279 
280  if (size < 0)
281  PG_RETURN_NULL();
282 
283  PG_RETURN_INT64(size);
284 }
285 
286 Datum
288 {
289  Name tblspcName = PG_GETARG_NAME(0);
290  Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName), false);
291  int64 size;
292 
293  size = calculate_tablespace_size(tblspcOid);
294 
295  if (size < 0)
296  PG_RETURN_NULL();
297 
298  PG_RETURN_INT64(size);
299 }
300 
301 
302 /*
303  * calculate size of (one fork of) a relation
304  *
305  * Note: we can safely apply this to temp tables of other sessions, so there
306  * is no check here or at the call sites for that.
307  */
308 static int64
310 {
311  int64 totalsize = 0;
312  char *relationpath;
313  char pathname[MAXPGPATH];
314  unsigned int segcount = 0;
315 
316  relationpath = relpathbackend(*rfn, backend, forknum);
317 
318  for (segcount = 0;; segcount++)
319  {
320  struct stat fst;
321 
323 
324  if (segcount == 0)
325  snprintf(pathname, MAXPGPATH, "%s",
326  relationpath);
327  else
328  snprintf(pathname, MAXPGPATH, "%s.%u",
329  relationpath, segcount);
330 
331  if (stat(pathname, &fst) < 0)
332  {
333  if (errno == ENOENT)
334  break;
335  else
336  ereport(ERROR,
338  errmsg("could not stat file \"%s\": %m", pathname)));
339  }
340  totalsize += fst.st_size;
341  }
342 
343  return totalsize;
344 }
345 
346 Datum
348 {
349  Oid relOid = PG_GETARG_OID(0);
350  text *forkName = PG_GETARG_TEXT_PP(1);
351  Relation rel;
352  int64 size;
353 
354  rel = try_relation_open(relOid, AccessShareLock);
355 
356  /*
357  * Before 9.2, we used to throw an error if the relation didn't exist, but
358  * that makes queries like "SELECT pg_relation_size(oid) FROM pg_class"
359  * less robust, because while we scan pg_class with an MVCC snapshot,
360  * someone else might drop the table. It's better to return NULL for
361  * already-dropped tables than throw an error and abort the whole query.
362  */
363  if (rel == NULL)
364  PG_RETURN_NULL();
365 
366  size = calculate_relation_size(&(rel->rd_locator), rel->rd_backend,
368 
370 
371  PG_RETURN_INT64(size);
372 }
373 
374 /*
375  * Calculate total on-disk size of a TOAST relation, including its indexes.
376  * Must not be applied to non-TOAST relations.
377  */
378 static int64
380 {
381  int64 size = 0;
382  Relation toastRel;
383  ForkNumber forkNum;
384  ListCell *lc;
385  List *indexlist;
386 
387  toastRel = relation_open(toastrelid, AccessShareLock);
388 
389  /* toast heap size, including FSM and VM size */
390  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
391  size += calculate_relation_size(&(toastRel->rd_locator),
392  toastRel->rd_backend, forkNum);
393 
394  /* toast index size, including FSM and VM size */
395  indexlist = RelationGetIndexList(toastRel);
396 
397  /* Size is calculated using all the indexes available */
398  foreach(lc, indexlist)
399  {
400  Relation toastIdxRel;
401 
402  toastIdxRel = relation_open(lfirst_oid(lc),
404  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
405  size += calculate_relation_size(&(toastIdxRel->rd_locator),
406  toastIdxRel->rd_backend, forkNum);
407 
408  relation_close(toastIdxRel, AccessShareLock);
409  }
410  list_free(indexlist);
411  relation_close(toastRel, AccessShareLock);
412 
413  return size;
414 }
415 
416 /*
417  * Calculate total on-disk size of a given table,
418  * including FSM and VM, plus TOAST table if any.
419  * Indexes other than the TOAST table's index are not included.
420  *
421  * Note that this also behaves sanely if applied to an index or toast table;
422  * those won't have attached toast tables, but they can have multiple forks.
423  */
424 static int64
426 {
427  int64 size = 0;
428  ForkNumber forkNum;
429 
430  /*
431  * heap size, including FSM and VM
432  */
433  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
434  size += calculate_relation_size(&(rel->rd_locator), rel->rd_backend,
435  forkNum);
436 
437  /*
438  * Size of toast relation
439  */
440  if (OidIsValid(rel->rd_rel->reltoastrelid))
441  size += calculate_toast_table_size(rel->rd_rel->reltoastrelid);
442 
443  return size;
444 }
445 
446 /*
447  * Calculate total on-disk size of all indexes attached to the given table.
448  *
449  * Can be applied safely to an index, but you'll just get zero.
450  */
451 static int64
453 {
454  int64 size = 0;
455 
456  /*
457  * Aggregate all indexes on the given relation
458  */
459  if (rel->rd_rel->relhasindex)
460  {
461  List *index_oids = RelationGetIndexList(rel);
462  ListCell *cell;
463 
464  foreach(cell, index_oids)
465  {
466  Oid idxOid = lfirst_oid(cell);
467  Relation idxRel;
468  ForkNumber forkNum;
469 
470  idxRel = relation_open(idxOid, AccessShareLock);
471 
472  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
473  size += calculate_relation_size(&(idxRel->rd_locator),
474  idxRel->rd_backend,
475  forkNum);
476 
478  }
479 
480  list_free(index_oids);
481  }
482 
483  return size;
484 }
485 
486 Datum
488 {
489  Oid relOid = PG_GETARG_OID(0);
490  Relation rel;
491  int64 size;
492 
493  rel = try_relation_open(relOid, AccessShareLock);
494 
495  if (rel == NULL)
496  PG_RETURN_NULL();
497 
498  size = calculate_table_size(rel);
499 
501 
502  PG_RETURN_INT64(size);
503 }
504 
505 Datum
507 {
508  Oid relOid = PG_GETARG_OID(0);
509  Relation rel;
510  int64 size;
511 
512  rel = try_relation_open(relOid, AccessShareLock);
513 
514  if (rel == NULL)
515  PG_RETURN_NULL();
516 
517  size = calculate_indexes_size(rel);
518 
520 
521  PG_RETURN_INT64(size);
522 }
523 
524 /*
525  * Compute the on-disk size of all files for the relation,
526  * including heap data, index data, toast data, FSM, VM.
527  */
528 static int64
530 {
531  int64 size;
532 
533  /*
534  * Aggregate the table size, this includes size of the heap, toast and
535  * toast index with free space and visibility map
536  */
537  size = calculate_table_size(rel);
538 
539  /*
540  * Add size of all attached indexes as well
541  */
542  size += calculate_indexes_size(rel);
543 
544  return size;
545 }
546 
547 Datum
549 {
550  Oid relOid = PG_GETARG_OID(0);
551  Relation rel;
552  int64 size;
553 
554  rel = try_relation_open(relOid, AccessShareLock);
555 
556  if (rel == NULL)
557  PG_RETURN_NULL();
558 
559  size = calculate_total_relation_size(rel);
560 
562 
563  PG_RETURN_INT64(size);
564 }
565 
566 /*
567  * formatting with size units
568  */
569 Datum
571 {
572  int64 size = PG_GETARG_INT64(0);
573  char buf[64];
574  const struct size_pretty_unit *unit;
575 
576  for (unit = size_pretty_units; unit->name != NULL; unit++)
577  {
578  uint8 bits;
579 
580  /* use this unit if there are no more units or we're below the limit */
581  if (unit[1].name == NULL || i64abs(size) < unit->limit)
582  {
583  if (unit->round)
584  size = half_rounded(size);
585 
586  snprintf(buf, sizeof(buf), INT64_FORMAT " %s", size, unit->name);
587  break;
588  }
589 
590  /*
591  * Determine the number of bits to use to build the divisor. We may
592  * need to use 1 bit less than the difference between this and the
593  * next unit if the next unit uses half rounding. Or we may need to
594  * shift an extra bit if this unit uses half rounding and the next one
595  * does not. We use division rather than shifting right by this
596  * number of bits to ensure positive and negative values are rounded
597  * in the same way.
598  */
599  bits = (unit[1].unitbits - unit->unitbits - (unit[1].round == true)
600  + (unit->round == true));
601  size /= ((int64) 1) << bits;
602  }
603 
605 }
606 
607 static char *
609 {
610  Datum d = NumericGetDatum(n);
611 
613 }
614 
615 static bool
617 {
618  Datum da = NumericGetDatum(a);
619  Datum db = NumericGetDatum(b);
620 
622 }
623 
624 static Numeric
626 {
627  Datum d = NumericGetDatum(n);
628  Datum result;
629 
630  result = DirectFunctionCall1(numeric_abs, d);
631  return DatumGetNumeric(result);
632 }
633 
634 static Numeric
636 {
637  Datum d = NumericGetDatum(n);
638  Datum zero;
639  Datum one;
640  Datum two;
641  Datum result;
642 
646 
648  d = DirectFunctionCall2(numeric_add, d, one);
649  else
650  d = DirectFunctionCall2(numeric_sub, d, one);
651 
652  result = DirectFunctionCall2(numeric_div_trunc, d, two);
653  return DatumGetNumeric(result);
654 }
655 
656 static Numeric
658 {
659  Datum d = NumericGetDatum(n);
660  Datum divisor_numeric;
661  Datum result;
662 
663  divisor_numeric = NumericGetDatum(int64_to_numeric(divisor));
664  result = DirectFunctionCall2(numeric_div_trunc, d, divisor_numeric);
665  return DatumGetNumeric(result);
666 }
667 
668 Datum
670 {
671  Numeric size = PG_GETARG_NUMERIC(0);
672  char *result = NULL;
673  const struct size_pretty_unit *unit;
674 
675  for (unit = size_pretty_units; unit->name != NULL; unit++)
676  {
677  unsigned int shiftby;
678 
679  /* use this unit if there are no more units or we're below the limit */
680  if (unit[1].name == NULL ||
682  int64_to_numeric(unit->limit)))
683  {
684  if (unit->round)
685  size = numeric_half_rounded(size);
686 
687  result = psprintf("%s %s", numeric_to_cstring(size), unit->name);
688  break;
689  }
690 
691  /*
692  * Determine the number of bits to use to build the divisor. We may
693  * need to use 1 bit less than the difference between this and the
694  * next unit if the next unit uses half rounding. Or we may need to
695  * shift an extra bit if this unit uses half rounding and the next one
696  * does not.
697  */
698  shiftby = (unit[1].unitbits - unit->unitbits - (unit[1].round == true)
699  + (unit->round == true));
700  size = numeric_truncated_divide(size, ((int64) 1) << shiftby);
701  }
702 
704 }
705 
706 /*
707  * Convert a human-readable size to a size in bytes
708  */
709 Datum
711 {
712  text *arg = PG_GETARG_TEXT_PP(0);
713  char *str,
714  *strptr,
715  *endptr;
716  char saved_char;
717  Numeric num;
718  int64 result;
719  bool have_digits = false;
720 
722 
723  /* Skip leading whitespace */
724  strptr = str;
725  while (isspace((unsigned char) *strptr))
726  strptr++;
727 
728  /* Check that we have a valid number and determine where it ends */
729  endptr = strptr;
730 
731  /* Part (1): sign */
732  if (*endptr == '-' || *endptr == '+')
733  endptr++;
734 
735  /* Part (2): main digit string */
736  if (isdigit((unsigned char) *endptr))
737  {
738  have_digits = true;
739  do
740  endptr++;
741  while (isdigit((unsigned char) *endptr));
742  }
743 
744  /* Part (3): optional decimal point and fractional digits */
745  if (*endptr == '.')
746  {
747  endptr++;
748  if (isdigit((unsigned char) *endptr))
749  {
750  have_digits = true;
751  do
752  endptr++;
753  while (isdigit((unsigned char) *endptr));
754  }
755  }
756 
757  /* Complain if we don't have a valid number at this point */
758  if (!have_digits)
759  ereport(ERROR,
760  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
761  errmsg("invalid size: \"%s\"", str)));
762 
763  /* Part (4): optional exponent */
764  if (*endptr == 'e' || *endptr == 'E')
765  {
766  long exponent;
767  char *cp;
768 
769  /*
770  * Note we might one day support EB units, so if what follows 'E'
771  * isn't a number, just treat it all as a unit to be parsed.
772  */
773  exponent = strtol(endptr + 1, &cp, 10);
774  (void) exponent; /* Silence -Wunused-result warnings */
775  if (cp > endptr + 1)
776  endptr = cp;
777  }
778 
779  /*
780  * Parse the number, saving the next character, which may be the first
781  * character of the unit string.
782  */
783  saved_char = *endptr;
784  *endptr = '\0';
785 
787  CStringGetDatum(strptr),
789  Int32GetDatum(-1)));
790 
791  *endptr = saved_char;
792 
793  /* Skip whitespace between number and unit */
794  strptr = endptr;
795  while (isspace((unsigned char) *strptr))
796  strptr++;
797 
798  /* Handle possible unit */
799  if (*strptr != '\0')
800  {
801  const struct size_pretty_unit *unit;
802  int64 multiplier = 0;
803 
804  /* Trim any trailing whitespace */
805  endptr = str + VARSIZE_ANY_EXHDR(arg) - 1;
806 
807  while (isspace((unsigned char) *endptr))
808  endptr--;
809 
810  endptr++;
811  *endptr = '\0';
812 
813  for (unit = size_pretty_units; unit->name != NULL; unit++)
814  {
815  /* Parse the unit case-insensitively */
816  if (pg_strcasecmp(strptr, unit->name) == 0)
817  break;
818  }
819 
820  /* If not found, look in table of aliases */
821  if (unit->name == NULL)
822  {
823  for (const struct size_bytes_unit_alias *a = size_bytes_aliases; a->alias != NULL; a++)
824  {
825  if (pg_strcasecmp(strptr, a->alias) == 0)
826  {
827  unit = &size_pretty_units[a->unit_index];
828  break;
829  }
830  }
831  }
832 
833  /* Verify we found a valid unit in the loop above */
834  if (unit->name == NULL)
835  ereport(ERROR,
836  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
837  errmsg("invalid size: \"%s\"", text_to_cstring(arg)),
838  errdetail("Invalid size unit: \"%s\".", strptr),
839  errhint("Valid units are \"bytes\", \"B\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\".")));
840 
841  multiplier = ((int64) 1) << unit->unitbits;
842 
843  if (multiplier > 1)
844  {
845  Numeric mul_num;
846 
847  mul_num = int64_to_numeric(multiplier);
848 
850  NumericGetDatum(mul_num),
851  NumericGetDatum(num)));
852  }
853  }
854 
856  NumericGetDatum(num)));
857 
858  PG_RETURN_INT64(result);
859 }
860 
861 /*
862  * Get the filenode of a relation
863  *
864  * This is expected to be used in queries like
865  * SELECT pg_relation_filenode(oid) FROM pg_class;
866  * That leads to a couple of choices. We work from the pg_class row alone
867  * rather than actually opening each relation, for efficiency. We don't
868  * fail if we can't find the relation --- some rows might be visible in
869  * the query's MVCC snapshot even though the relations have been dropped.
870  * (Note: we could avoid using the catcache, but there's little point
871  * because the relation mapper also works "in the now".) We also don't
872  * fail if the relation doesn't have storage. In all these cases it
873  * seems better to quietly return NULL.
874  */
875 Datum
877 {
878  Oid relid = PG_GETARG_OID(0);
879  RelFileNumber result;
880  HeapTuple tuple;
881  Form_pg_class relform;
882 
883  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
884  if (!HeapTupleIsValid(tuple))
885  PG_RETURN_NULL();
886  relform = (Form_pg_class) GETSTRUCT(tuple);
887 
888  if (RELKIND_HAS_STORAGE(relform->relkind))
889  {
890  if (relform->relfilenode)
891  result = relform->relfilenode;
892  else /* Consult the relation mapper */
893  result = RelationMapOidToFilenumber(relid,
894  relform->relisshared);
895  }
896  else
897  {
898  /* no storage, return NULL */
899  result = InvalidRelFileNumber;
900  }
901 
902  ReleaseSysCache(tuple);
903 
904  if (!RelFileNumberIsValid(result))
905  PG_RETURN_NULL();
906 
907  PG_RETURN_OID(result);
908 }
909 
910 /*
911  * Get the relation via (reltablespace, relfilenumber)
912  *
913  * This is expected to be used when somebody wants to match an individual file
914  * on the filesystem back to its table. That's not trivially possible via
915  * pg_class, because that doesn't contain the relfilenumbers of shared and nailed
916  * tables.
917  *
918  * We don't fail but return NULL if we cannot find a mapping.
919  *
920  * InvalidOid can be passed instead of the current database's default
921  * tablespace.
922  */
923 Datum
925 {
926  Oid reltablespace = PG_GETARG_OID(0);
927  RelFileNumber relfilenumber = PG_GETARG_OID(1);
928  Oid heaprel;
929 
930  /* test needed so RelidByRelfilenumber doesn't misbehave */
931  if (!RelFileNumberIsValid(relfilenumber))
932  PG_RETURN_NULL();
933 
934  heaprel = RelidByRelfilenumber(reltablespace, relfilenumber);
935 
936  if (!OidIsValid(heaprel))
937  PG_RETURN_NULL();
938  else
939  PG_RETURN_OID(heaprel);
940 }
941 
942 /*
943  * Get the pathname (relative to $PGDATA) of a relation
944  *
945  * See comments for pg_relation_filenode.
946  */
947 Datum
949 {
950  Oid relid = PG_GETARG_OID(0);
951  HeapTuple tuple;
952  Form_pg_class relform;
953  RelFileLocator rlocator;
954  BackendId backend;
955  char *path;
956 
957  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
958  if (!HeapTupleIsValid(tuple))
959  PG_RETURN_NULL();
960  relform = (Form_pg_class) GETSTRUCT(tuple);
961 
962  if (RELKIND_HAS_STORAGE(relform->relkind))
963  {
964  /* This logic should match RelationInitPhysicalAddr */
965  if (relform->reltablespace)
966  rlocator.spcOid = relform->reltablespace;
967  else
968  rlocator.spcOid = MyDatabaseTableSpace;
969  if (rlocator.spcOid == GLOBALTABLESPACE_OID)
970  rlocator.dbOid = InvalidOid;
971  else
972  rlocator.dbOid = MyDatabaseId;
973  if (relform->relfilenode)
974  rlocator.relNumber = relform->relfilenode;
975  else /* Consult the relation mapper */
976  rlocator.relNumber = RelationMapOidToFilenumber(relid,
977  relform->relisshared);
978  }
979  else
980  {
981  /* no storage, return NULL */
982  rlocator.relNumber = InvalidRelFileNumber;
983  /* some compilers generate warnings without these next two lines */
984  rlocator.dbOid = InvalidOid;
985  rlocator.spcOid = InvalidOid;
986  }
987 
988  if (!RelFileNumberIsValid(rlocator.relNumber))
989  {
990  ReleaseSysCache(tuple);
991  PG_RETURN_NULL();
992  }
993 
994  /* Determine owning backend. */
995  switch (relform->relpersistence)
996  {
997  case RELPERSISTENCE_UNLOGGED:
998  case RELPERSISTENCE_PERMANENT:
999  backend = InvalidBackendId;
1000  break;
1001  case RELPERSISTENCE_TEMP:
1002  if (isTempOrTempToastNamespace(relform->relnamespace))
1003  backend = BackendIdForTempRelations();
1004  else
1005  {
1006  /* Do it the hard way. */
1007  backend = GetTempNamespaceBackendId(relform->relnamespace);
1008  Assert(backend != InvalidBackendId);
1009  }
1010  break;
1011  default:
1012  elog(ERROR, "invalid relpersistence: %c", relform->relpersistence);
1013  backend = InvalidBackendId; /* placate compiler */
1014  break;
1015  }
1016 
1017  ReleaseSysCache(tuple);
1018 
1019  path = relpathbackend(rlocator, backend, MAIN_FORKNUM);
1020 
1022 }
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:5060
AclResult
Definition: acl.h:181
@ ACLCHECK_OK
Definition: acl.h:182
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2695
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3843
char * get_tablespace_name(Oid spc_oid)
Definition: tablespace.c:1478
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
Definition: tablespace.c:1432
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:2924
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4232
Datum numeric_int8(PG_FUNCTION_ARGS)
Definition: numeric.c:4435
Datum numeric_ge(PG_FUNCTION_ARGS)
Definition: numeric.c:2459
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:806
Datum numeric_div_trunc(PG_FUNCTION_ARGS)
Definition: numeric.c:3258
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:627
Datum numeric_lt(PG_FUNCTION_ARGS)
Definition: numeric.c:2474
Datum numeric_add(PG_FUNCTION_ARGS)
Definition: numeric.c:2847
Datum numeric_abs(PG_FUNCTION_ARGS)
Definition: numeric.c:1383
Datum numeric_mul(PG_FUNCTION_ARGS)
Definition: numeric.c:3002
int BackendId
Definition: backendid.h:21
#define BackendIdForTempRelations()
Definition: backendid.h:34
#define InvalidBackendId
Definition: backendid.h:23
#define NameStr(name)
Definition: c.h:735
unsigned int uint32
Definition: c.h:495
#define INT64_FORMAT
Definition: c.h:537
unsigned char uint8
Definition: c.h:493
#define OidIsValid(objectId)
Definition: c.h:764
#define i64abs(i)
Definition: c.h:1317
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3089
Oid get_database_oid(const char *dbname, bool missing_ok)
Definition: dbcommands.c:3042
static int64 calculate_total_relation_size(Relation rel)
Definition: dbsize.c:529
static bool numeric_is_less(Numeric a, Numeric b)
Definition: dbsize.c:616
Datum pg_indexes_size(PG_FUNCTION_ARGS)
Definition: dbsize.c:506
Datum pg_size_bytes(PG_FUNCTION_ARGS)
Definition: dbsize.c:710
static Numeric numeric_truncated_divide(Numeric n, int64 divisor)
Definition: dbsize.c:657
static const struct size_bytes_unit_alias size_bytes_aliases[]
Definition: dbsize.c:68
Datum pg_database_size_oid(PG_FUNCTION_ARGS)
Definition: dbsize.c:169
static char * numeric_to_cstring(Numeric n)
Definition: dbsize.c:608
Datum pg_total_relation_size(PG_FUNCTION_ARGS)
Definition: dbsize.c:548
Datum pg_tablespace_size_name(PG_FUNCTION_ARGS)
Definition: dbsize.c:287
static int64 calculate_tablespace_size(Oid tblspcOid)
Definition: dbsize.c:203
Datum pg_size_pretty_numeric(PG_FUNCTION_ARGS)
Definition: dbsize.c:669
Datum pg_relation_size(PG_FUNCTION_ARGS)
Definition: dbsize.c:347
static int64 calculate_indexes_size(Relation rel)
Definition: dbsize.c:452
static int64 calculate_relation_size(RelFileLocator *rfn, BackendId backend, ForkNumber forknum)
Definition: dbsize.c:309
static int64 db_dir_size(const char *path)
Definition: dbsize.c:75
Datum pg_database_size_name(PG_FUNCTION_ARGS)
Definition: dbsize.c:183
static Numeric numeric_absolute(Numeric n)
Definition: dbsize.c:625
#define half_rounded(x)
Definition: dbsize.c:36
Datum pg_size_pretty(PG_FUNCTION_ARGS)
Definition: dbsize.c:570
static int64 calculate_database_size(Oid dbOid)
Definition: dbsize.c:119
Datum pg_table_size(PG_FUNCTION_ARGS)
Definition: dbsize.c:487
Datum pg_tablespace_size_oid(PG_FUNCTION_ARGS)
Definition: dbsize.c:273
static Numeric numeric_half_rounded(Numeric n)
Definition: dbsize.c:635
Datum pg_relation_filenode(PG_FUNCTION_ARGS)
Definition: dbsize.c:876
Datum pg_filenode_relation(PG_FUNCTION_ARGS)
Definition: dbsize.c:924
static const struct size_pretty_unit size_pretty_units[]
Definition: dbsize.c:50
Datum pg_relation_filepath(PG_FUNCTION_ARGS)
Definition: dbsize.c:948
static int64 calculate_toast_table_size(Oid toastrelid)
Definition: dbsize.c:379
static int64 calculate_table_size(Relation rel)
Definition: dbsize.c:425
int errcode_for_file_access(void)
Definition: elog.c:881
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2879
int FreeDir(DIR *dir)
Definition: fd.c:2931
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2813
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:646
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Oid MyDatabaseTableSpace
Definition: globals.c:91
Oid MyDatabaseId
Definition: globals.c:89
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
int b
Definition: isn.c:70
int a
Definition: isn.c:69
Assert(fmt[strlen(fmt) - 1] !='\n')
void list_free(List *list)
Definition: list.c:1545
#define AccessShareLock
Definition: lockdefs.h:36
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
Oid GetUserId(void)
Definition: miscinit.c:508
bool isTempOrTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3640
int GetTempNamespaceBackendId(Oid namespaceId)
Definition: namespace.c:3733
static Numeric DatumGetNumeric(Datum X)
Definition: numeric.h:60
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:77
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:72
@ OBJECT_TABLESPACE
Definition: parsenodes.h:2138
@ OBJECT_DATABASE
Definition: parsenodes.h:2105
#define ACL_CONNECT
Definition: parsenodes.h:87
#define ACL_CREATE
Definition: parsenodes.h:85
void * arg
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:121
#define lfirst_oid(lc)
Definition: pg_list.h:174
static char * buf
Definition: pg_test_fsync.c:73
const char * dbName
Definition: pgbench.c:297
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define snprintf
Definition: port.h:238
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:385
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4773
Oid RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
RelFileNumber RelationMapOidToFilenumber(Oid relationId, bool shared)
Definition: relmapper.c:166
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:50
Oid RelFileNumber
Definition: relpath.h:25
ForkNumber
Definition: relpath.h:48
@ MAIN_FORKNUM
Definition: relpath.h:50
#define MAX_FORKNUM
Definition: relpath.h:62
#define InvalidRelFileNumber
Definition: relpath.h:26
#define relpathbackend(rlocator, backend, forknum)
Definition: relpath.h:85
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:33
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
Definition: dirent.c:26
Definition: pg_list.h:54
RelFileNumber relNumber
BackendId rd_backend
Definition: rel.h:60
RelFileLocator rd_locator
Definition: rel.h:57
Form_pg_class rd_rel
Definition: rel.h:111
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
Definition: c.h:730
const char * alias
Definition: dbsize.c:63
const char * name
Definition: dbsize.c:41
uint8 unitbits
Definition: dbsize.c:45
uint32 limit
Definition: dbsize.c:42
__int64 st_size
Definition: win32_port.h:273
unsigned short st_mode
Definition: win32_port.h:268
Definition: c.h:676
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:820
@ RELOID
Definition: syscache.h:89
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
char * text_to_cstring(const text *t)
Definition: varlena.c:217
text * cstring_to_text(const char *s)
Definition: varlena.c:184
const char * name
#define stat
Definition: win32_port.h:284
#define S_ISDIR(m)
Definition: win32_port.h:325