Skip to content

Commit 7d1f2f8

Browse files
author
Thomas G. Lockhart
committed
Support alternate database locations.
1 parent d98f2f9 commit 7d1f2f8

File tree

8 files changed

+260
-241
lines changed

8 files changed

+260
-241
lines changed

src/backend/parser/dbcommands.c

Lines changed: 99 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.9 1997/09/08 21:46:07 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.10 1997/11/07 06:37:55 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include <stdio.h>
1515
#include <string.h>
1616
#include <signal.h>
17+
#include <sys/stat.h>
1718

1819
#include "postgres.h"
1920
#include "miscadmin.h" /* for DataDir */
@@ -36,41 +37,58 @@
3637

3738
/* non-export function prototypes */
3839
static void
39-
check_permissions(char *command, char *dbname,
40+
check_permissions(char *command, char *dbpath, char *dbname,
4041
Oid *dbIdP, Oid *userIdP);
4142
static HeapTuple get_pg_dbtup(char *command, char *dbname, Relation dbrel);
42-
static void stop_vacuum(char *dbname);
43+
static void stop_vacuum(char *dbpath, char *dbname);
4344

4445
void
45-
createdb(char *dbname)
46+
createdb(char *dbname, char *dbpath)
4647
{
4748
Oid db_id,
4849
user_id;
4950
char buf[512];
51+
char *lp,
52+
loc[512];
5053

5154
/*
5255
* If this call returns, the database does not exist and we're allowed
5356
* to create databases.
5457
*/
55-
check_permissions("createdb", dbname, &db_id, &user_id);
58+
check_permissions("createdb", dbpath, dbname, &db_id, &user_id);
5659

5760
/* close virtual file descriptors so we can do system() calls */
5861
closeAllVfds();
5962

60-
sprintf(buf, "mkdir %s%cbase%c%s", DataDir, SEP_CHAR, SEP_CHAR, dbname);
61-
system(buf);
62-
sprintf(buf, "%s %s%cbase%ctemplate1%c* %s%cbase%c%s",
63-
COPY_CMD, DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR, DataDir,
64-
SEP_CHAR, SEP_CHAR, dbname);
63+
/* Now create directory for this new database */
64+
if ((dbpath != NULL) && (strcmp(dbpath,dbname) != 0))
65+
{
66+
if (*(dbpath+strlen(dbpath)-1) == SEP_CHAR)
67+
*(dbpath+strlen(dbpath)-1) = '\0';
68+
sprintf(loc, "%s%c%s", dbpath, SEP_CHAR, dbname);
69+
}
70+
else
71+
{
72+
strcpy(loc, dbname);
73+
}
74+
75+
lp = ExpandDatabasePath(loc);
76+
77+
if (mkdir(lp,S_IRWXU) != 0)
78+
elog(WARN,"Unable to create database directory %s",lp);
79+
80+
sprintf(buf, "%s %s%cbase%ctemplate1%c* %s",
81+
COPY_CMD, DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR, lp);
6582
system(buf);
6683

67-
/* sprintf(buf, "insert into pg_database (datname, datdba, datpath) \
68-
values (\'%s\'::char16, \'%d\'::oid, \'%s\'::text);",
69-
dbname, user_id, dbname);
70-
*/
84+
#if FALSE
7185
sprintf(buf, "insert into pg_database (datname, datdba, datpath) \
72-
values (\'%s\', \'%d\', \'%s\');",
86+
values (\'%s\'::char16, \'%d\'::oid, \'%s\'::text);",
7387
dbname, user_id, dbname);
88+
#endif
89+
90+
sprintf(buf, "insert into pg_database (datname, datdba, datpath)"
91+
" values (\'%s\', \'%d\', \'%s\');", dbname, user_id, loc);
7492

7593
pg_eval(buf, (char **) NULL, (Oid *) NULL, 0);
7694
}
@@ -80,21 +98,57 @@ destroydb(char *dbname)
8098
{
8199
Oid user_id,
82100
db_id;
101+
char *path;
102+
char dbpath[MAXPGPATH+1];
83103
char buf[512];
104+
char loc[512];
105+
text *dbtext;
106+
107+
Relation dbrel;
108+
HeapTuple dbtup;
84109

85110
/*
86111
* If this call returns, the database exists and we're allowed to
87112
* remove it.
88113
*/
89-
check_permissions("destroydb", dbname, &db_id, &user_id);
114+
check_permissions("destroydb", dbpath, dbname, &db_id, &user_id);
90115

91116
if (!OidIsValid(db_id))
92117
{
93118
elog(FATAL, "impossible: pg_database instance with invalid OID.");
94119
}
95120

96121
/* stop the vacuum daemon */
97-
stop_vacuum(dbname);
122+
stop_vacuum(dbpath, dbname);
123+
124+
#if FALSE
125+
dbrel = heap_openr(DatabaseRelationName);
126+
if (!RelationIsValid(dbrel))
127+
elog(FATAL, "%s: cannot open relation \"%-.*s\"",
128+
"destroydb", DatabaseRelationName);
129+
130+
dbtup = get_pg_dbtup("destroydb", dbname, dbrel);
131+
132+
if (!HeapTupleIsValid(dbtup))
133+
elog(NOTICE,"destroydb: pg_database entry not found %s",dbname);
134+
135+
dbtext = (text *) heap_getattr(dbtup, InvalidBuffer,
136+
Anum_pg_database_datpath,
137+
RelationGetTupleDescriptor(dbrel),
138+
(char *) NULL);
139+
memcpy(loc, VARDATA(dbtext), (VARSIZE(dbtext)-VARHDRSZ));
140+
*(loc+(VARSIZE(dbtext)-VARHDRSZ)) = '\0';
141+
142+
#if FALSE
143+
if (*loc != SEP_CHAR)
144+
{
145+
sprintf(buf, "%s/base/%s", DataDir, loc);
146+
strcpy(loc, buf);
147+
}
148+
#endif
149+
150+
heap_close(dbrel);
151+
#endif
98152

99153
/*
100154
* remove the pg_database tuple FIRST, this may fail due to
@@ -108,7 +162,9 @@ destroydb(char *dbname)
108162
* remove the data directory. If the DELETE above failed, this will
109163
* not be reached
110164
*/
111-
sprintf(buf, "rm -r %s/base/%s", DataDir, dbname);
165+
path = ExpandDatabasePath(dbpath);
166+
167+
sprintf(buf, "rm -r %s", path);
112168
system(buf);
113169

114170
/* 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)
160216

161217
static void
162218
check_permissions(char *command,
219+
char *dbpath,
163220
char *dbname,
164221
Oid *dbIdP,
165222
Oid *userIdP)
@@ -172,6 +229,8 @@ check_permissions(char *command,
172229
bool dbfound;
173230
bool use_super;
174231
char *userName;
232+
text *dbtext;
233+
char path[MAXPGPATH+1];
175234

176235
userName = GetPgUserName();
177236
utup = SearchSysCacheTuple(USENAME, PointerGetDatum(userName),
@@ -228,6 +287,13 @@ check_permissions(char *command,
228287
RelationGetTupleDescriptor(dbrel),
229288
(char *) NULL);
230289
*dbIdP = dbtup->t_oid;
290+
dbtext = (text *) heap_getattr(dbtup, InvalidBuffer,
291+
Anum_pg_database_datpath,
292+
RelationGetTupleDescriptor(dbrel),
293+
(char *) NULL);
294+
295+
strncpy(path, VARDATA(dbtext), (VARSIZE(dbtext)-VARHDRSZ));
296+
*(path+VARSIZE(dbtext)-VARHDRSZ) = '\0';
231297
}
232298
else
233299
{
@@ -259,21 +325,31 @@ check_permissions(char *command,
259325
elog(WARN, "%s: database %s is not owned by you.", command, dbname);
260326

261327
}
262-
}
328+
329+
if (dbfound && !strcmp(command, "destroydb"))
330+
strcpy(dbpath, path);
331+
} /* check_permissions() */
263332

264333
/*
265-
* stop_vacuum() -- stop the vacuum daemon on the database, if one is
266-
* running.
334+
* stop_vacuum() -- stop the vacuum daemon on the database, if one is running.
267335
*/
268336
static void
269-
stop_vacuum(char *dbname)
337+
stop_vacuum(char *dbpath, char *dbname)
270338
{
271339
char filename[256];
272340
FILE *fp;
273341
int pid;
274342

275-
sprintf(filename, "%s%cbase%c%s%c%s.vacuum", DataDir, SEP_CHAR, SEP_CHAR,
343+
if (strchr(dbpath, SEP_CHAR) != 0)
344+
{
345+
sprintf(filename, "%s%cbase%c%s%c%s.vacuum", DataDir, SEP_CHAR, SEP_CHAR,
276346
dbname, SEP_CHAR, dbname);
347+
}
348+
else
349+
{
350+
sprintf(filename, "%s%c%s.vacuum", dbpath, SEP_CHAR, dbname);
351+
}
352+
277353
if ((fp = AllocateFile(filename, "r")) != NULL)
278354
{
279355
fscanf(fp, "%d", &pid);

src/backend/storage/file/fd.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 1994, Regents of the University of California
77
*
88
* IDENTIFICATION
9-
* $Id: fd.c,v 1.26 1997/09/18 20:21:24 momjian Exp $
9+
* $Id: fd.c,v 1.27 1997/11/07 06:38:15 thomas Exp $
1010
*
1111
* NOTES:
1212
*
@@ -125,8 +125,6 @@ static Size SizeVfdCache = 0;
125125
*/
126126
static int nfile = 0;
127127

128-
static char Sep_char = '/';
129-
130128
/*
131129
* Private Routines
132130
*
@@ -458,30 +456,36 @@ FreeVfd(File file)
458456
VfdCache[0].nextFree = file;
459457
}
460458

459+
/* filepath()
460+
* Open specified file name.
461+
* Fill in absolute path fields if necessary.
462+
*
463+
* Modify to use GetDatabasePath() rather than hardcoded paths.
464+
* - thomas 1997-11-02
465+
*/
461466
static char *
462467
filepath(char *filename)
463468
{
464469
char *buf;
465-
char basename[16];
466470
int len;
467471

468-
if (*filename != Sep_char)
472+
/* Not an absolute path name? Then fill in with database path... */
473+
if (*filename != SEP_CHAR)
469474
{
470-
/* Either /base/ or \base\ */
471-
sprintf(basename, "%cbase%c", Sep_char, Sep_char);
472-
473-
len = strlen(DataDir) + strlen(basename) + strlen(GetDatabaseName())
474-
+ strlen(filename) + 2;
475+
len = strlen(GetDatabasePath()) + strlen(filename) + 2;
475476
buf = (char *) palloc(len);
476-
sprintf(buf, "%s%s%s%c%s",
477-
DataDir, basename, GetDatabaseName(), Sep_char, filename);
477+
sprintf(buf, "%s%c%s", GetDatabasePath(), SEP_CHAR, filename);
478478
}
479479
else
480480
{
481481
buf = (char *) palloc(strlen(filename) + 1);
482482
strcpy(buf, filename);
483483
}
484484

485+
#ifdef FILEDEBUG
486+
printf("filepath: path is %s\n", buf);
487+
#endif
488+
485489
return (buf);
486490
}
487491

src/backend/storage/smgr/md.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.23 1997/10/25 01:10:04 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.24 1997/11/07 06:38:19 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -480,6 +480,7 @@ mdblindwrt(char *dbstr,
480480
nchars = 0;
481481

482482
/* construct the path to the file and open it */
483+
/* system table? then put in system area... */
483484
if (dbid == (Oid) 0)
484485
{
485486
path = (char *) palloc(strlen(DataDir) + sizeof(NameData) + 2 + nchars);
@@ -488,15 +489,23 @@ mdblindwrt(char *dbstr,
488489
else
489490
sprintf(path, "%s/%s.%d", DataDir, relstr, segno);
490491
}
492+
/* user table? then put in user database area... */
491493
else
492494
{
495+
#if FALSE
493496
path = (char *) palloc(strlen(DataDir) + strlen("/base/") + 2 * sizeof(NameData) + 2 + nchars);
494497
if (segno == 0)
495498
sprintf(path, "%s/base/%s/%s", DataDir,
496499
dbstr, relstr);
497500
else
498501
sprintf(path, "%s/base/%s/%s.%d", DataDir, dbstr,
499502
relstr, segno);
503+
#endif
504+
path = (char *) palloc(strlen(GetDatabasePath()) + 2 * sizeof(NameData) + 2 + nchars);
505+
if (segno == 0)
506+
sprintf(path, "%s%c%s", GetDatabasePath(), SEP_CHAR, relstr);
507+
else
508+
sprintf(path, "%s%c%s.%d", GetDatabasePath(), SEP_CHAR, relstr, segno);
500509
}
501510

502511
if ((fd = open(path, O_RDWR, 0600)) < 0)

src/backend/tcop/utility.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.27 1997/10/28 14:57:24 vadim Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.28 1997/11/07 06:38:51 thomas Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -569,7 +569,7 @@ ProcessUtility(Node * parsetree,
569569

570570
commandTag = "CREATEDB";
571571
CHECK_IF_ABORTED();
572-
createdb(stmt->dbname);
572+
createdb(stmt->dbname, stmt->dbpath);
573573
}
574574
break;
575575

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy