PostgreSQL Source Code: src/backend/commands/dbcommands.c File Reference (original) (raw)
685{
686 Oid src_dboid;
687 Oid src_owner;
688 int src_encoding = -1;
689 char *src_collate = NULL;
690 char *src_ctype = NULL;
691 char *src_locale = NULL;
692 char *src_icurules = NULL;
693 char src_locprovider = '\0';
694 char *src_collversion = NULL;
695 bool src_istemplate;
696 bool src_hasloginevt = false;
697 bool src_allowconn;
700 Oid src_deftablespace;
701 volatile Oid dst_deftablespace;
704 Datum new_record[Natts_pg_database] = {0};
705 bool new_record_nulls[Natts_pg_database] = {0};
707 Oid datdba;
709 DefElem *tablespacenameEl = NULL;
711 DefElem *templateEl = NULL;
712 DefElem *encodingEl = NULL;
713 DefElem *localeEl = NULL;
714 DefElem *builtinlocaleEl = NULL;
715 DefElem *collateEl = NULL;
717 DefElem *iculocaleEl = NULL;
718 DefElem *icurulesEl = NULL;
719 DefElem *locproviderEl = NULL;
720 DefElem *istemplateEl = NULL;
721 DefElem *allowconnectionsEl = NULL;
722 DefElem *connlimitEl = NULL;
723 DefElem *collversionEl = NULL;
724 DefElem *strategyEl = NULL;
726 char *dbowner = NULL;
727 const char *dbtemplate = NULL;
728 char *dbcollate = NULL;
729 char *dbctype = NULL;
730 const char *dblocale = NULL;
731 char *dbicurules = NULL;
732 char dblocprovider = '\0';
733 char *canonname;
735 bool dbistemplate = false;
736 bool dballowconnections = true;
738 char *dbcollversion = NULL;
739 int notherbackends;
740 int npreparedxacts;
743
744
746 {
748
749 if (strcmp(defel->defname, "tablespace") == 0)
750 {
751 if (tablespacenameEl)
753 tablespacenameEl = defel;
754 }
755 else if (strcmp(defel->defname, "owner") == 0)
756 {
757 if (ownerEl)
759 ownerEl = defel;
760 }
761 else if (strcmp(defel->defname, "template") == 0)
762 {
763 if (templateEl)
765 templateEl = defel;
766 }
767 else if (strcmp(defel->defname, "encoding") == 0)
768 {
769 if (encodingEl)
771 encodingEl = defel;
772 }
773 else if (strcmp(defel->defname, "locale") == 0)
774 {
775 if (localeEl)
777 localeEl = defel;
778 }
779 else if (strcmp(defel->defname, "builtin_locale") == 0)
780 {
781 if (builtinlocaleEl)
783 builtinlocaleEl = defel;
784 }
785 else if (strcmp(defel->defname, "lc_collate") == 0)
786 {
787 if (collateEl)
789 collateEl = defel;
790 }
791 else if (strcmp(defel->defname, "lc_ctype") == 0)
792 {
793 if (ctypeEl)
795 ctypeEl = defel;
796 }
797 else if (strcmp(defel->defname, "icu_locale") == 0)
798 {
799 if (iculocaleEl)
801 iculocaleEl = defel;
802 }
803 else if (strcmp(defel->defname, "icu_rules") == 0)
804 {
805 if (icurulesEl)
807 icurulesEl = defel;
808 }
809 else if (strcmp(defel->defname, "locale_provider") == 0)
810 {
811 if (locproviderEl)
813 locproviderEl = defel;
814 }
815 else if (strcmp(defel->defname, "is_template") == 0)
816 {
817 if (istemplateEl)
819 istemplateEl = defel;
820 }
821 else if (strcmp(defel->defname, "allow_connections") == 0)
822 {
823 if (allowconnectionsEl)
825 allowconnectionsEl = defel;
826 }
827 else if (strcmp(defel->defname, "connection_limit") == 0)
828 {
829 if (connlimitEl)
831 connlimitEl = defel;
832 }
833 else if (strcmp(defel->defname, "collation_version") == 0)
834 {
835 if (collversionEl)
837 collversionEl = defel;
838 }
839 else if (strcmp(defel->defname, "location") == 0)
840 {
842 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
843 errmsg("LOCATION is not supported anymore"),
844 errhint("Consider using tablespaces instead."),
846 }
847 else if (strcmp(defel->defname, "oid") == 0)
848 {
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
868 (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
870 }
871 else if (strcmp(defel->defname, "strategy") == 0)
872 {
873 if (strategyEl)
875 strategyEl = defel;
876 }
877 else
879 (errcode(ERRCODE_SYNTAX_ERROR),
880 errmsg("option \"%s\" not recognized", defel->defname),
882 }
883
884 if (ownerEl && ownerEl->arg)
886 if (templateEl && templateEl->arg)
888 if (encodingEl && encodingEl->arg)
889 {
890 const char *encoding_name;
891
893 {
896 if (strcmp(encoding_name, "") == 0 ||
899 (errcode(ERRCODE_UNDEFINED_OBJECT),
900 errmsg("%d is not a valid encoding code",
903 }
904 else
905 {
910 (errcode(ERRCODE_UNDEFINED_OBJECT),
911 errmsg("%s is not a valid encoding name",
912 encoding_name),
914 }
915 }
916 if (localeEl && localeEl->arg)
917 {
921 }
922 if (builtinlocaleEl && builtinlocaleEl->arg)
924 if (collateEl && collateEl->arg)
926 if (ctypeEl && ctypeEl->arg)
928 if (iculocaleEl && iculocaleEl->arg)
930 if (icurulesEl && icurulesEl->arg)
932 if (locproviderEl && locproviderEl->arg)
933 {
934 char *locproviderstr = defGetString(locproviderEl);
935
936 if (pg_strcasecmp(locproviderstr, "builtin") == 0)
937 dblocprovider = COLLPROVIDER_BUILTIN;
938 else if (pg_strcasecmp(locproviderstr, "icu") == 0)
939 dblocprovider = COLLPROVIDER_ICU;
940 else if (pg_strcasecmp(locproviderstr, "libc") == 0)
941 dblocprovider = COLLPROVIDER_LIBC;
942 else
944 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
945 errmsg("unrecognized locale provider: %s",
946 locproviderstr)));
947 }
948 if (istemplateEl && istemplateEl->arg)
950 if (allowconnectionsEl && allowconnectionsEl->arg)
951 dballowconnections = defGetBoolean(allowconnectionsEl);
952 if (connlimitEl && connlimitEl->arg)
953 {
957 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
958 errmsg("invalid connection limit: %d", dbconnlimit)));
959 }
960 if (collversionEl)
962
963
964 if (dbowner)
966 else
968
969
970
971
972
973
974
975
978 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
979 errmsg("permission denied to create database")));
980
982
983
984
985
986
987
988
989
990
991
992 if (!dbtemplate)
993 dbtemplate = "template1";
994
996 &src_dboid, &src_owner, &src_encoding,
997 &src_istemplate, &src_allowconn, &src_hasloginevt,
998 &src_frozenxid, &src_minmxid, &src_deftablespace,
999 &src_collate, &src_ctype, &src_locale, &src_icurules, &src_locprovider,
1000 &src_collversion))
1002 (errcode(ERRCODE_UNDEFINED_DATABASE),
1003 errmsg("template database \"%s\" does not exist",
1004 dbtemplate)));
1005
1006
1007
1008
1009
1012 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1013 errmsg("cannot use invalid database \"%s\" as template", dbtemplate),
1014 errhint("Use DROP DATABASE to drop invalid databases."));
1015
1016
1017
1018
1019
1020 if (!src_istemplate)
1021 {
1024 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1025 errmsg("permission denied to copy database \"%s\"",
1026 dbtemplate)));
1027 }
1028
1029
1030 if (strategyEl && strategyEl->arg)
1031 {
1032 char *strategy;
1033
1037 else if (pg_strcasecmp(strategy, "file_copy") == 0)
1039 else
1041 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1042 errmsg("invalid create database strategy \"%s\"", strategy),
1043 errhint("Valid strategies are \"wal_log\" and \"file_copy\".")));
1044 }
1045
1046
1049 if (dbcollate == NULL)
1050 dbcollate = src_collate;
1051 if (dbctype == NULL)
1052 dbctype = src_ctype;
1053 if (dblocprovider == '\0')
1054 dblocprovider = src_locprovider;
1055 if (dblocale == NULL)
1056 dblocale = src_locale;
1057 if (dbicurules == NULL)
1058 dbicurules = src_icurules;
1059
1060
1063 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1065
1066
1067 if ((LC_COLLATE, dbcollate, &canonname))
1069 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1070 errmsg("invalid LC_COLLATE locale name: \"%s\"", dbcollate),
1071 errhint("If the locale name is specific to ICU, use ICU_LOCALE.")));
1072 dbcollate = canonname;
1073 if ((LC_CTYPE, dbctype, &canonname))
1075 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1076 errmsg("invalid LC_CTYPE locale name: \"%s\"", dbctype),
1077 errhint("If the locale name is specific to ICU, use ICU_LOCALE.")));
1078 dbctype = canonname;
1079
1081
1082
1083 if (dblocprovider != COLLPROVIDER_BUILTIN)
1084 {
1085 if (builtinlocaleEl)
1087 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1088 errmsg("BUILTIN_LOCALE cannot be specified unless locale provider is builtin")));
1089 }
1090
1091 if (dblocprovider != COLLPROVIDER_ICU)
1092 {
1093 if (iculocaleEl)
1095 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1096 errmsg("ICU locale cannot be specified unless locale provider is ICU")));
1097
1098 if (dbicurules)
1100 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1101 errmsg("ICU rules cannot be specified unless locale provider is ICU")));
1102 }
1103
1104
1105 if (dblocprovider == COLLPROVIDER_BUILTIN)
1106 {
1107
1108
1109
1110
1111 if (!dblocale)
1113 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1114 errmsg("LOCALE or BUILTIN_LOCALE must be specified")));
1115
1117 }
1118 else if (dblocprovider == COLLPROVIDER_ICU)
1119 {
1122 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1123 errmsg("encoding \"%s\" is not supported with ICU provider",
1125
1126
1127
1128
1129
1130 if (!dblocale)
1132 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1133 errmsg("LOCALE or ICU_LOCALE must be specified")));
1134
1135
1136
1137
1138
1139
1141 {
1144
1145 if (langtag && strcmp(dblocale, langtag) != 0)
1146 {
1148 (errmsg("using standard form \"%s\" for ICU locale \"%s\"",
1149 langtag, dblocale)));
1150
1151 dblocale = langtag;
1152 }
1153 }
1154
1156 }
1157
1158
1159 if (dblocprovider == COLLPROVIDER_LIBC)
1160 dblocale = NULL;
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172 if (strcmp(dbtemplate, "template0") != 0)
1173 {
1174 if (encoding != src_encoding)
1176 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1177 errmsg("new encoding (%s) is incompatible with the encoding of the template database (%s)",
1180 errhint("Use the same encoding as in the template database, or use template0 as template.")));
1181
1182 if (strcmp(dbcollate, src_collate) != 0)
1184 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1185 errmsg("new collation (%s) is incompatible with the collation of the template database (%s)",
1186 dbcollate, src_collate),
1187 errhint("Use the same collation as in the template database, or use template0 as template.")));
1188
1189 if (strcmp(dbctype, src_ctype) != 0)
1191 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1192 errmsg("new LC_CTYPE (%s) is incompatible with the LC_CTYPE of the template database (%s)",
1193 dbctype, src_ctype),
1194 errhint("Use the same LC_CTYPE as in the template database, or use template0 as template.")));
1195
1196 if (dblocprovider != src_locprovider)
1198 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1199 errmsg("new locale provider (%s) does not match locale provider of the template database (%s)",
1200 collprovider_name(dblocprovider), collprovider_name(src_locprovider)),
1201 errhint("Use the same locale provider as in the template database, or use template0 as template.")));
1202
1203 if (dblocprovider == COLLPROVIDER_ICU)
1204 {
1205 char *val1;
1206 char *val2;
1207
1210 if (strcmp(dblocale, src_locale) != 0)
1212 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1213 errmsg("new ICU locale (%s) is incompatible with the ICU locale of the template database (%s)",
1214 dblocale, src_locale),
1215 errhint("Use the same ICU locale as in the template database, or use template0 as template.")));
1216
1217 val1 = dbicurules;
1218 if (!val1)
1219 val1 = "";
1220 val2 = src_icurules;
1221 if (!val2)
1222 val2 = "";
1223 if (strcmp(val1, val2) != 0)
1225 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1226 errmsg("new ICU collation rules (%s) are incompatible with the ICU collation rules of the template database (%s)",
1227 val1, val2),
1228 errhint("Use the same ICU collation rules as in the template database, or use template0 as template.")));
1229 }
1230 }
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244 if (src_collversion && !collversionEl)
1245 {
1246 char *actual_versionstr;
1248
1249 if (dblocprovider == COLLPROVIDER_LIBC)
1251 else
1253
1255 if (!actual_versionstr)
1257 (errmsg("template database \"%s\" has a collation version, but no actual collation version could be determined",
1258 dbtemplate)));
1259
1260 if (strcmp(actual_versionstr, src_collversion) != 0)
1262 (errmsg("template database \"%s\" has a collation version mismatch",
1263 dbtemplate),
1264 errdetail("The template database was created using collation version %s, "
1265 "but the operating system provides version %s.",
1266 src_collversion, actual_versionstr),
1267 errhint("Rebuild all objects in the template database that use the default collation and run "
1268 "ALTER DATABASE %s REFRESH COLLATION VERSION, "
1269 "or build PostgreSQL with the right library version.",
1271 }
1272
1273 if (dbcollversion == NULL)
1274 dbcollversion = src_collversion;
1275
1276
1277
1278
1279
1280
1281 if (dbcollversion == NULL)
1282 {
1284
1285 if (dblocprovider == COLLPROVIDER_LIBC)
1287 else
1289
1291 }
1292
1293
1294 if (tablespacenameEl && tablespacenameEl->arg)
1295 {
1296 char *tablespacename;
1298
1299 tablespacename = defGetString(tablespacenameEl);
1301
1306 tablespacename);
1307
1308
1309 if (dst_deftablespace == GLOBALTABLESPACE_OID)
1311 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1312 errmsg("pg_global cannot be used as default tablespace")));
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326 if (dst_deftablespace != src_deftablespace)
1327 {
1328 char *srcpath;
1329 struct stat st;
1330
1331 srcpath = GetDatabasePath(src_dboid, dst_deftablespace);
1332
1333 if (stat(srcpath, &st) == 0 &&
1337 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1338 errmsg("cannot assign new default tablespace \"%s\"",
1339 tablespacename),
1340 errdetail("There is a conflict because database \"%s\" already has some tables in this tablespace.",
1341 dbtemplate)));
1343 }
1344 }
1345 else
1346 {
1347
1348 dst_deftablespace = src_deftablespace;
1349
1350 }
1351
1352
1353
1354
1355
1356
1357#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1359 elog(WARNING, "databases created by regression test cases should have names including \"regression\"");
1360#endif
1361
1362
1363
1364
1365
1366
1369 (errcode(ERRCODE_DUPLICATE_DATABASE),
1370 errmsg("database \"%s\" already exists", dbname)));
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1383 (errcode(ERRCODE_OBJECT_IN_USE),
1384 errmsg("source database \"%s\" is being accessed by other users",
1385 dbtemplate),
1387
1388
1389
1390
1391
1392
1394
1395
1396
1397
1398
1400 {
1402
1403 if (existing_dbname != NULL)
1405 (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1406 errmsg("database OID %u is already in use by database \"%s\"",
1407 dboid, existing_dbname));
1408
1411 (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
1412 errmsg("data directory with the specified OID %u already exists", dboid));
1413 }
1414 else
1415 {
1416
1417 do
1418 {
1420 Anum_pg_database_oid);
1422 }
1423
1424
1425
1426
1427
1428
1429
1430 Assert((dblocprovider != COLLPROVIDER_LIBC && dblocale) ||
1431 (dblocprovider == COLLPROVIDER_LIBC && !dblocale));
1432
1433
1434 new_record[Anum_pg_database_oid - 1] = ObjectIdGetDatum(dboid);
1435 new_record[Anum_pg_database_datname - 1] =
1437 new_record[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(datdba);
1439 new_record[Anum_pg_database_datlocprovider - 1] = CharGetDatum(dblocprovider);
1440 new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(dbistemplate);
1441 new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(dballowconnections);
1442 new_record[Anum_pg_database_dathasloginevt - 1] = BoolGetDatum(src_hasloginevt);
1443 new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit);
1444 new_record[Anum_pg_database_datfrozenxid - 1] = TransactionIdGetDatum(src_frozenxid);
1445 new_record[Anum_pg_database_datminmxid - 1] = TransactionIdGetDatum(src_minmxid);
1446 new_record[Anum_pg_database_dattablespace - 1] = ObjectIdGetDatum(dst_deftablespace);
1447 new_record[Anum_pg_database_datcollate - 1] = CStringGetTextDatum(dbcollate);
1448 new_record[Anum_pg_database_datctype - 1] = CStringGetTextDatum(dbctype);
1449 if (dblocale)
1450 new_record[Anum_pg_database_datlocale - 1] = CStringGetTextDatum(dblocale);
1451 else
1452 new_record_nulls[Anum_pg_database_datlocale - 1] = true;
1453 if (dbicurules)
1454 new_record[Anum_pg_database_daticurules - 1] = CStringGetTextDatum(dbicurules);
1455 else
1456 new_record_nulls[Anum_pg_database_daticurules - 1] = true;
1457 if (dbcollversion)
1458 new_record[Anum_pg_database_datcollversion - 1] = CStringGetTextDatum(dbcollversion);
1459 else
1460 new_record_nulls[Anum_pg_database_datcollversion - 1] = true;
1461
1462
1463
1464
1465
1466
1467 new_record_nulls[Anum_pg_database_datacl - 1] = true;
1468
1470 new_record, new_record_nulls);
1471
1473
1474
1475
1476
1477
1478
1480
1481
1483
1484
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1500
1501
1502
1503
1504
1505
1506
1507
1510 fparms.strategy = dbstrategy;
1511
1514 {
1515
1516
1517
1518
1519
1520
1521
1524 dst_deftablespace);
1525 else
1527 dst_deftablespace);
1528
1529
1530
1531
1533
1534
1535
1536
1537
1538
1539
1541 }
1544
1545 return dboid;
1546}
Oid get_role_oid(const char *rolname, bool missing_ok)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
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)
char * get_database_name(Oid dbid)
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
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)
#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)
const char * quote_identifier(const char *ident)
CreateDBStrategy strategy
#define InvalidTransactionId
#define FirstNormalObjectId
void ForceSyncCommit(void)