aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2006-11-06 03:06:41 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2006-11-06 03:06:41 +0000
commit74686b6de78d19aed6d9aa33a7bb5dfb06f3e018 (patch)
treeebabe8645bcfa7a1c84ba228aa25fc4731bb7cbe /src
parent62fe410ec6cd7811b0cf76c8e175c47daba108d9 (diff)
downloadpostgresql-74686b6de78d19aed6d9aa33a7bb5dfb06f3e018.tar.gz
postgresql-74686b6de78d19aed6d9aa33a7bb5dfb06f3e018.zip
Get rid of some unnecessary dependencies on DataDir: wherever possible,
the backend should rely on its working-directory setting instead. Also do some message-style police work in contrib/adminpack.
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/adt/dbsize.c49
-rw-r--r--src/backend/utils/adt/genfile.c22
2 files changed, 27 insertions, 44 deletions
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index 09c1be6b07e..fa8b13787e5 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -5,7 +5,7 @@
* Copyright (c) 2002-2006, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.8 2006/03/05 15:58:41 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.9 2006/11/06 03:06:41 tgl Exp $
*
*/
@@ -15,6 +15,7 @@
#include <sys/stat.h>
#include "access/heapam.h"
+#include "catalog/catalog.h"
#include "catalog/namespace.h"
#include "catalog/pg_tablespace.h"
#include "commands/dbcommands.h"
@@ -77,11 +78,11 @@ calculate_database_size(Oid dbOid)
/* Shared storage in pg_global is not counted */
/* Include pg_default storage */
- snprintf(pathname, MAXPGPATH, "%s/base/%u", DataDir, dbOid);
+ snprintf(pathname, MAXPGPATH, "base/%u", dbOid);
totalsize = db_dir_size(pathname);
/* Scan the non-default tablespaces */
- snprintf(dirpath, MAXPGPATH, "%s/pg_tblspc", DataDir);
+ snprintf(dirpath, MAXPGPATH, "pg_tblspc");
dirdesc = AllocateDir(dirpath);
if (!dirdesc)
ereport(ERROR,
@@ -95,8 +96,8 @@ calculate_database_size(Oid dbOid)
strcmp(direntry->d_name, "..") == 0)
continue;
- snprintf(pathname, MAXPGPATH, "%s/pg_tblspc/%s/%u",
- DataDir, direntry->d_name, dbOid);
+ snprintf(pathname, MAXPGPATH, "pg_tblspc/%s/%u",
+ direntry->d_name, dbOid);
totalsize += db_dir_size(pathname);
}
@@ -148,11 +149,11 @@ calculate_tablespace_size(Oid tblspcOid)
struct dirent *direntry;
if (tblspcOid == DEFAULTTABLESPACE_OID)
- snprintf(tblspcPath, MAXPGPATH, "%s/base", DataDir);
+ snprintf(tblspcPath, MAXPGPATH, "base");
else if (tblspcOid == GLOBALTABLESPACE_OID)
- snprintf(tblspcPath, MAXPGPATH, "%s/global", DataDir);
+ snprintf(tblspcPath, MAXPGPATH, "global");
else
- snprintf(tblspcPath, MAXPGPATH, "%s/pg_tblspc/%u", DataDir, tblspcOid);
+ snprintf(tblspcPath, MAXPGPATH, "pg_tblspc/%u", tblspcOid);
dirdesc = AllocateDir(tblspcPath);
@@ -219,30 +220,22 @@ static int64
calculate_relation_size(RelFileNode *rfn)
{
int64 totalsize = 0;
- char dirpath[MAXPGPATH];
+ char *relationpath;
char pathname[MAXPGPATH];
unsigned int segcount = 0;
- Assert(OidIsValid(rfn->spcNode));
-
- if (rfn->spcNode == DEFAULTTABLESPACE_OID)
- snprintf(dirpath, MAXPGPATH, "%s/base/%u", DataDir, rfn->dbNode);
- else if (rfn->spcNode == GLOBALTABLESPACE_OID)
- snprintf(dirpath, MAXPGPATH, "%s/global", DataDir);
- else
- snprintf(dirpath, MAXPGPATH, "%s/pg_tblspc/%u/%u",
- DataDir, rfn->spcNode, rfn->dbNode);
+ relationpath = relpath(*rfn);
for (segcount = 0;; segcount++)
{
struct stat fst;
if (segcount == 0)
- snprintf(pathname, MAXPGPATH, "%s/%u",
- dirpath, rfn->relNode);
+ snprintf(pathname, MAXPGPATH, "%s",
+ relationpath);
else
- snprintf(pathname, MAXPGPATH, "%s/%u.%u",
- dirpath, rfn->relNode, segcount);
+ snprintf(pathname, MAXPGPATH, "%s.%u",
+ relationpath, segcount);
if (stat(pathname, &fst) < 0)
{
@@ -296,8 +289,7 @@ pg_relation_size_name(PG_FUNCTION_ARGS)
/*
* Compute the on-disk size of files for the relation according to the
- * stat function, optionally including heap data, index data, and/or
- * toast data.
+ * stat function, including heap data, index data, and toast data.
*/
static int64
calculate_total_relation_size(Oid Relid)
@@ -313,10 +305,9 @@ calculate_total_relation_size(Oid Relid)
/* Get the heap size */
size = calculate_relation_size(&(heapRel->rd_node));
- /* Get index size */
+ /* Include any dependent indexes */
if (heapRel->rd_rel->relhasindex)
{
- /* recursively include any dependent indexes */
List *index_oids = RelationGetIndexList(heapRel);
foreach(cell, index_oids)
@@ -334,7 +325,7 @@ calculate_total_relation_size(Oid Relid)
list_free(index_oids);
}
- /* Get toast table (and index) size */
+ /* Recursively include toast table (and index) size */
if (OidIsValid(toastOid))
size += calculate_total_relation_size(toastOid);
@@ -343,10 +334,6 @@ calculate_total_relation_size(Oid Relid)
return size;
}
-/*
- * Compute on-disk size of files for 'relation' including
- * heap data, index data, and toasted data.
- */
Datum
pg_total_relation_size_oid(PG_FUNCTION_ARGS)
{
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index 19a03cee0a5..cee910f854c 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -9,7 +9,7 @@
* Author: Andreas Pflug <pgadmin@pse-consulting.de>
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.11 2006/07/13 16:49:16 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.12 2006/11/06 03:06:41 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -38,13 +38,13 @@ typedef struct
/*
- * Validate a path and convert to absolute form.
+ * Convert a "text" filename argument to C string, and check it's allowable.
*
- * Argument may be absolute or relative to the DataDir (but we only allow
- * absolute paths that match DataDir or Log_directory).
+ * Filename may be absolute or relative to the DataDir, but we only allow
+ * absolute paths that match DataDir or Log_directory.
*/
static char *
-check_and_make_absolute(text *arg)
+convert_and_check_filename(text *arg)
{
int input_len = VARSIZE(arg) - VARHDRSZ;
char *filename = palloc(input_len + 1);
@@ -77,11 +77,7 @@ check_and_make_absolute(text *arg)
}
else
{
- char *absname = palloc(strlen(DataDir) + strlen(filename) + 2);
-
- sprintf(absname, "%s/%s", DataDir, filename);
- pfree(filename);
- return absname;
+ return filename;
}
}
@@ -105,7 +101,7 @@ pg_read_file(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
(errmsg("must be superuser to read files"))));
- filename = check_and_make_absolute(filename_t);
+ filename = convert_and_check_filename(filename_t);
if ((file = AllocateFile(filename, PG_BINARY_R)) == NULL)
ereport(ERROR,
@@ -166,7 +162,7 @@ pg_stat_file(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
(errmsg("must be superuser to get file information"))));
- filename = check_and_make_absolute(filename_t);
+ filename = convert_and_check_filename(filename_t);
if (stat(filename, &fst) < 0)
ereport(ERROR,
@@ -238,7 +234,7 @@ pg_ls_dir(PG_FUNCTION_ARGS)
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
fctx = palloc(sizeof(directory_fctx));
- fctx->location = check_and_make_absolute(PG_GETARG_TEXT_P(0));
+ fctx->location = convert_and_check_filename(PG_GETARG_TEXT_P(0));
fctx->dirdesc = AllocateDir(fctx->location);