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 /* Extract options from the statement node tree */
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 * We don't normally permit new databases to be created with
854 * system-assigned OIDs. pg_upgrade tries to preserve database
855 * OIDs, so we can't allow any database to be created with an OID
856 * that might be in use in a freshly-initialized cluster created
857 * by some future version. We assume all such OIDs will be from
858 * the system-managed OID range.
859 *
860 * As an exception, however, we permit any OID to be assigned when
861 * allow_system_table_mods=on (so that initdb can assign system
862 * OIDs to template0 and postgres) or when performing a binary
863 * upgrade (so that pg_upgrade can preserve whatever OIDs it finds
864 * in the source cluster).
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 /* obtain OID of proposed owner */
965 if (dbowner)
967 else
969
970 /*
971 * To create a database, must have createdb privilege and must be able to
972 * become the target role (this does not imply that the target role itself
973 * must have createdb privilege). The latter provision guards against
974 * "giveaway" attacks. Note that a superuser will always have both of
975 * these privileges a fortiori.
976 */
979 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
980 errmsg(
"permission denied to create database")));
981
983
984 /*
985 * Lookup database (template) to be cloned, and obtain share lock on it.
986 * ShareLock allows two CREATE DATABASEs to work from the same template
987 * concurrently, while ensuring no one is busy dropping it in parallel
988 * (which would be Very Bad since we'd likely get an incomplete copy
989 * without knowing it). This also prevents any new connections from being
990 * made to the source until we finish copying it, so we can be sure it
991 * won't change underneath us.
992 */
993 if (!dbtemplate)
994 dbtemplate = "template1"; /* Default template database name */
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 * If the source database was in the process of being dropped, we can't
1009 * use it as a template.
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 * Permission check: to copy a DB that's not marked datistemplate, you
1019 * must be superuser or the owner thereof.
1020 */
1021 if (!src_istemplate)
1022 {
1025 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1026 errmsg(
"permission denied to copy database \"%s\"",
1027 dbtemplate)));
1028 }
1029
1030 /* Validate the database creation strategy. */
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 /* If encoding or locales are defaulted, use source's setting */
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 /* Some encodings are client only */
1064 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1066
1067 /* Check that the chosen locales are valid, and get canonical spellings */
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 /* validate provider-specific parameters */
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 /* validate and canonicalize locale for the provider */
1131 if (dblocprovider == COLLPROVIDER_BUILTIN)
1132 {
1133 /*
1134 * This would happen if template0 uses the libc provider but the new
1135 * database uses builtin.
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 * This would happen if template0 uses the libc provider but the new
1154 * database uses icu.
1155 */
1156 if (!dblocale)
1158 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1159 errmsg(
"LOCALE or ICU_LOCALE must be specified")));
1160
1161 /*
1162 * During binary upgrade, or when the locale came from the template
1163 * database, preserve locale string. Otherwise, canonicalize to a
1164 * language tag.
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 /* for libc, locale comes from datcollate and datctype */
1185 if (dblocprovider == COLLPROVIDER_LIBC)
1186 dblocale = NULL;
1187
1188 /*
1189 * Check that the new encoding and locale settings match the source
1190 * database. We insist on this because we simply copy the source data ---
1191 * any non-ASCII data would be wrongly encoded, and any indexes sorted
1192 * according to the source locale would be wrong.
1193 *
1194 * However, we assume that template0 doesn't contain any non-ASCII data
1195 * nor any indexes that depend on collation or ctype, so template0 can be
1196 * used as template for creating a database with any encoding or locale.
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 * If we got a collation version for the template database, check that it
1260 * matches the actual OS collation version. Otherwise error; the user
1261 * needs to fix the template database first. Don't complain if a
1262 * collation version was specified explicitly as a statement option; that
1263 * is used by pg_upgrade to reproduce the old state exactly.
1264 *
1265 * (If the template database has no collation version, then either the
1266 * platform/provider does not support collation versioning, or it's
1267 * template0, for which we stipulate that it does not contain
1268 * collation-using objects.)
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 * Normally, we copy the collation version from the template database.
1304 * This last resort only applies if the template database does not have a
1305 * collation version, which is normally only the case for template0.
1306 */
1307 if (dbcollversion == NULL)
1308 {
1310
1311 if (dblocprovider == COLLPROVIDER_LIBC)
1313 else
1315
1317 }
1318
1319 /* Resolve default tablespace for new database */
1320 if (tablespacenameEl && tablespacenameEl->
arg)
1321 {
1322 char *tablespacename;
1324
1327 /* check permissions */
1332 tablespacename);
1333
1334 /* pg_global must never be the default tablespace */
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 * If we are trying to change the default tablespace of the template,
1342 * we require that the template not have any files in the new default
1343 * tablespace. This is necessary because otherwise the copied
1344 * database would contain pg_class rows that refer to its default
1345 * tablespace both explicitly (by OID) and implicitly (as zero), which
1346 * would cause problems. For example another CREATE DATABASE using
1347 * the copied database as template, and trying to change its default
1348 * tablespace again, would yield outright incorrect results (it would
1349 * improperly move tables to the new default tablespace that should
1350 * stay in the same tablespace).
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 /* Use template database's default tablespace */
1374 dst_deftablespace = src_deftablespace;
1375 /* Note there is no additional permission check in this path */
1376 }
1377
1378 /*
1379 * If built with appropriate switch, whine when regression-testing
1380 * conventions for database names are violated. But don't complain during
1381 * initdb.
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 * Check for db name conflict. This is just to give a more friendly error
1390 * message than "unique index violation". There's a race condition but
1391 * we're willing to accept the less friendly message in that case.
1392 */
1395 (
errcode(ERRCODE_DUPLICATE_DATABASE),
1397
1398 /*
1399 * The source DB can't have any active backends, except this one
1400 * (exception is to allow CREATE DB while connected to template1).
1401 * Otherwise we might copy inconsistent data.
1402 *
1403 * This should be last among the basic error checks, because it involves
1404 * potential waiting; we may as well throw an error first if we're gonna
1405 * throw one.
1406 */
1409 (
errcode(ERRCODE_OBJECT_IN_USE),
1410 errmsg(
"source database \"%s\" is being accessed by other users",
1411 dbtemplate),
1413
1414 /*
1415 * Select an OID for the new database, checking that it doesn't have a
1416 * filename conflict with anything already existing in the tablespace
1417 * directories.
1418 */
1420
1421 /*
1422 * If database OID is configured, check if the OID is already in use or
1423 * data directory already exists.
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 /* Select an OID for the new database if is not explicitly configured. */
1443 do
1444 {
1446 Anum_pg_database_oid);
1448 }
1449
1450 /*
1451 * Insert a new tuple into pg_database. This establishes our ownership of
1452 * the new database name (anyone else trying to insert the same name will
1453 * block on the unique index, and fail after we commit).
1454 */
1455
1456 Assert((dblocprovider != COLLPROVIDER_LIBC && dblocale) ||
1457 (dblocprovider == COLLPROVIDER_LIBC && !dblocale));
1458
1459 /* Form tuple */
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 * We deliberately set datacl to default (NULL), rather than copying it
1490 * from the template database. Copying it would be a bad idea when the
1491 * owner is not the same as the template's owner.
1492 */
1493 new_record_nulls[Anum_pg_database_datacl - 1] = true;
1494
1496 new_record, new_record_nulls);
1497
1499
1500 /*
1501 * Now generate additional catalog entries associated with the new DB
1502 */
1503
1504 /* Register owner dependency */
1506
1507 /* Create pg_shdepend entries for objects within database */
1509
1510 /* Post creation hook for new database */
1512
1513 /*
1514 * If we're going to be reading data for the to-be-created database into
1515 * shared_buffers, take a lock on it. Nobody should know that this
1516 * database exists yet, but it's good to maintain the invariant that an
1517 * AccessExclusiveLock on the database is sufficient to drop all of its
1518 * buffers without worrying about more being read later.
1519 *
1520 * Note that we need to do this before entering the
1521 * PG_ENSURE_ERROR_CLEANUP block below, because createdb_failure_callback
1522 * expects this lock to be held already.
1523 */
1526
1527 /*
1528 * Once we start copying subdirectories, we need to be able to clean 'em
1529 * up if we fail. Use an ENSURE block to make sure this happens. (This
1530 * is not a 100% solution, because of the possibility of failure during
1531 * transaction commit after we leave this routine, but it should handle
1532 * most scenarios.)
1533 */
1537
1540 {
1541 /*
1542 * If the user has asked to create a database with WAL_LOG strategy
1543 * then call CreateDatabaseUsingWalLog, which will copy the database
1544 * at the block level and it will WAL log each copied block.
1545 * Otherwise, call CreateDatabaseUsingFileCopy that will copy the
1546 * database file by file.
1547 */
1550 dst_deftablespace);
1551 else
1553 dst_deftablespace);
1554
1555 /*
1556 * Close pg_database, but keep lock till commit.
1557 */
1559
1560 /*
1561 * Force synchronous commit, thus minimizing the window between
1562 * creation of the database files and committal of the transaction. If
1563 * we crash before committing, we'll have a DB that's taking up disk
1564 * space but is not in pg_database, which is not good.
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)