diff options
Diffstat (limited to 'src/backend/utils')
| -rw-r--r-- | src/backend/utils/init/flatfiles.c | 20 | ||||
| -rw-r--r-- | src/backend/utils/init/postinit.c | 88 | ||||
| -rw-r--r-- | src/backend/utils/misc/Makefile | 14 | ||||
| -rw-r--r-- | src/backend/utils/misc/database.c | 189 |
4 files changed, 81 insertions, 230 deletions
diff --git a/src/backend/utils/init/flatfiles.c b/src/backend/utils/init/flatfiles.c index 8968d572c8..e7ddd0a42c 100644 --- a/src/backend/utils/init/flatfiles.c +++ b/src/backend/utils/init/flatfiles.c @@ -22,7 +22,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/utils/init/flatfiles.c,v 1.3 2005/02/20 22:02:19 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/init/flatfiles.c,v 1.4 2005/02/26 18:43:33 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -243,10 +243,12 @@ write_database_file(Relation drel) Form_pg_database dbform = (Form_pg_database) GETSTRUCT(tuple); char *datname; Oid datoid; + Oid dattablespace; TransactionId datfrozenxid; datname = NameStr(dbform->datname); datoid = HeapTupleGetOid(tuple); + dattablespace = dbform->dattablespace; datfrozenxid = dbform->datfrozenxid; /* @@ -276,13 +278,13 @@ write_database_file(Relation drel) } /* - * The file format is: "dbname" oid frozenxid + * The file format is: "dbname" oid tablespace frozenxid * * The xid is not needed for backend startup, but may be of use * for forensic purposes. */ fputs_quote(datname, fp); - fprintf(fp, " %u %u\n", datoid, datfrozenxid); + fprintf(fp, " %u %u %u\n", datoid, dattablespace, datfrozenxid); } heap_endscan(scan); @@ -830,15 +832,3 @@ flatfile_update_trigger(PG_FUNCTION_ARGS) return PointerGetDatum(NULL); } - - -/* - * Old version of trigger --- remove after we can force an initdb - */ -extern Datum update_pg_pwd_and_pg_group(PG_FUNCTION_ARGS); - -Datum -update_pg_pwd_and_pg_group(PG_FUNCTION_ARGS) -{ - return flatfile_update_trigger(fcinfo); -} diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 53eb47a97e..d1479bbab7 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.140 2005/02/20 21:46:49 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.141 2005/02/26 18:43:33 tgl Exp $ * * *------------------------------------------------------------------------- @@ -27,14 +27,16 @@ #include "catalog/pg_database.h" #include "catalog/pg_shadow.h" #include "catalog/pg_tablespace.h" +#include "libpq/hba.h" #include "mb/pg_wchar.h" #include "miscadmin.h" #include "postmaster/postmaster.h" -#include "storage/backendid.h" +#include "storage/fd.h" #include "storage/ipc.h" #include "storage/proc.h" #include "storage/sinval.h" #include "storage/smgr.h" +#include "utils/flatfiles.h" #include "utils/fmgroids.h" #include "utils/guc.h" #include "utils/portal.h" @@ -42,6 +44,7 @@ #include "utils/syscache.h" +static bool FindMyDatabase(const char *name, Oid *db_id, Oid *db_tablespace); static void ReverifyMyDatabase(const char *name); static void InitCommunication(void); static void ShutdownPostgres(int code, Datum arg); @@ -51,18 +54,60 @@ static bool ThereIsAtLeastOneUser(void); /*** InitPostgres support ***/ -/* -------------------------------- - * ReverifyMyDatabase +/* + * FindMyDatabase -- get the critical info needed to locate my database * - * Since we are forced to fetch the database OID out of pg_database without - * benefit of locking or transaction ID checking (see utils/misc/database.c), - * we might have gotten a wrong answer. Or, we might have attached to a - * database that's in process of being destroyed by destroydb(). This - * routine is called after we have all the locking and other infrastructure - * running --- now we can check that we are really attached to a valid - * database. + * Find the named database in pg_database, return its database OID and the + * OID of its default tablespace. Return TRUE if found, FALSE if not. * - * In reality, if destroydb() is running in parallel with our startup, + * Since we are not yet up and running as a backend, we cannot look directly + * at pg_database (we can't obtain locks nor participate in transactions). + * So to get the info we need before starting up, we must look at the "flat + * file" copy of pg_database that is helpfully maintained by flatfiles.c. + * This is subject to various race conditions, so after we have the + * transaction infrastructure started, we have to recheck the information; + * see ReverifyMyDatabase. + */ +static bool +FindMyDatabase(const char *name, Oid *db_id, Oid *db_tablespace) +{ + bool result = false; + char *filename; + FILE *db_file; + char thisname[NAMEDATALEN]; + + filename = database_getflatfilename(); + db_file = AllocateFile(filename, "r"); + if (db_file == NULL) + ereport(FATAL, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", filename))); + + while (read_pg_database_line(db_file, thisname, db_id, db_tablespace)) + { + if (strcmp(thisname, name) == 0) + { + result = true; + break; + } + } + + FreeFile(db_file); + pfree(filename); + + return result; +} + +/* + * ReverifyMyDatabase -- recheck info obtained by FindMyDatabase + * + * Since FindMyDatabase cannot lock pg_database, the information it read + * could be stale; for example we might have attached to a database that's in + * process of being destroyed by dropdb(). This routine is called after + * we have all the locking and other infrastructure running --- now we can + * check that we are really attached to a valid database. + * + * In reality, if dropdb() is running in parallel with our startup, * it's pretty likely that we will have failed before now, due to being * unable to read some of the system tables within the doomed database. * This routine just exists to make *sure* we have not started up in an @@ -75,7 +120,6 @@ static bool ThereIsAtLeastOneUser(void); * To avoid having to read pg_database more times than necessary * during session startup, this place is also fitting to set up any * database-specific configuration variables. - * -------------------------------- */ static void ReverifyMyDatabase(const char *name) @@ -87,10 +131,10 @@ ReverifyMyDatabase(const char *name) Form_pg_database dbform; /* - * Because we grab AccessShareLock here, we can be sure that destroydb + * Because we grab RowShareLock here, we can be sure that dropdb() * is not running in parallel with us (any more). */ - pgdbrel = heap_openr(DatabaseRelationName, AccessShareLock); + pgdbrel = heap_openr(DatabaseRelationName, RowShareLock); ScanKeyInit(&key, Anum_pg_database_datname, @@ -104,7 +148,7 @@ ReverifyMyDatabase(const char *name) HeapTupleGetOid(tup) != MyDatabaseId) { /* OOPS */ - heap_close(pgdbrel, AccessShareLock); + heap_close(pgdbrel, RowShareLock); /* * The only real problem I could have created is to load dirty @@ -131,7 +175,7 @@ ReverifyMyDatabase(const char *name) name))); /* - * OK, we're golden. Only other to-do item is to save the encoding + * OK, we're golden. Next to-do item is to save the encoding * info out of the pg_database tuple. */ SetDatabaseEncoding(dbform->encoding); @@ -143,7 +187,7 @@ ReverifyMyDatabase(const char *name) PGC_BACKEND, PGC_S_DEFAULT); /* - * Set up database-specific configuration variables. + * Lastly, set up any database-specific configuration variables. */ if (IsUnderPostmaster) { @@ -161,7 +205,7 @@ ReverifyMyDatabase(const char *name) } heap_endscan(pgdbscan); - heap_close(pgdbrel, AccessShareLock); + heap_close(pgdbrel, RowShareLock); } @@ -261,11 +305,9 @@ InitPostgres(const char *dbname, const char *username) /* * Find oid and tablespace of the database we're about to open. * Since we're not yet up and running we have to use the hackish - * GetRawDatabaseInfo. + * FindMyDatabase. */ - GetRawDatabaseInfo(dbname, &MyDatabaseId, &MyDatabaseTableSpace); - - if (!OidIsValid(MyDatabaseId)) + if (!FindMyDatabase(dbname, &MyDatabaseId, &MyDatabaseTableSpace)) ereport(FATAL, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist", diff --git a/src/backend/utils/misc/Makefile b/src/backend/utils/misc/Makefile index f907f22d16..afa3216558 100644 --- a/src/backend/utils/misc/Makefile +++ b/src/backend/utils/misc/Makefile @@ -1,4 +1,12 @@ -# $PostgreSQL: pgsql/src/backend/utils/misc/Makefile,v 1.22 2003/11/29 19:52:03 pgsql Exp $ +#------------------------------------------------------------------------- +# +# Makefile-- +# Makefile for utils/misc +# +# IDENTIFICATION +# $PostgreSQL: pgsql/src/backend/utils/misc/Makefile,v 1.23 2005/02/26 18:43:33 tgl Exp $ +# +#------------------------------------------------------------------------- subdir = src/backend/utils/misc top_builddir = ../../../.. @@ -6,10 +14,10 @@ include $(top_builddir)/src/Makefile.global override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) -OBJS = database.o superuser.o guc.o help_config.o ps_status.o +OBJS = guc.o help_config.o ps_status.o superuser.o # This location might depend on the installation directories. Therefore -# we can't subsitute it into config.h. +# we can't subsitute it into pg_config.h. ifdef krb_srvtab override CPPFLAGS += -DPG_KRB_SRVTAB='"$(krb_srvtab)"' endif diff --git a/src/backend/utils/misc/database.c b/src/backend/utils/misc/database.c deleted file mode 100644 index 66ea6dbed7..0000000000 --- a/src/backend/utils/misc/database.c +++ /dev/null @@ -1,189 +0,0 @@ -/*------------------------------------------------------------------------- - * - * database.c - * miscellaneous initialization support stuff - * - * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * - * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/misc/database.c,v 1.63 2004/12/31 22:02:45 pgsql Exp $ - * - *------------------------------------------------------------------------- - */ -#include "postgres.h" - -#include <fcntl.h> -#include <unistd.h> - -#include "access/xact.h" -#include "catalog/catname.h" -#include "catalog/catalog.h" -#include "catalog/pg_database.h" -#include "catalog/pg_tablespace.h" -#include "miscadmin.h" -#include "utils/syscache.h" - - -static bool PhonyHeapTupleSatisfiesNow(HeapTupleHeader tuple); - - -/* -------------------------------- - * GetRawDatabaseInfo() -- Find the OID and tablespace of the database. - * - * We need both the OID and the default tablespace in order to find - * the database's system catalogs. Moreover the database's OID forms - * half of the unique key for the system caches and lock tables, so - * we must have it before we can use any of the cache mechanisms. - * To get around these problems, this code opens and scans the - * pg_database relation by hand. - * - * This code knows way more than it should about the layout of - * tuples on disk, but there seems to be no help for that. - * We're pulling ourselves up by the bootstraps here... - * -------------------------------- - */ -void -GetRawDatabaseInfo(const char *name, Oid *db_id, Oid *db_tablespace) -{ - int dbfd; - int nbytes; - HeapTupleData tup; - Form_pg_database tup_db; - Page pg; - char *dbfname; - RelFileNode rnode; - - /* hard-wired path to pg_database */ - rnode.spcNode = GLOBALTABLESPACE_OID; - rnode.dbNode = 0; - rnode.relNode = RelOid_pg_database; - - dbfname = relpath(rnode); - - if ((dbfd = open(dbfname, O_RDONLY | PG_BINARY, 0)) < 0) - ereport(FATAL, - (errcode_for_file_access(), - errmsg("could not open file \"%s\": %m", dbfname))); - - pfree(dbfname); - - /* - * read and examine every page in pg_database - * - * Raw I/O! Read those tuples the hard way! Yow! - * - * Why don't we use the access methods or move this code someplace else? - * This is really pg_database schema dependent code. Perhaps it - * should go in lib/catalog/pg_database? -cim 10/3/90 - * - * mao replies 4 apr 91: yeah, maybe this should be moved to - * lib/catalog. however, we CANNOT use the access methods since those - * use the buffer cache, which uses the relation cache, which requires - * that the dbid be set, which is what we're trying to do here. - * - */ - pg = (Page) palloc(BLCKSZ); - - while ((nbytes = read(dbfd, pg, BLCKSZ)) == BLCKSZ) - { - OffsetNumber max = PageGetMaxOffsetNumber(pg); - OffsetNumber lineoff; - - /* look at each tuple on the page */ - for (lineoff = FirstOffsetNumber; lineoff <= max; lineoff++) - { - ItemId lpp = PageGetItemId(pg, lineoff); - - /* if it's a freed tuple, ignore it */ - if (!ItemIdIsUsed(lpp)) - continue; - - /* get a pointer to the tuple itself */ - tup.t_datamcxt = NULL; - tup.t_data = (HeapTupleHeader) PageGetItem(pg, lpp); - - /* - * Check to see if tuple is valid (committed). - * - * XXX warning, will robinson: violation of transaction semantics - * happens right here. We cannot really determine if the - * tuple is valid without checking transaction commit status, - * and the only way to do that at init time is to paw over - * pg_clog by hand, too. Instead of checking, we assume that - * the inserting transaction committed, and that any deleting - * transaction did also, unless shown otherwise by on-row - * commit status bits. - * - * All in all, this code is pretty shaky. We will cross-check - * our result in ReverifyMyDatabase() in postinit.c. - * - * NOTE: if a bogus tuple in pg_database prevents connection to a - * valid database, a fix is to connect to another database and - * do "select * from pg_database". That should cause - * committed and dead tuples to be marked with correct states. - * - * XXX wouldn't it be better to let new backends read the - * database info from a flat file, handled the same way we - * handle the password relation? - */ - if (!PhonyHeapTupleSatisfiesNow(tup.t_data)) - continue; - - /* - * Okay, see if this is the one we want. - */ - tup_db = (Form_pg_database) GETSTRUCT(&tup); - - if (strcmp(name, NameStr(tup_db->datname)) == 0) - { - /* Found it; extract the db's OID and tablespace. */ - *db_id = HeapTupleGetOid(&tup); - *db_tablespace = tup_db->dattablespace; - goto done; - } - } - } - - /* failed to find it... */ - *db_id = InvalidOid; - *db_tablespace = InvalidOid; - -done: - close(dbfd); - pfree(pg); -} - -/* - * PhonyHeapTupleSatisfiesNow --- cut-down tuple time qual test - * - * This is a simplified version of HeapTupleSatisfiesNow() that does not - * depend on having transaction commit info available. Any transaction - * that touched the tuple is assumed committed unless later marked invalid. - * (While we could think about more complex rules, this seems appropriate - * for examining pg_database, since both CREATE DATABASE and DROP DATABASE - * are non-roll-back-able.) - */ -static bool -PhonyHeapTupleSatisfiesNow(HeapTupleHeader tuple) -{ - if (!(tuple->t_infomask & HEAP_XMIN_COMMITTED)) - { - if (tuple->t_infomask & HEAP_XMIN_INVALID) - return false; - - if (tuple->t_infomask & HEAP_MOVED_OFF) - return false; - /* else assume committed */ - } - - if (tuple->t_infomask & HEAP_XMAX_INVALID) /* xid invalid or aborted */ - return true; - - /* assume xmax transaction committed */ - if (tuple->t_infomask & HEAP_MARKED_FOR_UPDATE) - return true; - - return false; -} |
