1 /*-------------------------------------------------------------------------
4 * miscellaneous initialization support stuff
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/misc/Attic/database.c,v 1.53 2002/09/04 20:31:32 momjian Exp $
13 *-------------------------------------------------------------------------
22 #include "access/xact.h"
23 #include "catalog/catname.h"
24 #include "catalog/catalog.h"
25 #include "catalog/pg_database.h"
26 #include "miscadmin.h"
27 #include "utils/syscache.h"
30 static bool PhonyHeapTupleSatisfiesNow(HeapTupleHeader tuple);
34 * ExpandDatabasePath resolves a proposed database path (obtained from
35 * pg_database.datpath) to a full absolute path for further consumption.
36 * NULL means an error, which the caller should process. One reason for
37 * such an error would be an absolute alternative path when no absolute
42 ExpandDatabasePath(const char *dbpath)
51 if (strlen(dbpath) >= MAXPGPATH)
52 return NULL; /* ain't gonna fit nohow */
54 /* leading path delimiter? then already absolute path */
57 #ifdef ALLOW_ABSOLUTE_DBPATHS
58 cp = strrchr(dbpath, '/');
60 strncpy(buf, dbpath, len);
61 snprintf(&buf[len], MAXPGPATH - len, "/base/%s", (cp + 1));
66 /* path delimiter somewhere? then has leading environment variable */
67 else if ((cp = strchr(dbpath, '/')) != NULL)
72 strncpy(buf, dbpath, len);
78 snprintf(buf, sizeof(buf), "%s/base/%s", envvar, (cp + 1));
82 /* no path delimiter? then add the default path prefix */
83 snprintf(buf, sizeof(buf), "%s/base/%s", DataDir, dbpath);
87 * check for illegal characters in dbpath these should really throw an
88 * error, shouldn't they? or else all callers need to test for NULL
90 for (cp = buf; *cp; cp++)
93 * The following characters will not be allowed anywhere in the
94 * database path. (Do not include the slash or '.' here.)
96 char illegal_dbpath_chars[] =
97 "\001\002\003\004\005\006\007\010"
98 "\011\012\013\014\015\016\017\020"
99 "\021\022\023\024\025\026\027\030"
100 "\031\032\033\034\035\036\037"
105 for (cx = illegal_dbpath_chars; *cx; cx++)
108 /* don't allow access to parent dirs */
109 if (strncmp(cp, "/../", 4) == 0)
114 } /* ExpandDatabasePath() */
118 /* --------------------------------
119 * GetRawDatabaseInfo() -- Find the OID and path of the database.
121 * The database's oid forms half of the unique key for the system
122 * caches and lock tables. We therefore want it initialized before
123 * we open any relations, since opening relations puts things in the
124 * cache. To get around this problem, this code opens and scans the
125 * pg_database relation by hand.
127 * This code knows way more than it should about the layout of
128 * tuples on disk, but there seems to be no help for that.
129 * We're pulling ourselves up by the bootstraps here...
130 * --------------------------------
133 GetRawDatabaseInfo(const char *name, Oid *db_id, char *path)
141 Form_pg_database tup_db;
145 rnode.relNode = RelOid_pg_database;
146 dbfname = relpath(rnode);
148 if ((dbfd = open(dbfname, O_RDONLY | PG_BINARY, 0)) < 0)
149 elog(FATAL, "cannot open %s: %m", dbfname);
154 * read and examine every page in pg_database
156 * Raw I/O! Read those tuples the hard way! Yow!
158 * Why don't we use the access methods or move this code someplace else?
159 * This is really pg_database schema dependent code. Perhaps it
160 * should go in lib/catalog/pg_database? -cim 10/3/90
162 * mao replies 4 apr 91: yeah, maybe this should be moved to
163 * lib/catalog. however, we CANNOT use the access methods since those
164 * use the buffer cache, which uses the relation cache, which requires
165 * that the dbid be set, which is what we're trying to do here.
168 pg = (Page) palloc(BLCKSZ);
170 while ((nbytes = read(dbfd, pg, BLCKSZ)) == BLCKSZ)
172 OffsetNumber max = PageGetMaxOffsetNumber(pg);
173 OffsetNumber lineoff;
175 /* look at each tuple on the page */
176 for (lineoff = FirstOffsetNumber; lineoff <= max; lineoff++)
178 ItemId lpp = PageGetItemId(pg, lineoff);
180 /* if it's a freed tuple, ignore it */
181 if (!ItemIdIsUsed(lpp))
184 /* get a pointer to the tuple itself */
185 tup.t_datamcxt = NULL;
186 tup.t_data = (HeapTupleHeader) PageGetItem(pg, lpp);
189 * Check to see if tuple is valid (committed).
191 * XXX warning, will robinson: violation of transaction semantics
192 * happens right here. We cannot really determine if the
193 * tuple is valid without checking transaction commit status,
194 * and the only way to do that at init time is to paw over
195 * pg_clog by hand, too. Instead of checking, we assume that
196 * the inserting transaction committed, and that any deleting
197 * transaction did also, unless shown otherwise by on-row
198 * commit status bits.
200 * All in all, this code is pretty shaky. We will cross-check
201 * our result in ReverifyMyDatabase() in postinit.c.
203 * NOTE: if a bogus tuple in pg_database prevents connection to a
204 * valid database, a fix is to connect to another database and
205 * do "select * from pg_database". That should cause
206 * committed and dead tuples to be marked with correct states.
208 * XXX wouldn't it be better to let new backends read the
209 * database OID from a flat file, handled the same way we
210 * handle the password relation?
212 if (!PhonyHeapTupleSatisfiesNow(tup.t_data))
216 * Okay, see if this is the one we want.
218 tup_db = (Form_pg_database) GETSTRUCT(&tup);
220 if (strcmp(name, NameStr(tup_db->datname)) == 0)
222 /* Found it; extract the OID and the database path. */
223 *db_id = HeapTupleGetOid(&tup);
224 pathlen = VARSIZE(&(tup_db->datpath)) - VARHDRSZ;
226 pathlen = 0; /* pure paranoia */
227 if (pathlen >= MAXPGPATH)
228 pathlen = MAXPGPATH - 1; /* more paranoia */
229 strncpy(path, VARDATA(&(tup_db->datpath)), pathlen);
230 path[pathlen] = '\0';
236 /* failed to find it... */
246 * PhonyHeapTupleSatisfiesNow --- cut-down tuple time qual test
248 * This is a simplified version of HeapTupleSatisfiesNow() that does not
249 * depend on having transaction commit info available. Any transaction
250 * that touched the tuple is assumed committed unless later marked invalid.
251 * (While we could think about more complex rules, this seems appropriate
252 * for examining pg_database, since both CREATE DATABASE and DROP DATABASE
253 * are non-roll-back-able.)
256 PhonyHeapTupleSatisfiesNow(HeapTupleHeader tuple)
258 if (!(tuple->t_infomask & HEAP_XMIN_COMMITTED))
260 if (tuple->t_infomask & HEAP_XMIN_INVALID)
263 if (tuple->t_infomask & HEAP_MOVED_OFF)
265 /* else assume committed */
268 if (tuple->t_infomask & HEAP_XMAX_INVALID) /* xid invalid or aborted */
271 /* assume xmax transaction committed */
272 if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE)