Commit 22817041 authored by Bruce Momjian's avatar Bruce Momjian

Please tablespace directories in their own subdirectory so pg_migrator

can upgrade clusters without renaming the tablespace directories.  New
directory structure format is, e.g.:

	$PGDATA/pg_tblspc/20981/PG_8.5_201001061/719849/83292814
parent 894fc6b7
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/catalog/catalog.c,v 1.86 2010/01/06 02:41:37 momjian Exp $ * $PostgreSQL: pgsql/src/backend/catalog/catalog.c,v 1.87 2010/01/12 02:42:51 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -115,16 +115,17 @@ relpath(RelFileNode rnode, ForkNumber forknum) ...@@ -115,16 +115,17 @@ relpath(RelFileNode rnode, ForkNumber forknum)
else else
{ {
/* All other tablespaces are accessed via symlinks */ /* All other tablespaces are accessed via symlinks */
pathlen = 10 + OIDCHARS + 1 + OIDCHARS + 1 + OIDCHARS + 1 pathlen = 9 + 1 + OIDCHARS + 1 + strlen(TABLESPACE_VERSION_DIRECTORY) +
+ FORKNAMECHARS + 1; 1 + OIDCHARS + 1 + OIDCHARS + 1 + FORKNAMECHARS + 1;
path = (char *) palloc(pathlen); path = (char *) palloc(pathlen);
if (forknum != MAIN_FORKNUM) if (forknum != MAIN_FORKNUM)
snprintf(path, pathlen, "pg_tblspc/%u/%u/%u_%s", snprintf(path, pathlen, "pg_tblspc/%u/%s/%u/%u_%s",
rnode.spcNode, rnode.dbNode, rnode.relNode, rnode.spcNode, TABLESPACE_VERSION_DIRECTORY,
forkNames[forknum]); rnode.dbNode, rnode.relNode, forkNames[forknum]);
else else
snprintf(path, pathlen, "pg_tblspc/%u/%u/%u", snprintf(path, pathlen, "pg_tblspc/%u/%s/%u/%u",
rnode.spcNode, rnode.dbNode, rnode.relNode); rnode.spcNode, TABLESPACE_VERSION_DIRECTORY,
rnode.dbNode, rnode.relNode);
} }
return path; return path;
} }
...@@ -161,10 +162,11 @@ GetDatabasePath(Oid dbNode, Oid spcNode) ...@@ -161,10 +162,11 @@ GetDatabasePath(Oid dbNode, Oid spcNode)
else else
{ {
/* All other tablespaces are accessed via symlinks */ /* All other tablespaces are accessed via symlinks */
pathlen = 10 + OIDCHARS + 1 + OIDCHARS + 1; pathlen = 9 + 1 + OIDCHARS + 1 + strlen(TABLESPACE_VERSION_DIRECTORY) +
1 + OIDCHARS + 1;
path = (char *) palloc(pathlen); path = (char *) palloc(pathlen);
snprintf(path, pathlen, "pg_tblspc/%u/%u", snprintf(path, pathlen, "pg_tblspc/%u/%s/%u",
spcNode, dbNode); spcNode, TABLESPACE_VERSION_DIRECTORY, dbNode);
} }
return path; return path;
} }
......
...@@ -15,8 +15,11 @@ ...@@ -15,8 +15,11 @@
* To support file access via the information given in RelFileNode, we * To support file access via the information given in RelFileNode, we
* maintain a symbolic-link map in $PGDATA/pg_tblspc. The symlinks are * maintain a symbolic-link map in $PGDATA/pg_tblspc. The symlinks are
* named by tablespace OIDs and point to the actual tablespace directories. * named by tablespace OIDs and point to the actual tablespace directories.
* There is also a per-cluster version directory in each tablespace.
* Thus the full path to an arbitrary file is * Thus the full path to an arbitrary file is
* $PGDATA/pg_tblspc/spcoid/dboid/relfilenode * $PGDATA/pg_tblspc/spcoid/PG_MAJORVER_CATVER/dboid/relfilenode
* e.g.
* $PGDATA/pg_tblspc/20981/PG_8.5_201001061/719849/83292814
* *
* There are two tablespaces created at initdb time: pg_global (for shared * There are two tablespaces created at initdb time: pg_global (for shared
* tables) and pg_default (for everything else). For backwards compatibility * tables) and pg_default (for everything else). For backwards compatibility
...@@ -37,7 +40,7 @@ ...@@ -37,7 +40,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.70 2010/01/07 04:10:39 momjian Exp $ * $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.71 2010/01/12 02:42:51 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -81,8 +84,9 @@ char *default_tablespace = NULL; ...@@ -81,8 +84,9 @@ char *default_tablespace = NULL;
char *temp_tablespaces = NULL; char *temp_tablespaces = NULL;
static bool remove_tablespace_directories(Oid tablespaceoid, bool redo); static void create_tablespace_directories(const char *location,
static void write_version_file(const char *path); const Oid tablespaceoid);
static bool destroy_tablespace_directories(Oid tablespaceoid, bool redo);
/* /*
...@@ -146,18 +150,36 @@ TablespaceCreateDbspace(Oid spcNode, Oid dbNode, bool isRedo) ...@@ -146,18 +150,36 @@ TablespaceCreateDbspace(Oid spcNode, Oid dbNode, bool isRedo)
{ {
char *parentdir; char *parentdir;
/* Failure other than not exists? */ /* Failure other than not exists or not in WAL replay? */
if (errno != ENOENT || !isRedo) if (errno != ENOENT || !isRedo)
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not create directory \"%s\": %m", errmsg("could not create directory \"%s\": %m",
dir))); dir)));
/* Parent directory must be missing */ /*
* Parent directories are missing during WAL replay, so
* continue by creating simple parent directories
* rather than a symlink.
*/
/* create two parents up if not exist */
parentdir = pstrdup(dir); parentdir = pstrdup(dir);
get_parent_directory(parentdir); get_parent_directory(parentdir);
/* Can't create parent either? */ get_parent_directory(parentdir);
if (mkdir(parentdir, S_IRWXU) < 0) /* Can't create parent and it doesn't already exist? */
if (mkdir(parentdir, S_IRWXU) < 0 && errno != EEXIST)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create directory \"%s\": %m",
parentdir)));
pfree(parentdir);
/* create one parent up if not exist */
parentdir = pstrdup(dir);
get_parent_directory(parentdir);
/* Can't create parent and it doesn't already exist? */
if (mkdir(parentdir, S_IRWXU) < 0 && errno != EEXIST)
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not create directory \"%s\": %m", errmsg("could not create directory \"%s\": %m",
...@@ -212,7 +234,6 @@ CreateTableSpace(CreateTableSpaceStmt *stmt) ...@@ -212,7 +234,6 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
HeapTuple tuple; HeapTuple tuple;
Oid tablespaceoid; Oid tablespaceoid;
char *location; char *location;
char *linkloc;
Oid ownerId; Oid ownerId;
/* Must be super user */ /* Must be super user */
...@@ -251,10 +272,11 @@ CreateTableSpace(CreateTableSpaceStmt *stmt) ...@@ -251,10 +272,11 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
/* /*
* Check that location isn't too long. Remember that we're going to append * Check that location isn't too long. Remember that we're going to append
* '/<dboid>/<relid>.<nnn>' (XXX but do we ever form the whole path * 'PG_XXX/<dboid>/<relid>.<nnn>'. FYI, we never actually reference the
* explicitly? This may be overly conservative.) * whole path, but mkdir() uses the first two parts.
*/ */
if (strlen(location) >= MAXPGPATH - 1 - OIDCHARS - 1 - OIDCHARS - 1 - OIDCHARS) if (strlen(location) + 1 + strlen(TABLESPACE_VERSION_DIRECTORY) + 1 +
OIDCHARS + 1 + OIDCHARS + 1 + OIDCHARS > MAXPGPATH)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("tablespace location \"%s\" is too long", errmsg("tablespace location \"%s\" is too long",
...@@ -311,45 +333,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt) ...@@ -311,45 +333,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
/* Record dependency on owner */ /* Record dependency on owner */
recordDependencyOnOwner(TableSpaceRelationId, tablespaceoid, ownerId); recordDependencyOnOwner(TableSpaceRelationId, tablespaceoid, ownerId);
/* create_tablespace_directories(location, tablespaceoid);
* Attempt to coerce target directory to safe permissions. If this fails,
* it doesn't exist or has the wrong owner.
*/
if (chmod(location, 0700) != 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not set permissions on directory \"%s\": %m",
location)));
/*
* Check the target directory is empty.
*/
if (!directory_is_empty(location))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("directory \"%s\" is not empty",
location)));
/*
* Create the PG_VERSION file in the target directory. This has several
* purposes: to make sure we can write in the directory, to prevent
* someone from creating another tablespace pointing at the same directory
* (the emptiness check above will fail), and to label tablespace
* directories by PG version.
*/
write_version_file(location);
/*
* All seems well, create the symlink
*/
linkloc = (char *) palloc(OIDCHARS + OIDCHARS + 1);
sprintf(linkloc, "pg_tblspc/%u", tablespaceoid);
if (symlink(location, linkloc) < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create symbolic link \"%s\": %m",
linkloc)));
/* Record the filesystem change in XLOG */ /* Record the filesystem change in XLOG */
{ {
...@@ -378,7 +362,6 @@ CreateTableSpace(CreateTableSpaceStmt *stmt) ...@@ -378,7 +362,6 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
*/ */
ForceSyncCommit(); ForceSyncCommit();
pfree(linkloc);
pfree(location); pfree(location);
/* We keep the lock on pg_tablespace until commit */ /* We keep the lock on pg_tablespace until commit */
...@@ -478,7 +461,7 @@ DropTableSpace(DropTableSpaceStmt *stmt) ...@@ -478,7 +461,7 @@ DropTableSpace(DropTableSpaceStmt *stmt)
/* /*
* Try to remove the physical infrastructure. * Try to remove the physical infrastructure.
*/ */
if (!remove_tablespace_directories(tablespaceoid, false)) if (!destroy_tablespace_directories(tablespaceoid, false))
{ {
/* /*
* Not all files deleted? However, there can be lingering empty files * Not all files deleted? However, there can be lingering empty files
...@@ -490,7 +473,7 @@ DropTableSpace(DropTableSpaceStmt *stmt) ...@@ -490,7 +473,7 @@ DropTableSpace(DropTableSpaceStmt *stmt)
* out any lingering files, and try again. * out any lingering files, and try again.
*/ */
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT); RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT);
if (!remove_tablespace_directories(tablespaceoid, false)) if (!destroy_tablespace_directories(tablespaceoid, false))
{ {
/* Still not empty, the files must be important then */ /* Still not empty, the files must be important then */
ereport(ERROR, ereport(ERROR,
...@@ -542,24 +525,97 @@ DropTableSpace(DropTableSpaceStmt *stmt) ...@@ -542,24 +525,97 @@ DropTableSpace(DropTableSpaceStmt *stmt)
#endif /* HAVE_SYMLINK */ #endif /* HAVE_SYMLINK */
} }
/* /*
* remove_tablespace_directories: attempt to remove filesystem infrastructure * create_tablespace_directories
* *
* Returns TRUE if successful, FALSE if some subdirectory is not empty * Attempt to create filesystem infrastructure linking $PGDATA/pg_tblspc/
* to the specified directory
*/
static void
create_tablespace_directories(const char *location, const Oid tablespaceoid)
{
char *linkloc = palloc(OIDCHARS + OIDCHARS + 1);
char *location_with_version_dir = palloc(strlen(location) + 1 +
strlen(TABLESPACE_VERSION_DIRECTORY) + 1);
sprintf(linkloc, "pg_tblspc/%u", tablespaceoid);
sprintf(location_with_version_dir, "%s/%s", location,
TABLESPACE_VERSION_DIRECTORY);
/*
* Attempt to coerce target directory to safe permissions. If this fails,
* it doesn't exist or has the wrong owner.
*/
if (chmod(location, 0700) != 0)
{
if (errno == ENOENT)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_FILE),
errmsg("directory \"%s\" does not exist",
location)));
else
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not set permissions on directory \"%s\": %m",
location)));
}
/*
* The creation of the version directory prevents more than one
* tablespace in a single location.
*/
if (mkdir(location_with_version_dir, S_IRWXU) < 0)
{
if (errno == EEXIST)
ereport(ERROR,
(errcode(ERRCODE_OBJECT_IN_USE),
errmsg("directory \"%s\" already in use as a tablespace",
location_with_version_dir)));
else
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create directory \"%s\": %m",
location_with_version_dir)));
}
/*
* Create the symlink under PGDATA
*/
if (symlink(location, linkloc) < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create symbolic link \"%s\": %m",
linkloc)));
pfree(linkloc);
pfree(location_with_version_dir);
}
/*
* destroy_tablespace_directories
*
* Attempt to remove filesystem infrastructure
* *
* redo indicates we are redoing a drop from XLOG; okay if nothing there * 'redo' indicates we are redoing a drop from XLOG; okay if nothing there
*
* Returns TRUE if successful, FALSE if some subdirectory is not empty
*/ */
static bool static bool
remove_tablespace_directories(Oid tablespaceoid, bool redo) destroy_tablespace_directories(Oid tablespaceoid, bool redo)
{ {
char *location; char *linkloc;
char *linkloc_with_version_dir;
DIR *dirdesc; DIR *dirdesc;
struct dirent *de; struct dirent *de;
char *subfile; char *subfile;
struct stat st; struct stat st;
location = (char *) palloc(OIDCHARS + OIDCHARS + 1); linkloc_with_version_dir = palloc(9 + 1 + OIDCHARS + 1 +
sprintf(location, "pg_tblspc/%u", tablespaceoid); strlen(TABLESPACE_VERSION_DIRECTORY));
sprintf(linkloc_with_version_dir, "pg_tblspc/%u/%s", tablespaceoid,
TABLESPACE_VERSION_DIRECTORY);
/* /*
* Check if the tablespace still contains any files. We try to rmdir each * Check if the tablespace still contains any files. We try to rmdir each
...@@ -582,7 +638,7 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo) ...@@ -582,7 +638,7 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo)
* and symlink. We want to allow a new DROP attempt to succeed at * and symlink. We want to allow a new DROP attempt to succeed at
* removing the catalog entries, so we should not give a hard error here. * removing the catalog entries, so we should not give a hard error here.
*/ */
dirdesc = AllocateDir(location); dirdesc = AllocateDir(linkloc_with_version_dir);
if (dirdesc == NULL) if (dirdesc == NULL)
{ {
if (errno == ENOENT) if (errno == ENOENT)
...@@ -591,32 +647,32 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo) ...@@ -591,32 +647,32 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo)
ereport(WARNING, ereport(WARNING,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not open directory \"%s\": %m", errmsg("could not open directory \"%s\": %m",
location))); linkloc_with_version_dir)));
pfree(location); pfree(linkloc_with_version_dir);
return true; return true;
} }
/* else let ReadDir report the error */ /* else let ReadDir report the error */
} }
while ((de = ReadDir(dirdesc, location)) != NULL) while ((de = ReadDir(dirdesc, linkloc_with_version_dir)) != NULL)
{ {
/* Note we ignore PG_VERSION for the nonce */
if (strcmp(de->d_name, ".") == 0 || if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0 || strcmp(de->d_name, "..") == 0)
strcmp(de->d_name, "PG_VERSION") == 0)
continue; continue;
subfile = palloc(strlen(location) + 1 + strlen(de->d_name) + 1); subfile = palloc(strlen(linkloc_with_version_dir) + 1 + strlen(de->d_name) + 1);
sprintf(subfile, "%s/%s", location, de->d_name); sprintf(subfile, "%s/%s", linkloc_with_version_dir, de->d_name);
/* This check is just to deliver a friendlier error message */ /* This check is just to deliver a friendlier error message */
if (!directory_is_empty(subfile)) if (!directory_is_empty(subfile))
{ {
FreeDir(dirdesc); FreeDir(dirdesc);
pfree(subfile);
pfree(linkloc_with_version_dir);
return false; return false;
} }
/* Do the real deed */ /* remove empty directory */
if (rmdir(subfile) < 0) if (rmdir(subfile) < 0)
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
...@@ -628,79 +684,44 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo) ...@@ -628,79 +684,44 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo)
FreeDir(dirdesc); FreeDir(dirdesc);
/* /* remove version directory */
* Okay, try to unlink PG_VERSION (we allow it to not be there, even in if (rmdir(linkloc_with_version_dir) < 0)
* non-REDO case, for robustness).
*/
subfile = palloc(strlen(location) + 11 + 1);
sprintf(subfile, "%s/PG_VERSION", location);
if (unlink(subfile) < 0)
{
if (errno != ENOENT)
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not remove file \"%s\": %m", errmsg("could not remove directory \"%s\": %m",
subfile))); linkloc_with_version_dir)));
}
pfree(subfile);
/* /*
* Okay, try to remove the symlink. We must however deal with the * Try to remove the symlink. We must however deal with the
* possibility that it's a directory instead of a symlink --- this could * possibility that it's a directory instead of a symlink --- this could
* happen during WAL replay (see TablespaceCreateDbspace), and it is also * happen during WAL replay (see TablespaceCreateDbspace), and it is also
* the normal case on Windows. * the case on Windows where junction points lstat() as directories.
*/ */
if (lstat(location, &st) == 0 && S_ISDIR(st.st_mode)) linkloc = pstrdup(linkloc_with_version_dir);
get_parent_directory(linkloc);
if (lstat(linkloc, &st) == 0 && S_ISDIR(st.st_mode))
{ {
if (rmdir(location) < 0) if (rmdir(linkloc) < 0)
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not remove directory \"%s\": %m", errmsg("could not remove directory \"%s\": %m",
location))); linkloc)));
} }
else else
{ {
if (unlink(location) < 0) if (unlink(linkloc) < 0)
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not remove symbolic link \"%s\": %m", errmsg("could not remove symbolic link \"%s\": %m",
location))); linkloc)));
} }
pfree(location); pfree(linkloc_with_version_dir);
pfree(linkloc);
return true; return true;
} }
/*
* write out the PG_VERSION file in the specified directory
*/
static void
write_version_file(const char *path)
{
char *fullname;
FILE *version_file;
/* Now write the file */
fullname = palloc(strlen(path) + 11 + 1);
sprintf(fullname, "%s/PG_VERSION", path);
if ((version_file = AllocateFile(fullname, PG_BINARY_W)) == NULL)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write to file \"%s\": %m",
fullname)));
fprintf(version_file, "%s\n", PG_MAJORVERSION);
if (FreeFile(version_file))
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write to file \"%s\": %m",
fullname)));
pfree(fullname);
}
/* /*
* Check if a directory is empty. * Check if a directory is empty.
...@@ -728,6 +749,7 @@ directory_is_empty(const char *path) ...@@ -728,6 +749,7 @@ directory_is_empty(const char *path)
return true; return true;
} }
/* /*
* Rename a tablespace * Rename a tablespace
*/ */
...@@ -1336,35 +1358,8 @@ tblspc_redo(XLogRecPtr lsn, XLogRecord *record) ...@@ -1336,35 +1358,8 @@ tblspc_redo(XLogRecPtr lsn, XLogRecord *record)
{ {
xl_tblspc_create_rec *xlrec = (xl_tblspc_create_rec *) XLogRecGetData(record); xl_tblspc_create_rec *xlrec = (xl_tblspc_create_rec *) XLogRecGetData(record);
char *location = xlrec->ts_path; char *location = xlrec->ts_path;
char *linkloc;
/*
* Attempt to coerce target directory to safe permissions. If this
* fails, it doesn't exist or has the wrong owner.
*/
if (chmod(location, 0700) != 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not set permissions on directory \"%s\": %m",
location)));
/* Create or re-create the PG_VERSION file in the target directory */
write_version_file(location);
/* Create the symlink if not already present */
linkloc = (char *) palloc(OIDCHARS + OIDCHARS + 1);
sprintf(linkloc, "pg_tblspc/%u", xlrec->ts_id);
if (symlink(location, linkloc) < 0) create_tablespace_directories(location, xlrec->ts_id);
{
if (errno != EEXIST)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create symbolic link \"%s\": %m",
linkloc)));
}
pfree(linkloc);
} }
else if (info == XLOG_TBLSPC_DROP) else if (info == XLOG_TBLSPC_DROP)
{ {
...@@ -1380,7 +1375,7 @@ tblspc_redo(XLogRecPtr lsn, XLogRecord *record) ...@@ -1380,7 +1375,7 @@ tblspc_redo(XLogRecPtr lsn, XLogRecord *record)
* remove all files then do conflict processing and try again, * remove all files then do conflict processing and try again,
* if currently enabled. * if currently enabled.
*/ */
if (!remove_tablespace_directories(xlrec->ts_id, true)) if (!destroy_tablespace_directories(xlrec->ts_id, true))
{ {
VirtualTransactionId *temp_file_users; VirtualTransactionId *temp_file_users;
...@@ -1416,7 +1411,7 @@ tblspc_redo(XLogRecPtr lsn, XLogRecord *record) ...@@ -1416,7 +1411,7 @@ tblspc_redo(XLogRecPtr lsn, XLogRecord *record)
* exited by now. So lets recheck before we throw an error. * exited by now. So lets recheck before we throw an error.
* If !process_conflicts then this will just fail again. * If !process_conflicts then this will just fail again.
*/ */
if (!remove_tablespace_directories(xlrec->ts_id, true)) if (!destroy_tablespace_directories(xlrec->ts_id, true))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("tablespace %u is not empty", errmsg("tablespace %u is not empty",
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.152 2010/01/02 16:57:51 momjian Exp $ * $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.153 2010/01/12 02:42:52 momjian Exp $
* *
* NOTES: * NOTES:
* *
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
#include "miscadmin.h" #include "miscadmin.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/pg_tablespace.h" #include "catalog/pg_tablespace.h"
#include "storage/fd.h" #include "storage/fd.h"
#include "storage/ipc.h" #include "storage/ipc.h"
...@@ -963,8 +964,8 @@ OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError) ...@@ -963,8 +964,8 @@ OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError)
else else
{ {
/* All other tablespaces are accessed via symlinks */ /* All other tablespaces are accessed via symlinks */
snprintf(tempdirpath, sizeof(tempdirpath), "pg_tblspc/%u/%s", snprintf(tempdirpath, sizeof(tempdirpath), "pg_tblspc/%u/%s/%s",
tblspcOid, PG_TEMP_FILES_DIR); tblspcOid, TABLESPACE_VERSION_DIRECTORY, PG_TEMP_FILES_DIR);
} }
/* /*
...@@ -1841,8 +1842,8 @@ RemovePgTempFiles(void) ...@@ -1841,8 +1842,8 @@ RemovePgTempFiles(void)
strcmp(spc_de->d_name, "..") == 0) strcmp(spc_de->d_name, "..") == 0)
continue; continue;
snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s", snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s/%s",
spc_de->d_name, PG_TEMP_FILES_DIR); spc_de->d_name, TABLESPACE_VERSION_DIRECTORY, PG_TEMP_FILES_DIR);
RemovePgTempFilesInDir(temp_path); RemovePgTempFilesInDir(temp_path);
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* Copyright (c) 2002-2010, PostgreSQL Global Development Group * Copyright (c) 2002-2010, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.25 2010/01/02 16:57:53 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.26 2010/01/12 02:42:52 momjian Exp $
* *
*/ */
...@@ -108,8 +108,8 @@ calculate_database_size(Oid dbOid) ...@@ -108,8 +108,8 @@ calculate_database_size(Oid dbOid)
strcmp(direntry->d_name, "..") == 0) strcmp(direntry->d_name, "..") == 0)
continue; continue;
snprintf(pathname, MAXPGPATH, "pg_tblspc/%s/%u", snprintf(pathname, MAXPGPATH, "pg_tblspc/%s/%s/%u",
direntry->d_name, dbOid); direntry->d_name, TABLESPACE_VERSION_DIRECTORY, dbOid);
totalsize += db_dir_size(pathname); totalsize += db_dir_size(pathname);
} }
...@@ -179,7 +179,8 @@ calculate_tablespace_size(Oid tblspcOid) ...@@ -179,7 +179,8 @@ calculate_tablespace_size(Oid tblspcOid)
else if (tblspcOid == GLOBALTABLESPACE_OID) else if (tblspcOid == GLOBALTABLESPACE_OID)
snprintf(tblspcPath, MAXPGPATH, "global"); snprintf(tblspcPath, MAXPGPATH, "global");
else else
snprintf(tblspcPath, MAXPGPATH, "pg_tblspc/%u", tblspcOid); snprintf(tblspcPath, MAXPGPATH, "pg_tblspc/%u/%s", tblspcOid,
TABLESPACE_VERSION_DIRECTORY);
dirdesc = AllocateDir(tblspcPath); dirdesc = AllocateDir(tblspcPath);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.73 2010/01/02 16:57:54 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.74 2010/01/12 02:42:52 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <math.h> #include <math.h>
#include "access/xact.h" #include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "catalog/pg_tablespace.h" #include "catalog/pg_tablespace.h"
#include "commands/dbcommands.h" #include "commands/dbcommands.h"
...@@ -185,7 +186,8 @@ pg_tablespace_databases(PG_FUNCTION_ARGS) ...@@ -185,7 +186,8 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
/* /*
* size = tablespace dirname length + dir sep char + oid + terminator * size = tablespace dirname length + dir sep char + oid + terminator
*/ */
fctx->location = (char *) palloc(10 + 10 + 1); fctx->location = (char *) palloc(9 + 1 + OIDCHARS + 1 +
strlen(TABLESPACE_VERSION_DIRECTORY) + 1);
if (tablespaceOid == GLOBALTABLESPACE_OID) if (tablespaceOid == GLOBALTABLESPACE_OID)
{ {
fctx->dirdesc = NULL; fctx->dirdesc = NULL;
...@@ -197,7 +199,8 @@ pg_tablespace_databases(PG_FUNCTION_ARGS) ...@@ -197,7 +199,8 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
if (tablespaceOid == DEFAULTTABLESPACE_OID) if (tablespaceOid == DEFAULTTABLESPACE_OID)
sprintf(fctx->location, "base"); sprintf(fctx->location, "base");
else else
sprintf(fctx->location, "pg_tblspc/%u", tablespaceOid); sprintf(fctx->location, "pg_tblspc/%u/%s", tablespaceOid,
TABLESPACE_VERSION_DIRECTORY);
fctx->dirdesc = AllocateDir(fctx->location); fctx->dirdesc = AllocateDir(fctx->location);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.297 2010/01/07 20:39:45 rhaas Exp $ * $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.298 2010/01/12 02:42:52 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -4302,8 +4302,8 @@ RelationCacheInitFileRemove(void) ...@@ -4302,8 +4302,8 @@ RelationCacheInitFileRemove(void)
if (strspn(de->d_name, "0123456789") == strlen(de->d_name)) if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
{ {
/* Scan the tablespace dir for per-database dirs */ /* Scan the tablespace dir for per-database dirs */
snprintf(path, sizeof(path), "%s/%s", snprintf(path, sizeof(path), "%s/%s/%s",
tblspcdir, de->d_name); tblspcdir, de->d_name, TABLESPACE_VERSION_DIRECTORY);
RelationCacheInitFileRemoveInDir(path); RelationCacheInitFileRemoveInDir(path);
} }
} }
......
...@@ -7,18 +7,21 @@ ...@@ -7,18 +7,21 @@
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/catalog/catalog.h,v 1.46 2010/01/06 02:41:37 momjian Exp $ * $PostgreSQL: pgsql/src/include/catalog/catalog.h,v 1.47 2010/01/12 02:42:52 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#ifndef CATALOG_H #ifndef CATALOG_H
#define CATALOG_H #define CATALOG_H
#include "catalog/catversion.h"
#include "catalog/pg_class.h" #include "catalog/pg_class.h"
#include "storage/relfilenode.h" #include "storage/relfilenode.h"
#include "utils/relcache.h" #include "utils/relcache.h"
#define OIDCHARS 10 /* max chars printed by %u */ #define OIDCHARS 10 /* max chars printed by %u */
#define TABLESPACE_VERSION_DIRECTORY "PG_" PG_MAJORVERSION "_" \
CppAsString2(CATALOG_VERSION_NO)
extern const char *forkNames[]; extern const char *forkNames[];
extern ForkNumber forkname_to_number(char *forkName); extern ForkNumber forkname_to_number(char *forkName);
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.570 2010/01/07 20:17:43 tgl Exp $ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.571 2010/01/12 02:42:52 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -53,6 +53,6 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 201001071 #define CATALOG_VERSION_NO 201001111
#endif #endif
...@@ -65,7 +65,7 @@ SELECT COUNT(*) FROM testschema.atable; -- checks heap ...@@ -65,7 +65,7 @@ SELECT COUNT(*) FROM testschema.atable; -- checks heap
-- Will fail with bad path -- Will fail with bad path
CREATE TABLESPACE badspace LOCATION '/no/such/location'; CREATE TABLESPACE badspace LOCATION '/no/such/location';
ERROR: could not set permissions on directory "/no/such/location": No such file or directory ERROR: directory "/no/such/location" does not exist
-- No such tablespace -- No such tablespace
CREATE TABLE bar (i int) TABLESPACE nosuchspace; CREATE TABLE bar (i int) TABLESPACE nosuchspace;
ERROR: tablespace "nosuchspace" does not exist ERROR: tablespace "nosuchspace" does not exist
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment