1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-15 03:41:20 +03:00

Extend CREATE DATABASE to allow selection of a template database to be

cloned, rather than always cloning template1.  Modify initdb to generate
two identical databases rather than one, template0 and template1.
Connections to template0 are disallowed, so that it will always remain
in its virgin as-initdb'd state.  pg_dumpall now dumps databases with
restore commands that say CREATE DATABASE foo WITH TEMPLATE = template0.
This allows proper behavior when there is user-added data in template1.
initdb forced!
This commit is contained in:
Tom Lane
2000-11-14 18:37:49 +00:00
parent 8a9315ca92
commit 2cf48ca04b
23 changed files with 515 additions and 311 deletions

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.70 2000/11/12 20:51:52 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.71 2000/11/14 18:37:44 tgl Exp $
*
*
*-------------------------------------------------------------------------
@@ -79,6 +79,7 @@ ReverifyMyDatabase(const char *name)
HeapScanDesc pgdbscan;
ScanKeyData key;
HeapTuple tup;
Form_pg_database dbform;
/*
* Because we grab AccessShareLock here, we can be sure that destroydb
@@ -106,25 +107,24 @@ ReverifyMyDatabase(const char *name)
*/
DropBuffers(MyDatabaseId);
/* Now I can commit hara-kiri with a clear conscience... */
elog(FATAL, "Database '%s', OID %u, has disappeared from pg_database",
elog(FATAL, "Database \"%s\", OID %u, has disappeared from pg_database",
name, MyDatabaseId);
}
/*
* Also check that the database is currently allowing connections.
*/
dbform = (Form_pg_database) GETSTRUCT(tup);
if (! dbform->datallowconn)
elog(FATAL, "Database \"%s\" is not currently accepting connections",
name);
/*
* OK, we're golden. Only other to-do item is to save the MULTIBYTE
* encoding info out of the pg_database tuple. Note we also set the
* "template encoding", which is the default encoding for any CREATE
* DATABASE commands executed in this backend; essentially, you get
* the same encoding of the database you connected to as the default.
* (This replaces code that unreliably grabbed template1's encoding
* out of pg_database. We could do an extra scan to find template1's
* tuple, but for 99.99% of all backend startups it'd be wasted cycles
* --- and the 'createdb' script connects to template1 anyway, so
* there's no difference.)
* encoding info out of the pg_database tuple.
*/
#ifdef MULTIBYTE
SetDatabaseEncoding(((Form_pg_database) GETSTRUCT(tup))->encoding);
SetTemplateEncoding(((Form_pg_database) GETSTRUCT(tup))->encoding);
SetDatabaseEncoding(dbform->encoding);
#endif
heap_endscan(pgdbscan);

View File

@@ -3,7 +3,7 @@
* client encoding and server internal encoding.
* (currently mule internal code (mic) is used)
* Tatsuo Ishii
* $Id: mbutils.c,v 1.13 2000/10/30 10:40:28 ishii Exp $ */
* $Id: mbutils.c,v 1.14 2000/11/14 18:37:44 tgl Exp $ */
#include "postgres.h"
@@ -271,6 +271,7 @@ pg_mbcliplen(const unsigned char *mbstr, int len, int limit)
* fuctions for utils/init
*/
static int DatabaseEncoding = MULTIBYTE;
void
SetDatabaseEncoding(int encoding)
{
@@ -289,17 +290,3 @@ getdatabaseencoding(PG_FUNCTION_ARGS)
{
PG_RETURN_NAME(pg_encoding_to_char(DatabaseEncoding));
}
/* set and get template1 database encoding */
static int templateEncoding;
void
SetTemplateEncoding(int encoding)
{
templateEncoding = encoding;
}
int
GetTemplateEncoding()
{
return (templateEncoding);
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/misc/Attic/database.c,v 1.40 2000/10/16 14:52:19 vadim Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/misc/Attic/database.c,v 1.41 2000/11/14 18:37:45 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -186,7 +186,7 @@ GetRawDatabaseInfo(const char *name, Oid *db_id, char *path)
max = PageGetMaxOffsetNumber(pg);
/* look at each tuple on the page */
for (i = 0; i <= max; i++)
for (i = 0; i < max; i++)
{
int offset;
@@ -221,8 +221,11 @@ GetRawDatabaseInfo(const char *name, Oid *db_id, char *path)
* database OID from a flat file, handled the same way we
* handle the password relation?
*/
if (TransactionIdIsValid((TransactionId) tup.t_data->t_xmax))
continue;
if (tup.t_data->t_infomask & HEAP_XMIN_INVALID)
continue; /* inserting xact known aborted */
if (TransactionIdIsValid((TransactionId) tup.t_data->t_xmax) &&
!(tup.t_data->t_infomask & HEAP_XMAX_INVALID))
continue; /* deleting xact happened, not known aborted */
/*
* Okay, see if this is the one we want.
@@ -241,6 +244,10 @@ GetRawDatabaseInfo(const char *name, Oid *db_id, char *path)
}
}
/* failed to find it... */
*db_id = InvalidOid;
*path = '\0';
done:
close(dbfd);
pfree(pg);