aboutsummaryrefslogtreecommitdiff
path: root/src/backend/storage
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2005-07-04 04:51:52 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2005-07-04 04:51:52 +0000
commiteb5949d190e80360386113fde0f05854f0c9824d (patch)
treef5683b4ff77c0b311ae975817b88c5ccc65ce5a9 /src/backend/storage
parent7504f0bae853b0b9fec03c8e361c8b1a4b1c3209 (diff)
downloadpostgresql-eb5949d190e80360386113fde0f05854f0c9824d.tar.gz
postgresql-eb5949d190e80360386113fde0f05854f0c9824d.zip
Arrange for the postmaster (and standalone backends, initdb, etc) to
chdir into PGDATA and subsequently use relative paths instead of absolute paths to access all files under PGDATA. This seems to give a small performance improvement, and it should make the system more robust against naive DBAs doing things like moving a database directory that has a live postmaster in it. Per recent discussion.
Diffstat (limited to 'src/backend/storage')
-rw-r--r--src/backend/storage/file/fd.c84
-rw-r--r--src/backend/storage/freespace/freespace.c36
-rw-r--r--src/backend/storage/smgr/md.c12
3 files changed, 52 insertions, 80 deletions
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index b91e667d0b3..11c7b807665 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.117 2005/06/19 21:34:02 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.118 2005/07/04 04:51:48 tgl Exp $
*
* NOTES:
*
@@ -224,8 +224,7 @@ static File AllocateVfd(void);
static void FreeVfd(File file);
static int FileAccess(File file);
-static File fileNameOpenFile(FileName fileName, int fileFlags, int fileMode);
-static char *filepath(const char *filename);
+static char *make_database_relative(const char *filename);
static void AtProcExit_Files(int code, Datum arg);
static void CleanupTempFiles(bool isProcExit);
static void RemovePgTempFilesInDir(const char *tmpdirname);
@@ -699,34 +698,20 @@ FreeVfd(File file)
VfdCache[0].nextFree = file;
}
-/* filepath()
- * Convert given pathname to absolute.
+/*
+ * make_database_relative()
+ * Prepend DatabasePath to the given file name.
*
* Result is a palloc'd string.
- *
- * (Generally, this isn't actually necessary, considering that we
- * should be cd'd into the database directory. Presently it is only
- * necessary to do it in "bootstrap" mode. Maybe we should change
- * bootstrap mode to do the cd, and save a few cycles/bytes here.)
*/
static char *
-filepath(const char *filename)
+make_database_relative(const char *filename)
{
char *buf;
- /* Not an absolute path name? Then fill in with database path... */
- if (!is_absolute_path(filename))
- {
- buf = (char *) palloc(strlen(DatabasePath) + strlen(filename) + 2);
- sprintf(buf, "%s/%s", DatabasePath, filename);
- }
- else
- buf = pstrdup(filename);
-
-#ifdef FILEDEBUG
- printf("filepath: path is %s\n", buf);
-#endif
-
+ Assert(!is_absolute_path(filename));
+ buf = (char *) palloc(strlen(DatabasePath) + strlen(filename) + 2);
+ sprintf(buf, "%s/%s", DatabasePath, filename);
return buf;
}
@@ -779,16 +764,21 @@ FileInvalidate(File file)
}
#endif
-static File
-fileNameOpenFile(FileName fileName,
- int fileFlags,
- int fileMode)
+/*
+ * open a file in an arbitrary directory
+ *
+ * NB: if the passed pathname is relative (which it usually is),
+ * it will be interpreted relative to the process' working directory
+ * (which should always be $PGDATA when this code is running).
+ */
+File
+PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
{
char *fnamecopy;
File file;
Vfd *vfdP;
- DO_DB(elog(LOG, "fileNameOpenFile: %s %x %o",
+ DO_DB(elog(LOG, "PathNameOpenFile: %s %x %o",
fileName, fileFlags, fileMode));
/*
@@ -818,7 +808,7 @@ fileNameOpenFile(FileName fileName,
return -1;
}
++nfile;
- DO_DB(elog(LOG, "fileNameOpenFile: success %d",
+ DO_DB(elog(LOG, "PathNameOpenFile: success %d",
vfdP->fd));
Insert(file);
@@ -834,7 +824,10 @@ fileNameOpenFile(FileName fileName,
}
/*
- * open a file in the database directory ($PGDATA/base/...)
+ * open a file in the database directory ($PGDATA/base/DIROID/)
+ *
+ * The passed name MUST be a relative path. Effectively, this
+ * prepends DatabasePath to it and then acts like PathNameOpenFile.
*/
File
FileNameOpenFile(FileName fileName, int fileFlags, int fileMode)
@@ -842,22 +835,13 @@ FileNameOpenFile(FileName fileName, int fileFlags, int fileMode)
File fd;
char *fname;
- fname = filepath(fileName);
- fd = fileNameOpenFile(fname, fileFlags, fileMode);
+ fname = make_database_relative(fileName);
+ fd = PathNameOpenFile(fname, fileFlags, fileMode);
pfree(fname);
return fd;
}
/*
- * open a file in an arbitrary directory
- */
-File
-PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
-{
- return fileNameOpenFile(fileName, fileFlags, fileMode);
-}
-
-/*
* Open a temporary file that will disappear when we close it.
*
* This routine takes care of generating an appropriate tempfile name.
@@ -903,7 +887,7 @@ OpenTemporaryFile(bool interXact)
* just did the same thing. If it doesn't work then we'll bomb
* out on the second create attempt, instead.
*/
- dirpath = filepath(PG_TEMP_FILES_DIR);
+ dirpath = make_database_relative(PG_TEMP_FILES_DIR);
mkdir(dirpath, S_IRWXU);
pfree(dirpath);
@@ -1568,7 +1552,6 @@ CleanupTempFiles(bool isProcExit)
void
RemovePgTempFiles(void)
{
- char db_path[MAXPGPATH];
char temp_path[MAXPGPATH];
DIR *db_dir;
struct dirent *db_de;
@@ -1577,17 +1560,16 @@ RemovePgTempFiles(void)
* Cycle through pgsql_tmp directories for all databases and remove old
* temp files.
*/
- snprintf(db_path, sizeof(db_path), "%s/base", DataDir);
- db_dir = AllocateDir(db_path);
+ db_dir = AllocateDir("base");
- while ((db_de = ReadDir(db_dir, db_path)) != NULL)
+ while ((db_de = ReadDir(db_dir, "base")) != NULL)
{
if (strcmp(db_de->d_name, ".") == 0 ||
strcmp(db_de->d_name, "..") == 0)
continue;
- snprintf(temp_path, sizeof(temp_path), "%s/%s/%s",
- db_path, db_de->d_name, PG_TEMP_FILES_DIR);
+ snprintf(temp_path, sizeof(temp_path), "base/%s/%s",
+ db_de->d_name, PG_TEMP_FILES_DIR);
RemovePgTempFilesInDir(temp_path);
}
@@ -1598,9 +1580,7 @@ RemovePgTempFiles(void)
* level of DataDir as well.
*/
#ifdef EXEC_BACKEND
- snprintf(temp_path, sizeof(temp_path), "%s/%s",
- DataDir, PG_TEMP_FILES_DIR);
- RemovePgTempFilesInDir(temp_path);
+ RemovePgTempFilesInDir(PG_TEMP_FILES_DIR);
#endif
}
diff --git a/src/backend/storage/freespace/freespace.c b/src/backend/storage/freespace/freespace.c
index 27e9952b3ff..355282db1a4 100644
--- a/src/backend/storage/freespace/freespace.c
+++ b/src/backend/storage/freespace/freespace.c
@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.45 2005/05/29 04:23:04 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.46 2005/07/04 04:51:48 tgl Exp $
*
*
* NOTES:
@@ -752,20 +752,16 @@ void
DumpFreeSpaceMap(int code, Datum arg)
{
FILE *fp;
- char cachefilename[MAXPGPATH];
FsmCacheFileHeader header;
FSMRelation *fsmrel;
/* Try to create file */
- snprintf(cachefilename, sizeof(cachefilename), "%s/%s",
- DataDir, FSM_CACHE_FILENAME);
+ unlink(FSM_CACHE_FILENAME); /* in case it exists w/wrong permissions */
- unlink(cachefilename); /* in case it exists w/wrong permissions */
-
- fp = AllocateFile(cachefilename, PG_BINARY_W);
+ fp = AllocateFile(FSM_CACHE_FILENAME, PG_BINARY_W);
if (fp == NULL)
{
- elog(LOG, "could not write \"%s\": %m", cachefilename);
+ elog(LOG, "could not write \"%s\": %m", FSM_CACHE_FILENAME);
return;
}
@@ -821,15 +817,15 @@ DumpFreeSpaceMap(int code, Datum arg)
if (FreeFile(fp))
{
- elog(LOG, "could not write \"%s\": %m", cachefilename);
+ elog(LOG, "could not write \"%s\": %m", FSM_CACHE_FILENAME);
/* Remove busted cache file */
- unlink(cachefilename);
+ unlink(FSM_CACHE_FILENAME);
}
return;
write_failed:
- elog(LOG, "could not write \"%s\": %m", cachefilename);
+ elog(LOG, "could not write \"%s\": %m", FSM_CACHE_FILENAME);
/* Clean up */
LWLockRelease(FreeSpaceLock);
@@ -837,7 +833,7 @@ write_failed:
FreeFile(fp);
/* Remove busted cache file */
- unlink(cachefilename);
+ unlink(FSM_CACHE_FILENAME);
}
/*
@@ -858,19 +854,15 @@ void
LoadFreeSpaceMap(void)
{
FILE *fp;
- char cachefilename[MAXPGPATH];
FsmCacheFileHeader header;
int relno;
/* Try to open file */
- snprintf(cachefilename, sizeof(cachefilename), "%s/%s",
- DataDir, FSM_CACHE_FILENAME);
-
- fp = AllocateFile(cachefilename, PG_BINARY_R);
+ fp = AllocateFile(FSM_CACHE_FILENAME, PG_BINARY_R);
if (fp == NULL)
{
if (errno != ENOENT)
- elog(LOG, "could not read \"%s\": %m", cachefilename);
+ elog(LOG, "could not read \"%s\": %m", FSM_CACHE_FILENAME);
return;
}
@@ -883,7 +875,7 @@ LoadFreeSpaceMap(void)
header.version != FSM_CACHE_VERSION ||
header.numRels < 0)
{
- elog(LOG, "bogus file header in \"%s\"", cachefilename);
+ elog(LOG, "bogus file header in \"%s\"", FSM_CACHE_FILENAME);
goto read_failed;
}
@@ -905,7 +897,7 @@ LoadFreeSpaceMap(void)
relheader.lastPageCount < 0 ||
relheader.storedPages < 0)
{
- elog(LOG, "bogus rel header in \"%s\"", cachefilename);
+ elog(LOG, "bogus rel header in \"%s\"", FSM_CACHE_FILENAME);
goto read_failed;
}
@@ -922,7 +914,7 @@ LoadFreeSpaceMap(void)
data = (char *) palloc(len);
if (fread(data, 1, len, fp) != len)
{
- elog(LOG, "premature EOF in \"%s\"", cachefilename);
+ elog(LOG, "premature EOF in \"%s\"", FSM_CACHE_FILENAME);
pfree(data);
goto read_failed;
}
@@ -993,7 +985,7 @@ read_failed:
FreeFile(fp);
/* Remove cache file before it can become stale; see notes above */
- unlink(cachefilename);
+ unlink(FSM_CACHE_FILENAME);
}
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index fa7913aff74..3a0a1f1262b 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/storage/smgr/md.c,v 1.116 2005/06/20 18:37:01 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/storage/smgr/md.c,v 1.117 2005/07/04 04:51:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -155,7 +155,7 @@ mdcreate(SMgrRelation reln, bool isRedo)
path = relpath(reln->smgr_rnode);
- fd = FileNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, 0600);
+ fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, 0600);
if (fd < 0)
{
@@ -169,7 +169,7 @@ mdcreate(SMgrRelation reln, bool isRedo)
* mdopen)
*/
if (isRedo || IsBootstrapProcessingMode())
- fd = FileNameOpenFile(path, O_RDWR | PG_BINARY, 0600);
+ fd = PathNameOpenFile(path, O_RDWR | PG_BINARY, 0600);
if (fd < 0)
{
pfree(path);
@@ -340,7 +340,7 @@ mdopen(SMgrRelation reln, bool allowNotFound)
path = relpath(reln->smgr_rnode);
- fd = FileNameOpenFile(path, O_RDWR | PG_BINARY, 0600);
+ fd = PathNameOpenFile(path, O_RDWR | PG_BINARY, 0600);
if (fd < 0)
{
@@ -352,7 +352,7 @@ mdopen(SMgrRelation reln, bool allowNotFound)
* (See mdcreate)
*/
if (IsBootstrapProcessingMode())
- fd = FileNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, 0600);
+ fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, 0600);
if (fd < 0)
{
pfree(path);
@@ -879,7 +879,7 @@ _mdfd_openseg(SMgrRelation reln, BlockNumber segno, int oflags)
fullpath = path;
/* open the file */
- fd = FileNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags, 0600);
+ fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags, 0600);
pfree(fullpath);