1 /*-------------------------------------------------------------------------
4 * Database management commands (create/drop database).
6 * Note: database creation/destruction commands take ExclusiveLock on
7 * pg_database to ensure that no two proceed in parallel. We must use
8 * at least this level of locking to ensure that no two backends try to
9 * write the flat-file copy of pg_database at once. We avoid using
10 * AccessExclusiveLock since there's no need to lock out ordinary readers
13 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
18 * $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.180 2006/05/03 22:45:26 tgl Exp $
20 *-------------------------------------------------------------------------
28 #include "access/genam.h"
29 #include "access/heapam.h"
30 #include "catalog/catalog.h"
31 #include "catalog/dependency.h"
32 #include "catalog/indexing.h"
33 #include "catalog/pg_authid.h"
34 #include "catalog/pg_database.h"
35 #include "catalog/pg_tablespace.h"
36 #include "commands/comment.h"
37 #include "commands/dbcommands.h"
38 #include "commands/tablespace.h"
39 #include "mb/pg_wchar.h"
40 #include "miscadmin.h"
41 #include "postmaster/bgwriter.h"
42 #include "storage/fd.h"
43 #include "storage/freespace.h"
44 #include "storage/procarray.h"
45 #include "utils/acl.h"
46 #include "utils/array.h"
47 #include "utils/builtins.h"
48 #include "utils/flatfiles.h"
49 #include "utils/fmgroids.h"
50 #include "utils/guc.h"
51 #include "utils/lsyscache.h"
52 #include "utils/syscache.h"
55 /* non-export function prototypes */
56 static bool get_db_info(const char *name, Oid *dbIdP, Oid *ownerIdP,
57 int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP,
59 TransactionId *dbVacuumXidP, TransactionId *dbFrozenXidP,
61 static bool have_createdb_privilege(void);
62 static void remove_dbtablespaces(Oid db_id);
69 createdb(const CreatedbStmt *stmt)
79 TransactionId src_vacuumxid;
80 TransactionId src_frozenxid;
81 Oid src_deftablespace;
82 volatile Oid dst_deftablespace;
83 volatile Relation pg_database_rel;
85 TupleDesc pg_database_dsc;
86 Datum new_record[Natts_pg_database];
87 char new_record_nulls[Natts_pg_database];
91 DefElem *dtablespacename = NULL;
92 DefElem *downer = NULL;
93 DefElem *dtemplate = NULL;
94 DefElem *dencoding = NULL;
95 DefElem *dconnlimit = NULL;
96 char *dbname = stmt->dbname;
98 const char *dbtemplate = NULL;
99 volatile int encoding = -1;
100 volatile int dbconnlimit = -1;
102 /* don't call this in a transaction block */
103 PreventTransactionChain((void *) stmt, "CREATE DATABASE");
105 /* Extract options from the statement node tree */
106 foreach(option, stmt->options)
108 DefElem *defel = (DefElem *) lfirst(option);
110 if (strcmp(defel->defname, "tablespace") == 0)
114 (errcode(ERRCODE_SYNTAX_ERROR),
115 errmsg("conflicting or redundant options")));
116 dtablespacename = defel;
118 else if (strcmp(defel->defname, "owner") == 0)
122 (errcode(ERRCODE_SYNTAX_ERROR),
123 errmsg("conflicting or redundant options")));
126 else if (strcmp(defel->defname, "template") == 0)
130 (errcode(ERRCODE_SYNTAX_ERROR),
131 errmsg("conflicting or redundant options")));
134 else if (strcmp(defel->defname, "encoding") == 0)
138 (errcode(ERRCODE_SYNTAX_ERROR),
139 errmsg("conflicting or redundant options")));
142 else if (strcmp(defel->defname, "connectionlimit") == 0)
146 (errcode(ERRCODE_SYNTAX_ERROR),
147 errmsg("conflicting or redundant options")));
150 else if (strcmp(defel->defname, "location") == 0)
153 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
154 errmsg("LOCATION is not supported anymore"),
155 errhint("Consider using tablespaces instead.")));
158 elog(ERROR, "option \"%s\" not recognized",
162 if (downer && downer->arg)
163 dbowner = strVal(downer->arg);
164 if (dtemplate && dtemplate->arg)
165 dbtemplate = strVal(dtemplate->arg);
166 if (dencoding && dencoding->arg)
168 const char *encoding_name;
170 if (IsA(dencoding->arg, Integer))
172 encoding = intVal(dencoding->arg);
173 encoding_name = pg_encoding_to_char(encoding);
174 if (strcmp(encoding_name, "") == 0 ||
175 pg_valid_server_encoding(encoding_name) < 0)
177 (errcode(ERRCODE_UNDEFINED_OBJECT),
178 errmsg("%d is not a valid encoding code",
181 else if (IsA(dencoding->arg, String))
183 encoding_name = strVal(dencoding->arg);
184 if (pg_valid_server_encoding(encoding_name) < 0)
186 (errcode(ERRCODE_UNDEFINED_OBJECT),
187 errmsg("%s is not a valid encoding name",
189 encoding = pg_char_to_encoding(encoding_name);
192 elog(ERROR, "unrecognized node type: %d",
193 nodeTag(dencoding->arg));
195 if (dconnlimit && dconnlimit->arg)
196 dbconnlimit = intVal(dconnlimit->arg);
198 /* obtain OID of proposed owner */
200 datdba = get_roleid_checked(dbowner);
202 datdba = GetUserId();
205 * To create a database, must have createdb privilege and must be able to
206 * become the target role (this does not imply that the target role itself
207 * must have createdb privilege). The latter provision guards against
208 * "giveaway" attacks. Note that a superuser will always have both of
209 * these privileges a fortiori.
211 if (!have_createdb_privilege())
213 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
214 errmsg("permission denied to create database")));
216 check_is_member_of_role(GetUserId(), datdba);
219 * Check for db name conflict. There is a race condition here, since
220 * another backend could create the same DB name before we commit.
221 * However, holding an exclusive lock on pg_database for the whole time we
222 * are copying the source database doesn't seem like a good idea, so
223 * accept possibility of race to create. We will check again after we
224 * grab the exclusive lock.
226 if (get_db_info(dbname, NULL, NULL, NULL,
227 NULL, NULL, NULL, NULL, NULL, NULL))
229 (errcode(ERRCODE_DUPLICATE_DATABASE),
230 errmsg("database \"%s\" already exists", dbname)));
233 * Lookup database (template) to be cloned.
236 dbtemplate = "template1"; /* Default template database name */
238 if (!get_db_info(dbtemplate, &src_dboid, &src_owner, &src_encoding,
239 &src_istemplate, &src_allowconn, &src_lastsysoid,
240 &src_vacuumxid, &src_frozenxid, &src_deftablespace))
242 (errcode(ERRCODE_UNDEFINED_DATABASE),
243 errmsg("template database \"%s\" does not exist", dbtemplate)));
246 * Permission check: to copy a DB that's not marked datistemplate, you
247 * must be superuser or the owner thereof.
251 if (!pg_database_ownercheck(src_dboid, GetUserId()))
253 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
254 errmsg("permission denied to copy database \"%s\"",
259 * The source DB can't have any active backends, except this one
260 * (exception is to allow CREATE DB while connected to template1).
261 * Otherwise we might copy inconsistent data. This check is not
262 * bulletproof, since someone might connect while we are copying...
264 if (DatabaseHasActiveBackends(src_dboid, true))
266 (errcode(ERRCODE_OBJECT_IN_USE),
267 errmsg("source database \"%s\" is being accessed by other users",
270 /* If encoding is defaulted, use source's encoding */
272 encoding = src_encoding;
274 /* Some encodings are client only */
275 if (!PG_VALID_BE_ENCODING(encoding))
277 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
278 errmsg("invalid server encoding %d", encoding)));
280 /* Resolve default tablespace for new database */
281 if (dtablespacename && dtablespacename->arg)
283 char *tablespacename;
286 tablespacename = strVal(dtablespacename->arg);
287 dst_deftablespace = get_tablespace_oid(tablespacename);
288 if (!OidIsValid(dst_deftablespace))
290 (errcode(ERRCODE_UNDEFINED_OBJECT),
291 errmsg("tablespace \"%s\" does not exist",
293 /* check permissions */
294 aclresult = pg_tablespace_aclcheck(dst_deftablespace, GetUserId(),
296 if (aclresult != ACLCHECK_OK)
297 aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
301 * If we are trying to change the default tablespace of the template,
302 * we require that the template not have any files in the new default
303 * tablespace. This is necessary because otherwise the copied
304 * database would contain pg_class rows that refer to its default
305 * tablespace both explicitly (by OID) and implicitly (as zero), which
306 * would cause problems. For example another CREATE DATABASE using
307 * the copied database as template, and trying to change its default
308 * tablespace again, would yield outright incorrect results (it would
309 * improperly move tables to the new default tablespace that should
310 * stay in the same tablespace).
312 if (dst_deftablespace != src_deftablespace)
317 srcpath = GetDatabasePath(src_dboid, dst_deftablespace);
319 if (stat(srcpath, &st) == 0 &&
320 S_ISDIR(st.st_mode) &&
321 !directory_is_empty(srcpath))
323 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
324 errmsg("cannot assign new default tablespace \"%s\"",
326 errdetail("There is a conflict because database \"%s\" already has some tables in this tablespace.",
333 /* Use template database's default tablespace */
334 dst_deftablespace = src_deftablespace;
335 /* Note there is no additional permission check in this path */
339 * Normally we mark the new database with the same datvacuumxid and
340 * datfrozenxid as the source. However, if the source is not allowing
341 * connections then we assume it is fully frozen, and we can set the
342 * current transaction ID as the xid limits. This avoids immediately
343 * starting to generate warnings after cloning template0.
346 src_vacuumxid = src_frozenxid = GetCurrentTransactionId();
349 * Preassign OID for pg_database tuple, so that we can compute db path. We
350 * have to open pg_database to do this, but we don't want to take
351 * ExclusiveLock yet, so just do it and close again.
353 pg_database_rel = heap_open(DatabaseRelationId, AccessShareLock);
354 dboid = GetNewOid(pg_database_rel);
355 heap_close(pg_database_rel, AccessShareLock);
356 pg_database_rel = NULL;
359 * Force dirty buffers out to disk, to ensure source database is
360 * up-to-date for the copy. (We really only need to flush buffers for the
361 * source database, but bufmgr.c provides no API for that.)
366 * Once we start copying subdirectories, we need to be able to clean 'em
367 * up if we fail. Establish a TRY block to make sure this happens. (This
368 * is not a 100% solution, because of the possibility of failure during
369 * transaction commit after we leave this routine, but it should handle
375 * Iterate through all tablespaces of the template database, and copy
376 * each one to the new database.
378 rel = heap_open(TableSpaceRelationId, AccessShareLock);
379 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
380 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
382 Oid srctablespace = HeapTupleGetOid(tuple);
388 /* No need to copy global tablespace */
389 if (srctablespace == GLOBALTABLESPACE_OID)
392 srcpath = GetDatabasePath(src_dboid, srctablespace);
394 if (stat(srcpath, &st) < 0 || !S_ISDIR(st.st_mode) ||
395 directory_is_empty(srcpath))
397 /* Assume we can ignore it */
402 if (srctablespace == src_deftablespace)
403 dsttablespace = dst_deftablespace;
405 dsttablespace = srctablespace;
407 dstpath = GetDatabasePath(dboid, dsttablespace);
410 * Copy this subdirectory to the new location
412 * We don't need to copy subdirectories
414 copydir(srcpath, dstpath, false);
416 /* Record the filesystem change in XLOG */
418 xl_dbase_create_rec xlrec;
419 XLogRecData rdata[1];
422 xlrec.tablespace_id = dsttablespace;
423 xlrec.src_db_id = src_dboid;
424 xlrec.src_tablespace_id = srctablespace;
426 rdata[0].data = (char *) &xlrec;
427 rdata[0].len = sizeof(xl_dbase_create_rec);
428 rdata[0].buffer = InvalidBuffer;
429 rdata[0].next = NULL;
431 (void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_CREATE, rdata);
435 heap_close(rel, AccessShareLock);
438 * Now OK to grab exclusive lock on pg_database.
440 pg_database_rel = heap_open(DatabaseRelationId, ExclusiveLock);
442 /* Check to see if someone else created same DB name meanwhile. */
443 if (get_db_info(dbname, NULL, NULL, NULL,
444 NULL, NULL, NULL, NULL, NULL, NULL))
446 (errcode(ERRCODE_DUPLICATE_DATABASE),
447 errmsg("database \"%s\" already exists", dbname)));
450 * Insert a new tuple into pg_database
452 pg_database_dsc = RelationGetDescr(pg_database_rel);
455 MemSet(new_record, 0, sizeof(new_record));
456 MemSet(new_record_nulls, ' ', sizeof(new_record_nulls));
458 new_record[Anum_pg_database_datname - 1] =
459 DirectFunctionCall1(namein, CStringGetDatum(dbname));
460 new_record[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(datdba);
461 new_record[Anum_pg_database_encoding - 1] = Int32GetDatum(encoding);
462 new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(false);
463 new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(true);
464 new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit);
465 new_record[Anum_pg_database_datlastsysoid - 1] = ObjectIdGetDatum(src_lastsysoid);
466 new_record[Anum_pg_database_datvacuumxid - 1] = TransactionIdGetDatum(src_vacuumxid);
467 new_record[Anum_pg_database_datfrozenxid - 1] = TransactionIdGetDatum(src_frozenxid);
468 new_record[Anum_pg_database_dattablespace - 1] = ObjectIdGetDatum(dst_deftablespace);
471 * We deliberately set datconfig and datacl to defaults (NULL), rather
472 * than copying them from the template database. Copying datacl would
473 * be a bad idea when the owner is not the same as the template's
474 * owner. It's more debatable whether datconfig should be copied.
476 new_record_nulls[Anum_pg_database_datconfig - 1] = 'n';
477 new_record_nulls[Anum_pg_database_datacl - 1] = 'n';
479 tuple = heap_formtuple(pg_database_dsc, new_record, new_record_nulls);
481 HeapTupleSetOid(tuple, dboid); /* override heap_insert's OID
484 simple_heap_insert(pg_database_rel, tuple);
487 CatalogUpdateIndexes(pg_database_rel, tuple);
489 /* Register owner dependency */
490 recordDependencyOnOwner(DatabaseRelationId, dboid, datdba);
492 /* Create pg_shdepend entries for objects within database */
493 copyTemplateDependencies(src_dboid, dboid);
496 * We force a checkpoint before committing. This effectively means
497 * that committed XLOG_DBASE_CREATE operations will never need to be
498 * replayed (at least not in ordinary crash recovery; we still have to
499 * make the XLOG entry for the benefit of PITR operations). This
500 * avoids two nasty scenarios:
502 * #1: When PITR is off, we don't XLOG the contents of newly created
503 * indexes; therefore the drop-and-recreate-whole-directory behavior
504 * of DBASE_CREATE replay would lose such indexes.
506 * #2: Since we have to recopy the source database during DBASE_CREATE
507 * replay, we run the risk of copying changes in it that were
508 * committed after the original CREATE DATABASE command but before the
509 * system crash that led to the replay. This is at least unexpected
510 * and at worst could lead to inconsistencies, eg duplicate table
513 * (Both of these were real bugs in releases 8.0 through 8.0.3.)
515 * In PITR replay, the first of these isn't an issue, and the second
516 * is only a risk if the CREATE DATABASE and subsequent template
517 * database change both occur while a base backup is being taken.
518 * There doesn't seem to be much we can do about that except document
519 * it as a limitation.
521 * Perhaps if we ever implement CREATE DATABASE in a less cheesy way,
524 RequestCheckpoint(true, false);
527 * Set flag to update flat database file at commit.
529 database_file_update_needed();
533 /* Don't hold pg_database lock while doing recursive remove */
534 if (pg_database_rel != NULL)
535 heap_close(pg_database_rel, ExclusiveLock);
537 /* Throw away any successfully copied subdirectories */
538 remove_dbtablespaces(dboid);
544 /* Close pg_database, but keep exclusive lock till commit */
545 /* This has to be outside the PG_TRY */
546 heap_close(pg_database_rel, NoLock);
554 dropdb(const char *dbname, bool missing_ok)
561 PreventTransactionChain((void *) dbname, "DROP DATABASE");
565 if (strcmp(dbname, get_database_name(MyDatabaseId)) == 0)
567 (errcode(ERRCODE_OBJECT_IN_USE),
568 errmsg("cannot drop the currently open database")));
571 * Obtain exclusive lock on pg_database. We need this to ensure that no
572 * new backend starts up in the target database while we are deleting it.
573 * (Actually, a new backend might still manage to start up, because it
574 * isn't able to lock pg_database while starting. But it will detect its
575 * error in ReverifyMyDatabase and shut down before any serious damage is
576 * done. See postinit.c.)
578 * An ExclusiveLock, rather than AccessExclusiveLock, is sufficient since
579 * ReverifyMyDatabase takes RowShareLock. This allows ordinary readers of
580 * pg_database to proceed in parallel.
582 pgdbrel = heap_open(DatabaseRelationId, ExclusiveLock);
584 if (!get_db_info(dbname, &db_id, NULL, NULL,
585 &db_istemplate, NULL, NULL, NULL, NULL, NULL))
590 (errcode(ERRCODE_UNDEFINED_DATABASE),
591 errmsg("database \"%s\" does not exist", dbname)));
596 /* Close pg_database, release the lock, since we changed nothing */
597 heap_close(pgdbrel, ExclusiveLock);
599 (errmsg("database \"%s\" does not exist, skipping",
606 if (!pg_database_ownercheck(db_id, GetUserId()))
607 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
611 * Disallow dropping a DB that is marked istemplate. This is just to
612 * prevent people from accidentally dropping template0 or template1; they
613 * can do so if they're really determined ...
617 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
618 errmsg("cannot drop a template database")));
621 * Check for active backends in the target database.
623 if (DatabaseHasActiveBackends(db_id, false))
625 (errcode(ERRCODE_OBJECT_IN_USE),
626 errmsg("database \"%s\" is being accessed by other users",
630 * Remove the database's tuple from pg_database.
632 tup = SearchSysCache(DATABASEOID,
633 ObjectIdGetDatum(db_id),
635 if (!HeapTupleIsValid(tup))
636 elog(ERROR, "cache lookup failed for database %u", db_id);
638 simple_heap_delete(pgdbrel, &tup->t_self);
640 ReleaseSysCache(tup);
643 * Delete any comments associated with the database
646 DeleteSharedComments(db_id, DatabaseRelationId);
649 * Remove shared dependency references for the database.
651 dropDatabaseDependencies(db_id);
654 * Drop pages for this database that are in the shared buffer cache. This
655 * is important to ensure that no remaining backend tries to write out a
656 * dirty buffer to the dead database later...
658 DropDatabaseBuffers(db_id);
661 * Also, clean out any entries in the shared free space map.
663 FreeSpaceMapForgetDatabase(db_id);
666 * On Windows, force a checkpoint so that the bgwriter doesn't hold any
667 * open files, which would cause rmdir() to fail.
670 RequestCheckpoint(true, false);
674 * Remove all tablespace subdirs belonging to the database.
676 remove_dbtablespaces(db_id);
678 /* Close pg_database, but keep exclusive lock till commit */
679 heap_close(pgdbrel, NoLock);
682 * Set flag to update flat database file at commit.
684 database_file_update_needed();
692 RenameDatabase(const char *oldname, const char *newname)
703 * Obtain ExclusiveLock so that no new session gets started while the
704 * rename is in progress.
706 rel = heap_open(DatabaseRelationId, ExclusiveLock);
709 Anum_pg_database_datname,
710 BTEqualStrategyNumber, F_NAMEEQ,
711 NameGetDatum(oldname));
712 scan = systable_beginscan(rel, DatabaseNameIndexId, true,
713 SnapshotNow, 1, &key);
715 tup = systable_getnext(scan);
716 if (!HeapTupleIsValid(tup))
718 (errcode(ERRCODE_UNDEFINED_DATABASE),
719 errmsg("database \"%s\" does not exist", oldname)));
722 * XXX Client applications probably store the current database somewhere,
723 * so renaming it could cause confusion. On the other hand, there may not
724 * be an actual problem besides a little confusion, so think about this
727 if (HeapTupleGetOid(tup) == MyDatabaseId)
729 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
730 errmsg("current database may not be renamed")));
733 * Make sure the database does not have active sessions. Might not be
734 * necessary, but it's consistent with other database operations.
736 if (DatabaseHasActiveBackends(HeapTupleGetOid(tup), false))
738 (errcode(ERRCODE_OBJECT_IN_USE),
739 errmsg("database \"%s\" is being accessed by other users",
742 /* make sure the new name doesn't exist */
744 Anum_pg_database_datname,
745 BTEqualStrategyNumber, F_NAMEEQ,
746 NameGetDatum(newname));
747 scan2 = systable_beginscan(rel, DatabaseNameIndexId, true,
748 SnapshotNow, 1, &key2);
749 if (HeapTupleIsValid(systable_getnext(scan2)))
751 (errcode(ERRCODE_DUPLICATE_DATABASE),
752 errmsg("database \"%s\" already exists", newname)));
753 systable_endscan(scan2);
756 if (!pg_database_ownercheck(HeapTupleGetOid(tup), GetUserId()))
757 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
760 /* must have createdb rights */
761 if (!have_createdb_privilege())
763 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
764 errmsg("permission denied to rename database")));
767 newtup = heap_copytuple(tup);
768 namestrcpy(&(((Form_pg_database) GETSTRUCT(newtup))->datname), newname);
769 simple_heap_update(rel, &newtup->t_self, newtup);
770 CatalogUpdateIndexes(rel, newtup);
772 systable_endscan(scan);
774 /* Close pg_database, but keep exclusive lock till commit */
775 heap_close(rel, NoLock);
778 * Set flag to update flat database file at commit.
780 database_file_update_needed();
785 * ALTER DATABASE name ...
788 AlterDatabase(AlterDatabaseStmt *stmt)
797 DefElem *dconnlimit = NULL;
798 Datum new_record[Natts_pg_database];
799 char new_record_nulls[Natts_pg_database];
800 char new_record_repl[Natts_pg_database];
802 /* Extract options from the statement node tree */
803 foreach(option, stmt->options)
805 DefElem *defel = (DefElem *) lfirst(option);
807 if (strcmp(defel->defname, "connectionlimit") == 0)
811 (errcode(ERRCODE_SYNTAX_ERROR),
812 errmsg("conflicting or redundant options")));
816 elog(ERROR, "option \"%s\" not recognized",
821 connlimit = intVal(dconnlimit->arg);
824 * We don't need ExclusiveLock since we aren't updating the flat file.
826 rel = heap_open(DatabaseRelationId, RowExclusiveLock);
827 ScanKeyInit(&scankey,
828 Anum_pg_database_datname,
829 BTEqualStrategyNumber, F_NAMEEQ,
830 NameGetDatum(stmt->dbname));
831 scan = systable_beginscan(rel, DatabaseNameIndexId, true,
832 SnapshotNow, 1, &scankey);
833 tuple = systable_getnext(scan);
834 if (!HeapTupleIsValid(tuple))
836 (errcode(ERRCODE_UNDEFINED_DATABASE),
837 errmsg("database \"%s\" does not exist", stmt->dbname)));
839 if (!pg_database_ownercheck(HeapTupleGetOid(tuple), GetUserId()))
840 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
844 * Build an updated tuple, perusing the information just obtained
846 MemSet(new_record, 0, sizeof(new_record));
847 MemSet(new_record_nulls, ' ', sizeof(new_record_nulls));
848 MemSet(new_record_repl, ' ', sizeof(new_record_repl));
852 new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(connlimit);
853 new_record_repl[Anum_pg_database_datconnlimit - 1] = 'r';
856 newtuple = heap_modifytuple(tuple, RelationGetDescr(rel), new_record,
857 new_record_nulls, new_record_repl);
858 simple_heap_update(rel, &tuple->t_self, newtuple);
861 CatalogUpdateIndexes(rel, newtuple);
863 systable_endscan(scan);
865 /* Close pg_database, but keep lock till commit */
866 heap_close(rel, NoLock);
869 * We don't bother updating the flat file since the existing options for
870 * ALTER DATABASE don't affect it.
876 * ALTER DATABASE name SET ...
879 AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
887 Datum repl_val[Natts_pg_database];
888 char repl_null[Natts_pg_database];
889 char repl_repl[Natts_pg_database];
891 valuestr = flatten_set_variable_args(stmt->variable, stmt->value);
894 * We don't need ExclusiveLock since we aren't updating the flat file.
896 rel = heap_open(DatabaseRelationId, RowExclusiveLock);
897 ScanKeyInit(&scankey,
898 Anum_pg_database_datname,
899 BTEqualStrategyNumber, F_NAMEEQ,
900 NameGetDatum(stmt->dbname));
901 scan = systable_beginscan(rel, DatabaseNameIndexId, true,
902 SnapshotNow, 1, &scankey);
903 tuple = systable_getnext(scan);
904 if (!HeapTupleIsValid(tuple))
906 (errcode(ERRCODE_UNDEFINED_DATABASE),
907 errmsg("database \"%s\" does not exist", stmt->dbname)));
909 if (!pg_database_ownercheck(HeapTupleGetOid(tuple), GetUserId()))
910 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
913 MemSet(repl_repl, ' ', sizeof(repl_repl));
914 repl_repl[Anum_pg_database_datconfig - 1] = 'r';
916 if (strcmp(stmt->variable, "all") == 0 && valuestr == NULL)
919 repl_null[Anum_pg_database_datconfig - 1] = 'n';
920 repl_val[Anum_pg_database_datconfig - 1] = (Datum) 0;
928 repl_null[Anum_pg_database_datconfig - 1] = ' ';
930 datum = heap_getattr(tuple, Anum_pg_database_datconfig,
931 RelationGetDescr(rel), &isnull);
933 a = isnull ? NULL : DatumGetArrayTypeP(datum);
936 a = GUCArrayAdd(a, stmt->variable, valuestr);
938 a = GUCArrayDelete(a, stmt->variable);
941 repl_val[Anum_pg_database_datconfig - 1] = PointerGetDatum(a);
943 repl_null[Anum_pg_database_datconfig - 1] = 'n';
946 newtuple = heap_modifytuple(tuple, RelationGetDescr(rel), repl_val, repl_null, repl_repl);
947 simple_heap_update(rel, &tuple->t_self, newtuple);
950 CatalogUpdateIndexes(rel, newtuple);
952 systable_endscan(scan);
954 /* Close pg_database, but keep lock till commit */
955 heap_close(rel, NoLock);
958 * We don't bother updating the flat file since ALTER DATABASE SET doesn't
965 * ALTER DATABASE name OWNER TO newowner
968 AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
974 Form_pg_database datForm;
977 * We don't need ExclusiveLock since we aren't updating the flat file.
979 rel = heap_open(DatabaseRelationId, RowExclusiveLock);
980 ScanKeyInit(&scankey,
981 Anum_pg_database_datname,
982 BTEqualStrategyNumber, F_NAMEEQ,
983 NameGetDatum(dbname));
984 scan = systable_beginscan(rel, DatabaseNameIndexId, true,
985 SnapshotNow, 1, &scankey);
986 tuple = systable_getnext(scan);
987 if (!HeapTupleIsValid(tuple))
989 (errcode(ERRCODE_UNDEFINED_DATABASE),
990 errmsg("database \"%s\" does not exist", dbname)));
992 datForm = (Form_pg_database) GETSTRUCT(tuple);
995 * If the new owner is the same as the existing owner, consider the
996 * command to have succeeded. This is to be consistent with other
999 if (datForm->datdba != newOwnerId)
1001 Datum repl_val[Natts_pg_database];
1002 char repl_null[Natts_pg_database];
1003 char repl_repl[Natts_pg_database];
1009 /* Otherwise, must be owner of the existing object */
1010 if (!pg_database_ownercheck(HeapTupleGetOid(tuple), GetUserId()))
1011 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
1014 /* Must be able to become new owner */
1015 check_is_member_of_role(GetUserId(), newOwnerId);
1018 * must have createdb rights
1020 * NOTE: This is different from other alter-owner checks in that the
1021 * current user is checked for createdb privileges instead of the
1022 * destination owner. This is consistent with the CREATE case for
1023 * databases. Because superusers will always have this right, we need
1024 * no special case for them.
1026 if (!have_createdb_privilege())
1028 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1029 errmsg("permission denied to change owner of database")));
1031 memset(repl_null, ' ', sizeof(repl_null));
1032 memset(repl_repl, ' ', sizeof(repl_repl));
1034 repl_repl[Anum_pg_database_datdba - 1] = 'r';
1035 repl_val[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(newOwnerId);
1038 * Determine the modified ACL for the new owner. This is only
1039 * necessary when the ACL is non-null.
1041 aclDatum = heap_getattr(tuple,
1042 Anum_pg_database_datacl,
1043 RelationGetDescr(rel),
1047 newAcl = aclnewowner(DatumGetAclP(aclDatum),
1048 datForm->datdba, newOwnerId);
1049 repl_repl[Anum_pg_database_datacl - 1] = 'r';
1050 repl_val[Anum_pg_database_datacl - 1] = PointerGetDatum(newAcl);
1053 newtuple = heap_modifytuple(tuple, RelationGetDescr(rel), repl_val, repl_null, repl_repl);
1054 simple_heap_update(rel, &newtuple->t_self, newtuple);
1055 CatalogUpdateIndexes(rel, newtuple);
1057 heap_freetuple(newtuple);
1059 /* Update owner dependency reference */
1060 changeDependencyOnOwner(DatabaseRelationId, HeapTupleGetOid(tuple),
1064 systable_endscan(scan);
1066 /* Close pg_database, but keep lock till commit */
1067 heap_close(rel, NoLock);
1070 * We don't bother updating the flat file since ALTER DATABASE OWNER
1071 * doesn't affect it.
1081 get_db_info(const char *name, Oid *dbIdP, Oid *ownerIdP,
1082 int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP,
1084 TransactionId *dbVacuumXidP, TransactionId *dbFrozenXidP,
1088 ScanKeyData scanKey;
1095 /* Caller may wish to grab a better lock on pg_database beforehand... */
1096 relation = heap_open(DatabaseRelationId, AccessShareLock);
1098 ScanKeyInit(&scanKey,
1099 Anum_pg_database_datname,
1100 BTEqualStrategyNumber, F_NAMEEQ,
1101 NameGetDatum(name));
1103 scan = systable_beginscan(relation, DatabaseNameIndexId, true,
1104 SnapshotNow, 1, &scanKey);
1106 tuple = systable_getnext(scan);
1108 gottuple = HeapTupleIsValid(tuple);
1111 Form_pg_database dbform = (Form_pg_database) GETSTRUCT(tuple);
1113 /* oid of the database */
1115 *dbIdP = HeapTupleGetOid(tuple);
1116 /* oid of the owner */
1118 *ownerIdP = dbform->datdba;
1119 /* character encoding */
1121 *encodingP = dbform->encoding;
1122 /* allowed as template? */
1124 *dbIsTemplateP = dbform->datistemplate;
1125 /* allowing connections? */
1127 *dbAllowConnP = dbform->datallowconn;
1128 /* last system OID used in database */
1130 *dbLastSysOidP = dbform->datlastsysoid;
1131 /* limit of vacuumed XIDs */
1133 *dbVacuumXidP = dbform->datvacuumxid;
1134 /* limit of frozen XIDs */
1136 *dbFrozenXidP = dbform->datfrozenxid;
1137 /* default tablespace for this database */
1139 *dbTablespace = dbform->dattablespace;
1142 systable_endscan(scan);
1143 heap_close(relation, AccessShareLock);
1148 /* Check if current user has createdb privileges */
1150 have_createdb_privilege(void)
1152 bool result = false;
1155 /* Superusers can always do everything */
1159 utup = SearchSysCache(AUTHOID,
1160 ObjectIdGetDatum(GetUserId()),
1162 if (HeapTupleIsValid(utup))
1164 result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreatedb;
1165 ReleaseSysCache(utup);
1171 * Remove tablespace directories
1173 * We don't know what tablespaces db_id is using, so iterate through all
1174 * tablespaces removing <tablespace>/db_id
1177 remove_dbtablespaces(Oid db_id)
1183 rel = heap_open(TableSpaceRelationId, AccessShareLock);
1184 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
1185 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1187 Oid dsttablespace = HeapTupleGetOid(tuple);
1191 /* Don't mess with the global tablespace */
1192 if (dsttablespace == GLOBALTABLESPACE_OID)
1195 dstpath = GetDatabasePath(db_id, dsttablespace);
1197 if (stat(dstpath, &st) < 0 || !S_ISDIR(st.st_mode))
1199 /* Assume we can ignore it */
1204 if (!rmtree(dstpath, true))
1206 (errmsg("could not remove database directory \"%s\"",
1209 /* Record the filesystem change in XLOG */
1211 xl_dbase_drop_rec xlrec;
1212 XLogRecData rdata[1];
1214 xlrec.db_id = db_id;
1215 xlrec.tablespace_id = dsttablespace;
1217 rdata[0].data = (char *) &xlrec;
1218 rdata[0].len = sizeof(xl_dbase_drop_rec);
1219 rdata[0].buffer = InvalidBuffer;
1220 rdata[0].next = NULL;
1222 (void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_DROP, rdata);
1229 heap_close(rel, AccessShareLock);
1234 * get_database_oid - given a database name, look up the OID
1236 * Returns InvalidOid if database name not found.
1238 * This is not actually used in this file, but is exported for use elsewhere.
1241 get_database_oid(const char *dbname)
1243 Relation pg_database;
1244 ScanKeyData entry[1];
1250 * There's no syscache for pg_database indexed by name,
1251 * so we must look the hard way.
1253 pg_database = heap_open(DatabaseRelationId, AccessShareLock);
1254 ScanKeyInit(&entry[0],
1255 Anum_pg_database_datname,
1256 BTEqualStrategyNumber, F_NAMEEQ,
1257 CStringGetDatum(dbname));
1258 scan = systable_beginscan(pg_database, DatabaseNameIndexId, true,
1259 SnapshotNow, 1, entry);
1261 dbtuple = systable_getnext(scan);
1263 /* We assume that there can be at most one matching tuple */
1264 if (HeapTupleIsValid(dbtuple))
1265 oid = HeapTupleGetOid(dbtuple);
1269 systable_endscan(scan);
1270 heap_close(pg_database, AccessShareLock);
1277 * get_database_name - given a database OID, look up the name
1279 * Returns a palloc'd string, or NULL if no such database.
1281 * This is not actually used in this file, but is exported for use elsewhere.
1284 get_database_name(Oid dbid)
1289 dbtuple = SearchSysCache(DATABASEOID,
1290 ObjectIdGetDatum(dbid),
1292 if (HeapTupleIsValid(dbtuple))
1294 result = pstrdup(NameStr(((Form_pg_database) GETSTRUCT(dbtuple))->datname));
1295 ReleaseSysCache(dbtuple);
1304 * DATABASE resource manager's routines
1307 dbase_redo(XLogRecPtr lsn, XLogRecord *record)
1309 uint8 info = record->xl_info & ~XLR_INFO_MASK;
1311 if (info == XLOG_DBASE_CREATE)
1313 xl_dbase_create_rec *xlrec = (xl_dbase_create_rec *) XLogRecGetData(record);
1318 src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id);
1319 dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
1322 * Our theory for replaying a CREATE is to forcibly drop the target
1323 * subdirectory if present, then re-copy the source data. This may be
1324 * more work than needed, but it is simple to implement.
1326 if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
1328 if (!rmtree(dst_path, true))
1330 (errmsg("could not remove database directory \"%s\"",
1335 * Force dirty buffers out to disk, to ensure source database is
1336 * up-to-date for the copy. (We really only need to flush buffers for
1337 * the source database, but bufmgr.c provides no API for that.)
1342 * Copy this subdirectory to the new location
1344 * We don't need to copy subdirectories
1346 copydir(src_path, dst_path, false);
1348 else if (info == XLOG_DBASE_DROP)
1350 xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) XLogRecGetData(record);
1353 dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
1355 /* Drop pages for this database that are in the shared buffer cache */
1356 DropDatabaseBuffers(xlrec->db_id);
1358 /* Also, clean out any entries in the shared free space map */
1359 FreeSpaceMapForgetDatabase(xlrec->db_id);
1361 /* Clean out the xlog relcache too */
1362 XLogDropDatabase(xlrec->db_id);
1364 /* And remove the physical files */
1365 if (!rmtree(dst_path, true))
1367 (errmsg("could not remove database directory \"%s\"",
1371 elog(PANIC, "dbase_redo: unknown op code %u", info);
1375 dbase_desc(StringInfo buf, uint8 xl_info, char *rec)
1377 uint8 info = xl_info & ~XLR_INFO_MASK;
1379 if (info == XLOG_DBASE_CREATE)
1381 xl_dbase_create_rec *xlrec = (xl_dbase_create_rec *) rec;
1383 appendStringInfo(buf, "create db: copy dir %u/%u to %u/%u",
1384 xlrec->src_db_id, xlrec->src_tablespace_id,
1385 xlrec->db_id, xlrec->tablespace_id);
1387 else if (info == XLOG_DBASE_DROP)
1389 xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) rec;
1391 appendStringInfo(buf, "drop db: dir %u/%u",
1392 xlrec->db_id, xlrec->tablespace_id);
1395 appendStringInfo(buf, "UNKNOWN");