1 /*-------------------------------------------------------------------------
4 * Database management commands (create/drop database).
6 * Note: database creation/destruction commands use exclusive locks on
7 * the database objects (as expressed by LockSharedObject()) to avoid
8 * stepping on each others' toes. Formerly we used table-level locks
9 * on pg_database, but that's too coarse-grained.
11 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
16 * src/backend/commands/dbcommands.c
18 *-------------------------------------------------------------------------
27 #include "access/genam.h"
28 #include "access/heapam.h"
29 #include "access/htup_details.h"
30 #include "access/xact.h"
31 #include "access/xlogutils.h"
32 #include "catalog/catalog.h"
33 #include "catalog/dependency.h"
34 #include "catalog/indexing.h"
35 #include "catalog/objectaccess.h"
36 #include "catalog/pg_authid.h"
37 #include "catalog/pg_database.h"
38 #include "catalog/pg_db_role_setting.h"
39 #include "catalog/pg_tablespace.h"
40 #include "commands/comment.h"
41 #include "commands/dbcommands.h"
42 #include "commands/defrem.h"
43 #include "commands/seclabel.h"
44 #include "commands/tablespace.h"
45 #include "mb/pg_wchar.h"
46 #include "miscadmin.h"
48 #include "postmaster/bgwriter.h"
49 #include "replication/slot.h"
50 #include "storage/copydir.h"
51 #include "storage/fd.h"
52 #include "storage/lmgr.h"
53 #include "storage/ipc.h"
54 #include "storage/procarray.h"
55 #include "storage/smgr.h"
56 #include "utils/acl.h"
57 #include "utils/builtins.h"
58 #include "utils/fmgroids.h"
59 #include "utils/pg_locale.h"
60 #include "utils/snapmgr.h"
61 #include "utils/syscache.h"
62 #include "utils/tqual.h"
67 Oid src_dboid; /* source (template) DB */
68 Oid dest_dboid; /* DB we are trying to create */
69 } createdb_failure_params;
73 Oid dest_dboid; /* DB we are trying to move */
74 Oid dest_tsoid; /* tablespace we are trying to move to */
75 } movedb_failure_params;
77 /* non-export function prototypes */
78 static void createdb_failure_callback(int code, Datum arg);
79 static void movedb(const char *dbname, const char *tblspcname);
80 static void movedb_failure_callback(int code, Datum arg);
81 static bool get_db_info(const char *name, LOCKMODE lockmode,
82 Oid *dbIdP, Oid *ownerIdP,
83 int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP,
84 Oid *dbLastSysOidP, TransactionId *dbFrozenXidP,
85 MultiXactId *dbMinMultiP,
86 Oid *dbTablespace, char **dbCollate, char **dbCtype);
87 static bool have_createdb_privilege(void);
88 static void remove_dbtablespaces(Oid db_id);
89 static bool check_db_file_conflict(Oid db_id);
90 static int errdetail_busy_db(int notherbackends, int npreparedxacts);
97 createdb(const CreatedbStmt *stmt)
109 TransactionId src_frozenxid;
110 MultiXactId src_minmxid;
111 Oid src_deftablespace;
112 volatile Oid dst_deftablespace;
113 Relation pg_database_rel;
115 Datum new_record[Natts_pg_database];
116 bool new_record_nulls[Natts_pg_database];
120 DefElem *dtablespacename = NULL;
121 DefElem *downer = NULL;
122 DefElem *dtemplate = NULL;
123 DefElem *dencoding = NULL;
124 DefElem *dcollate = NULL;
125 DefElem *dctype = NULL;
126 DefElem *distemplate = NULL;
127 DefElem *dallowconnections = NULL;
128 DefElem *dconnlimit = NULL;
129 char *dbname = stmt->dbname;
130 char *dbowner = NULL;
131 const char *dbtemplate = NULL;
132 char *dbcollate = NULL;
133 char *dbctype = NULL;
136 bool dbistemplate = false;
137 bool dballowconnections = true;
138 int dbconnlimit = -1;
141 createdb_failure_params fparms;
143 /* Extract options from the statement node tree */
144 foreach(option, stmt->options)
146 DefElem *defel = (DefElem *) lfirst(option);
148 if (strcmp(defel->defname, "tablespace") == 0)
152 (errcode(ERRCODE_SYNTAX_ERROR),
153 errmsg("conflicting or redundant options")));
154 dtablespacename = defel;
156 else if (strcmp(defel->defname, "owner") == 0)
160 (errcode(ERRCODE_SYNTAX_ERROR),
161 errmsg("conflicting or redundant options")));
164 else if (strcmp(defel->defname, "template") == 0)
168 (errcode(ERRCODE_SYNTAX_ERROR),
169 errmsg("conflicting or redundant options")));
172 else if (strcmp(defel->defname, "encoding") == 0)
176 (errcode(ERRCODE_SYNTAX_ERROR),
177 errmsg("conflicting or redundant options")));
180 else if (strcmp(defel->defname, "lc_collate") == 0)
184 (errcode(ERRCODE_SYNTAX_ERROR),
185 errmsg("conflicting or redundant options")));
188 else if (strcmp(defel->defname, "lc_ctype") == 0)
192 (errcode(ERRCODE_SYNTAX_ERROR),
193 errmsg("conflicting or redundant options")));
196 else if (strcmp(defel->defname, "is_template") == 0)
200 (errcode(ERRCODE_SYNTAX_ERROR),
201 errmsg("conflicting or redundant options")));
204 else if (strcmp(defel->defname, "allow_connections") == 0)
206 if (dallowconnections)
208 (errcode(ERRCODE_SYNTAX_ERROR),
209 errmsg("conflicting or redundant options")));
210 dallowconnections = defel;
212 else if (strcmp(defel->defname, "connection_limit") == 0)
216 (errcode(ERRCODE_SYNTAX_ERROR),
217 errmsg("conflicting or redundant options")));
220 else if (strcmp(defel->defname, "location") == 0)
223 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
224 errmsg("LOCATION is not supported anymore"),
225 errhint("Consider using tablespaces instead.")));
229 (errcode(ERRCODE_SYNTAX_ERROR),
230 errmsg("option \"%s\" not recognized", defel->defname)));
233 if (downer && downer->arg)
234 dbowner = defGetString(downer);
235 if (dtemplate && dtemplate->arg)
236 dbtemplate = defGetString(dtemplate);
237 if (dencoding && dencoding->arg)
239 const char *encoding_name;
241 if (IsA(dencoding->arg, Integer))
243 encoding = defGetInt32(dencoding);
244 encoding_name = pg_encoding_to_char(encoding);
245 if (strcmp(encoding_name, "") == 0 ||
246 pg_valid_server_encoding(encoding_name) < 0)
248 (errcode(ERRCODE_UNDEFINED_OBJECT),
249 errmsg("%d is not a valid encoding code",
254 encoding_name = defGetString(dencoding);
255 encoding = pg_valid_server_encoding(encoding_name);
258 (errcode(ERRCODE_UNDEFINED_OBJECT),
259 errmsg("%s is not a valid encoding name",
263 if (dcollate && dcollate->arg)
264 dbcollate = defGetString(dcollate);
265 if (dctype && dctype->arg)
266 dbctype = defGetString(dctype);
267 if (distemplate && distemplate->arg)
268 dbistemplate = defGetBoolean(distemplate);
269 if (dallowconnections && dallowconnections->arg)
270 dballowconnections = defGetBoolean(dallowconnections);
271 if (dconnlimit && dconnlimit->arg)
273 dbconnlimit = defGetInt32(dconnlimit);
274 if (dbconnlimit < -1)
276 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
277 errmsg("invalid connection limit: %d", dbconnlimit)));
280 /* obtain OID of proposed owner */
282 datdba = get_role_oid(dbowner, false);
284 datdba = GetUserId();
287 * To create a database, must have createdb privilege and must be able to
288 * become the target role (this does not imply that the target role itself
289 * must have createdb privilege). The latter provision guards against
290 * "giveaway" attacks. Note that a superuser will always have both of
291 * these privileges a fortiori.
293 if (!have_createdb_privilege())
295 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
296 errmsg("permission denied to create database")));
298 check_is_member_of_role(GetUserId(), datdba);
301 * Lookup database (template) to be cloned, and obtain share lock on it.
302 * ShareLock allows two CREATE DATABASEs to work from the same template
303 * concurrently, while ensuring no one is busy dropping it in parallel
304 * (which would be Very Bad since we'd likely get an incomplete copy
305 * without knowing it). This also prevents any new connections from being
306 * made to the source until we finish copying it, so we can be sure it
307 * won't change underneath us.
310 dbtemplate = "template1"; /* Default template database name */
312 if (!get_db_info(dbtemplate, ShareLock,
313 &src_dboid, &src_owner, &src_encoding,
314 &src_istemplate, &src_allowconn, &src_lastsysoid,
315 &src_frozenxid, &src_minmxid, &src_deftablespace,
316 &src_collate, &src_ctype))
318 (errcode(ERRCODE_UNDEFINED_DATABASE),
319 errmsg("template database \"%s\" does not exist",
323 * Permission check: to copy a DB that's not marked datistemplate, you
324 * must be superuser or the owner thereof.
328 if (!pg_database_ownercheck(src_dboid, GetUserId()))
330 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
331 errmsg("permission denied to copy database \"%s\"",
335 /* If encoding or locales are defaulted, use source's setting */
337 encoding = src_encoding;
338 if (dbcollate == NULL)
339 dbcollate = src_collate;
343 /* Some encodings are client only */
344 if (!PG_VALID_BE_ENCODING(encoding))
346 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
347 errmsg("invalid server encoding %d", encoding)));
349 /* Check that the chosen locales are valid, and get canonical spellings */
350 if (!check_locale(LC_COLLATE, dbcollate, &canonname))
352 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
353 errmsg("invalid locale name: \"%s\"", dbcollate)));
354 dbcollate = canonname;
355 if (!check_locale(LC_CTYPE, dbctype, &canonname))
357 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
358 errmsg("invalid locale name: \"%s\"", dbctype)));
361 check_encoding_locale_matches(encoding, dbcollate, dbctype);
364 * Check that the new encoding and locale settings match the source
365 * database. We insist on this because we simply copy the source data ---
366 * any non-ASCII data would be wrongly encoded, and any indexes sorted
367 * according to the source locale would be wrong.
369 * However, we assume that template0 doesn't contain any non-ASCII data
370 * nor any indexes that depend on collation or ctype, so template0 can be
371 * used as template for creating a database with any encoding or locale.
373 if (strcmp(dbtemplate, "template0") != 0)
375 if (encoding != src_encoding)
377 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
378 errmsg("new encoding (%s) is incompatible with the encoding of the template database (%s)",
379 pg_encoding_to_char(encoding),
380 pg_encoding_to_char(src_encoding)),
381 errhint("Use the same encoding as in the template database, or use template0 as template.")));
383 if (strcmp(dbcollate, src_collate) != 0)
385 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
386 errmsg("new collation (%s) is incompatible with the collation of the template database (%s)",
387 dbcollate, src_collate),
388 errhint("Use the same collation as in the template database, or use template0 as template.")));
390 if (strcmp(dbctype, src_ctype) != 0)
392 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
393 errmsg("new LC_CTYPE (%s) is incompatible with the LC_CTYPE of the template database (%s)",
395 errhint("Use the same LC_CTYPE as in the template database, or use template0 as template.")));
398 /* Resolve default tablespace for new database */
399 if (dtablespacename && dtablespacename->arg)
401 char *tablespacename;
404 tablespacename = defGetString(dtablespacename);
405 dst_deftablespace = get_tablespace_oid(tablespacename, false);
406 /* check permissions */
407 aclresult = pg_tablespace_aclcheck(dst_deftablespace, GetUserId(),
409 if (aclresult != ACLCHECK_OK)
410 aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
413 /* pg_global must never be the default tablespace */
414 if (dst_deftablespace == GLOBALTABLESPACE_OID)
416 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
417 errmsg("pg_global cannot be used as default tablespace")));
420 * If we are trying to change the default tablespace of the template,
421 * we require that the template not have any files in the new default
422 * tablespace. This is necessary because otherwise the copied
423 * database would contain pg_class rows that refer to its default
424 * tablespace both explicitly (by OID) and implicitly (as zero), which
425 * would cause problems. For example another CREATE DATABASE using
426 * the copied database as template, and trying to change its default
427 * tablespace again, would yield outright incorrect results (it would
428 * improperly move tables to the new default tablespace that should
429 * stay in the same tablespace).
431 if (dst_deftablespace != src_deftablespace)
436 srcpath = GetDatabasePath(src_dboid, dst_deftablespace);
438 if (stat(srcpath, &st) == 0 &&
439 S_ISDIR(st.st_mode) &&
440 !directory_is_empty(srcpath))
442 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
443 errmsg("cannot assign new default tablespace \"%s\"",
445 errdetail("There is a conflict because database \"%s\" already has some tables in this tablespace.",
452 /* Use template database's default tablespace */
453 dst_deftablespace = src_deftablespace;
454 /* Note there is no additional permission check in this path */
458 * Check for db name conflict. This is just to give a more friendly error
459 * message than "unique index violation". There's a race condition but
460 * we're willing to accept the less friendly message in that case.
462 if (OidIsValid(get_database_oid(dbname, true)))
464 (errcode(ERRCODE_DUPLICATE_DATABASE),
465 errmsg("database \"%s\" already exists", dbname)));
468 * The source DB can't have any active backends, except this one
469 * (exception is to allow CREATE DB while connected to template1).
470 * Otherwise we might copy inconsistent data.
472 * This should be last among the basic error checks, because it involves
473 * potential waiting; we may as well throw an error first if we're gonna
476 if (CountOtherDBBackends(src_dboid, ¬herbackends, &npreparedxacts))
478 (errcode(ERRCODE_OBJECT_IN_USE),
479 errmsg("source database \"%s\" is being accessed by other users",
481 errdetail_busy_db(notherbackends, npreparedxacts)));
484 * Select an OID for the new database, checking that it doesn't have a
485 * filename conflict with anything already existing in the tablespace
488 pg_database_rel = heap_open(DatabaseRelationId, RowExclusiveLock);
492 dboid = GetNewOid(pg_database_rel);
493 } while (check_db_file_conflict(dboid));
496 * Insert a new tuple into pg_database. This establishes our ownership of
497 * the new database name (anyone else trying to insert the same name will
498 * block on the unique index, and fail after we commit).
502 MemSet(new_record, 0, sizeof(new_record));
503 MemSet(new_record_nulls, false, sizeof(new_record_nulls));
505 new_record[Anum_pg_database_datname - 1] =
506 DirectFunctionCall1(namein, CStringGetDatum(dbname));
507 new_record[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(datdba);
508 new_record[Anum_pg_database_encoding - 1] = Int32GetDatum(encoding);
509 new_record[Anum_pg_database_datcollate - 1] =
510 DirectFunctionCall1(namein, CStringGetDatum(dbcollate));
511 new_record[Anum_pg_database_datctype - 1] =
512 DirectFunctionCall1(namein, CStringGetDatum(dbctype));
513 new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(dbistemplate);
514 new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(dballowconnections);
515 new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit);
516 new_record[Anum_pg_database_datlastsysoid - 1] = ObjectIdGetDatum(src_lastsysoid);
517 new_record[Anum_pg_database_datfrozenxid - 1] = TransactionIdGetDatum(src_frozenxid);
518 new_record[Anum_pg_database_datminmxid - 1] = TransactionIdGetDatum(src_minmxid);
519 new_record[Anum_pg_database_dattablespace - 1] = ObjectIdGetDatum(dst_deftablespace);
522 * We deliberately set datacl to default (NULL), rather than copying it
523 * from the template database. Copying it would be a bad idea when the
524 * owner is not the same as the template's owner.
526 new_record_nulls[Anum_pg_database_datacl - 1] = true;
528 tuple = heap_form_tuple(RelationGetDescr(pg_database_rel),
529 new_record, new_record_nulls);
531 HeapTupleSetOid(tuple, dboid);
533 simple_heap_insert(pg_database_rel, tuple);
536 CatalogUpdateIndexes(pg_database_rel, tuple);
539 * Now generate additional catalog entries associated with the new DB
542 /* Register owner dependency */
543 recordDependencyOnOwner(DatabaseRelationId, dboid, datdba);
545 /* Create pg_shdepend entries for objects within database */
546 copyTemplateDependencies(src_dboid, dboid);
548 /* Post creation hook for new database */
549 InvokeObjectPostCreateHook(DatabaseRelationId, dboid, 0);
552 * Force a checkpoint before starting the copy. This will force all dirty
553 * buffers, including those of unlogged tables, out to disk, to ensure
554 * source database is up-to-date on disk for the copy.
555 * FlushDatabaseBuffers() would suffice for that, but we also want
556 * to process any pending unlink requests. Otherwise, if a checkpoint
557 * happened while we're copying files, a file might be deleted just when
558 * we're about to copy it, causing the lstat() call in copydir() to fail
561 RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT
562 | CHECKPOINT_FLUSH_ALL);
565 * Once we start copying subdirectories, we need to be able to clean 'em
566 * up if we fail. Use an ENSURE block to make sure this happens. (This
567 * is not a 100% solution, because of the possibility of failure during
568 * transaction commit after we leave this routine, but it should handle
571 fparms.src_dboid = src_dboid;
572 fparms.dest_dboid = dboid;
573 PG_ENSURE_ERROR_CLEANUP(createdb_failure_callback,
574 PointerGetDatum(&fparms));
577 * Iterate through all tablespaces of the template database, and copy
578 * each one to the new database.
580 rel = heap_open(TableSpaceRelationId, AccessShareLock);
581 scan = heap_beginscan_catalog(rel, 0, NULL);
582 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
584 Oid srctablespace = HeapTupleGetOid(tuple);
590 /* No need to copy global tablespace */
591 if (srctablespace == GLOBALTABLESPACE_OID)
594 srcpath = GetDatabasePath(src_dboid, srctablespace);
596 if (stat(srcpath, &st) < 0 || !S_ISDIR(st.st_mode) ||
597 directory_is_empty(srcpath))
599 /* Assume we can ignore it */
604 if (srctablespace == src_deftablespace)
605 dsttablespace = dst_deftablespace;
607 dsttablespace = srctablespace;
609 dstpath = GetDatabasePath(dboid, dsttablespace);
612 * Copy this subdirectory to the new location
614 * We don't need to copy subdirectories
616 copydir(srcpath, dstpath, false);
618 /* Record the filesystem change in XLOG */
620 xl_dbase_create_rec xlrec;
621 XLogRecData rdata[1];
624 xlrec.tablespace_id = dsttablespace;
625 xlrec.src_db_id = src_dboid;
626 xlrec.src_tablespace_id = srctablespace;
628 rdata[0].data = (char *) &xlrec;
629 rdata[0].len = sizeof(xl_dbase_create_rec);
630 rdata[0].buffer = InvalidBuffer;
631 rdata[0].next = NULL;
633 (void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_CREATE, rdata);
637 heap_close(rel, AccessShareLock);
640 * We force a checkpoint before committing. This effectively means
641 * that committed XLOG_DBASE_CREATE operations will never need to be
642 * replayed (at least not in ordinary crash recovery; we still have to
643 * make the XLOG entry for the benefit of PITR operations). This
644 * avoids two nasty scenarios:
646 * #1: When PITR is off, we don't XLOG the contents of newly created
647 * indexes; therefore the drop-and-recreate-whole-directory behavior
648 * of DBASE_CREATE replay would lose such indexes.
650 * #2: Since we have to recopy the source database during DBASE_CREATE
651 * replay, we run the risk of copying changes in it that were
652 * committed after the original CREATE DATABASE command but before the
653 * system crash that led to the replay. This is at least unexpected
654 * and at worst could lead to inconsistencies, eg duplicate table
657 * (Both of these were real bugs in releases 8.0 through 8.0.3.)
659 * In PITR replay, the first of these isn't an issue, and the second
660 * is only a risk if the CREATE DATABASE and subsequent template
661 * database change both occur while a base backup is being taken.
662 * There doesn't seem to be much we can do about that except document
663 * it as a limitation.
665 * Perhaps if we ever implement CREATE DATABASE in a less cheesy way,
668 RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT);
671 * Close pg_database, but keep lock till commit.
673 heap_close(pg_database_rel, NoLock);
676 * Force synchronous commit, thus minimizing the window between
677 * creation of the database files and commital of the transaction. If
678 * we crash before committing, we'll have a DB that's taking up disk
679 * space but is not in pg_database, which is not good.
683 PG_END_ENSURE_ERROR_CLEANUP(createdb_failure_callback,
684 PointerGetDatum(&fparms));
690 * Check whether chosen encoding matches chosen locale settings. This
691 * restriction is necessary because libc's locale-specific code usually
692 * fails when presented with data in an encoding it's not expecting. We
693 * allow mismatch in four cases:
695 * 1. locale encoding = SQL_ASCII, which means that the locale is C/POSIX
696 * which works with any encoding.
698 * 2. locale encoding = -1, which means that we couldn't determine the
699 * locale's encoding and have to trust the user to get it right.
701 * 3. selected encoding is UTF8 and platform is win32. This is because
702 * UTF8 is a pseudo codepage that is supported in all locales since it's
703 * converted to UTF16 before being used.
705 * 4. selected encoding is SQL_ASCII, but only if you're a superuser. This
706 * is risky but we have historically allowed it --- notably, the
707 * regression tests require it.
709 * Note: if you change this policy, fix initdb to match.
712 check_encoding_locale_matches(int encoding, const char *collate, const char *ctype)
714 int ctype_encoding = pg_get_encoding_from_locale(ctype, true);
715 int collate_encoding = pg_get_encoding_from_locale(collate, true);
717 if (!(ctype_encoding == encoding ||
718 ctype_encoding == PG_SQL_ASCII ||
719 ctype_encoding == -1 ||
721 encoding == PG_UTF8 ||
723 (encoding == PG_SQL_ASCII && superuser())))
725 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
726 errmsg("encoding \"%s\" does not match locale \"%s\"",
727 pg_encoding_to_char(encoding),
729 errdetail("The chosen LC_CTYPE setting requires encoding \"%s\".",
730 pg_encoding_to_char(ctype_encoding))));
732 if (!(collate_encoding == encoding ||
733 collate_encoding == PG_SQL_ASCII ||
734 collate_encoding == -1 ||
736 encoding == PG_UTF8 ||
738 (encoding == PG_SQL_ASCII && superuser())))
740 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
741 errmsg("encoding \"%s\" does not match locale \"%s\"",
742 pg_encoding_to_char(encoding),
744 errdetail("The chosen LC_COLLATE setting requires encoding \"%s\".",
745 pg_encoding_to_char(collate_encoding))));
748 /* Error cleanup callback for createdb */
750 createdb_failure_callback(int code, Datum arg)
752 createdb_failure_params *fparms = (createdb_failure_params *) DatumGetPointer(arg);
755 * Release lock on source database before doing recursive remove. This is
756 * not essential but it seems desirable to release the lock as soon as
759 UnlockSharedObject(DatabaseRelationId, fparms->src_dboid, 0, ShareLock);
761 /* Throw away any successfully copied subdirectories */
762 remove_dbtablespaces(fparms->dest_dboid);
770 dropdb(const char *dbname, bool missing_ok)
782 * Look up the target database's OID, and get exclusive lock on it. We
783 * need this to ensure that no new backend starts up in the target
784 * database while we are deleting it (see postinit.c), and that no one is
785 * using it as a CREATE DATABASE template or trying to delete it for
788 pgdbrel = heap_open(DatabaseRelationId, RowExclusiveLock);
790 if (!get_db_info(dbname, AccessExclusiveLock, &db_id, NULL, NULL,
791 &db_istemplate, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
796 (errcode(ERRCODE_UNDEFINED_DATABASE),
797 errmsg("database \"%s\" does not exist", dbname)));
801 /* Close pg_database, release the lock, since we changed nothing */
802 heap_close(pgdbrel, RowExclusiveLock);
804 (errmsg("database \"%s\" does not exist, skipping",
813 if (!pg_database_ownercheck(db_id, GetUserId()))
814 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
817 /* DROP hook for the database being removed */
818 InvokeObjectDropHook(DatabaseRelationId, db_id, 0);
821 * Disallow dropping a DB that is marked istemplate. This is just to
822 * prevent people from accidentally dropping template0 or template1; they
823 * can do so if they're really determined ...
827 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
828 errmsg("cannot drop a template database")));
830 /* Obviously can't drop my own database */
831 if (db_id == MyDatabaseId)
833 (errcode(ERRCODE_OBJECT_IN_USE),
834 errmsg("cannot drop the currently open database")));
837 * Check whether there are, possibly unconnected, logical slots that refer
838 * to the to-be-dropped database. The database lock we are holding
839 * prevents the creation of new slots using the database.
841 if (ReplicationSlotsCountDBSlots(db_id, &nslots, &nslots_active))
843 (errcode(ERRCODE_OBJECT_IN_USE),
844 errmsg("database \"%s\" is used by a logical replication slot",
846 errdetail_plural("There is %d slot, %d of them active.",
847 "There are %d slots, %d of them active.",
849 nslots, nslots_active)));
852 * Check for other backends in the target database. (Because we hold the
853 * database lock, no new ones can start after this.)
855 * As in CREATE DATABASE, check this after other error conditions.
857 if (CountOtherDBBackends(db_id, ¬herbackends, &npreparedxacts))
859 (errcode(ERRCODE_OBJECT_IN_USE),
860 errmsg("database \"%s\" is being accessed by other users",
862 errdetail_busy_db(notherbackends, npreparedxacts)));
865 * Remove the database's tuple from pg_database.
867 tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(db_id));
868 if (!HeapTupleIsValid(tup))
869 elog(ERROR, "cache lookup failed for database %u", db_id);
871 simple_heap_delete(pgdbrel, &tup->t_self);
873 ReleaseSysCache(tup);
876 * Delete any comments or security labels associated with the database.
878 DeleteSharedComments(db_id, DatabaseRelationId);
879 DeleteSharedSecurityLabel(db_id, DatabaseRelationId);
882 * Remove settings associated with this database
884 DropSetting(db_id, InvalidOid);
887 * Remove shared dependency references for the database.
889 dropDatabaseDependencies(db_id);
892 * Drop pages for this database that are in the shared buffer cache. This
893 * is important to ensure that no remaining backend tries to write out a
894 * dirty buffer to the dead database later...
896 DropDatabaseBuffers(db_id);
899 * Tell the stats collector to forget it immediately, too.
901 pgstat_drop_database(db_id);
904 * Tell checkpointer to forget any pending fsync and unlink requests for
905 * files in the database; else the fsyncs will fail at next checkpoint, or
906 * worse, it will delete files that belong to a newly created database
909 ForgetDatabaseFsyncRequests(db_id);
912 * Force a checkpoint to make sure the checkpointer has received the
913 * message sent by ForgetDatabaseFsyncRequests. On Windows, this also
914 * ensures that background procs don't hold any open files, which would
915 * cause rmdir() to fail.
917 RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT);
920 * Remove all tablespace subdirs belonging to the database.
922 remove_dbtablespaces(db_id);
925 * Close pg_database, but keep lock till commit.
927 heap_close(pgdbrel, NoLock);
930 * Force synchronous commit, thus minimizing the window between removal of
931 * the database files and commital of the transaction. If we crash before
932 * committing, we'll have a DB that's gone on disk but still there
933 * according to pg_database, which is not good.
943 RenameDatabase(const char *oldname, const char *newname)
952 * Look up the target database's OID, and get exclusive lock on it. We
953 * need this for the same reasons as DROP DATABASE.
955 rel = heap_open(DatabaseRelationId, RowExclusiveLock);
957 if (!get_db_info(oldname, AccessExclusiveLock, &db_id, NULL, NULL,
958 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
960 (errcode(ERRCODE_UNDEFINED_DATABASE),
961 errmsg("database \"%s\" does not exist", oldname)));
964 if (!pg_database_ownercheck(db_id, GetUserId()))
965 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
968 /* must have createdb rights */
969 if (!have_createdb_privilege())
971 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
972 errmsg("permission denied to rename database")));
975 * Make sure the new name doesn't exist. See notes for same error in
978 if (OidIsValid(get_database_oid(newname, true)))
980 (errcode(ERRCODE_DUPLICATE_DATABASE),
981 errmsg("database \"%s\" already exists", newname)));
984 * XXX Client applications probably store the current database somewhere,
985 * so renaming it could cause confusion. On the other hand, there may not
986 * be an actual problem besides a little confusion, so think about this
989 if (db_id == MyDatabaseId)
991 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
992 errmsg("current database cannot be renamed")));
995 * Make sure the database does not have active sessions. This is the same
996 * concern as above, but applied to other sessions.
998 * As in CREATE DATABASE, check this after other error conditions.
1000 if (CountOtherDBBackends(db_id, ¬herbackends, &npreparedxacts))
1002 (errcode(ERRCODE_OBJECT_IN_USE),
1003 errmsg("database \"%s\" is being accessed by other users",
1005 errdetail_busy_db(notherbackends, npreparedxacts)));
1008 newtup = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(db_id));
1009 if (!HeapTupleIsValid(newtup))
1010 elog(ERROR, "cache lookup failed for database %u", db_id);
1011 namestrcpy(&(((Form_pg_database) GETSTRUCT(newtup))->datname), newname);
1012 simple_heap_update(rel, &newtup->t_self, newtup);
1013 CatalogUpdateIndexes(rel, newtup);
1015 InvokeObjectPostAlterHook(DatabaseRelationId, db_id, 0);
1018 * Close pg_database, but keep lock till commit.
1020 heap_close(rel, NoLock);
1027 * ALTER DATABASE SET TABLESPACE
1030 movedb(const char *dbname, const char *tblspcname)
1040 Datum new_record[Natts_pg_database];
1041 bool new_record_nulls[Natts_pg_database];
1042 bool new_record_repl[Natts_pg_database];
1043 ScanKeyData scankey;
1044 SysScanDesc sysscan;
1045 AclResult aclresult;
1049 struct dirent *xlde;
1050 movedb_failure_params fparms;
1053 * Look up the target database's OID, and get exclusive lock on it. We
1054 * need this to ensure that no new backend starts up in the database while
1055 * we are moving it, and that no one is using it as a CREATE DATABASE
1056 * template or trying to delete it.
1058 pgdbrel = heap_open(DatabaseRelationId, RowExclusiveLock);
1060 if (!get_db_info(dbname, AccessExclusiveLock, &db_id, NULL, NULL,
1061 NULL, NULL, NULL, NULL, NULL, &src_tblspcoid, NULL, NULL))
1063 (errcode(ERRCODE_UNDEFINED_DATABASE),
1064 errmsg("database \"%s\" does not exist", dbname)));
1067 * We actually need a session lock, so that the lock will persist across
1068 * the commit/restart below. (We could almost get away with letting the
1069 * lock be released at commit, except that someone could try to move
1070 * relations of the DB back into the old directory while we rmtree() it.)
1072 LockSharedObjectForSession(DatabaseRelationId, db_id, 0,
1073 AccessExclusiveLock);
1078 if (!pg_database_ownercheck(db_id, GetUserId()))
1079 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
1083 * Obviously can't move the tables of my own database
1085 if (db_id == MyDatabaseId)
1087 (errcode(ERRCODE_OBJECT_IN_USE),
1088 errmsg("cannot change the tablespace of the currently open database")));
1091 * Get tablespace's oid
1093 dst_tblspcoid = get_tablespace_oid(tblspcname, false);
1098 aclresult = pg_tablespace_aclcheck(dst_tblspcoid, GetUserId(),
1100 if (aclresult != ACLCHECK_OK)
1101 aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
1105 * pg_global must never be the default tablespace
1107 if (dst_tblspcoid == GLOBALTABLESPACE_OID)
1109 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1110 errmsg("pg_global cannot be used as default tablespace")));
1113 * No-op if same tablespace
1115 if (src_tblspcoid == dst_tblspcoid)
1117 heap_close(pgdbrel, NoLock);
1118 UnlockSharedObjectForSession(DatabaseRelationId, db_id, 0,
1119 AccessExclusiveLock);
1124 * Check for other backends in the target database. (Because we hold the
1125 * database lock, no new ones can start after this.)
1127 * As in CREATE DATABASE, check this after other error conditions.
1129 if (CountOtherDBBackends(db_id, ¬herbackends, &npreparedxacts))
1131 (errcode(ERRCODE_OBJECT_IN_USE),
1132 errmsg("database \"%s\" is being accessed by other users",
1134 errdetail_busy_db(notherbackends, npreparedxacts)));
1137 * Get old and new database paths
1139 src_dbpath = GetDatabasePath(db_id, src_tblspcoid);
1140 dst_dbpath = GetDatabasePath(db_id, dst_tblspcoid);
1143 * Force a checkpoint before proceeding. This will force all dirty
1144 * buffers, including those of unlogged tables, out to disk, to ensure
1145 * source database is up-to-date on disk for the copy.
1146 * FlushDatabaseBuffers() would suffice for that, but we also want to
1147 * process any pending unlink requests. Otherwise, the check for existing
1148 * files in the target directory might fail unnecessarily, not to mention
1149 * that the copy might fail due to source files getting deleted under it.
1150 * On Windows, this also ensures that background procs don't hold any open
1151 * files, which would cause rmdir() to fail.
1153 RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT
1154 | CHECKPOINT_FLUSH_ALL);
1157 * Now drop all buffers holding data of the target database; they should
1158 * no longer be dirty so DropDatabaseBuffers is safe.
1160 * It might seem that we could just let these buffers age out of shared
1161 * buffers naturally, since they should not get referenced anymore. The
1162 * problem with that is that if the user later moves the database back to
1163 * its original tablespace, any still-surviving buffers would appear to
1164 * contain valid data again --- but they'd be missing any changes made in
1165 * the database while it was in the new tablespace. In any case, freeing
1166 * buffers that should never be used again seems worth the cycles.
1168 * Note: it'd be sufficient to get rid of buffers matching db_id and
1169 * src_tblspcoid, but bufmgr.c presently provides no API for that.
1171 DropDatabaseBuffers(db_id);
1174 * Check for existence of files in the target directory, i.e., objects of
1175 * this database that are already in the target tablespace. We can't
1176 * allow the move in such a case, because we would need to change those
1177 * relations' pg_class.reltablespace entries to zero, and we don't have
1178 * access to the DB's pg_class to do so.
1180 dstdir = AllocateDir(dst_dbpath);
1183 while ((xlde = ReadDir(dstdir, dst_dbpath)) != NULL)
1185 if (strcmp(xlde->d_name, ".") == 0 ||
1186 strcmp(xlde->d_name, "..") == 0)
1190 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1191 errmsg("some relations of database \"%s\" are already in tablespace \"%s\"",
1192 dbname, tblspcname),
1193 errhint("You must move them back to the database's default tablespace before using this command.")));
1199 * The directory exists but is empty. We must remove it before using
1200 * the copydir function.
1202 if (rmdir(dst_dbpath) != 0)
1203 elog(ERROR, "could not remove directory \"%s\": %m",
1208 * Use an ENSURE block to make sure we remove the debris if the copy fails
1209 * (eg, due to out-of-disk-space). This is not a 100% solution, because
1210 * of the possibility of failure during transaction commit, but it should
1211 * handle most scenarios.
1213 fparms.dest_dboid = db_id;
1214 fparms.dest_tsoid = dst_tblspcoid;
1215 PG_ENSURE_ERROR_CLEANUP(movedb_failure_callback,
1216 PointerGetDatum(&fparms));
1219 * Copy files from the old tablespace to the new one
1221 copydir(src_dbpath, dst_dbpath, false);
1224 * Record the filesystem change in XLOG
1227 xl_dbase_create_rec xlrec;
1228 XLogRecData rdata[1];
1230 xlrec.db_id = db_id;
1231 xlrec.tablespace_id = dst_tblspcoid;
1232 xlrec.src_db_id = db_id;
1233 xlrec.src_tablespace_id = src_tblspcoid;
1235 rdata[0].data = (char *) &xlrec;
1236 rdata[0].len = sizeof(xl_dbase_create_rec);
1237 rdata[0].buffer = InvalidBuffer;
1238 rdata[0].next = NULL;
1240 (void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_CREATE, rdata);
1244 * Update the database's pg_database tuple
1246 ScanKeyInit(&scankey,
1247 Anum_pg_database_datname,
1248 BTEqualStrategyNumber, F_NAMEEQ,
1249 NameGetDatum(dbname));
1250 sysscan = systable_beginscan(pgdbrel, DatabaseNameIndexId, true,
1252 oldtuple = systable_getnext(sysscan);
1253 if (!HeapTupleIsValid(oldtuple)) /* shouldn't happen... */
1255 (errcode(ERRCODE_UNDEFINED_DATABASE),
1256 errmsg("database \"%s\" does not exist", dbname)));
1258 MemSet(new_record, 0, sizeof(new_record));
1259 MemSet(new_record_nulls, false, sizeof(new_record_nulls));
1260 MemSet(new_record_repl, false, sizeof(new_record_repl));
1262 new_record[Anum_pg_database_dattablespace - 1] = ObjectIdGetDatum(dst_tblspcoid);
1263 new_record_repl[Anum_pg_database_dattablespace - 1] = true;
1265 newtuple = heap_modify_tuple(oldtuple, RelationGetDescr(pgdbrel),
1267 new_record_nulls, new_record_repl);
1268 simple_heap_update(pgdbrel, &oldtuple->t_self, newtuple);
1270 /* Update indexes */
1271 CatalogUpdateIndexes(pgdbrel, newtuple);
1273 InvokeObjectPostAlterHook(DatabaseRelationId,
1274 HeapTupleGetOid(newtuple), 0);
1276 systable_endscan(sysscan);
1279 * Force another checkpoint here. As in CREATE DATABASE, this is to
1280 * ensure that we don't have to replay a committed XLOG_DBASE_CREATE
1281 * operation, which would cause us to lose any unlogged operations
1282 * done in the new DB tablespace before the next checkpoint.
1284 RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT);
1287 * Force synchronous commit, thus minimizing the window between
1288 * copying the database files and commital of the transaction. If we
1289 * crash before committing, we'll leave an orphaned set of files on
1290 * disk, which is not fatal but not good either.
1295 * Close pg_database, but keep lock till commit.
1297 heap_close(pgdbrel, NoLock);
1299 PG_END_ENSURE_ERROR_CLEANUP(movedb_failure_callback,
1300 PointerGetDatum(&fparms));
1303 * Commit the transaction so that the pg_database update is committed. If
1304 * we crash while removing files, the database won't be corrupt, we'll
1305 * just leave some orphaned files in the old directory.
1307 * (This is OK because we know we aren't inside a transaction block.)
1309 * XXX would it be safe/better to do this inside the ensure block? Not
1310 * convinced it's a good idea; consider elog just after the transaction
1313 PopActiveSnapshot();
1314 CommitTransactionCommand();
1316 /* Start new transaction for the remaining work; don't need a snapshot */
1317 StartTransactionCommand();
1320 * Remove files from the old tablespace
1322 if (!rmtree(src_dbpath, true))
1324 (errmsg("some useless files may be left behind in old database directory \"%s\"",
1328 * Record the filesystem change in XLOG
1331 xl_dbase_drop_rec xlrec;
1332 XLogRecData rdata[1];
1334 xlrec.db_id = db_id;
1335 xlrec.tablespace_id = src_tblspcoid;
1337 rdata[0].data = (char *) &xlrec;
1338 rdata[0].len = sizeof(xl_dbase_drop_rec);
1339 rdata[0].buffer = InvalidBuffer;
1340 rdata[0].next = NULL;
1342 (void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_DROP, rdata);
1345 /* Now it's safe to release the database lock */
1346 UnlockSharedObjectForSession(DatabaseRelationId, db_id, 0,
1347 AccessExclusiveLock);
1350 /* Error cleanup callback for movedb */
1352 movedb_failure_callback(int code, Datum arg)
1354 movedb_failure_params *fparms = (movedb_failure_params *) DatumGetPointer(arg);
1357 /* Get rid of anything we managed to copy to the target directory */
1358 dstpath = GetDatabasePath(fparms->dest_dboid, fparms->dest_tsoid);
1360 (void) rmtree(dstpath, true);
1365 * ALTER DATABASE name ...
1368 AlterDatabase(AlterDatabaseStmt *stmt, bool isTopLevel)
1374 ScanKeyData scankey;
1377 bool dbistemplate = false;
1378 bool dballowconnections = true;
1379 int dbconnlimit = -1;
1380 DefElem *distemplate = NULL;
1381 DefElem *dallowconnections = NULL;
1382 DefElem *dconnlimit = NULL;
1383 DefElem *dtablespace = NULL;
1384 Datum new_record[Natts_pg_database];
1385 bool new_record_nulls[Natts_pg_database];
1386 bool new_record_repl[Natts_pg_database];
1388 /* Extract options from the statement node tree */
1389 foreach(option, stmt->options)
1391 DefElem *defel = (DefElem *) lfirst(option);
1393 if (strcmp(defel->defname, "is_template") == 0)
1397 (errcode(ERRCODE_SYNTAX_ERROR),
1398 errmsg("conflicting or redundant options")));
1399 distemplate = defel;
1401 else if (strcmp(defel->defname, "allow_connections") == 0)
1403 if (dallowconnections)
1405 (errcode(ERRCODE_SYNTAX_ERROR),
1406 errmsg("conflicting or redundant options")));
1407 dallowconnections = defel;
1409 else if (strcmp(defel->defname, "connection_limit") == 0)
1413 (errcode(ERRCODE_SYNTAX_ERROR),
1414 errmsg("conflicting or redundant options")));
1417 else if (strcmp(defel->defname, "tablespace") == 0)
1421 (errcode(ERRCODE_SYNTAX_ERROR),
1422 errmsg("conflicting or redundant options")));
1423 dtablespace = defel;
1427 (errcode(ERRCODE_SYNTAX_ERROR),
1428 errmsg("option \"%s\" not recognized", defel->defname)));
1434 * While the SET TABLESPACE syntax doesn't allow any other options,
1435 * somebody could write "WITH TABLESPACE ...". Forbid any other
1436 * options from being specified in that case.
1438 if (list_length(stmt->options) != 1)
1440 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1441 errmsg("option \"%s\" cannot be specified with other options",
1442 dtablespace->defname)));
1443 /* this case isn't allowed within a transaction block */
1444 PreventTransactionChain(isTopLevel, "ALTER DATABASE SET TABLESPACE");
1445 movedb(stmt->dbname, defGetString(dtablespace));
1449 if (distemplate && distemplate->arg)
1450 dbistemplate = defGetBoolean(distemplate);
1451 if (dallowconnections && dallowconnections->arg)
1452 dballowconnections = defGetBoolean(dallowconnections);
1453 if (dconnlimit && dconnlimit->arg)
1455 dbconnlimit = defGetInt32(dconnlimit);
1456 if (dbconnlimit < -1)
1458 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1459 errmsg("invalid connection limit: %d", dbconnlimit)));
1463 * Get the old tuple. We don't need a lock on the database per se,
1464 * because we're not going to do anything that would mess up incoming
1467 rel = heap_open(DatabaseRelationId, RowExclusiveLock);
1468 ScanKeyInit(&scankey,
1469 Anum_pg_database_datname,
1470 BTEqualStrategyNumber, F_NAMEEQ,
1471 NameGetDatum(stmt->dbname));
1472 scan = systable_beginscan(rel, DatabaseNameIndexId, true,
1474 tuple = systable_getnext(scan);
1475 if (!HeapTupleIsValid(tuple))
1477 (errcode(ERRCODE_UNDEFINED_DATABASE),
1478 errmsg("database \"%s\" does not exist", stmt->dbname)));
1480 dboid = HeapTupleGetOid(tuple);
1482 if (!pg_database_ownercheck(HeapTupleGetOid(tuple), GetUserId()))
1483 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
1487 * In order to avoid getting locked out and having to go through
1488 * standalone mode, we refuse to disallow connections to the database
1489 * we're currently connected to. Lockout can still happen with concurrent
1490 * sessions but the likeliness of that is not high enough to worry about.
1492 if (!dballowconnections && dboid == MyDatabaseId)
1494 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1495 errmsg("cannot disallow connections for current database")));
1498 * Build an updated tuple, perusing the information just obtained
1500 MemSet(new_record, 0, sizeof(new_record));
1501 MemSet(new_record_nulls, false, sizeof(new_record_nulls));
1502 MemSet(new_record_repl, false, sizeof(new_record_repl));
1506 new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(dbistemplate);
1507 new_record_repl[Anum_pg_database_datistemplate - 1] = true;
1509 if (dallowconnections)
1511 new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(dballowconnections);
1512 new_record_repl[Anum_pg_database_datallowconn - 1] = true;
1516 new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit);
1517 new_record_repl[Anum_pg_database_datconnlimit - 1] = true;
1520 newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel), new_record,
1521 new_record_nulls, new_record_repl);
1522 simple_heap_update(rel, &tuple->t_self, newtuple);
1524 /* Update indexes */
1525 CatalogUpdateIndexes(rel, newtuple);
1527 InvokeObjectPostAlterHook(DatabaseRelationId,
1528 HeapTupleGetOid(newtuple), 0);
1530 systable_endscan(scan);
1532 /* Close pg_database, but keep lock till commit */
1533 heap_close(rel, NoLock);
1540 * ALTER DATABASE name SET ...
1543 AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
1545 Oid datid = get_database_oid(stmt->dbname, false);
1548 * Obtain a lock on the database and make sure it didn't go away in the
1551 shdepLockAndCheckObject(DatabaseRelationId, datid);
1553 if (!pg_database_ownercheck(datid, GetUserId()))
1554 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
1557 AlterSetting(datid, InvalidOid, stmt->setstmt);
1559 UnlockSharedObject(DatabaseRelationId, datid, 0, AccessShareLock);
1566 * ALTER DATABASE name OWNER TO newowner
1569 AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
1574 ScanKeyData scankey;
1576 Form_pg_database datForm;
1579 * Get the old tuple. We don't need a lock on the database per se,
1580 * because we're not going to do anything that would mess up incoming
1583 rel = heap_open(DatabaseRelationId, RowExclusiveLock);
1584 ScanKeyInit(&scankey,
1585 Anum_pg_database_datname,
1586 BTEqualStrategyNumber, F_NAMEEQ,
1587 NameGetDatum(dbname));
1588 scan = systable_beginscan(rel, DatabaseNameIndexId, true,
1590 tuple = systable_getnext(scan);
1591 if (!HeapTupleIsValid(tuple))
1593 (errcode(ERRCODE_UNDEFINED_DATABASE),
1594 errmsg("database \"%s\" does not exist", dbname)));
1596 db_id = HeapTupleGetOid(tuple);
1597 datForm = (Form_pg_database) GETSTRUCT(tuple);
1600 * If the new owner is the same as the existing owner, consider the
1601 * command to have succeeded. This is to be consistent with other
1604 if (datForm->datdba != newOwnerId)
1606 Datum repl_val[Natts_pg_database];
1607 bool repl_null[Natts_pg_database];
1608 bool repl_repl[Natts_pg_database];
1614 /* Otherwise, must be owner of the existing object */
1615 if (!pg_database_ownercheck(HeapTupleGetOid(tuple), GetUserId()))
1616 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
1619 /* Must be able to become new owner */
1620 check_is_member_of_role(GetUserId(), newOwnerId);
1623 * must have createdb rights
1625 * NOTE: This is different from other alter-owner checks in that the
1626 * current user is checked for createdb privileges instead of the
1627 * destination owner. This is consistent with the CREATE case for
1628 * databases. Because superusers will always have this right, we need
1629 * no special case for them.
1631 if (!have_createdb_privilege())
1633 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1634 errmsg("permission denied to change owner of database")));
1636 memset(repl_null, false, sizeof(repl_null));
1637 memset(repl_repl, false, sizeof(repl_repl));
1639 repl_repl[Anum_pg_database_datdba - 1] = true;
1640 repl_val[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(newOwnerId);
1643 * Determine the modified ACL for the new owner. This is only
1644 * necessary when the ACL is non-null.
1646 aclDatum = heap_getattr(tuple,
1647 Anum_pg_database_datacl,
1648 RelationGetDescr(rel),
1652 newAcl = aclnewowner(DatumGetAclP(aclDatum),
1653 datForm->datdba, newOwnerId);
1654 repl_repl[Anum_pg_database_datacl - 1] = true;
1655 repl_val[Anum_pg_database_datacl - 1] = PointerGetDatum(newAcl);
1658 newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel), repl_val, repl_null, repl_repl);
1659 simple_heap_update(rel, &newtuple->t_self, newtuple);
1660 CatalogUpdateIndexes(rel, newtuple);
1662 heap_freetuple(newtuple);
1664 /* Update owner dependency reference */
1665 changeDependencyOnOwner(DatabaseRelationId, HeapTupleGetOid(tuple),
1669 InvokeObjectPostAlterHook(DatabaseRelationId, HeapTupleGetOid(tuple), 0);
1671 systable_endscan(scan);
1673 /* Close pg_database, but keep lock till commit */
1674 heap_close(rel, NoLock);
1685 * Look up info about the database named "name". If the database exists,
1686 * obtain the specified lock type on it, fill in any of the remaining
1687 * parameters that aren't NULL, and return TRUE. If no such database,
1691 get_db_info(const char *name, LOCKMODE lockmode,
1692 Oid *dbIdP, Oid *ownerIdP,
1693 int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP,
1694 Oid *dbLastSysOidP, TransactionId *dbFrozenXidP,
1695 MultiXactId *dbMinMultiP,
1696 Oid *dbTablespace, char **dbCollate, char **dbCtype)
1698 bool result = false;
1703 /* Caller may wish to grab a better lock on pg_database beforehand... */
1704 relation = heap_open(DatabaseRelationId, AccessShareLock);
1707 * Loop covers the rare case where the database is renamed before we can
1708 * lock it. We try again just in case we can find a new one of the same
1713 ScanKeyData scanKey;
1719 * there's no syscache for database-indexed-by-name, so must do it the
1722 ScanKeyInit(&scanKey,
1723 Anum_pg_database_datname,
1724 BTEqualStrategyNumber, F_NAMEEQ,
1725 NameGetDatum(name));
1727 scan = systable_beginscan(relation, DatabaseNameIndexId, true,
1730 tuple = systable_getnext(scan);
1732 if (!HeapTupleIsValid(tuple))
1734 /* definitely no database of that name */
1735 systable_endscan(scan);
1739 dbOid = HeapTupleGetOid(tuple);
1741 systable_endscan(scan);
1744 * Now that we have a database OID, we can try to lock the DB.
1746 if (lockmode != NoLock)
1747 LockSharedObject(DatabaseRelationId, dbOid, 0, lockmode);
1750 * And now, re-fetch the tuple by OID. If it's still there and still
1751 * the same name, we win; else, drop the lock and loop back to try
1754 tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(dbOid));
1755 if (HeapTupleIsValid(tuple))
1757 Form_pg_database dbform = (Form_pg_database) GETSTRUCT(tuple);
1759 if (strcmp(name, NameStr(dbform->datname)) == 0)
1761 /* oid of the database */
1764 /* oid of the owner */
1766 *ownerIdP = dbform->datdba;
1767 /* character encoding */
1769 *encodingP = dbform->encoding;
1770 /* allowed as template? */
1772 *dbIsTemplateP = dbform->datistemplate;
1773 /* allowing connections? */
1775 *dbAllowConnP = dbform->datallowconn;
1776 /* last system OID used in database */
1778 *dbLastSysOidP = dbform->datlastsysoid;
1779 /* limit of frozen XIDs */
1781 *dbFrozenXidP = dbform->datfrozenxid;
1782 /* minimum MultixactId */
1784 *dbMinMultiP = dbform->datminmxid;
1785 /* default tablespace for this database */
1787 *dbTablespace = dbform->dattablespace;
1788 /* default locale settings for this database */
1790 *dbCollate = pstrdup(NameStr(dbform->datcollate));
1792 *dbCtype = pstrdup(NameStr(dbform->datctype));
1793 ReleaseSysCache(tuple);
1797 /* can only get here if it was just renamed */
1798 ReleaseSysCache(tuple);
1801 if (lockmode != NoLock)
1802 UnlockSharedObject(DatabaseRelationId, dbOid, 0, lockmode);
1805 heap_close(relation, AccessShareLock);
1810 /* Check if current user has createdb privileges */
1812 have_createdb_privilege(void)
1814 bool result = false;
1817 /* Superusers can always do everything */
1821 utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(GetUserId()));
1822 if (HeapTupleIsValid(utup))
1824 result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreatedb;
1825 ReleaseSysCache(utup);
1831 * Remove tablespace directories
1833 * We don't know what tablespaces db_id is using, so iterate through all
1834 * tablespaces removing <tablespace>/db_id
1837 remove_dbtablespaces(Oid db_id)
1843 rel = heap_open(TableSpaceRelationId, AccessShareLock);
1844 scan = heap_beginscan_catalog(rel, 0, NULL);
1845 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1847 Oid dsttablespace = HeapTupleGetOid(tuple);
1851 /* Don't mess with the global tablespace */
1852 if (dsttablespace == GLOBALTABLESPACE_OID)
1855 dstpath = GetDatabasePath(db_id, dsttablespace);
1857 if (lstat(dstpath, &st) < 0 || !S_ISDIR(st.st_mode))
1859 /* Assume we can ignore it */
1864 if (!rmtree(dstpath, true))
1866 (errmsg("some useless files may be left behind in old database directory \"%s\"",
1869 /* Record the filesystem change in XLOG */
1871 xl_dbase_drop_rec xlrec;
1872 XLogRecData rdata[1];
1874 xlrec.db_id = db_id;
1875 xlrec.tablespace_id = dsttablespace;
1877 rdata[0].data = (char *) &xlrec;
1878 rdata[0].len = sizeof(xl_dbase_drop_rec);
1879 rdata[0].buffer = InvalidBuffer;
1880 rdata[0].next = NULL;
1882 (void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_DROP, rdata);
1889 heap_close(rel, AccessShareLock);
1893 * Check for existing files that conflict with a proposed new DB OID;
1894 * return TRUE if there are any
1896 * If there were a subdirectory in any tablespace matching the proposed new
1897 * OID, we'd get a create failure due to the duplicate name ... and then we'd
1898 * try to remove that already-existing subdirectory during the cleanup in
1899 * remove_dbtablespaces. Nuking existing files seems like a bad idea, so
1900 * instead we make this extra check before settling on the OID of the new
1901 * database. This exactly parallels what GetNewRelFileNode() does for table
1902 * relfilenode values.
1905 check_db_file_conflict(Oid db_id)
1907 bool result = false;
1912 rel = heap_open(TableSpaceRelationId, AccessShareLock);
1913 scan = heap_beginscan_catalog(rel, 0, NULL);
1914 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1916 Oid dsttablespace = HeapTupleGetOid(tuple);
1920 /* Don't mess with the global tablespace */
1921 if (dsttablespace == GLOBALTABLESPACE_OID)
1924 dstpath = GetDatabasePath(db_id, dsttablespace);
1926 if (lstat(dstpath, &st) == 0)
1928 /* Found a conflicting file (or directory, whatever) */
1938 heap_close(rel, AccessShareLock);
1944 * Issue a suitable errdetail message for a busy database
1947 errdetail_busy_db(int notherbackends, int npreparedxacts)
1949 if (notherbackends > 0 && npreparedxacts > 0)
1952 * We don't deal with singular versus plural here, since gettext
1953 * doesn't support multiple plurals in one string.
1955 errdetail("There are %d other session(s) and %d prepared transaction(s) using the database.",
1956 notherbackends, npreparedxacts);
1957 else if (notherbackends > 0)
1958 errdetail_plural("There is %d other session using the database.",
1959 "There are %d other sessions using the database.",
1963 errdetail_plural("There is %d prepared transaction using the database.",
1964 "There are %d prepared transactions using the database.",
1967 return 0; /* just to keep ereport macro happy */
1971 * get_database_oid - given a database name, look up the OID
1973 * If missing_ok is false, throw an error if database name not found. If
1974 * true, just return InvalidOid.
1977 get_database_oid(const char *dbname, bool missing_ok)
1979 Relation pg_database;
1980 ScanKeyData entry[1];
1986 * There's no syscache for pg_database indexed by name, so we must look
1989 pg_database = heap_open(DatabaseRelationId, AccessShareLock);
1990 ScanKeyInit(&entry[0],
1991 Anum_pg_database_datname,
1992 BTEqualStrategyNumber, F_NAMEEQ,
1993 CStringGetDatum(dbname));
1994 scan = systable_beginscan(pg_database, DatabaseNameIndexId, true,
1997 dbtuple = systable_getnext(scan);
1999 /* We assume that there can be at most one matching tuple */
2000 if (HeapTupleIsValid(dbtuple))
2001 oid = HeapTupleGetOid(dbtuple);
2005 systable_endscan(scan);
2006 heap_close(pg_database, AccessShareLock);
2008 if (!OidIsValid(oid) && !missing_ok)
2010 (errcode(ERRCODE_UNDEFINED_DATABASE),
2011 errmsg("database \"%s\" does not exist",
2019 * get_database_name - given a database OID, look up the name
2021 * Returns a palloc'd string, or NULL if no such database.
2024 get_database_name(Oid dbid)
2029 dbtuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(dbid));
2030 if (HeapTupleIsValid(dbtuple))
2032 result = pstrdup(NameStr(((Form_pg_database) GETSTRUCT(dbtuple))->datname));
2033 ReleaseSysCache(dbtuple);
2042 * DATABASE resource manager's routines
2045 dbase_redo(XLogRecPtr lsn, XLogRecord *record)
2047 uint8 info = record->xl_info & ~XLR_INFO_MASK;
2049 /* Backup blocks are not used in dbase records */
2050 Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
2052 if (info == XLOG_DBASE_CREATE)
2054 xl_dbase_create_rec *xlrec = (xl_dbase_create_rec *) XLogRecGetData(record);
2059 src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id);
2060 dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
2063 * Our theory for replaying a CREATE is to forcibly drop the target
2064 * subdirectory if present, then re-copy the source data. This may be
2065 * more work than needed, but it is simple to implement.
2067 if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
2069 if (!rmtree(dst_path, true))
2070 /* If this failed, copydir() below is going to error. */
2072 (errmsg("some useless files may be left behind in old database directory \"%s\"",
2077 * Force dirty buffers out to disk, to ensure source database is
2078 * up-to-date for the copy.
2080 FlushDatabaseBuffers(xlrec->src_db_id);
2083 * Copy this subdirectory to the new location
2085 * We don't need to copy subdirectories
2087 copydir(src_path, dst_path, false);
2089 else if (info == XLOG_DBASE_DROP)
2091 xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) XLogRecGetData(record);
2094 dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
2099 * Lock database while we resolve conflicts to ensure that
2100 * InitPostgres() cannot fully re-execute concurrently. This
2101 * avoids backends re-connecting automatically to same database,
2102 * which can happen in some cases.
2104 LockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
2105 ResolveRecoveryConflictWithDatabase(xlrec->db_id);
2108 /* Drop pages for this database that are in the shared buffer cache */
2109 DropDatabaseBuffers(xlrec->db_id);
2111 /* Also, clean out any fsync requests that might be pending in md.c */
2112 ForgetDatabaseFsyncRequests(xlrec->db_id);
2114 /* Clean out the xlog relcache too */
2115 XLogDropDatabase(xlrec->db_id);
2117 /* And remove the physical files */
2118 if (!rmtree(dst_path, true))
2120 (errmsg("some useless files may be left behind in old database directory \"%s\"",
2126 * Release locks prior to commit. XXX There is a race condition
2127 * here that may allow backends to reconnect, but the window for
2128 * this is small because the gap between here and commit is mostly
2129 * fairly small and it is unlikely that people will be dropping
2130 * databases that we are trying to connect to anyway.
2132 UnlockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
2136 elog(PANIC, "dbase_redo: unknown op code %u", info);