PostgreSQL Source Code git master
pg_locale.c File Reference
#include "postgres.h"
#include <time.h>
#include "access/htup_details.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_database.h"
#include "common/hashfn.h"
#include "common/string.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/guc_hooks.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_locale.h"
#include "utils/pg_locale_c.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "lib/simplehash.h"
Include dependency graph for pg_locale.c:

Go to the source code of this file.

Data Structures

struct  collation_cache_entry
 

Macros

#define PGLOCALE_SUPPORT_ERROR(provider)    elog(ERROR, "unsupported collprovider for %s: %c", __func__, provider)
 
#define TEXTBUFLEN   1024
 
#define MAX_L10N_DATA   80
 
#define SH_PREFIX   collation_cache
 
#define SH_ELEMENT_TYPE   collation_cache_entry
 
#define SH_KEY_TYPE   Oid
 
#define SH_KEY   collid
 
#define SH_HASH_KEY(tb, key)   murmurhash32((uint32) key)
 
#define SH_EQUAL(tb, a, b)   (a == b)
 
#define SH_GET_HASH(tb, a)   a->hash
 
#define SH_SCOPE   static inline
 
#define SH_STORE_HASH
 
#define SH_DECLARE
 
#define SH_DEFINE
 

Functions

pg_locale_t create_pg_locale_builtin (Oid collid, MemoryContext context)
 
char * get_collation_actual_version_builtin (const char *collcollate)
 
pg_locale_t create_pg_locale_icu (Oid collid, MemoryContext context)
 
pg_locale_t create_pg_locale_libc (Oid collid, MemoryContext context)
 
char * get_collation_actual_version_libc (const char *collcollate)
 
char * pg_perm_setlocale (int category, const char *locale)
 
bool check_locale (int category, const char *locale, char **canonname)
 
bool check_locale_monetary (char **newval, void **extra, GucSource source)
 
void assign_locale_monetary (const char *newval, void *extra)
 
bool check_locale_numeric (char **newval, void **extra, GucSource source)
 
void assign_locale_numeric (const char *newval, void *extra)
 
bool check_locale_time (char **newval, void **extra, GucSource source)
 
void assign_locale_time (const char *newval, void *extra)
 
bool check_locale_messages (char **newval, void **extra, GucSource source)
 
void assign_locale_messages (const char *newval, void *extra)
 
static void free_struct_lconv (struct lconv *s)
 
static bool struct_lconv_is_valid (struct lconv *s)
 
static void db_encoding_convert (int encoding, char **str)
 
struct lconv * PGLC_localeconv (void)
 
static void cache_single_string (char **dst, const char *src, int encoding)
 
void cache_locale_time (void)
 
static pg_locale_t create_pg_locale (Oid collid, MemoryContext context)
 
void init_database_collation (void)
 
pg_locale_t pg_database_locale (void)
 
pg_locale_t pg_newlocale_from_collation (Oid collid)
 
char * get_collation_actual_version (char collprovider, const char *collcollate)
 
