1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* dbcommands.c
|
2001-06-12 07:55:50 +02:00
|
|
|
* Database management commands (create/drop database).
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2005-02-26 19:43:34 +01:00
|
|
|
* Note: database creation/destruction commands take ExclusiveLock on
|
|
|
|
* pg_database to ensure that no two proceed in parallel. We must use
|
|
|
|
* at least this level of locking to ensure that no two backends try to
|
|
|
|
* write the flat-file copy of pg_database at once. We avoid using
|
|
|
|
* AccessExclusiveLock since there's no need to lock out ordinary readers
|
|
|
|
* of pg_database.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2005-11-22 19:17:34 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.175 2005/11/22 18:17:08 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2000-01-13 19:26:18 +01:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
1998-04-27 06:08:07 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "access/genam.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "access/heapam.h"
|
2000-10-16 16:52:28 +02:00
|
|
|
#include "catalog/catalog.h"
|
2005-07-07 22:40:02 +02:00
|
|
|
#include "catalog/dependency.h"
|
2005-06-28 07:09:14 +02:00
|
|
|
#include "catalog/indexing.h"
|
|
|
|
#include "catalog/pg_authid.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "catalog/pg_database.h"
|
2004-06-18 08:14:31 +02:00
|
|
|
#include "catalog/pg_tablespace.h"
|
1999-10-26 05:12:39 +02:00
|
|
|
#include "commands/comment.h"
|
2000-11-14 19:37:49 +01:00
|
|
|
#include "commands/dbcommands.h"
|
2004-06-18 08:14:31 +02:00
|
|
|
#include "commands/tablespace.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "miscadmin.h"
|
2004-10-28 02:39:59 +02:00
|
|
|
#include "postmaster/bgwriter.h"
|
2004-02-10 02:55:27 +01:00
|
|
|
#include "storage/fd.h"
|
2001-07-02 22:50:46 +02:00
|
|
|
#include "storage/freespace.h"
|
2005-05-19 23:35:48 +02:00
|
|
|
#include "storage/procarray.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "utils/acl.h"
|
2002-03-01 23:45:19 +01:00
|
|
|
#include "utils/array.h"
|
2000-01-13 19:26:18 +01:00
|
|
|
#include "utils/builtins.h"
|
2005-02-20 03:22:07 +01:00
|
|
|
#include "utils/flatfiles.h"
|
2000-05-28 19:56:29 +02:00
|
|
|
#include "utils/fmgroids.h"
|
2002-03-01 23:45:19 +01:00
|
|
|
#include "utils/guc.h"
|
2002-02-24 21:20:21 +01:00
|
|
|
#include "utils/lsyscache.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "utils/syscache.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* non-export function prototypes */
|
2005-06-28 07:09:14 +02:00
|
|
|
static bool get_db_info(const char *name, Oid *dbIdP, Oid *ownerIdP,
|
2005-03-12 22:33:55 +01:00
|
|
|
int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP,
|
|
|
|
Oid *dbLastSysOidP,
|
2001-08-26 18:56:03 +02:00
|
|
|
TransactionId *dbVacuumXidP, TransactionId *dbFrozenXidP,
|
2004-06-18 08:14:31 +02:00
|
|
|
Oid *dbTablespace);
|
2002-04-11 07:32:03 +02:00
|
|
|
static bool have_createdb_privilege(void);
|
2004-06-18 08:14:31 +02:00
|
|
|
static void remove_dbtablespaces(Oid db_id);
|
|
|
|
|
2000-01-13 19:26:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CREATE DATABASE
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
2002-06-20 18:00:44 +02:00
|
|
|
createdb(const CreatedbStmt *stmt)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-06-18 08:14:31 +02:00
|
|
|
HeapScanDesc scan;
|
|
|
|
Relation rel;
|
2000-11-14 19:37:49 +01:00
|
|
|
Oid src_dboid;
|
2005-06-28 07:09:14 +02:00
|
|
|
Oid src_owner;
|
2000-11-14 19:37:49 +01:00
|
|
|
int src_encoding;
|
|
|
|
bool src_istemplate;
|
2005-03-12 22:33:55 +01:00
|
|
|
bool src_allowconn;
|
2000-11-14 19:37:49 +01:00
|
|
|
Oid src_lastsysoid;
|
2001-08-26 18:56:03 +02:00
|
|
|
TransactionId src_vacuumxid;
|
|
|
|
TransactionId src_frozenxid;
|
2004-06-18 08:14:31 +02:00
|
|
|
Oid src_deftablespace;
|
2005-08-02 21:02:32 +02:00
|
|
|
volatile Oid dst_deftablespace;
|
2005-08-12 03:36:05 +02:00
|
|
|
volatile Relation pg_database_rel;
|
2000-01-13 19:26:18 +01:00
|
|
|
HeapTuple tuple;
|
2000-04-12 19:17:23 +02:00
|
|
|
TupleDesc pg_database_dsc;
|
|
|
|
Datum new_record[Natts_pg_database];
|
2000-11-14 19:37:49 +01:00
|
|
|
char new_record_nulls[Natts_pg_database];
|
2000-10-22 19:55:49 +02:00
|
|
|
Oid dboid;
|
2005-08-02 21:02:32 +02:00
|
|
|
volatile Oid datdba;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *option;
|
2004-08-29 07:07:03 +02:00
|
|
|
DefElem *dtablespacename = NULL;
|
2002-06-18 19:27:58 +02:00
|
|
|
DefElem *downer = NULL;
|
2002-09-04 22:31:48 +02:00
|
|
|
DefElem *dtemplate = NULL;
|
|
|
|
DefElem *dencoding = NULL;
|
2005-07-31 19:19:22 +02:00
|
|
|
DefElem *dconnlimit = NULL;
|
2002-06-18 19:27:58 +02:00
|
|
|
char *dbname = stmt->dbname;
|
|
|
|
char *dbowner = NULL;
|
2005-06-21 06:02:34 +02:00
|
|
|
const char *dbtemplate = NULL;
|
2005-08-02 21:02:32 +02:00
|
|
|
volatile int encoding = -1;
|
|
|
|
volatile int dbconnlimit = -1;
|
2004-06-18 08:14:31 +02:00
|
|
|
|
|
|
|
/* don't call this in a transaction block */
|
|
|
|
PreventTransactionChain((void *) stmt, "CREATE DATABASE");
|
2002-06-18 19:27:58 +02:00
|
|
|
|
|
|
|
/* Extract options from the statement node tree */
|
|
|
|
foreach(option, stmt->options)
|
|
|
|
{
|
|
|
|
DefElem *defel = (DefElem *) lfirst(option);
|
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
if (strcmp(defel->defname, "tablespace") == 0)
|
2002-06-18 19:27:58 +02:00
|
|
|
{
|
2004-06-18 08:14:31 +02:00
|
|
|
if (dtablespacename)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
2004-06-18 08:14:31 +02:00
|
|
|
dtablespacename = defel;
|
2002-06-18 19:27:58 +02:00
|
|
|
}
|
2004-06-18 08:14:31 +02:00
|
|
|
else if (strcmp(defel->defname, "owner") == 0)
|
2002-06-18 19:27:58 +02:00
|
|
|
{
|
2004-06-18 08:14:31 +02:00
|
|
|
if (downer)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
2004-06-18 08:14:31 +02:00
|
|
|
downer = defel;
|
2002-06-18 19:27:58 +02:00
|
|
|
}
|
|
|
|
else if (strcmp(defel->defname, "template") == 0)
|
|
|
|
{
|
|
|
|
if (dtemplate)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
2002-06-18 19:27:58 +02:00
|
|
|
dtemplate = defel;
|
|
|
|
}
|
|
|
|
else if (strcmp(defel->defname, "encoding") == 0)
|
|
|
|
{
|
|
|
|
if (dencoding)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
2002-06-18 19:27:58 +02:00
|
|
|
dencoding = defel;
|
|
|
|
}
|
2005-07-31 19:19:22 +02:00
|
|
|
else if (strcmp(defel->defname, "connectionlimit") == 0)
|
|
|
|
{
|
|
|
|
if (dconnlimit)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
|
|
|
dconnlimit = defel;
|
|
|
|
}
|
2004-06-18 08:14:31 +02:00
|
|
|
else if (strcmp(defel->defname, "location") == 0)
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("LOCATION is not supported anymore"),
|
|
|
|
errhint("Consider using tablespaces instead.")));
|
|
|
|
}
|
2002-06-18 19:27:58 +02:00
|
|
|
else
|
2003-07-19 01:20:33 +02:00
|
|
|
elog(ERROR, "option \"%s\" not recognized",
|
2002-06-18 19:27:58 +02:00
|
|
|
defel->defname);
|
|
|
|
}
|
|
|
|
|
2002-11-02 19:41:22 +01:00
|
|
|
if (downer && downer->arg)
|
2002-06-18 19:27:58 +02:00
|
|
|
dbowner = strVal(downer->arg);
|
2002-11-02 19:41:22 +01:00
|
|
|
if (dtemplate && dtemplate->arg)
|
2002-06-18 19:27:58 +02:00
|
|
|
dbtemplate = strVal(dtemplate->arg);
|
2002-11-02 19:41:22 +01:00
|
|
|
if (dencoding && dencoding->arg)
|
|
|
|
{
|
|
|
|
const char *encoding_name;
|
|
|
|
|
|
|
|
if (IsA(dencoding->arg, Integer))
|
|
|
|
{
|
|
|
|
encoding = intVal(dencoding->arg);
|
|
|
|
encoding_name = pg_encoding_to_char(encoding);
|
|
|
|
if (strcmp(encoding_name, "") == 0 ||
|
|
|
|
pg_valid_server_encoding(encoding_name) < 0)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("%d is not a valid encoding code",
|
|
|
|
encoding)));
|
2002-11-02 19:41:22 +01:00
|
|
|
}
|
|
|
|
else if (IsA(dencoding->arg, String))
|
|
|
|
{
|
|
|
|
encoding_name = strVal(dencoding->arg);
|
|
|
|
if (pg_valid_server_encoding(encoding_name) < 0)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("%s is not a valid encoding name",
|
|
|
|
encoding_name)));
|
2002-11-02 19:41:22 +01:00
|
|
|
encoding = pg_char_to_encoding(encoding_name);
|
|
|
|
}
|
|
|
|
else
|
2003-07-19 01:20:33 +02:00
|
|
|
elog(ERROR, "unrecognized node type: %d",
|
|
|
|
nodeTag(dencoding->arg));
|
2002-11-02 19:41:22 +01:00
|
|
|
}
|
2005-07-31 19:19:22 +02:00
|
|
|
if (dconnlimit && dconnlimit->arg)
|
|
|
|
dbconnlimit = intVal(dconnlimit->arg);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
/* obtain OID of proposed owner */
|
2002-02-24 21:20:21 +01:00
|
|
|
if (dbowner)
|
2005-06-28 07:09:14 +02:00
|
|
|
datdba = get_roleid_checked(dbowner);
|
2002-02-24 21:20:21 +01:00
|
|
|
else
|
|
|
|
datdba = GetUserId();
|
|
|
|
|
2005-07-14 23:46:30 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* To create a database, must have createdb privilege and must be able to
|
|
|
|
* become the target role (this does not imply that the target role itself
|
|
|
|
* must have createdb privilege). The latter provision guards against
|
|
|
|
* "giveaway" attacks. Note that a superuser will always have both of
|
|
|
|
* these privileges a fortiori.
|
2005-07-14 23:46:30 +02:00
|
|
|
*/
|
|
|
|
if (!have_createdb_privilege())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied to create database")));
|
|
|
|
|
|
|
|
check_is_member_of_role(GetUserId(), datdba);
|
2000-01-13 19:26:18 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Check for db name conflict. There is a race condition here, since
|
2000-11-14 19:37:49 +01:00
|
|
|
* another backend could create the same DB name before we commit.
|
2005-10-15 04:49:52 +02:00
|
|
|
* However, holding an exclusive lock on pg_database for the whole time we
|
|
|
|
* are copying the source database doesn't seem like a good idea, so
|
|
|
|
* accept possibility of race to create. We will check again after we
|
|
|
|
* grab the exclusive lock.
|
2000-04-12 19:17:23 +02:00
|
|
|
*/
|
2005-03-12 22:33:55 +01:00
|
|
|
if (get_db_info(dbname, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL))
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_DATABASE),
|
|
|
|
errmsg("database \"%s\" already exists", dbname)));
|
2000-01-13 19:26:18 +01:00
|
|
|
|
2000-11-14 19:37:49 +01:00
|
|
|
/*
|
|
|
|
* Lookup database (template) to be cloned.
|
2000-10-22 19:55:49 +02:00
|
|
|
*/
|
2000-11-14 19:37:49 +01:00
|
|
|
if (!dbtemplate)
|
2001-03-22 05:01:46 +01:00
|
|
|
dbtemplate = "template1"; /* Default template database name */
|
2000-10-22 19:55:49 +02:00
|
|
|
|
2000-11-14 19:37:49 +01:00
|
|
|
if (!get_db_info(dbtemplate, &src_dboid, &src_owner, &src_encoding,
|
2005-03-12 22:33:55 +01:00
|
|
|
&src_istemplate, &src_allowconn, &src_lastsysoid,
|
2004-06-18 08:14:31 +02:00
|
|
|
&src_vacuumxid, &src_frozenxid, &src_deftablespace))
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("template database \"%s\" does not exist", dbtemplate)));
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-11-14 19:37:49 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Permission check: to copy a DB that's not marked datistemplate, you
|
|
|
|
* must be superuser or the owner thereof.
|
2000-11-14 19:37:49 +01:00
|
|
|
*/
|
|
|
|
if (!src_istemplate)
|
|
|
|
{
|
2005-06-28 07:09:14 +02:00
|
|
|
if (!pg_database_ownercheck(src_dboid, GetUserId()))
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2003-08-01 02:15:26 +02:00
|
|
|
errmsg("permission denied to copy database \"%s\"",
|
2003-07-19 01:20:33 +02:00
|
|
|
dbtemplate)));
|
2000-11-14 19:37:49 +01:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-11-14 19:37:49 +01:00
|
|
|
/*
|
|
|
|
* The source DB can't have any active backends, except this one
|
|
|
|
* (exception is to allow CREATE DB while connected to template1).
|
|
|
|
* Otherwise we might copy inconsistent data. This check is not
|
|
|
|
* bulletproof, since someone might connect while we are copying...
|
|
|
|
*/
|
|
|
|
if (DatabaseHasActiveBackends(src_dboid, true))
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_IN_USE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("source database \"%s\" is being accessed by other users",
|
|
|
|
dbtemplate)));
|
2000-01-13 19:26:18 +01:00
|
|
|
|
2000-11-14 19:37:49 +01:00
|
|
|
/* If encoding is defaulted, use source's encoding */
|
|
|
|
if (encoding < 0)
|
|
|
|
encoding = src_encoding;
|
2000-10-22 19:55:49 +02:00
|
|
|
|
Commit Karel's patch.
-------------------------------------------------------------------
Subject: Re: [PATCHES] encoding names
From: Karel Zak <zakkr@zf.jcu.cz>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-patches <pgsql-patches@postgresql.org>
Date: Fri, 31 Aug 2001 17:24:38 +0200
On Thu, Aug 30, 2001 at 01:30:40AM +0200, Peter Eisentraut wrote:
> > - convert encoding 'name' to 'id'
>
> I thought we decided not to add functions returning "new" names until we
> know exactly what the new names should be, and pending schema
Ok, the patch not to add functions.
> better
>
> ...(): encoding name too long
Fixed.
I found new bug in command/variable.c in parse_client_encoding(), nobody
probably never see this error:
if (pg_set_client_encoding(encoding))
{
elog(ERROR, "Conversion between %s and %s is not supported",
value, GetDatabaseEncodingName());
}
because pg_set_client_encoding() returns -1 for error and 0 as true.
It's fixed too.
IMHO it can be apply.
Karel
PS:
* following files are renamed:
src/utils/mb/Unicode/KOI8_to_utf8.map -->
src/utils/mb/Unicode/koi8r_to_utf8.map
src/utils/mb/Unicode/WIN_to_utf8.map -->
src/utils/mb/Unicode/win1251_to_utf8.map
src/utils/mb/Unicode/utf8_to_KOI8.map -->
src/utils/mb/Unicode/utf8_to_koi8r.map
src/utils/mb/Unicode/utf8_to_WIN.map -->
src/utils/mb/Unicode/utf8_to_win1251.map
* new file:
src/utils/mb/encname.c
* removed file:
src/utils/mb/common.c
--
Karel Zak <zakkr@zf.jcu.cz>
http://home.zf.jcu.cz/~zakkr/
C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz
2001-09-06 06:57:30 +02:00
|
|
|
/* Some encodings are client only */
|
2001-10-25 07:50:21 +02:00
|
|
|
if (!PG_VALID_BE_ENCODING(encoding))
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("invalid server encoding %d", encoding)));
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
/* Resolve default tablespace for new database */
|
|
|
|
if (dtablespacename && dtablespacename->arg)
|
|
|
|
{
|
|
|
|
char *tablespacename;
|
2004-08-29 07:07:03 +02:00
|
|
|
AclResult aclresult;
|
2004-06-18 08:14:31 +02:00
|
|
|
|
|
|
|
tablespacename = strVal(dtablespacename->arg);
|
|
|
|
dst_deftablespace = get_tablespace_oid(tablespacename);
|
|
|
|
if (!OidIsValid(dst_deftablespace))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("tablespace \"%s\" does not exist",
|
|
|
|
tablespacename)));
|
|
|
|
/* check permissions */
|
2004-08-29 07:07:03 +02:00
|
|
|
aclresult = pg_tablespace_aclcheck(dst_deftablespace, GetUserId(),
|
2004-06-18 08:14:31 +02:00
|
|
|
ACL_CREATE);
|
2004-08-29 07:07:03 +02:00
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
|
|
|
|
tablespacename);
|
2004-10-17 22:47:21 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are trying to change the default tablespace of the template,
|
|
|
|
* we require that the template not have any files in the new default
|
2005-10-15 04:49:52 +02:00
|
|
|
* tablespace. This is necessary because otherwise the copied
|
2004-10-17 22:47:21 +02:00
|
|
|
* database would contain pg_class rows that refer to its default
|
|
|
|
* tablespace both explicitly (by OID) and implicitly (as zero), which
|
|
|
|
* would cause problems. For example another CREATE DATABASE using
|
|
|
|
* the copied database as template, and trying to change its default
|
|
|
|
* tablespace again, would yield outright incorrect results (it would
|
|
|
|
* improperly move tables to the new default tablespace that should
|
|
|
|
* stay in the same tablespace).
|
|
|
|
*/
|
|
|
|
if (dst_deftablespace != src_deftablespace)
|
|
|
|
{
|
|
|
|
char *srcpath;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
srcpath = GetDatabasePath(src_dboid, dst_deftablespace);
|
|
|
|
|
|
|
|
if (stat(srcpath, &st) == 0 &&
|
|
|
|
S_ISDIR(st.st_mode) &&
|
|
|
|
!directory_is_empty(srcpath))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot assign new default tablespace \"%s\"",
|
|
|
|
tablespacename),
|
|
|
|
errdetail("There is a conflict because database \"%s\" already has some tables in this tablespace.",
|
|
|
|
dbtemplate)));
|
|
|
|
pfree(srcpath);
|
|
|
|
}
|
2004-06-18 08:14:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use template database's default tablespace */
|
|
|
|
dst_deftablespace = src_deftablespace;
|
|
|
|
/* Note there is no additional permission check in this path */
|
|
|
|
}
|
|
|
|
|
2005-03-12 22:33:55 +01:00
|
|
|
/*
|
|
|
|
* Normally we mark the new database with the same datvacuumxid and
|
2005-10-15 04:49:52 +02:00
|
|
|
* datfrozenxid as the source. However, if the source is not allowing
|
2005-03-12 22:33:55 +01:00
|
|
|
* connections then we assume it is fully frozen, and we can set the
|
|
|
|
* current transaction ID as the xid limits. This avoids immediately
|
|
|
|
* starting to generate warnings after cloning template0.
|
|
|
|
*/
|
|
|
|
if (!src_allowconn)
|
|
|
|
src_vacuumxid = src_frozenxid = GetCurrentTransactionId();
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Preassign OID for pg_database tuple, so that we can compute db path. We
|
|
|
|
* have to open pg_database to do this, but we don't want to take
|
2005-10-15 04:49:52 +02:00
|
|
|
* ExclusiveLock yet, so just do it and close again.
|
2000-11-14 19:37:49 +01:00
|
|
|
*/
|
2005-08-12 03:36:05 +02:00
|
|
|
pg_database_rel = heap_open(DatabaseRelationId, AccessShareLock);
|
|
|
|
dboid = GetNewOid(pg_database_rel);
|
|
|
|
heap_close(pg_database_rel, AccessShareLock);
|
|
|
|
pg_database_rel = NULL;
|
2000-10-22 19:55:49 +02:00
|
|
|
|
2001-01-14 23:14:10 +01:00
|
|
|
/*
|
|
|
|
* Force dirty buffers out to disk, to ensure source database is
|
2005-10-15 04:49:52 +02:00
|
|
|
* up-to-date for the copy. (We really only need to flush buffers for the
|
|
|
|
* source database, but bufmgr.c provides no API for that.)
|
2000-11-18 04:36:48 +01:00
|
|
|
*/
|
2005-03-04 21:21:07 +01:00
|
|
|
BufferSync();
|
1998-08-24 03:14:24 +02:00
|
|
|
|
2000-06-02 06:04:54 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Once we start copying subdirectories, we need to be able to clean 'em
|
|
|
|
* up if we fail. Establish a TRY block to make sure this happens. (This
|
|
|
|
* is not a 100% solution, because of the possibility of failure during
|
|
|
|
* transaction commit after we leave this routine, but it should handle
|
|
|
|
* most scenarios.)
|
2000-06-02 06:04:54 +02:00
|
|
|
*/
|
2005-08-02 21:02:32 +02:00
|
|
|
PG_TRY();
|
2004-06-18 08:14:31 +02:00
|
|
|
{
|
2005-08-02 21:02:32 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Iterate through all tablespaces of the template database, and copy
|
|
|
|
* each one to the new database.
|
2005-08-02 21:02:32 +02:00
|
|
|
*/
|
|
|
|
rel = heap_open(TableSpaceRelationId, AccessShareLock);
|
|
|
|
scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
|
|
|
|
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
|
2004-06-18 08:14:31 +02:00
|
|
|
{
|
2005-08-02 21:02:32 +02:00
|
|
|
Oid srctablespace = HeapTupleGetOid(tuple);
|
|
|
|
Oid dsttablespace;
|
|
|
|
char *srcpath;
|
|
|
|
char *dstpath;
|
|
|
|
struct stat st;
|
2004-06-18 08:14:31 +02:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/* No need to copy global tablespace */
|
|
|
|
if (srctablespace == GLOBALTABLESPACE_OID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
srcpath = GetDatabasePath(src_dboid, srctablespace);
|
|
|
|
|
|
|
|
if (stat(srcpath, &st) < 0 || !S_ISDIR(st.st_mode) ||
|
|
|
|
directory_is_empty(srcpath))
|
|
|
|
{
|
|
|
|
/* Assume we can ignore it */
|
|
|
|
pfree(srcpath);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srctablespace == src_deftablespace)
|
|
|
|
dsttablespace = dst_deftablespace;
|
|
|
|
else
|
|
|
|
dsttablespace = srctablespace;
|
|
|
|
|
|
|
|
dstpath = GetDatabasePath(dboid, dsttablespace);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy this subdirectory to the new location
|
|
|
|
*
|
|
|
|
* We don't need to copy subdirectories
|
|
|
|
*/
|
|
|
|
copydir(srcpath, dstpath, false);
|
|
|
|
|
|
|
|
/* Record the filesystem change in XLOG */
|
|
|
|
{
|
|
|
|
xl_dbase_create_rec xlrec;
|
|
|
|
XLogRecData rdata[1];
|
|
|
|
|
|
|
|
xlrec.db_id = dboid;
|
|
|
|
xlrec.tablespace_id = dsttablespace;
|
|
|
|
xlrec.src_db_id = src_dboid;
|
|
|
|
xlrec.src_tablespace_id = srctablespace;
|
|
|
|
|
|
|
|
rdata[0].data = (char *) &xlrec;
|
|
|
|
rdata[0].len = sizeof(xl_dbase_create_rec);
|
|
|
|
rdata[0].buffer = InvalidBuffer;
|
|
|
|
rdata[0].next = NULL;
|
|
|
|
|
|
|
|
(void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_CREATE, rdata);
|
|
|
|
}
|
2004-06-18 08:14:31 +02:00
|
|
|
}
|
2005-08-02 21:02:32 +02:00
|
|
|
heap_endscan(scan);
|
|
|
|
heap_close(rel, AccessShareLock);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
/*
|
2005-08-02 21:02:32 +02:00
|
|
|
* Now OK to grab exclusive lock on pg_database.
|
2004-06-18 08:14:31 +02:00
|
|
|
*/
|
2005-08-02 21:02:32 +02:00
|
|
|
pg_database_rel = heap_open(DatabaseRelationId, ExclusiveLock);
|
2004-06-18 08:14:31 +02:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/* Check to see if someone else created same DB name meanwhile. */
|
|
|
|
if (get_db_info(dbname, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL))
|
2003-09-10 22:24:09 +02:00
|
|
|
ereport(ERROR,
|
2005-08-02 21:02:32 +02:00
|
|
|
(errcode(ERRCODE_DUPLICATE_DATABASE),
|
|
|
|
errmsg("database \"%s\" already exists", dbname)));
|
2005-03-23 01:03:37 +01:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/*
|
|
|
|
* Insert a new tuple into pg_database
|
|
|
|
*/
|
|
|
|
pg_database_dsc = RelationGetDescr(pg_database_rel);
|
|
|
|
|
|
|
|
/* Form tuple */
|
|
|
|
MemSet(new_record, 0, sizeof(new_record));
|
|
|
|
MemSet(new_record_nulls, ' ', sizeof(new_record_nulls));
|
|
|
|
|
|
|
|
new_record[Anum_pg_database_datname - 1] =
|
|
|
|
DirectFunctionCall1(namein, CStringGetDatum(dbname));
|
|
|
|
new_record[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(datdba);
|
|
|
|
new_record[Anum_pg_database_encoding - 1] = Int32GetDatum(encoding);
|
|
|
|
new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(false);
|
|
|
|
new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(true);
|
|
|
|
new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit);
|
|
|
|
new_record[Anum_pg_database_datlastsysoid - 1] = ObjectIdGetDatum(src_lastsysoid);
|
|
|
|
new_record[Anum_pg_database_datvacuumxid - 1] = TransactionIdGetDatum(src_vacuumxid);
|
|
|
|
new_record[Anum_pg_database_datfrozenxid - 1] = TransactionIdGetDatum(src_frozenxid);
|
|
|
|
new_record[Anum_pg_database_dattablespace - 1] = ObjectIdGetDatum(dst_deftablespace);
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/*
|
|
|
|
* We deliberately set datconfig and datacl to defaults (NULL), rather
|
|
|
|
* than copying them from the template database. Copying datacl would
|
|
|
|
* be a bad idea when the owner is not the same as the template's
|
|
|
|
* owner. It's more debatable whether datconfig should be copied.
|
|
|
|
*/
|
|
|
|
new_record_nulls[Anum_pg_database_datconfig - 1] = 'n';
|
|
|
|
new_record_nulls[Anum_pg_database_datacl - 1] = 'n';
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
tuple = heap_formtuple(pg_database_dsc, new_record, new_record_nulls);
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
HeapTupleSetOid(tuple, dboid); /* override heap_insert's OID
|
|
|
|
* selection */
|
2002-04-21 02:26:44 +02:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
simple_heap_insert(pg_database_rel, tuple);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/* Update indexes */
|
|
|
|
CatalogUpdateIndexes(pg_database_rel, tuple);
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/* Register owner dependency */
|
|
|
|
recordDependencyOnOwner(DatabaseRelationId, dboid, datdba);
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/* Create pg_shdepend entries for objects within database */
|
|
|
|
copyTemplateDependencies(src_dboid, dboid);
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/*
|
|
|
|
* We force a checkpoint before committing. This effectively means
|
|
|
|
* that committed XLOG_DBASE_CREATE operations will never need to be
|
2005-10-15 04:49:52 +02:00
|
|
|
* replayed (at least not in ordinary crash recovery; we still have to
|
|
|
|
* make the XLOG entry for the benefit of PITR operations). This
|
|
|
|
* avoids two nasty scenarios:
|
2005-08-02 21:02:32 +02:00
|
|
|
*
|
|
|
|
* #1: When PITR is off, we don't XLOG the contents of newly created
|
|
|
|
* indexes; therefore the drop-and-recreate-whole-directory behavior
|
|
|
|
* of DBASE_CREATE replay would lose such indexes.
|
|
|
|
*
|
|
|
|
* #2: Since we have to recopy the source database during DBASE_CREATE
|
2005-10-15 04:49:52 +02:00
|
|
|
* replay, we run the risk of copying changes in it that were
|
|
|
|
* committed after the original CREATE DATABASE command but before the
|
|
|
|
* system crash that led to the replay. This is at least unexpected
|
|
|
|
* and at worst could lead to inconsistencies, eg duplicate table
|
|
|
|
* names.
|
2005-08-02 21:02:32 +02:00
|
|
|
*
|
|
|
|
* (Both of these were real bugs in releases 8.0 through 8.0.3.)
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* In PITR replay, the first of these isn't an issue, and the second
|
|
|
|
* is only a risk if the CREATE DATABASE and subsequent template
|
|
|
|
* database change both occur while a base backup is being taken.
|
|
|
|
* There doesn't seem to be much we can do about that except document
|
|
|
|
* it as a limitation.
|
2005-08-02 21:02:32 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Perhaps if we ever implement CREATE DATABASE in a less cheesy way,
|
|
|
|
* we can avoid this.
|
2005-08-02 21:02:32 +02:00
|
|
|
*/
|
|
|
|
RequestCheckpoint(true, false);
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/*
|
|
|
|
* Set flag to update flat database file at commit.
|
|
|
|
*/
|
|
|
|
database_file_update_needed();
|
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
/* Don't hold pg_database lock while doing recursive remove */
|
|
|
|
if (pg_database_rel != NULL)
|
|
|
|
heap_close(pg_database_rel, ExclusiveLock);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
/* Throw away any successfully copied subdirectories */
|
|
|
|
remove_dbtablespaces(dboid);
|
2005-07-07 22:40:02 +02:00
|
|
|
|
2005-08-02 21:02:32 +02:00
|
|
|
PG_RE_THROW();
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
2005-07-07 22:40:02 +02:00
|
|
|
|
2004-11-18 02:14:26 +01:00
|
|
|
/* Close pg_database, but keep exclusive lock till commit */
|
2005-08-02 21:02:32 +02:00
|
|
|
/* This has to be outside the PG_TRY */
|
2004-11-18 02:14:26 +01:00
|
|
|
heap_close(pg_database_rel, NoLock);
|
2000-11-14 19:37:49 +01:00
|
|
|
}
|
1999-12-12 06:15:10 +01:00
|
|
|
|
|
|
|
|
2000-01-13 19:26:18 +01:00
|
|
|
/*
|
|
|
|
* DROP DATABASE
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
2005-11-22 16:24:18 +01:00
|
|
|
dropdb(const char *dbname, bool missing_ok)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-08-29 06:09:29 +02:00
|
|
|
Oid db_id;
|
2005-06-28 07:09:14 +02:00
|
|
|
bool db_istemplate;
|
1999-09-24 02:25:33 +02:00
|
|
|
Relation pgdbrel;
|
2003-08-04 02:43:34 +02:00
|
|
|
SysScanDesc pgdbscan;
|
2000-04-12 19:17:23 +02:00
|
|
|
ScanKeyData key;
|
1999-09-24 02:25:33 +02:00
|
|
|
HeapTuple tup;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
PreventTransactionChain((void *) dbname, "DROP DATABASE");
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
AssertArg(dbname);
|
1999-12-12 06:15:10 +01:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
if (strcmp(dbname, get_database_name(MyDatabaseId)) == 0)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_IN_USE),
|
|
|
|
errmsg("cannot drop the currently open database")));
|
2000-03-15 07:50:51 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Obtain exclusive lock on pg_database. We need this to ensure that no
|
|
|
|
* new backend starts up in the target database while we are deleting it.
|
|
|
|
* (Actually, a new backend might still manage to start up, because it
|
|
|
|
* isn't able to lock pg_database while starting. But it will detect its
|
|
|
|
* error in ReverifyMyDatabase and shut down before any serious damage is
|
|
|
|
* done. See postinit.c.)
|
2005-02-26 19:43:34 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* An ExclusiveLock, rather than AccessExclusiveLock, is sufficient since
|
|
|
|
* ReverifyMyDatabase takes RowShareLock. This allows ordinary readers of
|
|
|
|
* pg_database to proceed in parallel.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
pgdbrel = heap_open(DatabaseRelationId, ExclusiveLock);
|
1999-09-24 02:25:33 +02:00
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
if (!get_db_info(dbname, &db_id, NULL, NULL,
|
2005-03-12 22:33:55 +01:00
|
|
|
&db_istemplate, NULL, NULL, NULL, NULL, NULL))
|
2005-11-22 16:24:18 +01:00
|
|
|
{
|
2005-11-22 19:17:34 +01:00
|
|
|
if (!missing_ok)
|
2005-11-22 16:24:18 +01:00
|
|
|
{
|
|
|
|
ereport(ERROR,
|
2005-11-22 19:17:34 +01:00
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
|
|
|
errmsg("database \"%s\" does not exist", dbname)));
|
2005-11-22 16:24:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-11-22 19:17:34 +01:00
|
|
|
|
2005-11-22 16:24:18 +01:00
|
|
|
/* Close pg_database, release the lock, since we changed nothing */
|
|
|
|
heap_close(pgdbrel, ExclusiveLock);
|
2005-11-22 19:17:34 +01:00
|
|
|
ereport(NOTICE,
|
|
|
|
(errmsg("database \"%s\" does not exist, skipping",
|
2005-11-22 16:24:18 +01:00
|
|
|
dbname)));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
if (!pg_database_ownercheck(db_id, GetUserId()))
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
|
|
|
|
dbname);
|
2000-11-14 19:37:49 +01:00
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Disallow dropping a DB that is marked istemplate. This is just to
|
2005-10-15 04:49:52 +02:00
|
|
|
* prevent people from accidentally dropping template0 or template1; they
|
|
|
|
* can do so if they're really determined ...
|
2000-11-14 19:37:49 +01:00
|
|
|
*/
|
|
|
|
if (db_istemplate)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("cannot drop a template database")));
|
2000-11-14 19:37:49 +01:00
|
|
|
|
1999-09-24 02:25:33 +02:00
|
|
|
/*
|
|
|
|
* Check for active backends in the target database.
|
|
|
|
*/
|
2000-11-12 21:51:52 +01:00
|
|
|
if (DatabaseHasActiveBackends(db_id, false))
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_IN_USE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("database \"%s\" is being accessed by other users",
|
|
|
|
dbname)));
|
1999-09-24 02:25:33 +02:00
|
|
|
|
|
|
|
/*
|
2001-06-12 07:55:50 +02:00
|
|
|
* Find the database's tuple by OID (should be unique).
|
1999-09-24 02:25:33 +02:00
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key,
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(db_id));
|
1999-09-24 02:25:33 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
pgdbscan = systable_beginscan(pgdbrel, DatabaseOidIndexId, true,
|
2003-11-09 22:30:38 +01:00
|
|
|
SnapshotNow, 1, &key);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
tup = systable_getnext(pgdbscan);
|
1999-09-24 02:25:33 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This error should never come up since the existence of the database
|
|
|
|
* is checked earlier
|
2000-04-12 19:17:23 +02:00
|
|
|
*/
|
2003-07-19 01:20:33 +02:00
|
|
|
elog(ERROR, "database \"%s\" doesn't exist despite earlier reports to the contrary",
|
2000-01-13 19:26:18 +01:00
|
|
|
dbname);
|
1999-09-24 02:25:33 +02:00
|
|
|
}
|
|
|
|
|
2000-01-13 19:26:18 +01:00
|
|
|
/* Remove the database's tuple from pg_database */
|
2001-01-23 05:32:23 +01:00
|
|
|
simple_heap_delete(pgdbrel, &tup->t_self);
|
1999-09-24 02:25:33 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
systable_endscan(pgdbscan);
|
1999-09-24 02:25:33 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
|
|
|
* Delete any comments associated with the database
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* NOTE: this is probably dead code since any such comments should have
|
|
|
|
* been in that database, not mine.
|
2002-07-12 20:43:19 +02:00
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
DeleteComments(db_id, DatabaseRelationId, 0);
|
2001-08-10 20:57:42 +02:00
|
|
|
|
2005-10-10 22:02:20 +02:00
|
|
|
/*
|
|
|
|
* Remove shared dependency references for the database.
|
|
|
|
*/
|
|
|
|
dropDatabaseDependencies(db_id);
|
|
|
|
|
1999-09-24 02:25:33 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Drop pages for this database that are in the shared buffer cache. This
|
|
|
|
* is important to ensure that no remaining backend tries to write out a
|
|
|
|
* dirty buffer to the dead database later...
|
1999-09-24 02:25:33 +02:00
|
|
|
*/
|
1999-03-15 15:07:44 +01:00
|
|
|
DropBuffers(db_id);
|
|
|
|
|
2001-07-02 22:50:46 +02:00
|
|
|
/*
|
|
|
|
* Also, clean out any entries in the shared free space map.
|
|
|
|
*/
|
|
|
|
FreeSpaceMapForgetDatabase(db_id);
|
|
|
|
|
2004-10-28 02:39:59 +02:00
|
|
|
/*
|
|
|
|
* On Windows, force a checkpoint so that the bgwriter doesn't hold any
|
|
|
|
* open files, which would cause rmdir() to fail.
|
|
|
|
*/
|
|
|
|
#ifdef WIN32
|
2005-06-30 02:00:52 +02:00
|
|
|
RequestCheckpoint(true, false);
|
2004-10-28 02:39:59 +02:00
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2004-06-18 08:14:31 +02:00
|
|
|
* Remove all tablespace subdirs belonging to the database.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-06-18 08:14:31 +02:00
|
|
|
remove_dbtablespaces(db_id);
|
2001-01-14 23:14:10 +01:00
|
|
|
|
2004-11-18 02:14:26 +01:00
|
|
|
/* Close pg_database, but keep exclusive lock till commit */
|
|
|
|
heap_close(pgdbrel, NoLock);
|
2005-02-20 03:22:07 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set flag to update flat database file at commit.
|
|
|
|
*/
|
|
|
|
database_file_update_needed();
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-12-12 06:15:10 +01:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
/*
|
|
|
|
* Rename database
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RenameDatabase(const char *oldname, const char *newname)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
HeapTuple tup,
|
|
|
|
newtup;
|
2003-06-27 16:45:32 +02:00
|
|
|
Relation rel;
|
2003-08-04 02:43:34 +02:00
|
|
|
SysScanDesc scan,
|
|
|
|
scan2;
|
|
|
|
ScanKeyData key,
|
|
|
|
key2;
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Obtain ExclusiveLock so that no new session gets started while the
|
|
|
|
* rename is in progress.
|
2003-06-27 16:45:32 +02:00
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
rel = heap_open(DatabaseRelationId, ExclusiveLock);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key,
|
|
|
|
Anum_pg_database_datname,
|
|
|
|
BTEqualStrategyNumber, F_NAMEEQ,
|
|
|
|
NameGetDatum(oldname));
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(rel, DatabaseNameIndexId, true,
|
2003-11-09 22:30:38 +01:00
|
|
|
SnapshotNow, 1, &key);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
tup = systable_getnext(scan);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
ereport(ERROR,
|
2003-07-19 01:20:33 +02:00
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
2003-06-27 16:45:32 +02:00
|
|
|
errmsg("database \"%s\" does not exist", oldname)));
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* XXX Client applications probably store the current database somewhere,
|
|
|
|
* so renaming it could cause confusion. On the other hand, there may not
|
|
|
|
* be an actual problem besides a little confusion, so think about this
|
|
|
|
* and decide.
|
2003-06-27 16:45:32 +02:00
|
|
|
*/
|
|
|
|
if (HeapTupleGetOid(tup) == MyDatabaseId)
|
|
|
|
ereport(ERROR,
|
2003-07-19 01:20:33 +02:00
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2003-06-27 16:45:32 +02:00
|
|
|
errmsg("current database may not be renamed")));
|
|
|
|
|
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Make sure the database does not have active sessions. Might not be
|
|
|
|
* necessary, but it's consistent with other database operations.
|
2003-06-27 16:45:32 +02:00
|
|
|
*/
|
|
|
|
if (DatabaseHasActiveBackends(HeapTupleGetOid(tup), false))
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_IN_USE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("database \"%s\" is being accessed by other users",
|
|
|
|
oldname)));
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
/* make sure the new name doesn't exist */
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&key2,
|
|
|
|
Anum_pg_database_datname,
|
|
|
|
BTEqualStrategyNumber, F_NAMEEQ,
|
|
|
|
NameGetDatum(newname));
|
2005-04-14 22:03:27 +02:00
|
|
|
scan2 = systable_beginscan(rel, DatabaseNameIndexId, true,
|
2003-11-09 22:30:38 +01:00
|
|
|
SnapshotNow, 1, &key2);
|
2003-06-27 16:45:32 +02:00
|
|
|
if (HeapTupleIsValid(systable_getnext(scan2)))
|
|
|
|
ereport(ERROR,
|
2003-07-19 01:20:33 +02:00
|
|
|
(errcode(ERRCODE_DUPLICATE_DATABASE),
|
2003-06-27 16:45:32 +02:00
|
|
|
errmsg("database \"%s\" already exists", newname)));
|
|
|
|
systable_endscan(scan2);
|
|
|
|
|
|
|
|
/* must be owner */
|
|
|
|
if (!pg_database_ownercheck(HeapTupleGetOid(tup), GetUserId()))
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
|
|
|
|
oldname);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
2005-03-12 22:11:50 +01:00
|
|
|
/* must have createdb rights */
|
2005-06-29 22:34:15 +02:00
|
|
|
if (!have_createdb_privilege())
|
2003-06-27 16:45:32 +02:00
|
|
|
ereport(ERROR,
|
2003-07-19 01:20:33 +02:00
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2003-08-01 02:15:26 +02:00
|
|
|
errmsg("permission denied to rename database")));
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
/* rename */
|
|
|
|
newtup = heap_copytuple(tup);
|
|
|
|
namestrcpy(&(((Form_pg_database) GETSTRUCT(newtup))->datname), newname);
|
2004-06-25 23:55:59 +02:00
|
|
|
simple_heap_update(rel, &newtup->t_self, newtup);
|
2003-06-27 16:45:32 +02:00
|
|
|
CatalogUpdateIndexes(rel, newtup);
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
|
2004-11-18 02:14:26 +01:00
|
|
|
/* Close pg_database, but keep exclusive lock till commit */
|
|
|
|
heap_close(rel, NoLock);
|
2005-02-20 03:22:07 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set flag to update flat database file at commit.
|
|
|
|
*/
|
|
|
|
database_file_update_needed();
|
2003-06-27 16:45:32 +02:00
|
|
|
}
|
|
|
|
|
1999-12-12 06:15:10 +01:00
|
|
|
|
2005-07-31 19:19:22 +02:00
|
|
|
/*
|
|
|
|
* ALTER DATABASE name ...
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterDatabase(AlterDatabaseStmt *stmt)
|
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
HeapTuple tuple,
|
|
|
|
newtuple;
|
|
|
|
ScanKeyData scankey;
|
|
|
|
SysScanDesc scan;
|
|
|
|
ListCell *option;
|
|
|
|
int connlimit = -1;
|
|
|
|
DefElem *dconnlimit = NULL;
|
|
|
|
Datum new_record[Natts_pg_database];
|
|
|
|
char new_record_nulls[Natts_pg_database];
|
|
|
|
char new_record_repl[Natts_pg_database];
|
|
|
|
|
|
|
|
/* Extract options from the statement node tree */
|
|
|
|
foreach(option, stmt->options)
|
|
|
|
{
|
|
|
|
DefElem *defel = (DefElem *) lfirst(option);
|
|
|
|
|
|
|
|
if (strcmp(defel->defname, "connectionlimit") == 0)
|
|
|
|
{
|
|
|
|
if (dconnlimit)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
|
|
|
dconnlimit = defel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(ERROR, "option \"%s\" not recognized",
|
|
|
|
defel->defname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dconnlimit)
|
|
|
|
connlimit = intVal(dconnlimit->arg);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We don't need ExclusiveLock since we aren't updating the flat file.
|
2005-07-31 19:19:22 +02:00
|
|
|
*/
|
|
|
|
rel = heap_open(DatabaseRelationId, RowExclusiveLock);
|
|
|
|
ScanKeyInit(&scankey,
|
|
|
|
Anum_pg_database_datname,
|
|
|
|
BTEqualStrategyNumber, F_NAMEEQ,
|
|
|
|
NameGetDatum(stmt->dbname));
|
|
|
|
scan = systable_beginscan(rel, DatabaseNameIndexId, true,
|
|
|
|
SnapshotNow, 1, &scankey);
|
|
|
|
tuple = systable_getnext(scan);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
|
|
|
errmsg("database \"%s\" does not exist", stmt->dbname)));
|
|
|
|
|
|
|
|
if (!pg_database_ownercheck(HeapTupleGetOid(tuple), GetUserId()))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
|
|
|
|
stmt->dbname);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build an updated tuple, perusing the information just obtained
|
|
|
|
*/
|
|
|
|
MemSet(new_record, 0, sizeof(new_record));
|
|
|
|
MemSet(new_record_nulls, ' ', sizeof(new_record_nulls));
|
|
|
|
MemSet(new_record_repl, ' ', sizeof(new_record_repl));
|
|
|
|
|
|
|
|
if (dconnlimit)
|
|
|
|
{
|
|
|
|
new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(connlimit);
|
|
|
|
new_record_repl[Anum_pg_database_datconnlimit - 1] = 'r';
|
|
|
|
}
|
|
|
|
|
|
|
|
newtuple = heap_modifytuple(tuple, RelationGetDescr(rel), new_record,
|
|
|
|
new_record_nulls, new_record_repl);
|
|
|
|
simple_heap_update(rel, &tuple->t_self, newtuple);
|
|
|
|
|
|
|
|
/* Update indexes */
|
|
|
|
CatalogUpdateIndexes(rel, newtuple);
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
|
|
|
|
/* Close pg_database, but keep lock till commit */
|
|
|
|
heap_close(rel, NoLock);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We don't bother updating the flat file since the existing options for
|
|
|
|
* ALTER DATABASE don't affect it.
|
2005-07-31 19:19:22 +02:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-01 23:45:19 +01:00
|
|
|
/*
|
|
|
|
* ALTER DATABASE name SET ...
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
|
|
|
|
{
|
|
|
|
char *valuestr;
|
|
|
|
HeapTuple tuple,
|
|
|
|
newtuple;
|
|
|
|
Relation rel;
|
2002-09-04 22:31:48 +02:00
|
|
|
ScanKeyData scankey;
|
2003-08-04 02:43:34 +02:00
|
|
|
SysScanDesc scan;
|
2002-03-01 23:45:19 +01:00
|
|
|
Datum repl_val[Natts_pg_database];
|
|
|
|
char repl_null[Natts_pg_database];
|
|
|
|
char repl_repl[Natts_pg_database];
|
|
|
|
|
2002-05-17 03:19:19 +02:00
|
|
|
valuestr = flatten_set_variable_args(stmt->variable, stmt->value);
|
2002-03-01 23:45:19 +01:00
|
|
|
|
2004-11-18 02:14:26 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We don't need ExclusiveLock since we aren't updating the flat file.
|
2004-11-18 02:14:26 +01:00
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
rel = heap_open(DatabaseRelationId, RowExclusiveLock);
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&scankey,
|
|
|
|
Anum_pg_database_datname,
|
|
|
|
BTEqualStrategyNumber, F_NAMEEQ,
|
|
|
|
NameGetDatum(stmt->dbname));
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(rel, DatabaseNameIndexId, true,
|
2003-11-09 22:30:38 +01:00
|
|
|
SnapshotNow, 1, &scankey);
|
2003-06-27 16:45:32 +02:00
|
|
|
tuple = systable_getnext(scan);
|
2002-03-01 23:45:19 +01:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
|
|
|
errmsg("database \"%s\" does not exist", stmt->dbname)));
|
2002-03-01 23:45:19 +01:00
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
if (!pg_database_ownercheck(HeapTupleGetOid(tuple), GetUserId()))
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
|
2003-08-04 02:43:34 +02:00
|
|
|
stmt->dbname);
|
2002-03-01 23:45:19 +01:00
|
|
|
|
2002-04-21 02:26:44 +02:00
|
|
|
MemSet(repl_repl, ' ', sizeof(repl_repl));
|
2002-09-04 22:31:48 +02:00
|
|
|
repl_repl[Anum_pg_database_datconfig - 1] = 'r';
|
2002-04-27 23:24:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (strcmp(stmt->variable, "all") == 0 && valuestr == NULL)
|
2002-04-27 23:24:34 +02:00
|
|
|
{
|
2002-03-01 23:45:19 +01:00
|
|
|
/* RESET ALL */
|
2002-09-04 22:31:48 +02:00
|
|
|
repl_null[Anum_pg_database_datconfig - 1] = 'n';
|
|
|
|
repl_val[Anum_pg_database_datconfig - 1] = (Datum) 0;
|
2002-04-27 23:24:34 +02:00
|
|
|
}
|
2002-03-01 23:45:19 +01:00
|
|
|
else
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
Datum datum;
|
|
|
|
bool isnull;
|
|
|
|
ArrayType *a;
|
2002-03-01 23:45:19 +01:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
repl_null[Anum_pg_database_datconfig - 1] = ' ';
|
2002-03-01 23:45:19 +01:00
|
|
|
|
|
|
|
datum = heap_getattr(tuple, Anum_pg_database_datconfig,
|
|
|
|
RelationGetDescr(rel), &isnull);
|
|
|
|
|
2004-01-07 19:56:30 +01:00
|
|
|
a = isnull ? NULL : DatumGetArrayTypeP(datum);
|
2002-04-27 23:24:34 +02:00
|
|
|
|
2002-03-01 23:45:19 +01:00
|
|
|
if (valuestr)
|
2002-04-27 23:24:34 +02:00
|
|
|
a = GUCArrayAdd(a, stmt->variable, valuestr);
|
2002-03-01 23:45:19 +01:00
|
|
|
else
|
2002-04-27 23:24:34 +02:00
|
|
|
a = GUCArrayDelete(a, stmt->variable);
|
2002-03-01 23:45:19 +01:00
|
|
|
|
2002-12-02 06:20:47 +01:00
|
|
|
if (a)
|
|
|
|
repl_val[Anum_pg_database_datconfig - 1] = PointerGetDatum(a);
|
|
|
|
else
|
|
|
|
repl_null[Anum_pg_database_datconfig - 1] = 'n';
|
2002-03-01 23:45:19 +01:00
|
|
|
}
|
|
|
|
|
2005-01-28 00:24:11 +01:00
|
|
|
newtuple = heap_modifytuple(tuple, RelationGetDescr(rel), repl_val, repl_null, repl_repl);
|
2002-03-01 23:45:19 +01:00
|
|
|
simple_heap_update(rel, &tuple->t_self, newtuple);
|
|
|
|
|
2002-08-05 05:29:17 +02:00
|
|
|
/* Update indexes */
|
|
|
|
CatalogUpdateIndexes(rel, newtuple);
|
2002-05-25 18:30:59 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
systable_endscan(scan);
|
2004-11-18 02:14:26 +01:00
|
|
|
|
2005-02-26 19:43:34 +01:00
|
|
|
/* Close pg_database, but keep lock till commit */
|
2004-06-25 23:55:59 +02:00
|
|
|
heap_close(rel, NoLock);
|
2005-02-20 03:22:07 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We don't bother updating the flat file since ALTER DATABASE SET doesn't
|
|
|
|
* affect it.
|
2005-02-20 03:22:07 +01:00
|
|
|
*/
|
2002-03-01 23:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-26 15:57:04 +02:00
|
|
|
/*
|
|
|
|
* ALTER DATABASE name OWNER TO newowner
|
|
|
|
*/
|
|
|
|
void
|
2005-06-28 07:09:14 +02:00
|
|
|
AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
|
2004-05-26 15:57:04 +02:00
|
|
|
{
|
2004-08-01 22:30:49 +02:00
|
|
|
HeapTuple tuple;
|
2004-05-26 15:57:04 +02:00
|
|
|
Relation rel;
|
|
|
|
ScanKeyData scankey;
|
|
|
|
SysScanDesc scan;
|
2004-08-29 07:07:03 +02:00
|
|
|
Form_pg_database datForm;
|
2004-05-26 15:57:04 +02:00
|
|
|
|
2004-11-18 02:14:26 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We don't need ExclusiveLock since we aren't updating the flat file.
|
2004-11-18 02:14:26 +01:00
|
|
|
*/
|
2005-04-14 22:03:27 +02:00
|
|
|
rel = heap_open(DatabaseRelationId, RowExclusiveLock);
|
2004-05-26 15:57:04 +02:00
|
|
|
ScanKeyInit(&scankey,
|
|
|
|
Anum_pg_database_datname,
|
|
|
|
BTEqualStrategyNumber, F_NAMEEQ,
|
|
|
|
NameGetDatum(dbname));
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(rel, DatabaseNameIndexId, true,
|
2004-05-26 15:57:04 +02:00
|
|
|
SnapshotNow, 1, &scankey);
|
|
|
|
tuple = systable_getnext(scan);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
|
|
|
errmsg("database \"%s\" does not exist", dbname)));
|
|
|
|
|
2004-08-01 22:30:49 +02:00
|
|
|
datForm = (Form_pg_database) GETSTRUCT(tuple);
|
2004-05-26 15:57:04 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
2004-06-25 23:55:59 +02:00
|
|
|
* If the new owner is the same as the existing owner, consider the
|
2004-08-29 07:07:03 +02:00
|
|
|
* command to have succeeded. This is to be consistent with other
|
|
|
|
* objects.
|
2004-06-25 23:55:59 +02:00
|
|
|
*/
|
2005-06-28 07:09:14 +02:00
|
|
|
if (datForm->datdba != newOwnerId)
|
2004-06-25 23:55:59 +02:00
|
|
|
{
|
2004-08-01 22:30:49 +02:00
|
|
|
Datum repl_val[Natts_pg_database];
|
|
|
|
char repl_null[Natts_pg_database];
|
|
|
|
char repl_repl[Natts_pg_database];
|
2004-08-29 07:07:03 +02:00
|
|
|
Acl *newAcl;
|
2004-08-01 22:30:49 +02:00
|
|
|
Datum aclDatum;
|
|
|
|
bool isNull;
|
|
|
|
HeapTuple newtuple;
|
|
|
|
|
2005-07-14 23:46:30 +02:00
|
|
|
/* Otherwise, must be owner of the existing object */
|
2005-10-15 04:49:52 +02:00
|
|
|
if (!pg_database_ownercheck(HeapTupleGetOid(tuple), GetUserId()))
|
2005-07-14 23:46:30 +02:00
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
|
|
|
|
dbname);
|
|
|
|
|
|
|
|
/* Must be able to become new owner */
|
|
|
|
check_is_member_of_role(GetUserId(), newOwnerId);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* must have createdb rights
|
2005-07-14 23:46:30 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* NOTE: This is different from other alter-owner checks in that the
|
|
|
|
* current user is checked for createdb privileges instead of the
|
|
|
|
* destination owner. This is consistent with the CREATE case for
|
|
|
|
* databases. Because superusers will always have this right, we need
|
|
|
|
* no special case for them.
|
2005-07-14 23:46:30 +02:00
|
|
|
*/
|
|
|
|
if (!have_createdb_privilege())
|
2004-06-25 23:55:59 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("permission denied to change owner of database")));
|
2004-05-26 15:57:04 +02:00
|
|
|
|
2004-08-01 22:30:49 +02:00
|
|
|
memset(repl_null, ' ', sizeof(repl_null));
|
|
|
|
memset(repl_repl, ' ', sizeof(repl_repl));
|
|
|
|
|
|
|
|
repl_repl[Anum_pg_database_datdba - 1] = 'r';
|
2005-06-28 07:09:14 +02:00
|
|
|
repl_val[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(newOwnerId);
|
2004-08-01 22:30:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the modified ACL for the new owner. This is only
|
|
|
|
* necessary when the ACL is non-null.
|
|
|
|
*/
|
|
|
|
aclDatum = heap_getattr(tuple,
|
2004-08-29 07:07:03 +02:00
|
|
|
Anum_pg_database_datacl,
|
|
|
|
RelationGetDescr(rel),
|
|
|
|
&isNull);
|
2004-08-01 22:30:49 +02:00
|
|
|
if (!isNull)
|
|
|
|
{
|
|
|
|
newAcl = aclnewowner(DatumGetAclP(aclDatum),
|
2005-06-28 07:09:14 +02:00
|
|
|
datForm->datdba, newOwnerId);
|
2004-08-01 22:30:49 +02:00
|
|
|
repl_repl[Anum_pg_database_datacl - 1] = 'r';
|
|
|
|
repl_val[Anum_pg_database_datacl - 1] = PointerGetDatum(newAcl);
|
|
|
|
}
|
|
|
|
|
2005-01-28 00:24:11 +01:00
|
|
|
newtuple = heap_modifytuple(tuple, RelationGetDescr(rel), repl_val, repl_null, repl_repl);
|
2004-06-25 23:55:59 +02:00
|
|
|
simple_heap_update(rel, &newtuple->t_self, newtuple);
|
|
|
|
CatalogUpdateIndexes(rel, newtuple);
|
2004-08-01 22:30:49 +02:00
|
|
|
|
|
|
|
heap_freetuple(newtuple);
|
2005-07-07 22:40:02 +02:00
|
|
|
|
|
|
|
/* Update owner dependency reference */
|
|
|
|
changeDependencyOnOwner(DatabaseRelationId, HeapTupleGetOid(tuple),
|
|
|
|
newOwnerId);
|
2004-06-25 23:55:59 +02:00
|
|
|
}
|
2004-05-26 15:57:04 +02:00
|
|
|
|
2005-02-26 19:43:34 +01:00
|
|
|
systable_endscan(scan);
|
|
|
|
|
|
|
|
/* Close pg_database, but keep lock till commit */
|
2004-05-26 15:57:04 +02:00
|
|
|
heap_close(rel, NoLock);
|
2005-02-20 03:22:07 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't bother updating the flat file since ALTER DATABASE OWNER
|
|
|
|
* doesn't affect it.
|
|
|
|
*/
|
2004-05-26 15:57:04 +02:00
|
|
|
}
|
|
|
|
|
2002-03-01 23:45:19 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2000-01-13 19:26:18 +01:00
|
|
|
* Helper functions
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
|
2000-01-13 19:26:18 +01:00
|
|
|
static bool
|
2005-06-28 07:09:14 +02:00
|
|
|
get_db_info(const char *name, Oid *dbIdP, Oid *ownerIdP,
|
2005-03-12 22:33:55 +01:00
|
|
|
int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP,
|
|
|
|
Oid *dbLastSysOidP,
|
2001-08-26 18:56:03 +02:00
|
|
|
TransactionId *dbVacuumXidP, TransactionId *dbFrozenXidP,
|
2004-06-18 08:14:31 +02:00
|
|
|
Oid *dbTablespace)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-01-13 19:26:18 +01:00
|
|
|
Relation relation;
|
|
|
|
ScanKeyData scanKey;
|
2003-08-04 02:43:34 +02:00
|
|
|
SysScanDesc scan;
|
2000-11-14 19:37:49 +01:00
|
|
|
HeapTuple tuple;
|
2001-08-26 18:56:03 +02:00
|
|
|
bool gottuple;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
AssertArg(name);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-14 19:37:49 +01:00
|
|
|
/* Caller may wish to grab a better lock on pg_database beforehand... */
|
2005-04-14 22:03:27 +02:00
|
|
|
relation = heap_open(DatabaseRelationId, AccessShareLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&scanKey,
|
|
|
|
Anum_pg_database_datname,
|
|
|
|
BTEqualStrategyNumber, F_NAMEEQ,
|
|
|
|
NameGetDatum(name));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(relation, DatabaseNameIndexId, true,
|
2003-11-09 22:30:38 +01:00
|
|
|
SnapshotNow, 1, &scanKey);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
tuple = systable_getnext(scan);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-08-26 18:56:03 +02:00
|
|
|
gottuple = HeapTupleIsValid(tuple);
|
|
|
|
if (gottuple)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-11-14 19:37:49 +01:00
|
|
|
Form_pg_database dbform = (Form_pg_database) GETSTRUCT(tuple);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
|
|
|
/* oid of the database */
|
|
|
|
if (dbIdP)
|
2002-07-20 07:16:59 +02:00
|
|
|
*dbIdP = HeapTupleGetOid(tuple);
|
2005-06-28 07:09:14 +02:00
|
|
|
/* oid of the owner */
|
2000-04-12 19:17:23 +02:00
|
|
|
if (ownerIdP)
|
2000-11-14 19:37:49 +01:00
|
|
|
*ownerIdP = dbform->datdba;
|
2002-09-03 23:45:44 +02:00
|
|
|
/* character encoding */
|
2000-11-14 19:37:49 +01:00
|
|
|
if (encodingP)
|
|
|
|
*encodingP = dbform->encoding;
|
|
|
|
/* allowed as template? */
|
|
|
|
if (dbIsTemplateP)
|
|
|
|
*dbIsTemplateP = dbform->datistemplate;
|
2005-03-12 22:33:55 +01:00
|
|
|
/* allowing connections? */
|
|
|
|
if (dbAllowConnP)
|
|
|
|
*dbAllowConnP = dbform->datallowconn;
|
2000-11-14 19:37:49 +01:00
|
|
|
/* last system OID used in database */
|
|
|
|
if (dbLastSysOidP)
|
|
|
|
*dbLastSysOidP = dbform->datlastsysoid;
|
2001-08-26 18:56:03 +02:00
|
|
|
/* limit of vacuumed XIDs */
|
|
|
|
if (dbVacuumXidP)
|
|
|
|
*dbVacuumXidP = dbform->datvacuumxid;
|
|
|
|
/* limit of frozen XIDs */
|
|
|
|
if (dbFrozenXidP)
|
|
|
|
*dbFrozenXidP = dbform->datfrozenxid;
|
2004-06-18 08:14:31 +02:00
|
|
|
/* default tablespace for this database */
|
|
|
|
if (dbTablespace)
|
|
|
|
*dbTablespace = dbform->dattablespace;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
systable_endscan(scan);
|
2000-11-14 19:37:49 +01:00
|
|
|
heap_close(relation, AccessShareLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-08-26 18:56:03 +02:00
|
|
|
return gottuple;
|
2000-01-13 19:26:18 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-03-12 22:11:50 +01:00
|
|
|
/* Check if current user has createdb privileges */
|
2000-01-13 19:26:18 +01:00
|
|
|
static bool
|
2002-04-11 07:32:03 +02:00
|
|
|
have_createdb_privilege(void)
|
2000-01-13 19:26:18 +01:00
|
|
|
{
|
2005-03-12 22:11:50 +01:00
|
|
|
bool result = false;
|
2000-04-12 19:17:23 +02:00
|
|
|
HeapTuple utup;
|
1997-11-07 07:38:51 +01:00
|
|
|
|
2005-06-29 22:34:15 +02:00
|
|
|
/* Superusers can always do everything */
|
|
|
|
if (superuser())
|
|
|
|
return true;
|
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
utup = SearchSysCache(AUTHOID,
|
|
|
|
ObjectIdGetDatum(GetUserId()),
|
2000-11-16 23:30:52 +01:00
|
|
|
0, 0, 0);
|
2005-03-12 22:11:50 +01:00
|
|
|
if (HeapTupleIsValid(utup))
|
|
|
|
{
|
2005-06-28 07:09:14 +02:00
|
|
|
result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreatedb;
|
2005-03-12 22:11:50 +01:00
|
|
|
ReleaseSysCache(utup);
|
|
|
|
}
|
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-11-08 17:59:50 +01:00
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
/*
|
|
|
|
* Remove tablespace directories
|
|
|
|
*
|
|
|
|
* We don't know what tablespaces db_id is using, so iterate through all
|
|
|
|
* tablespaces removing <tablespace>/db_id
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
remove_dbtablespaces(Oid db_id)
|
2000-11-08 17:59:50 +01:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
Relation rel;
|
2004-06-18 08:14:31 +02:00
|
|
|
HeapScanDesc scan;
|
2004-08-29 07:07:03 +02:00
|
|
|
HeapTuple tuple;
|
2004-06-18 08:14:31 +02:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
rel = heap_open(TableSpaceRelationId, AccessShareLock);
|
2004-06-18 08:14:31 +02:00
|
|
|
scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
|
|
|
|
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
|
2000-11-08 17:59:50 +01:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
Oid dsttablespace = HeapTupleGetOid(tuple);
|
|
|
|
char *dstpath;
|
2004-06-18 08:14:31 +02:00
|
|
|
struct stat st;
|
2000-11-08 17:59:50 +01:00
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
/* Don't mess with the global tablespace */
|
|
|
|
if (dsttablespace == GLOBALTABLESPACE_OID)
|
|
|
|
continue;
|
2002-02-23 21:55:46 +01:00
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
dstpath = GetDatabasePath(db_id, dsttablespace);
|
2000-11-08 17:59:50 +01:00
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
if (stat(dstpath, &st) < 0 || !S_ISDIR(st.st_mode))
|
2000-11-08 17:59:50 +01:00
|
|
|
{
|
2004-06-18 08:14:31 +02:00
|
|
|
/* Assume we can ignore it */
|
|
|
|
pfree(dstpath);
|
|
|
|
continue;
|
2000-11-08 17:59:50 +01:00
|
|
|
}
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2004-08-01 08:19:26 +02:00
|
|
|
if (!rmtree(dstpath, true))
|
2004-06-18 08:14:31 +02:00
|
|
|
ereport(WARNING,
|
2004-08-29 07:07:03 +02:00
|
|
|
(errmsg("could not remove database directory \"%s\"",
|
2004-08-29 23:08:48 +02:00
|
|
|
dstpath)));
|
|
|
|
|
|
|
|
/* Record the filesystem change in XLOG */
|
|
|
|
{
|
|
|
|
xl_dbase_drop_rec xlrec;
|
2005-03-23 01:03:37 +01:00
|
|
|
XLogRecData rdata[1];
|
2004-08-29 23:08:48 +02:00
|
|
|
|
|
|
|
xlrec.db_id = db_id;
|
2005-03-23 01:03:37 +01:00
|
|
|
xlrec.tablespace_id = dsttablespace;
|
|
|
|
|
2004-08-29 23:08:48 +02:00
|
|
|
rdata[0].data = (char *) &xlrec;
|
2005-03-23 01:03:37 +01:00
|
|
|
rdata[0].len = sizeof(xl_dbase_drop_rec);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata[0].buffer = InvalidBuffer;
|
2005-03-23 01:03:37 +01:00
|
|
|
rdata[0].next = NULL;
|
2004-08-29 23:08:48 +02:00
|
|
|
|
|
|
|
(void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_DROP, rdata);
|
2004-06-18 08:14:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pfree(dstpath);
|
2000-11-08 17:59:50 +01:00
|
|
|
}
|
|
|
|
|
2004-06-18 08:14:31 +02:00
|
|
|
heap_endscan(scan);
|
|
|
|
heap_close(rel, AccessShareLock);
|
2000-11-08 17:59:50 +01:00
|
|
|
}
|
2002-08-09 18:45:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_database_oid - given a database name, look up the OID
|
|
|
|
*
|
|
|
|
* Returns InvalidOid if database name not found.
|
|
|
|
*
|
|
|
|
* This is not actually used in this file, but is exported for use elsewhere.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
get_database_oid(const char *dbname)
|
|
|
|
{
|
|
|
|
Relation pg_database;
|
|
|
|
ScanKeyData entry[1];
|
2003-08-04 02:43:34 +02:00
|
|
|
SysScanDesc scan;
|
2002-08-09 18:45:16 +02:00
|
|
|
HeapTuple dbtuple;
|
|
|
|
Oid oid;
|
|
|
|
|
|
|
|
/* There's no syscache for pg_database, so must look the hard way */
|
2005-04-14 22:03:27 +02:00
|
|
|
pg_database = heap_open(DatabaseRelationId, AccessShareLock);
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&entry[0],
|
|
|
|
Anum_pg_database_datname,
|
|
|
|
BTEqualStrategyNumber, F_NAMEEQ,
|
|
|
|
CStringGetDatum(dbname));
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(pg_database, DatabaseNameIndexId, true,
|
2003-11-09 22:30:38 +01:00
|
|
|
SnapshotNow, 1, entry);
|
2002-08-09 18:45:16 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
dbtuple = systable_getnext(scan);
|
2002-08-09 18:45:16 +02:00
|
|
|
|
|
|
|
/* We assume that there can be at most one matching tuple */
|
|
|
|
if (HeapTupleIsValid(dbtuple))
|
|
|
|
oid = HeapTupleGetOid(dbtuple);
|
|
|
|
else
|
|
|
|
oid = InvalidOid;
|
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
systable_endscan(scan);
|
2002-08-09 18:45:16 +02:00
|
|
|
heap_close(pg_database, AccessShareLock);
|
|
|
|
|
|
|
|
return oid;
|
|
|
|
}
|
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
|
2002-08-09 18:45:16 +02:00
|
|
|
/*
|
2003-06-27 16:45:32 +02:00
|
|
|
* get_database_name - given a database OID, look up the name
|
2002-08-09 18:45:16 +02:00
|
|
|
*
|
2004-06-18 08:14:31 +02:00
|
|
|
* Returns a palloc'd string, or NULL if no such database.
|
2002-08-09 18:45:16 +02:00
|
|
|
*
|
|
|
|
* This is not actually used in this file, but is exported for use elsewhere.
|
|
|
|
*/
|
2003-06-27 16:45:32 +02:00
|
|
|
char *
|
|
|
|
get_database_name(Oid dbid)
|
2002-08-09 18:45:16 +02:00
|
|
|
{
|
|
|
|
Relation pg_database;
|
|
|
|
ScanKeyData entry[1];
|
2003-08-04 02:43:34 +02:00
|
|
|
SysScanDesc scan;
|
2002-08-09 18:45:16 +02:00
|
|
|
HeapTuple dbtuple;
|
2003-06-27 16:45:32 +02:00
|
|
|
char *result;
|
2002-08-09 18:45:16 +02:00
|
|
|
|
|
|
|
/* There's no syscache for pg_database, so must look the hard way */
|
2005-04-14 22:03:27 +02:00
|
|
|
pg_database = heap_open(DatabaseRelationId, AccessShareLock);
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&entry[0],
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(dbid));
|
2005-04-14 22:03:27 +02:00
|
|
|
scan = systable_beginscan(pg_database, DatabaseOidIndexId, true,
|
2003-11-09 22:30:38 +01:00
|
|
|
SnapshotNow, 1, entry);
|
2002-08-09 18:45:16 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
dbtuple = systable_getnext(scan);
|
2002-08-09 18:45:16 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
/* We assume that there can be at most one matching tuple */
|
|
|
|
if (HeapTupleIsValid(dbtuple))
|
|
|
|
result = pstrdup(NameStr(((Form_pg_database) GETSTRUCT(dbtuple))->datname));
|
|
|
|
else
|
|
|
|
result = NULL;
|
2002-08-09 18:45:16 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
systable_endscan(scan);
|
2002-08-09 18:45:16 +02:00
|
|
|
heap_close(pg_database, AccessShareLock);
|
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
return result;
|
2002-08-09 18:45:16 +02:00
|
|
|
}
|
2004-08-29 23:08:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* DATABASE resource manager's routines
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dbase_redo(XLogRecPtr lsn, XLogRecord *record)
|
|
|
|
{
|
|
|
|
uint8 info = record->xl_info & ~XLR_INFO_MASK;
|
|
|
|
|
|
|
|
if (info == XLOG_DBASE_CREATE)
|
|
|
|
{
|
|
|
|
xl_dbase_create_rec *xlrec = (xl_dbase_create_rec *) XLogRecGetData(record);
|
2005-03-23 01:03:37 +01:00
|
|
|
char *src_path;
|
|
|
|
char *dst_path;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id);
|
|
|
|
dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Our theory for replaying a CREATE is to forcibly drop the target
|
|
|
|
* subdirectory if present, then re-copy the source data. This may be
|
|
|
|
* more work than needed, but it is simple to implement.
|
2005-03-23 01:03:37 +01:00
|
|
|
*/
|
|
|
|
if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
if (!rmtree(dst_path, true))
|
|
|
|
ereport(WARNING,
|
2005-08-02 21:02:32 +02:00
|
|
|
(errmsg("could not remove database directory \"%s\"",
|
|
|
|
dst_path)));
|
2005-03-23 01:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Force dirty buffers out to disk, to ensure source database is
|
|
|
|
* up-to-date for the copy. (We really only need to flush buffers for
|
|
|
|
* the source database, but bufmgr.c provides no API for that.)
|
|
|
|
*/
|
|
|
|
BufferSync();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy this subdirectory to the new location
|
|
|
|
*
|
2005-08-02 21:02:32 +02:00
|
|
|
* We don't need to copy subdirectories
|
2005-03-23 01:03:37 +01:00
|
|
|
*/
|
2005-08-02 21:02:32 +02:00
|
|
|
copydir(src_path, dst_path, false);
|
2005-03-23 01:03:37 +01:00
|
|
|
}
|
|
|
|
else if (info == XLOG_DBASE_DROP)
|
|
|
|
{
|
|
|
|
xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) XLogRecGetData(record);
|
|
|
|
char *dst_path;
|
|
|
|
|
|
|
|
dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Drop pages for this database that are in the shared buffer cache
|
2005-03-23 01:03:37 +01:00
|
|
|
*/
|
|
|
|
DropBuffers(xlrec->db_id);
|
|
|
|
|
|
|
|
if (!rmtree(dst_path, true))
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("could not remove database directory \"%s\"",
|
|
|
|
dst_path)));
|
|
|
|
}
|
2004-08-29 23:08:48 +02:00
|
|
|
else
|
|
|
|
elog(PANIC, "dbase_redo: unknown op code %u", info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dbase_desc(char *buf, uint8 xl_info, char *rec)
|
|
|
|
{
|
|
|
|
uint8 info = xl_info & ~XLR_INFO_MASK;
|
|
|
|
|
|
|
|
if (info == XLOG_DBASE_CREATE)
|
|
|
|
{
|
|
|
|
xl_dbase_create_rec *xlrec = (xl_dbase_create_rec *) rec;
|
2005-03-23 01:03:37 +01:00
|
|
|
|
|
|
|
sprintf(buf + strlen(buf), "create db: copy dir %u/%u to %u/%u",
|
|
|
|
xlrec->src_db_id, xlrec->src_tablespace_id,
|
|
|
|
xlrec->db_id, xlrec->tablespace_id);
|
|
|
|
}
|
|
|
|
else if (info == XLOG_DBASE_DROP)
|
|
|
|
{
|
|
|
|
xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) rec;
|
|
|
|
|
|
|
|
sprintf(buf + strlen(buf), "drop db: dir %u/%u",
|
|
|
|
xlrec->db_id, xlrec->tablespace_id);
|
|
|
|
}
|
2004-08-29 23:08:48 +02:00
|
|
|
else
|
|
|
|
strcat(buf, "UNKNOWN");
|
|
|
|
}
|