66#include "utils/fmgroids.h"
115static void movedb(
const char *
dbname,
const char *tblspcname);
118 Oid *dbIdP,
Oid *ownerIdP,
119 int *encodingP,
bool *dbIsTemplateP,
bool *dbAllowConnP,
bool *dbHasLoginEvtP,
121 Oid *dbTablespace,
char **dbCollate,
char **dbCtype,
char **dbLocale,
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,
331 char *srcpath,
List *rlocatorlist,
377 rlocatorlist =
lappend(rlocatorlist, relinfo);
413 if (classForm->reltablespace == GLOBALTABLESPACE_OID ||
414 !RELKIND_HAS_STORAGE(classForm->relkind) ||
415 classForm->relpersistence == RELPERSISTENCE_TEMP)
423 relfilenumber = classForm->relfilenode;
430 elog(
ERROR,
"relation with OID %u does not have a valid relfilenumber",
442 relinfo->
reloid = classForm->oid;
445 Assert(classForm->relpersistence != RELPERSISTENCE_TEMP);
447 (classForm->relpersistence == RELPERSISTENCE_PERMANENT) ?
true :
false;
470 nbytes = strlen(PG_MAJORVERSION) + 1;
476 if (errno != EEXIST || !isRedo)
479 errmsg(
"could not create directory \"%s\": %m", dbpath)));
487 snprintf(versionfile,
sizeof(versionfile),
"%s/%s", dbpath,
"PG_VERSION");
490 if (
fd < 0 && errno == EEXIST && isRedo)
496 errmsg(
"could not create file \"%s\": %m", versionfile)));
508 errmsg(
"could not write to file \"%s\": %m", versionfile)));
516 errmsg(
"could not fsync file \"%s\": %m", versionfile)));
586 Oid srctablespace = spaceform->oid;
593 if (srctablespace == GLOBALTABLESPACE_OID)
606 if (srctablespace == src_tsid)
607 dsttablespace = dst_tsid;
609 dsttablespace = srctablespace;
624 xlrec.
db_id = dst_dboid;
689 int src_encoding = -1;
690 char *src_collate = NULL;
691 char *src_ctype = NULL;
692 char *src_locale = NULL;
693 char *src_icurules = NULL;
694 char src_locprovider =
'\0';
695 char *src_collversion = NULL;
697 bool src_hasloginevt =
false;
701 Oid src_deftablespace;
702 volatile Oid dst_deftablespace;
705 Datum new_record[Natts_pg_database] = {0};
706 bool new_record_nulls[Natts_pg_database] = {0};
710 DefElem *tablespacenameEl = NULL;
715 DefElem *builtinlocaleEl = NULL;
722 DefElem *allowconnectionsEl = NULL;
727 char *dbowner = NULL;
728 const char *dbtemplate = NULL;
729 char *dbcollate = NULL;
730 char *dbctype = NULL;
731 const char *dblocale = NULL;
732 char *dbicurules = NULL;
733 char dblocprovider =
'\0';
736 bool dbistemplate =
false;
737 bool dballowconnections =
true;
739 char *dbcollversion = NULL;
750 if (strcmp(defel->
defname,
"tablespace") == 0)
752 if (tablespacenameEl)
754 tablespacenameEl = defel;
756 else if (strcmp(defel->
defname,
"owner") == 0)
762 else if (strcmp(defel->
defname,
"template") == 0)
768 else if (strcmp(defel->
defname,
"encoding") == 0)
774 else if (strcmp(defel->
defname,
"locale") == 0)
780 else if (strcmp(defel->
defname,
"builtin_locale") == 0)
784 builtinlocaleEl = defel;
786 else if (strcmp(defel->
defname,
"lc_collate") == 0)
792 else if (strcmp(defel->
defname,
"lc_ctype") == 0)
798 else if (strcmp(defel->
defname,
"icu_locale") == 0)
804 else if (strcmp(defel->
defname,
"icu_rules") == 0)
810 else if (strcmp(defel->
defname,
"locale_provider") == 0)
814 locproviderEl = defel;
816 else if (strcmp(defel->
defname,
"is_template") == 0)
820 istemplateEl = defel;
822 else if (strcmp(defel->
defname,
"allow_connections") == 0)
824 if (allowconnectionsEl)
826 allowconnectionsEl = defel;
828 else if (strcmp(defel->
defname,
"connection_limit") == 0)
834 else if (strcmp(defel->
defname,
"collation_version") == 0)
838 collversionEl = defel;
840 else if (strcmp(defel->
defname,
"location") == 0)
843 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
844 errmsg(
"LOCATION is not supported anymore"),
845 errhint(
"Consider using tablespaces instead."),
848 else if (strcmp(defel->
defname,
"oid") == 0)
869 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
872 else if (strcmp(defel->
defname,
"strategy") == 0)
880 (
errcode(ERRCODE_SYNTAX_ERROR),
885 if (ownerEl && ownerEl->
arg)
887 if (templateEl && templateEl->
arg)
889 if (encodingEl && encodingEl->
arg)
891 const char *encoding_name;
897 if (strcmp(encoding_name,
"") == 0 ||
900 (
errcode(ERRCODE_UNDEFINED_OBJECT),
901 errmsg(
"%d is not a valid encoding code",
911 (
errcode(ERRCODE_UNDEFINED_OBJECT),
912 errmsg(
"%s is not a valid encoding name",
917 if (localeEl && localeEl->
arg)
923 if (builtinlocaleEl && builtinlocaleEl->
arg)
925 if (collateEl && collateEl->
arg)
927 if (ctypeEl && ctypeEl->
arg)
929 if (iculocaleEl && iculocaleEl->
arg)
931 if (icurulesEl && icurulesEl->
arg)
933 if (locproviderEl && locproviderEl->
arg)
938 dblocprovider = COLLPROVIDER_BUILTIN;
940 dblocprovider = COLLPROVIDER_ICU;
942 dblocprovider = COLLPROVIDER_LIBC;
945 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
946 errmsg(
"unrecognized locale provider: %s",
949 if (istemplateEl && istemplateEl->
arg)
951 if (allowconnectionsEl && allowconnectionsEl->
arg)
953 if (connlimitEl && connlimitEl->
arg)
958 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
959 errmsg(
"invalid connection limit: %d", dbconnlimit)));
979 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
980 errmsg(
"permission denied to create database")));
994 dbtemplate =
"template1";
997 &src_dboid, &src_owner, &src_encoding,
998 &src_istemplate, &src_allowconn, &src_hasloginevt,
999 &src_frozenxid, &src_minmxid, &src_deftablespace,
1000 &src_collate, &src_ctype, &src_locale, &src_icurules, &src_locprovider,
1003 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1004 errmsg(
"template database \"%s\" does not exist",
1013 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1014 errmsg(
"cannot use invalid database \"%s\" as template", dbtemplate),
1015 errhint(
"Use DROP DATABASE to drop invalid databases."));
1021 if (!src_istemplate)
1025 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1026 errmsg(
"permission denied to copy database \"%s\"",
1031 if (strategyEl && strategyEl->
arg)
1042 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1043 errmsg(
"invalid create database strategy \"%s\"", strategy),
1044 errhint(
"Valid strategies are \"wal_log\" and \"file_copy\".")));
1050 if (dbcollate == NULL)
1051 dbcollate = src_collate;
1052 if (dbctype == NULL)
1053 dbctype = src_ctype;
1054 if (dblocprovider ==
'\0')
1055 dblocprovider = src_locprovider;
1056 if (dblocale == NULL && dblocprovider == src_locprovider)
1057 dblocale = src_locale;
1058 if (dbicurules == NULL)
1059 dbicurules = src_icurules;
1064 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1070 if (dblocprovider == COLLPROVIDER_BUILTIN)
1072 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1073 errmsg(
"invalid LC_COLLATE locale name: \"%s\"", dbcollate),
1074 errhint(
"If the locale name is specific to the builtin provider, use BUILTIN_LOCALE.")));
1075 else if (dblocprovider == COLLPROVIDER_ICU)
1077 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1078 errmsg(
"invalid LC_COLLATE locale name: \"%s\"", dbcollate),
1079 errhint(
"If the locale name is specific to the ICU provider, use ICU_LOCALE.")));
1082 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1083 errmsg(
"invalid LC_COLLATE locale name: \"%s\"", dbcollate)));
1085 dbcollate = canonname;
1088 if (dblocprovider == COLLPROVIDER_BUILTIN)
1090 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1091 errmsg(
"invalid LC_CTYPE locale name: \"%s\"", dbctype),
1092 errhint(
"If the locale name is specific to the builtin provider, use BUILTIN_LOCALE.")));
1093 else if (dblocprovider == COLLPROVIDER_ICU)
1095 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1096 errmsg(
"invalid LC_CTYPE locale name: \"%s\"", dbctype),
1097 errhint(
"If the locale name is specific to the ICU provider, use ICU_LOCALE.")));
1100 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1101 errmsg(
"invalid LC_CTYPE locale name: \"%s\"", dbctype)));
1104 dbctype = canonname;
1109 if (dblocprovider != COLLPROVIDER_BUILTIN)
1111 if (builtinlocaleEl)
1113 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1114 errmsg(
"BUILTIN_LOCALE cannot be specified unless locale provider is builtin")));
1117 if (dblocprovider != COLLPROVIDER_ICU)
1121 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1122 errmsg(
"ICU locale cannot be specified unless locale provider is ICU")));
1126 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1127 errmsg(
"ICU rules cannot be specified unless locale provider is ICU")));
1131 if (dblocprovider == COLLPROVIDER_BUILTIN)
1139 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1140 errmsg(
"LOCALE or BUILTIN_LOCALE must be specified")));
1144 else if (dblocprovider == COLLPROVIDER_ICU)
1148 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1149 errmsg(
"encoding \"%s\" is not supported with ICU provider",
1158 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1159 errmsg(
"LOCALE or ICU_LOCALE must be specified")));
1171 if (langtag && strcmp(dblocale, langtag) != 0)
1174 (
errmsg(
"using standard form \"%s\" for ICU locale \"%s\"",
1175 langtag, dblocale)));
1185 if (dblocprovider == COLLPROVIDER_LIBC)
1198 if (strcmp(dbtemplate,
"template0") != 0)
1202 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1203 errmsg(
"new encoding (%s) is incompatible with the encoding of the template database (%s)",
1206 errhint(
"Use the same encoding as in the template database, or use template0 as template.")));
1208 if (strcmp(dbcollate, src_collate) != 0)
1210 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1211 errmsg(
"new collation (%s) is incompatible with the collation of the template database (%s)",
1212 dbcollate, src_collate),
1213 errhint(
"Use the same collation as in the template database, or use template0 as template.")));
1215 if (strcmp(dbctype, src_ctype) != 0)
1217 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1218 errmsg(
"new LC_CTYPE (%s) is incompatible with the LC_CTYPE of the template database (%s)",
1219 dbctype, src_ctype),
1220 errhint(
"Use the same LC_CTYPE as in the template database, or use template0 as template.")));
1222 if (dblocprovider != src_locprovider)
1224 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1225 errmsg(
"new locale provider (%s) does not match locale provider of the template database (%s)",
1226 collprovider_name(dblocprovider), collprovider_name(src_locprovider)),
1227 errhint(
"Use the same locale provider as in the template database, or use template0 as template.")));
1229 if (dblocprovider == COLLPROVIDER_ICU)
1236 if (strcmp(dblocale, src_locale) != 0)
1238 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1239 errmsg(
"new ICU locale (%s) is incompatible with the ICU locale of the template database (%s)",
1240 dblocale, src_locale),
1241 errhint(
"Use the same ICU locale as in the template database, or use template0 as template.")));
1246 val2 = src_icurules;
1249 if (strcmp(val1, val2) != 0)
1251 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1252 errmsg(
"new ICU collation rules (%s) are incompatible with the ICU collation rules of the template database (%s)",
1254 errhint(
"Use the same ICU collation rules as in the template database, or use template0 as template.")));
1270 if (src_collversion && !collversionEl)
1272 char *actual_versionstr;
1275 if (dblocprovider == COLLPROVIDER_LIBC)
1281 if (!actual_versionstr)
1283 (
errmsg(
"template database \"%s\" has a collation version, but no actual collation version could be determined",
1286 if (strcmp(actual_versionstr, src_collversion) != 0)
1288 (
errmsg(
"template database \"%s\" has a collation version mismatch",
1290 errdetail(
"The template database was created using collation version %s, "
1291 "but the operating system provides version %s.",
1292 src_collversion, actual_versionstr),
1293 errhint(
"Rebuild all objects in the template database that use the default collation and run "
1294 "ALTER DATABASE %s REFRESH COLLATION VERSION, "
1295 "or build PostgreSQL with the right library version.",
1299 if (dbcollversion == NULL)
1300 dbcollversion = src_collversion;
1307 if (dbcollversion == NULL)
1311 if (dblocprovider == COLLPROVIDER_LIBC)
1320 if (tablespacenameEl && tablespacenameEl->
arg)
1322 char *tablespacename;
1335 if (dst_deftablespace == GLOBALTABLESPACE_OID)
1337 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1338 errmsg(
"pg_global cannot be used as default tablespace")));
1352 if (dst_deftablespace != src_deftablespace)
1359 if (
stat(srcpath, &st) == 0 &&
1363 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1364 errmsg(
"cannot assign new default tablespace \"%s\"",
1366 errdetail(
"There is a conflict because database \"%s\" already has some tables in this tablespace.",
1374 dst_deftablespace = src_deftablespace;
1383#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1385 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1395 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1409 (
errcode(ERRCODE_OBJECT_IN_USE),
1410 errmsg(
"source database \"%s\" is being accessed by other users",
1429 if (existing_dbname != NULL)
1431 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1432 errmsg(
"database OID %u is already in use by database \"%s\"",
1433 dboid, existing_dbname));
1437 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1438 errmsg(
"data directory with the specified OID %u already exists", dboid));
1446 Anum_pg_database_oid);
1456 Assert((dblocprovider != COLLPROVIDER_LIBC && dblocale) ||
1457 (dblocprovider == COLLPROVIDER_LIBC && !dblocale));
1461 new_record[Anum_pg_database_datname - 1] =
1465 new_record[Anum_pg_database_datlocprovider - 1] =
CharGetDatum(dblocprovider);
1466 new_record[Anum_pg_database_datistemplate - 1] =
BoolGetDatum(dbistemplate);
1467 new_record[Anum_pg_database_datallowconn - 1] =
BoolGetDatum(dballowconnections);
1468 new_record[Anum_pg_database_dathasloginevt - 1] =
BoolGetDatum(src_hasloginevt);
1469 new_record[Anum_pg_database_datconnlimit - 1] =
Int32GetDatum(dbconnlimit);
1472 new_record[Anum_pg_database_dattablespace - 1] =
ObjectIdGetDatum(dst_deftablespace);
1478 new_record_nulls[Anum_pg_database_datlocale - 1] =
true;
1482 new_record_nulls[Anum_pg_database_daticurules - 1] =
true;
1486 new_record_nulls[Anum_pg_database_datcollversion - 1] =
true;
1493 new_record_nulls[Anum_pg_database_datacl - 1] =
true;
1496 new_record, new_record_nulls);
1602 if (!(ctype_encoding ==
encoding ||
1604 ctype_encoding == -1 ||
1610 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1611 errmsg(
"encoding \"%s\" does not match locale \"%s\"",
1614 errdetail(
"The chosen LC_CTYPE setting requires encoding \"%s\".",
1617 if (!(collate_encoding ==
encoding ||
1619 collate_encoding == -1 ||
1625 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1626 errmsg(
"encoding \"%s\" does not match locale \"%s\"",
1629 errdetail(
"The chosen LC_COLLATE setting requires encoding \"%s\".",
1681 void *inplace_state;
1699 &db_istemplate, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
1704 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1712 (
errmsg(
"database \"%s\" does not exist, skipping",
1735 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1736 errmsg(
"cannot drop a template database")));
1741 (
errcode(ERRCODE_OBJECT_IN_USE),
1742 errmsg(
"cannot drop the currently open database")));
1754 (
errcode(ERRCODE_OBJECT_IN_USE),
1755 errmsg(
"database \"%s\" is used by an active logical replication slot",
1758 "There are %d active slots.",
1759 nslots_active, nslots_active)));
1770 (
errcode(ERRCODE_OBJECT_IN_USE),
1771 errmsg(
"database \"%s\" is being used by logical replication subscription",
1774 "There are %d subscriptions.",
1775 nsubscriptions, nsubscriptions)));
1793 (
errcode(ERRCODE_OBJECT_IN_USE),
1794 errmsg(
"database \"%s\" is being accessed by other users",
1831 Anum_pg_database_datname,
1835 NULL, 1, &scankey, &tup, &inplace_state);
1837 elog(
ERROR,
"cache lookup failed for database %u", db_id);
1920 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
1922 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1923 errmsg(
"database \"%s\" does not exist", oldname)));
1933 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1934 errmsg(
"permission denied to rename database")));
1940#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1941 if (strstr(newname,
"regression") == NULL)
1942 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1951 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1952 errmsg(
"database \"%s\" already exists", newname)));
1962 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1963 errmsg(
"current database cannot be renamed")));
1973 (
errcode(ERRCODE_OBJECT_IN_USE),
1974 errmsg(
"database \"%s\" is being accessed by other users",
1981 elog(
ERROR,
"cache lookup failed for database %u", db_id);
2032 NULL, NULL, NULL, NULL, &src_tblspcoid, NULL, NULL, NULL, NULL, NULL, NULL))
2034 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2058 (
errcode(ERRCODE_OBJECT_IN_USE),
2059 errmsg(
"cannot change the tablespace of the currently open database")));
2078 if (dst_tblspcoid == GLOBALTABLESPACE_OID)
2080 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2081 errmsg(
"pg_global cannot be used as default tablespace")));
2086 if (src_tblspcoid == dst_tblspcoid)
2102 (
errcode(ERRCODE_OBJECT_IN_USE),
2103 errmsg(
"database \"%s\" is being accessed by other users",
2157 while ((xlde =
ReadDir(dstdir, dst_dbpath)) != NULL)
2159 if (strcmp(xlde->
d_name,
".") == 0 ||
2160 strcmp(xlde->
d_name,
"..") == 0)
2164 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2165 errmsg(
"some relations of database \"%s\" are already in tablespace \"%s\"",
2167 errhint(
"You must move them back to the database's default tablespace before using this command.")));
2176 if (rmdir(dst_dbpath) != 0)
2177 elog(
ERROR,
"could not remove directory \"%s\": %m",
2192 Datum new_record[Natts_pg_database] = {0};
2193 bool new_record_nulls[Natts_pg_database] = {0};
2194 bool new_record_repl[Natts_pg_database] = {0};
2199 copydir(src_dbpath, dst_dbpath,
false);
2207 xlrec.
db_id = db_id;
2224 Anum_pg_database_datname,
2232 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2236 new_record[Anum_pg_database_dattablespace - 1] =
ObjectIdGetDatum(dst_tblspcoid);
2237 new_record_repl[Anum_pg_database_dattablespace - 1] =
true;
2241 new_record_nulls, new_record_repl);
2294 if (!
rmtree(src_dbpath,
true))
2296 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
2305 xlrec.
db_id = db_id;
2348 foreach(lc,
stmt->options)
2352 if (strcmp(opt->
defname,
"force") == 0)
2356 (
errcode(ERRCODE_SYNTAX_ERROR),
2357 errmsg(
"unrecognized DROP DATABASE option \"%s\"", opt->
defname),
2378 bool dbistemplate =
false;
2379 bool dballowconnections =
true;
2382 DefElem *dallowconnections = NULL;
2385 Datum new_record[Natts_pg_database] = {0};
2386 bool new_record_nulls[Natts_pg_database] = {0};
2387 bool new_record_repl[Natts_pg_database] = {0};
2394 if (strcmp(defel->
defname,
"is_template") == 0)
2398 distemplate = defel;
2400 else if (strcmp(defel->
defname,
"allow_connections") == 0)
2402 if (dallowconnections)
2404 dallowconnections = defel;
2406 else if (strcmp(defel->
defname,
"connection_limit") == 0)
2412 else if (strcmp(defel->
defname,
"tablespace") == 0)
2416 dtablespace = defel;
2420 (
errcode(ERRCODE_SYNTAX_ERROR),
2434 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2435 errmsg(
"option \"%s\" cannot be specified with other options",
2444 if (distemplate && distemplate->
arg)
2446 if (dallowconnections && dallowconnections->
arg)
2448 if (dconnlimit && dconnlimit->
arg)
2453 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2454 errmsg(
"invalid connection limit: %d", dbconnlimit)));
2464 Anum_pg_database_datname,
2472 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2473 errmsg(
"database \"%s\" does not exist",
stmt->dbname)));
2477 dboid = datform->oid;
2482 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2483 errmsg(
"cannot alter invalid database \"%s\"",
stmt->dbname),
2484 errhint(
"Use DROP DATABASE to drop invalid databases."));
2499 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2500 errmsg(
"cannot disallow connections for current database")));
2507 new_record[Anum_pg_database_datistemplate - 1] =
BoolGetDatum(dbistemplate);
2508 new_record_repl[Anum_pg_database_datistemplate - 1] =
true;
2510 if (dallowconnections)
2512 new_record[Anum_pg_database_datallowconn - 1] =
BoolGetDatum(dballowconnections);
2513 new_record_repl[Anum_pg_database_datallowconn - 1] =
true;
2517 new_record[Anum_pg_database_datconnlimit - 1] =
Int32GetDatum(dbconnlimit);
2518 new_record_repl[Anum_pg_database_datconnlimit - 1] =
true;
2522 new_record_nulls, new_record_repl);
2557 Anum_pg_database_datname,
2565 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2566 errmsg(
"database \"%s\" does not exist",
stmt->dbname)));
2569 db_id = datForm->oid;
2579 if (datForm->datlocprovider == COLLPROVIDER_LIBC)
2583 elog(
ERROR,
"unexpected null in pg_database");
2589 elog(
ERROR,
"unexpected null in pg_database");
2596 if ((!oldversion && newversion) || (oldversion && !newversion))
2597 elog(
ERROR,
"invalid collation version change");
2598 else if (oldversion && newversion && strcmp(newversion, oldversion) != 0)
2600 bool nulls[Natts_pg_database] = {0};
2601 bool replaces[Natts_pg_database] = {0};
2606 (
errmsg(
"changing version from %s to %s",
2607 oldversion, newversion)));
2610 replaces[Anum_pg_database_datcollversion - 1] =
true;
2613 values, nulls, replaces);
2619 (
errmsg(
"version has not changed")));
2681 Anum_pg_database_datname,
2689 (
errcode(ERRCODE_UNDEFINED_DATABASE),
2693 db_id = datForm->oid;
2700 if (datForm->datdba != newOwnerId)
2702 Datum repl_val[Natts_pg_database];
2703 bool repl_null[Natts_pg_database] = {0};
2704 bool repl_repl[Natts_pg_database] = {0};
2729 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2730 errmsg(
"permission denied to change owner of database")));
2734 repl_repl[Anum_pg_database_datdba - 1] =
true;
2742 Anum_pg_database_datacl,
2748 datForm->datdba, newOwnerId);
2749 repl_repl[Anum_pg_database_datacl - 1] =
true;
2788 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2789 errmsg(
"database with OID %u does not exist", dbid)));
2822 Oid *dbIdP,
Oid *ownerIdP,
2823 int *encodingP,
bool *dbIsTemplateP,
bool *dbAllowConnP,
bool *dbHasLoginEvtP,
2825 Oid *dbTablespace,
char **dbCollate,
char **dbCtype,
char **dbLocale,
2827 char *dbLocProvider,
2828 char **dbCollversion)
2830 bool result =
false;
2855 Anum_pg_database_datname,
2901 *ownerIdP = dbform->datdba;
2904 *encodingP = dbform->encoding;
2907 *dbIsTemplateP = dbform->datistemplate;
2910 *dbHasLoginEvtP = dbform->dathasloginevt;
2913 *dbAllowConnP = dbform->datallowconn;
2916 *dbFrozenXidP = dbform->datfrozenxid;
2919 *dbMinMultiP = dbform->datminmxid;
2922 *dbTablespace = dbform->dattablespace;
2925 *dbLocProvider = dbform->datlocprovider;
2938 datum =
SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datlocale, &isnull);
2946 datum =
SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_daticurules, &isnull);
2954 datum =
SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datcollversion, &isnull);
2956 *dbCollversion = NULL;
2981 bool result =
false;
3013 Oid *tablespace_ids;
3020 Oid dsttablespace = spcform->oid;
3025 if (dsttablespace == GLOBALTABLESPACE_OID)
3039 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3054 tablespace_ids = (
Oid *)
palloc(ntblspc *
sizeof(
Oid));
3056 foreach(cell, ltblspc)
3063 xlrec.
db_id = db_id;
3075 pfree(tablespace_ids);
3096 bool result =
false;
3106 Oid dsttablespace = spcform->oid;
3111 if (dsttablespace == GLOBALTABLESPACE_OID)
3139 if (notherbackends > 0 && npreparedxacts > 0)
3145 errdetail(
"There are %d other session(s) and %d prepared transaction(s) using the database.",
3146 notherbackends, npreparedxacts);
3147 else if (notherbackends > 0)
3149 "There are %d other sessions using the database.",
3154 "There are %d prepared transactions using the database.",
3181 Anum_pg_database_datname,
3200 (
errcode(ERRCODE_UNDEFINED_DATABASE),
3201 errmsg(
"database \"%s\" does not exist",
3232 elog(
ERROR,
"cache lookup failed for database %u", dboid);
3263 if (
stat(path, &st) == 0)
3267 elog(
PANIC,
"requested to created invalid directory: %s", path);
3271 errmsg(
"missing directory \"%s\"", path));
3274 "creating missing directory: %s", path);
3278 errmsg(
"could not create missing directory \"%s\": %m", path));
3312 if (!
rmtree(dst_path,
true))
3315 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3323 parent_path =
pstrdup(dst_path);
3325 if (
stat(parent_path, &st) < 0)
3327 if (errno != ENOENT)
3329 errmsg(
"could not stat directory \"%s\": %m",
3343 if (
stat(src_path, &st) < 0 && errno == ENOENT)
3360 copydir(src_path, dst_path,
false);
3375 parent_path =
pstrdup(dbpath);
3427 if (!
rmtree(dst_path,
true))
3429 (
errmsg(
"some useless files may be left behind in old database directory \"%s\"",
3447 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(const PageData *page)
static void * PageGetItem(const PageData *page, const ItemIdData *itemId)
static bool PageIsNew(const PageData *page)
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
static OffsetNumber PageGetMaxOffsetNumber(const PageData *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)
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)
char * defGetString(DefElem *def)
bool defGetBoolean(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)
int MakePGDirectory(const char *directoryName)
int CloseTransientFile(int fd)
void fsync_fname(const char *fname, bool isdir)
int data_sync_elevel(int elevel)
DIR * AllocateDir(const char *dirname)
struct dirent * ReadDir(DIR *dir, const char *dirname)
int OpenTransientFile(const char *fileName, int fileFlags)
static char dstpath[MAXPGPATH]
#define DirectFunctionCall1(func, arg1)
#define PG_RETURN_TEXT_P(x)
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
void systable_endscan(SysScanDesc sysscan)
void systable_inplace_update_begin(Relation relation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, const ScanKeyData *key, HeapTuple *oldtupcopy, void **state)
void systable_inplace_update_finish(void *state, HeapTuple tuple)
HeapTuple systable_getnext(SysScanDesc sysscan)
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
bool allowSystemTableMods
Assert(PointerIsAligned(start, uint64))
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)
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *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 UnlockTuple(Relation relation, const ItemPointerData *tid, LOCKMODE lockmode)
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 LockTuple(Relation relation, const ItemPointerData *tid, 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
#define InplaceUpdateTupleLock
char * get_database_name(Oid dbid)
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 UnregisterSnapshot(Snapshot snapshot)
Snapshot RegisterSnapshot(Snapshot snapshot)
void PopActiveSnapshot(void)
void ResolveRecoveryConflictWithDatabase(Oid dbid)
#define BTEqualStrategyNumber
CreateDBStrategy strategy
Oid tablespace_ids[FLEXIBLE_ARRAY_MEMBER]
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCacheLockedCopy1(int cacheId, Datum key1)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, 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_UNLOGGED
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
void XLogRegisterData(const void *data, uint32 len)
void XLogBeginInsert(void)
#define XLogRecGetInfo(decoder)
#define XLogRecGetData(decoder)
#define XLogRecHasAnyBlockRefs(decoder)
#define XLR_SPECIAL_REL_UPDATE
void XLogDropDatabase(Oid dbid)