LCOV - code coverage report
Current view: top level - src/backend/utils/adt - pg_locale.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 353 422 83.6 %
Date: 2025-04-01 14:15:22 Functions: 35 36 97.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-----------------------------------------------------------------------
       2             :  *
       3             :  * PostgreSQL locale utilities
       4             :  *
       5             :  * Portions Copyright (c) 2002-2025, PostgreSQL Global Development Group
       6             :  *
       7             :  * src/backend/utils/adt/pg_locale.c
       8             :  *
       9             :  *-----------------------------------------------------------------------
      10             :  */
      11             : 
      12             : /*----------
      13             :  * Here is how the locale stuff is handled: LC_COLLATE and LC_CTYPE
      14             :  * are fixed at CREATE DATABASE time, stored in pg_database, and cannot
      15             :  * be changed. Thus, the effects of strcoll(), strxfrm(), isupper(),
      16             :  * toupper(), etc. are always in the same fixed locale.
      17             :  *
      18             :  * LC_MESSAGES is settable at run time and will take effect
      19             :  * immediately.
      20             :  *
      21             :  * The other categories, LC_MONETARY, LC_NUMERIC, and LC_TIME are
      22             :  * permanentaly set to "C", and then we use temporary locale_t
      23             :  * objects when we need to look up locale data based on the GUCs
      24             :  * of the same name.  Information is cached when the GUCs change.
      25             :  * The cached information is only used by the formatting functions
      26             :  * (to_char, etc.) and the money type.  For the user, this should all be
      27             :  * transparent.
      28             :  *----------
      29             :  */
      30             : 
      31             : 
      32             : #include "postgres.h"
      33             : 
      34             : #include <time.h>
      35             : 
      36             : #include "access/htup_details.h"
      37             : #include "catalog/pg_collation.h"
      38             : #include "catalog/pg_database.h"
      39             : #include "common/hashfn.h"
      40             : #include "common/string.h"
      41             : #include "mb/pg_wchar.h"
      42             : #include "miscadmin.h"
      43             : #include "utils/builtins.h"
      44             : #include "utils/formatting.h"
      45             : #include "utils/guc_hooks.h"
      46             : #include "utils/lsyscache.h"
      47             : #include "utils/memutils.h"
      48             : #include "utils/pg_locale.h"
      49             : #include "utils/syscache.h"
      50             : 
      51             : #ifdef WIN32
      52             : #include <shlwapi.h>
      53             : #endif
      54             : 
      55             : /* Error triggered for locale-sensitive subroutines */
      56             : #define     PGLOCALE_SUPPORT_ERROR(provider) \
      57             :     elog(ERROR, "unsupported collprovider for %s: %c", __func__, provider)
      58             : 
      59             : /*
      60             :  * This should be large enough that most strings will fit, but small enough
      61             :  * that we feel comfortable putting it on the stack
      62             :  */
      63             : #define     TEXTBUFLEN          1024
      64             : 
      65             : #define     MAX_L10N_DATA       80
      66             : 
      67             : /* pg_locale_builtin.c */
      68             : extern pg_locale_t create_pg_locale_builtin(Oid collid, MemoryContext context);
      69             : extern char *get_collation_actual_version_builtin(const char *collcollate);
      70             : 
      71             : /* pg_locale_icu.c */
      72             : #ifdef USE_ICU
      73             : extern UCollator *pg_ucol_open(const char *loc_str);
      74             : extern char *get_collation_actual_version_icu(const char *collcollate);
      75             : #endif
      76             : extern pg_locale_t create_pg_locale_icu(Oid collid, MemoryContext context);
      77             : 
      78             : /* pg_locale_libc.c */
      79             : extern pg_locale_t create_pg_locale_libc(Oid collid, MemoryContext context);
      80             : extern char *get_collation_actual_version_libc(const char *collcollate);
      81             : 
      82             : extern size_t strlower_builtin(char *dst, size_t dstsize, const char *src,
      83             :                                ssize_t srclen, pg_locale_t locale);
      84             : extern size_t strtitle_builtin(char *dst, size_t dstsize, const char *src,
      85             :                                ssize_t srclen, pg_locale_t locale);
      86             : extern size_t strupper_builtin(char *dst, size_t dstsize, const char *src,
      87             :                                ssize_t srclen, pg_locale_t locale);
      88             : extern size_t strfold_builtin(char *dst, size_t dstsize, const char *src,
      89             :                               ssize_t srclen, pg_locale_t locale);
      90             : 
      91             : extern size_t strlower_icu(char *dst, size_t dstsize, const char *src,
      92             :                            ssize_t srclen, pg_locale_t locale);
      93             : extern size_t strtitle_icu(char *dst, size_t dstsize, const char *src,
      94             :                            ssize_t srclen, pg_locale_t locale);
      95             : extern size_t strupper_icu(char *dst, size_t dstsize, const char *src,
      96             :                            ssize_t srclen, pg_locale_t locale);
      97             : extern size_t strfold_icu(char *dst, size_t dstsize, const char *src,
      98             :                           ssize_t srclen, pg_locale_t locale);
      99             : 
     100             : extern size_t strlower_libc(char *dst, size_t dstsize, const char *src,
     101             :                             ssize_t srclen, pg_locale_t locale);
     102             : extern size_t strtitle_libc(char *dst, size_t dstsize, const char *src,
     103             :                             ssize_t srclen, pg_locale_t locale);
     104             : extern size_t strupper_libc(char *dst, size_t dstsize, const char *src,
     105             :                             ssize_t srclen, pg_locale_t locale);
     106             : 
     107             : /* GUC settings */
     108             : char       *locale_messages;
     109             : char       *locale_monetary;
     110             : char       *locale_numeric;
     111             : char       *locale_time;
     112             : 
     113             : int         icu_validation_level = WARNING;
     114             : 
     115             : /*
     116             :  * lc_time localization cache.
     117             :  *
     118             :  * We use only the first 7 or 12 entries of these arrays.  The last array
     119             :  * element is left as NULL for the convenience of outside code that wants
     120             :  * to sequentially scan these arrays.
     121             :  */
     122             : char       *localized_abbrev_days[7 + 1];
     123             : char       *localized_full_days[7 + 1];
     124             : char       *localized_abbrev_months[12 + 1];
     125             : char       *localized_full_months[12 + 1];
     126             : 
     127             : /* is the databases's LC_CTYPE the C locale? */
     128             : bool        database_ctype_is_c = false;
     129             : 
     130             : static pg_locale_t default_locale = NULL;
     131             : 
     132             : /* indicates whether locale information cache is valid */
     133             : static bool CurrentLocaleConvValid = false;
     134             : static bool CurrentLCTimeValid = false;
     135             : 
     136             : /* Cache for collation-related knowledge */
     137             : 
     138             : typedef struct
     139             : {
     140             :     Oid         collid;         /* hash key: pg_collation OID */
     141             :     pg_locale_t locale;         /* locale_t struct, or 0 if not valid */
     142             : 
     143             :     /* needed for simplehash */
     144             :     uint32      hash;
     145             :     char        status;
     146             : } collation_cache_entry;
     147             : 
     148             : #define SH_PREFIX       collation_cache
     149             : #define SH_ELEMENT_TYPE collation_cache_entry
     150             : #define SH_KEY_TYPE     Oid
     151             : #define SH_KEY          collid
     152             : #define SH_HASH_KEY(tb, key)    murmurhash32((uint32) key)
     153             : #define SH_EQUAL(tb, a, b)      (a == b)
     154             : #define SH_GET_HASH(tb, a)      a->hash
     155             : #define SH_SCOPE        static inline
     156             : #define SH_STORE_HASH
     157             : #define SH_DECLARE
     158             : #define SH_DEFINE
     159             : #include "lib/simplehash.h"
     160             : 
     161             : static MemoryContext CollationCacheContext = NULL;
     162             : static collation_cache_hash *CollationCache = NULL;
     163             : 
     164             : /*
     165             :  * The collation cache is often accessed repeatedly for the same collation, so
     166             :  * remember the last one used.
     167             :  */
     168             : static Oid  last_collation_cache_oid = InvalidOid;
     169             : static pg_locale_t last_collation_cache_locale = NULL;
     170             : 
     171             : #if defined(WIN32) && defined(LC_MESSAGES)
     172             : static char *IsoLocaleName(const char *);
     173             : #endif
     174             : 
     175             : /*
     176             :  * pg_perm_setlocale
     177             :  *
     178             :  * This wraps the libc function setlocale(), with two additions.  First, when
     179             :  * changing LC_CTYPE, update gettext's encoding for the current message
     180             :  * domain.  GNU gettext automatically tracks LC_CTYPE on most platforms, but
     181             :  * not on Windows.  Second, if the operation is successful, the corresponding
     182             :  * LC_XXX environment variable is set to match.  By setting the environment
     183             :  * variable, we ensure that any subsequent use of setlocale(..., "") will
     184             :  * preserve the settings made through this routine.  Of course, LC_ALL must
     185             :  * also be unset to fully ensure that, but that has to be done elsewhere after
     186             :  * all the individual LC_XXX variables have been set correctly.  (Thank you
     187             :  * Perl for making this kluge necessary.)
     188             :  */
     189             : char *
     190      102122 : pg_perm_setlocale(int category, const char *locale)
     191             : {
     192             :     char       *result;
     193             :     const char *envvar;
     194             : 
     195             : #ifndef WIN32
     196      102122 :     result = setlocale(category, locale);
     197             : #else
     198             : 
     199             :     /*
     200             :      * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
     201             :      * the given value is good and set it in the environment variables. We
     202             :      * must ignore attempts to set to "", which means "keep using the old
     203             :      * environment value".
     204             :      */
     205             : #ifdef LC_MESSAGES
     206             :     if (category == LC_MESSAGES)
     207             :     {
     208             :         result = (char *) locale;
     209             :         if (locale == NULL || locale[0] == '\0')
     210             :             return result;
     211             :     }
     212             :     else
     213             : #endif
     214             :         result = setlocale(category, locale);
     215             : #endif                          /* WIN32 */
     216             : 
     217      102122 :     if (result == NULL)
     218           0 :         return result;          /* fall out immediately on failure */
     219             : 
     220             :     /*
     221             :      * Use the right encoding in translated messages.  Under ENABLE_NLS, let
     222             :      * pg_bind_textdomain_codeset() figure it out.  Under !ENABLE_NLS, message
     223             :      * format strings are ASCII, but database-encoding strings may enter the
     224             :      * message via %s.  This makes the overall message encoding equal to the
     225             :      * database encoding.
     226             :      */
     227      102122 :     if (category == LC_CTYPE)
     228             :     {
     229             :         static char save_lc_ctype[LOCALE_NAME_BUFLEN];
     230             : 
     231             :         /* copy setlocale() return value before callee invokes it again */
     232       35456 :         strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
     233       35456 :         result = save_lc_ctype;
     234             : 
     235             : #ifdef ENABLE_NLS
     236       35456 :         SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
     237             : #else
     238             :         SetMessageEncoding(GetDatabaseEncoding());
     239             : #endif
     240             :     }
     241             : 
     242      102122 :     switch (category)
     243             :     {
     244       35456 :         case LC_COLLATE:
     245       35456 :             envvar = "LC_COLLATE";
     246       35456 :             break;
     247       35456 :         case LC_CTYPE:
     248       35456 :             envvar = "LC_CTYPE";
     249       35456 :             break;
     250             : #ifdef LC_MESSAGES
     251       20554 :         case LC_MESSAGES:
     252       20554 :             envvar = "LC_MESSAGES";
     253             : #ifdef WIN32
     254             :             result = IsoLocaleName(locale);
     255             :             if (result == NULL)
     256             :                 result = (char *) locale;
     257             :             elog(DEBUG3, "IsoLocaleName() executed; locale: \"%s\"", result);
     258             : #endif                          /* WIN32 */
     259       20554 :             break;
     260             : #endif                          /* LC_MESSAGES */
     261        3552 :         case LC_MONETARY:
     262        3552 :             envvar = "LC_MONETARY";
     263        3552 :             break;
     264        3552 :         case LC_NUMERIC:
     265        3552 :             envvar = "LC_NUMERIC";
     266        3552 :             break;
     267        3552 :         case LC_TIME:
     268        3552 :             envvar = "LC_TIME";
     269        3552 :             break;
     270           0 :         default:
     271           0 :             elog(FATAL, "unrecognized LC category: %d", category);
     272             :             return NULL;        /* keep compiler quiet */
     273             :     }
     274             : 
     275      102122 :     if (setenv(envvar, result, 1) != 0)
     276           0 :         return NULL;
     277             : 
     278      102118 :     return result;
     279             : }
     280             : 
     281             : 
     282             : /*
     283             :  * Is the locale name valid for the locale category?
     284             :  *
     285             :  * If successful, and canonname isn't NULL, a palloc'd copy of the locale's
     286             :  * canonical name is stored there.  This is especially useful for figuring out
     287             :  * what locale name "" means (ie, the server environment value).  (Actually,
     288             :  * it seems that on most implementations that's the only thing it's good for;
     289             :  * we could wish that setlocale gave back a canonically spelled version of
     290             :  * the locale name, but typically it doesn't.)
     291             :  */
     292             : bool
     293       65334 : check_locale(int category, const char *locale, char **canonname)
     294             : {
     295             :     char       *save;
     296             :     char       *res;
     297             : 
     298             :     /* Don't let Windows' non-ASCII locale names in. */
     299       65334 :     if (!pg_is_ascii(locale))
     300             :     {
     301           0 :         ereport(WARNING,
     302             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     303             :                  errmsg("locale name \"%s\" contains non-ASCII characters",
     304             :                         locale)));
     305           0 :         return false;
     306             :     }
     307             : 
     308       65334 :     if (canonname)
     309        1454 :         *canonname = NULL;      /* in case of failure */
     310             : 
     311       65334 :     save = setlocale(category, NULL);
     312       65334 :     if (!save)
     313           0 :         return false;           /* won't happen, we hope */
     314             : 
     315             :     /* save may be pointing at a modifiable scratch variable, see above. */
     316       65334 :     save = pstrdup(save);
     317             : 
     318             :     /* set the locale with setlocale, to see if it accepts it. */
     319       65334 :     res = setlocale(category, locale);
     320             : 
     321             :     /* save canonical name if requested. */
     322       65334 :     if (res && canonname)
     323        1450 :         *canonname = pstrdup(res);
     324             : 
     325             :     /* restore old value. */
     326       65334 :     if (!setlocale(category, save))
     327           0 :         elog(WARNING, "failed to restore old locale \"%s\"", save);
     328       65334 :     pfree(save);
     329             : 
     330             :     /* Don't let Windows' non-ASCII locale names out. */
     331       65334 :     if (canonname && *canonname && !pg_is_ascii(*canonname))
     332             :     {
     333           0 :         ereport(WARNING,
     334             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     335             :                  errmsg("locale name \"%s\" contains non-ASCII characters",
     336             :                         *canonname)));
     337           0 :         pfree(*canonname);
     338           0 :         *canonname = NULL;
     339           0 :         return false;
     340             :     }
     341             : 
     342       65334 :     return (res != NULL);
     343             : }
     344             : 
     345             : 
     346             : /*
     347             :  * GUC check/assign hooks
     348             :  *
     349             :  * For most locale categories, the assign hook doesn't actually set the locale
     350             :  * permanently, just reset flags so that the next use will cache the
     351             :  * appropriate values.  (See explanation at the top of this file.)
     352             :  *
     353             :  * Note: we accept value = "" as selecting the postmaster's environment
     354             :  * value, whatever it was (so long as the environment setting is legal).
     355             :  * This will have been locked down by an earlier call to pg_perm_setlocale.
     356             :  */
     357             : bool
     358       17172 : check_locale_monetary(char **newval, void **extra, GucSource source)
     359             : {
     360       17172 :     return check_locale(LC_MONETARY, *newval, NULL);
     361             : }
     362             : 
     363             : void
     364       16974 : assign_locale_monetary(const char *newval, void *extra)
     365             : {
     366       16974 :     CurrentLocaleConvValid = false;
     367       16974 : }
     368             : 
     369             : bool
     370       17178 : check_locale_numeric(char **newval, void **extra, GucSource source)
     371             : {
     372       17178 :     return check_locale(LC_NUMERIC, *newval, NULL);
     373             : }
     374             : 
     375             : void
     376       16986 : assign_locale_numeric(const char *newval, void *extra)
     377             : {
     378       16986 :     CurrentLocaleConvValid = false;
     379       16986 : }
     380             : 
     381             : bool
     382       17172 : check_locale_time(char **newval, void **extra, GucSource source)
     383             : {
     384       17172 :     return check_locale(LC_TIME, *newval, NULL);
     385             : }
     386             : 
     387             : void
     388       16974 : assign_locale_time(const char *newval, void *extra)
     389             : {
     390       16974 :     CurrentLCTimeValid = false;
     391       16974 : }
     392             : 
     393             : /*
     394             :  * We allow LC_MESSAGES to actually be set globally.
     395             :  *
     396             :  * Note: we normally disallow value = "" because it wouldn't have consistent
     397             :  * semantics (it'd effectively just use the previous value).  However, this
     398             :  * is the value passed for PGC_S_DEFAULT, so don't complain in that case,
     399             :  * not even if the attempted setting fails due to invalid environment value.
     400             :  * The idea there is just to accept the environment setting *if possible*
     401             :  * during startup, until we can read the proper value from postgresql.conf.
     402             :  */
     403             : bool
     404       17202 : check_locale_messages(char **newval, void **extra, GucSource source)
     405             : {
     406       17202 :     if (**newval == '\0')
     407             :     {
     408        4844 :         if (source == PGC_S_DEFAULT)
     409        4844 :             return true;
     410             :         else
     411           0 :             return false;
     412             :     }
     413             : 
     414             :     /*
     415             :      * LC_MESSAGES category does not exist everywhere, but accept it anyway
     416             :      *
     417             :      * On Windows, we can't even check the value, so accept blindly
     418             :      */
     419             : #if defined(LC_MESSAGES) && !defined(WIN32)
     420       12358 :     return check_locale(LC_MESSAGES, *newval, NULL);
     421             : #else
     422             :     return true;
     423             : #endif
     424             : }
     425             : 
     426             : void
     427       17002 : assign_locale_messages(const char *newval, void *extra)
     428             : {
     429             :     /*
     430             :      * LC_MESSAGES category does not exist everywhere, but accept it anyway.
     431             :      * We ignore failure, as per comment above.
     432             :      */
     433             : #ifdef LC_MESSAGES
     434       17002 :     (void) pg_perm_setlocale(LC_MESSAGES, newval);
     435             : #endif
     436       17002 : }
     437             : 
     438             : 
     439             : /*
     440             :  * Frees the malloced content of a struct lconv.  (But not the struct
     441             :  * itself.)  It's important that this not throw elog(ERROR).
     442             :  */
     443             : static void
     444           6 : free_struct_lconv(struct lconv *s)
     445             : {
     446           6 :     free(s->decimal_point);
     447           6 :     free(s->thousands_sep);
     448           6 :     free(s->grouping);
     449           6 :     free(s->int_curr_symbol);
     450           6 :     free(s->currency_symbol);
     451           6 :     free(s->mon_decimal_point);
     452           6 :     free(s->mon_thousands_sep);
     453           6 :     free(s->mon_grouping);
     454           6 :     free(s->positive_sign);
     455           6 :     free(s->negative_sign);
     456           6 : }
     457             : 
     458             : /*
     459             :  * Check that all fields of a struct lconv (or at least, the ones we care
     460             :  * about) are non-NULL.  The field list must match free_struct_lconv().
     461             :  */
     462             : static bool
     463          56 : struct_lconv_is_valid(struct lconv *s)
     464             : {
     465          56 :     if (s->decimal_point == NULL)
     466           0 :         return false;
     467          56 :     if (s->thousands_sep == NULL)
     468           0 :         return false;
     469          56 :     if (s->grouping == NULL)
     470           0 :         return false;
     471          56 :     if (s->int_curr_symbol == NULL)
     472           0 :         return false;
     473          56 :     if (s->currency_symbol == NULL)
     474           0 :         return false;
     475          56 :     if (s->mon_decimal_point == NULL)
     476           0 :         return false;
     477          56 :     if (s->mon_thousands_sep == NULL)
     478           0 :         return false;
     479          56 :     if (s->mon_grouping == NULL)
     480           0 :         return false;
     481          56 :     if (s->positive_sign == NULL)
     482           0 :         return false;
     483          56 :     if (s->negative_sign == NULL)
     484           0 :         return false;
     485          56 :     return true;
     486             : }
     487             : 
     488             : 
     489             : /*
     490             :  * Convert the strdup'd string at *str from the specified encoding to the
     491             :  * database encoding.
     492             :  */
     493             : static void
     494         448 : db_encoding_convert(int encoding, char **str)
     495             : {
     496             :     char       *pstr;
     497             :     char       *mstr;
     498             : 
     499             :     /* convert the string to the database encoding */
     500         448 :     pstr = pg_any_to_server(*str, strlen(*str), encoding);
     501         448 :     if (pstr == *str)
     502         448 :         return;                 /* no conversion happened */
     503             : 
     504             :     /* need it malloc'd not palloc'd */
     505           0 :     mstr = strdup(pstr);
     506           0 :     if (mstr == NULL)
     507           0 :         ereport(ERROR,
     508             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
     509             :                  errmsg("out of memory")));
     510             : 
     511             :     /* replace old string */
     512           0 :     free(*str);
     513           0 :     *str = mstr;
     514             : 
     515           0 :     pfree(pstr);
     516             : }
     517             : 
     518             : 
     519             : /*
     520             :  * Return the POSIX lconv struct (contains number/money formatting
     521             :  * information) with locale information for all categories.
     522             :  */
     523             : struct lconv *
     524        2966 : PGLC_localeconv(void)
     525             : {
     526             :     static struct lconv CurrentLocaleConv;
     527             :     static bool CurrentLocaleConvAllocated = false;
     528             :     struct lconv *extlconv;
     529             :     struct lconv tmp;
     530        2966 :     struct lconv worklconv = {0};
     531             : 
     532             :     /* Did we do it already? */
     533        2966 :     if (CurrentLocaleConvValid)
     534        2910 :         return &CurrentLocaleConv;
     535             : 
     536             :     /* Free any already-allocated storage */
     537          56 :     if (CurrentLocaleConvAllocated)
     538             :     {
     539           6 :         free_struct_lconv(&CurrentLocaleConv);
     540           6 :         CurrentLocaleConvAllocated = false;
     541             :     }
     542             : 
     543             :     /*
     544             :      * Use thread-safe method of obtaining a copy of lconv from the operating
     545             :      * system.
     546             :      */
     547          56 :     if (pg_localeconv_r(locale_monetary,
     548             :                         locale_numeric,
     549             :                         &tmp) != 0)
     550           0 :         elog(ERROR,
     551             :              "could not get lconv for LC_MONETARY = \"%s\", LC_NUMERIC = \"%s\": %m",
     552             :              locale_monetary, locale_numeric);
     553             : 
     554             :     /* Must copy data now now so we can re-encode it. */
     555          56 :     extlconv = &tmp;
     556          56 :     worklconv.decimal_point = strdup(extlconv->decimal_point);
     557          56 :     worklconv.thousands_sep = strdup(extlconv->thousands_sep);
     558          56 :     worklconv.grouping = strdup(extlconv->grouping);
     559          56 :     worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
     560          56 :     worklconv.currency_symbol = strdup(extlconv->currency_symbol);
     561          56 :     worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
     562          56 :     worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
     563          56 :     worklconv.mon_grouping = strdup(extlconv->mon_grouping);
     564          56 :     worklconv.positive_sign = strdup(extlconv->positive_sign);
     565          56 :     worklconv.negative_sign = strdup(extlconv->negative_sign);
     566             :     /* Copy scalar fields as well */
     567          56 :     worklconv.int_frac_digits = extlconv->int_frac_digits;
     568          56 :     worklconv.frac_digits = extlconv->frac_digits;
     569          56 :     worklconv.p_cs_precedes = extlconv->p_cs_precedes;
     570          56 :     worklconv.p_sep_by_space = extlconv->p_sep_by_space;
     571          56 :     worklconv.n_cs_precedes = extlconv->n_cs_precedes;
     572          56 :     worklconv.n_sep_by_space = extlconv->n_sep_by_space;
     573          56 :     worklconv.p_sign_posn = extlconv->p_sign_posn;
     574          56 :     worklconv.n_sign_posn = extlconv->n_sign_posn;
     575             : 
     576             :     /* Free the contents of the object populated by pg_localeconv_r(). */
     577          56 :     pg_localeconv_free(&tmp);
     578             : 
     579             :     /* If any of the preceding strdup calls failed, complain now. */
     580          56 :     if (!struct_lconv_is_valid(&worklconv))
     581           0 :         ereport(ERROR,
     582             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
     583             :                  errmsg("out of memory")));
     584             : 
     585          56 :     PG_TRY();
     586             :     {
     587             :         int         encoding;
     588             : 
     589             :         /*
     590             :          * Now we must perform encoding conversion from whatever's associated
     591             :          * with the locales into the database encoding.  If we can't identify
     592             :          * the encoding implied by LC_NUMERIC or LC_MONETARY (ie we get -1),
     593             :          * use PG_SQL_ASCII, which will result in just validating that the
     594             :          * strings are OK in the database encoding.
     595             :          */
     596          56 :         encoding = pg_get_encoding_from_locale(locale_numeric, true);
     597          56 :         if (encoding < 0)
     598           0 :             encoding = PG_SQL_ASCII;
     599             : 
     600          56 :         db_encoding_convert(encoding, &worklconv.decimal_point);
     601          56 :         db_encoding_convert(encoding, &worklconv.thousands_sep);
     602             :         /* grouping is not text and does not require conversion */
     603             : 
     604          56 :         encoding = pg_get_encoding_from_locale(locale_monetary, true);
     605          56 :         if (encoding < 0)
     606           0 :             encoding = PG_SQL_ASCII;
     607             : 
     608          56 :         db_encoding_convert(encoding, &worklconv.int_curr_symbol);
     609          56 :         db_encoding_convert(encoding, &worklconv.currency_symbol);
     610          56 :         db_encoding_convert(encoding, &worklconv.mon_decimal_point);
     611          56 :         db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
     612             :         /* mon_grouping is not text and does not require conversion */
     613          56 :         db_encoding_convert(encoding, &worklconv.positive_sign);
     614          56 :         db_encoding_convert(encoding, &worklconv.negative_sign);
     615             :     }
     616           0 :     PG_CATCH();
     617             :     {
     618           0 :         free_struct_lconv(&worklconv);
     619           0 :         PG_RE_THROW();
     620             :     }
     621          56 :     PG_END_TRY();
     622             : 
     623             :     /*
     624             :      * Everything is good, so save the results.
     625             :      */
     626          56 :     CurrentLocaleConv = worklconv;
     627          56 :     CurrentLocaleConvAllocated = true;
     628          56 :     CurrentLocaleConvValid = true;
     629          56 :     return &CurrentLocaleConv;
     630             : }
     631             : 
     632             : #ifdef WIN32
     633             : /*
     634             :  * On Windows, strftime() returns its output in encoding CP_ACP (the default
     635             :  * operating system codepage for the computer), which is likely different
     636             :  * from SERVER_ENCODING.  This is especially important in Japanese versions
     637             :  * of Windows which will use SJIS encoding, which we don't support as a
     638             :  * server encoding.
     639             :  *
     640             :  * So, instead of using strftime(), use wcsftime() to return the value in
     641             :  * wide characters (internally UTF16) and then convert to UTF8, which we
     642             :  * know how to handle directly.
     643             :  *
     644             :  * Note that this only affects the calls to strftime() in this file, which are
     645             :  * used to get the locale-aware strings. Other parts of the backend use
     646             :  * pg_strftime(), which isn't locale-aware and does not need to be replaced.
     647             :  */
     648             : static size_t
     649             : strftime_l_win32(char *dst, size_t dstlen,
     650             :                  const char *format, const struct tm *tm, locale_t locale)
     651             : {
     652             :     size_t      len;
     653             :     wchar_t     wformat[8];     /* formats used below need 3 chars */
     654             :     wchar_t     wbuf[MAX_L10N_DATA];
     655             : 
     656             :     /*
     657             :      * Get a wchar_t version of the format string.  We only actually use
     658             :      * plain-ASCII formats in this file, so we can say that they're UTF8.
     659             :      */
     660             :     len = MultiByteToWideChar(CP_UTF8, 0, format, -1,
     661             :                               wformat, lengthof(wformat));
     662             :     if (len == 0)
     663             :         elog(ERROR, "could not convert format string from UTF-8: error code %lu",
     664             :              GetLastError());
     665             : 
     666             :     len = _wcsftime_l(wbuf, MAX_L10N_DATA, wformat, tm, locale);
     667             :     if (len == 0)
     668             :     {
     669             :         /*
     670             :          * wcsftime failed, possibly because the result would not fit in
     671             :          * MAX_L10N_DATA.  Return 0 with the contents of dst unspecified.
     672             :          */
     673             :         return 0;
     674             :     }
     675             : 
     676             :     len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen - 1,
     677             :                               NULL, NULL);
     678             :     if (len == 0)
     679             :         elog(ERROR, "could not convert string to UTF-8: error code %lu",
     680             :              GetLastError());
     681             : 
     682             :     dst[len] = '\0';
     683             : 
     684             :     return len;
     685             : }
     686             : 
     687             : /* redefine strftime_l() */
     688             : #define strftime_l(a,b,c,d,e) strftime_l_win32(a,b,c,d,e)
     689             : #endif                          /* WIN32 */
     690             : 
     691             : /*
     692             :  * Subroutine for cache_locale_time().
     693             :  * Convert the given string from encoding "encoding" to the database
     694             :  * encoding, and store the result at *dst, replacing any previous value.
     695             :  */
     696             : static void
     697        1748 : cache_single_string(char **dst, const char *src, int encoding)
     698             : {
     699             :     char       *ptr;
     700             :     char       *olddst;
     701             : 
     702             :     /* Convert the string to the database encoding, or validate it's OK */
     703        1748 :     ptr = pg_any_to_server(src, strlen(src), encoding);
     704             : 
     705             :     /* Store the string in long-lived storage, replacing any previous value */
     706        1748 :     olddst = *dst;
     707        1748 :     *dst = MemoryContextStrdup(TopMemoryContext, ptr);
     708        1748 :     if (olddst)
     709           0 :         pfree(olddst);
     710             : 
     711             :     /* Might as well clean up any palloc'd conversion result, too */
     712        1748 :     if (ptr != src)
     713           0 :         pfree(ptr);
     714        1748 : }
     715             : 
     716             : /*
     717             :  * Update the lc_time localization cache variables if needed.
     718             :  */
     719             : void
     720       49546 : cache_locale_time(void)
     721             : {
     722             :     char        buf[(2 * 7 + 2 * 12) * MAX_L10N_DATA];
     723             :     char       *bufptr;
     724             :     time_t      timenow;
     725             :     struct tm  *timeinfo;
     726             :     struct tm   timeinfobuf;
     727       49546 :     bool        strftimefail = false;
     728             :     int         encoding;
     729             :     int         i;
     730             :     locale_t    locale;
     731             : 
     732             :     /* did we do this already? */
     733       49546 :     if (CurrentLCTimeValid)
     734       49500 :         return;
     735             : 
     736          46 :     elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
     737             : 
     738          46 :     errno = ENOENT;
     739             : #ifdef WIN32
     740             :     locale = _create_locale(LC_ALL, locale_time);
     741             :     if (locale == (locale_t) 0)
     742             :         _dosmaperr(GetLastError());
     743             : #else
     744          46 :     locale = newlocale(LC_ALL_MASK, locale_time, (locale_t) 0);
     745             : #endif
     746          46 :     if (!locale)
     747           0 :         report_newlocale_failure(locale_time);
     748             : 
     749             :     /* We use times close to current time as data for strftime(). */
     750          46 :     timenow = time(NULL);
     751          46 :     timeinfo = gmtime_r(&timenow, &timeinfobuf);
     752             : 
     753             :     /* Store the strftime results in MAX_L10N_DATA-sized portions of buf[] */
     754          46 :     bufptr = buf;
     755             : 
     756             :     /*
     757             :      * MAX_L10N_DATA is sufficient buffer space for every known locale, and
     758             :      * POSIX defines no strftime() errors.  (Buffer space exhaustion is not an
     759             :      * error.)  An implementation might report errors (e.g. ENOMEM) by
     760             :      * returning 0 (or, less plausibly, a negative value) and setting errno.
     761             :      * Report errno just in case the implementation did that, but clear it in
     762             :      * advance of the calls so we don't emit a stale, unrelated errno.
     763             :      */
     764          46 :     errno = 0;
     765             : 
     766             :     /* localized days */
     767         368 :     for (i = 0; i < 7; i++)
     768             :     {
     769         322 :         timeinfo->tm_wday = i;
     770         322 :         if (strftime_l(bufptr, MAX_L10N_DATA, "%a", timeinfo, locale) <= 0)
     771           0 :             strftimefail = true;
     772         322 :         bufptr += MAX_L10N_DATA;
     773         322 :         if (strftime_l(bufptr, MAX_L10N_DATA, "%A", timeinfo, locale) <= 0)
     774           0 :             strftimefail = true;
     775         322 :         bufptr += MAX_L10N_DATA;
     776             :     }
     777             : 
     778             :     /* localized months */
     779         598 :     for (i = 0; i < 12; i++)
     780             :     {
     781         552 :         timeinfo->tm_mon = i;
     782         552 :         timeinfo->tm_mday = 1;   /* make sure we don't have invalid date */
     783         552 :         if (strftime_l(bufptr, MAX_L10N_DATA, "%b", timeinfo, locale) <= 0)
     784           0 :             strftimefail = true;
     785         552 :         bufptr += MAX_L10N_DATA;
     786         552 :         if (strftime_l(bufptr, MAX_L10N_DATA, "%B", timeinfo, locale) <= 0)
     787           0 :             strftimefail = true;
     788         552 :         bufptr += MAX_L10N_DATA;
     789             :     }
     790             : 
     791             : #ifdef WIN32
     792             :     _free_locale(locale);
     793             : #else
     794          46 :     freelocale(locale);
     795             : #endif
     796             : 
     797             :     /*
     798             :      * At this point we've done our best to clean up, and can throw errors, or
     799             :      * call functions that might throw errors, with a clean conscience.
     800             :      */
     801          46 :     if (strftimefail)
     802           0 :         elog(ERROR, "strftime_l() failed");
     803             : 
     804             : #ifndef WIN32
     805             : 
     806             :     /*
     807             :      * As in PGLC_localeconv(), we must convert strftime()'s output from the
     808             :      * encoding implied by LC_TIME to the database encoding.  If we can't
     809             :      * identify the LC_TIME encoding, just perform encoding validation.
     810             :      */
     811          46 :     encoding = pg_get_encoding_from_locale(locale_time, true);
     812          46 :     if (encoding < 0)
     813           0 :         encoding = PG_SQL_ASCII;
     814             : 
     815             : #else
     816             : 
     817             :     /*
     818             :      * On Windows, strftime_win32() always returns UTF8 data, so convert from
     819             :      * that if necessary.
     820             :      */
     821             :     encoding = PG_UTF8;
     822             : 
     823             : #endif                          /* WIN32 */
     824             : 
     825          46 :     bufptr = buf;
     826             : 
     827             :     /* localized days */
     828         368 :     for (i = 0; i < 7; i++)
     829             :     {
     830         322 :         cache_single_string(&localized_abbrev_days[i], bufptr, encoding);
     831         322 :         bufptr += MAX_L10N_DATA;
     832         322 :         cache_single_string(&localized_full_days[i], bufptr, encoding);
     833         322 :         bufptr += MAX_L10N_DATA;
     834             :     }
     835          46 :     localized_abbrev_days[7] = NULL;
     836          46 :     localized_full_days[7] = NULL;
     837             : 
     838             :     /* localized months */
     839         598 :     for (i = 0; i < 12; i++)
     840             :     {
     841         552 :         cache_single_string(&localized_abbrev_months[i], bufptr, encoding);
     842         552 :         bufptr += MAX_L10N_DATA;
     843         552 :         cache_single_string(&localized_full_months[i], bufptr, encoding);
     844         552 :         bufptr += MAX_L10N_DATA;
     845             :     }
     846          46 :     localized_abbrev_months[12] = NULL;
     847          46 :     localized_full_months[12] = NULL;
     848             : 
     849          46 :     CurrentLCTimeValid = true;
     850             : }
     851             : 
     852             : 
     853             : #if defined(WIN32) && defined(LC_MESSAGES)
     854             : /*
     855             :  * Convert a Windows setlocale() argument to a Unix-style one.
     856             :  *
     857             :  * Regardless of platform, we install message catalogs under a Unix-style
     858             :  * LL[_CC][.ENCODING][@VARIANT] naming convention.  Only LC_MESSAGES settings
     859             :  * following that style will elicit localized interface strings.
     860             :  *
     861             :  * Before Visual Studio 2012 (msvcr110.dll), Windows setlocale() accepted "C"
     862             :  * (but not "c") and strings of the form <Language>[_<Country>][.<CodePage>],
     863             :  * case-insensitive.  setlocale() returns the fully-qualified form; for
     864             :  * example, setlocale("thaI") returns "Thai_Thailand.874".  Internally,
     865             :  * setlocale() and _create_locale() select a "locale identifier"[1] and store
     866             :  * it in an undocumented _locale_t field.  From that LCID, we can retrieve the
     867             :  * ISO 639 language and the ISO 3166 country.  Character encoding does not
     868             :  * matter, because the server and client encodings govern that.
     869             :  *
     870             :  * Windows Vista introduced the "locale name" concept[2], closely following
     871             :  * RFC 4646.  Locale identifiers are now deprecated.  Starting with Visual
     872             :  * Studio 2012, setlocale() accepts locale names in addition to the strings it
     873             :  * accepted historically.  It does not standardize them; setlocale("Th-tH")
     874             :  * returns "Th-tH".  setlocale(category, "") still returns a traditional
     875             :  * string.  Furthermore, msvcr110.dll changed the undocumented _locale_t
     876             :  * content to carry locale names instead of locale identifiers.
     877             :  *
     878             :  * Visual Studio 2015 should still be able to do the same as Visual Studio
     879             :  * 2012, but the declaration of locale_name is missing in _locale_t, causing
     880             :  * this code compilation to fail, hence this falls back instead on to
     881             :  * enumerating all system locales by using EnumSystemLocalesEx to find the
     882             :  * required locale name.  If the input argument is in Unix-style then we can
     883             :  * get ISO Locale name directly by using GetLocaleInfoEx() with LCType as
     884             :  * LOCALE_SNAME.
     885             :  *
     886             :  * This function returns a pointer to a static buffer bearing the converted
     887             :  * name or NULL if conversion fails.
     888             :  *
     889             :  * [1] https://docs.microsoft.com/en-us/windows/win32/intl/locale-identifiers
     890             :  * [2] https://docs.microsoft.com/en-us/windows/win32/intl/locale-names
     891             :  */
     892             : 
     893             : /*
     894             :  * Callback function for EnumSystemLocalesEx() in get_iso_localename().
     895             :  *
     896             :  * This function enumerates all system locales, searching for one that matches
     897             :  * an input with the format: <Language>[_<Country>], e.g.
     898             :  * English[_United States]
     899             :  *
     900             :  * The input is a three wchar_t array as an LPARAM. The first element is the
     901             :  * locale_name we want to match, the second element is an allocated buffer
     902             :  * where the Unix-style locale is copied if a match is found, and the third
     903             :  * element is the search status, 1 if a match was found, 0 otherwise.
     904             :  */
     905             : static BOOL CALLBACK
     906             : search_locale_enum(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
     907             : {
     908             :     wchar_t     test_locale[LOCALE_NAME_MAX_LENGTH];
     909             :     wchar_t   **argv;
     910             : 
     911             :     (void) (dwFlags);
     912             : 
     913             :     argv = (wchar_t **) lparam;
     914             :     *argv[2] = (wchar_t) 0;
     915             : 
     916             :     memset(test_locale, 0, sizeof(test_locale));
     917             : 
     918             :     /* Get the name of the <Language> in English */
     919             :     if (GetLocaleInfoEx(pStr, LOCALE_SENGLISHLANGUAGENAME,
     920             :                         test_locale, LOCALE_NAME_MAX_LENGTH))
     921             :     {
     922             :         /*
     923             :          * If the enumerated locale does not have a hyphen ("en") OR the
     924             :          * locale_name input does not have an underscore ("English"), we only
     925             :          * need to compare the <Language> tags.
     926             :          */
     927             :         if (wcsrchr(pStr, '-') == NULL || wcsrchr(argv[0], '_') == NULL)
     928             :         {
     929             :             if (_wcsicmp(argv[0], test_locale) == 0)
     930             :             {
     931             :                 wcscpy(argv[1], pStr);
     932             :                 *argv[2] = (wchar_t) 1;
     933             :                 return FALSE;
     934             :             }
     935             :         }
     936             : 
     937             :         /*
     938             :          * We have to compare a full <Language>_<Country> tag, so we append
     939             :          * the underscore and name of the country/region in English, e.g.
     940             :          * "English_United States".
     941             :          */
     942             :         else
     943             :         {
     944             :             size_t      len;
     945             : 
     946             :             wcscat(test_locale, L"_");
     947             :             len = wcslen(test_locale);
     948             :             if (GetLocaleInfoEx(pStr, LOCALE_SENGLISHCOUNTRYNAME,
     949             :                                 test_locale + len,
     950             :                                 LOCALE_NAME_MAX_LENGTH - len))
     951             :             {
     952             :                 if (_wcsicmp(argv[0], test_locale) == 0)
     953             :                 {
     954             :                     wcscpy(argv[1], pStr);
     955             :                     *argv[2] = (wchar_t) 1;
     956             :                     return FALSE;
     957             :                 }
     958             :             }
     959             :         }
     960             :     }
     961             : 
     962             :     return TRUE;
     963             : }
     964             : 
     965             : /*
     966             :  * This function converts a Windows locale name to an ISO formatted version
     967             :  * for Visual Studio 2015 or greater.
     968             :  *
     969             :  * Returns NULL, if no valid conversion was found.
     970             :  */
     971             : static char *
     972             : get_iso_localename(const char *winlocname)
     973             : {
     974             :     wchar_t     wc_locale_name[LOCALE_NAME_MAX_LENGTH];
     975             :     wchar_t     buffer[LOCALE_NAME_MAX_LENGTH];
     976             :     static char iso_lc_messages[LOCALE_NAME_MAX_LENGTH];
     977             :     char       *period;
     978             :     int         len;
     979             :     int         ret_val;
     980             : 
     981             :     /*
     982             :      * Valid locales have the following syntax:
     983             :      * <Language>[_<Country>[.<CodePage>]]
     984             :      *
     985             :      * GetLocaleInfoEx can only take locale name without code-page and for the
     986             :      * purpose of this API the code-page doesn't matter.
     987             :      */
     988             :     period = strchr(winlocname, '.');
     989             :     if (period != NULL)
     990             :         len = period - winlocname;
     991             :     else
     992             :         len = pg_mbstrlen(winlocname);
     993             : 
     994             :     memset(wc_locale_name, 0, sizeof(wc_locale_name));
     995             :     memset(buffer, 0, sizeof(buffer));
     996             :     MultiByteToWideChar(CP_ACP, 0, winlocname, len, wc_locale_name,
     997             :                         LOCALE_NAME_MAX_LENGTH);
     998             : 
     999             :     /*
    1000             :      * If the lc_messages is already a Unix-style string, we have a direct
    1001             :      * match with LOCALE_SNAME, e.g. en-US, en_US.
    1002             :      */
    1003             :     ret_val = GetLocaleInfoEx(wc_locale_name, LOCALE_SNAME, (LPWSTR) &buffer,
    1004             :                               LOCALE_NAME_MAX_LENGTH);
    1005             :     if (!ret_val)
    1006             :     {
    1007             :         /*
    1008             :          * Search for a locale in the system that matches language and country
    1009             :          * name.
    1010             :          */
    1011             :         wchar_t    *argv[3];
    1012             : 
    1013             :         argv[0] = wc_locale_name;
    1014             :         argv[1] = buffer;
    1015             :         argv[2] = (wchar_t *) &ret_val;
    1016             :         EnumSystemLocalesEx(search_locale_enum, LOCALE_WINDOWS, (LPARAM) argv,
    1017             :                             NULL);
    1018             :     }
    1019             : 
    1020             :     if (ret_val)
    1021             :     {
    1022             :         size_t      rc;
    1023             :         char       *hyphen;
    1024             : 
    1025             :         /* Locale names use only ASCII, any conversion locale suffices. */
    1026             :         rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), NULL);
    1027             :         if (rc == -1 || rc == sizeof(iso_lc_messages))
    1028             :             return NULL;
    1029             : 
    1030             :         /*
    1031             :          * Since the message catalogs sit on a case-insensitive filesystem, we
    1032             :          * need not standardize letter case here.  So long as we do not ship
    1033             :          * message catalogs for which it would matter, we also need not
    1034             :          * translate the script/variant portion, e.g.  uz-Cyrl-UZ to
    1035             :          * uz_UZ@cyrillic.  Simply replace the hyphen with an underscore.
    1036             :          */
    1037             :         hyphen = strchr(iso_lc_messages, '-');
    1038             :         if (hyphen)
    1039             :             *hyphen = '_';
    1040             :         return iso_lc_messages;
    1041             :     }
    1042             : 
    1043             :     return NULL;
    1044             : }
    1045             : 
    1046             : static char *
    1047             : IsoLocaleName(const char *winlocname)
    1048             : {
    1049             :     static char iso_lc_messages[LOCALE_NAME_MAX_LENGTH];
    1050             : 
    1051             :     if (pg_strcasecmp("c", winlocname) == 0 ||
    1052             :         pg_strcasecmp("posix", winlocname) == 0)
    1053             :     {
    1054             :         strcpy(iso_lc_messages, "C");
    1055             :         return iso_lc_messages;
    1056             :     }
    1057             :     else
    1058             :         return get_iso_localename(winlocname);
    1059             : }
    1060             : 
    1061             : #endif                          /* WIN32 && LC_MESSAGES */
    1062             : 
    1063             : /*
    1064             :  * Create a new pg_locale_t struct for the given collation oid.
    1065             :  */
    1066             : static pg_locale_t
    1067        3994 : create_pg_locale(Oid collid, MemoryContext context)
    1068             : {
    1069             :     HeapTuple   tp;
    1070             :     Form_pg_collation collform;
    1071             :     pg_locale_t result;
    1072             :     Datum       datum;
    1073             :     bool        isnull;
    1074             : 
    1075        3994 :     tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
    1076        3994 :     if (!HeapTupleIsValid(tp))
    1077           0 :         elog(ERROR, "cache lookup failed for collation %u", collid);
    1078        3994 :     collform = (Form_pg_collation) GETSTRUCT(tp);
    1079             : 
    1080        3994 :     if (collform->collprovider == COLLPROVIDER_BUILTIN)
    1081          52 :         result = create_pg_locale_builtin(collid, context);
    1082        3942 :     else if (collform->collprovider == COLLPROVIDER_ICU)
    1083         184 :         result = create_pg_locale_icu(collid, context);
    1084        3758 :     else if (collform->collprovider == COLLPROVIDER_LIBC)
    1085        3758 :         result = create_pg_locale_libc(collid, context);
    1086             :     else
    1087             :         /* shouldn't happen */
    1088           0 :         PGLOCALE_SUPPORT_ERROR(collform->collprovider);
    1089             : 
    1090        3988 :     result->is_default = false;
    1091             : 
    1092             :     Assert((result->collate_is_c && result->collate == NULL) ||
    1093             :            (!result->collate_is_c && result->collate != NULL));
    1094             : 
    1095        3988 :     datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collversion,
    1096             :                             &isnull);
    1097        3988 :     if (!isnull)
    1098             :     {
    1099             :         char       *actual_versionstr;
    1100             :         char       *collversionstr;
    1101             : 
    1102         230 :         collversionstr = TextDatumGetCString(datum);
    1103             : 
    1104         230 :         if (collform->collprovider == COLLPROVIDER_LIBC)
    1105           0 :             datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate);
    1106             :         else
    1107         230 :             datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale);
    1108             : 
    1109         230 :         actual_versionstr = get_collation_actual_version(collform->collprovider,
    1110         230 :                                                          TextDatumGetCString(datum));
    1111         230 :         if (!actual_versionstr)
    1112             :         {
    1113             :             /*
    1114             :              * This could happen when specifying a version in CREATE COLLATION
    1115             :              * but the provider does not support versioning, or manually
    1116             :              * creating a mess in the catalogs.
    1117             :              */
    1118           0 :             ereport(ERROR,
    1119             :                     (errmsg("collation \"%s\" has no actual version, but a version was recorded",
    1120             :                             NameStr(collform->collname))));
    1121             :         }
    1122             : 
    1123         230 :         if (strcmp(actual_versionstr, collversionstr) != 0)
    1124           0 :             ereport(WARNING,
    1125             :                     (errmsg("collation \"%s\" has version mismatch",
    1126             :                             NameStr(collform->collname)),
    1127             :                      errdetail("The collation in the database was created using version %s, "
    1128             :                                "but the operating system provides version %s.",
    1129             :                                collversionstr, actual_versionstr),
    1130             :                      errhint("Rebuild all objects affected by this collation and run "
    1131             :                              "ALTER COLLATION %s REFRESH VERSION, "
    1132             :                              "or build PostgreSQL with the right library version.",
    1133             :                              quote_qualified_identifier(get_namespace_name(collform->collnamespace),
    1134             :                                                         NameStr(collform->collname)))));
    1135             :     }
    1136             : 
    1137        3988 :     ReleaseSysCache(tp);
    1138             : 
    1139        3988 :     return result;
    1140             : }
    1141             : 
    1142             : /*
    1143             :  * Initialize default_locale with database locale settings.
    1144             :  */
    1145             : void
    1146       31904 : init_database_collation(void)
    1147             : {
    1148             :     HeapTuple   tup;
    1149             :     Form_pg_database dbform;
    1150             :     pg_locale_t result;
    1151             : 
    1152             :     Assert(default_locale == NULL);
    1153             : 
    1154             :     /* Fetch our pg_database row normally, via syscache */
    1155       31904 :     tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
    1156       31904 :     if (!HeapTupleIsValid(tup))
    1157           0 :         elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
    1158       31904 :     dbform = (Form_pg_database) GETSTRUCT(tup);
    1159             : 
    1160       31904 :     if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
    1161        1716 :         result = create_pg_locale_builtin(DEFAULT_COLLATION_OID,
    1162             :                                           TopMemoryContext);
    1163       30188 :     else if (dbform->datlocprovider == COLLPROVIDER_ICU)
    1164          26 :         result = create_pg_locale_icu(DEFAULT_COLLATION_OID,
    1165             :                                       TopMemoryContext);
    1166       30162 :     else if (dbform->datlocprovider == COLLPROVIDER_LIBC)
    1167       30162 :         result = create_pg_locale_libc(DEFAULT_COLLATION_OID,
    1168             :                                        TopMemoryContext);
    1169             :     else
    1170             :         /* shouldn't happen */
    1171           0 :         PGLOCALE_SUPPORT_ERROR(dbform->datlocprovider);
    1172             : 
    1173       31900 :     result->is_default = true;
    1174       31900 :     ReleaseSysCache(tup);
    1175             : 
    1176       31900 :     default_locale = result;
    1177       31900 : }
    1178             : 
    1179             : /*
    1180             :  * Create a pg_locale_t from a collation OID.  Results are cached for the
    1181             :  * lifetime of the backend.  Thus, do not free the result with freelocale().
    1182             :  *
    1183             :  * For simplicity, we always generate COLLATE + CTYPE even though we
    1184             :  * might only need one of them.  Since this is called only once per session,
    1185             :  * it shouldn't cost much.
    1186             :  */
    1187             : pg_locale_t
    1188    29262802 : pg_newlocale_from_collation(Oid collid)
    1189             : {
    1190             :     collation_cache_entry *cache_entry;
    1191             :     bool        found;
    1192             : 
    1193    29262802 :     if (collid == DEFAULT_COLLATION_OID)
    1194    25179552 :         return default_locale;
    1195             : 
    1196     4083250 :     if (!OidIsValid(collid))
    1197           0 :         elog(ERROR, "cache lookup failed for collation %u", collid);
    1198             : 
    1199     4083250 :     if (last_collation_cache_oid == collid)
    1200     4077322 :         return last_collation_cache_locale;
    1201             : 
    1202        5928 :     if (CollationCache == NULL)
    1203             :     {
    1204        3686 :         CollationCacheContext = AllocSetContextCreate(TopMemoryContext,
    1205             :                                                       "collation cache",
    1206             :                                                       ALLOCSET_DEFAULT_SIZES);
    1207        3686 :         CollationCache = collation_cache_create(CollationCacheContext,
    1208             :                                                 16, NULL);
    1209             :     }
    1210             : 
    1211        5928 :     cache_entry = collation_cache_insert(CollationCache, collid, &found);
    1212        5928 :     if (!found)
    1213             :     {
    1214             :         /*
    1215             :          * Make sure cache entry is marked invalid, in case we fail before
    1216             :          * setting things.
    1217             :          */
    1218        3994 :         cache_entry->locale = 0;
    1219             :     }
    1220             : 
    1221        5928 :     if (cache_entry->locale == 0)
    1222             :     {
    1223        3994 :         cache_entry->locale = create_pg_locale(collid, CollationCacheContext);
    1224             :     }
    1225             : 
    1226        5922 :     last_collation_cache_oid = collid;
    1227        5922 :     last_collation_cache_locale = cache_entry->locale;
    1228             : 
    1229        5922 :     return cache_entry->locale;
    1230             : }
    1231             : 
    1232             : /*
    1233             :  * Get provider-specific collation version string for the given collation from
    1234             :  * the operating system/library.
    1235             :  */
    1236             : char *
    1237       99114 : get_collation_actual_version(char collprovider, const char *collcollate)
    1238             : {
    1239       99114 :     char       *collversion = NULL;
    1240             : 
    1241       99114 :     if (collprovider == COLLPROVIDER_BUILTIN)
    1242        1830 :         collversion = get_collation_actual_version_builtin(collcollate);
    1243             : #ifdef USE_ICU
    1244       97284 :     else if (collprovider == COLLPROVIDER_ICU)
    1245       67792 :         collversion = get_collation_actual_version_icu(collcollate);
    1246             : #endif
    1247       29492 :     else if (collprovider == COLLPROVIDER_LIBC)
    1248       29492 :         collversion = get_collation_actual_version_libc(collcollate);
    1249             : 
    1250       99114 :     return collversion;
    1251             : }
    1252             : 
    1253             : size_t
    1254      435362 : pg_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen,
    1255             :             pg_locale_t locale)
    1256             : {
    1257      435362 :     if (locale->provider == COLLPROVIDER_BUILTIN)
    1258       11978 :         return strlower_builtin(dst, dstsize, src, srclen, locale);
    1259             : #ifdef USE_ICU
    1260      423384 :     else if (locale->provider == COLLPROVIDER_ICU)
    1261         528 :         return strlower_icu(dst, dstsize, src, srclen, locale);
    1262             : #endif
    1263      422856 :     else if (locale->provider == COLLPROVIDER_LIBC)
    1264      422856 :         return strlower_libc(dst, dstsize, src, srclen, locale);
    1265             :     else
    1266             :         /* shouldn't happen */
    1267           0 :         PGLOCALE_SUPPORT_ERROR(locale->provider);
    1268             : 
    1269             :     return 0;                   /* keep compiler quiet */
    1270             : }
    1271             : 
    1272             : size_t
    1273         208 : pg_strtitle(char *dst, size_t dstsize, const char *src, ssize_t srclen,
    1274             :             pg_locale_t locale)
    1275             : {
    1276         208 :     if (locale->provider == COLLPROVIDER_BUILTIN)
    1277         170 :         return strtitle_builtin(dst, dstsize, src, srclen, locale);
    1278             : #ifdef USE_ICU
    1279          38 :     else if (locale->provider == COLLPROVIDER_ICU)
    1280          30 :         return strtitle_icu(dst, dstsize, src, srclen, locale);
    1281             : #endif
    1282           8 :     else if (locale->provider == COLLPROVIDER_LIBC)
    1283           8 :         return strtitle_libc(dst, dstsize, src, srclen, locale);
    1284             :     else
    1285             :         /* shouldn't happen */
    1286           0 :         PGLOCALE_SUPPORT_ERROR(locale->provider);
    1287             : 
    1288             :     return 0;                   /* keep compiler quiet */
    1289             : }
    1290             : 
    1291             : size_t
    1292     1034076 : pg_strupper(char *dst, size_t dstsize, const char *src, ssize_t srclen,
    1293             :             pg_locale_t locale)
    1294             : {
    1295     1034076 :     if (locale->provider == COLLPROVIDER_BUILTIN)
    1296      316858 :         return strupper_builtin(dst, dstsize, src, srclen, locale);
    1297             : #ifdef USE_ICU
    1298      717218 :     else if (locale->provider == COLLPROVIDER_ICU)
    1299          54 :         return strupper_icu(dst, dstsize, src, srclen, locale);
    1300             : #endif
    1301      717164 :     else if (locale->provider == COLLPROVIDER_LIBC)
    1302      717164 :         return strupper_libc(dst, dstsize, src, srclen, locale);
    1303             :     else
    1304             :         /* shouldn't happen */
    1305           0 :         PGLOCALE_SUPPORT_ERROR(locale->provider);
    1306             : 
    1307             :     return 0;                   /* keep compiler quiet */
    1308             : }
    1309             : 
    1310             : size_t
    1311          24 : pg_strfold(char *dst, size_t dstsize, const char *src, ssize_t srclen,
    1312             :            pg_locale_t locale)
    1313             : {
    1314          24 :     if (locale->provider == COLLPROVIDER_BUILTIN)
    1315          12 :         return strfold_builtin(dst, dstsize, src, srclen, locale);
    1316             : #ifdef USE_ICU
    1317          12 :     else if (locale->provider == COLLPROVIDER_ICU)
    1318          12 :         return strfold_icu(dst, dstsize, src, srclen, locale);
    1319             : #endif
    1320             :     /* for libc, just use strlower */
    1321           0 :     else if (locale->provider == COLLPROVIDER_LIBC)
    1322           0 :         return strlower_libc(dst, dstsize, src, srclen, locale);
    1323             :     else
    1324             :         /* shouldn't happen */
    1325           0 :         PGLOCALE_SUPPORT_ERROR(locale->provider);
    1326             : 
    1327             :     return 0;                   /* keep compiler quiet */
    1328             : }
    1329             : 
    1330             : /*
    1331             :  * pg_strcoll
    1332             :  *
    1333             :  * Like pg_strncoll for NUL-terminated input strings.
    1334             :  */
    1335             : int
    1336    25153116 : pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale)
    1337             : {
    1338    25153116 :     return locale->collate->strncoll(arg1, -1, arg2, -1, locale);
    1339             : }
    1340             : 
    1341             : /*
    1342             :  * pg_strncoll
    1343             :  *
    1344             :  * Call ucol_strcollUTF8(), ucol_strcoll(), strcoll_l() or wcscoll_l() as
    1345             :  * appropriate for the given locale, platform, and database encoding. If the
    1346             :  * locale is not specified, use the database collation.
    1347             :  *
    1348             :  * The input strings must be encoded in the database encoding. If an input
    1349             :  * string is NUL-terminated, its length may be specified as -1.
    1350             :  *
    1351             :  * The caller is responsible for breaking ties if the collation is
    1352             :  * deterministic; this maintains consistency with pg_strnxfrm(), which cannot
    1353             :  * easily account for deterministic collations.
    1354             :  */
    1355             : int
    1356     4477408 : pg_strncoll(const char *arg1, ssize_t len1, const char *arg2, ssize_t len2,
    1357             :             pg_locale_t locale)
    1358             : {
    1359     4477408 :     return locale->collate->strncoll(arg1, len1, arg2, len2, locale);
    1360             : }
    1361             : 
    1362             : /*
    1363             :  * Return true if the collation provider supports pg_strxfrm() and
    1364             :  * pg_strnxfrm(); otherwise false.
    1365             :  *
    1366             :  *
    1367             :  * No similar problem is known for the ICU provider.
    1368             :  */
    1369             : bool
    1370       44670 : pg_strxfrm_enabled(pg_locale_t locale)
    1371             : {
    1372             :     /*
    1373             :      * locale->collate->strnxfrm is still a required method, even if it may
    1374             :      * have the wrong behavior, because the planner uses it for estimates in
    1375             :      * some cases.
    1376             :      */
    1377       44670 :     return locale->collate->strxfrm_is_safe;
    1378             : }
    1379             : 
    1380             : /*
    1381             :  * pg_strxfrm
    1382             :  *
    1383             :  * Like pg_strnxfrm for a NUL-terminated input string.
    1384             :  */
    1385             : size_t
    1386         144 : pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
    1387             : {
    1388         144 :     return locale->collate->strnxfrm(dest, destsize, src, -1, locale);
    1389             : }
    1390             : 
    1391             : /*
    1392             :  * pg_strnxfrm
    1393             :  *
    1394             :  * Transforms 'src' to a nul-terminated string stored in 'dest' such that
    1395             :  * ordinary strcmp() on transformed strings is equivalent to pg_strcoll() on
    1396             :  * untransformed strings.
    1397             :  *
    1398             :  * The input string must be encoded in the database encoding. If the input
    1399             :  * string is NUL-terminated, its length may be specified as -1. If 'destsize'
    1400             :  * is zero, 'dest' may be NULL.
    1401             :  *
    1402             :  * Not all providers support pg_strnxfrm() safely. The caller should check
    1403             :  * pg_strxfrm_enabled() first, otherwise this function may return wrong
    1404             :  * results or an error.
    1405             :  *
    1406             :  * Returns the number of bytes needed (or more) to store the transformed
    1407             :  * string, excluding the terminating nul byte. If the value returned is
    1408             :  * 'destsize' or greater, the resulting contents of 'dest' are undefined.
    1409             :  */
    1410             : size_t
    1411       10020 : pg_strnxfrm(char *dest, size_t destsize, const char *src, ssize_t srclen,
    1412             :             pg_locale_t locale)
    1413             : {
    1414       10020 :     return locale->collate->strnxfrm(dest, destsize, src, srclen, locale);
    1415             : }
    1416             : 
    1417             : /*
    1418             :  * Return true if the collation provider supports pg_strxfrm_prefix() and
    1419             :  * pg_strnxfrm_prefix(); otherwise false.
    1420             :  */
    1421             : bool
    1422        1656 : pg_strxfrm_prefix_enabled(pg_locale_t locale)
    1423             : {
    1424        1656 :     return (locale->collate->strnxfrm_prefix != NULL);
    1425             : }
    1426             : 
    1427             : /*
    1428             :  * pg_strxfrm_prefix
    1429             :  *
    1430             :  * Like pg_strnxfrm_prefix for a NUL-terminated input string.
    1431             :  */
    1432             : size_t
    1433        1656 : pg_strxfrm_prefix(char *dest, const char *src, size_t destsize,
    1434             :                   pg_locale_t locale)
    1435             : {
    1436        1656 :     return locale->collate->strnxfrm_prefix(dest, destsize, src, -1, locale);
    1437             : }
    1438             : 
    1439             : /*
    1440             :  * pg_strnxfrm_prefix
    1441             :  *
    1442             :  * Transforms 'src' to a byte sequence stored in 'dest' such that ordinary
    1443             :  * memcmp() on the byte sequence is equivalent to pg_strncoll() on
    1444             :  * untransformed strings. The result is not nul-terminated.
    1445             :  *
    1446             :  * The input string must be encoded in the database encoding. If the input
    1447             :  * string is NUL-terminated, its length may be specified as -1.
    1448             :  *
    1449             :  * Not all providers support pg_strnxfrm_prefix() safely. The caller should
    1450             :  * check pg_strxfrm_prefix_enabled() first, otherwise this function may return
    1451             :  * wrong results or an error.
    1452             :  *
    1453             :  * If destsize is not large enough to hold the resulting byte sequence, stores
    1454             :  * only the first destsize bytes in 'dest'. Returns the number of bytes
    1455             :  * actually copied to 'dest'.
    1456             :  */
    1457             : size_t
    1458           0 : pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src,
    1459             :                    ssize_t srclen, pg_locale_t locale)
    1460             : {
    1461           0 :     return locale->collate->strnxfrm_prefix(dest, destsize, src, srclen, locale);
    1462             : }
    1463             : 
    1464             : /*
    1465             :  * Return required encoding ID for the given locale, or -1 if any encoding is
    1466             :  * valid for the locale.
    1467             :  */
    1468             : int
    1469        1890 : builtin_locale_encoding(const char *locale)
    1470             : {
    1471        1890 :     if (strcmp(locale, "C") == 0)
    1472          64 :         return -1;
    1473        1826 :     else if (strcmp(locale, "C.UTF-8") == 0)
    1474        1796 :         return PG_UTF8;
    1475          30 :     else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
    1476          30 :         return PG_UTF8;
    1477             : 
    1478             : 
    1479           0 :     ereport(ERROR,
    1480             :             (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1481             :              errmsg("invalid locale name \"%s\" for builtin provider",
    1482             :                     locale)));
    1483             : 
    1484             :     return 0;                   /* keep compiler quiet */
    1485             : }
    1486             : 
    1487             : 
    1488             : /*
    1489             :  * Validate the locale and encoding combination, and return the canonical form
    1490             :  * of the locale name.
    1491             :  */
    1492             : const char *
    1493        1874 : builtin_validate_locale(int encoding, const char *locale)
    1494             : {
    1495        1874 :     const char *canonical_name = NULL;
    1496             :     int         required_encoding;
    1497             : 
    1498        1874 :     if (strcmp(locale, "C") == 0)
    1499          52 :         canonical_name = "C";
    1500        1822 :     else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
    1501        1782 :         canonical_name = "C.UTF-8";
    1502          40 :     else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
    1503          22 :         canonical_name = "PG_UNICODE_FAST";
    1504             : 
    1505        1874 :     if (!canonical_name)
    1506          18 :         ereport(ERROR,
    1507             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1508             :                  errmsg("invalid locale name \"%s\" for builtin provider",
    1509             :                         locale)));
    1510             : 
    1511        1856 :     required_encoding = builtin_locale_encoding(canonical_name);
    1512        1856 :     if (required_encoding >= 0 && encoding != required_encoding)
    1513           2 :         ereport(ERROR,
    1514             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1515             :                  errmsg("encoding \"%s\" does not match locale \"%s\"",
    1516             :                         pg_encoding_to_char(encoding), locale)));
    1517             : 
    1518        1854 :     return canonical_name;
    1519             : }
    1520             : 
    1521             : 
    1522             : 
    1523             : /*
    1524             :  * Return the BCP47 language tag representation of the requested locale.
    1525             :  *
    1526             :  * This function should be called before passing the string to ucol_open(),
    1527             :  * because conversion to a language tag also performs "level 2
    1528             :  * canonicalization". In addition to producing a consistent format, level 2
    1529             :  * canonicalization is able to more accurately interpret different input
    1530             :  * locale string formats, such as POSIX and .NET IDs.
    1531             :  */
    1532             : char *
    1533       67496 : icu_language_tag(const char *loc_str, int elevel)
    1534             : {
    1535             : #ifdef USE_ICU
    1536             :     UErrorCode  status;
    1537             :     char       *langtag;
    1538       67496 :     size_t      buflen = 32;    /* arbitrary starting buffer size */
    1539       67496 :     const bool  strict = true;
    1540             : 
    1541             :     /*
    1542             :      * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
    1543             :      * RFC5646 section 4.4). Additionally, in older ICU versions,
    1544             :      * uloc_toLanguageTag() doesn't always return the ultimate length on the
    1545             :      * first call, necessitating a loop.
    1546             :      */
    1547       67496 :     langtag = palloc(buflen);
    1548             :     while (true)
    1549             :     {
    1550       67496 :         status = U_ZERO_ERROR;
    1551       67496 :         uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
    1552             : 
    1553             :         /* try again if the buffer is not large enough */
    1554       67496 :         if ((status == U_BUFFER_OVERFLOW_ERROR ||
    1555       67496 :              status == U_STRING_NOT_TERMINATED_WARNING) &&
    1556             :             buflen < MaxAllocSize)
    1557             :         {
    1558           0 :             buflen = Min(buflen * 2, MaxAllocSize);
    1559           0 :             langtag = repalloc(langtag, buflen);
    1560           0 :             continue;
    1561             :         }
    1562             : 
    1563       67496 :         break;
    1564             :     }
    1565             : 
    1566       67496 :     if (U_FAILURE(status))
    1567             :     {
    1568          18 :         pfree(langtag);
    1569             : 
    1570          18 :         if (elevel > 0)
    1571          14 :             ereport(elevel,
    1572             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1573             :                      errmsg("could not convert locale name \"%s\" to language tag: %s",
    1574             :                             loc_str, u_errorName(status))));
    1575          12 :         return NULL;
    1576             :     }
    1577             : 
    1578       67478 :     return langtag;
    1579             : #else                           /* not USE_ICU */
    1580             :     ereport(ERROR,
    1581             :             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1582             :              errmsg("ICU is not supported in this build")));
    1583             :     return NULL;                /* keep compiler quiet */
    1584             : #endif                          /* not USE_ICU */
    1585             : }
    1586             : 
    1587             : /*
    1588             :  * Perform best-effort check that the locale is a valid one.
    1589             :  */
    1590             : void
    1591         166 : icu_validate_locale(const char *loc_str)
    1592             : {
    1593             : #ifdef USE_ICU
    1594             :     UCollator  *collator;
    1595             :     UErrorCode  status;
    1596             :     char        lang[ULOC_LANG_CAPACITY];
    1597         166 :     bool        found = false;
    1598         166 :     int         elevel = icu_validation_level;
    1599             : 
    1600             :     /* no validation */
    1601         166 :     if (elevel < 0)
    1602          12 :         return;
    1603             : 
    1604             :     /* downgrade to WARNING during pg_upgrade */
    1605         154 :     if (IsBinaryUpgrade && elevel > WARNING)
    1606           0 :         elevel = WARNING;
    1607             : 
    1608             :     /* validate that we can extract the language */
    1609         154 :     status = U_ZERO_ERROR;
    1610         154 :     uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
    1611         154 :     if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
    1612             :     {
    1613           0 :         ereport(elevel,
    1614             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1615             :                  errmsg("could not get language from ICU locale \"%s\": %s",
    1616             :                         loc_str, u_errorName(status)),
    1617             :                  errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
    1618             :                          "icu_validation_level", "disabled")));
    1619           0 :         return;
    1620             :     }
    1621             : 
    1622             :     /* check for special language name */
    1623         154 :     if (strcmp(lang, "") == 0 ||
    1624          46 :         strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
    1625         108 :         found = true;
    1626             : 
    1627             :     /* search for matching language within ICU */
    1628       15138 :     for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
    1629             :     {
    1630       14984 :         const char *otherloc = uloc_getAvailable(i);
    1631             :         char        otherlang[ULOC_LANG_CAPACITY];
    1632             : 
    1633       14984 :         status = U_ZERO_ERROR;
    1634       14984 :         uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
    1635       14984 :         if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
    1636           0 :             continue;
    1637             : 
    1638       14984 :         if (strcmp(lang, otherlang) == 0)
    1639          32 :             found = true;
    1640             :     }
    1641             : 
    1642         154 :     if (!found)
    1643          14 :         ereport(elevel,
    1644             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1645             :                  errmsg("ICU locale \"%s\" has unknown language \"%s\"",
    1646             :                         loc_str, lang),
    1647             :                  errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
    1648             :                          "icu_validation_level", "disabled")));
    1649             : 
    1650             :     /* check that it can be opened */
    1651         148 :     collator = pg_ucol_open(loc_str);
    1652         140 :     ucol_close(collator);
    1653             : #else                           /* not USE_ICU */
    1654             :     /* could get here if a collation was created by a build with ICU */
    1655             :     ereport(ERROR,
    1656             :             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1657             :              errmsg("ICU is not supported in this build")));
    1658             : #endif                          /* not USE_ICU */
    1659             : }

Generated by: LCOV version 1.14