PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_locale.c
Go to the documentation of this file.
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"
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 */
69extern char *get_collation_actual_version_builtin(const char *collcollate);
70
71/* pg_locale_icu.c */
72#ifdef USE_ICU
73extern UCollator *pg_ucol_open(const char *loc_str);
74extern char *get_collation_actual_version_icu(const char *collcollate);
75#endif
77
78/* pg_locale_libc.c */
80extern char *get_collation_actual_version_libc(const char *collcollate);
81
82extern size_t strlower_builtin(char *dst, size_t dstsize, const char *src,
83 ssize_t srclen, pg_locale_t locale);
84extern size_t strtitle_builtin(char *dst, size_t dstsize, const char *src,
85 ssize_t srclen, pg_locale_t locale);
86extern size_t strupper_builtin(char *dst, size_t dstsize, const char *src,
87 ssize_t srclen, pg_locale_t locale);
88extern size_t strfold_builtin(char *dst, size_t dstsize, const char *src,
89 ssize_t srclen, pg_locale_t locale);
90
91extern size_t strlower_icu(char *dst, size_t dstsize, const char *src,
92 ssize_t srclen, pg_locale_t locale);
93extern size_t strtitle_icu(char *dst, size_t dstsize, const char *src,
94 ssize_t srclen, pg_locale_t locale);
95extern size_t strupper_icu(char *dst, size_t dstsize, const char *src,
96 ssize_t srclen, pg_locale_t locale);
97extern size_t strfold_icu(char *dst, size_t dstsize, const char *src,
98 ssize_t srclen, pg_locale_t locale);
99
100extern size_t strlower_libc(char *dst, size_t dstsize, const char *src,
101 ssize_t srclen, pg_locale_t locale);
102extern size_t strtitle_libc(char *dst, size_t dstsize, const char *src,
103 ssize_t srclen, pg_locale_t locale);
104extern size_t strupper_libc(char *dst, size_t dstsize, const char *src,
105 ssize_t srclen, pg_locale_t locale);
106
107/* GUC settings */
112
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 */
126
127/* is the databases's LC_CTYPE the C locale? */
129
131
132/* indicates whether locale information cache is valid */
133static bool CurrentLocaleConvValid = false;
134static bool CurrentLCTimeValid = false;
135
136/* Cache for collation-related knowledge */
137
138typedef 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 */
145 char status;
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
162static 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 */
170
171#if defined(WIN32) && defined(LC_MESSAGES)
172static 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 */
189char *
190pg_perm_setlocale(int category, const char *locale)
191{
192 char *result;
193 const char *envvar;
194
195#ifndef WIN32
196 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 if (result == NULL)
218 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 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 strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
233 result = save_lc_ctype;
234
235#ifdef ENABLE_NLS
236 SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
237#else
239#endif
240 }
241
242 switch (category)
243 {
244 case LC_COLLATE:
245 envvar = "LC_COLLATE";
246 break;
247 case LC_CTYPE:
248 envvar = "LC_CTYPE";
249 break;
250#ifdef LC_MESSAGES
251 case LC_MESSAGES:
252 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 break;
260#endif /* LC_MESSAGES */
261 case LC_MONETARY:
262 envvar = "LC_MONETARY";
263 break;
264 case LC_NUMERIC:
265 envvar = "LC_NUMERIC";
266 break;
267 case LC_TIME:
268 envvar = "LC_TIME";
269 break;
270 default:
271 elog(FATAL, "unrecognized LC category: %d", category);
272 return NULL; /* keep compiler quiet */
273 }
274
275 if (setenv(envvar, result, 1) != 0)
276 return NULL;
277
278 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 */
292bool
293check_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 if (!pg_is_ascii(locale))
300 {
302 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
303 errmsg("locale name \"%s\" contains non-ASCII characters",
304 locale)));
305 return false;
306 }
307
308 if (canonname)
309 *canonname = NULL; /* in case of failure */
310
311 save = setlocale(category, NULL);
312 if (!save)
313 return false; /* won't happen, we hope */
314
315 /* save may be pointing at a modifiable scratch variable, see above. */
316 save = pstrdup(save);
317
318 /* set the locale with setlocale, to see if it accepts it. */
319 res = setlocale(category, locale);
320
321 /* save canonical name if requested. */
322 if (res && canonname)
323 *canonname = pstrdup(res);
324
325 /* restore old value. */
326 if (!setlocale(category, save))
327 elog(WARNING, "failed to restore old locale \"%s\"", save);
328 pfree(save);
329
330 /* Don't let Windows' non-ASCII locale names out. */
331 if (canonname && *canonname && !pg_is_ascii(*canonname))
332 {
334 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
335 errmsg("locale name \"%s\" contains non-ASCII characters",
336 *canonname)));
337 pfree(*canonname);
338 *canonname = NULL;
339 return false;
340 }
341
342 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 */
357bool
359{
360 return check_locale(LC_MONETARY, *newval, NULL);
361}
362
363void
364assign_locale_monetary(const char *newval, void *extra)
365{
367}
368
369bool
371{
372 return check_locale(LC_NUMERIC, *newval, NULL);
373}
374
375void
376assign_locale_numeric(const char *newval, void *extra)
377{
379}
380
381bool
383{
384 return check_locale(LC_TIME, *newval, NULL);
385}
386
387void
388assign_locale_time(const char *newval, void *extra)
389{
390 CurrentLCTimeValid = false;
391}
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 */
403bool
405{
406 if (**newval == '\0')
407 {
408 if (source == PGC_S_DEFAULT)
409 return true;
410 else
411 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 return check_locale(LC_MESSAGES, *newval, NULL);
421#else
422 return true;
423#endif
424}
425
426void
427assign_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 (void) pg_perm_setlocale(LC_MESSAGES, newval);
435#endif
436}
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 */
443static void
444free_struct_lconv(struct lconv *s)
445{
446 free(s->decimal_point);
447 free(s->thousands_sep);
448 free(s->grouping);
449 free(s->int_curr_symbol);
450 free(s->currency_symbol);
451 free(s->mon_decimal_point);
452 free(s->mon_thousands_sep);
453 free(s->mon_grouping);
454 free(s->positive_sign);
455 free(s->negative_sign);
456}
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 */
462static bool
463struct_lconv_is_valid(struct lconv *s)
464{
465 if (s->decimal_point == NULL)
466 return false;
467 if (s->thousands_sep == NULL)
468 return false;
469 if (s->grouping == NULL)
470 return false;
471 if (s->int_curr_symbol == NULL)
472 return false;
473 if (s->currency_symbol == NULL)
474 return false;
475 if (s->mon_decimal_point == NULL)
476 return false;
477 if (s->mon_thousands_sep == NULL)
478 return false;
479 if (s->mon_grouping == NULL)
480 return false;
481 if (s->positive_sign == NULL)
482 return false;
483 if (s->negative_sign == NULL)
484 return false;
485 return true;
486}
487
488
489/*
490 * Convert the strdup'd string at *str from the specified encoding to the
491 * database encoding.
492 */
493static void
495{
496 char *pstr;
497 char *mstr;
498
499 /* convert the string to the database encoding */
500 pstr = pg_any_to_server(*str, strlen(*str), encoding);
501 if (pstr == *str)
502 return; /* no conversion happened */
503
504 /* need it malloc'd not palloc'd */
505 mstr = strdup(pstr);
506 if (mstr == NULL)
508 (errcode(ERRCODE_OUT_OF_MEMORY),
509 errmsg("out of memory")));
510
511 /* replace old string */
512 free(*str);
513 *str = mstr;
514
515 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 */
523struct lconv *
525{
526 static struct lconv CurrentLocaleConv;
527 static bool CurrentLocaleConvAllocated = false;
528 struct lconv *extlconv;
529 struct lconv tmp;
530 struct lconv worklconv = {0};
531
532 /* Did we do it already? */
534 return &CurrentLocaleConv;
535
536 /* Free any already-allocated storage */
537 if (CurrentLocaleConvAllocated)
538 {
539 free_struct_lconv(&CurrentLocaleConv);
540 CurrentLocaleConvAllocated = false;
541 }
542
543 /*
544 * Use thread-safe method of obtaining a copy of lconv from the operating
545 * system.
546 */
549 &tmp) != 0)
550 elog(ERROR,
551 "could not get lconv for LC_MONETARY = \"%s\", LC_NUMERIC = \"%s\": %m",
553
554 /* Must copy data now now so we can re-encode it. */
555 extlconv = &tmp;
556 worklconv.decimal_point = strdup(extlconv->decimal_point);
557 worklconv.thousands_sep = strdup(extlconv->thousands_sep);
558 worklconv.grouping = strdup(extlconv->grouping);
559 worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
560 worklconv.currency_symbol = strdup(extlconv->currency_symbol);
561 worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
562 worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
563 worklconv.mon_grouping = strdup(extlconv->mon_grouping);
564 worklconv.positive_sign = strdup(extlconv->positive_sign);
565 worklconv.negative_sign = strdup(extlconv->negative_sign);
566 /* Copy scalar fields as well */
567 worklconv.int_frac_digits = extlconv->int_frac_digits;
568 worklconv.frac_digits = extlconv->frac_digits;
569 worklconv.p_cs_precedes = extlconv->p_cs_precedes;
570 worklconv.p_sep_by_space = extlconv->p_sep_by_space;
571 worklconv.n_cs_precedes = extlconv->n_cs_precedes;
572 worklconv.n_sep_by_space = extlconv->n_sep_by_space;
573 worklconv.p_sign_posn = extlconv->p_sign_posn;
574 worklconv.n_sign_posn = extlconv->n_sign_posn;
575
576 /* Free the contents of the object populated by pg_localeconv_r(). */
577 pg_localeconv_free(&tmp);
578
579 /* If any of the preceding strdup calls failed, complain now. */
580 if (!struct_lconv_is_valid(&worklconv))
582 (errcode(ERRCODE_OUT_OF_MEMORY),
583 errmsg("out of memory")));
584
585 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 */
597 if (encoding < 0)
599
600 db_encoding_convert(encoding, &worklconv.decimal_point);
601 db_encoding_convert(encoding, &worklconv.thousands_sep);
602 /* grouping is not text and does not require conversion */
603
605 if (encoding < 0)
607
608 db_encoding_convert(encoding, &worklconv.int_curr_symbol);
609 db_encoding_convert(encoding, &worklconv.currency_symbol);
610 db_encoding_convert(encoding, &worklconv.mon_decimal_point);
611 db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
612 /* mon_grouping is not text and does not require conversion */
613 db_encoding_convert(encoding, &worklconv.positive_sign);
614 db_encoding_convert(encoding, &worklconv.negative_sign);
615 }
616 PG_CATCH();
617 {
618 free_struct_lconv(&worklconv);
619 PG_RE_THROW();
620 }
621 PG_END_TRY();
622
623 /*
624 * Everything is good, so save the results.
625 */
626 CurrentLocaleConv = worklconv;
627 CurrentLocaleConvAllocated = true;
629 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 */
648static size_t
649strftime_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 */
696static void
697cache_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 ptr = pg_any_to_server(src, strlen(src), encoding);
704
705 /* Store the string in long-lived storage, replacing any previous value */
706 olddst = *dst;
708 if (olddst)
709 pfree(olddst);
710
711 /* Might as well clean up any palloc'd conversion result, too */
712 if (ptr != src)
713 pfree(ptr);
714}
715
716/*
717 * Update the lc_time localization cache variables if needed.
718 */
719void
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 bool strftimefail = false;
728 int encoding;
729 int i;
731
732 /* did we do this already? */
734 return;
735
736 elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
737
738 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 locale = newlocale(LC_ALL_MASK, locale_time, (locale_t) 0);
745#endif
746 if (!locale)
748
749 /* We use times close to current time as data for strftime(). */
750 timenow = time(NULL);
751 timeinfo = gmtime_r(&timenow, &timeinfobuf);
752
753 /* Store the strftime results in MAX_L10N_DATA-sized portions of buf[] */
754 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 errno = 0;
765
766 /* localized days */
767 for (i = 0; i < 7; i++)
768 {
769 timeinfo->tm_wday = i;
770 if (strftime_l(bufptr, MAX_L10N_DATA, "%a", timeinfo, locale) <= 0)
771 strftimefail = true;
772 bufptr += MAX_L10N_DATA;
773 if (strftime_l(bufptr, MAX_L10N_DATA, "%A", timeinfo, locale) <= 0)
774 strftimefail = true;
775 bufptr += MAX_L10N_DATA;
776 }
777
778 /* localized months */
779 for (i = 0; i < 12; i++)
780 {
781 timeinfo->tm_mon = i;
782 timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
783 if (strftime_l(bufptr, MAX_L10N_DATA, "%b", timeinfo, locale) <= 0)
784 strftimefail = true;
785 bufptr += MAX_L10N_DATA;
786 if (strftime_l(bufptr, MAX_L10N_DATA, "%B", timeinfo, locale) <= 0)
787 strftimefail = true;
788 bufptr += MAX_L10N_DATA;
789 }
790
791#ifdef WIN32
792 _free_locale(locale);
793#else
794 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 if (strftimefail)
802 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 */
812 if (encoding < 0)
814
815#else
816
817 /*
818 * On Windows, strftime_win32() always returns UTF8 data, so convert from
819 * that if necessary.
820 */
822
823#endif /* WIN32 */
824
825 bufptr = buf;
826
827 /* localized days */
828 for (i = 0; i < 7; i++)
829 {
831 bufptr += MAX_L10N_DATA;
833 bufptr += MAX_L10N_DATA;
834 }
835 localized_abbrev_days[7] = NULL;
836 localized_full_days[7] = NULL;
837
838 /* localized months */
839 for (i = 0; i < 12; i++)
840 {
842 bufptr += MAX_L10N_DATA;
844 bufptr += MAX_L10N_DATA;
845 }
846 localized_abbrev_months[12] = NULL;
847 localized_full_months[12] = NULL;
848
849 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 */
905static BOOL CALLBACK
906search_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 */
971static char *
972get_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
1046static char *
1047IsoLocaleName(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 */
1066static pg_locale_t
1068{
1069 HeapTuple tp;
1070 Form_pg_collation collform;
1071 pg_locale_t result;
1072 Datum datum;
1073 bool isnull;
1074
1075 tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1076 if (!HeapTupleIsValid(tp))
1077 elog(ERROR, "cache lookup failed for collation %u", collid);
1078 collform = (Form_pg_collation) GETSTRUCT(tp);
1079
1080 if (collform->collprovider == COLLPROVIDER_BUILTIN)
1081 result = create_pg_locale_builtin(collid, context);
1082 else if (collform->collprovider == COLLPROVIDER_ICU)
1083 result = create_pg_locale_icu(collid, context);
1084 else if (collform->collprovider == COLLPROVIDER_LIBC)
1085 result = create_pg_locale_libc(collid, context);
1086 else
1087 /* shouldn't happen */
1088 PGLOCALE_SUPPORT_ERROR(collform->collprovider);
1089
1090 result->is_default = false;
1091
1092 Assert((result->collate_is_c && result->collate == NULL) ||
1093 (!result->collate_is_c && result->collate != NULL));
1094
1095 datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collversion,
1096 &isnull);
1097 if (!isnull)
1098 {
1099 char *actual_versionstr;
1100 char *collversionstr;
1101
1102 collversionstr = TextDatumGetCString(datum);
1103
1104 if (collform->collprovider == COLLPROVIDER_LIBC)
1105 datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate);
1106 else
1107 datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale);
1108
1109 actual_versionstr = get_collation_actual_version(collform->collprovider,
1110 TextDatumGetCString(datum));
1111 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 ereport(ERROR,
1119 (errmsg("collation \"%s\" has no actual version, but a version was recorded",
1120 NameStr(collform->collname))));
1121 }
1122
1123 if (strcmp(actual_versionstr, collversionstr) != 0)
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 ReleaseSysCache(tp);
1138
1139 return result;
1140}
1141
1142/*
1143 * Initialize default_locale with database locale settings.
1144 */
1145void
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 tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1156 if (!HeapTupleIsValid(tup))
1157 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1158 dbform = (Form_pg_database) GETSTRUCT(tup);
1159
1160 if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
1161 result = create_pg_locale_builtin(DEFAULT_COLLATION_OID,
1163 else if (dbform->datlocprovider == COLLPROVIDER_ICU)
1164 result = create_pg_locale_icu(DEFAULT_COLLATION_OID,
1166 else if (dbform->datlocprovider == COLLPROVIDER_LIBC)
1167 result = create_pg_locale_libc(DEFAULT_COLLATION_OID,
1169 else
1170 /* shouldn't happen */
1171 PGLOCALE_SUPPORT_ERROR(dbform->datlocprovider);
1172
1173 result->is_default = true;
1174 ReleaseSysCache(tup);
1175
1176 default_locale = result;
1177}
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 */
1189{
1190 collation_cache_entry *cache_entry;
1191 bool found;
1192
1193 if (collid == DEFAULT_COLLATION_OID)
1194 return default_locale;
1195
1196 if (!OidIsValid(collid))
1197 elog(ERROR, "cache lookup failed for collation %u", collid);
1198
1201
1202 if (CollationCache == NULL)
1203 {
1205 "collation cache",
1207 CollationCache = collation_cache_create(CollationCacheContext,
1208 16, NULL);
1209 }
1210
1211 cache_entry = collation_cache_insert(CollationCache, collid, &found);
1212 if (!found)
1213 {
1214 /*
1215 * Make sure cache entry is marked invalid, in case we fail before
1216 * setting things.
1217 */
1218 cache_entry->locale = 0;
1219 }
1220
1221 if (cache_entry->locale == 0)
1222 {
1224 }
1225
1227 last_collation_cache_locale = cache_entry->locale;
1228
1229 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 */
1236char *
1237get_collation_actual_version(char collprovider, const char *collcollate)
1238{
1239 char *collversion = NULL;
1240
1241 if (collprovider == COLLPROVIDER_BUILTIN)
1242 collversion = get_collation_actual_version_builtin(collcollate);
1243#ifdef USE_ICU
1244 else if (collprovider == COLLPROVIDER_ICU)
1245 collversion = get_collation_actual_version_icu(collcollate);
1246#endif
1247 else if (collprovider == COLLPROVIDER_LIBC)
1248 collversion = get_collation_actual_version_libc(collcollate);
1249
1250 return collversion;
1251}
1252
1253size_t
1254pg_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen,
1256{
1257 if (locale->provider == COLLPROVIDER_BUILTIN)
1258 return strlower_builtin(dst, dstsize, src, srclen, locale);
1259#ifdef USE_ICU
1260 else if (locale->provider == COLLPROVIDER_ICU)
1261 return strlower_icu(dst, dstsize, src, srclen, locale);
1262#endif
1263 else if (locale->provider == COLLPROVIDER_LIBC)
1264 return strlower_libc(dst, dstsize, src, srclen, locale);
1265 else
1266 /* shouldn't happen */
1267 PGLOCALE_SUPPORT_ERROR(locale->provider);
1268
1269 return 0; /* keep compiler quiet */
1270}
1271
1272size_t
1273pg_strtitle(char *dst, size_t dstsize, const char *src, ssize_t srclen,
1275{
1276 if (locale->provider == COLLPROVIDER_BUILTIN)
1277 return strtitle_builtin(dst, dstsize, src, srclen, locale);
1278#ifdef USE_ICU
1279 else if (locale->provider == COLLPROVIDER_ICU)
1280 return strtitle_icu(dst, dstsize, src, srclen, locale);
1281#endif
1282 else if (locale->provider == COLLPROVIDER_LIBC)
1283 return strtitle_libc(dst, dstsize, src, srclen, locale);
1284 else
1285 /* shouldn't happen */
1286 PGLOCALE_SUPPORT_ERROR(locale->provider);
1287
1288 return 0; /* keep compiler quiet */
1289}
1290
1291size_t
1292pg_strupper(char *dst, size_t dstsize, const char *src, ssize_t srclen,
1294{
1295 if (locale->provider == COLLPROVIDER_BUILTIN)
1296 return strupper_builtin(dst, dstsize, src, srclen, locale);
1297#ifdef USE_ICU
1298 else if (locale->provider == COLLPROVIDER_ICU)
1299 return strupper_icu(dst, dstsize, src, srclen, locale);
1300#endif
1301 else if (locale->provider == COLLPROVIDER_LIBC)
1302 return strupper_libc(dst, dstsize, src, srclen, locale);
1303 else
1304 /* shouldn't happen */
1305 PGLOCALE_SUPPORT_ERROR(locale->provider);
1306
1307 return 0; /* keep compiler quiet */
1308}
1309
1310size_t
1311pg_strfold(char *dst, size_t dstsize, const char *src, ssize_t srclen,
1313{
1314 if (locale->provider == COLLPROVIDER_BUILTIN)
1315 return strfold_builtin(dst, dstsize, src, srclen, locale);
1316#ifdef USE_ICU
1317 else if (locale->provider == COLLPROVIDER_ICU)
1318 return strfold_icu(dst, dstsize, src, srclen, locale);
1319#endif
1320 /* for libc, just use strlower */
1321 else if (locale->provider == COLLPROVIDER_LIBC)
1322 return strlower_libc(dst, dstsize, src, srclen, locale);
1323 else
1324 /* shouldn't happen */
1325 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 */
1335int
1336pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale)
1337{
1338 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 */
1355int
1356pg_strncoll(const char *arg1, ssize_t len1, const char *arg2, ssize_t len2,
1358{
1359 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 */
1369bool
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 return locale->collate->strxfrm_is_safe;
1378}
1379
1380/*
1381 * pg_strxfrm
1382 *
1383 * Like pg_strnxfrm for a NUL-terminated input string.
1384 */
1385size_t
1386pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
1387{
1388 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 */
1410size_t
1411pg_strnxfrm(char *dest, size_t destsize, const char *src, ssize_t srclen,
1413{
1414 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 */
1421bool
1423{
1424 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 */
1432size_t
1433pg_strxfrm_prefix(char *dest, const char *src, size_t destsize,
1435{
1436 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 */
1457size_t
1458pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src,
1459 ssize_t srclen, pg_locale_t locale)
1460{
1461 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 */
1468int
1470{
1471 if (strcmp(locale, "C") == 0)
1472 return -1;
1473 else if (strcmp(locale, "C.UTF-8") == 0)
1474 return PG_UTF8;
1475 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1476 return PG_UTF8;
1477
1478
1479 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 */
1492const char *
1494{
1495 const char *canonical_name = NULL;
1496 int required_encoding;
1497
1498 if (strcmp(locale, "C") == 0)
1499 canonical_name = "C";
1500 else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
1501 canonical_name = "C.UTF-8";
1502 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1503 canonical_name = "PG_UNICODE_FAST";
1504
1505 if (!canonical_name)
1506 ereport(ERROR,
1507 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1508 errmsg("invalid locale name \"%s\" for builtin provider",
1509 locale)));
1510
1511 required_encoding = builtin_locale_encoding(canonical_name);
1512 if (required_encoding >= 0 && encoding != required_encoding)
1513 ereport(ERROR,
1514 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1515 errmsg("encoding \"%s\" does not match locale \"%s\"",
1517
1518 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 */
1532char *
1533icu_language_tag(const char *loc_str, int elevel)
1534{
1535#ifdef USE_ICU
1536 UErrorCode status;
1537 char *langtag;
1538 size_t buflen = 32; /* arbitrary starting buffer size */
1539 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 langtag = palloc(buflen);
1548 while (true)
1549 {
1550 status = U_ZERO_ERROR;
1551 uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
1552
1553 /* try again if the buffer is not large enough */
1554 if ((status == U_BUFFER_OVERFLOW_ERROR ||
1555 status == U_STRING_NOT_TERMINATED_WARNING) &&
1556 buflen < MaxAllocSize)
1557 {
1558 buflen = Min(buflen * 2, MaxAllocSize);
1559 langtag = repalloc(langtag, buflen);
1560 continue;
1561 }
1562
1563 break;
1564 }
1565
1566 if (U_FAILURE(status))
1567 {
1568 pfree(langtag);
1569
1570 if (elevel > 0)
1571 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 return NULL;
1576 }
1577
1578 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 */
1590void
1591icu_validate_locale(const char *loc_str)
1592{
1593#ifdef USE_ICU
1594 UCollator *collator;
1595 UErrorCode status;
1596 char lang[ULOC_LANG_CAPACITY];
1597 bool found = false;
1598 int elevel = icu_validation_level;
1599
1600 /* no validation */
1601 if (elevel < 0)
1602 return;
1603
1604 /* downgrade to WARNING during pg_upgrade */
1605 if (IsBinaryUpgrade && elevel > WARNING)
1606 elevel = WARNING;
1607
1608 /* validate that we can extract the language */
1609 status = U_ZERO_ERROR;
1610 uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
1611 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1612 {
1613 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 return;
1620 }
1621
1622 /* check for special language name */
1623 if (strcmp(lang, "") == 0 ||
1624 strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
1625 found = true;
1626
1627 /* search for matching language within ICU */
1628 for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
1629 {
1630 const char *otherloc = uloc_getAvailable(i);
1631 char otherlang[ULOC_LANG_CAPACITY];
1632
1633 status = U_ZERO_ERROR;
1634 uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
1635 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1636 continue;
1637
1638 if (strcmp(lang, otherlang) == 0)
1639 found = true;
1640 }
1641
1642 if (!found)
1643 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 collator = pg_ucol_open(loc_str);
1652 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}
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define NameStr(name)
Definition: c.h:717
#define Min(x, y)
Definition: c.h:975
uint32_t uint32
Definition: c.h:502
#define lengthof(array)
Definition: c.h:759
#define OidIsValid(objectId)
Definition: c.h:746
Oid collid
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define PG_RE_THROW()
Definition: elog.h:405
#define DEBUG3
Definition: elog.h:28
#define FATAL
Definition: elog.h:41
#define PG_TRY(...)
Definition: elog.h:372
#define WARNING
Definition: elog.h:36
#define PG_END_TRY(...)
Definition: elog.h:397
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:382
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:149
#define MaxAllocSize
Definition: fe_memutils.h:22
bool IsBinaryUpgrade
Definition: globals.c:122
Oid MyDatabaseId
Definition: globals.c:95
#define newval
GucSource
Definition: guc.h:112
@ PGC_S_DEFAULT
Definition: guc.h:113
Assert(PointerIsAligned(start, uint64))
const char * str
#define free(a)
Definition: header.h:65
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
#define period
Definition: indent_codes.h:66
static char * locale
Definition: initdb.c:140
int i
Definition: isn.c:77
static struct pg_tm tm
Definition: localtime.c:104
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3506
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:1037
void SetMessageEncoding(int encoding)
Definition: mbutils.c:1171
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:2309
char * pstrdup(const char *in)
Definition: mcxt.c:2322
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:2167
void pfree(void *pointer)
Definition: mcxt.c:2147
MemoryContext TopMemoryContext
Definition: mcxt.c:165
void * palloc(Size size)
Definition: mcxt.c:1940
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:180
static char format
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:58
const void size_t len
FormData_pg_database * Form_pg_database
Definition: pg_database.h:96
int32 encoding
Definition: pg_database.h:41
size_t strfold_icu(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
int icu_validation_level
Definition: pg_locale.c:113
static pg_locale_t last_collation_cache_locale
Definition: pg_locale.c:169
void cache_locale_time(void)
Definition: pg_locale.c:720
size_t pg_strnxfrm(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1411
bool pg_strxfrm_enabled(pg_locale_t locale)
Definition: pg_locale.c:1370
char * localized_full_months[12+1]
Definition: pg_locale.c:125
void icu_validate_locale(const char *loc_str)
Definition: pg_locale.c:1591
pg_locale_t create_pg_locale_libc(Oid collid, MemoryContext context)
static bool CurrentLCTimeValid
Definition: pg_locale.c:134
void assign_locale_time(const char *newval, void *extra)
Definition: pg_locale.c:388
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1237
pg_locale_t create_pg_locale_builtin(Oid collid, MemoryContext context)
size_t strupper_icu(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
bool check_locale_time(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:382
char * locale_messages
Definition: pg_locale.c:108
size_t strlower_icu(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
size_t strtitle_libc(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
size_t strupper_libc(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
char * locale_numeric
Definition: pg_locale.c:110
size_t strtitle_icu(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1188
size_t pg_strfold(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1311
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:1469
size_t pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1458
size_t strupper_builtin(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
bool database_ctype_is_c
Definition: pg_locale.c:128
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:190
#define PGLOCALE_SUPPORT_ERROR(provider)
Definition: pg_locale.c:56
static pg_locale_t create_pg_locale(Oid collid, MemoryContext context)
Definition: pg_locale.c:1067
char * locale_time
Definition: pg_locale.c:111
static void cache_single_string(char **dst, const char *src, int encoding)
Definition: pg_locale.c:697
size_t strlower_builtin(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
char * get_collation_actual_version_libc(const char *collcollate)
size_t pg_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1254
bool check_locale_numeric(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:370
static void db_encoding_convert(int encoding, char **str)
Definition: pg_locale.c:494
void assign_locale_numeric(const char *newval, void *extra)
Definition: pg_locale.c:376
bool check_locale_messages(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:404
#define MAX_L10N_DATA
Definition: pg_locale.c:65
size_t strlower_libc(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
char * get_collation_actual_version_builtin(const char *collcollate)
static void free_struct_lconv(struct lconv *s)
Definition: pg_locale.c:444
static MemoryContext CollationCacheContext
Definition: pg_locale.c:161
void assign_locale_messages(const char *newval, void *extra)
Definition: pg_locale.c:427
static bool CurrentLocaleConvValid
Definition: pg_locale.c:133
struct lconv * PGLC_localeconv(void)
Definition: pg_locale.c:524
pg_locale_t create_pg_locale_icu(Oid collid, MemoryContext context)
size_t pg_strtitle(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1273
int pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale)
Definition: pg_locale.c:1336
bool pg_strxfrm_prefix_enabled(pg_locale_t locale)
Definition: pg_locale.c:1422
char * icu_language_tag(const char *loc_str, int elevel)
Definition: pg_locale.c:1533
char * localized_abbrev_months[12+1]
Definition: pg_locale.c:124
static pg_locale_t default_locale
Definition: pg_locale.c:130
static collation_cache_hash * CollationCache
Definition: pg_locale.c:162
int pg_strncoll(const char *arg1, ssize_t len1, const char *arg2, ssize_t len2, pg_locale_t locale)
Definition: pg_locale.c:1356
size_t strfold_builtin(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
static bool struct_lconv_is_valid(struct lconv *s)
Definition: pg_locale.c:463
void init_database_collation(void)
Definition: pg_locale.c:1146
char * localized_full_days[7+1]
Definition: pg_locale.c:123
size_t strtitle_builtin(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
size_t pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
Definition: pg_locale.c:1386
const char * builtin_validate_locale(int encoding, const char *locale)
Definition: pg_locale.c:1493
size_t pg_strupper(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1292
void assign_locale_monetary(const char *newval, void *extra)
Definition: pg_locale.c:364
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:293
char * localized_abbrev_days[7+1]
Definition: pg_locale.c:122
size_t pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, pg_locale_t locale)
Definition: pg_locale.c:1433
char * locale_monetary
Definition: pg_locale.c:109
bool check_locale_monetary(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:358
static Oid last_collation_cache_oid
Definition: pg_locale.c:168
#define LOCALE_NAME_BUFLEN
Definition: pg_locale.h:20
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
void report_newlocale_failure(const char *localename)
static rewind_source * source
Definition: pg_rewind.c:89
static char * buf
Definition: pg_test_fsync.c:72
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
@ PG_UTF8
Definition: pg_wchar.h:232
#define pg_encoding_to_char
Definition: pg_wchar.h:630
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int pg_localeconv_r(const char *lc_monetary, const char *lc_numeric, struct lconv *output)
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:301
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void pg_localeconv_free(struct lconv *lconv)
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:13103
bool pg_is_ascii(const char *str)
Definition: string.c:132
Definition: pg_locale.c:139
char status
Definition: pg_locale.c:145
Oid collid
Definition: pg_locale.c:140
pg_locale_t locale
Definition: pg_locale.c:141
uint32 hash
Definition: pg_locale.c:144
const struct collate_methods * collate
Definition: pg_locale.h:104
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:631
#define locale_t
Definition: win32_port.h:432
void _dosmaperr(unsigned long)
Definition: win32error.c:177
#define setenv(x, y, z)
Definition: win32_port.h:545
#define setlocale(a, b)
Definition: win32_port.h:475