static size_t strlower_c (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
static size_t strtitle_c (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
static size_t strupper_c (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
size_t pg_strlower (char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
size_t pg_strtitle (char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
size_t pg_strupper (char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
size_t pg_strfold (char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
size_t pg_downcase_ident (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
int pg_strcoll (const char *arg1, const char *arg2, pg_locale_t locale)
 
int pg_strncoll (const char *arg1, ssize_t len1, const char *arg2, ssize_t len2, pg_locale_t locale)
 
bool pg_strxfrm_enabled (pg_locale_t locale)
 
size_t pg_strxfrm (char *dest, const char *src, size_t destsize, pg_locale_t locale)
 
size_t pg_strnxfrm (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
bool pg_strxfrm_prefix_enabled (pg_locale_t locale)
 
size_t pg_strxfrm_prefix (char *dest, const char *src, size_t destsize, pg_locale_t locale)
 
size_t pg_strnxfrm_prefix (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
bool pg_iswdigit (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswalpha (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswalnum (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswupper (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswlower (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswgraph (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswprint (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswpunct (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswspace (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswxdigit (pg_wchar wc, pg_locale_t locale)
 
bool pg_iswcased (pg_wchar wc, pg_locale_t locale)
 
pg_wchar pg_towupper (pg_wchar wc, pg_locale_t locale)
 
pg_wchar pg_towlower (pg_wchar wc, pg_locale_t locale)
 
const char * pg_icu_unicode_version ()
 
int builtin_locale_encoding (const char *locale)
 
const char * builtin_validate_locale (int encoding, const char *locale)
 
char * icu_language_tag (const char *loc_str, int elevel)
 
void icu_validate_locale (const char *loc_str)
 

Variables

char * locale_messages
 
char * locale_monetary
 
char * locale_numeric
 
char * locale_time
 
int icu_validation_level = WARNING
 
char * localized_abbrev_days [7+1]
 
char * localized_full_days [7+1]
 
char * localized_abbrev_months [12+1]
 
char * localized_full_months [12+1]
 
static pg_locale_t default_locale = NULL
 
static bool CurrentLocaleConvValid = false
 
static bool CurrentLCTimeValid = false
 
static struct pg_locale_struct c_locale
 
static MemoryContext CollationCacheContext = NULL
 
static collation_cache_hash * CollationCache = NULL
 
static Oid last_collation_cache_oid = InvalidOid
 
static pg_locale_t last_collation_cache_locale = NULL
 

Macro Definition Documentation

◆ MAX_L10N_DATA

#define MAX_L10N_DATA   80

Definition at line 69 of file pg_locale.c.

◆ PGLOCALE_SUPPORT_ERROR

#define PGLOCALE_SUPPORT_ERROR (   provider)     elog(ERROR, "unsupported collprovider for %s: %c", __func__, provider)

Definition at line 60 of file pg_locale.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 139 of file pg_locale.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 140 of file pg_locale.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   collation_cache_entry

Definition at line 131 of file pg_locale.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)    (a == b)

Definition at line 135 of file pg_locale.c.

◆ SH_GET_HASH

#define SH_GET_HASH (   tb,
  a 
)    a->hash

Definition at line 136 of file pg_locale.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    murmurhash32((uint32) key)

Definition at line 134 of file pg_locale.c.

◆ SH_KEY

#define SH_KEY   collid

Definition at line 133 of file pg_locale.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   Oid

Definition at line 132 of file pg_locale.c.

◆ SH_PREFIX

#define SH_PREFIX   collation_cache

Definition at line 130 of file pg_locale.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 137 of file pg_locale.c.

◆ SH_STORE_HASH

#define SH_STORE_HASH

Definition at line 138 of file pg_locale.c.

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 67 of file pg_locale.c.

Function Documentation

◆ assign_locale_messages()

void assign_locale_messages ( const char *  newval,
void *  extra 
)

Definition at line 409 of file pg_locale.c.

410{
411 /*
412 * LC_MESSAGES category does not exist everywhere, but accept it anyway.
413 * We ignore failure, as per comment above.
414 */
415#ifdef LC_MESSAGES
416 (void) pg_perm_setlocale(LC_MESSAGES, newval);
417#endif
418}
#define newval
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:172

References newval, and pg_perm_setlocale().

◆ assign_locale_monetary()

void assign_locale_monetary ( const char *  newval,
void *  extra 
)

Definition at line 346 of file pg_locale.c.

347{
349}
static bool CurrentLocaleConvValid
Definition: pg_locale.c:109

References CurrentLocaleConvValid.

◆ assign_locale_numeric()

void assign_locale_numeric ( const char *  newval,
void *  extra 
)

Definition at line 358 of file pg_locale.c.

359{
361}

References CurrentLocaleConvValid.

◆ assign_locale_time()

void assign_locale_time ( const char *  newval,
void *  extra 
)

Definition at line 370 of file pg_locale.c.

371{
372 CurrentLCTimeValid = false;
373}
static bool CurrentLCTimeValid
Definition: pg_locale.c:110

References CurrentLCTimeValid.

◆ builtin_locale_encoding()

int builtin_locale_encoding ( const char *  locale)

Definition at line 1667 of file pg_locale.c.

1668{
1669 if (strcmp(locale, "C") == 0)
1670 return -1;
1671 else if (strcmp(locale, "C.UTF-8") == 0)
1672 return PG_UTF8;
1673 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1674 return PG_UTF8;
1675
1676
1677 ereport(ERROR,
1678 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1679 errmsg("invalid locale name \"%s\" for builtin provider",
1680 locale)));
1681
1682 return 0; /* keep compiler quiet */
1683}
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
static char * locale
Definition: initdb.c:140
@ PG_UTF8
Definition: pg_wchar.h:232

References ereport, errcode(), errmsg(), ERROR, locale, and PG_UTF8.

Referenced by builtin_validate_locale(), and DefineCollation().

◆ builtin_validate_locale()

const char * builtin_validate_locale ( int  encoding,
const char *  locale 
)

Definition at line 1691 of file pg_locale.c.

1692{
1693 const char *canonical_name = NULL;
1694 int required_encoding;
1695
1696 if (strcmp(locale, "C") == 0)
1697 canonical_name = "C";
1698 else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
1699 canonical_name = "C.UTF-8";
1700 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1701 canonical_name = "PG_UNICODE_FAST";
1702
1703 if (!canonical_name)
1704 ereport(ERROR,
1705 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1706 errmsg("invalid locale name \"%s\" for builtin provider",
1707 locale)));
1708
1709 required_encoding = builtin_locale_encoding(canonical_name);
1710 if (required_encoding >= 0 && encoding != required_encoding)
1711 ereport(ERROR,
1712 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1713 errmsg("encoding \"%s\" does not match locale \"%s\"",
1715
1716 return canonical_name;
1717}
int32 encoding
Definition: pg_database.h:41
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:1667
#define pg_encoding_to_char
Definition: pg_wchar.h:630

References builtin_locale_encoding(), encoding, ereport, errcode(), errmsg(), ERROR, locale, and pg_encoding_to_char.

Referenced by create_pg_locale_builtin(), createdb(), and DefineCollation().

◆ cache_locale_time()

void cache_locale_time ( void  )

Definition at line 702 of file pg_locale.c.

703{
704 char buf[(2 * 7 + 2 * 12) * MAX_L10N_DATA];
705 char *bufptr;
706 time_t timenow;
707 struct tm *timeinfo;
708 struct tm timeinfobuf;
709 bool strftimefail = false;
710 int encoding;
711 int i;
713
714 /* did we do this already? */
716 return;
717
718 elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
719
720 errno = ENOENT;
721#ifdef WIN32
722 locale = _create_locale(LC_ALL, locale_time);
723 if (locale == (locale_t) 0)
724 _dosmaperr(GetLastError());
725#else
726 locale = newlocale(LC_ALL_MASK, locale_time, (locale_t) 0);
727#endif
728 if (!locale)
730
731 /* We use times close to current time as data for strftime(). */
732 timenow = time(NULL);
733 timeinfo = gmtime_r(&timenow, &timeinfobuf);
734
735 /* Store the strftime results in MAX_L10N_DATA-sized portions of buf[] */
736 bufptr = buf;
737
738 /*
739 * MAX_L10N_DATA is sufficient buffer space for every known locale, and
740 * POSIX defines no strftime() errors. (Buffer space exhaustion is not an
741 * error.) An implementation might report errors (e.g. ENOMEM) by
742 * returning 0 (or, less plausibly, a negative value) and setting errno.
743 * Report errno just in case the implementation did that, but clear it in
744 * advance of the calls so we don't emit a stale, unrelated errno.
745 */
746 errno = 0;
747
748 /* localized days */
749 for (i = 0; i < 7; i++)
750 {
751 timeinfo->tm_wday = i;
752 if (strftime_l(bufptr, MAX_L10N_DATA, "%a", timeinfo, locale) <= 0)
753 strftimefail = true;
754 bufptr += MAX_L10N_DATA;
755 if (strftime_l(bufptr, MAX_L10N_DATA, "%A", timeinfo, locale) <= 0)
756 strftimefail = true;
757 bufptr += MAX_L10N_DATA;
758 }
759
760 /* localized months */
761 for (i = 0; i < 12; i++)
762 {
763 timeinfo->tm_mon = i;
764 timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
765 if (strftime_l(bufptr, MAX_L10N_DATA, "%b", timeinfo, locale) <= 0)
766 strftimefail = true;
767 bufptr += MAX_L10N_DATA;
768 if (strftime_l(bufptr, MAX_L10N_DATA, "%B", timeinfo, locale) <= 0)
769 strftimefail = true;
770 bufptr += MAX_L10N_DATA;
771 }
772
773#ifdef WIN32
774 _free_locale(locale);
775#else
776 freelocale(locale);
777#endif
778
779 /*
780 * At this point we've done our best to clean up, and can throw errors, or
781 * call functions that might throw errors, with a clean conscience.
782 */
783 if (strftimefail)
784 elog(ERROR, "strftime_l() failed");
785
786#ifndef WIN32
787
788 /*
789 * As in PGLC_localeconv(), we must convert strftime()'s output from the
790 * encoding implied by LC_TIME to the database encoding. If we can't
791 * identify the LC_TIME encoding, just perform encoding validation.
792 */
794 if (encoding < 0)
796
797#else
798
799 /*
800 * On Windows, strftime_win32() always returns UTF8 data, so convert from
801 * that if necessary.
802 */
804
805#endif /* WIN32 */
806
807 bufptr = buf;
808
809 /* localized days */
810 for (i = 0; i < 7; i++)
811 {
813 bufptr += MAX_L10N_DATA;
815 bufptr += MAX_L10N_DATA;
816 }
817 localized_abbrev_days[7] = NULL;
818 localized_full_days[7] = NULL;
819
820 /* localized months */
821 for (i = 0; i < 12; i++)
822 {
824 bufptr += MAX_L10N_DATA;
826 bufptr += MAX_L10N_DATA;
827 }
828 localized_abbrev_months[12] = NULL;
829 localized_full_months[12] = NULL;
830
831 CurrentLCTimeValid = true;
832}
#define DEBUG3
Definition: elog.h:28
#define elog(elevel,...)
Definition: elog.h:226
int i
Definition: isn.c:77
static struct pg_tm tm
Definition: localtime.c:104
char * localized_full_months[12+1]
Definition: pg_locale.c:104
char * locale_time
Definition: pg_locale.c:90
static void cache_single_string(char **dst, const char *src, int encoding)
Definition: pg_locale.c:679
#define MAX_L10N_DATA
Definition: pg_locale.c:69
char * localized_abbrev_months[12+1]
Definition: pg_locale.c:103
char * localized_full_days[7+1]
Definition: pg_locale.c:102
char * localized_abbrev_days[7+1]
Definition: pg_locale.c:101
void report_newlocale_failure(const char *localename)
static char buf[DEFAULT_XLOG_SEG_SIZE]
Definition: pg_test_fsync.c:71
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:301
#define locale_t
Definition: win32_port.h:429
void _dosmaperr(unsigned long)
Definition: win32error.c:177

References _dosmaperr(), buf, cache_single_string(), CurrentLCTimeValid, DEBUG3, elog, encoding, ERROR, i, locale, locale_t, locale_time, localized_abbrev_days, localized_abbrev_months, localized_full_days, localized_full_months, MAX_L10N_DATA, pg_get_encoding_from_locale(), PG_SQL_ASCII, PG_UTF8, report_newlocale_failure(), and tm.

Referenced by DCH_from_char(), and DCH_to_char().

◆ cache_single_string()

static void cache_single_string ( char **  dst,
const char *  src,
int  encoding 
)
static

Definition at line 679 of file pg_locale.c.

680{
681 char *ptr;
682 char *olddst;
683
684 /* Convert the string to the database encoding, or validate it's OK */
685 ptr = pg_any_to_server(src, strlen(src), encoding);
686
687 /* Store the string in long-lived storage, replacing any previous value */
688 olddst = *dst;
690 if (olddst)
691 pfree(olddst);
692
693 /* Might as well clean up any palloc'd conversion result, too */
694 if (ptr != src)
695 pfree(ptr);
696}
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:679
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1768
void pfree(void *pointer)
Definition: mcxt.c:1616
MemoryContext TopMemoryContext
Definition: mcxt.c:166

References encoding, MemoryContextStrdup(), pfree(), pg_any_to_server(), and TopMemoryContext.

Referenced by cache_locale_time().

◆ check_locale()

bool check_locale ( int  category,
const char *  locale,
char **  canonname 
)

Definition at line 275 of file pg_locale.c.

276{
277 char *save;
278 char *res;
279
280 /* Don't let Windows' non-ASCII locale names in. */
281 if (!pg_is_ascii(locale))
282 {
284 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
285 errmsg("locale name \"%s\" contains non-ASCII characters",
286 locale)));
287 return false;
288 }
289
290 if (canonname)
291 *canonname = NULL; /* in case of failure */
292
293 save = setlocale(category, NULL);
294 if (!save)
295 return false; /* won't happen, we hope */
296
297 /* save may be pointing at a modifiable scratch variable, see above. */
298 save = pstrdup(save);
299
300 /* set the locale with setlocale, to see if it accepts it. */
301 res = setlocale(category, locale);
302
303 /* save canonical name if requested. */
304 if (res && canonname)
305 *canonname = pstrdup(res);
306
307 /* restore old value. */
308 if (!setlocale(category, save))
309 elog(WARNING, "failed to restore old locale \"%s\"", save);
310 pfree(save);
311
312 /* Don't let Windows' non-ASCII locale names out. */
313 if (canonname && *canonname && !pg_is_ascii(*canonname))
314 {
316 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
317 errmsg("locale name \"%s\" contains non-ASCII characters",
318 *canonname)));
319 pfree(*canonname);
320 *canonname = NULL;
321 return false;
322 }
323
324 return (res != NULL);
325}
#define WARNING
Definition: elog.h:36
char * pstrdup(const char *in)
Definition: mcxt.c:1781
bool pg_is_ascii(const char *str)
Definition: string.c:132
#define setlocale(a, b)
Definition: win32_port.h:472

References elog, ereport, errcode(), errmsg(), locale, pfree(), pg_is_ascii(), pstrdup(), setlocale, and WARNING.

Referenced by check_locale_messages(), check_locale_monetary(), check_locale_numeric(), check_locale_time(), and createdb().

◆ check_locale_messages()

bool check_locale_messages ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 386 of file pg_locale.c.

387{
388 if (**newval == '\0')
389 {
390 if (source == PGC_S_DEFAULT)
391 return true;
392 else
393 return false;
394 }
395
396 /*
397 * LC_MESSAGES category does not exist everywhere, but accept it anyway
398 *
399 * On Windows, we can't even check the value, so accept blindly
400 */
401#if defined(LC_MESSAGES) && !defined(WIN32)
402 return check_locale(LC_MESSAGES, *newval, NULL);
403#else
404 return true;
405#endif
406}
@ PGC_S_DEFAULT
Definition: guc.h:113
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:275
static rewind_source * source
Definition: pg_rewind.c:89

References check_locale(), newval, PGC_S_DEFAULT, and source.

◆ check_locale_monetary()

bool check_locale_monetary ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 340 of file pg_locale.c.

341{
342 return check_locale(LC_MONETARY, *newval, NULL);
343}

References check_locale(), and newval.

◆ check_locale_numeric()

bool check_locale_numeric ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 352 of file pg_locale.c.

353{
354 return check_locale(LC_NUMERIC, *newval, NULL);
355}

References check_locale(), and newval.

◆ check_locale_time()

bool check_locale_time ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 364 of file pg_locale.c.

365{
366 return check_locale(LC_TIME, *newval, NULL);
367}

References check_locale(), and newval.

◆ create_pg_locale()

static pg_locale_t create_pg_locale ( Oid  collid,
MemoryContext  context 
)
static

Definition at line 1049 of file pg_locale.c.

1050{
1051 HeapTuple tp;
1052 Form_pg_collation collform;
1053 pg_locale_t result;
1054 Datum datum;
1055 bool isnull;
1056
1057 tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1058 if (!HeapTupleIsValid(tp))
1059 elog(ERROR, "cache lookup failed for collation %u", collid);
1060 collform = (Form_pg_collation) GETSTRUCT(tp);
1061
1062 if (collform->collprovider == COLLPROVIDER_BUILTIN)
1063 result = create_pg_locale_builtin(collid, context);
1064 else if (collform->collprovider == COLLPROVIDER_ICU)
1065 result = create_pg_locale_icu(collid, context);
1066 else if (collform->collprovider == COLLPROVIDER_LIBC)
1067 result = create_pg_locale_libc(collid, context);
1068 else
1069 /* shouldn't happen */
1070 PGLOCALE_SUPPORT_ERROR(collform->collprovider);
1071
1072 result->is_default = false;
1073
1074 Assert((result->collate_is_c && result->collate == NULL) ||
1075 (!result->collate_is_c && result->collate != NULL));
1076
1077 Assert((result->ctype_is_c && result->ctype == NULL) ||
1078 (!result->ctype_is_c && result->ctype != NULL));
1079
1080 datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collversion,
1081 &isnull);
1082 if (!isnull)
1083 {
1084 char *actual_versionstr;
1085 char *collversionstr;
1086
1087 collversionstr = TextDatumGetCString(datum);
1088
1089 if (collform->collprovider == COLLPROVIDER_LIBC)
1090 datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate);
1091 else
1092 datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale);
1093
1094 actual_versionstr = get_collation_actual_version(collform->collprovider,
1095 TextDatumGetCString(datum));
1096 if (!actual_versionstr)
1097 {
1098 /*
1099 * This could happen when specifying a version in CREATE COLLATION
1100 * but the provider does not support versioning, or manually
1101 * creating a mess in the catalogs.
1102 */
1103 ereport(ERROR,
1104 (errmsg("collation \"%s\" has no actual version, but a version was recorded",
1105 NameStr(collform->collname))));
1106 }
1107
1108 if (strcmp(actual_versionstr, collversionstr) != 0)
1110 (errmsg("collation \"%s\" has version mismatch",
1111 NameStr(collform->collname)),
1112 errdetail("The collation in the database was created using version %s, "
1113 "but the operating system provides version %s.",
1114 collversionstr, actual_versionstr),
1115 errhint("Rebuild all objects affected by this collation and run "
1116 "ALTER COLLATION %s REFRESH VERSION, "
1117 "or build PostgreSQL with the right library version.",
1118 quote_qualified_identifier(get_namespace_name(collform->collnamespace),
1119 NameStr(collform->collname)))));
1120 }
1121
1122 ReleaseSysCache(tp);
1123
1124 return result;
1125}
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define NameStr(name)
Definition: c.h:771
Oid collid
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errhint(const char *fmt,...)
Definition: elog.c:1330
Assert(PointerIsAligned(start, uint64))
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3516
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:58
pg_locale_t create_pg_locale_libc(Oid collid, MemoryContext context)
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1247
pg_locale_t create_pg_locale_builtin(Oid collid, MemoryContext context)
#define PGLOCALE_SUPPORT_ERROR(provider)
Definition: pg_locale.c:60
pg_locale_t create_pg_locale_icu(Oid collid, MemoryContext context)
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:13146
const struct ctype_methods * ctype
Definition: pg_locale.h:146
const struct collate_methods * collate
Definition: pg_locale.h:145
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:595
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:625

References Assert(), pg_locale_struct::collate, pg_locale_struct::collate_is_c, collid, create_pg_locale_builtin(), create_pg_locale_icu(), create_pg_locale_libc(), pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, elog, ereport, errdetail(), errhint(), errmsg(), ERROR, get_collation_actual_version(), get_namespace_name(), GETSTRUCT(), HeapTupleIsValid, pg_locale_struct::is_default, NameStr, ObjectIdGetDatum(), PGLOCALE_SUPPORT_ERROR, quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), TextDatumGetCString, and WARNING.

Referenced by pg_newlocale_from_collation().

◆ create_pg_locale_builtin()

pg_locale_t create_pg_locale_builtin ( Oid  collid,
MemoryContext  context 
)

Definition at line 229 of file pg_locale_builtin.c.

230{
231 const char *locstr;
232 pg_locale_t result;
233
234 if (collid == DEFAULT_COLLATION_OID)
235 {
236 HeapTuple tp;
237 Datum datum;
238
240 if (!HeapTupleIsValid(tp))
241 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
242 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
243 Anum_pg_database_datlocale);
244 locstr = TextDatumGetCString(datum);
245 ReleaseSysCache(tp);
246 }
247 else
248 {
249 HeapTuple tp;
250 Datum datum;
251
253 if (!HeapTupleIsValid(tp))
254 elog(ERROR, "cache lookup failed for collation %u", collid);
255 datum = SysCacheGetAttrNotNull(COLLOID, tp,
256 Anum_pg_collation_colllocale);
257 locstr = TextDatumGetCString(datum);
258 ReleaseSysCache(tp);
259 }
260
262
263 result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
264
265 result->builtin.locale = MemoryContextStrdup(context, locstr);
266 result->builtin.casemap_full = (strcmp(locstr, "PG_UNICODE_FAST") == 0);
267 result->deterministic = true;
268 result->collate_is_c = true;
269 result->ctype_is_c = (strcmp(locstr, "C") == 0);
270 if (!result->ctype_is_c)
271 result->ctype = &ctype_methods_builtin;
272
273 return result;
274}
Oid MyDatabaseId
Definition: globals.c:94
int GetDatabaseEncoding(void)
Definition: mbutils.c:1264
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1266
const char * builtin_validate_locale(int encoding, const char *locale)
Definition: pg_locale.c:1691
static const struct ctype_methods ctype_methods_builtin
struct pg_locale_struct::@166::@168 builtin
const char * locale
Definition: pg_locale.h:152

References pg_locale_struct::builtin, builtin_validate_locale(), pg_locale_struct::casemap_full, pg_locale_struct::collate_is_c, collid, pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, ctype_methods_builtin, pg_locale_struct::deterministic, elog, ERROR, GetDatabaseEncoding(), HeapTupleIsValid, pg_locale_struct::locale, MemoryContextAllocZero(), MemoryContextStrdup(), MyDatabaseId, ObjectIdGetDatum(), ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttrNotNull(), and TextDatumGetCString.

Referenced by create_pg_locale(), and init_database_collation().

◆ create_pg_locale_icu()

pg_locale_t create_pg_locale_icu ( Oid  collid,
MemoryContext  context 
)

Definition at line 307 of file pg_locale_icu.c.

308{
309#ifdef USE_ICU
310 bool deterministic;
311 const char *iculocstr;
312 const char *icurules = NULL;
313 UCollator *collator;
314 locale_t loc = (locale_t) 0;
315 pg_locale_t result;
316
317 if (collid == DEFAULT_COLLATION_OID)
318 {
319 HeapTuple tp;
320 Datum datum;
321 bool isnull;
322
324 if (!HeapTupleIsValid(tp))
325 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
326
327 /* default database collation is always deterministic */
328 deterministic = true;
329 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
330 Anum_pg_database_datlocale);
331 iculocstr = TextDatumGetCString(datum);
332 datum = SysCacheGetAttr(DATABASEOID, tp,
333 Anum_pg_database_daticurules, &isnull);
334 if (!isnull)
335 icurules = TextDatumGetCString(datum);
336
337 /* libc only needed for default locale and single-byte encoding */
339 {
340 const char *ctype;
341
342 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
343 Anum_pg_database_datctype);
344 ctype = TextDatumGetCString(datum);
345
346 loc = make_libc_ctype_locale(ctype);
347 }
348
349 ReleaseSysCache(tp);
350 }
351 else
352 {
353 Form_pg_collation collform;
354 HeapTuple tp;
355 Datum datum;
356 bool isnull;
357
359 if (!HeapTupleIsValid(tp))
360 elog(ERROR, "cache lookup failed for collation %u", collid);
361 collform = (Form_pg_collation) GETSTRUCT(tp);
362 deterministic = collform->collisdeterministic;
363 datum = SysCacheGetAttrNotNull(COLLOID, tp,
364 Anum_pg_collation_colllocale);
365 iculocstr = TextDatumGetCString(datum);
366 datum = SysCacheGetAttr(COLLOID, tp,
367 Anum_pg_collation_collicurules, &isnull);
368 if (!isnull)
369 icurules = TextDatumGetCString(datum);
370
371 ReleaseSysCache(tp);
372 }
373
374 collator = make_icu_collator(iculocstr, icurules);
375
376 result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
377 result->icu.locale = MemoryContextStrdup(context, iculocstr);
378 result->icu.ucol = collator;
379 result->icu.lt = loc;
380 result->deterministic = deterministic;
381 result->collate_is_c = false;
382 result->ctype_is_c = false;
384 {
385 result->icu.ucasemap = pg_ucasemap_open(iculocstr);
386 result->collate = &collate_methods_icu_utf8;
387 result->ctype = &ctype_methods_icu_utf8;
388 }
389 else
390 {
391 result->collate = &collate_methods_icu;
392 result->ctype = &ctype_methods_icu;
393 }
394
395 return result;
396#else
397 /* could get here if a collation was created by a build with ICU */
399 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
400 errmsg("ICU is not supported in this build")));
401
402 return NULL;
403#endif
404}
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1549

References pg_locale_struct::collate, pg_locale_struct::collate_is_c, collid, pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_locale_struct::deterministic, elog, ereport, errcode(), errmsg(), ERROR, GetDatabaseEncoding(), GETSTRUCT(), HeapTupleIsValid, pg_locale_struct::locale, locale_t, pg_locale_struct::lt, MemoryContextAllocZero(), MemoryContextStrdup(), MyDatabaseId, ObjectIdGetDatum(), pg_database_encoding_max_length(), PG_UTF8, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), and TextDatumGetCString.

Referenced by create_pg_locale(), and init_database_collation().

◆ create_pg_locale_libc()

pg_locale_t create_pg_locale_libc ( Oid  collid,
MemoryContext  context 
)

Definition at line 741 of file pg_locale_libc.c.

742{
743 const char *collate;
744 const char *ctype;
745 locale_t loc;
746 pg_locale_t result;
747
748 if (collid == DEFAULT_COLLATION_OID)
749 {
750 HeapTuple tp;
751 Datum datum;
752
754 if (!HeapTupleIsValid(tp))
755 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
756 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
757 Anum_pg_database_datcollate);
758 collate = TextDatumGetCString(datum);
759 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
760 Anum_pg_database_datctype);
761 ctype = TextDatumGetCString(datum);
762
763 ReleaseSysCache(tp);
764 }
765 else
766 {
767 HeapTuple tp;
768 Datum datum;
769
771 if (!HeapTupleIsValid(tp))
772 elog(ERROR, "cache lookup failed for collation %u", collid);
773
774 datum = SysCacheGetAttrNotNull(COLLOID, tp,
775 Anum_pg_collation_collcollate);
776 collate = TextDatumGetCString(datum);
777 datum = SysCacheGetAttrNotNull(COLLOID, tp,
778 Anum_pg_collation_collctype);
779 ctype = TextDatumGetCString(datum);
780
781 ReleaseSysCache(tp);
782 }
783
784
785 loc = make_libc_collator(collate, ctype);
786
787 result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
788 result->deterministic = true;
789 result->collate_is_c = (strcmp(collate, "C") == 0) ||
790 (strcmp(collate, "POSIX") == 0);
791 result->ctype_is_c = (strcmp(ctype, "C") == 0) ||
792 (strcmp(ctype, "POSIX") == 0);
793 result->lt = loc;
794 if (!result->collate_is_c)
795 {
796#ifdef WIN32
798 result->collate = &collate_methods_libc_win32_utf8;
799 else
800#endif
801 result->collate = &collate_methods_libc;
802 }
803 if (!result->ctype_is_c)
804 {
809 else
810 result->ctype = &ctype_methods_libc_sb;
811 }
812
813 return result;
814}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
static const struct ctype_methods ctype_methods_libc_other_mb
static const struct ctype_methods ctype_methods_libc_utf8
static locale_t make_libc_collator(const char *collate, const char *ctype)
static const struct collate_methods collate_methods_libc
static const struct ctype_methods ctype_methods_libc_sb

