initdb: replace check_icu_locale() with default_icu_locale().
authorJeff Davis <jdavis@postgresql.org>
Tue, 28 Mar 2023 14:55:45 +0000 (07:55 -0700)
committerJeff Davis <jdavis@postgresql.org>
Tue, 28 Mar 2023 15:24:21 +0000 (08:24 -0700)
The extra checks done in check_icu_locale() are not necessary. An
existing comment already pointed out that the checks would be done
during post-bootstrap initialization, when the locale is opened by the
backend. This was a mistake in commit 27b62377b4.

This commit creates a simpler function default_icu_locale() to just
return the locale of the default collator.

Discussion: https://postgr.es/m/04182066-7655-344a-b8b7-040b1b2490fb%40enterprisedb.com
Reviewed-by: Peter Eisentraut
src/bin/initdb/initdb.c

index bae97539fc8c5eb7f9f73188ccba46b63ac637b2..0acfd0fea299200f7476dff39db41fc1ff03a1a0 100644 (file)
@@ -2243,46 +2243,44 @@ check_icu_locale_encoding(int user_enc)
 }
 
 /*
- * Check that ICU accepts the locale name; or if not specified, retrieve the
- * default ICU locale.
+ * Determine default ICU locale by opening the default collator and reading
+ * its locale.
+ *
+ * NB: The default collator (opened using NULL) is different from the collator
+ * for the root locale (opened with "", "und", or "root"). The former depends
+ * on the environment (useful at initdb time) and the latter does not.
  */
-static void
-check_icu_locale(void)
+static char *
+default_icu_locale(void)
 {
 #ifdef USE_ICU
    UCollator   *collator;
    UErrorCode   status;
+   const char  *valid_locale;
+   char        *default_locale;
 
    status = U_ZERO_ERROR;
-   collator = ucol_open(icu_locale, &status);
+   collator = ucol_open(NULL, &status);
    if (U_FAILURE(status))
-   {
-       if (icu_locale)
-           pg_fatal("could not open collator for locale \"%s\": %s",
-                    icu_locale, u_errorName(status));
-       else
-           pg_fatal("could not open collator for default locale: %s",
-                    u_errorName(status));
-   }
+       pg_fatal("could not open collator for default locale: %s",
+                u_errorName(status));
 
-   /* if not specified, get locale from default collator */
-   if (icu_locale == NULL)
+   status = U_ZERO_ERROR;
+   valid_locale = ucol_getLocaleByType(collator, ULOC_VALID_LOCALE,
+                                       &status);
+   if (U_FAILURE(status))
    {
-       const char  *default_locale;
-
-       status = U_ZERO_ERROR;
-       default_locale = ucol_getLocaleByType(collator, ULOC_VALID_LOCALE,
-                                             &status);
-       if (U_FAILURE(status))
-       {
-           ucol_close(collator);
-           pg_fatal("could not determine default ICU locale");
-       }
-
-       icu_locale = pg_strdup(default_locale);
+       ucol_close(collator);
+       pg_fatal("could not determine default ICU locale");
    }
 
+   default_locale = pg_strdup(valid_locale);
+
    ucol_close(collator);
+
+   return default_locale;
+#else
+   pg_fatal("ICU is not supported in this build");
 #endif
 }
 
@@ -2339,7 +2337,9 @@ setlocales(void)
 
    if (locale_provider == COLLPROVIDER_ICU)
    {
-       check_icu_locale();
+       /* acquire default locale from the environment, if not specified */
+       if (icu_locale == NULL)
+           icu_locale = default_icu_locale();
 
        /*
         * In supported builds, the ICU locale ID will be checked by the