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)
 
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)
 
pg_wchar pg_towupper (pg_wchar wc, pg_locale_t locale)
 
pg_wchar pg_towlower (pg_wchar wc, pg_locale_t locale)
 
bool char_is_cased (char ch, pg_locale_t locale)
 
bool char_tolower_enabled (pg_locale_t locale)
 
char char_tolower (unsigned char ch, pg_locale_t locale)
 
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 66 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 57 of file pg_locale.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 136 of file pg_locale.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 137 of file pg_locale.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   collation_cache_entry

Definition at line 128 of file pg_locale.c.

◆ SH_EQUAL

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

Definition at line 132 of file pg_locale.c.

◆ SH_GET_HASH

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

Definition at line 133 of file pg_locale.c.

◆ SH_HASH_KEY

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

Definition at line 131 of file pg_locale.c.

◆ SH_KEY

#define SH_KEY   collid

Definition at line 130 of file pg_locale.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   Oid

Definition at line 129 of file pg_locale.c.

◆ SH_PREFIX

#define SH_PREFIX   collation_cache

Definition at line 127 of file pg_locale.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 134 of file pg_locale.c.

◆ SH_STORE_HASH

#define SH_STORE_HASH

Definition at line 135 of file pg_locale.c.

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 64 of file pg_locale.c.

Function Documentation

◆ assign_locale_messages()

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

Definition at line 406 of file pg_locale.c.

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

References newval, and pg_perm_setlocale().

◆ assign_locale_monetary()

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

Definition at line 343 of file pg_locale.c.

344{
346}
static bool CurrentLocaleConvValid
Definition: pg_locale.c:106

References CurrentLocaleConvValid.

◆ assign_locale_numeric()

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

Definition at line 355 of file pg_locale.c.

356{
358}

References CurrentLocaleConvValid.

◆ assign_locale_time()

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

Definition at line 367 of file pg_locale.c.

368{
369 CurrentLCTimeValid = false;
370}
static bool CurrentLCTimeValid
Definition: pg_locale.c:107

References CurrentLCTimeValid.

◆ builtin_locale_encoding()

int builtin_locale_encoding ( const char *  locale)

Definition at line 1663 of file pg_locale.c.

1664{
1665 if (strcmp(locale, "C") == 0)
1666 return -1;
1667 else if (strcmp(locale, "C.UTF-8") == 0)
1668 return PG_UTF8;
1669 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1670 return PG_UTF8;
1671
1672
1673 ereport(ERROR,
1674 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1675 errmsg("invalid locale name \"%s\" for builtin provider",
1676 locale)));
1677
1678 return 0; /* keep compiler quiet */
1679}
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 1687 of file pg_locale.c.

1688{
1689 const char *canonical_name = NULL;
1690 int required_encoding;
1691
1692 if (strcmp(locale, "C") == 0)
1693 canonical_name = "C";
1694 else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
1695 canonical_name = "C.UTF-8";
1696 else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1697 canonical_name = "PG_UNICODE_FAST";
1698
1699 if (!canonical_name)
1700 ereport(ERROR,
1701 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1702 errmsg("invalid locale name \"%s\" for builtin provider",
1703 locale)));
1704
1705 required_encoding = builtin_locale_encoding(canonical_name);
1706 if (required_encoding >= 0 && encoding != required_encoding)
1707 ereport(ERROR,
1708 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1709 errmsg("encoding \"%s\" does not match locale \"%s\"",
1711
1712 return canonical_name;
1713}
int32 encoding
Definition: pg_database.h:41
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:1663
#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 699 of file pg_locale.c.

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

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

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

Referenced by cache_locale_time().

◆ char_is_cased()

bool char_is_cased ( char  ch,
pg_locale_t  locale 
)

Definition at line 1625 of file pg_locale.c.

1626{
1627 if (locale->ctype == NULL)
1628 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
1629 return locale->ctype->char_is_cased(ch, locale);
1630}

References locale.

Referenced by pattern_char_isalpha().

◆ char_tolower()

char char_tolower ( unsigned char  ch,
pg_locale_t  locale 
)

Definition at line 1651 of file pg_locale.c.

1652{
1653 if (locale->ctype == NULL)
1654 return pg_ascii_tolower(ch);
1655 return locale->ctype->char_tolower(ch, locale);
1656}
static unsigned char pg_ascii_tolower(unsigned char ch)
Definition: port.h:188

References locale, and pg_ascii_tolower().

Referenced by SB_lower_char().

◆ char_tolower_enabled()

bool char_tolower_enabled ( pg_locale_t  locale)

Definition at line 1638 of file pg_locale.c.

1639{
1640 if (locale->ctype == NULL)
1641 return true;
1642 return (locale->ctype->char_tolower != NULL);
1643}

References locale.

Referenced by Generic_Text_IC_like().

◆ check_locale()

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

Definition at line 272 of file pg_locale.c.

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

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 383 of file pg_locale.c.

384{
385 if (**newval == '\0')
386 {
387 if (source == PGC_S_DEFAULT)
388 return true;
389 else
390 return false;
391 }
392
393 /*
394 * LC_MESSAGES category does not exist everywhere, but accept it anyway
395 *
396 * On Windows, we can't even check the value, so accept blindly
397 */
398#if defined(LC_MESSAGES) && !defined(WIN32)
399 return check_locale(LC_MESSAGES, *newval, NULL);
400#else
401 return true;
402#endif
403}
@ PGC_S_DEFAULT
Definition: guc.h:113
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:272
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 337 of file pg_locale.c.

338{
339 return check_locale(LC_MONETARY, *newval, NULL);
340}

References check_locale(), and newval.

◆ check_locale_numeric()

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

Definition at line 349 of file pg_locale.c.

350{
351 return check_locale(LC_NUMERIC, *newval, NULL);
352}

References check_locale(), and newval.

◆ check_locale_time()

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

Definition at line 361 of file pg_locale.c.

362{
363 return check_locale(LC_TIME, *newval, NULL);
364}

References check_locale(), and newval.

◆ create_pg_locale()

static pg_locale_t create_pg_locale ( Oid  collid,
MemoryContext  context 
)
static

Definition at line 1046 of file pg_locale.c.

1047{
1048 HeapTuple tp;
1049 Form_pg_collation collform;
1050 pg_locale_t result;
1051 Datum datum;
1052 bool isnull;
1053
1054 tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1055 if (!HeapTupleIsValid(tp))
1056 elog(ERROR, "cache lookup failed for collation %u", collid);
1057 collform = (Form_pg_collation) GETSTRUCT(tp);
1058
1059 if (collform->collprovider == COLLPROVIDER_BUILTIN)
1060 result = create_pg_locale_builtin(collid, context);
1061 else if (collform->collprovider == COLLPROVIDER_ICU)
1062 result = create_pg_locale_icu(collid, context);
1063 else if (collform->collprovider == COLLPROVIDER_LIBC)
1064 result = create_pg_locale_libc(collid, context);
1065 else
1066 /* shouldn't happen */
1067 PGLOCALE_SUPPORT_ERROR(collform->collprovider);
1068
1069 result->is_default = false;
1070
1071 Assert((result->collate_is_c && result->collate == NULL) ||
1072 (!result->collate_is_c && result->collate != NULL));
1073
1074 Assert((result->ctype_is_c && result->ctype == NULL) ||
1075 (!result->ctype_is_c && result->ctype != NULL));
1076
1077 datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collversion,
1078 &isnull);
1079 if (!isnull)
1080 {
1081 char *actual_versionstr;
1082 char *collversionstr;
1083
1084 collversionstr = TextDatumGetCString(datum);
1085
1086 if (collform->collprovider == COLLPROVIDER_LIBC)
1087 datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate);
1088 else
1089 datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale);
1090
1091 actual_versionstr = get_collation_actual_version(collform->collprovider,
1092 TextDatumGetCString(datum));
1093 if (!actual_versionstr)
1094 {
1095 /*
1096 * This could happen when specifying a version in CREATE COLLATION
1097 * but the provider does not support versioning, or manually
1098 * creating a mess in the catalogs.
1099 */
1100 ereport(ERROR,
1101 (errmsg("collation \"%s\" has no actual version, but a version was recorded",
1102 NameStr(collform->collname))));
1103 }
1104
1105 if (strcmp(actual_versionstr, collversionstr) != 0)
1107 (errmsg("collation \"%s\" has version mismatch",
1108 NameStr(collform->collname)),
1109 errdetail("The collation in the database was created using version %s, "
1110 "but the operating system provides version %s.",
1111 collversionstr, actual_versionstr),
1112 errhint("Rebuild all objects affected by this collation and run "
1113 "ALTER COLLATION %s REFRESH VERSION, "
1114 "or build PostgreSQL with the right library version.",
1115 quote_qualified_identifier(get_namespace_name(collform->collnamespace),
1116 NameStr(collform->collname)))));
1117 }
1118
1119 ReleaseSysCache(tp);
1120
1121 return result;
1122}
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define NameStr(name)
Definition: c.h:754
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:3533
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:1244
pg_locale_t create_pg_locale_builtin(Oid collid, MemoryContext context)
#define PGLOCALE_SUPPORT_ERROR(provider)
Definition: pg_locale.c:57
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:161
const struct collate_methods * collate
Definition: pg_locale.h:160
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 228 of file pg_locale_builtin.c.

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

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 248 of file pg_locale_icu.c.

