Commit 7d1f2f8a authored by Thomas G. Lockhart's avatar Thomas G. Lockhart

Support alternate database locations.

parent d98f2f99
...@@ -7,13 +7,14 @@ ...@@ -7,13 +7,14 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.9 1997/09/08 21:46:07 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.10 1997/11/07 06:37:55 thomas Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <sys/stat.h>
#include "postgres.h" #include "postgres.h"
#include "miscadmin.h" /* for DataDir */ #include "miscadmin.h" /* for DataDir */
...@@ -36,41 +37,58 @@ ...@@ -36,41 +37,58 @@
/* non-export function prototypes */ /* non-export function prototypes */
static void static void
check_permissions(char *command, char *dbname, check_permissions(char *command, char *dbpath, char *dbname,
Oid *dbIdP, Oid *userIdP); Oid *dbIdP, Oid *userIdP);
static HeapTuple get_pg_dbtup(char *command, char *dbname, Relation dbrel); static HeapTuple get_pg_dbtup(char *command, char *dbname, Relation dbrel);
static void stop_vacuum(char *dbname); static void stop_vacuum(char *dbpath, char *dbname);
void void
createdb(char *dbname) createdb(char *dbname, char *dbpath)
{ {
Oid db_id, Oid db_id,
user_id; user_id;
char buf[512]; char buf[512];
char *lp,
loc[512];
/* /*
* If this call returns, the database does not exist and we're allowed * If this call returns, the database does not exist and we're allowed
* to create databases. * to create databases.
*/ */
check_permissions("createdb", dbname, &db_id, &user_id); check_permissions("createdb", dbpath, dbname, &db_id, &user_id);
/* close virtual file descriptors so we can do system() calls */ /* close virtual file descriptors so we can do system() calls */
closeAllVfds(); closeAllVfds();
sprintf(buf, "mkdir %s%cbase%c%s", DataDir, SEP_CHAR, SEP_CHAR, dbname); /* Now create directory for this new database */
system(buf); if ((dbpath != NULL) && (strcmp(dbpath,dbname) != 0))
sprintf(buf, "%s %s%cbase%ctemplate1%c* %s%cbase%c%s", {
COPY_CMD, DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR, DataDir, if (*(dbpath+strlen(dbpath)-1) == SEP_CHAR)
SEP_CHAR, SEP_CHAR, dbname); *(dbpath+strlen(dbpath)-1) = '\0';
sprintf(loc, "%s%c%s", dbpath, SEP_CHAR, dbname);
}
else
{
strcpy(loc, dbname);
}
lp = ExpandDatabasePath(loc);
if (mkdir(lp,S_IRWXU) != 0)
elog(WARN,"Unable to create database directory %s",lp);
sprintf(buf, "%s %s%cbase%ctemplate1%c* %s",
COPY_CMD, DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR, lp);
system(buf); system(buf);
/* sprintf(buf, "insert into pg_database (datname, datdba, datpath) \ #if FALSE
values (\'%s\'::char16, \'%d\'::oid, \'%s\'::text);",
dbname, user_id, dbname);
*/
sprintf(buf, "insert into pg_database (datname, datdba, datpath) \ sprintf(buf, "insert into pg_database (datname, datdba, datpath) \
values (\'%s\', \'%d\', \'%s\');", values (\'%s\'::char16, \'%d\'::oid, \'%s\'::text);",
dbname, user_id, dbname); dbname, user_id, dbname);
#endif
sprintf(buf, "insert into pg_database (datname, datdba, datpath)"
" values (\'%s\', \'%d\', \'%s\');", dbname, user_id, loc);
pg_eval(buf, (char **) NULL, (Oid *) NULL, 0); pg_eval(buf, (char **) NULL, (Oid *) NULL, 0);
} }
...@@ -80,13 +98,20 @@ destroydb(char *dbname) ...@@ -80,13 +98,20 @@ destroydb(char *dbname)
{ {
Oid user_id, Oid user_id,
db_id; db_id;
char *path;
char dbpath[MAXPGPATH+1];
char buf[512]; char buf[512];
char loc[512];
text *dbtext;
Relation dbrel;
HeapTuple dbtup;
/* /*
* If this call returns, the database exists and we're allowed to * If this call returns, the database exists and we're allowed to
* remove it. * remove it.
*/ */
check_permissions("destroydb", dbname, &db_id, &user_id); check_permissions("destroydb", dbpath, dbname, &db_id, &user_id);
if (!OidIsValid(db_id)) if (!OidIsValid(db_id))
{ {
...@@ -94,7 +119,36 @@ destroydb(char *dbname) ...@@ -94,7 +119,36 @@ destroydb(char *dbname)
} }
/* stop the vacuum daemon */ /* stop the vacuum daemon */
stop_vacuum(dbname); stop_vacuum(dbpath, dbname);
#if FALSE
dbrel = heap_openr(DatabaseRelationName);
if (!RelationIsValid(dbrel))
elog(FATAL, "%s: cannot open relation \"%-.*s\"",
"destroydb", DatabaseRelationName);
dbtup = get_pg_dbtup("destroydb", dbname, dbrel);
if (!HeapTupleIsValid(dbtup))
elog(NOTICE,"destroydb: pg_database entry not found %s",dbname);
dbtext = (text *) heap_getattr(dbtup, InvalidBuffer,
Anum_pg_database_datpath,
RelationGetTupleDescriptor(dbrel),
(char *) NULL);
memcpy(loc, VARDATA(dbtext), (VARSIZE(dbtext)-VARHDRSZ));
*(loc+(VARSIZE(dbtext)-VARHDRSZ)) = '\0';
#if FALSE
if (*loc != SEP_CHAR)
{
sprintf(buf, "%s/base/%s", DataDir, loc);
strcpy(loc, buf);
}
#endif
heap_close(dbrel);
#endif
/* /*
* remove the pg_database tuple FIRST, this may fail due to * remove the pg_database tuple FIRST, this may fail due to
...@@ -108,7 +162,9 @@ destroydb(char *dbname) ...@@ -108,7 +162,9 @@ destroydb(char *dbname)
* remove the data directory. If the DELETE above failed, this will * remove the data directory. If the DELETE above failed, this will
* not be reached * not be reached
*/ */
sprintf(buf, "rm -r %s/base/%s", DataDir, dbname); path = ExpandDatabasePath(dbpath);
sprintf(buf, "rm -r %s", path);
system(buf); system(buf);
/* drop pages for this database that are in the shared buffer cache */ /* drop pages for this database that are in the shared buffer cache */
...@@ -160,6 +216,7 @@ get_pg_dbtup(char *command, char *dbname, Relation dbrel) ...@@ -160,6 +216,7 @@ get_pg_dbtup(char *command, char *dbname, Relation dbrel)
static void static void
check_permissions(char *command, check_permissions(char *command,
char *dbpath,
char *dbname, char *dbname,
Oid *dbIdP, Oid *dbIdP,
Oid *userIdP) Oid *userIdP)
...@@ -172,6 +229,8 @@ check_permissions(char *command, ...@@ -172,6 +229,8 @@ check_permissions(char *command,
bool dbfound; bool dbfound;
bool use_super; bool use_super;
char *userName; char *userName;
text *dbtext;
char path[MAXPGPATH+1];
userName = GetPgUserName(); userName = GetPgUserName();
utup = SearchSysCacheTuple(USENAME, PointerGetDatum(userName), utup = SearchSysCacheTuple(USENAME, PointerGetDatum(userName),
...@@ -228,6 +287,13 @@ check_permissions(char *command, ...@@ -228,6 +287,13 @@ check_permissions(char *command,
RelationGetTupleDescriptor(dbrel), RelationGetTupleDescriptor(dbrel),
(char *) NULL); (char *) NULL);
*dbIdP = dbtup->t_oid; *dbIdP = dbtup->t_oid;
dbtext = (text *) heap_getattr(dbtup, InvalidBuffer,
Anum_pg_database_datpath,
RelationGetTupleDescriptor(dbrel),
(char *) NULL);
strncpy(path, VARDATA(dbtext), (VARSIZE(dbtext)-VARHDRSZ));
*(path+VARSIZE(dbtext)-VARHDRSZ) = '\0';
} }
else else
{ {
...@@ -259,21 +325,31 @@ check_permissions(char *command, ...@@ -259,21 +325,31 @@ check_permissions(char *command,
elog(WARN, "%s: database %s is not owned by you.", command, dbname); elog(WARN, "%s: database %s is not owned by you.", command, dbname);
} }
}
if (dbfound && !strcmp(command, "destroydb"))
strcpy(dbpath, path);
} /* check_permissions() */
/* /*
* stop_vacuum() -- stop the vacuum daemon on the database, if one is * stop_vacuum() -- stop the vacuum daemon on the database, if one is running.
* running.
*/ */
static void static void
stop_vacuum(char *dbname) stop_vacuum(char *dbpath, char *dbname)
{ {
char filename[256]; char filename[256];
FILE *fp; FILE *fp;
int pid; int pid;
sprintf(filename, "%s%cbase%c%s%c%s.vacuum", DataDir, SEP_CHAR, SEP_CHAR, if (strchr(dbpath, SEP_CHAR) != 0)
{
sprintf(filename, "%s%cbase%c%s%c%s.vacuum", DataDir, SEP_CHAR, SEP_CHAR,
dbname, SEP_CHAR, dbname); dbname, SEP_CHAR, dbname);
}
else
{
sprintf(filename, "%s%c%s.vacuum", dbpath, SEP_CHAR, dbname);
}
if ((fp = AllocateFile(filename, "r")) != NULL) if ((fp = AllocateFile(filename, "r")) != NULL)
{ {
fscanf(fp, "%d", &pid); fscanf(fp, "%d", &pid);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Id: fd.c,v 1.26 1997/09/18 20:21:24 momjian Exp $ * $Id: fd.c,v 1.27 1997/11/07 06:38:15 thomas Exp $
* *
* NOTES: * NOTES:
* *
...@@ -125,8 +125,6 @@ static Size SizeVfdCache = 0; ...@@ -125,8 +125,6 @@ static Size SizeVfdCache = 0;
*/ */
static int nfile = 0; static int nfile = 0;
static char Sep_char = '/';
/* /*
* Private Routines * Private Routines
* *
...@@ -458,23 +456,25 @@ FreeVfd(File file) ...@@ -458,23 +456,25 @@ FreeVfd(File file)
VfdCache[0].nextFree = file; VfdCache[0].nextFree = file;
} }
/* filepath()
* Open specified file name.
* Fill in absolute path fields if necessary.
*
* Modify to use GetDatabasePath() rather than hardcoded paths.
* - thomas 1997-11-02
*/
static char * static char *
filepath(char *filename) filepath(char *filename)
{ {
char *buf; char *buf;
char basename[16];
int len; int len;
if (*filename != Sep_char) /* Not an absolute path name? Then fill in with database path... */
if (*filename != SEP_CHAR)
{ {
/* Either /base/ or \base\ */ len = strlen(GetDatabasePath()) + strlen(filename) + 2;
sprintf(basename, "%cbase%c", Sep_char, Sep_char);
len = strlen(DataDir) + strlen(basename) + strlen(GetDatabaseName())
+ strlen(filename) + 2;
buf = (char *) palloc(len); buf = (char *) palloc(len);
sprintf(buf, "%s%s%s%c%s", sprintf(buf, "%s%c%s", GetDatabasePath(), SEP_CHAR, filename);
DataDir, basename, GetDatabaseName(), Sep_char, filename);
} }
else else
{ {
...@@ -482,6 +482,10 @@ filepath(char *filename) ...@@ -482,6 +482,10 @@ filepath(char *filename)
strcpy(buf, filename); strcpy(buf, filename);
} }
#ifdef FILEDEBUG
printf("filepath: path is %s\n", buf);
#endif
return (buf); return (buf);
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.23 1997/10/25 01:10:04 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.24 1997/11/07 06:38:19 thomas Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -480,6 +480,7 @@ mdblindwrt(char *dbstr, ...@@ -480,6 +480,7 @@ mdblindwrt(char *dbstr,
nchars = 0; nchars = 0;
/* construct the path to the file and open it */ /* construct the path to the file and open it */
/* system table? then put in system area... */
if (dbid == (Oid) 0) if (dbid == (Oid) 0)
{ {
path = (char *) palloc(strlen(DataDir) + sizeof(NameData) + 2 + nchars); path = (char *) palloc(strlen(DataDir) + sizeof(NameData) + 2 + nchars);
...@@ -488,8 +489,10 @@ mdblindwrt(char *dbstr, ...@@ -488,8 +489,10 @@ mdblindwrt(char *dbstr,
else else
sprintf(path, "%s/%s.%d", DataDir, relstr, segno); sprintf(path, "%s/%s.%d", DataDir, relstr, segno);
} }
/* user table? then put in user database area... */
else else
{ {
#if FALSE
path = (char *) palloc(strlen(DataDir) + strlen("/base/") + 2 * sizeof(NameData) + 2 + nchars); path = (char *) palloc(strlen(DataDir) + strlen("/base/") + 2 * sizeof(NameData) + 2 + nchars);
if (segno == 0) if (segno == 0)
sprintf(path, "%s/base/%s/%s", DataDir, sprintf(path, "%s/base/%s/%s", DataDir,
...@@ -497,6 +500,12 @@ mdblindwrt(char *dbstr, ...@@ -497,6 +500,12 @@ mdblindwrt(char *dbstr,
else else
sprintf(path, "%s/base/%s/%s.%d", DataDir, dbstr, sprintf(path, "%s/base/%s/%s.%d", DataDir, dbstr,
relstr, segno); relstr, segno);
#endif
path = (char *) palloc(strlen(GetDatabasePath()) + 2 * sizeof(NameData) + 2 + nchars);
if (segno == 0)
sprintf(path, "%s%c%s", GetDatabasePath(), SEP_CHAR, relstr);
else
sprintf(path, "%s%c%s.%d", GetDatabasePath(), SEP_CHAR, relstr, segno);
} }
if ((fd = open(path, O_RDWR, 0600)) < 0) if ((fd = open(path, O_RDWR, 0600)) < 0)
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.27 1997/10/28 14:57:24 vadim Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.28 1997/11/07 06:38:51 thomas Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -569,7 +569,7 @@ ProcessUtility(Node * parsetree, ...@@ -569,7 +569,7 @@ ProcessUtility(Node * parsetree,
commandTag = "CREATEDB"; commandTag = "CREATEDB";
CHECK_IF_ABORTED(); CHECK_IF_ABORTED();
createdb(stmt->dbname); createdb(stmt->dbname, stmt->dbpath);
} }
break; break;
......
This diff is collapsed.
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: miscadmin.h,v 1.16 1997/09/18 14:42:22 vadim Exp $ * $Id: miscadmin.h,v 1.17 1997/11/07 06:38:29 thomas Exp $
* *
* NOTES * NOTES
* some of the information in this file will be moved to * some of the information in this file will be moved to
...@@ -107,6 +107,11 @@ extern Oid LastOidProcessed; /* for query rewrite */ ...@@ -107,6 +107,11 @@ extern Oid LastOidProcessed; /* for query rewrite */
* POSTGRES directory path definitions. * * POSTGRES directory path definitions. *
*****************************************************************************/ *****************************************************************************/
/* in utils/misc/database.c */
extern void GetRawDatabaseInfo(char *name, Oid *owner, Oid *db_id, char *path);
extern int GetDatabaseInfo(char *name, Oid *owner, char *path);
extern char *ExpandDatabasePath(char *path);
/* now in utils/init/miscinit.c */ /* now in utils/init/miscinit.c */
extern char *GetDatabasePath(void); extern char *GetDatabasePath(void);
extern char *GetDatabaseName(void); extern char *GetDatabaseName(void);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: parsenodes.h,v 1.29 1997/10/28 15:10:39 vadim Exp $ * $Id: parsenodes.h,v 1.30 1997/11/07 06:38:38 thomas Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -473,6 +473,7 @@ typedef struct CreatedbStmt ...@@ -473,6 +473,7 @@ typedef struct CreatedbStmt
{ {
NodeTag type; NodeTag type;
char *dbname; /* database to create */ char *dbname; /* database to create */
char *dbpath; /* location of database */
} CreatedbStmt; } CreatedbStmt;
/* ---------------------- /* ----------------------
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: dbcommands.h,v 1.4 1997/09/08 02:38:05 momjian Exp $ * $Id: dbcommands.h,v 1.5 1997/11/07 06:38:09 thomas Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#define SIGKILLDAEMON1 SIGINT #define SIGKILLDAEMON1 SIGINT
#define SIGKILLDAEMON2 SIGTERM #define SIGKILLDAEMON2 SIGTERM
extern void createdb(char *dbname); extern void createdb(char *dbname, char *dbpath);
extern void destroydb(char *dbname); extern void destroydb(char *dbname);
#endif /* DBCOMMANDS_H */ #endif /* DBCOMMANDS_H */
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