686{
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;
696 bool src_istemplate;
697 bool src_hasloginevt = false;
698 bool src_allowconn;
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';
734 char *canonname;
736 bool dbistemplate = false;
737 bool dballowconnections = true;
739 char *dbcollversion = NULL;
740 int notherbackends;
741 int npreparedxacts;
744
745
747 {
749
750 if (strcmp(defel->
defname,
"tablespace") == 0)
751 {
752 if (tablespacenameEl)
754 tablespacenameEl = defel;
755 }
756 else if (strcmp(defel->
defname,
"owner") == 0)
757 {
758 if (ownerEl)
760 ownerEl = defel;
761 }
762 else if (strcmp(defel->
defname,
"template") == 0)
763 {
764 if (templateEl)
766 templateEl = defel;
767 }
768 else if (strcmp(defel->
defname,
"encoding") == 0)
769 {
770 if (encodingEl)
772 encodingEl = defel;
773 }
774 else if (strcmp(defel->
defname,
"locale") == 0)
775 {
776 if (localeEl)
778 localeEl = defel;
779 }
780 else if (strcmp(defel->
defname,
"builtin_locale") == 0)
781 {
782 if (builtinlocaleEl)
784 builtinlocaleEl = defel;
785 }
786 else if (strcmp(defel->
defname,
"lc_collate") == 0)
787 {
788 if (collateEl)
790 collateEl = defel;
791 }
792 else if (strcmp(defel->
defname,
"lc_ctype") == 0)
793 {
794 if (ctypeEl)
796 ctypeEl = defel;
797 }
798 else if (strcmp(defel->
defname,
"icu_locale") == 0)
799 {
800 if (iculocaleEl)
802 iculocaleEl = defel;
803 }
804 else if (strcmp(defel->
defname,
"icu_rules") == 0)
805 {
806 if (icurulesEl)
808 icurulesEl = defel;
809 }
810 else if (strcmp(defel->
defname,
"locale_provider") == 0)
811 {
812 if (locproviderEl)
814 locproviderEl = defel;
815 }
816 else if (strcmp(defel->
defname,
"is_template") == 0)
817 {
818 if (istemplateEl)
820 istemplateEl = defel;
821 }
822 else if (strcmp(defel->
defname,
"allow_connections") == 0)
823 {
824 if (allowconnectionsEl)
826 allowconnectionsEl = defel;
827 }
828 else if (strcmp(defel->
defname,
"connection_limit") == 0)
829 {
830 if (connlimitEl)
832 connlimitEl = defel;
833 }
834 else if (strcmp(defel->
defname,
"collation_version") == 0)
835 {
836 if (collversionEl)
838 collversionEl = defel;
839 }
840 else if (strcmp(defel->
defname,
"location") == 0)
841 {
843 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
844 errmsg(
"LOCATION is not supported anymore"),
845 errhint(
"Consider using tablespaces instead."),
847 }
848 else if (strcmp(defel->
defname,
"oid") == 0)
849 {
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
869 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
871 }
872 else if (strcmp(defel->
defname,
"strategy") == 0)
873 {
874 if (strategyEl)
876 strategyEl = defel;
877 }
878 else
880 (
errcode(ERRCODE_SYNTAX_ERROR),
883 }
884
885 if (ownerEl && ownerEl->
arg)
887 if (templateEl && templateEl->
arg)
889 if (encodingEl && encodingEl->
arg)
890 {
891 const char *encoding_name;
892
894 {
897 if (strcmp(encoding_name, "") == 0 ||
900 (
errcode(ERRCODE_UNDEFINED_OBJECT),
901 errmsg(
"%d is not a valid encoding code",
904 }
905 else
906 {
911 (
errcode(ERRCODE_UNDEFINED_OBJECT),
912 errmsg(
"%s is not a valid encoding name",
913 encoding_name),
915 }
916 }
917 if (localeEl && localeEl->
arg)
918 {
922 }
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)
934 {
936
938 dblocprovider = COLLPROVIDER_BUILTIN;
940 dblocprovider = COLLPROVIDER_ICU;
942 dblocprovider = COLLPROVIDER_LIBC;
943 else
945 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
946 errmsg(
"unrecognized locale provider: %s",
947 locproviderstr)));
948 }
949 if (istemplateEl && istemplateEl->
arg)
951 if (allowconnectionsEl && allowconnectionsEl->
arg)
953 if (connlimitEl && connlimitEl->
arg)
954 {
958 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
959 errmsg(
"invalid connection limit: %d", dbconnlimit)));
960 }
961 if (collversionEl)
963
964
965 if (dbowner)
967 else
969
970
971
972
973
974
975
976
979 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
980 errmsg(
"permission denied to create database")));
981
983
984
985
986
987
988
989
990
991
992
993 if (!dbtemplate)
994 dbtemplate = "template1";
995
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,
1001 &src_collversion))
1003 (
errcode(ERRCODE_UNDEFINED_DATABASE),
1004 errmsg(
"template database \"%s\" does not exist",
1005 dbtemplate)));
1006
1007
1008
1009
1010
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."));
1016
1017
1018
1019
1020
1021 if (!src_istemplate)
1022 {
1025 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1026 errmsg(
"permission denied to copy database \"%s\"",
1027 dbtemplate)));
1028 }
1029
1030
1031 if (strategyEl && strategyEl->
arg)
1032 {
1033 char *strategy;
1034
1040 else
1042 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1043 errmsg(
"invalid create database strategy \"%s\"", strategy),
1044 errhint(
"Valid strategies are \"wal_log\" and \"file_copy\".")));
1045 }
1046
1047
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;
1060
1061
1064 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1066
1067
1069 {
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.")));
1080 else
1082 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1083 errmsg(
"invalid LC_COLLATE locale name: \"%s\"", dbcollate)));
1084 }
1085 dbcollate = canonname;
1087 {
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.")));
1098 else
1100 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1101 errmsg(
"invalid LC_CTYPE locale name: \"%s\"", dbctype)));
1102 }
1103
1104 dbctype = canonname;
1105
1107
1108
1109 if (dblocprovider != COLLPROVIDER_BUILTIN)
1110 {
1111 if (builtinlocaleEl)
1113 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1114 errmsg(
"BUILTIN_LOCALE cannot be specified unless locale provider is builtin")));
1115 }
1116
1117 if (dblocprovider != COLLPROVIDER_ICU)
1118 {
1119 if (iculocaleEl)
1121 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1122 errmsg(
"ICU locale cannot be specified unless locale provider is ICU")));
1123
1124 if (dbicurules)
1126 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1127 errmsg(
"ICU rules cannot be specified unless locale provider is ICU")));
1128 }
1129
1130
1131 if (dblocprovider == COLLPROVIDER_BUILTIN)
1132 {
1133
1134
1135
1136
1137 if (!dblocale)
1139 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1140 errmsg(
"LOCALE or BUILTIN_LOCALE must be specified")));
1141
1143 }
1144 else if (dblocprovider == COLLPROVIDER_ICU)
1145 {
1148 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1149 errmsg(
"encoding \"%s\" is not supported with ICU provider",
1151
1152
1153
1154
1155
1156 if (!dblocale)
1158 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1159 errmsg(
"LOCALE or ICU_LOCALE must be specified")));
1160
1161
1162
1163
1164
1165
1167 {
1170
1171 if (langtag && strcmp(dblocale, langtag) != 0)
1172 {
1174 (
errmsg(
"using standard form \"%s\" for ICU locale \"%s\"",
1175 langtag, dblocale)));
1176
1177 dblocale = langtag;
1178 }
1179 }
1180
1182 }
1183
1184
1185 if (dblocprovider == COLLPROVIDER_LIBC)
1186 dblocale = NULL;
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198 if (strcmp(dbtemplate, "template0") != 0)
1199 {
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.")));
1207
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.")));
1214
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.")));
1221
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.")));
1228
1229 if (dblocprovider == COLLPROVIDER_ICU)
1230 {
1231 char *val1;
1232 char *val2;
1233
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.")));
1242
1243 val1 = dbicurules;
1244 if (!val1)
1245 val1 = "";
1246 val2 = src_icurules;
1247 if (!val2)
1248 val2 = "";
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)",
1253 val1, val2),
1254 errhint(
"Use the same ICU collation rules as in the template database, or use template0 as template.")));
1255 }
1256 }
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 if (src_collversion && !collversionEl)
1271 {
1272 char *actual_versionstr;
1274
1275 if (dblocprovider == COLLPROVIDER_LIBC)
1277 else
1279
1281 if (!actual_versionstr)
1283 (
errmsg(
"template database \"%s\" has a collation version, but no actual collation version could be determined",
1284 dbtemplate)));
1285
1286 if (strcmp(actual_versionstr, src_collversion) != 0)
1288 (
errmsg(
"template database \"%s\" has a collation version mismatch",
1289 dbtemplate),
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.",
1297 }
1298
1299 if (dbcollversion == NULL)
1300 dbcollversion = src_collversion;
1301
1302
1303
1304
1305
1306
1307 if (dbcollversion == NULL)
1308 {
1310
1311 if (dblocprovider == COLLPROVIDER_LIBC)
1313 else
1315
1317 }
1318
1319
1320 if (tablespacenameEl && tablespacenameEl->
arg)
1321 {
1322 char *tablespacename;
1324
1327
1332 tablespacename);
1333
1334
1335 if (dst_deftablespace == GLOBALTABLESPACE_OID)
1337 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1338 errmsg(
"pg_global cannot be used as default tablespace")));
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352 if (dst_deftablespace != src_deftablespace)
1353 {
1354 char *srcpath;
1356
1358
1359 if (
stat(srcpath, &st) == 0 &&
1363 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1364 errmsg(
"cannot assign new default tablespace \"%s\"",
1365 tablespacename),
1366 errdetail(
"There is a conflict because database \"%s\" already has some tables in this tablespace.",
1367 dbtemplate)));
1369 }
1370 }
1371 else
1372 {
1373
1374 dst_deftablespace = src_deftablespace;
1375
1376 }
1377
1378
1379
1380
1381
1382
1383#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1385 elog(
WARNING,
"databases created by regression test cases should have names including \"regression\"");
1386#endif
1387
1388
1389
1390
1391
1392
1395 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1409 (
errcode(ERRCODE_OBJECT_IN_USE),
1410 errmsg(
"source database \"%s\" is being accessed by other users",
1411 dbtemplate),
1413
1414
1415
1416
1417
1418
1420
1421
1422
1423
1424
1426 {
1428
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));
1434
1437 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1438 errmsg(
"data directory with the specified OID %u already exists", dboid));
1439 }
1440 else
1441 {
1442
1443 do
1444 {
1446 Anum_pg_database_oid);
1448 }
1449
1450
1451
1452
1453
1454
1455
1456 Assert((dblocprovider != COLLPROVIDER_LIBC && dblocale) ||
1457 (dblocprovider == COLLPROVIDER_LIBC && !dblocale));
1458
1459
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);
1475 if (dblocale)
1477 else
1478 new_record_nulls[Anum_pg_database_datlocale - 1] = true;
1479 if (dbicurules)
1481 else
1482 new_record_nulls[Anum_pg_database_daticurules - 1] = true;
1483 if (dbcollversion)
1485 else
1486 new_record_nulls[Anum_pg_database_datcollversion - 1] = true;
1487
1488
1489
1490
1491
1492
1493 new_record_nulls[Anum_pg_database_datacl - 1] = true;
1494
1496 new_record, new_record_nulls);
1497
1499
1500
1501
1502
1503
1504
1506
1507
1509
1510
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1526
1527
1528
1529
1530
1531
1532
1533
1537
1540 {
1541
1542
1543
1544
1545
1546
1547
1550 dst_deftablespace);
1551 else
1553 dst_deftablespace);
1554
1555
1556
1557
1559
1560
1561
1562
1563
1564
1565
1567 }
1570
1571 return dboid;
1572}
Oid get_role_oid(const char *rolname, bool missing_ok)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
bool directory_is_empty(const char *path)
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
TransactionId MultiXactId
#define OidIsValid(objectId)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
static void CreateDatabaseUsingWalLog(Oid src_dboid, Oid dst_dboid, Oid src_tsid, Oid dst_tsid)
void check_encoding_locale_matches(int encoding, const char *collate, const char *ctype)
static int errdetail_busy_db(int notherbackends, int npreparedxacts)
static bool check_db_file_conflict(Oid db_id)
static void CreateDatabaseUsingFileCopy(Oid src_dboid, Oid dst_dboid, Oid src_tsid, Oid dst_tsid)
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)
Oid defGetObjectId(DefElem *def)
bool is_encoding_supported_by_icu(int encoding)
#define DirectFunctionCall1(func, arg1)
bool allowSystemTableMods
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
char * get_database_name(Oid dbid)
void pfree(void *pointer)
#define InvalidMultiXactId
Datum namein(PG_FUNCTION_ARGS)
#define IsA(nodeptr, _type_)
#define InvokeObjectPostCreateHook(classId, objectId, subId)
void icu_validate_locale(const char *loc_str)
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 copyTemplateDependencies(Oid templateDbId, Oid newDbId)
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
#define PG_VALID_BE_ENCODING(_enc)
#define pg_valid_server_encoding
int pg_strcasecmp(const char *s1, const char *s2)
static Datum TransactionIdGetDatum(TransactionId X)
static Datum CharGetDatum(char X)
bool CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
char * GetDatabasePath(Oid dbOid, Oid spcOid)
const char * quote_identifier(const char *ident)
CreateDBStrategy strategy
#define InvalidTransactionId
#define FirstNormalObjectId
void ForceSyncCommit(void)