249{
250#ifdef USE_ICU
251 bool deterministic;
252 const char *iculocstr;
253 const char *icurules = NULL;
254 UCollator *collator;
255 pg_locale_t result;
256
257 if (collid == DEFAULT_COLLATION_OID)
258 {
259 HeapTuple tp;
260 Datum datum;
261 bool isnull;
262
264 if (!HeapTupleIsValid(tp))
265 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
266
267 /* default database collation is always deterministic */
268 deterministic = true;
269 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
270 Anum_pg_database_datlocale);
271 iculocstr = TextDatumGetCString(datum);
272 datum = SysCacheGetAttr(DATABASEOID, tp,
273 Anum_pg_database_daticurules, &isnull);
274 if (!isnull)
275 icurules = TextDatumGetCString(datum);
276
277 ReleaseSysCache(tp);
278 }
279 else
280 {
281 Form_pg_collation collform;
282 HeapTuple tp;
283 Datum datum;
284 bool isnull;
285
287 if (!HeapTupleIsValid(tp))
288 elog(ERROR, "cache lookup failed for collation %u", collid);
289 collform = (Form_pg_collation) GETSTRUCT(tp);
290 deterministic = collform->collisdeterministic;
291 datum = SysCacheGetAttrNotNull(COLLOID, tp,
292 Anum_pg_collation_colllocale);
293 iculocstr = TextDatumGetCString(datum);
294 datum = SysCacheGetAttr(COLLOID, tp,
295 Anum_pg_collation_collicurules, &isnull);
296 if (!isnull)
297 icurules = TextDatumGetCString(datum);
298
299 ReleaseSysCache(tp);
300 }
301
302 collator = make_icu_collator(iculocstr, icurules);
303
304 result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
305 result->icu.locale = MemoryContextStrdup(context, iculocstr);
306 result->icu.ucol = collator;
307 result->deterministic = deterministic;
308 result->collate_is_c = false;
309 result->ctype_is_c = false;
311 result->collate = &collate_methods_icu_utf8;
312 else
313 result->collate = &collate_methods_icu;
314 result->ctype = &ctype_methods_icu;
315
316 return result;
317#else
318 /* could get here if a collation was created by a build with ICU */
320 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
321 errmsg("ICU is not supported in this build")));
322
323 return NULL;
324#endif
325}

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, MemoryContextAllocZero(), MemoryContextStrdup(), MyDatabaseId, ObjectIdGetDatum(), 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 715 of file pg_locale_libc.c.

