66 #include "utils/fmgroids.h"
115 static void movedb(
const char *
dbname,
const char *tblspcname);
118 Oid *dbIdP,
Oid *ownerIdP,
119 int *encodingP,
bool *dbIsTemplateP,
bool *dbAllowConnP,
121 Oid *dbTablespace,
char **dbCollate,
char **dbCtype,
char **dbIculocale,
124 char **dbCollversion);
132 Oid dbid,
char *srcpath,
140 Oid src_tsid,
Oid dst_tsid);
150 Oid src_tsid,
Oid dst_tsid)
154 List *rlocatorlist = NULL;
180 srcrelid.
dbId = src_dboid;
181 dstrelid.
dbId = dst_dboid;
184 foreach(cell, rlocatorlist)
195 if (srcrlocator.
spcOid == src_tsid)
196 dstrlocator.
spcOid = dst_tsid;
200 dstrlocator.
dbOid = dst_dboid;
271 relid.
relId = RelationRelationId;
276 rlocator.
dbOid = dbid;
295 for (blkno = 0; blkno < nblocks; blkno++)
312 srcpath, rlocatorlist,
330 char *srcpath,
List *rlocatorlist,
376 rlocatorlist =
lappend(rlocatorlist, relinfo);
412 if (classForm->reltablespace == GLOBALTABLESPACE_OID ||
413 !RELKIND_HAS_STORAGE(classForm->relkind) ||
414 classForm->relpersistence == RELPERSISTENCE_TEMP)
422 relfilenumber = classForm->relfilenode;
429 elog(
ERROR,
"relation with OID %u does not have a valid relfilenumber",
441 relinfo->
reloid = classForm->oid;
444 Assert(classForm->relpersistence != RELPERSISTENCE_TEMP);
446 (classForm->relpersistence == RELPERSISTENCE_PERMANENT) ?
true :
false;
471 nbytes = strlen(PG_MAJORVERSION) + 1;
498 if (errno != EEXIST || !isRedo)
501 errmsg(
"could not create directory \"%s\": %m", dbpath)));
509 snprintf(versionfile,
sizeof(versionfile),
"%s/%s", dbpath,
"PG_VERSION");
512 if (
fd < 0 && errno == EEXIST && isRedo)
518 errmsg(
"could not create file \"%s\": %m", versionfile)));
530 errmsg(
"could not write to file \"%s\": %m", versionfile)));
580 Oid srctablespace = spaceform->oid;
587 if (srctablespace == GLOBALTABLESPACE_OID)
600 if (srctablespace == src_tsid)
601 dsttablespace = dst_tsid;
603 dsttablespace = srctablespace;
618 xlrec.
db_id = dst_dboid;
675 int src_encoding = -1;
676 char *src_collate = NULL;
677 char *src_ctype = NULL;
678 char *src_iculocale = NULL;
679 char *src_icurules = NULL;
680 char src_locprovider =
'\0';
681 char *src_collversion = NULL;
686 Oid src_deftablespace;
687 volatile Oid dst_deftablespace;
690 Datum new_record[Natts_pg_database] = {0};
691 bool new_record_nulls[Natts_pg_database] = {0};
695 DefElem *dtablespacename = NULL;
706 DefElem *dallowconnections = NULL;
711 char *dbowner = NULL;
712 const char *dbtemplate = NULL;
713 char *dbcollate = NULL;
714 char *dbctype = NULL;
715 char *dbiculocale = NULL;
716 char *dbicurules = 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,
"icu_rules") == 0)
788 else if (strcmp(defel->
defname,
"locale_provider") == 0)
792 dlocprovider = defel;
794 else if (strcmp(defel->
defname,
"is_template") == 0)
800 else if (strcmp(defel->
defname,
"allow_connections") == 0)
802 if (dallowconnections)
804 dallowconnections = defel;
806 else if (strcmp(defel->
defname,
"connection_limit") == 0)
812 else if (strcmp(defel->
defname,
"collation_version") == 0)
816 dcollversion = defel;
818 else if (strcmp(defel->
defname,
"location") == 0)
821 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
822 errmsg(
"LOCATION is not supported anymore"),
823 errhint(
"Consider using tablespaces instead."),
826 else if (strcmp(defel->
defname,
"oid") == 0)
847 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
850 else if (strcmp(defel->
defname,
"strategy") == 0)
858 (
errcode(ERRCODE_SYNTAX_ERROR),
863 if (downer && downer->
arg)
865 if (dtemplate && dtemplate->
arg)
867 if (dencoding && dencoding->
arg)
869 const char *encoding_name;
875 if (strcmp(encoding_name,
"") == 0 ||
878 (
errcode(ERRCODE_UNDEFINED_OBJECT),
879 errmsg(
"%d is not a valid encoding code",
889 (
errcode(ERRCODE_UNDEFINED_OBJECT),
890 errmsg(
"%s is not a valid encoding name",
895 if (dlocale && dlocale->
arg)
900 if (dcollate && dcollate->
arg)
902 if (dctype && dctype->
arg)
904 if (diculocale && diculocale->
arg)
906 if (dicurules && dicurules->
arg)
908 if (dlocprovider && dlocprovider->
arg)
913 dblocprovider = COLLPROVIDER_ICU;
915 dblocprovider = COLLPROVIDER_LIBC;
918 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
919 errmsg(
"unrecognized locale provider: %s",
922 if (distemplate && distemplate->
arg)
924 if (dallowconnections && dallowconnections->
arg)
926 if (dconnlimit && dconnlimit->
arg)
929 if (dbconnlimit < -1)
931 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
932 errmsg(
"invalid connection limit: %d", dbconnlimit)));
952 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
953 errmsg(
"permission denied to create database")));
967 dbtemplate =
"template1";
970 &src_dboid, &src_owner, &src_encoding,
971 &src_istemplate, &src_allowconn,
972 &src_frozenxid, &src_minmxid, &src_deftablespace,
973 &src_collate, &src_ctype, &src_iculocale, &src_icurules, &src_locprovider,
976 (
errcode(ERRCODE_UNDEFINED_DATABASE),
977 errmsg(
"template database \"%s\" does not exist",
988 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
989 errmsg(
"permission denied to copy database \"%s\"",
994 if (dstrategy && dstrategy->
arg)
999 if (strcmp(strategy,
"wal_log") == 0)
1001 else if (strcmp(strategy,
"file_copy") == 0)
1005 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1006 errmsg(
"invalid create database strategy \"%s\"", strategy),
1007 errhint(
"Valid strategies are \"wal_log\", and \"file_copy\".")));
1013 if (dbcollate == NULL)
1014 dbcollate = src_collate;
1015 if (dbctype == NULL)
1016 dbctype = src_ctype;
1017 if (dblocprovider ==
'\0')
1018 dblocprovider = src_locprovider;
1019 if (dbiculocale == NULL && dblocprovider == COLLPROVIDER_ICU)
1020 dbiculocale = src_iculocale;
1021 if (dbicurules == NULL && dblocprovider == COLLPROVIDER_ICU)
1022 dbicurules = src_icurules;
1027 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1033 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1034 errmsg(
"invalid locale name: \"%s\"", dbcollate)));
1035 dbcollate = canonname;
1038 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1039 errmsg(
"invalid locale name: \"%s\"", dbctype)));
1040 dbctype = canonname;
1044 if (dblocprovider == COLLPROVIDER_ICU)
1048 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1049 errmsg(
"encoding \"%s\" is not supported with ICU provider",
1058 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1059 errmsg(
"ICU locale must be specified")));
1067 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1068 errmsg(
"ICU locale cannot be specified unless locale provider is ICU")));
1072 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1073 errmsg(
"ICU rules cannot be specified unless locale provider is ICU")));
1086 if (strcmp(dbtemplate,
"template0") != 0)
1090 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1091 errmsg(
"new encoding (%s) is incompatible with the encoding of the template database (%s)",
1094 errhint(
"Use the same encoding as in the template database, or use template0 as template.")));
1096 if (strcmp(dbcollate, src_collate) != 0)
1098 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1099 errmsg(
"new collation (%s) is incompatible with the collation of the template database (%s)",
1100 dbcollate, src_collate),
1101 errhint(
"Use the same collation as in the template database, or use template0 as template.")));
1103 if (strcmp(dbctype, src_ctype) != 0)
1105 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1106 errmsg(
"new LC_CTYPE (%s) is incompatible with the LC_CTYPE of the template database (%s)",
1107 dbctype, src_ctype),
1108 errhint(
"Use the same LC_CTYPE as in the template database, or use template0 as template.")));
1110 if (dblocprovider != src_locprovider)
1112 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1113 errmsg(
"new locale provider (%s) does not match locale provider of the template database (%s)",
1114 collprovider_name(dblocprovider), collprovider_name(src_locprovider)),
1115 errhint(
"Use the same locale provider as in the template database, or use template0 as template.")));
1117 if (dblocprovider == COLLPROVIDER_ICU)
1124 if (strcmp(dbiculocale, src_iculocale) != 0)
1126 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1127 errmsg(
"new ICU locale (%s) is incompatible with the ICU locale of the template database (%s)",
1128 dbiculocale, src_iculocale),
1129 errhint(
"Use the same ICU locale as in the template database, or use template0 as template.")));
1134 val2 = src_icurules;
1137 if (strcmp(val1, val2) != 0)
1139 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1140 errmsg(
"new ICU collation rules (%s) are incompatible with the ICU collation rules of the template database (%s)",
1142 errhint(
"Use the same ICU collation rules as in the template database, or use template0 as template.")));
1158 if (src_collversion && !dcollversion)
1160 char *actual_versionstr;
1163 if (!actual_versionstr)
1165 (
errmsg(
"template database \"%s\" has a collation version, but no actual collation version could be determined",
1168 if (strcmp(actual_versionstr, src_collversion) != 0)
1170 (
errmsg(
"template database \"%s\" has a collation version mismatch",
1172 errdetail(
"The template database was created using collation version %s, "
1173 "but the operating system provides version %s.",
1174 src_collversion, actual_versionstr),
1175 errhint(
"Rebuild all objects in the template database that use the default collation and run "
1176 "ALTER DATABASE %s REFRESH COLLATION VERSION, "
1177 "or build PostgreSQL with the right library version.",
1181 if (dbcollversion == NULL)
1182 dbcollversion = src_collversion;
1189 if (dbcollversion == NULL)
1193 if (dtablespacename && dtablespacename->
arg)
1195 char *tablespacename;
1208 if (dst_deftablespace == GLOBALTABLESPACE_OID)
1210 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1211 errmsg(
"pg_global cannot be used as default tablespace")));
1225 if (dst_deftablespace != src_deftablespace)
1232 if (
stat(srcpath, &st) == 0 &&
1236 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1237 errmsg(
"cannot assign new default tablespace \"%s\"",
1239 errdetail(
"There is a conflict because database \"%s\" already has some tables in this tablespace.",
1247 dst_deftablespace = src_deftablespace;
1256 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1258 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1268 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1282 (
errcode(ERRCODE_OBJECT_IN_USE),
1283 errmsg(
"source database \"%s\" is being accessed by other users",
1302 if (existing_dbname != NULL)
1304 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1305 errmsg(
"database OID %u is already in use by database \"%s\"",
1306 dboid, existing_dbname));
1310 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1311 errmsg(
"data directory with the specified OID %u already exists", dboid));
1319 Anum_pg_database_oid);
1329 Assert((dblocprovider == COLLPROVIDER_ICU && dbiculocale) ||
1330 (dblocprovider != COLLPROVIDER_ICU && !dbiculocale));
1334 new_record[Anum_pg_database_datname - 1] =
1338 new_record[Anum_pg_database_datlocprovider - 1] =
CharGetDatum(dblocprovider);
1339 new_record[Anum_pg_database_datistemplate - 1] =
BoolGetDatum(dbistemplate);
1340 new_record[Anum_pg_database_datallowconn - 1] =
BoolGetDatum(dballowconnections);
1341 new_record[Anum_pg_database_datconnlimit - 1] =
Int32GetDatum(dbconnlimit);
1344 new_record[Anum_pg_database_dattablespace - 1] =
ObjectIdGetDatum(dst_deftablespace);
1350 new_record_nulls[Anum_pg_database_daticulocale - 1] =
true;
1354 new_record_nulls[Anum_pg_database_daticurules - 1] =
true;
1358 new_record_nulls[Anum_pg_database_datcollversion - 1] =
true;
1365 new_record_nulls[Anum_pg_database_datacl - 1] =
true;
1368 new_record, new_record_nulls);
1474 if (!(ctype_encoding ==
encoding ||
1476 ctype_encoding == -1 ||
1482 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1483 errmsg(
"encoding \"%s\" does not match locale \"%s\"",
1486 errdetail(
"The chosen LC_CTYPE setting requires encoding \"%s\".",
1489 if (!(collate_encoding ==
encoding ||
1491 collate_encoding == -1 ||
1497 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1498 errmsg(
"encoding \"%s\" does not match locale \"%s\"",
1501 errdetail(
"The chosen LC_COLLATE setting requires encoding \"%s\".",
1568 &db_istemplate, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
1573 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1581 (
errmsg(
"database \"%s\" does not exist, skipping",
1604 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1605 errmsg(
"cannot drop a template database")));
1610 (
errcode(ERRCODE_OBJECT_IN_USE),
1611 errmsg(
"cannot drop the currently open database")));
1623 (
errcode(ERRCODE_OBJECT_IN_USE),
1624 errmsg(
"database \"%s\" is used by an active logical replication slot",
1627 "There are %d active slots.",
1628 nslots_active, nslots_active)));
1639 (
errcode(ERRCODE_OBJECT_IN_USE),
1640 errmsg(
"database \"%s\" is being used by logical replication subscription",
1643 "There are %d subscriptions.",
1644 nsubscriptions, nsubscriptions)));
1662 (
errcode(ERRCODE_OBJECT_IN_USE),
1663 errmsg(
"database \"%s\" is being accessed by other users",
1672 elog(
ERROR,
"cache lookup failed for database %u", db_id);
1768 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
1770 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1771 errmsg(
"database \"%s\" does not exist", oldname)));
1781 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1782 errmsg(
"permission denied to rename database")));
1788 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1789 if (strstr(newname,
"regression") == NULL)
1790 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1799 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1800 errmsg(
"database \"%s\" already exists", newname)));
1810 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1811 errmsg(
"current database cannot be renamed")));
1821 (
errcode(ERRCODE_OBJECT_IN_USE),
1822 errmsg(
"database \"%s\" is being accessed by other users",
1829 elog(
ERROR,
"cache lookup failed for database %u", db_id);
1878 NULL, NULL, NULL, NULL, &src_tblspcoid, NULL, NULL, NULL, NULL, NULL, NULL))
1880 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1904 (
errcode(ERRCODE_OBJECT_IN_USE),
1905 errmsg(
"cannot change the tablespace of the currently open database")));
1924 if (dst_tblspcoid == GLOBALTABLESPACE_OID)
1926 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1927 errmsg(
"pg_global cannot be used as default tablespace")));
1932 if (src_tblspcoid == dst_tblspcoid)
1948 (
errcode(ERRCODE_OBJECT_IN_USE),
1949 errmsg(
"database \"%s\" is being accessed by other users",
2003 while ((xlde =
ReadDir(dstdir, dst_dbpath)) != NULL)
2005 if (strcmp(xlde->
d_name,
".") == 0 ||
2006 strcmp(xlde->
d_name,
"..") == 0)
2010 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2011 errmsg(
"some relations of database \"%s\" are already in tablespace \"%s\"",
2013 errhint(
"You must move them back to the database's default tablespace before using this command.")));
2022 if (rmdir(dst_dbpath) != 0)
2023 elog(
ERROR,
"could not remove directory \"%s\": %m",
2038 Datum new_record[Natts_pg_database] = {0};
2039 bool new_record_nulls[Natts_pg_database] = {0};
2040 bool new_record_repl[Natts_pg_database] = {0};
2045 copydir(src_dbpath, dst_dbpath,
false);
2053 xlrec.
db_id = db_id;
2070 Anum_pg_database_datname,
2078 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2081 new_record[Anum_pg_database_dattablespace - 1] =
ObjectIdGetDatum(dst_tblspcoid);
2082 new_record_repl[Anum_pg_database_dattablespace - 1] =
true;
2086 new_record_nulls, new_record_repl);
2138 if (!
rmtree(src_dbpath,
true))
2140 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
2149 xlrec.
db_id = db_id;
2192 foreach(lc,
stmt->options)
2196 if (strcmp(opt->
defname,
"force") == 0)
2200 (
errcode(ERRCODE_SYNTAX_ERROR),
2201 errmsg(
"unrecognized DROP DATABASE option \"%s\"", opt->
defname),
2222 bool dbistemplate =
false;
2223 bool dballowconnections =
true;
2224 int dbconnlimit = -1;
2226 DefElem *dallowconnections = NULL;
2229 Datum new_record[Natts_pg_database] = {0};
2230 bool new_record_nulls[Natts_pg_database] = {0};
2231 bool new_record_repl[Natts_pg_database] = {0};
2238 if (strcmp(defel->
defname,
"is_template") == 0)
2242 distemplate = defel;
2244 else if (strcmp(defel->
defname,
"allow_connections") == 0)
2246 if (dallowconnections)
2248 dallowconnections = defel;
2250 else if (strcmp(defel->
defname,
"connection_limit") == 0)
2256 else if (strcmp(defel->
defname,
"tablespace") == 0)
2260 dtablespace = defel;
2264 (
errcode(ERRCODE_SYNTAX_ERROR),
2278 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2279 errmsg(
"option \"%s\" cannot be specified with other options",
2288 if (distemplate && distemplate->
arg)
2290 if (dallowconnections && dallowconnections->
arg)
2292 if (dconnlimit && dconnlimit->
arg)
2295 if (dbconnlimit < -1)
2297 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2298 errmsg(
"invalid connection limit: %d", dbconnlimit)));
2308 Anum_pg_database_datname,
2316 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2317 errmsg(
"database \"%s\" does not exist",
stmt->dbname)));
2320 dboid = datform->oid;
2334 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2335 errmsg(
"cannot disallow connections for current database")));
2342 new_record[Anum_pg_database_datistemplate - 1] =
BoolGetDatum(dbistemplate);
2343 new_record_repl[Anum_pg_database_datistemplate - 1] =
true;
2345 if (dallowconnections)
2347 new_record[Anum_pg_database_datallowconn - 1] =
BoolGetDatum(dballowconnections);
2348 new_record_repl[Anum_pg_database_datallowconn - 1] =
true;
2352 new_record[Anum_pg_database_datconnlimit - 1] =
Int32GetDatum(dbconnlimit);
2353 new_record_repl[Anum_pg_database_datconnlimit - 1] =
true;
2357 new_record_nulls, new_record_repl);
2391 Anum_pg_database_datname,
2399 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2400 errmsg(
"database \"%s\" does not exist",
stmt->dbname)));
2403 db_id = datForm->oid;
2412 datum =
heap_getattr(tuple, datForm->datlocprovider == COLLPROVIDER_ICU ? Anum_pg_database_daticulocale : Anum_pg_database_datcollate,
RelationGetDescr(rel), &isnull);
2414 elog(
ERROR,
"unexpected null in pg_database");
2418 if ((!oldversion && newversion) || (oldversion && !newversion))
2419 elog(
ERROR,
"invalid collation version change");
2420 else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
2422 bool nulls[Natts_pg_database] = {0};
2423 bool replaces[Natts_pg_database] = {0};
2427 (
errmsg(
"changing version from %s to %s",
2428 oldversion, newversion)));
2431 replaces[Anum_pg_database_datcollversion - 1] =
true;
2434 values, nulls, replaces);
2440 (
errmsg(
"version has not changed")));
2501 Anum_pg_database_datname,
2509 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2513 db_id = datForm->oid;
2520 if (datForm->datdba != newOwnerId)
2522 Datum repl_val[Natts_pg_database];
2523 bool repl_null[Natts_pg_database] = {0};
2524 bool repl_repl[Natts_pg_database] = {0};
2549 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2550 errmsg(
"permission denied to change owner of database")));
2552 repl_repl[Anum_pg_database_datdba - 1] =
true;
2560 Anum_pg_database_datacl,
2566 datForm->datdba, newOwnerId);
2567 repl_repl[Anum_pg_database_datacl - 1] =
true;
2606 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2607 errmsg(
"database with OID %u does not exist", dbid)));
2613 elog(
ERROR,
"unexpected null in pg_database");
2637 Oid *dbIdP,
Oid *ownerIdP,
2638 int *encodingP,
bool *dbIsTemplateP,
bool *dbAllowConnP,
2640 Oid *dbTablespace,
char **dbCollate,
char **dbCtype,
char **dbIculocale,
2642 char *dbLocProvider,
2643 char **dbCollversion)
2645 bool result =
false;
2670 Anum_pg_database_datname,
2716 *ownerIdP = dbform->datdba;
2719 *encodingP = dbform->encoding;
2722 *dbIsTemplateP = dbform->datistemplate;
2725 *dbAllowConnP = dbform->datallowconn;
2728 *dbFrozenXidP = dbform->datfrozenxid;
2731 *dbMinMultiP = dbform->datminmxid;
2734 *dbTablespace = dbform->dattablespace;
2737 *dbLocProvider = dbform->datlocprovider;
2754 *dbIculocale = NULL;
2770 *dbCollversion = NULL;
2795 bool result =
false;
2827 Oid *tablespace_ids;
2834 Oid dsttablespace = spcform->oid;
2839 if (dsttablespace == GLOBALTABLESPACE_OID)
2853 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
2868 tablespace_ids = (
Oid *)
palloc(ntblspc *
sizeof(
Oid));
2870 foreach(cell, ltblspc)
2877 xlrec.
db_id = db_id;
2889 pfree(tablespace_ids);
2910 bool result =
false;
2920 Oid dsttablespace = spcform->oid;
2925 if (dsttablespace == GLOBALTABLESPACE_OID)
2953 if (notherbackends > 0 && npreparedxacts > 0)
2959 errdetail(
"There are %d other session(s) and %d prepared transaction(s) using the database.",
2960 notherbackends, npreparedxacts);
2961 else if (notherbackends > 0)
2963 "There are %d other sessions using the database.",
2968 "There are %d prepared transactions using the database.",
2995 Anum_pg_database_datname,
3014 (
errcode(ERRCODE_UNDEFINED_DATABASE),
3015 errmsg(
"database \"%s\" does not exist",
3065 if (
stat(path, &st) == 0)
3068 if (only_tblspc && strstr(path,
"pg_tblspc/") == NULL)
3069 elog(
PANIC,
"requested to created invalid directory: %s", path);
3073 errmsg(
"missing directory \"%s\"", path));
3076 "creating missing directory: %s", path);
3080 errmsg(
"could not create missing directory \"%s\": %m", path));
3114 if (!
rmtree(dst_path,
true))
3117 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3125 parent_path =
pstrdup(dst_path);
3127 if (
stat(parent_path, &st) < 0)
3129 if (errno != ENOENT)
3131 errmsg(
"could not stat directory \"%s\": %m",
3145 if (
stat(src_path, &st) < 0 && errno == ENOENT)
3162 copydir(src_path, dst_path,
false);
3177 parent_path =
pstrdup(dbpath);
3228 if (!
rmtree(dst_path,
true))
3230 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3248 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_can_set_role(Oid member, Oid role)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
bool directory_is_empty(const char *path)
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
bool allow_in_place_tablespaces
static Datum values[MAXATTR]
void DropDatabaseBuffers(Oid dbid)
BlockNumber BufferGetBlockNumber(Buffer buffer)
void CreateAndCopyRelationData(RelFileLocator src_rlocator, RelFileLocator dst_rlocator, bool permanent)
void UnlockReleaseBuffer(Buffer buffer)
void LockBuffer(Buffer buffer, int mode)
Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
void FlushDatabaseBuffers(Oid dbid)
#define BUFFER_LOCK_SHARE
static Page BufferGetPage(Buffer buffer)
static bool PageIsEmpty(Page page)
static Item PageGetItem(Page page, ItemId itemId)
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
static bool PageIsNew(Page page)
static OffsetNumber PageGetMaxOffsetNumber(Page page)
#define CStringGetTextDatum(s)
#define TextDatumGetCString(d)
TransactionId MultiXactId
#define OidIsValid(objectId)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
void RequestCheckpoint(int flags)
void copydir(const char *fromdir, const char *todir, bool recurse)
static void remove_dbtablespaces(Oid db_id)
static void CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, bool isRedo)
ObjectAddress AlterDatabaseRefreshColl(AlterDatabaseRefreshCollStmt *stmt)
bool have_createdb_privilege(void)
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 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 **dbIcurules, char *dbLocProvider, char **dbCollversion)
static void movedb_failure_callback(int code, Datum arg)
static void CreateDatabaseUsingWalLog(Oid src_dboid, Oid dst_dboid, Oid src_tsid, Oid dst_tsid)
static List * ScanSourceDatabasePgClass(Oid tbid, Oid dbid, char *srcpath)
static void recovery_create_dbdir(char *path, bool only_tblspc)
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)
static List * ScanSourceDatabasePgClassPage(Page page, Buffer buf, Oid tbid, Oid dbid, char *srcpath, List *rlocatorlist, Snapshot snapshot)
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)
struct CreateDBRelInfo CreateDBRelInfo
void dbase_redo(XLogReaderState *record)
static void CreateDatabaseUsingFileCopy(Oid src_dboid, Oid dst_dboid, Oid src_tsid, Oid dst_tsid)
void DropDatabase(ParseState *pstate, DropdbStmt *stmt)
void dropdb(const char *dbname, bool missing_ok, bool force)
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
elog(ERROR, "%s: %s", p2, msg)
int32 defGetInt32(DefElem *def)
bool defGetBoolean(DefElem *def)
char * defGetString(DefElem *def)
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Oid defGetObjectId(DefElem *def)
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)
bool is_encoding_supported_by_icu(int encoding)
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 htup, 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)
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber 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_mkdir_p(char *path, int omode)
int pg_strcasecmp(const char *s1, const char *s2)
void get_parent_directory(char *path)
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
static Datum PointerGetDatum(const void *X)
static Datum TransactionIdGetDatum(TransactionId X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Pointer DatumGetPointer(Datum X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
static Datum CharGetDatum(char 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
#define RelationGetDescr(relation)
RelFileNumber RelationMapOidToFilenumberForDatabase(char *dbpath, Oid relationId)
void RelationMapCopy(Oid dbid, Oid tsid, char *srcdbpath, char *dstdbpath)
char * GetDatabasePath(Oid dbOid, Oid spcOid)
#define InvalidRelFileNumber
#define RelFileNumberIsValid(relnumber)
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)
void smgrclose(SMgrRelation reln)
SMgrRelation smgropen(RelFileLocator rlocator, BackendId backend)
Snapshot GetLatestSnapshot(void)
void PopActiveSnapshot(void)
void ResolveRecoveryConflictWithDatabase(Oid dbid)
#define BTEqualStrategyNumber
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)
bool RecoveryInProgress(void)
void XLogFlush(XLogRecPtr record)
#define CHECKPOINT_FLUSH_ALL
#define CHECKPOINT_IMMEDIATE
void XLogRegisterData(char *data, uint32 len)
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
void XLogBeginInsert(void)
#define XLogRecGetInfo(decoder)
#define XLogRecGetData(decoder)
#define XLogRecHasAnyBlockRefs(decoder)
#define XLR_SPECIAL_REL_UPDATE
void XLogDropDatabase(Oid dbid)