64 #include "utils/fmgroids.h"
112 static void movedb(
const char *
dbname,
const char *tblspcname);
115 Oid *dbIdP,
Oid *ownerIdP,
116 int *encodingP,
bool *dbIsTemplateP,
bool *dbAllowConnP,
118 Oid *dbTablespace,
char **dbCollate,
char **dbCtype,
char **dbIculocale,
120 char **dbCollversion);
129 Oid dbid,
char *srcpath,
146 Oid src_tsid,
Oid dst_tsid)
150 List *rnodelist = NULL;
176 srcrelid.
dbId = src_dboid;
177 dstrelid.
dbId = dst_dboid;
180 foreach(cell, rnodelist)
183 srcrnode = relinfo->
rnode;
191 if (srcrnode.
spcNode == src_tsid)
196 dstrnode.
dbNode = dst_dboid;
267 relid.
relId = RelationRelationId;
298 for (blkno = 0; blkno < nblocks; blkno++)
333 char *srcpath,
List *rnodelist,
379 rnodelist =
lappend(rnodelist, relinfo);
415 if (classForm->reltablespace == GLOBALTABLESPACE_OID ||
416 !RELKIND_HAS_STORAGE(classForm->relkind) ||
417 classForm->relpersistence == RELPERSISTENCE_TEMP)
425 relfilenode = classForm->relfilenode;
432 elog(
ERROR,
"relation with OID %u does not have a valid relfilenode",
444 relinfo->
reloid = classForm->oid;
447 Assert(classForm->relpersistence != RELPERSISTENCE_TEMP);
449 (classForm->relpersistence == RELPERSISTENCE_PERMANENT) ?
true :
false;
474 nbytes = strlen(PG_MAJORVERSION) + 1;
501 if (errno != EEXIST || !isRedo)
504 errmsg(
"could not create directory \"%s\": %m", dbpath)));
512 snprintf(versionfile,
sizeof(versionfile),
"%s/%s", dbpath,
"PG_VERSION");
515 if (
fd < 0 && errno == EEXIST && isRedo)
521 errmsg(
"could not create file \"%s\": %m", versionfile)));
533 errmsg(
"could not write to file \"%s\": %m", versionfile)));
583 Oid srctablespace = spaceform->oid;
590 if (srctablespace == GLOBALTABLESPACE_OID)
603 if (srctablespace == src_tsid)
604 dsttablespace = dst_tsid;
606 dsttablespace = srctablespace;
621 xlrec.
db_id = dst_dboid;
678 int src_encoding = -1;
679 char *src_collate = NULL;
680 char *src_ctype = NULL;
681 char *src_iculocale = NULL;
682 char src_locprovider =
'\0';
683 char *src_collversion = NULL;
688 Oid src_deftablespace;
689 volatile Oid dst_deftablespace;
692 Datum new_record[Natts_pg_database];
693 bool new_record_nulls[Natts_pg_database];
697 DefElem *dtablespacename = NULL;
707 DefElem *dallowconnections = NULL;
712 char *dbowner = NULL;
713 const char *dbtemplate = NULL;
714 char *dbcollate = NULL;
715 char *dbctype = NULL;
716 char *dbiculocale = NULL;
717 char dblocprovider =
'\0';
720 bool dbistemplate =
false;
721 bool dballowconnections =
true;
722 int dbconnlimit = -1;
723 char *dbcollversion = NULL;
734 if (strcmp(defel->
defname,
"tablespace") == 0)
738 dtablespacename = defel;
740 else if (strcmp(defel->
defname,
"owner") == 0)
746 else if (strcmp(defel->
defname,
"template") == 0)
752 else if (strcmp(defel->
defname,
"encoding") == 0)
758 else if (strcmp(defel->
defname,
"locale") == 0)
764 else if (strcmp(defel->
defname,
"lc_collate") == 0)
770 else if (strcmp(defel->
defname,
"lc_ctype") == 0)
776 else if (strcmp(defel->
defname,
"icu_locale") == 0)
782 else if (strcmp(defel->
defname,
"locale_provider") == 0)
786 dlocprovider = defel;
788 else if (strcmp(defel->
defname,
"is_template") == 0)
794 else if (strcmp(defel->
defname,
"allow_connections") == 0)
796 if (dallowconnections)
798 dallowconnections = defel;
800 else if (strcmp(defel->
defname,
"connection_limit") == 0)
806 else if (strcmp(defel->
defname,
"collation_version") == 0)
810 dcollversion = defel;
812 else if (strcmp(defel->
defname,
"location") == 0)
815 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
816 errmsg(
"LOCATION is not supported anymore"),
817 errhint(
"Consider using tablespaces instead."),
820 else if (strcmp(defel->
defname,
"oid") == 0)
841 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
844 else if (strcmp(defel->
defname,
"strategy") == 0)
852 (
errcode(ERRCODE_SYNTAX_ERROR),
857 if (downer && downer->
arg)
859 if (dtemplate && dtemplate->
arg)
861 if (dencoding && dencoding->
arg)
863 const char *encoding_name;
869 if (strcmp(encoding_name,
"") == 0 ||
872 (
errcode(ERRCODE_UNDEFINED_OBJECT),
873 errmsg(
"%d is not a valid encoding code",
883 (
errcode(ERRCODE_UNDEFINED_OBJECT),
884 errmsg(
"%s is not a valid encoding name",
889 if (dlocale && dlocale->
arg)
894 if (dcollate && dcollate->
arg)
896 if (dctype && dctype->
arg)
898 if (diculocale && diculocale->
arg)
900 if (dlocprovider && dlocprovider->
arg)
905 dblocprovider = COLLPROVIDER_ICU;
907 dblocprovider = COLLPROVIDER_LIBC;
910 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
911 errmsg(
"unrecognized locale provider: %s",
914 if (diculocale && dblocprovider != COLLPROVIDER_ICU)
916 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
917 errmsg(
"ICU locale cannot be specified unless locale provider is ICU")));
918 if (dblocprovider == COLLPROVIDER_ICU && !dbiculocale)
920 if (dlocale && dlocale->
arg)
923 if (distemplate && distemplate->
arg)
925 if (dallowconnections && dallowconnections->
arg)
927 if (dconnlimit && dconnlimit->
arg)
930 if (dbconnlimit < -1)
932 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
933 errmsg(
"invalid connection limit: %d", dbconnlimit)));
953 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
954 errmsg(
"permission denied to create database")));
968 dbtemplate =
"template1";
971 &src_dboid, &src_owner, &src_encoding,
972 &src_istemplate, &src_allowconn,
973 &src_frozenxid, &src_minmxid, &src_deftablespace,
974 &src_collate, &src_ctype, &src_iculocale, &src_locprovider,
977 (
errcode(ERRCODE_UNDEFINED_DATABASE),
978 errmsg(
"template database \"%s\" does not exist",
989 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
990 errmsg(
"permission denied to copy database \"%s\"",
995 if (dstrategy && dstrategy->
arg)
1000 if (strcmp(strategy,
"wal_log") == 0)
1002 else if (strcmp(strategy,
"file_copy") == 0)
1006 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1007 errmsg(
"invalid create database strategy %s", strategy),
1008 errhint(
"Valid strategies are \"wal_log\", and \"file_copy\".")));
1014 if (dbcollate == NULL)
1015 dbcollate = src_collate;
1016 if (dbctype == NULL)
1017 dbctype = src_ctype;
1018 if (dbiculocale == NULL)
1019 dbiculocale = src_iculocale;
1020 if (dblocprovider ==
'\0')
1021 dblocprovider = src_locprovider;
1026 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1032 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1033 errmsg(
"invalid locale name: \"%s\"", dbcollate)));
1034 dbcollate = canonname;
1037 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1038 errmsg(
"invalid locale name: \"%s\"", dbctype)));
1039 dbctype = canonname;
1043 if (dblocprovider == COLLPROVIDER_ICU)
1051 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1052 errmsg(
"ICU locale must be specified")));
1055 if (dblocprovider == COLLPROVIDER_ICU)
1068 if (strcmp(dbtemplate,
"template0") != 0)
1072 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1073 errmsg(
"new encoding (%s) is incompatible with the encoding of the template database (%s)",
1076 errhint(
"Use the same encoding as in the template database, or use template0 as template.")));
1078 if (strcmp(dbcollate, src_collate) != 0)
1080 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1081 errmsg(
"new collation (%s) is incompatible with the collation of the template database (%s)",
1082 dbcollate, src_collate),
1083 errhint(
"Use the same collation as in the template database, or use template0 as template.")));
1085 if (strcmp(dbctype, src_ctype) != 0)
1087 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1088 errmsg(
"new LC_CTYPE (%s) is incompatible with the LC_CTYPE of the template database (%s)",
1089 dbctype, src_ctype),
1090 errhint(
"Use the same LC_CTYPE as in the template database, or use template0 as template.")));
1092 if (dblocprovider != src_locprovider)
1094 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1095 errmsg(
"new locale provider (%s) does not match locale provider of the template database (%s)",
1096 collprovider_name(dblocprovider), collprovider_name(src_locprovider)),
1097 errhint(
"Use the same locale provider as in the template database, or use template0 as template.")));
1099 if (dblocprovider == COLLPROVIDER_ICU)
1103 if (strcmp(dbiculocale, src_iculocale) != 0)
1105 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1106 errmsg(
"new ICU locale (%s) is incompatible with the ICU locale of the template database (%s)",
1107 dbiculocale, src_iculocale),
1108 errhint(
"Use the same ICU locale as in the template database, or use template0 as template.")));
1124 if (src_collversion && !dcollversion)
1126 char *actual_versionstr;
1129 if (!actual_versionstr)
1131 (
errmsg(
"template database \"%s\" has a collation version, but no actual collation version could be determined",
1134 if (strcmp(actual_versionstr, src_collversion) != 0)
1136 (
errmsg(
"template database \"%s\" has a collation version mismatch",
1138 errdetail(
"The template database was created using collation version %s, "
1139 "but the operating system provides version %s.",
1140 src_collversion, actual_versionstr),
1141 errhint(
"Rebuild all objects in the template database that use the default collation and run "
1142 "ALTER DATABASE %s REFRESH COLLATION VERSION, "
1143 "or build PostgreSQL with the right library version.",
1147 if (dbcollversion == NULL)
1148 dbcollversion = src_collversion;
1155 if (dbcollversion == NULL)
1159 if (dtablespacename && dtablespacename->
arg)
1161 char *tablespacename;
1174 if (dst_deftablespace == GLOBALTABLESPACE_OID)
1176 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1177 errmsg(
"pg_global cannot be used as default tablespace")));
1191 if (dst_deftablespace != src_deftablespace)
1198 if (
stat(srcpath, &st) == 0 &&
1202 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1203 errmsg(
"cannot assign new default tablespace \"%s\"",
1205 errdetail(
"There is a conflict because database \"%s\" already has some tables in this tablespace.",
1213 dst_deftablespace = src_deftablespace;
1222 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1224 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1234 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1248 (
errcode(ERRCODE_OBJECT_IN_USE),
1249 errmsg(
"source database \"%s\" is being accessed by other users",
1268 if (existing_dbname != NULL)
1270 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1271 errmsg(
"database OID %u is already in use by database \"%s\"",
1272 dboid, existing_dbname));
1276 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1277 errmsg(
"data directory with the specified OID %u already exists", dboid));
1285 Anum_pg_database_oid);
1295 Assert((dblocprovider == COLLPROVIDER_ICU && dbiculocale) ||
1296 (dblocprovider != COLLPROVIDER_ICU && !dbiculocale));
1299 MemSet(new_record, 0,
sizeof(new_record));
1300 MemSet(new_record_nulls,
false,
sizeof(new_record_nulls));
1303 new_record[Anum_pg_database_datname - 1] =
1307 new_record[Anum_pg_database_datlocprovider - 1] =
CharGetDatum(dblocprovider);
1308 new_record[Anum_pg_database_datistemplate - 1] =
BoolGetDatum(dbistemplate);
1309 new_record[Anum_pg_database_datallowconn - 1] =
BoolGetDatum(dballowconnections);
1310 new_record[Anum_pg_database_datconnlimit - 1] =
Int32GetDatum(dbconnlimit);
1313 new_record[Anum_pg_database_dattablespace - 1] =
ObjectIdGetDatum(dst_deftablespace);
1319 new_record_nulls[Anum_pg_database_daticulocale - 1] =
true;
1323 new_record_nulls[Anum_pg_database_datcollversion - 1] =
true;
1330 new_record_nulls[Anum_pg_database_datacl - 1] =
true;
1333 new_record, new_record_nulls);
1439 if (!(ctype_encoding ==
encoding ||
1441 ctype_encoding == -1 ||
1447 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1448 errmsg(
"encoding \"%s\" does not match locale \"%s\"",
1451 errdetail(
"The chosen LC_CTYPE setting requires encoding \"%s\".",
1454 if (!(collate_encoding ==
encoding ||
1456 collate_encoding == -1 ||
1462 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1463 errmsg(
"encoding \"%s\" does not match locale \"%s\"",
1466 errdetail(
"The chosen LC_COLLATE setting requires encoding \"%s\".",
1533 &db_istemplate, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
1538 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1546 (
errmsg(
"database \"%s\" does not exist, skipping",
1569 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1570 errmsg(
"cannot drop a template database")));
1575 (
errcode(ERRCODE_OBJECT_IN_USE),
1576 errmsg(
"cannot drop the currently open database")));
1588 (
errcode(ERRCODE_OBJECT_IN_USE),
1589 errmsg(
"database \"%s\" is used by an active logical replication slot",
1592 "There are %d active slots.",
1593 nslots_active, nslots_active)));
1604 (
errcode(ERRCODE_OBJECT_IN_USE),
1605 errmsg(
"database \"%s\" is being used by logical replication subscription",
1608 "There are %d subscriptions.",
1609 nsubscriptions, nsubscriptions)));
1627 (
errcode(ERRCODE_OBJECT_IN_USE),
1628 errmsg(
"database \"%s\" is being accessed by other users",
1637 elog(
ERROR,
"cache lookup failed for database %u", db_id);
1733 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
1735 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1736 errmsg(
"database \"%s\" does not exist", oldname)));
1746 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1747 errmsg(
"permission denied to rename database")));
1753 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1754 if (strstr(newname,
"regression") == NULL)
1755 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1764 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1765 errmsg(
"database \"%s\" already exists", newname)));
1775 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1776 errmsg(
"current database cannot be renamed")));
1786 (
errcode(ERRCODE_OBJECT_IN_USE),
1787 errmsg(
"database \"%s\" is being accessed by other users",
1794 elog(
ERROR,
"cache lookup failed for database %u", db_id);
1825 Datum new_record[Natts_pg_database];
1826 bool new_record_nulls[Natts_pg_database];
1827 bool new_record_repl[Natts_pg_database];
1846 NULL, NULL, NULL, NULL, &src_tblspcoid, NULL, NULL, NULL, NULL, NULL))
1848 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1872 (
errcode(ERRCODE_OBJECT_IN_USE),
1873 errmsg(
"cannot change the tablespace of the currently open database")));
1892 if (dst_tblspcoid == GLOBALTABLESPACE_OID)
1894 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1895 errmsg(
"pg_global cannot be used as default tablespace")));
1900 if (src_tblspcoid == dst_tblspcoid)
1916 (
errcode(ERRCODE_OBJECT_IN_USE),
1917 errmsg(
"database \"%s\" is being accessed by other users",
1971 while ((xlde =
ReadDir(dstdir, dst_dbpath)) != NULL)
1973 if (strcmp(xlde->
d_name,
".") == 0 ||
1974 strcmp(xlde->
d_name,
"..") == 0)
1978 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1979 errmsg(
"some relations of database \"%s\" are already in tablespace \"%s\"",
1981 errhint(
"You must move them back to the database's default tablespace before using this command.")));
1990 if (rmdir(dst_dbpath) != 0)
1991 elog(
ERROR,
"could not remove directory \"%s\": %m",
2009 copydir(src_dbpath, dst_dbpath,
false);
2017 xlrec.
db_id = db_id;
2034 Anum_pg_database_datname,
2042 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2045 MemSet(new_record, 0,
sizeof(new_record));
2046 MemSet(new_record_nulls,
false,
sizeof(new_record_nulls));
2047 MemSet(new_record_repl,
false,
sizeof(new_record_repl));
2049 new_record[Anum_pg_database_dattablespace - 1] =
ObjectIdGetDatum(dst_tblspcoid);
2050 new_record_repl[Anum_pg_database_dattablespace - 1] =
true;
2054 new_record_nulls, new_record_repl);
2106 if (!
rmtree(src_dbpath,
true))
2108 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
2117 xlrec.
db_id = db_id;
2164 if (strcmp(opt->
defname,
"force") == 0)
2168 (
errcode(ERRCODE_SYNTAX_ERROR),
2169 errmsg(
"unrecognized DROP DATABASE option \"%s\"", opt->
defname),
2190 bool dbistemplate =
false;
2191 bool dballowconnections =
true;
2192 int dbconnlimit = -1;
2194 DefElem *dallowconnections = NULL;
2197 Datum new_record[Natts_pg_database];
2198 bool new_record_nulls[Natts_pg_database];
2199 bool new_record_repl[Natts_pg_database];
2206 if (strcmp(defel->
defname,
"is_template") == 0)
2210 distemplate = defel;
2212 else if (strcmp(defel->
defname,
"allow_connections") == 0)
2214 if (dallowconnections)
2216 dallowconnections = defel;
2218 else if (strcmp(defel->
defname,
"connection_limit") == 0)
2224 else if (strcmp(defel->
defname,
"tablespace") == 0)
2228 dtablespace = defel;
2232 (
errcode(ERRCODE_SYNTAX_ERROR),
2246 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2247 errmsg(
"option \"%s\" cannot be specified with other options",
2256 if (distemplate && distemplate->
arg)
2258 if (dallowconnections && dallowconnections->
arg)
2260 if (dconnlimit && dconnlimit->
arg)
2263 if (dbconnlimit < -1)
2265 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2266 errmsg(
"invalid connection limit: %d", dbconnlimit)));
2276 Anum_pg_database_datname,
2284 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2285 errmsg(
"database \"%s\" does not exist", stmt->
dbname)));
2288 dboid = datform->oid;
2302 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2303 errmsg(
"cannot disallow connections for current database")));
2308 MemSet(new_record, 0,
sizeof(new_record));
2309 MemSet(new_record_nulls,
false,
sizeof(new_record_nulls));
2310 MemSet(new_record_repl,
false,
sizeof(new_record_repl));
2314 new_record[Anum_pg_database_datistemplate - 1] =
BoolGetDatum(dbistemplate);
2315 new_record_repl[Anum_pg_database_datistemplate - 1] =
true;
2317 if (dallowconnections)
2319 new_record[Anum_pg_database_datallowconn - 1] =
BoolGetDatum(dballowconnections);
2320 new_record_repl[Anum_pg_database_datallowconn - 1] =
true;
2324 new_record[Anum_pg_database_datconnlimit - 1] =
Int32GetDatum(dbconnlimit);
2325 new_record_repl[Anum_pg_database_datconnlimit - 1] =
true;
2329 new_record_nulls, new_record_repl);
2363 Anum_pg_database_datname,
2371 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2372 errmsg(
"database \"%s\" does not exist", stmt->
dbname)));
2375 db_id = datForm->oid;
2384 datum =
heap_getattr(tuple, datForm->datlocprovider == COLLPROVIDER_ICU ? Anum_pg_database_daticulocale : Anum_pg_database_datcollate,
RelationGetDescr(rel), &isnull);
2386 elog(
ERROR,
"unexpected null in pg_database");
2390 if ((!oldversion && newversion) || (oldversion && !newversion))
2391 elog(
ERROR,
"invalid collation version change");
2392 else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
2394 bool nulls[Natts_pg_database] = {0};
2395 bool replaces[Natts_pg_database] = {0};
2399 (
errmsg(
"changing version from %s to %s",
2400 oldversion, newversion)));
2403 replaces[Anum_pg_database_datcollversion - 1] =
true;
2406 values, nulls, replaces);
2412 (
errmsg(
"version has not changed")));
2473 Anum_pg_database_datname,
2481 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2485 db_id = datForm->oid;
2492 if (datForm->datdba != newOwnerId)
2494 Datum repl_val[Natts_pg_database];
2495 bool repl_null[Natts_pg_database];
2496 bool repl_repl[Natts_pg_database];
2521 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2522 errmsg(
"permission denied to change owner of database")));
2524 memset(repl_null,
false,
sizeof(repl_null));
2525 memset(repl_repl,
false,
sizeof(repl_repl));
2527 repl_repl[Anum_pg_database_datdba - 1] =
true;
2535 Anum_pg_database_datacl,
2541 datForm->datdba, newOwnerId);
2542 repl_repl[Anum_pg_database_datacl - 1] =
true;
2581 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2582 errmsg(
"database with OID %u does not exist", dbid)));
2588 elog(
ERROR,
"unexpected null in pg_database");
2612 Oid *dbIdP,
Oid *ownerIdP,
2613 int *encodingP,
bool *dbIsTemplateP,
bool *dbAllowConnP,
2615 Oid *dbTablespace,
char **dbCollate,
char **dbCtype,
char **dbIculocale,
2616 char *dbLocProvider,
2617 char **dbCollversion)
2619 bool result =
false;
2644 Anum_pg_database_datname,
2690 *ownerIdP = dbform->datdba;
2693 *encodingP = dbform->encoding;
2696 *dbIsTemplateP = dbform->datistemplate;
2699 *dbAllowConnP = dbform->datallowconn;
2702 *dbFrozenXidP = dbform->datfrozenxid;
2705 *dbMinMultiP = dbform->datminmxid;
2708 *dbTablespace = dbform->dattablespace;
2711 *dbLocProvider = dbform->datlocprovider;
2728 *dbIculocale = NULL;
2736 *dbCollversion = NULL;
2761 bool result =
false;
2793 Oid *tablespace_ids;
2800 Oid dsttablespace = spcform->oid;
2805 if (dsttablespace == GLOBALTABLESPACE_OID)
2819 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
2834 tablespace_ids = (
Oid *)
palloc(ntblspc *
sizeof(
Oid));
2836 foreach(cell, ltblspc)
2843 xlrec.
db_id = db_id;
2855 pfree(tablespace_ids);
2876 bool result =
false;
2886 Oid dsttablespace = spcform->oid;
2891 if (dsttablespace == GLOBALTABLESPACE_OID)
2919 if (notherbackends > 0 && npreparedxacts > 0)
2925 errdetail(
"There are %d other session(s) and %d prepared transaction(s) using the database.",
2926 notherbackends, npreparedxacts);
2927 else if (notherbackends > 0)
2929 "There are %d other sessions using the database.",
2934 "There are %d prepared transactions using the database.",
2961 Anum_pg_database_datname,
2980 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2981 errmsg(
"database \"%s\" does not exist",
3040 if (!
rmtree(dst_path,
true))
3043 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3061 copydir(src_path, dst_path,
false);
3121 if (!
rmtree(dst_path,
true))
3123 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3141 elog(
PANIC,
"dbase_redo: unknown op code %u", info);
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Oid get_role_oid(const char *rolname, bool missing_ok)
void check_is_member_of_role(Oid member, Oid role)
AclResult pg_tablespace_aclcheck(Oid spc_oid, Oid roleid, AclMode mode)
bool pg_database_ownercheck(Oid db_oid, Oid roleid)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
bool directory_is_empty(const char *path)
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
static Datum values[MAXATTR]
void CreateAndCopyRelationData(RelFileNode src_rnode, RelFileNode dst_rnode, bool permanent)
void DropDatabaseBuffers(Oid dbid)
BlockNumber BufferGetBlockNumber(Buffer buffer)
Buffer ReadBufferWithoutRelcache(RelFileNode rnode, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
void UnlockReleaseBuffer(Buffer buffer)
void LockBuffer(Buffer buffer, int mode)
void FlushDatabaseBuffers(Oid dbid)
#define BUFFER_LOCK_SHARE
#define BufferGetPage(buffer)
#define PageGetMaxOffsetNumber(page)
#define PageGetItemId(page, offsetNumber)
#define PageIsEmpty(page)
#define PageGetItem(page, itemId)
#define CStringGetTextDatum(s)
#define TextDatumGetCString(d)
TransactionId MultiXactId
#define MemSet(start, val, len)
#define OidIsValid(objectId)
#define AssertArg(condition)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
void RequestCheckpoint(int flags)
void copydir(char *fromdir, char *todir, bool recurse)
static bool get_db_info(const char *name, LOCKMODE lockmode, Oid *dbIdP, Oid *ownerIdP, int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP, TransactionId *dbFrozenXidP, MultiXactId *dbMinMultiP, Oid *dbTablespace, char **dbCollate, char **dbCtype, char **dbIculocale, char *dbLocProvider, char **dbCollversion)
static void remove_dbtablespaces(Oid db_id)
static void CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, bool isRedo)
ObjectAddress AlterDatabaseRefreshColl(AlterDatabaseRefreshCollStmt *stmt)
ObjectAddress RenameDatabase(const char *oldname, const char *newname)
char * get_database_name(Oid dbid)
Oid get_database_oid(const char *dbname, bool missing_ok)
static void movedb_failure_callback(int code, Datum arg)
static List * ScanSourceDatabasePgClass(Oid srctbid, Oid srcdbid, char *srcpath)
ObjectAddress AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
Datum pg_database_collation_actual_version(PG_FUNCTION_ARGS)
static void movedb(const char *dbname, const char *tblspcname)
void check_encoding_locale_matches(int encoding, const char *collate, const char *ctype)
Oid createdb(ParseState *pstate, const CreatedbStmt *stmt)
Oid AlterDatabase(ParseState *pstate, AlterDatabaseStmt *stmt, bool isTopLevel)
static int errdetail_busy_db(int notherbackends, int npreparedxacts)
static CreateDBRelInfo * ScanSourceDatabasePgClassTuple(HeapTupleData *tuple, Oid tbid, Oid dbid, char *srcpath)
static bool check_db_file_conflict(Oid db_id)
static void CreateDatabaseUsingFileCopy(Oid src_dboid, Oid dboid, Oid src_tsid, Oid dst_tsid)
struct CreateDBRelInfo CreateDBRelInfo
static bool have_createdb_privilege(void)
void dbase_redo(XLogReaderState *record)
void DropDatabase(ParseState *pstate, DropdbStmt *stmt)
void dropdb(const char *dbname, bool missing_ok, bool force)
static void CreateDatabaseUsingWalLog(Oid src_dboid, Oid dboid, Oid src_tsid, Oid dst_tsid)
static List * ScanSourceDatabasePgClassPage(Page page, Buffer buf, Oid tbid, Oid dbid, char *srcpath, List *rnodelist, Snapshot snapshot)
Oid AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
static void createdb_failure_callback(int code, Datum arg)
#define XLOG_DBASE_CREATE_WAL_LOG
#define MinSizeOfDbaseDropRec
#define XLOG_DBASE_CREATE_FILE_COPY
int32 defGetInt32(DefElem *def)
bool defGetBoolean(DefElem *def)
char * defGetString(DefElem *def)
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
int errcode_for_file_access(void)
int errdetail(const char *fmt,...)
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
int pg_valid_server_encoding(const char *name)
const char * pg_encoding_to_char(int encoding)
struct dirent * ReadDir(DIR *dir, const char *dirname)
int MakePGDirectory(const char *directoryName)
int CloseTransientFile(int fd)
int OpenTransientFile(const char *fileName, int fileFlags)
DIR * AllocateDir(const char *dirname)
static char dstpath[MAXPGPATH]
#define DirectFunctionCall1(func, arg1)
#define PG_RETURN_TEXT_P(x)
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
void systable_endscan(SysScanDesc sysscan)
HeapTuple systable_getnext(SysScanDesc sysscan)
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
bool allowSystemTableMods
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
bool HeapTupleSatisfiesVisibility(HeapTuple tup, Snapshot snapshot, Buffer buffer)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
void heap_freetuple(HeapTuple htup)
HeapTupleHeaderData * HeapTupleHeader
#define HeapTupleIsValid(tuple)
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
#define ItemIdGetLength(itemId)
#define ItemIdIsNormal(itemId)
#define ItemIdIsDead(itemId)
#define ItemIdIsUsed(itemId)
#define ItemIdIsRedirected(itemId)
#define ItemPointerSet(pointer, blockNumber, offNum)
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
List * lappend_oid(List *list, Oid datum)
void list_free(List *list)
void list_free_deep(List *list)
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
void UnlockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode)
void UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
void LockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
void LockRelationId(LockRelId *relid, LOCKMODE lockmode)
#define AccessExclusiveLock
char * pstrdup(const char *in)
void pfree(void *pointer)
void ForgetDatabaseSyncRequests(Oid dbid)
#define START_CRIT_SECTION()
#define CHECK_FOR_INTERRUPTS()
#define END_CRIT_SECTION()
#define InvalidMultiXactId
void namestrcpy(Name name, const char *str)
Datum namein(PG_FUNCTION_ARGS)
#define IsA(nodeptr, _type_)
#define InvokeObjectPostCreateHook(classId, objectId, subId)
#define InvokeObjectPostAlterHook(classId, objectId, subId)
#define InvokeObjectDropHook(classId, objectId, subId)
#define ObjectAddressSet(addr, class_id, object_id)
#define OffsetNumberNext(offsetNumber)
#define FirstOffsetNumber
int parser_errposition(ParseState *pstate, int location)
FormData_pg_authid * Form_pg_authid
FormData_pg_class * Form_pg_class
FormData_pg_database * Form_pg_database
void DropSetting(Oid databaseid, Oid roleid)
void AlterSetting(Oid databaseid, Oid roleid, VariableSetStmt *setstmt)
static int list_length(const List *l)
char * get_collation_actual_version(char collprovider, const char *collcollate)
void check_icu_locale(const char *icu_locale)
bool check_locale(int category, const char *locale, char **canonname)
void dropDatabaseDependencies(Oid databaseId)
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
void copyTemplateDependencies(Oid templateDbId, Oid newDbId)
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
void shdepLockAndCheckObject(Oid classId, Oid objectId)
int CountDBSubscriptions(Oid dbid)
FormData_pg_tablespace * Form_pg_tablespace
#define PG_VALID_BE_ENCODING(_enc)
void pgstat_drop_database(Oid databaseid)
int pg_strcasecmp(const char *s1, const char *s2)
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
#define TransactionIdGetDatum(X)
#define CStringGetDatum(X)
#define DatumGetPointer(X)
#define ObjectIdGetDatum(X)
#define PointerGetDatum(X)
static int fd(const char *x, int i)
void TerminateOtherDBBackends(Oid databaseId)
bool CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
void WaitForProcSignalBarrier(uint64 generation)
uint64 EmitProcSignalBarrier(ProcSignalBarrierType type)
@ PROCSIGNAL_BARRIER_SMGRRELEASE
static SMgrRelation RelationGetSmgr(Relation rel)
#define RelationGetDescr(relation)
void RelationMapCopy(Oid dbid, Oid tsid, char *srcdbpath, char *dstdbpath)
Oid RelationMapOidToFilenodeForDatabase(char *dbpath, Oid relationId)
char * GetDatabasePath(Oid dbNode, Oid spcNode)
bool rmtree(const char *path, bool rmtopdir)
const char * quote_identifier(const char *ident)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
void DeleteSharedSecurityLabel(Oid objectId, Oid classId)
bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive)
void ReplicationSlotsDropDBSlots(Oid dboid)
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Snapshot GetLatestSnapshot(void)
void PopActiveSnapshot(void)
void ResolveRecoveryConflictWithDatabase(Oid dbid)
#define BTEqualStrategyNumber
VariableSetStmt * setstmt
CreateDBStrategy strategy
Oid tablespace_ids[FLEXIBLE_ARRAY_MEMBER]
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
#define SearchSysCacheCopy1(cacheId, key1)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
static void table_endscan(TableScanDesc scan)
#define InvalidTransactionId
#define FirstNormalObjectId
text * cstring_to_text(const char *s)
@ WAIT_EVENT_VERSION_FILE_WRITE
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
void StartTransactionCommand(void)
void ForceSyncCommit(void)
void CommitTransactionCommand(void)
void XLogFlush(XLogRecPtr record)
#define CHECKPOINT_FLUSH_ALL
#define CHECKPOINT_IMMEDIATE
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
void XLogBeginInsert(void)
void XLogRegisterData(char *data, int len)
#define XLogRecGetInfo(decoder)
#define XLogRecGetData(decoder)
#define XLogRecHasAnyBlockRefs(decoder)
#define XLR_SPECIAL_REL_UPDATE
void FreeFakeRelcacheEntry(Relation fakerel)
Relation CreateFakeRelcacheEntry(RelFileNode rnode)
void XLogDropDatabase(Oid dbid)