References pg_locale_struct::collate, pg_locale_struct::collate_is_c, collate_methods_libc, collid, pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, ctype_methods_libc_other_mb, ctype_methods_libc_sb, ctype_methods_libc_utf8, pg_locale_struct::deterministic, elog, ERROR, GetDatabaseEncoding(), HeapTupleIsValid, if(), locale_t, pg_locale_struct::lt, make_libc_collator(), MemoryContextAllocZero(), MyDatabaseId, ObjectIdGetDatum(), pg_database_encoding_max_length(), PG_UTF8, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttrNotNull(), and TextDatumGetCString.

Referenced by create_pg_locale(), and init_database_collation().

◆ db_encoding_convert()

static void db_encoding_convert ( int  encoding,
char **  str 
)
static

Definition at line 476 of file pg_locale.c.

477{
478 char *pstr;
479 char *mstr;
480
481 /* convert the string to the database encoding */
482 pstr = pg_any_to_server(*str, strlen(*str), encoding);
483 if (pstr == *str)
484 return; /* no conversion happened */
485
486 /* need it malloc'd not palloc'd */
487 mstr = strdup(pstr);
488 if (mstr == NULL)
490 (errcode(ERRCODE_OUT_OF_MEMORY),
491 errmsg("out of memory")));
492
493 /* replace old string */
494 free(*str);
495 *str = mstr;
496
497 pfree(pstr);
498}
const char * str
#define free(a)

