66 #include "utils/fmgroids.h"
114 static void movedb(
const char *
dbname,
const char *tblspcname);
117 Oid *dbIdP,
Oid *ownerIdP,
118 int *encodingP,
bool *dbIsTemplateP,
bool *dbAllowConnP,
bool *dbHasLoginEvtP,
120 Oid *dbTablespace,
char **dbCollate,
char **dbCtype,
char **dbLocale,
123 char **dbCollversion);
131 Oid dbid,
char *srcpath,
139 Oid src_tsid,
Oid dst_tsid);
149 Oid src_tsid,
Oid dst_tsid)
153 List *rlocatorlist = NULL;
179 srcrelid.
dbId = src_dboid;
180 dstrelid.
dbId = dst_dboid;
183 foreach(cell, rlocatorlist)
194 if (srcrlocator.
spcOid == src_tsid)
195 dstrlocator.
spcOid = dst_tsid;
199 dstrlocator.
dbOid = dst_dboid;
270 relid.
relId = RelationRelationId;
275 rlocator.
dbOid = dbid;
294 for (blkno = 0; blkno < nblocks; blkno++)
311 srcpath, rlocatorlist,
329 char *srcpath,
List *rlocatorlist,
375 rlocatorlist =
lappend(rlocatorlist, relinfo);
411 if (classForm->reltablespace == GLOBALTABLESPACE_OID ||
412 !RELKIND_HAS_STORAGE(classForm->relkind) ||
413 classForm->relpersistence == RELPERSISTENCE_TEMP)
421 relfilenumber = classForm->relfilenode;
428 elog(
ERROR,
"relation with OID %u does not have a valid relfilenumber",
440 relinfo->
reloid = classForm->oid;
443 Assert(classForm->relpersistence != RELPERSISTENCE_TEMP);
445 (classForm->relpersistence == RELPERSISTENCE_PERMANENT) ?
true :
false;
468 nbytes = strlen(PG_MAJORVERSION) + 1;
474 if (errno != EEXIST || !isRedo)
477 errmsg(
"could not create directory \"%s\": %m", dbpath)));
485 snprintf(versionfile,
sizeof(versionfile),
"%s/%s", dbpath,
"PG_VERSION");
488 if (
fd < 0 && errno == EEXIST && isRedo)
494 errmsg(
"could not create file \"%s\": %m", versionfile)));
506 errmsg(
"could not write to file \"%s\": %m", versionfile)));
514 errmsg(
"could not fsync file \"%s\": %m", versionfile)));
584 Oid srctablespace = spaceform->oid;
591 if (srctablespace == GLOBALTABLESPACE_OID)
604 if (srctablespace == src_tsid)
605 dsttablespace = dst_tsid;
607 dsttablespace = srctablespace;
622 xlrec.
db_id = dst_dboid;
687 int src_encoding = -1;
688 char *src_collate = NULL;
689 char *src_ctype = NULL;
690 char *src_locale = NULL;
691 char *src_icurules = NULL;
692 char src_locprovider =
'\0';
693 char *src_collversion = NULL;
695 bool src_hasloginevt =
false;
699 Oid src_deftablespace;
700 volatile Oid dst_deftablespace;
703 Datum new_record[Natts_pg_database] = {0};
704 bool new_record_nulls[Natts_pg_database] = {0};
708 DefElem *tablespacenameEl = NULL;
713 DefElem *builtinlocaleEl = NULL;
720 DefElem *allowconnectionsEl = NULL;
725 char *dbowner = NULL;
726 const char *dbtemplate = NULL;
727 char *dbcollate = NULL;
728 char *dbctype = NULL;
729 const char *dblocale = NULL;
730 char *dbicurules = NULL;
731 char dblocprovider =
'\0';
734 bool dbistemplate =
false;
735 bool dballowconnections =
true;
737 char *dbcollversion = NULL;
748 if (strcmp(defel->
defname,
"tablespace") == 0)
750 if (tablespacenameEl)
752 tablespacenameEl = defel;
754 else if (strcmp(defel->
defname,
"owner") == 0)
760 else if (strcmp(defel->
defname,
"template") == 0)
766 else if (strcmp(defel->
defname,
"encoding") == 0)
772 else if (strcmp(defel->
defname,
"locale") == 0)
778 else if (strcmp(defel->
defname,
"builtin_locale") == 0)
782 builtinlocaleEl = defel;
784 else if (strcmp(defel->
defname,
"lc_collate") == 0)
790 else if (strcmp(defel->
defname,
"lc_ctype") == 0)
796 else if (strcmp(defel->
defname,
"icu_locale") == 0)
802 else if (strcmp(defel->
defname,
"icu_rules") == 0)
808 else if (strcmp(defel->
defname,
"locale_provider") == 0)
812 locproviderEl = defel;
814 else if (strcmp(defel->
defname,
"is_template") == 0)
818 istemplateEl = defel;
820 else if (strcmp(defel->
defname,
"allow_connections") == 0)
822 if (allowconnectionsEl)
824 allowconnectionsEl = defel;
826 else if (strcmp(defel->
defname,
"connection_limit") == 0)
832 else if (strcmp(defel->
defname,
"collation_version") == 0)
836 collversionEl = defel;
838 else if (strcmp(defel->
defname,
"location") == 0)
841 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
842 errmsg(
"LOCATION is not supported anymore"),
843 errhint(
"Consider using tablespaces instead."),
846 else if (strcmp(defel->
defname,
"oid") == 0)
867 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
870 else if (strcmp(defel->
defname,
"strategy") == 0)
878 (
errcode(ERRCODE_SYNTAX_ERROR),
883 if (ownerEl && ownerEl->
arg)
885 if (templateEl && templateEl->
arg)
887 if (encodingEl && encodingEl->
arg)
889 const char *encoding_name;
895 if (strcmp(encoding_name,
"") == 0 ||
898 (
errcode(ERRCODE_UNDEFINED_OBJECT),
899 errmsg(
"%d is not a valid encoding code",
909 (
errcode(ERRCODE_UNDEFINED_OBJECT),
910 errmsg(
"%s is not a valid encoding name",
915 if (localeEl && localeEl->
arg)
921 if (builtinlocaleEl && builtinlocaleEl->
arg)
923 if (collateEl && collateEl->
arg)
925 if (ctypeEl && ctypeEl->
arg)
927 if (iculocaleEl && iculocaleEl->
arg)
929 if (icurulesEl && icurulesEl->
arg)
931 if (locproviderEl && locproviderEl->
arg)
936 dblocprovider = COLLPROVIDER_BUILTIN;
938 dblocprovider = COLLPROVIDER_ICU;
940 dblocprovider = COLLPROVIDER_LIBC;
943 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
944 errmsg(
"unrecognized locale provider: %s",
947 if (istemplateEl && istemplateEl->
arg)
949 if (allowconnectionsEl && allowconnectionsEl->
arg)
951 if (connlimitEl && connlimitEl->
arg)
956 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
957 errmsg(
"invalid connection limit: %d", dbconnlimit)));
977 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
978 errmsg(
"permission denied to create database")));
992 dbtemplate =
"template1";
995 &src_dboid, &src_owner, &src_encoding,
996 &src_istemplate, &src_allowconn, &src_hasloginevt,
997 &src_frozenxid, &src_minmxid, &src_deftablespace,
998 &src_collate, &src_ctype, &src_locale, &src_icurules, &src_locprovider,
1001 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1002 errmsg(
"template database \"%s\" does not exist",
1011 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1012 errmsg(
"cannot use invalid database \"%s\" as template", dbtemplate),
1013 errhint(
"Use DROP DATABASE to drop invalid databases."));
1019 if (!src_istemplate)
1023 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1024 errmsg(
"permission denied to copy database \"%s\"",
1029 if (strategyEl && strategyEl->
arg)
1040 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1041 errmsg(
"invalid create database strategy \"%s\"", strategy),
1042 errhint(
"Valid strategies are \"wal_log\" and \"file_copy\".")));
1048 if (dbcollate == NULL)
1049 dbcollate = src_collate;
1050 if (dbctype == NULL)
1051 dbctype = src_ctype;
1052 if (dblocprovider ==
'\0')
1053 dblocprovider = src_locprovider;
1054 if (dblocale == NULL)
1055 dblocale = src_locale;
1056 if (dbicurules == NULL)
1057 dbicurules = src_icurules;
1062 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1068 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1069 errmsg(
"invalid LC_COLLATE locale name: \"%s\"", dbcollate),
1070 errhint(
"If the locale name is specific to ICU, use ICU_LOCALE.")));
1071 dbcollate = canonname;
1074 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1075 errmsg(
"invalid LC_CTYPE locale name: \"%s\"", dbctype),
1076 errhint(
"If the locale name is specific to ICU, use ICU_LOCALE.")));
1077 dbctype = canonname;
1082 if (dblocprovider != COLLPROVIDER_BUILTIN)
1084 if (builtinlocaleEl)
1086 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1087 errmsg(
"BUILTIN_LOCALE cannot be specified unless locale provider is builtin")));
1090 if (dblocprovider != COLLPROVIDER_ICU)
1094 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1095 errmsg(
"ICU locale cannot be specified unless locale provider is ICU")));
1099 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1100 errmsg(
"ICU rules cannot be specified unless locale provider is ICU")));
1104 if (dblocprovider == COLLPROVIDER_BUILTIN)
1112 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1113 errmsg(
"LOCALE or BUILTIN_LOCALE must be specified")));
1117 else if (dblocprovider == COLLPROVIDER_ICU)
1121 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1122 errmsg(
"encoding \"%s\" is not supported with ICU provider",
1131 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1132 errmsg(
"LOCALE or ICU_LOCALE must be specified")));
1144 if (langtag && strcmp(dblocale, langtag) != 0)
1147 (
errmsg(
"using standard form \"%s\" for ICU locale \"%s\"",
1148 langtag, dblocale)));
1158 if (dblocprovider == COLLPROVIDER_LIBC)
1171 if (strcmp(dbtemplate,
"template0") != 0)
1175 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1176 errmsg(
"new encoding (%s) is incompatible with the encoding of the template database (%s)",
1179 errhint(
"Use the same encoding as in the template database, or use template0 as template.")));
1181 if (strcmp(dbcollate, src_collate) != 0)
1183 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1184 errmsg(
"new collation (%s) is incompatible with the collation of the template database (%s)",
1185 dbcollate, src_collate),
1186 errhint(
"Use the same collation as in the template database, or use template0 as template.")));
1188 if (strcmp(dbctype, src_ctype) != 0)
1190 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1191 errmsg(
"new LC_CTYPE (%s) is incompatible with the LC_CTYPE of the template database (%s)",
1192 dbctype, src_ctype),
1193 errhint(
"Use the same LC_CTYPE as in the template database, or use template0 as template.")));
1195 if (dblocprovider != src_locprovider)
1197 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1198 errmsg(
"new locale provider (%s) does not match locale provider of the template database (%s)",
1199 collprovider_name(dblocprovider), collprovider_name(src_locprovider)),
1200 errhint(
"Use the same locale provider as in the template database, or use template0 as template.")));
1202 if (dblocprovider == COLLPROVIDER_ICU)
1209 if (strcmp(dblocale, src_locale) != 0)
1211 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1212 errmsg(
"new ICU locale (%s) is incompatible with the ICU locale of the template database (%s)",
1213 dblocale, src_locale),
1214 errhint(
"Use the same ICU locale as in the template database, or use template0 as template.")));
1219 val2 = src_icurules;
1222 if (strcmp(val1, val2) != 0)
1224 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1225 errmsg(
"new ICU collation rules (%s) are incompatible with the ICU collation rules of the template database (%s)",
1227 errhint(
"Use the same ICU collation rules as in the template database, or use template0 as template.")));
1243 if (src_collversion && !collversionEl)
1245 char *actual_versionstr;
1248 if (dblocprovider == COLLPROVIDER_LIBC)
1254 if (!actual_versionstr)
1256 (
errmsg(
"template database \"%s\" has a collation version, but no actual collation version could be determined",
1259 if (strcmp(actual_versionstr, src_collversion) != 0)
1261 (
errmsg(
"template database \"%s\" has a collation version mismatch",
1263 errdetail(
"The template database was created using collation version %s, "
1264 "but the operating system provides version %s.",
1265 src_collversion, actual_versionstr),
1266 errhint(
"Rebuild all objects in the template database that use the default collation and run "
1267 "ALTER DATABASE %s REFRESH COLLATION VERSION, "
1268 "or build PostgreSQL with the right library version.",
1272 if (dbcollversion == NULL)
1273 dbcollversion = src_collversion;
1280 if (dbcollversion == NULL)
1284 if (dblocprovider == COLLPROVIDER_LIBC)
1293 if (tablespacenameEl && tablespacenameEl->
arg)
1295 char *tablespacename;
1308 if (dst_deftablespace == GLOBALTABLESPACE_OID)
1310 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1311 errmsg(
"pg_global cannot be used as default tablespace")));
1325 if (dst_deftablespace != src_deftablespace)
1332 if (
stat(srcpath, &st) == 0 &&
1336 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1337 errmsg(
"cannot assign new default tablespace \"%s\"",
1339 errdetail(
"There is a conflict because database \"%s\" already has some tables in this tablespace.",
1347 dst_deftablespace = src_deftablespace;
1356 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1358 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1368 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1382 (
errcode(ERRCODE_OBJECT_IN_USE),
1383 errmsg(
"source database \"%s\" is being accessed by other users",
1402 if (existing_dbname != NULL)
1404 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1405 errmsg(
"database OID %u is already in use by database \"%s\"",
1406 dboid, existing_dbname));
1410 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1411 errmsg(
"data directory with the specified OID %u already exists", dboid));
1419 Anum_pg_database_oid);
1429 Assert((dblocprovider != COLLPROVIDER_LIBC && dblocale) ||
1430 (dblocprovider == COLLPROVIDER_LIBC && !dblocale));
1434 new_record[Anum_pg_database_datname - 1] =
1438 new_record[Anum_pg_database_datlocprovider - 1] =
CharGetDatum(dblocprovider);
1439 new_record[Anum_pg_database_datistemplate - 1] =
BoolGetDatum(dbistemplate);
1440 new_record[Anum_pg_database_datallowconn - 1] =
BoolGetDatum(dballowconnections);
1441 new_record[Anum_pg_database_dathasloginevt - 1] =
BoolGetDatum(src_hasloginevt);
1442 new_record[Anum_pg_database_datconnlimit - 1] =
Int32GetDatum(dbconnlimit);
1445 new_record[Anum_pg_database_dattablespace - 1] =
ObjectIdGetDatum(dst_deftablespace);
1451 new_record_nulls[Anum_pg_database_datlocale - 1] =
true;
1455 new_record_nulls[Anum_pg_database_daticurules - 1] =
true;
1459 new_record_nulls[Anum_pg_database_datcollversion - 1] =
true;
1466 new_record_nulls[Anum_pg_database_datacl - 1] =
true;
1469 new_record, new_record_nulls);
1575 if (!(ctype_encoding ==
encoding ||
1577 ctype_encoding == -1 ||
1583 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1584 errmsg(
"encoding \"%s\" does not match locale \"%s\"",
1587 errdetail(
"The chosen LC_CTYPE setting requires encoding \"%s\".",
1590 if (!(collate_encoding ==
encoding ||
1592 collate_encoding == -1 ||
1598 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1599 errmsg(
"encoding \"%s\" does not match locale \"%s\"",
1602 errdetail(
"The chosen LC_COLLATE setting requires encoding \"%s\".",
1672 &db_istemplate, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
1677 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1685 (
errmsg(
"database \"%s\" does not exist, skipping",
1708 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1709 errmsg(
"cannot drop a template database")));
1714 (
errcode(ERRCODE_OBJECT_IN_USE),
1715 errmsg(
"cannot drop the currently open database")));
1727 (
errcode(ERRCODE_OBJECT_IN_USE),
1728 errmsg(
"database \"%s\" is used by an active logical replication slot",
1731 "There are %d active slots.",
1732 nslots_active, nslots_active)));
1743 (
errcode(ERRCODE_OBJECT_IN_USE),
1744 errmsg(
"database \"%s\" is being used by logical replication subscription",
1747 "There are %d subscriptions.",
1748 nsubscriptions, nsubscriptions)));
1766 (
errcode(ERRCODE_OBJECT_IN_USE),
1767 errmsg(
"database \"%s\" is being accessed by other users",
1798 Anum_pg_database_datname,
1807 elog(
ERROR,
"cache lookup failed for database %u", db_id);
1902 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
1904 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1905 errmsg(
"database \"%s\" does not exist", oldname)));
1915 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1916 errmsg(
"permission denied to rename database")));
1922 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1923 if (strstr(newname,
"regression") == NULL)
1924 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1933 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1934 errmsg(
"database \"%s\" already exists", newname)));
1944 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1945 errmsg(
"current database cannot be renamed")));
1955 (
errcode(ERRCODE_OBJECT_IN_USE),
1956 errmsg(
"database \"%s\" is being accessed by other users",
1963 elog(
ERROR,
"cache lookup failed for database %u", db_id);
2012 NULL, NULL, NULL, NULL, &src_tblspcoid, NULL, NULL, NULL, NULL, NULL, NULL))
2014 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2038 (
errcode(ERRCODE_OBJECT_IN_USE),
2039 errmsg(
"cannot change the tablespace of the currently open database")));
2058 if (dst_tblspcoid == GLOBALTABLESPACE_OID)
2060 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2061 errmsg(
"pg_global cannot be used as default tablespace")));
2066 if (src_tblspcoid == dst_tblspcoid)
2082 (
errcode(ERRCODE_OBJECT_IN_USE),
2083 errmsg(
"database \"%s\" is being accessed by other users",
2137 while ((xlde =
ReadDir(dstdir, dst_dbpath)) != NULL)
2139 if (strcmp(xlde->
d_name,
".") == 0 ||
2140 strcmp(xlde->
d_name,
"..") == 0)
2144 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2145 errmsg(
"some relations of database \"%s\" are already in tablespace \"%s\"",
2147 errhint(
"You must move them back to the database's default tablespace before using this command.")));
2156 if (rmdir(dst_dbpath) != 0)
2157 elog(
ERROR,
"could not remove directory \"%s\": %m",
2172 Datum new_record[Natts_pg_database] = {0};
2173 bool new_record_nulls[Natts_pg_database] = {0};
2174 bool new_record_repl[Natts_pg_database] = {0};
2179 copydir(src_dbpath, dst_dbpath,
false);
2187 xlrec.
db_id = db_id;
2204 Anum_pg_database_datname,
2212 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2215 new_record[Anum_pg_database_dattablespace - 1] =
ObjectIdGetDatum(dst_tblspcoid);
2216 new_record_repl[Anum_pg_database_dattablespace - 1] =
true;
2220 new_record_nulls, new_record_repl);
2272 if (!
rmtree(src_dbpath,
true))
2274 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
2283 xlrec.
db_id = db_id;
2326 foreach(lc,
stmt->options)
2330 if (strcmp(opt->
defname,
"force") == 0)
2334 (
errcode(ERRCODE_SYNTAX_ERROR),
2335 errmsg(
"unrecognized DROP DATABASE option \"%s\"", opt->
defname),
2356 bool dbistemplate =
false;
2357 bool dballowconnections =
true;
2360 DefElem *dallowconnections = NULL;
2363 Datum new_record[Natts_pg_database] = {0};
2364 bool new_record_nulls[Natts_pg_database] = {0};
2365 bool new_record_repl[Natts_pg_database] = {0};
2372 if (strcmp(defel->
defname,
"is_template") == 0)
2376 distemplate = defel;
2378 else if (strcmp(defel->
defname,
"allow_connections") == 0)
2380 if (dallowconnections)
2382 dallowconnections = defel;
2384 else if (strcmp(defel->
defname,
"connection_limit") == 0)
2390 else if (strcmp(defel->
defname,
"tablespace") == 0)
2394 dtablespace = defel;
2398 (
errcode(ERRCODE_SYNTAX_ERROR),
2412 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2413 errmsg(
"option \"%s\" cannot be specified with other options",
2422 if (distemplate && distemplate->
arg)
2424 if (dallowconnections && dallowconnections->
arg)
2426 if (dconnlimit && dconnlimit->
arg)
2431 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2432 errmsg(
"invalid connection limit: %d", dbconnlimit)));
2442 Anum_pg_database_datname,
2450 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2451 errmsg(
"database \"%s\" does not exist",
stmt->dbname)));
2454 dboid = datform->oid;
2459 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2460 errmsg(
"cannot alter invalid database \"%s\"",
stmt->dbname),
2461 errhint(
"Use DROP DATABASE to drop invalid databases."));
2476 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2477 errmsg(
"cannot disallow connections for current database")));
2484 new_record[Anum_pg_database_datistemplate - 1] =
BoolGetDatum(dbistemplate);
2485 new_record_repl[Anum_pg_database_datistemplate - 1] =
true;
2487 if (dallowconnections)
2489 new_record[Anum_pg_database_datallowconn - 1] =
BoolGetDatum(dballowconnections);
2490 new_record_repl[Anum_pg_database_datallowconn - 1] =
true;
2494 new_record[Anum_pg_database_datconnlimit - 1] =
Int32GetDatum(dbconnlimit);
2495 new_record_repl[Anum_pg_database_datconnlimit - 1] =
true;
2499 new_record_nulls, new_record_repl);
2533 Anum_pg_database_datname,
2541 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2542 errmsg(
"database \"%s\" does not exist",
stmt->dbname)));
2545 db_id = datForm->oid;
2554 if (datForm->datlocprovider == COLLPROVIDER_LIBC)
2558 elog(
ERROR,
"unexpected null in pg_database");
2564 elog(
ERROR,
"unexpected null in pg_database");
2571 if ((!oldversion && newversion) || (oldversion && !newversion))
2572 elog(
ERROR,
"invalid collation version change");
2573 else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
2575 bool nulls[Natts_pg_database] = {0};
2576 bool replaces[Natts_pg_database] = {0};
2580 (
errmsg(
"changing version from %s to %s",
2581 oldversion, newversion)));
2584 replaces[Anum_pg_database_datcollversion - 1] =
true;
2587 values, nulls, replaces);
2593 (
errmsg(
"version has not changed")));
2654 Anum_pg_database_datname,
2662 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2666 db_id = datForm->oid;
2673 if (datForm->datdba != newOwnerId)
2675 Datum repl_val[Natts_pg_database];
2676 bool repl_null[Natts_pg_database] = {0};
2677 bool repl_repl[Natts_pg_database] = {0};
2702 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2703 errmsg(
"permission denied to change owner of database")));
2705 repl_repl[Anum_pg_database_datdba - 1] =
true;
2713 Anum_pg_database_datacl,
2719 datForm->datdba, newOwnerId);
2720 repl_repl[Anum_pg_database_datacl - 1] =
true;
2758 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2759 errmsg(
"database with OID %u does not exist", dbid)));
2792 Oid *dbIdP,
Oid *ownerIdP,
2793 int *encodingP,
bool *dbIsTemplateP,
bool *dbAllowConnP,
bool *dbHasLoginEvtP,
2795 Oid *dbTablespace,
char **dbCollate,
char **dbCtype,
char **dbLocale,
2797 char *dbLocProvider,
2798 char **dbCollversion)
2800 bool result =
false;
2825 Anum_pg_database_datname,
2871 *ownerIdP = dbform->datdba;
2874 *encodingP = dbform->encoding;
2877 *dbIsTemplateP = dbform->datistemplate;
2880 *dbHasLoginEvtP = dbform->dathasloginevt;
2883 *dbAllowConnP = dbform->datallowconn;
2886 *dbFrozenXidP = dbform->datfrozenxid;
2889 *dbMinMultiP = dbform->datminmxid;
2892 *dbTablespace = dbform->dattablespace;
2895 *dbLocProvider = dbform->datlocprovider;
2908 datum =
SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datlocale, &isnull);
2916 datum =
SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_daticurules, &isnull);
2924 datum =
SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datcollversion, &isnull);
2926 *dbCollversion = NULL;
2951 bool result =
false;
2983 Oid *tablespace_ids;
2990 Oid dsttablespace = spcform->oid;
2995 if (dsttablespace == GLOBALTABLESPACE_OID)
3009 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3024 tablespace_ids = (
Oid *)
palloc(ntblspc *
sizeof(
Oid));
3026 foreach(cell, ltblspc)
3033 xlrec.
db_id = db_id;
3045 pfree(tablespace_ids);
3066 bool result =
false;
3076 Oid dsttablespace = spcform->oid;
3081 if (dsttablespace == GLOBALTABLESPACE_OID)
3109 if (notherbackends > 0 && npreparedxacts > 0)
3115 errdetail(
"There are %d other session(s) and %d prepared transaction(s) using the database.",
3116 notherbackends, npreparedxacts);
3117 else if (notherbackends > 0)
3119 "There are %d other sessions using the database.",
3124 "There are %d prepared transactions using the database.",
3151 Anum_pg_database_datname,
3170 (
errcode(ERRCODE_UNDEFINED_DATABASE),
3171 errmsg(
"database \"%s\" does not exist",
3226 elog(
ERROR,
"cache lookup failed for database %u", dboid);
3257 if (
stat(path, &st) == 0)
3261 elog(
PANIC,
"requested to created invalid directory: %s", path);
3265 errmsg(
"missing directory \"%s\"", path));
3268 "creating missing directory: %s", path);
3272 errmsg(
"could not create missing directory \"%s\": %m", path));
3306 if (!
rmtree(dst_path,
true))
3309 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3317 parent_path =
pstrdup(dst_path);
3319 if (
stat(parent_path, &st) < 0)
3321 if (errno != ENOENT)
3323 errmsg(
"could not stat directory \"%s\": %m",
3337 if (
stat(src_path, &st) < 0 && errno == ENOENT)
3354 copydir(src_path, dst_path,
false);
3369 parent_path =
pstrdup(dbpath);
3420 if (!
rmtree(dst_path,
true))
3422 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3440 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)
#define Assert(condition)
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 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)
bool database_is_invalid_form(Form_pg_database datform)
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 bool get_db_info(const char *name, LOCKMODE lockmode, Oid *dbIdP, Oid *ownerIdP, int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP, bool *dbHasLoginEvtP, TransactionId *dbFrozenXidP, MultiXactId *dbMinMultiP, Oid *dbTablespace, char **dbCollate, char **dbCtype, char **dbLocale, char **dbIcurules, char *dbLocProvider, char **dbCollversion)
static void createdb_failure_callback(int code, Datum arg)
bool database_is_invalid_oid(Oid dboid)
#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)
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,...)
bool is_encoding_supported_by_icu(int encoding)
struct dirent * ReadDir(DIR *dir, const char *dirname)
int MakePGDirectory(const char *directoryName)
int CloseTransientFile(int fd)
void fsync_fname(const char *fname, bool isdir)
int data_sync_elevel(int elevel)
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
void heap_inplace_update(Relation relation, HeapTuple tuple)
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
bool HeapTupleSatisfiesVisibility(HeapTuple htup, Snapshot snapshot, Buffer buffer)
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
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)
invalidindex index d is invalid
#define ItemIdGetLength(itemId)
#define ItemIdIsNormal(itemId)
#define ItemIdIsDead(itemId)
#define ItemIdIsUsed(itemId)
#define ItemIdIsRedirected(itemId)
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
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
#define DATCONNLIMIT_INVALID_DB
#define DATCONNLIMIT_UNLIMITED
void DropSetting(Oid databaseid, Oid roleid)
void AlterSetting(Oid databaseid, Oid roleid, VariableSetStmt *setstmt)
static int list_length(const List *l)
void icu_validate_locale(const char *loc_str)
char * get_collation_actual_version(char collprovider, const char *collcollate)
char * icu_language_tag(const char *loc_str, int elevel)
const char * builtin_validate_locale(int encoding, const char *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)
#define pg_encoding_to_char
#define pg_valid_server_encoding
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)
#define INVALID_PROC_NUMBER
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 PG_TBLSPC_DIR_SLASH
#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)
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
void smgrclose(SMgrRelation reln)
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)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
#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)
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)
XLogRecPtr XactLastRecEnd
void XLogFlush(XLogRecPtr record)
#define CHECKPOINT_FLUSH_ALL
#define CHECKPOINT_IMMEDIATE
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
void XLogRegisterData(const char *data, uint32 len)
void XLogBeginInsert(void)
#define XLogRecGetInfo(decoder)
#define XLogRecGetData(decoder)
#define XLogRecHasAnyBlockRefs(decoder)
#define XLR_SPECIAL_REL_UPDATE
void XLogDropDatabase(Oid dbid)