1 /*-------------------------------------------------------------------------
4 * postgres initialization utilities
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.61 2000/07/02 15:20:56 petere Exp $
14 *-------------------------------------------------------------------------
18 #include <sys/types.h>
24 #include "access/heapam.h"
25 #include "catalog/catname.h"
26 #include "catalog/pg_database.h"
27 #include "libpq/libpq.h"
28 #include "miscadmin.h"
29 #include "storage/backendid.h"
30 #include "storage/proc.h"
31 #include "storage/sinval.h"
32 #include "storage/smgr.h"
33 #include "utils/fmgroids.h"
34 #include "utils/portal.h"
35 #include "utils/relcache.h"
36 #include "utils/syscache.h"
39 #include "mb/pg_wchar.h"
42 static void ReverifyMyDatabase(const char *name);
43 static void InitCommunication(void);
45 static IPCKey PostgresIpcKey;
47 /*** InitPostgres support ***/
50 /* --------------------------------
53 * Since we are forced to fetch the database OID out of pg_database without
54 * benefit of locking or transaction ID checking (see utils/misc/database.c),
55 * we might have gotten a wrong answer. Or, we might have attached to a
56 * database that's in process of being destroyed by destroydb(). This
57 * routine is called after we have all the locking and other infrastructure
58 * running --- now we can check that we are really attached to a valid
61 * In reality, if destroydb() is running in parallel with our startup,
62 * it's pretty likely that we will have failed before now, due to being
63 * unable to read some of the system tables within the doomed database.
64 * This routine just exists to make *sure* we have not started up in an
65 * invalid database. If we quit now, we should have managed to avoid
66 * creating any serious problems.
68 * This is also a handy place to fetch the database encoding info out
69 * of pg_database, if we are in MULTIBYTE mode.
70 * --------------------------------
73 ReverifyMyDatabase(const char *name)
76 HeapScanDesc pgdbscan;
81 * Because we grab AccessShareLock here, we can be sure that destroydb
82 * is not running in parallel with us (any more).
84 pgdbrel = heap_openr(DatabaseRelationName, AccessShareLock);
86 ScanKeyEntryInitialize(&key, 0, Anum_pg_database_datname,
87 F_NAMEEQ, NameGetDatum(name));
89 pgdbscan = heap_beginscan(pgdbrel, 0, SnapshotNow, 1, &key);
91 tup = heap_getnext(pgdbscan, 0);
92 if (!HeapTupleIsValid(tup) ||
93 tup->t_data->t_oid != MyDatabaseId)
96 heap_close(pgdbrel, AccessShareLock);
99 * The only real problem I could have created is to load dirty
100 * buffers for the dead database into shared buffer cache; if I
101 * did, some other backend will eventually try to write them and
102 * die in mdblindwrt. Flush any such pages to forestall trouble.
104 DropBuffers(MyDatabaseId);
105 /* Now I can commit hara-kiri with a clear conscience... */
106 elog(FATAL, "Database '%s', OID %u, has disappeared from pg_database",
111 * OK, we're golden. Only other to-do item is to save the MULTIBYTE
112 * encoding info out of the pg_database tuple. Note we also set the
113 * "template encoding", which is the default encoding for any CREATE
114 * DATABASE commands executed in this backend; essentially, you get
115 * the same encoding of the database you connected to as the default.
116 * (This replaces code that unreliably grabbed template1's encoding
117 * out of pg_database. We could do an extra scan to find template1's
118 * tuple, but for 99.99% of all backend startups it'd be wasted cycles
119 * --- and the 'createdb' script connects to template1 anyway, so
120 * there's no difference.)
123 SetDatabaseEncoding(((Form_pg_database) GETSTRUCT(tup))->encoding);
124 SetTemplateEncoding(((Form_pg_database) GETSTRUCT(tup))->encoding);
127 heap_endscan(pgdbscan);
128 heap_close(pgdbrel, AccessShareLock);
133 /* --------------------------------
136 * This routine initializes stuff needed for ipc, locking, etc.
137 * it should be called something more informative.
140 * This does not set MyBackendId. MyBackendTag is set, however.
141 * --------------------------------
146 char *postid; /* value of environment variable */
147 char *postport; /* value of environment variable */
148 char *ipc_key; /* value of environemnt variable */
152 * try and get the backend tag from POSTID
157 postid = getenv("POSTID");
158 if (!PointerIsValid(postid))
162 MyBackendTag = atoi(postid);
163 Assert(MyBackendTag >= 0);
167 ipc_key = getenv("IPC_KEY");
168 if (!PointerIsValid(ipc_key))
173 Assert(MyBackendTag >= 0);
176 postport = getenv("POSTPORT");
178 if (PointerIsValid(postport))
180 if (MyBackendTag == -1)
181 elog(FATAL, "InitCommunication: missing POSTID");
183 else if (IsUnderPostmaster)
186 "InitCommunication: under postmaster and POSTPORT not set");
191 * assume we're running a postgres backend by itself with
192 * no front end or postmaster.
195 if (MyBackendTag == -1)
202 * initialize shared memory and semaphores appropriately.
205 if (!IsUnderPostmaster) /* postmaster already did this */
207 PostgresIpcKey = key;
208 AttachSharedMemoryAndSemaphores(key);
214 /* --------------------------------
216 * Initialize POSTGRES.
219 * Be very careful with the order of calls in the InitPostgres function.
220 * --------------------------------
222 int lockingOff = 0; /* backend -L switch */
227 InitPostgres(const char *dbname)
229 bool bootstrap = IsBootstrapProcessingMode();
232 * initialize the backend local portal stack used by
233 * internal PQ function calls. see src/lib/libpq/be-dumpdata.c
234 * This is different from the "portal manager" so this goes here.
240 /* initialize the local buffer manager */
244 if (!TransactionFlushEnabled())
245 on_shmem_exit(FlushBufferPool, (caddr_t) NULL);
248 SetDatabaseName(dbname);
250 * initialize the database id used for system caches and lock tables
255 SetDatabasePath(ExpandDatabasePath(dbname));
264 /* Verify if DataDir is ok */
265 if (access(DataDir, F_OK) == -1)
266 elog(FATAL, "Database system not found. Data directory '%s' does not exist.",
269 ValidatePgVersion(DataDir);
272 * Find oid and path of the database we're about to open. Since we're
273 * not yet up and running we have to use the hackish GetRawDatabaseInfo.
276 * The database's oid forms half of the unique key for the system
277 * caches and lock tables. We therefore want it initialized before
278 * we open any relations, since opening relations puts things in the
279 * cache. To get around this problem, this code opens and scans the
280 * pg_database relation by hand.
283 GetRawDatabaseInfo(dbname, &MyDatabaseId, datpath);
285 if (!OidIsValid(MyDatabaseId))
287 "Database \"%s\" does not exist in the system catalog.",
290 fullpath = ExpandDatabasePath(datpath);
292 elog(FATAL, "Database path could not be resolved.");
294 /* Verify the database path */
296 if (access(fullpath, F_OK) == -1)
297 elog(FATAL, "Database \"%s\" does not exist. The data directory '%s' is missing.",
300 ValidatePgVersion(fullpath);
302 if (chdir(fullpath) == -1)
303 elog(FATAL, "Unable to change directory to '%s': %s", fullpath, strerror(errno));
305 SetDatabasePath(fullpath);
309 * Code after this point assumes we are in the proper directory!
313 * Initialize the transaction system and the relation descriptor
314 * cache. Note we have to make certain the lock manager is off while
317 AmiTransactionOverride(IsBootstrapProcessingMode());
321 * Part of the initialization processing done here sets a read lock on
322 * pg_log. Since locking is disabled the set doesn't have intended
323 * effect of locking out writers, but this is ok, since we only lock
324 * it to examine AMI transaction status, and this is never written
325 * after initdb is done. -mer 15 June 1992
327 RelationInitialize(); /* pre-allocated reldescs created here */
328 InitializeTransactionSystem(); /* pg_log,etc init/crash recovery
334 * Set up my per-backend PROC struct in shared memory.
336 InitProcess(PostgresIpcKey);
339 * Initialize my entry in the shared-invalidation manager's array of
340 * per-backend data. (Formerly this came before InitProcess, but now
341 * it must happen after, because it uses MyProc.) Once I have done
342 * this, I am visible to other backends!
344 * Sets up MyBackendId, a unique backend identifier.
346 InitSharedInvalidationState();
348 if (MyBackendId > MAXBACKENDS || MyBackendId <= 0)
350 elog(FATAL, "cinit2: bad backend id %d (%d)",
356 * Initialize the access methods. Does not touch files (?) - thomas
362 * Initialize all the system catalog caches.
367 * Does not touch files since all routines are builtins (?) - thomas
372 /* start a new transaction here before access to db */
374 StartTransactionCommand();
377 * Set ourselves to the proper user id and figure out our postgres
378 * user id. If we ever add security so that we check for valid
379 * postgres users, we might do it here.
388 * Unless we are bootstrapping, double-check that InitMyDatabaseInfo()
389 * got a correct result. We can't do this until essentially all the
390 * infrastructure is up, so just do it at the end.
393 ReverifyMyDatabase(dbname);
400 * Attach to shared memory and semaphores, and initialize our
401 * input/output/debugging file descriptors.
407 EnablePortalManager(); /* memory for portal/transaction stuff */