References encoding, ereport, errcode(), errmsg(), ERROR, free, pfree(), pg_any_to_server(), and str.

Referenced by PGLC_localeconv().

◆ free_struct_lconv()

static void free_struct_lconv ( struct lconv *  s)
static

Definition at line 426 of file pg_locale.c.

427{
428 free(s->decimal_point);
429 free(s->thousands_sep);
430 free(s->grouping);
431 free(s->int_curr_symbol);
432 free(s->currency_symbol);
433 free(s->mon_decimal_point);
434 free(s->mon_thousands_sep);
435 free(s->mon_grouping);
436 free(s->positive_sign);
437 free(s->negative_sign);
438}

References free.

Referenced by PGLC_localeconv().

◆ get_collation_actual_version()

char * get_collation_actual_version ( char  collprovider,
const char *  collcollate 
)

Definition at line 1247 of file pg_locale.c.

1248{
1249 char *collversion = NULL;
1250
1251 if (collprovider == COLLPROVIDER_BUILTIN)
1252 collversion = get_collation_actual_version_builtin(collcollate);
1253#ifdef USE_ICU
1254 else if (collprovider == COLLPROVIDER_ICU)
1255 collversion = get_collation_actual_version_icu(collcollate);
1256#endif
1257 else if (collprovider == COLLPROVIDER_LIBC)
1258 collversion = get_collation_actual_version_libc(collcollate);
1259
1260 return collversion;
1261}
char * get_collation_actual_version_libc(const char *collcollate)
char * get_collation_actual_version_builtin(const char *collcollate)

References get_collation_actual_version_builtin(), and get_collation_actual_version_libc().

Referenced by AlterCollation(), AlterDatabaseRefreshColl(), CheckMyDatabase(), create_pg_locale(), createdb(), DefineCollation(), pg_collation_actual_version(), pg_database_collation_actual_version(), and pg_import_system_collations().

◆ get_collation_actual_version_builtin()

char * get_collation_actual_version_builtin ( const char *  collcollate)

Definition at line 277 of file pg_locale_builtin.c.

278{
279 /*
280 * The only two supported locales (C and C.UTF-8) are both based on memcmp
281 * and are not expected to change, but track the version anyway.
282 *
283 * Note that the character semantics may change for some locales, but the
284 * collation version only tracks changes to sort order.
285 */
286 if (strcmp(collcollate, "C") == 0)
287 return "1";
288 else if (strcmp(collcollate, "C.UTF-8") == 0)
289 return "1";
290 else if (strcmp(collcollate, "PG_UNICODE_FAST") == 0)
291 return "1";
292 else
294 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
295 errmsg("invalid locale name \"%s\" for builtin provider",
296 collcollate)));
297
298 return NULL; /* keep compiler quiet */
299}

References ereport, errcode(), errmsg(), and ERROR.

Referenced by get_collation_actual_version().

◆ get_collation_actual_version_libc()

char * get_collation_actual_version_libc ( const char *  collcollate)

Definition at line 983 of file pg_locale_libc.c.