716{
717 const char *collate;
718 const char *ctype;
719 locale_t loc;
720 pg_locale_t result;
721
722 if (collid == DEFAULT_COLLATION_OID)
723 {
724 HeapTuple tp;
725 Datum datum;
726
728 if (!HeapTupleIsValid(tp))
729 elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
730 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
731 Anum_pg_database_datcollate);
732 collate = TextDatumGetCString(datum);
733 datum = SysCacheGetAttrNotNull(DATABASEOID, tp,
734 Anum_pg_database_datctype);
735 ctype = TextDatumGetCString(datum);
736
737 ReleaseSysCache(tp);
738 }
739 else
740 {
741 HeapTuple tp;
742 Datum datum;
743
745 if (!HeapTupleIsValid(tp))
746 elog(ERROR, "cache lookup failed for collation %u", collid);
747
748 datum = SysCacheGetAttrNotNull(COLLOID, tp,
749 Anum_pg_collation_collcollate);
750 collate = TextDatumGetCString(datum);
751 datum = SysCacheGetAttrNotNull(COLLOID, tp,
752 Anum_pg_collation_collctype);
753 ctype = TextDatumGetCString(datum);
754
755 ReleaseSysCache(tp);
756 }
757
758
759 loc = make_libc_collator(collate, ctype);
760
761 result = MemoryContextAllocZero(context, sizeof(struct pg_locale_struct));
762 result->deterministic = true;
763 result->collate_is_c = (strcmp(collate, "C") == 0) ||
764 (strcmp(collate, "POSIX") == 0);
765 result->ctype_is_c = (strcmp(ctype, "C") == 0) ||
766 (strcmp(ctype, "POSIX") == 0);
767 result->lt = loc;
768 if (!result->collate_is_c)
769 {
770#ifdef WIN32
772 result->collate = &collate_methods_libc_win32_utf8;
773 else
774#endif
775 result->collate = &collate_methods_libc;
776 }
777 if (!result->ctype_is_c)
778 {
783 else
784 result->ctype = &ctype_methods_libc_sb;
785 }
786
787 return result;
788}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1547
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 473 of file pg_locale.c.

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

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 423 of file pg_locale.c.

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

References free.

Referenced by PGLC_localeconv().

◆ get_collation_actual_version()

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

Definition at line 1244 of file pg_locale.c.

1245{
1246 char *collversion = NULL;
1247
1248 if (collprovider == COLLPROVIDER_BUILTIN)
1249 collversion = get_collation_actual_version_builtin(collcollate);
1250#ifdef USE_ICU
1251 else if (collprovider == COLLPROVIDER_ICU)
1252 collversion = get_collation_actual_version_icu(collcollate);
1253#endif
1254 else if (collprovider == COLLPROVIDER_LIBC)
1255 collversion = get_collation_actual_version_libc(collcollate);
1256
1257 return collversion;
1258}
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 276 of file pg_locale_builtin.c.

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

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 957 of file pg_locale_libc.c.