984{
985 char *collversion = NULL;
986
987 if (pg_strcasecmp("C", collcollate) != 0 &&
988 pg_strncasecmp("C.", collcollate, 2) != 0 &&
989 pg_strcasecmp("POSIX", collcollate) != 0)
990 {
991#if defined(__GLIBC__)
992 /* Use the glibc version because we don't have anything better. */
993 collversion = pstrdup(gnu_get_libc_version());
994#elif defined(LC_VERSION_MASK)
995 locale_t loc;
996
997 /* Look up FreeBSD collation version. */
998 loc = newlocale(LC_COLLATE_MASK, collcollate, NULL);
999 if (loc)
1000 {
1001 collversion =
1002 pstrdup(querylocale(LC_COLLATE_MASK | LC_VERSION_MASK, loc));
1003 freelocale(loc);
1004 }
1005 else
1006 ereport(ERROR,
1007 (errmsg("could not load locale \"%s\"", collcollate)));
1008#elif defined(WIN32)
1009 /*
1010 * If we are targeting Windows Vista and above, we can ask for a name
1011 * given a collation name (earlier versions required a location code
1012 * that we don't have).
1013 */
1014 NLSVERSIONINFOEX version = {sizeof(NLSVERSIONINFOEX)};
1015 WCHAR wide_collcollate[LOCALE_NAME_MAX_LENGTH];
1016
1017 MultiByteToWideChar(CP_ACP, 0, collcollate, -1, wide_collcollate,
1018 LOCALE_NAME_MAX_LENGTH);
1019 if (!GetNLSVersionEx(COMPARE_STRING, wide_collcollate, &version))
1020 {
1021 /*
1022 * GetNLSVersionEx() wants a language tag such as "en-US", not a
1023 * locale name like "English_United States.1252". Until those
1024 * values can be prevented from entering the system, or 100%
1025 * reliably converted to the more useful tag format, tolerate the
1026 * resulting error and report that we have no version data.
1027 */
1028 if (GetLastError() == ERROR_INVALID_PARAMETER)
1029 return NULL;
1030
1031 ereport(ERROR,
1032 (errmsg("could not get collation version for locale \"%s\": error code %lu",
1033 collcollate,
1034 GetLastError())));
1035 }
1036 collversion = psprintf("%lu.%lu,%lu.%lu",
1037 (version.dwNLSVersion >> 8) & 0xFFFF,
1038 version.dwNLSVersion & 0xFF,
1039 (version.dwDefinedVersion >> 8) & 0xFFFF,
1040 version.dwDefinedVersion & 0xFF);
1041#endif
1042 }
1043
1044 return collversion;
1045}
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:32
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:65
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43

References ereport, errmsg(), ERROR, locale_t, pg_strcasecmp(), pg_strncasecmp(), psprintf(), and pstrdup().

Referenced by get_collation_actual_version().

◆ icu_language_tag()

char * icu_language_tag ( const char *  loc_str,
int  elevel 
)

Definition at line 1731 of file pg_locale.c.

1732{
1733#ifdef USE_ICU
1734 UErrorCode status;
1735 char *langtag;
1736 size_t buflen = 32; /* arbitrary starting buffer size */
1737 const bool strict = true;
1738
1739 /*
1740 * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
1741 * RFC5646 section 4.4). Additionally, in older ICU versions,
1742 * uloc_toLanguageTag() doesn't always return the ultimate length on the
1743 * first call, necessitating a loop.
1744 */
1745 langtag = palloc(buflen);
1746 while (true)
1747 {
1748 status = U_ZERO_ERROR;
1749 uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
1750
1751 /* try again if the buffer is not large enough */
1752 if ((status == U_BUFFER_OVERFLOW_ERROR ||
1753 status == U_STRING_NOT_TERMINATED_WARNING) &&
1754 buflen < MaxAllocSize)
1755 {
1756 buflen = Min(buflen * 2, MaxAllocSize);
1757 langtag = repalloc(langtag, buflen);
1758 continue;
1759 }
1760
1761 break;
1762 }
1763
1764 if (U_FAILURE(status))
1765 {
1766 pfree(langtag);
1767
1768 if (elevel > 0)
1769 ereport(elevel,
1770 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1771 errmsg("could not convert locale name \"%s\" to language tag: %s",
1772 loc_str, u_errorName(status))));
1773 return NULL;
1774 }
1775
1776 return langtag;
1777#else /* not USE_ICU */
1778 ereport(ERROR,
1779 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1780 errmsg("ICU is not supported in this build")));
1781 return NULL; /* keep compiler quiet */
1782#endif /* not USE_ICU */
1783}
#define Min(x, y)
Definition: c.h:1003
#define MaxAllocSize
Definition: fe_memutils.h:22
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1632
void * palloc(Size size)
Definition: mcxt.c:1387

References ereport, errcode(), errmsg(), ERROR, MaxAllocSize, Min, palloc(), pfree(), and repalloc().

Referenced by createdb(), DefineCollation(), and pg_import_system_collations().

◆ icu_validate_locale()

void icu_validate_locale ( const char *  loc_str)

Definition at line 1789 of file pg_locale.c.

1790{
1791#ifdef USE_ICU
1792 UCollator *collator;
1793 UErrorCode status;
1794 char lang[ULOC_LANG_CAPACITY];
1795 bool found = false;
1796 int elevel = icu_validation_level;
1797
1798 /* no validation */
1799 if (elevel < 0)
1800 return;
1801
1802 /* downgrade to WARNING during pg_upgrade */
1803 if (IsBinaryUpgrade && elevel > WARNING)
1804 elevel = WARNING;
1805
1806 /* validate that we can extract the language */
1807 status = U_ZERO_ERROR;
1808 uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
1809 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1810 {
1811 ereport(elevel,
1812 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1813 errmsg("could not get language from ICU locale \"%s\": %s",
1814 loc_str, u_errorName(status)),
1815 errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1816 "icu_validation_level", "disabled")));
1817 return;
1818 }
1819
1820 /* check for special language name */
1821 if (strcmp(lang, "") == 0 ||
1822 strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
1823 found = true;
1824
1825 /* search for matching language within ICU */
1826 for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
1827 {
1828 const char *otherloc = uloc_getAvailable(i);
1829 char otherlang[ULOC_LANG_CAPACITY];
1830
1831 status = U_ZERO_ERROR;
1832 uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
1833 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1834 continue;
1835
1836 if (strcmp(lang, otherlang) == 0)
1837 found = true;
1838 }
1839
1840 if (!found)
1841 ereport(elevel,
1842 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1843 errmsg("ICU locale \"%s\" has unknown language \"%s\"",
1844 loc_str, lang),
1845 errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1846 "icu_validation_level", "disabled")));
1847
1848 /* check that it can be opened */
1849 collator = pg_ucol_open(loc_str);
1850 ucol_close(collator);
1851#else /* not USE_ICU */
1852 /* could get here if a collation was created by a build with ICU */
1853 ereport(ERROR,
1854 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1855 errmsg("ICU is not supported in this build")));
1856#endif /* not USE_ICU */
1857}
bool IsBinaryUpgrade
Definition: globals.c:121
int icu_validation_level
Definition: pg_locale.c:92

References ereport, errcode(), errhint(), errmsg(), ERROR, i, icu_validation_level, IsBinaryUpgrade, and WARNING.

Referenced by createdb(), and DefineCollation().

◆ init_database_collation()

void init_database_collation ( void  )

Definition at line 1131 of file pg_locale.c.