958{
959 char *collversion = NULL;
960
961 if (pg_strcasecmp("C", collcollate) != 0 &&
962 pg_strncasecmp("C.", collcollate, 2) != 0 &&
963 pg_strcasecmp("POSIX", collcollate) != 0)
964 {
965#if defined(__GLIBC__)
966 /* Use the glibc version because we don't have anything better. */
967 collversion = pstrdup(gnu_get_libc_version());
968#elif defined(LC_VERSION_MASK)
969 locale_t loc;
970
971 /* Look up FreeBSD collation version. */
972 loc = newlocale(LC_COLLATE_MASK, collcollate, NULL);
973 if (loc)
974 {
975 collversion =
976 pstrdup(querylocale(LC_COLLATE_MASK | LC_VERSION_MASK, loc));
977 freelocale(loc);
978 }
979 else
981 (errmsg("could not load locale \"%s\"", collcollate)));
982#elif defined(WIN32)
983 /*
984 * If we are targeting Windows Vista and above, we can ask for a name
985 * given a collation name (earlier versions required a location code
986 * that we don't have).
987 */
988 NLSVERSIONINFOEX version = {sizeof(NLSVERSIONINFOEX)};
989 WCHAR wide_collcollate[LOCALE_NAME_MAX_LENGTH];
990
991 MultiByteToWideChar(CP_ACP, 0, collcollate, -1, wide_collcollate,
992 LOCALE_NAME_MAX_LENGTH);
993 if (!GetNLSVersionEx(COMPARE_STRING, wide_collcollate, &version))
994 {
995 /*
996 * GetNLSVersionEx() wants a language tag such as "en-US", not a
997 * locale name like "English_United States.1252". Until those
998 * values can be prevented from entering the system, or 100%
999 * reliably converted to the more useful tag format, tolerate the
1000 * resulting error and report that we have no version data.
1001 */
1002 if (GetLastError() == ERROR_INVALID_PARAMETER)
1003 return NULL;
1004
1005 ereport(ERROR,
1006 (errmsg("could not get collation version for locale \"%s\": error code %lu",
1007 collcollate,
1008 GetLastError())));
1009 }
1010 collversion = psprintf("%lu.%lu,%lu.%lu",
1011 (version.dwNLSVersion >> 8) & 0xFFFF,
1012 version.dwNLSVersion & 0xFF,
1013 (version.dwDefinedVersion >> 8) & 0xFFFF,
1014 version.dwDefinedVersion & 0xFF);
1015#endif
1016 }
1017
1018 return collversion;
1019}
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 1727 of file pg_locale.c.

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

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 1785 of file pg_locale.c.

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

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 1128 of file pg_locale.c.

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

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 1172 of file pg_locale.c.

1173{
1174 return pg_newlocale_from_collation(DEFAULT_COLLATION_OID);
1175}
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1186

References pg_newlocale_from_collation().

Referenced by t_isalnum(), and t_isalpha().

◆ pg_iswalnum()

bool pg_iswalnum ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1510 of file pg_locale.c.