1132{
1133 HeapTuple tup;
1134 Form_pg_database dbform;
1135 pg_locale_t result;
1136
1137 Assert(default_locale == NULL);
1138
1139 /* Fetch our pg_database row normally, via syscache */
1140 tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1141 if (!HeapTupleIsValid(tup))
1142 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1143 dbform = (Form_pg_database) GETSTRUCT(tup);
1144
1145 if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
1146 result = create_pg_locale_builtin(DEFAULT_COLLATION_OID,
1148 else if (dbform->datlocprovider == COLLPROVIDER_ICU)
1149 result = create_pg_locale_icu(DEFAULT_COLLATION_OID,
1151 else if (dbform->datlocprovider == COLLPROVIDER_LIBC)
1152 result = create_pg_locale_libc(DEFAULT_COLLATION_OID,
1154 else
1155 /* shouldn't happen */
1156 PGLOCALE_SUPPORT_ERROR(dbform->datlocprovider);
1157
1158 result->is_default = true;
1159
1160 Assert((result->collate_is_c && result->collate == NULL) ||
1161 (!result->collate_is_c && result->collate != NULL));
1162
1163 Assert((result->ctype_is_c && result->ctype == NULL) ||
1164 (!result->ctype_is_c && result->ctype != NULL));
1165
1166 ReleaseSysCache(tup);
1167
1168 default_locale = result;
1169}
FormData_pg_database * Form_pg_database
Definition: pg_database.h:96
static pg_locale_t default_locale
Definition: pg_locale.c:106

References Assert(), pg_locale_struct::collate, pg_locale_struct::collate_is_c, create_pg_locale_builtin(), create_pg_locale_icu(), create_pg_locale_libc(), pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, default_locale, elog, ERROR, GETSTRUCT(), HeapTupleIsValid, pg_locale_struct::is_default, MyDatabaseId, ObjectIdGetDatum(), PGLOCALE_SUPPORT_ERROR, ReleaseSysCache(), SearchSysCache1(), and TopMemoryContext.

Referenced by CheckMyDatabase().

◆ pg_database_locale()

pg_locale_t pg_database_locale ( void  )

Definition at line 1175 of file pg_locale.c.

1176{
1177 return pg_newlocale_from_collation(DEFAULT_COLLATION_OID);
1178}
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1189

References pg_newlocale_from_collation().

Referenced by ltree_crc32_sz(), ltree_prefix_eq_ci(), t_isalnum(), and t_isalpha().

◆ pg_downcase_ident()

size_t pg_downcase_ident ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)

Definition at line 1366 of file pg_locale.c.

1367{
1369
1370 if (locale == NULL || locale->ctype == NULL ||
1371 locale->ctype->downcase_ident == NULL)
1372 return strlower_c(dst, dstsize, src, srclen);
1373 else
1374 return locale->ctype->downcase_ident(dst, dstsize, src, srclen,
1375 locale);
1376}
static size_t strlower_c(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: pg_locale.c:1265

References default_locale, locale, and strlower_c().

Referenced by downcase_identifier().

◆ pg_icu_unicode_version()

const char * pg_icu_unicode_version ( void  )

Definition at line 1653 of file pg_locale.c.

1654{
1655#ifdef USE_ICU
1656 return U_UNICODE_VERSION;
1657#else
1658 return NULL;
1659#endif
1660}

Referenced by icu_unicode_version().

◆ pg_iswalnum()

bool pg_iswalnum ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1533 of file pg_locale.c.

1534{
1535 if (locale->ctype == NULL)
1536 return (wc <= (pg_wchar) 127 &&
1538 else
1539 return locale->ctype->wc_isalnum(wc, locale);
1540}
unsigned int pg_wchar
Definition: mbprint.c:31
#define PG_ISALNUM
Definition: pg_locale_c.h:21
static const unsigned char pg_char_properties[128]
Definition: pg_locale_c.h:29

References locale, pg_char_properties, and PG_ISALNUM.

Referenced by t_isalnum().

◆ pg_iswalpha()

bool pg_iswalpha ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1523 of file pg_locale.c.

1524{
1525 if (locale->ctype == NULL)
1526 return (wc <= (pg_wchar) 127 &&
1528 else
1529 return locale->ctype->wc_isalpha(wc, locale);
1530}
#define PG_ISALPHA
Definition: pg_locale_c.h:20

References locale, pg_char_properties, and PG_ISALPHA.

Referenced by t_isalpha().

◆ pg_iswcased()

bool pg_iswcased ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1615 of file pg_locale.c.

1616{
1617 /* for the C locale, Cased and Alpha are equivalent */
1618 if (locale->ctype == NULL)
1619 return (wc <= (pg_wchar) 127 &&
1621 else
1622 return locale->ctype->wc_iscased(wc, locale);
1623}

References locale, pg_char_properties, and PG_ISALPHA.

Referenced by like_fixed_prefix_ci().

◆ pg_iswdigit()

bool pg_iswdigit ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1513 of file pg_locale.c.

1514{
1515 if (locale->ctype == NULL)
1516 return (wc <= (pg_wchar) 127 &&
1518 else
1519 return locale->ctype->wc_isdigit(wc, locale);
1520}
#define PG_ISDIGIT
Definition: pg_locale_c.h:19

References locale, pg_char_properties, and PG_ISDIGIT.

◆ pg_iswgraph()

bool pg_iswgraph ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1563 of file pg_locale.c.

1564{
1565 if (locale->ctype == NULL)
1566 return (wc <= (pg_wchar) 127 &&
1568 else
1569 return locale->ctype->wc_isgraph(wc, locale);
1570}
#define PG_ISGRAPH
Definition: pg_locale_c.h:24

References locale, pg_char_properties, and PG_ISGRAPH.

◆ pg_iswlower()

bool pg_iswlower ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1553 of file pg_locale.c.

1554{
1555 if (locale->ctype == NULL)
1556 return (wc <= (pg_wchar) 127 &&
1558 else
1559 return locale->ctype->wc_islower(wc, locale);
1560}
#define PG_ISLOWER
Definition: pg_locale_c.h:23

References locale, pg_char_properties, and PG_ISLOWER.

◆ pg_iswprint()

bool pg_iswprint ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1573 of file pg_locale.c.

1574{
1575 if (locale->ctype == NULL)
1576 return (wc <= (pg_wchar) 127 &&
1578 else
1579 return locale->ctype->wc_isprint(wc, locale);
1580}
#define PG_ISPRINT
Definition: pg_locale_c.h:25

References locale, pg_char_properties, and PG_ISPRINT.

◆ pg_iswpunct()

bool pg_iswpunct ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1583 of file pg_locale.c.

1584{
1585 if (locale->ctype == NULL)
1586 return (wc <= (pg_wchar) 127 &&
1588 else
1589 return locale->ctype->wc_ispunct(wc, locale);
1590}
#define PG_ISPUNCT
Definition: pg_locale_c.h:26

References locale, pg_char_properties, and PG_ISPUNCT.

◆ pg_iswspace()

bool pg_iswspace ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1593 of file pg_locale.c.

1594{
1595 if (locale->ctype == NULL)
1596 return (wc <= (pg_wchar) 127 &&
1598 else
1599 return locale->ctype->wc_isspace(wc, locale);
1600}
#define PG_ISSPACE
Definition: pg_locale_c.h:27

References locale, pg_char_properties, and PG_ISSPACE.

◆ pg_iswupper()

bool pg_iswupper ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1543 of file pg_locale.c.

1544{
1545 if (locale->ctype == NULL)
1546 return (wc <= (pg_wchar) 127 &&
1548 else
1549 return locale->ctype->wc_isupper(wc, locale);
1550}
#define PG_ISUPPER
Definition: pg_locale_c.h:22

References locale, pg_char_properties, and PG_ISUPPER.

◆ pg_iswxdigit()

bool pg_iswxdigit ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1603 of file pg_locale.c.

1604{
1605 if (locale->ctype == NULL)
1606 return (wc <= (pg_wchar) 127 &&
1607 ((pg_char_properties[wc] & PG_ISDIGIT) ||
1608 ((wc >= 'A' && wc <= 'F') ||
1609 (wc >= 'a' && wc <= 'f'))));
1610 else
1611 return locale->ctype->wc_isxdigit(wc, locale);
1612}

References locale, pg_char_properties, and PG_ISDIGIT.

◆ pg_newlocale_from_collation()

pg_locale_t pg_newlocale_from_collation ( Oid  collid)

Definition at line 1189 of file pg_locale.c.

1190{
1191 collation_cache_entry *cache_entry;
1192 bool found;
1193
1194 if (collid == DEFAULT_COLLATION_OID)
1195 return default_locale;
1196
1197 /*
1198 * Some callers expect C_COLLATION_OID to succeed even without catalog
1199 * access.
1200 */
1201 if (collid == C_COLLATION_OID)
1202 return &c_locale;
1203
1204 if (!OidIsValid(collid))
1205 elog(ERROR, "cache lookup failed for collation %u", collid);
1206
1208
1211
1212 if (CollationCache == NULL)
1213 {
1215 "collation cache",
1217 CollationCache = collation_cache_create(CollationCacheContext,
1218 16, NULL);
1219 }
1220
1221 cache_entry = collation_cache_insert(CollationCache, collid, &found);
1222 if (!found)
1223 {
1224 /*
1225 * Make sure cache entry is marked invalid, in case we fail before
1226 * setting things.
1227 */
1228 cache_entry->locale = NULL;
1229 }
1230
1231 if (cache_entry->locale == NULL)
1232 {
1234 }
1235
1237 last_collation_cache_locale = cache_entry->locale;
1238
1239 return cache_entry->locale;
1240}
#define OidIsValid(objectId)
Definition: c.h:794
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static pg_locale_t last_collation_cache_locale
Definition: pg_locale.c:151
static struct pg_locale_struct c_locale
Definition: pg_locale.c:112
static pg_locale_t create_pg_locale(Oid collid, MemoryContext context)
Definition: pg_locale.c:1049
static MemoryContext CollationCacheContext
Definition: pg_locale.c:143
static collation_cache_hash * CollationCache
Definition: pg_locale.c:144
static Oid last_collation_cache_oid
Definition: pg_locale.c:150
static void AssertCouldGetRelation(void)
Definition: relcache.h:44
Definition: pg_locale.c:121
pg_locale_t locale
Definition: pg_locale.c:123

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AssertCouldGetRelation(), c_locale, CollationCache, CollationCacheContext, collid, create_pg_locale(), default_locale, elog, ERROR, last_collation_cache_locale, last_collation_cache_oid, collation_cache_entry::locale, OidIsValid, and TopMemoryContext.

Referenced by bpchareq(), bpcharne(), btvarstrequalimage(), convert_string_datum(), DefineCollation(), Generic_Text_IC_like(), GenericMatchText(), hashbpchar(), hashbpcharextended(), hashtext(), hashtextextended(), like_fixed_prefix_ci(), make_greater_string(), match_pattern_prefix(), pg_database_locale(), pg_set_regex_collation(), spg_text_inner_consistent(), str_casefold(), str_initcap(), str_tolower(), str_toupper(), text_position(), text_position_setup(), text_starts_with(), texteq(), textne(), varstr_cmp(), and varstr_sortsupport().

◆ pg_perm_setlocale()

char * pg_perm_setlocale ( int  category,
const char *  locale 
)

Definition at line 172 of file pg_locale.c.

173{
174 char *result;
175 const char *envvar;
176
177#ifndef WIN32
178 result = setlocale(category, locale);
179#else
180
181 /*
182 * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
183 * the given value is good and set it in the environment variables. We
184 * must ignore attempts to set to "", which means "keep using the old
185 * environment value".
186 */
187#ifdef LC_MESSAGES
188 if (category == LC_MESSAGES)
189 {
190 result = (char *) locale;
191 if (locale == NULL || locale[0] == '\0')
192 return result;
193 }
194 else
195#endif
196 result = setlocale(category, locale);
197#endif /* WIN32 */
198
199 if (result == NULL)
200 return result; /* fall out immediately on failure */
201
202 /*
203 * Use the right encoding in translated messages. Under ENABLE_NLS, let
204 * pg_bind_textdomain_codeset() figure it out. Under !ENABLE_NLS, message
205 * format strings are ASCII, but database-encoding strings may enter the
206 * message via %s. This makes the overall message encoding equal to the
207 * database encoding.
208 */
209 if (category == LC_CTYPE)
210 {
211 static char save_lc_ctype[LOCALE_NAME_BUFLEN];
212
213 /* copy setlocale() return value before callee invokes it again */
214 strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
215 result = save_lc_ctype;
216
217#ifdef ENABLE_NLS
218 SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
219#else
221#endif
222 }
223
224 switch (category)
225 {
226 case LC_COLLATE:
227 envvar = "LC_COLLATE";
228 break;
229 case LC_CTYPE:
230 envvar = "LC_CTYPE";
231 break;
232#ifdef LC_MESSAGES
233 case LC_MESSAGES:
234 envvar = "LC_MESSAGES";
235#ifdef WIN32
236 result = IsoLocaleName(locale);
237 if (result == NULL)
238 result = (char *) locale;
239 elog(DEBUG3, "IsoLocaleName() executed; locale: \"%s\"", result);
240#endif /* WIN32 */
241 break;
242#endif /* LC_MESSAGES */
243 case LC_MONETARY:
244 envvar = "LC_MONETARY";
245 break;
246 case LC_NUMERIC:
247 envvar = "LC_NUMERIC";
248 break;
249 case LC_TIME:
250 envvar = "LC_TIME";
251 break;
252 default:
253 elog(FATAL, "unrecognized LC category: %d", category);
254 return NULL; /* keep compiler quiet */
255 }
256
257 if (setenv(envvar, result, 1) != 0)
258 return NULL;
259
260 return result;
261}
#define FATAL
Definition: elog.h:41
void SetMessageEncoding(int encoding)
Definition: mbutils.c:1174
#define LOCALE_NAME_BUFLEN
Definition: pg_locale.h:18
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define setenv(x, y, z)
Definition: win32_port.h:542

References DEBUG3, elog, FATAL, GetDatabaseEncoding(), locale, LOCALE_NAME_BUFLEN, setenv, setlocale, SetMessageEncoding(), and strlcpy().

Referenced by assign_locale_messages(), CheckMyDatabase(), and init_locale().

◆ pg_strcoll()

int pg_strcoll ( const char *  arg1,
const char *  arg2,
pg_locale_t  locale 
)

Definition at line 1384 of file pg_locale.c.

1385{
1386 return locale->collate->strncoll(arg1, -1, arg2, -1, locale);
1387}

References locale.

Referenced by varstrfastcmp_locale().

◆ pg_strfold()

size_t pg_strfold ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1348 of file pg_locale.c.

1350{
1351 /* in the C locale, casefolding is the same as lowercasing */
1352 if (locale->ctype == NULL)
1353 return strlower_c(dst, dstsize, src, srclen);
1354 else
1355 return locale->ctype->strfold(dst, dstsize, src, srclen, locale);
1356}

References locale, and strlower_c().

Referenced by ltree_crc32_sz(), ltree_prefix_eq_ci(), and str_casefold().

◆ pg_strlower()

size_t pg_strlower ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1318 of file pg_locale.c.

1320{
1321 if (locale->ctype == NULL)
1322 return strlower_c(dst, dstsize, src, srclen);
1323 else
1324 return locale->ctype->strlower(dst, dstsize, src, srclen, locale);
1325}

References locale, and strlower_c().

Referenced by str_tolower().

◆ pg_strncoll()

int pg_strncoll ( const char *  arg1,
ssize_t  len1,
const char *  arg2,
ssize_t  len2,
pg_locale_t  locale 
)

Definition at line 1404 of file pg_locale.c.

1406{
1407 return locale->collate->strncoll(arg1, len1, arg2, len2, locale);
1408}

References locale.

Referenced by MatchText(), text_position_next_internal(), and varstr_cmp().

◆ pg_strnxfrm()

size_t pg_strnxfrm ( char *  dest,
size_t  destsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1459 of file pg_locale.c.

1461{
1462 return locale->collate->strnxfrm(dest, destsize, src, srclen, locale);
1463}

References generate_unaccent_rules::dest, and locale.

Referenced by hashbpchar(), hashbpcharextended(), hashtext(), and hashtextextended().

◆ pg_strnxfrm_prefix()

size_t pg_strnxfrm_prefix ( char *  dest,
size_t  destsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1506 of file pg_locale.c.

1508{
1509 return locale->collate->strnxfrm_prefix(dest, destsize, src, srclen, locale);
1510}

References generate_unaccent_rules::dest, and locale.

◆ pg_strtitle()

size_t pg_strtitle ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1328 of file pg_locale.c.

1330{
1331 if (locale->ctype == NULL)
1332 return strtitle_c(dst, dstsize, src, srclen);
1333 else
1334 return locale->ctype->strtitle(dst, dstsize, src, srclen, locale);
1335}
static size_t strtitle_c(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: pg_locale.c:1279

References locale, and strtitle_c().

Referenced by str_initcap().

◆ pg_strupper()

size_t pg_strupper ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1338 of file pg_locale.c.

1340{
1341 if (locale->ctype == NULL)
1342 return strupper_c(dst, dstsize, src, srclen);
1343 else
1344 return locale->ctype->strupper(dst, dstsize, src, srclen, locale);
1345}
static size_t strupper_c(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: pg_locale.c:1305

References locale, and strupper_c().

Referenced by str_toupper().

◆ pg_strxfrm()

size_t pg_strxfrm ( char *  dest,
const char *  src,
size_t  destsize,
pg_locale_t  locale 
)

Definition at line 1434 of file pg_locale.c.

1435{
1436 return locale->collate->strnxfrm(dest, destsize, src, -1, locale);
1437}

References generate_unaccent_rules::dest, and locale.

Referenced by convert_string_datum(), and varstr_abbrev_convert().

◆ pg_strxfrm_enabled()

bool pg_strxfrm_enabled ( pg_locale_t  locale)

Definition at line 1418 of file pg_locale.c.

1419{
1420 /*
1421 * locale->collate->strnxfrm is still a required method, even if it may
1422 * have the wrong behavior, because the planner uses it for estimates in
1423 * some cases.
1424 */
1425 return locale->collate->strxfrm_is_safe;
1426}

References locale.

Referenced by varstr_sortsupport().

◆ pg_strxfrm_prefix()

size_t pg_strxfrm_prefix ( char *  dest,
const char *  src,
size_t  destsize,
pg_locale_t  locale 
)

Definition at line 1481 of file pg_locale.c.

1483{
1484 return locale->collate->strnxfrm_prefix(dest, destsize, src, -1, locale);
1485}

References generate_unaccent_rules::dest, and locale.

Referenced by varstr_abbrev_convert().

◆ pg_strxfrm_prefix_enabled()

bool pg_strxfrm_prefix_enabled ( pg_locale_t  locale)

Definition at line 1470 of file pg_locale.c.

1471{
1472 return (locale->collate->strnxfrm_prefix != NULL);
1473}

References locale.

Referenced by varstr_abbrev_convert().

◆ pg_towlower()

pg_wchar pg_towlower ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1639 of file pg_locale.c.

1640{
1641 if (locale->ctype == NULL)
1642 {
1643 if (wc <= (pg_wchar) 127)
1644 return pg_ascii_tolower((unsigned char) wc);
1645 return wc;
1646 }
1647 else
1648 return locale->ctype->wc_tolower(wc, locale);
1649}
static unsigned char pg_ascii_tolower(unsigned char ch)
Definition: port.h:188

References locale, and pg_ascii_tolower().

◆ pg_towupper()

pg_wchar pg_towupper ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1626 of file pg_locale.c.

1627{
1628 if (locale->ctype == NULL)
1629 {
1630 if (wc <= (pg_wchar) 127)
1631 return pg_ascii_toupper((unsigned char) wc);
1632 return wc;
1633 }
1634 else
1635 return locale->ctype->wc_toupper(wc, locale);
1636}
static unsigned char pg_ascii_toupper(unsigned char ch)
Definition: port.h:177

References locale, and pg_ascii_toupper().

◆ PGLC_localeconv()

struct lconv * PGLC_localeconv ( void  )

Definition at line 506 of file pg_locale.c.

507{
508 static struct lconv CurrentLocaleConv;
509 static bool CurrentLocaleConvAllocated = false;
510 struct lconv *extlconv;
511 struct lconv tmp;
512 struct lconv worklconv = {0};
513
514 /* Did we do it already? */
516 return &CurrentLocaleConv;
517
518 /* Free any already-allocated storage */
519 if (CurrentLocaleConvAllocated)
520 {
521 free_struct_lconv(&CurrentLocaleConv);
522 CurrentLocaleConvAllocated = false;
523 }
524
525 /*
526 * Use thread-safe method of obtaining a copy of lconv from the operating
527 * system.
528 */
531 &tmp) != 0)
532 elog(ERROR,
533 "could not get lconv for LC_MONETARY = \"%s\", LC_NUMERIC = \"%s\": %m",
535
536 /* Must copy data now so we can re-encode it. */
537 extlconv = &tmp;
538 worklconv.decimal_point = strdup(extlconv->decimal_point);
539 worklconv.thousands_sep = strdup(extlconv->thousands_sep);
540 worklconv.grouping = strdup(extlconv->grouping);
541 worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
542 worklconv.currency_symbol = strdup(extlconv->currency_symbol);
543 worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
544 worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
545 worklconv.mon_grouping = strdup(extlconv->mon_grouping);
546 worklconv.positive_sign = strdup(extlconv->positive_sign);
547 worklconv.negative_sign = strdup(extlconv->negative_sign);
548 /* Copy scalar fields as well */
549 worklconv.int_frac_digits = extlconv->int_frac_digits;
550 worklconv.frac_digits = extlconv->frac_digits;
551 worklconv.p_cs_precedes = extlconv->p_cs_precedes;
552 worklconv.p_sep_by_space = extlconv->p_sep_by_space;
553 worklconv.n_cs_precedes = extlconv->n_cs_precedes;
554 worklconv.n_sep_by_space = extlconv->n_sep_by_space;
555 worklconv.p_sign_posn = extlconv->p_sign_posn;
556 worklconv.n_sign_posn = extlconv->n_sign_posn;
557
558 /* Free the contents of the object populated by pg_localeconv_r(). */
559 pg_localeconv_free(&tmp);
560
561 /* If any of the preceding strdup calls failed, complain now. */
562 if (!struct_lconv_is_valid(&worklconv))
564 (errcode(ERRCODE_OUT_OF_MEMORY),
565 errmsg("out of memory")));
566
567 PG_TRY();
568 {
569 int encoding;
570
571 /*
572 * Now we must perform encoding conversion from whatever's associated
573 * with the locales into the database encoding. If we can't identify
574 * the encoding implied by LC_NUMERIC or LC_MONETARY (ie we get -1),
575 * use PG_SQL_ASCII, which will result in just validating that the
576 * strings are OK in the database encoding.
577 */
579 if (encoding < 0)
581
582 db_encoding_convert(encoding, &worklconv.decimal_point);
583 db_encoding_convert(encoding, &worklconv.thousands_sep);
584 /* grouping is not text and does not require conversion */
585
587 if (encoding < 0)
589
590 db_encoding_convert(encoding, &worklconv.int_curr_symbol);
591 db_encoding_convert(encoding, &worklconv.currency_symbol);
592 db_encoding_convert(encoding, &worklconv.mon_decimal_point);
593 db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
594 /* mon_grouping is not text and does not require conversion */
595 db_encoding_convert(encoding, &worklconv.positive_sign);
596 db_encoding_convert(encoding, &worklconv.negative_sign);
597 }
598 PG_CATCH();
599 {
600 free_struct_lconv(&worklconv);
601 PG_RE_THROW();
602 }
603 PG_END_TRY();
604
605 /*
606 * Everything is good, so save the results.
607 */
608 CurrentLocaleConv = worklconv;
609 CurrentLocaleConvAllocated = true;
611 return &CurrentLocaleConv;
612}
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define PG_CATCH(...)
Definition: elog.h:382
char * locale_numeric
Definition: pg_locale.c:89
static void db_encoding_convert(int encoding, char **str)
Definition: pg_locale.c:476
static void free_struct_lconv(struct lconv *s)
Definition: pg_locale.c:426
static bool struct_lconv_is_valid(struct lconv *s)
Definition: pg_locale.c:445
char * locale_monetary
Definition: pg_locale.c:88
int pg_localeconv_r(const char *lc_monetary, const char *lc_numeric, struct lconv *output)
void pg_localeconv_free(struct lconv *lconv)

References CurrentLocaleConvValid, db_encoding_convert(), elog, encoding, ereport, errcode(), errmsg(), ERROR, free_struct_lconv(), locale_monetary, locale_numeric, PG_CATCH, PG_END_TRY, pg_get_encoding_from_locale(), pg_localeconv_free(), pg_localeconv_r(), PG_RE_THROW, PG_SQL_ASCII, PG_TRY, and struct_lconv_is_valid().

Referenced by cash_in(), cash_numeric(), cash_out(), int4_cash(), int8_cash(), NUM_prepare_locale(), and numeric_cash().

◆ strlower_c()

static size_t strlower_c ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)
static

Definition at line 1265 of file pg_locale.c.

1266{
1267 int i;
1268
1269 srclen = (srclen >= 0) ? srclen : strlen(src);
1270 for (i = 0; i < srclen && i < dstsize; i++)
1271 dst[i] = pg_ascii_tolower(src[i]);
1272 if (i < dstsize)
1273 dst[i] = '\0';
1274 return srclen;
1275}

References i, and pg_ascii_tolower().

Referenced by pg_downcase_ident(), pg_strfold(), and pg_strlower().

◆ strtitle_c()

static size_t strtitle_c ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)
static

Definition at line 1279 of file pg_locale.c.

1280{
1281 bool wasalnum = false;
1282 int i;
1283
1284 srclen = (srclen >= 0) ? srclen : strlen(src);
1285 for (i = 0; i < srclen && i < dstsize; i++)
1286 {
1287 char c = src[i];
1288
1289 if (wasalnum)
1290 dst[i] = pg_ascii_tolower(c);
1291 else
1292 dst[i] = pg_ascii_toupper(c);
1293
1294 wasalnum = ((c >= '0' && c <= '9') ||
1295 (c >= 'A' && c <= 'Z') ||
1296 (c >= 'a' && c <= 'z'));
1297 }
1298 if (i < dstsize)
1299 dst[i] = '\0';
1300 return srclen;
1301}
char * c

References i, pg_ascii_tolower(), and pg_ascii_toupper().

Referenced by pg_strtitle().

◆ struct_lconv_is_valid()

static bool struct_lconv_is_valid ( struct lconv *  s)
static

Definition at line 445 of file pg_locale.c.

446{
447 if (s->decimal_point == NULL)
448 return false;
449 if (s->thousands_sep == NULL)
450 return false;
451 if (s->grouping == NULL)
452 return false;
453 if (s->int_curr_symbol == NULL)
454 return false;
455 if (s->currency_symbol == NULL)
456 return false;
457 if (s->mon_decimal_point == NULL)
458 return false;
459 if (s->mon_thousands_sep == NULL)
460 return false;
461 if (s->mon_grouping == NULL)
462 return false;
463 if (s->positive_sign == NULL)
464 return false;
465 if (s->negative_sign == NULL)
466 return false;
467 return true;
468}

Referenced by PGLC_localeconv().

◆ strupper_c()

static size_t strupper_c ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)
static

Definition at line 1305 of file pg_locale.c.

1306{
1307 int i;
1308
1309 srclen = (srclen >= 0) ? srclen : strlen(src);
1310 for (i = 0; i < srclen && i < dstsize; i++)
1311 dst[i] = pg_ascii_toupper(src[i]);
1312 if (i < dstsize)
1313 dst[i] = '\0';
1314 return srclen;
1315}

References i, and pg_ascii_toupper().

Referenced by pg_strupper().

Variable Documentation

◆ c_locale

struct pg_locale_struct c_locale
static
Initial value:
= {
.deterministic = true,
.collate_is_c = true,
.ctype_is_c = true,
}

Definition at line 112 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ CollationCache

collation_cache_hash* CollationCache = NULL
static

Definition at line 144 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ CollationCacheContext

MemoryContext CollationCacheContext = NULL
static

Definition at line 143 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ CurrentLCTimeValid

bool CurrentLCTimeValid = false
static

Definition at line 110 of file pg_locale.c.

Referenced by assign_locale_time(), and cache_locale_time().

◆ CurrentLocaleConvValid

bool CurrentLocaleConvValid = false
static

Definition at line 109 of file pg_locale.c.

Referenced by assign_locale_monetary(), assign_locale_numeric(), and PGLC_localeconv().

◆ default_locale

pg_locale_t default_locale = NULL
static

◆ icu_validation_level

int icu_validation_level = WARNING

Definition at line 92 of file pg_locale.c.

Referenced by createdb(), DefineCollation(), and icu_validate_locale().

◆ last_collation_cache_locale

pg_locale_t last_collation_cache_locale = NULL
static

Definition at line 151 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ last_collation_cache_oid

Oid last_collation_cache_oid = InvalidOid
static

Definition at line 150 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ locale_messages

char* locale_messages

Definition at line 87 of file pg_locale.c.

◆ locale_monetary

char* locale_monetary

Definition at line 88 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_numeric

char* locale_numeric

Definition at line 89 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_time

char* locale_time

Definition at line 90 of file pg_locale.c.

Referenced by cache_locale_time().

◆ localized_abbrev_days

char* localized_abbrev_days[7+1]

Definition at line 101 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_abbrev_months

char* localized_abbrev_months[12+1]

Definition at line 103 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_full_days

char* localized_full_days[7+1]

Definition at line 102 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_full_months

char* localized_full_months[12+1]

Definition at line 104 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().