1511{
1512 if (locale->ctype == NULL)
1513 return (wc <= (pg_wchar) 127 &&
1515 else
1516 return locale->ctype->wc_isalnum(wc, locale);
1517}
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 1500 of file pg_locale.c.

1501{
1502 if (locale->ctype == NULL)
1503 return (wc <= (pg_wchar) 127 &&
1505 else
1506 return locale->ctype->wc_isalpha(wc, locale);
1507}
#define PG_ISALPHA
Definition: pg_locale_c.h:20

References locale, pg_char_properties, and PG_ISALPHA.

Referenced by t_isalpha().

◆ pg_iswdigit()

bool pg_iswdigit ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1490 of file pg_locale.c.

1491{
1492 if (locale->ctype == NULL)
1493 return (wc <= (pg_wchar) 127 &&
1495 else
1496 return locale->ctype->wc_isdigit(wc, locale);
1497}
#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 1540 of file pg_locale.c.

1541{
1542 if (locale->ctype == NULL)
1543 return (wc <= (pg_wchar) 127 &&
1545 else
1546 return locale->ctype->wc_isgraph(wc, locale);
1547}
#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 1530 of file pg_locale.c.

1531{
1532 if (locale->ctype == NULL)
1533 return (wc <= (pg_wchar) 127 &&
1535 else
1536 return locale->ctype->wc_islower(wc, locale);
1537}
#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 1550 of file pg_locale.c.

1551{
1552 if (locale->ctype == NULL)
1553 return (wc <= (pg_wchar) 127 &&
1555 else
1556 return locale->ctype->wc_isprint(wc, locale);
1557}
#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 1560 of file pg_locale.c.

1561{
1562 if (locale->ctype == NULL)
1563 return (wc <= (pg_wchar) 127 &&
1565 else
1566 return locale->ctype->wc_ispunct(wc, locale);
1567}
#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 1570 of file pg_locale.c.

1571{
1572 if (locale->ctype == NULL)
1573 return (wc <= (pg_wchar) 127 &&
1575 else
1576 return locale->ctype->wc_isspace(wc, locale);
1577}
#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 1520 of file pg_locale.c.

1521{
1522 if (locale->ctype == NULL)
1523 return (wc <= (pg_wchar) 127 &&
1525 else
1526 return locale->ctype->wc_isupper(wc, locale);
1527}
#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 1580 of file pg_locale.c.

1581{
1582 if (locale->ctype == NULL)
1583 return (wc <= (pg_wchar) 127 &&
1584 ((pg_char_properties[wc] & PG_ISDIGIT) ||
1585 ((wc >= 'A' && wc <= 'F') ||
1586 (wc >= 'a' && wc <= 'f'))));
1587 else
1588 return locale->ctype->wc_isxdigit(wc, locale);
1589}

References locale, pg_char_properties, and PG_ISDIGIT.

◆ pg_newlocale_from_collation()

pg_locale_t pg_newlocale_from_collation ( Oid  collid)

Definition at line 1186 of file pg_locale.c.

1187{
1188 collation_cache_entry *cache_entry;
1189 bool found;
1190
1191 if (collid == DEFAULT_COLLATION_OID)
1192 return default_locale;
1193
1194 /*
1195 * Some callers expect C_COLLATION_OID to succeed even without catalog
1196 * access.
1197 */
1198 if (collid == C_COLLATION_OID)
1199 return &c_locale;
1200
1201 if (!OidIsValid(collid))
1202 elog(ERROR, "cache lookup failed for collation %u", collid);
1203
1205
1208
1209 if (CollationCache == NULL)
1210 {
1212 "collation cache",
1214 CollationCache = collation_cache_create(CollationCacheContext,
1215 16, NULL);
1216 }
1217
1218 cache_entry = collation_cache_insert(CollationCache, collid, &found);
1219 if (!found)
1220 {
1221 /*
1222 * Make sure cache entry is marked invalid, in case we fail before
1223 * setting things.
1224 */
1225 cache_entry->locale = NULL;
1226 }
1227
1228 if (cache_entry->locale == NULL)
1229 {
1231 }
1232
1234 last_collation_cache_locale = cache_entry->locale;
1235
1236 return cache_entry->locale;
1237}
#define OidIsValid(objectId)
Definition: c.h:777
#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:148
static struct pg_locale_struct c_locale
Definition: pg_locale.c:109
static pg_locale_t create_pg_locale(Oid collid, MemoryContext context)
Definition: pg_locale.c:1046
static MemoryContext CollationCacheContext
Definition: pg_locale.c:140
static collation_cache_hash * CollationCache
Definition: pg_locale.c:141
static Oid last_collation_cache_oid
Definition: pg_locale.c:147
static void AssertCouldGetRelation(void)
Definition: relcache.h:44
Definition: pg_locale.c:118
pg_locale_t locale
Definition: pg_locale.c:120

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(), 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 169 of file pg_locale.c.

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

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 1361 of file pg_locale.c.

1362{
1363 return locale->collate->strncoll(arg1, -1, arg2, -1, locale);
1364}

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 1345 of file pg_locale.c.

1347{
1348 /* in the C locale, casefolding is the same as lowercasing */
1349 if (locale->ctype == NULL)
1350 return strlower_c(dst, dstsize, src, srclen);
1351 else
1352 return locale->ctype->strfold(dst, dstsize, src, srclen, locale);
1353}
static size_t strlower_c(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: pg_locale.c:1262

References locale, and strlower_c().

Referenced by 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 1315 of file pg_locale.c.

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

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 1381 of file pg_locale.c.

1383{
1384 return locale->collate->strncoll(arg1, len1, arg2, len2, locale);
1385}

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 1436 of file pg_locale.c.

1438{
1439 return locale->collate->strnxfrm(dest, destsize, src, srclen, locale);
1440}

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 1483 of file pg_locale.c.

1485{
1486 return locale->collate->strnxfrm_prefix(dest, destsize, src, srclen, locale);
1487}

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 1325 of file pg_locale.c.

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

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 1335 of file pg_locale.c.

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

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 1411 of file pg_locale.c.

1412{
1413 return locale->collate->strnxfrm(dest, destsize, src, -1, locale);
1414}

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 1395 of file pg_locale.c.

1396{
1397 /*
1398 * locale->collate->strnxfrm is still a required method, even if it may
1399 * have the wrong behavior, because the planner uses it for estimates in
1400 * some cases.
1401 */
1402 return locale->collate->strxfrm_is_safe;
1403}

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 1458 of file pg_locale.c.

1460{
1461 return locale->collate->strnxfrm_prefix(dest, destsize, src, -1, locale);
1462}

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 1447 of file pg_locale.c.

1448{
1449 return (locale->collate->strnxfrm_prefix != NULL);
1450}

References locale.

Referenced by varstr_abbrev_convert().

◆ pg_towlower()

pg_wchar pg_towlower ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1605 of file pg_locale.c.

1606{
1607 if (locale->ctype == NULL)
1608 {
1609 if (wc <= (pg_wchar) 127)
1610 return pg_ascii_tolower((unsigned char) wc);
1611 return wc;
1612 }
1613 else
1614 return locale->ctype->wc_tolower(wc, locale);
1615}

References locale, and pg_ascii_tolower().

◆ pg_towupper()

pg_wchar pg_towupper ( pg_wchar  wc,
pg_locale_t  locale 
)

Definition at line 1592 of file pg_locale.c.

1593{
1594 if (locale->ctype == NULL)
1595 {
1596 if (wc <= (pg_wchar) 127)
1597 return pg_ascii_toupper((unsigned char) wc);
1598 return wc;
1599 }
1600 else
1601 return locale->ctype->wc_toupper(wc, locale);
1602}
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 503 of file pg_locale.c.

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

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

References i, and pg_ascii_tolower().

Referenced by 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 1276 of file pg_locale.c.

1277{
1278 bool wasalnum = false;
1279 int i;
1280
1281 srclen = (srclen >= 0) ? srclen : strlen(src);
1282 for (i = 0; i < srclen && i < dstsize; i++)
1283 {
1284 char c = src[i];
1285
1286 if (wasalnum)
1287 dst[i] = pg_ascii_tolower(c);
1288 else
1289 dst[i] = pg_ascii_toupper(c);
1290
1291 wasalnum = ((c >= '0' && c <= '9') ||
1292 (c >= 'A' && c <= 'Z') ||
1293 (c >= 'a' && c <= 'z'));
1294 }
1295 if (i < dstsize)
1296 dst[i] = '\0';
1297 return srclen;
1298}
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 442 of file pg_locale.c.

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

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 1302 of file pg_locale.c.

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

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 109 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ CollationCache

collation_cache_hash* CollationCache = NULL
static

Definition at line 141 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ CollationCacheContext

MemoryContext CollationCacheContext = NULL
static

Definition at line 140 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ CurrentLCTimeValid

bool CurrentLCTimeValid = false
static

Definition at line 107 of file pg_locale.c.

Referenced by assign_locale_time(), and cache_locale_time().

◆ CurrentLocaleConvValid

bool CurrentLocaleConvValid = false
static

Definition at line 106 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

Definition at line 103 of file pg_locale.c.

Referenced by init_database_collation(), and pg_newlocale_from_collation().

◆ icu_validation_level

int icu_validation_level = WARNING

Definition at line 89 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 148 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 147 of file pg_locale.c.

Referenced by pg_newlocale_from_collation().

◆ locale_messages

char* locale_messages

Definition at line 84 of file pg_locale.c.

◆ locale_monetary

char* locale_monetary

Definition at line 85 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_numeric

char* locale_numeric

Definition at line 86 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_time

char* locale_time

Definition at line 87 of file pg_locale.c.

Referenced by cache_locale_time().

◆ localized_abbrev_days

char* localized_abbrev_days[7+1]

Definition at line 98 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 100 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 99 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 101 of file pg_locale.c.

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