pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
tiny step toward threading: reduce dependence on setlocale()
33+ messages / 5 participants
[nested] [flat]

Commits from this thread:
  2025-11-05 d115de9d89 3ebea75f9a  Special case C_COLLATION_OID in pg_newlocale_from_collation().
  2024-09-13 433d8f40e9  Remove separate locale_is_c arguments
  2024-09-12 b0c30612c5  Simplify checks for deterministic collations.
  2024-09-12 6a9fc11033  Remove redundant check for default collation.
  2024-09-06 51edc4ca54  Remove lc_ctype_is_c().
  2024-09-05 7829f85a62  Be more careful with error paths in pg_set_regex_collation().
  2024-09-04 06421b0843  Remove lc_collate_is_c().
  2024-09-03 12d3345c0d  Remember last collation to speed up collation cache.
  2024-08-20 a95ff1fe2e  Slightly refactor varstr_sortsupport() to improve readability.
  2024-08-12 a459ac504c  Remove unnecessary check for NULL locale, per Coverity.
  2024-08-12 4eb5089e26  Remove dead code
  2024-08-06 a890ad2149  selfuncs.c: use pg_strxfrm() instead of strxfrm().
  2024-08-06 e9931bfb75  Remove support for null pg_locale_t most places.
  2024-07-30 72fe6d24a3  Make collation not depend on setlocale().
  2024-07-29 8240401437  Do not return NULL from pg_newlocale_from_collation().
  2024-07-28 1c461a8d8d  Refactor: make default_locale internal to pg_locale.c.
* tiny step toward threading: reduce dependence on setlocale()
@ 2024-06-06 00:23  Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-06-06 00:23 UTC (permalink / raw)
  To: pgsql-hackers


There was an unconference session at pgconf.dev related to threading
support. One of the problems identified was setlocale().

The attached series of patches make collation not depend on
setlocale(), even if the database collation uses the libc provider.

Since commit 8d9a9f034e, all supported platforms have locale_t, so we
can use strcoll_l(), etc., or uselocale() when no "_l" variant is
available.

A brief test shows that there may be a performance regression for libc
default collations. But if so, I'm not sure that's avoidable if the
goal is to take away setlocale. I'll see if removing the extra branches
mitigates it.


-- 
Jeff Davis
PostgreSQL Contributor Team - AWS

Attachments: download all (5 files, .tar.gz)

v1-0001-Make-database-default-collation-internal-to-pg_lo.patch (text/x-patch, 5.7K) download From 9f50c24878740acdd3f1bc036442a0fcc0ea1a5e Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:45:55 -0700 Subject: [PATCH v1 1/5] Make database default collation internal to pg_locale.c. --- src/backend/utils/adt/pg_locale.c | 64 ++++++++++++++++++++++++++++++- src/backend/utils/init/postinit.c | 35 ++--------------- src/include/utils/pg_locale.h | 3 +- 3 files changed, 66 insertions(+), 36 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 7e5bb2b703..29f16c49cb 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -56,6 +56,7 @@ #include "access/htup_details.h" #include "catalog/pg_collation.h" +#include "catalog/pg_database.h" #include "mb/pg_wchar.h" #include "miscadmin.h" #include "utils/builtins.h" @@ -116,6 +117,8 @@ char *localized_full_months[12 + 1]; /* is the databases's LC_CTYPE the C locale? */ bool database_ctype_is_c = false; +static struct pg_locale_struct default_locale; + /* indicates whether locale information cache is valid */ static bool CurrentLocaleConvValid = false; static bool CurrentLCTimeValid = false; @@ -1443,8 +1446,6 @@ lc_ctype_is_c(Oid collation) return (lookup_collation_cache(collation, true))->ctype_is_c; } -struct pg_locale_struct default_locale; - void make_icu_collator(const char *iculocstr, const char *icurules, @@ -1537,6 +1538,65 @@ pg_locale_deterministic(pg_locale_t locale) return locale->deterministic; } +void +pg_init_database_collation() +{ + HeapTuple tup; + Form_pg_database dbform; + Datum datum; + bool isnull; + + /* Fetch our pg_database row normally, via syscache */ + tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + if (!HeapTupleIsValid(tup)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + dbform = (Form_pg_database) GETSTRUCT(tup); + + if (dbform->datlocprovider == COLLPROVIDER_BUILTIN) + { + char *datlocale; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); + + builtin_validate_locale(dbform->encoding, datlocale); + + default_locale.info.builtin.locale = MemoryContextStrdup( + TopMemoryContext, datlocale); + } + else if (dbform->datlocprovider == COLLPROVIDER_ICU) + { + char *datlocale; + char *icurules; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); + + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); + if (!isnull) + icurules = TextDatumGetCString(datum); + else + icurules = NULL; + + make_icu_collator(datlocale, icurules, &default_locale); + } + else + { + Assert(dbform->datlocprovider == COLLPROVIDER_LIBC); + } + + default_locale.provider = dbform->datlocprovider; + + /* + * Default locale is currently always deterministic. Nondeterministic + * locales currently don't support pattern matching, which would break a + * lot of things if applied globally. + */ + default_locale.deterministic = true; + + ReleaseSysCache(tup); +} + /* * Create a locale_t from a collation OID. Results are cached for the * lifetime of the backend. Thus, do not free the result with freelocale(). diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 0805398e24..6347efdd5a 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -423,43 +423,14 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect strcmp(ctype, "POSIX") == 0) database_ctype_is_c = true; - if (dbform->datlocprovider == COLLPROVIDER_BUILTIN) - { - datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); - datlocale = TextDatumGetCString(datum); - - builtin_validate_locale(dbform->encoding, datlocale); - - default_locale.info.builtin.locale = MemoryContextStrdup( - TopMemoryContext, datlocale); - } - else if (dbform->datlocprovider == COLLPROVIDER_ICU) - { - char *icurules; + pg_init_database_collation(); - datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_datlocale, &isnull); + if (!isnull) datlocale = TextDatumGetCString(datum); - - datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); - if (!isnull) - icurules = TextDatumGetCString(datum); - else - icurules = NULL; - - make_icu_collator(datlocale, icurules, &default_locale); - } else datlocale = NULL; - default_locale.provider = dbform->datlocprovider; - - /* - * Default locale is currently always deterministic. Nondeterministic - * locales currently don't support pattern matching, which would break a - * lot of things if applied globally. - */ - default_locale.deterministic = true; - /* * Check collation version. See similar code in * pg_newlocale_from_collation(). Note that here we warn instead of error diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 205aa20067..1c81a84124 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -93,13 +93,12 @@ struct pg_locale_struct typedef struct pg_locale_struct *pg_locale_t; -extern PGDLLIMPORT struct pg_locale_struct default_locale; - extern void make_icu_collator(const char *iculocstr, const char *icurules, struct pg_locale_struct *resultp); extern bool pg_locale_deterministic(pg_locale_t locale); +extern void pg_init_database_collation(void); extern pg_locale_t pg_newlocale_from_collation(Oid collid); extern char *get_collation_actual_version(char collprovider, const char *collcollate); -- 2.34.1
v1-0002-Make-database-collation-pg_locale_t-always-non-NU.patch (text/x-patch, 2.6K) download From 8673b5559121b861db98d7b8eacc320ff3c15595 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 15:02:26 -0700 Subject: [PATCH v1 2/5] Make database collation pg_locale_t always non-NULL. Previously, the database collation's pg_locale_t was NULL for the libc provider. This commit properly initializes a pg_locale_t object in all cases. --- src/backend/utils/adt/pg_locale.c | 58 +++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 29f16c49cb..185b860dad 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1582,7 +1582,58 @@ pg_init_database_collation() } else { + const char *datcollate; + const char *datctype pg_attribute_unused(); + locale_t loc; + Assert(dbform->datlocprovider == COLLPROVIDER_LIBC); + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datcollate); + datcollate = TextDatumGetCString(datum); + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype); + datctype = TextDatumGetCString(datum); + + if (strcmp(datcollate, datctype) == 0) + { + /* Normal case where they're the same */ + errno = 0; +#ifndef WIN32 + loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, datcollate, + NULL); +#else + loc = _create_locale(LC_ALL, datcollate); +#endif + if (!loc) + report_newlocale_failure(datcollate); + } + else + { +#ifndef WIN32 + /* We need two newlocale() steps */ + locale_t loc1; + + errno = 0; + loc1 = newlocale(LC_COLLATE_MASK, datcollate, NULL); + if (!loc1) + report_newlocale_failure(datcollate); + errno = 0; + loc = newlocale(LC_CTYPE_MASK, datctype, loc1); + if (!loc) + report_newlocale_failure(datctype); +#else + + /* + * XXX The _create_locale() API doesn't appear to support + * this. Could perhaps be worked around by changing + * pg_locale_t to contain two separate fields. + */ + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("collations with different collate and ctype values are not supported on this platform"))); +#endif + } + + default_locale.info.lt = loc; } default_locale.provider = dbform->datlocprovider; @@ -1616,12 +1667,7 @@ pg_newlocale_from_collation(Oid collid) Assert(OidIsValid(collid)); if (collid == DEFAULT_COLLATION_OID) - { - if (default_locale.provider == COLLPROVIDER_LIBC) - return (pg_locale_t) 0; - else - return &default_locale; - } + return &default_locale; cache_entry = lookup_collation_cache(collid, false); -- 2.34.1
v1-0003-ts_locale.c-do-not-use-NULL-to-mean-the-database-.patch (text/x-patch, 3.2K) download From f13f0a08f0285ba6c94742eec4f57d7a1dfc1620 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 14:48:07 -0700 Subject: [PATCH v1 3/5] ts_locale.c: do not use NULL to mean the database collation. Use pg_newlocale_from_collation(DEFAULT_COLLATION_OID) to explicitly get the database collation. --- src/backend/tsearch/ts_locale.c | 13 +++++++------ src/backend/tsearch/wparser_def.c | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/backend/tsearch/ts_locale.c b/src/backend/tsearch/ts_locale.c index bc44599de6..86c3d6e790 100644 --- a/src/backend/tsearch/ts_locale.c +++ b/src/backend/tsearch/ts_locale.c @@ -13,6 +13,7 @@ */ #include "postgres.h" +#include "catalog/pg_collation.h" #include "common/string.h" #include "storage/fd.h" #include "tsearch/ts_locale.h" @@ -36,7 +37,7 @@ t_isdigit(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isdigit(TOUCHAR(ptr)); @@ -51,7 +52,7 @@ t_isspace(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isspace(TOUCHAR(ptr)); @@ -66,7 +67,7 @@ t_isalpha(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isalpha(TOUCHAR(ptr)); @@ -81,7 +82,7 @@ t_isalnum(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isalnum(TOUCHAR(ptr)); @@ -96,7 +97,7 @@ t_isprint(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID);; if (clen == 1 || database_ctype_is_c) return isprint(TOUCHAR(ptr)); @@ -266,7 +267,7 @@ char * lowerstr_with_len(const char *str, int len) { char *out; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (len == 0) return pstrdup(""); diff --git a/src/backend/tsearch/wparser_def.c b/src/backend/tsearch/wparser_def.c index 3919ef27b5..a333d46802 100644 --- a/src/backend/tsearch/wparser_def.c +++ b/src/backend/tsearch/wparser_def.c @@ -17,6 +17,7 @@ #include <limits.h> #include <wctype.h> +#include "catalog/pg_collation.h" #include "commands/defrem.h" #include "mb/pg_wchar.h" #include "miscadmin.h" @@ -299,7 +300,7 @@ TParserInit(char *str, int len) */ if (prs->charmaxlen > 1) { - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); prs->usewide = true; if (database_ctype_is_c) -- 2.34.1
v1-0004-Remove-support-for-null-pg_locale_t.patch (text/x-patch, 10.3K) download From c29ac64ddd1f33f61c6a284329d29afa0995499a Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:58:59 -0700 Subject: [PATCH v1 4/5] Remove support for null pg_locale_t. Previously, passing NULL for pg_locale_t meant "use the libc provider and the server environment". Now that the database collation is represented as a proper pg_locale_t (not dependent on setlocale()), remove special cases for NULL. --- src/backend/access/hash/hashfunc.c | 4 +-- src/backend/utils/adt/like.c | 2 +- src/backend/utils/adt/pg_locale.c | 57 +++++++++++------------------- src/backend/utils/adt/varchar.c | 4 +-- src/backend/utils/adt/varlena.c | 4 +-- 5 files changed, 27 insertions(+), 44 deletions(-) diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index ce8ee0ea2e..ec2133d4e1 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -280,7 +280,7 @@ hashtext(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); @@ -334,7 +334,7 @@ hashtextextended(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any_extended((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key), diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 57ead66b5a..0807b89b17 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -194,7 +194,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) else locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale_is_c && !pg_locale_deterministic(locale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for ILIKE"))); diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 185b860dad..2ffc551913 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1531,11 +1531,7 @@ report_newlocale_failure(const char *localename) bool pg_locale_deterministic(pg_locale_t locale) { - /* default locale must always be deterministic */ - if (locale == NULL) - return true; - else - return locale->deterministic; + return locale->deterministic; } void @@ -1954,7 +1950,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, int r; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); Assert(GetDatabaseEncoding() == PG_UTF8); #ifndef WIN32 Assert(false); @@ -1994,10 +1990,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, ((LPWSTR) a2p)[r] = 0; errno = 0; - if (locale) - result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); - else - result = wcscoll((LPWSTR) a1p, (LPWSTR) a2p); + result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); if (result == 2147483647) /* _NLSCMPERROR; missing from mingw headers */ ereport(ERROR, (errmsg("could not compare Unicode strings: %m"))); @@ -2023,7 +2016,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 if (GetDatabaseEncoding() == PG_UTF8) { @@ -2034,10 +2027,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) } else #endif /* WIN32 */ - if (locale) result = strcoll_l(arg1, arg2, locale->info.lt); - else - result = strcoll(arg1, arg2); return result; } @@ -2059,7 +2049,7 @@ pg_strncoll_libc(const char *arg1, size_t len1, const char *arg2, size_t len2, char *arg2n; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 /* check for this case before doing the work for nul-termination */ @@ -2205,7 +2195,7 @@ pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strcoll_libc(arg1, arg2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2241,7 +2231,7 @@ pg_strncoll(const char *arg1, size_t len1, const char *arg2, size_t len2, { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strncoll_libc(arg1, len1, arg2, len2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2259,13 +2249,10 @@ static size_t pg_strxfrm_libc(char *dest, const char *src, size_t destsize, pg_locale_t locale) { - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef TRUST_STRXFRM - if (locale) - return strxfrm_l(dest, src, destsize, locale->info.lt); - else - return strxfrm(dest, src, destsize); + return strxfrm_l(dest, src, destsize, locale->info.lt); #else /* shouldn't happen */ PGLOCALE_SUPPORT_ERROR(locale->provider); @@ -2282,7 +2269,7 @@ pg_strnxfrm_libc(char *dest, const char *src, size_t srclen, size_t destsize, size_t bufsize = srclen + 1; size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (bufsize > TEXTBUFLEN) buf = palloc(bufsize); @@ -2454,7 +2441,7 @@ pg_strnxfrm_prefix_icu(char *dest, const char *src, int32_t srclen, bool pg_strxfrm_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) #ifdef TRUST_STRXFRM return true; #else @@ -2488,7 +2475,7 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale) { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strxfrm_libc(dest, src, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2525,7 +2512,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strnxfrm_libc(dest, src, srclen, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2545,7 +2532,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, bool pg_strxfrm_prefix_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) return false; else if (locale->provider == COLLPROVIDER_ICU) return true; @@ -2575,13 +2562,11 @@ pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -2610,13 +2595,11 @@ pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -3166,7 +3149,7 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; @@ -3223,7 +3206,7 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 02dfe219f5..60b1372227 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -1014,7 +1014,7 @@ hashbpchar(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any((unsigned char *) keydata, keylen); } @@ -1069,7 +1069,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any_extended((unsigned char *) keydata, keylen, PG_GETARG_INT64(1)); diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index d2e2e9bbba..9abae63221 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1224,7 +1224,7 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (mylocale && !pg_locale_deterministic(mylocale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); @@ -1803,7 +1803,7 @@ text_starts_with(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (mylocale && !pg_locale_deterministic(mylocale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); -- 2.34.1
v1-0005-Avoid-setlocale-in-lc_collate_is_c-and-lc_ctype_i.patch (text/x-patch, 14.6K) download From 865a8c586d97eea6d496eab16edf35a842294ed2 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 15:22:04 -0700 Subject: [PATCH v1 5/5] Avoid setlocale() in lc_collate_is_c() and lc_ctype_is_c(). Store the collate and ctype strings in the pg_locale_t structure, which requires some minor refactoring. --- src/backend/regex/regc_pg_locale.c | 44 ++++++++++++++-------------- src/backend/utils/adt/formatting.c | 20 ++++++------- src/backend/utils/adt/like.c | 2 +- src/backend/utils/adt/like_support.c | 2 +- src/backend/utils/adt/pg_locale.c | 30 +++++++++++-------- src/include/utils/pg_locale.h | 7 ++++- 6 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 85f3238eb0..1e180b844f 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -309,11 +309,11 @@ pg_wc_isdigit(pg_wchar c) isdigit((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswdigit_l((wint_t) c, pg_regex_locale->info.lt); + return iswdigit_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isdigit_l((unsigned char) c, pg_regex_locale->info.lt)); + isdigit_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -343,11 +343,11 @@ pg_wc_isalpha(pg_wchar c) isalpha((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalpha_l((wint_t) c, pg_regex_locale->info.lt); + return iswalpha_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isalpha_l((unsigned char) c, pg_regex_locale->info.lt)); + isalpha_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -377,11 +377,11 @@ pg_wc_isalnum(pg_wchar c) isalnum((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalnum_l((wint_t) c, pg_regex_locale->info.lt); + return iswalnum_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isalnum_l((unsigned char) c, pg_regex_locale->info.lt)); + isalnum_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -420,11 +420,11 @@ pg_wc_isupper(pg_wchar c) isupper((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswupper_l((wint_t) c, pg_regex_locale->info.lt); + return iswupper_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isupper_l((unsigned char) c, pg_regex_locale->info.lt)); + isupper_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -454,11 +454,11 @@ pg_wc_islower(pg_wchar c) islower((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswlower_l((wint_t) c, pg_regex_locale->info.lt); + return iswlower_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - islower_l((unsigned char) c, pg_regex_locale->info.lt)); + islower_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -488,11 +488,11 @@ pg_wc_isgraph(pg_wchar c) isgraph((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswgraph_l((wint_t) c, pg_regex_locale->info.lt); + return iswgraph_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isgraph_l((unsigned char) c, pg_regex_locale->info.lt)); + isgraph_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -522,11 +522,11 @@ pg_wc_isprint(pg_wchar c) isprint((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswprint_l((wint_t) c, pg_regex_locale->info.lt); + return iswprint_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isprint_l((unsigned char) c, pg_regex_locale->info.lt)); + isprint_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -556,11 +556,11 @@ pg_wc_ispunct(pg_wchar c) ispunct((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswpunct_l((wint_t) c, pg_regex_locale->info.lt); + return iswpunct_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - ispunct_l((unsigned char) c, pg_regex_locale->info.lt)); + ispunct_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -590,11 +590,11 @@ pg_wc_isspace(pg_wchar c) isspace((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswspace_l((wint_t) c, pg_regex_locale->info.lt); + return iswspace_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isspace_l((unsigned char) c, pg_regex_locale->info.lt)); + isspace_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -632,11 +632,11 @@ pg_wc_toupper(pg_wchar c) return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towupper_l((wint_t) c, pg_regex_locale->info.lt); + return towupper_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: if (c <= (pg_wchar) UCHAR_MAX) - return toupper_l((unsigned char) c, pg_regex_locale->info.lt); + return toupper_l((unsigned char) c, pg_regex_locale->info.libc.lt); return c; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -674,11 +674,11 @@ pg_wc_tolower(pg_wchar c) return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towlower_l((wint_t) c, pg_regex_locale->info.lt); + return towlower_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: if (c <= (pg_wchar) UCHAR_MAX) - return tolower_l((unsigned char) c, pg_regex_locale->info.lt); + return tolower_l((unsigned char) c, pg_regex_locale->info.libc.lt); return c; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 8736ada4be..2c3a28ca25 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1732,7 +1732,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) for (curr_char = 0; workspace[curr_char] != 0; curr_char++) { if (mylocale) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.libc.lt); else workspace[curr_char] = towlower(workspace[curr_char]); } @@ -1763,7 +1763,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) for (p = result; *p; p++) { if (mylocale) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); + *p = tolower_l((unsigned char) *p, mylocale->info.libc.lt); else *p = pg_tolower((unsigned char) *p); } @@ -1880,7 +1880,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) for (curr_char = 0; workspace[curr_char] != 0; curr_char++) { if (mylocale) - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.libc.lt); else workspace[curr_char] = towupper(workspace[curr_char]); } @@ -1911,7 +1911,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) for (p = result; *p; p++) { if (mylocale) - *p = toupper_l((unsigned char) *p, mylocale->info.lt); + *p = toupper_l((unsigned char) *p, mylocale->info.libc.lt); else *p = pg_toupper((unsigned char) *p); } @@ -2084,10 +2084,10 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) if (mylocale) { if (wasalnum) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.libc.lt); else - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); - wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt); + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.libc.lt); + wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.libc.lt); } else { @@ -2127,10 +2127,10 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) if (mylocale) { if (wasalnum) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); + *p = tolower_l((unsigned char) *p, mylocale->info.libc.lt); else - *p = toupper_l((unsigned char) *p, mylocale->info.lt); - wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt); + *p = toupper_l((unsigned char) *p, mylocale->info.libc.lt); + wasalnum = isalnum_l((unsigned char) *p, mylocale->info.libc.lt); } else { diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 0807b89b17..4d9a207816 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -96,7 +96,7 @@ SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) if (locale_is_c) return pg_ascii_tolower(c); else if (locale) - return tolower_l(c, locale->info.lt); + return tolower_l(c, locale->info.libc.lt); else return pg_tolower(c); } diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c index 2635050861..6a1c64176d 100644 --- a/src/backend/utils/adt/like_support.c +++ b/src/backend/utils/adt/like_support.c @@ -1509,7 +1509,7 @@ pattern_char_isalpha(char c, bool is_multibyte, return IS_HIGHBIT_SET(c) || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); else if (locale && locale->provider == COLLPROVIDER_LIBC) - return isalpha_l((unsigned char) c, locale->info.lt); + return isalpha_l((unsigned char) c, locale->info.libc.lt); else return isalpha((unsigned char) c); } diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 2ffc551913..060dcbcb14 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1350,9 +1350,7 @@ lc_collate_is_c(Oid collation) } else if (default_locale.provider == COLLPROVIDER_LIBC) { - localeptr = setlocale(LC_CTYPE, NULL); - if (!localeptr) - elog(ERROR, "invalid LC_CTYPE setting"); + localeptr = default_locale.info.libc.collate; } else elog(ERROR, "unexpected collation provider '%c'", @@ -1416,9 +1414,7 @@ lc_ctype_is_c(Oid collation) } else if (default_locale.provider == COLLPROVIDER_LIBC) { - localeptr = setlocale(LC_CTYPE, NULL); - if (!localeptr) - elog(ERROR, "invalid LC_CTYPE setting"); + localeptr = default_locale.info.libc.ctype; } else elog(ERROR, "unexpected collation provider '%c'", @@ -1629,7 +1625,11 @@ pg_init_database_collation() #endif } - default_locale.info.lt = loc; + default_locale.info.libc.collate = MemoryContextStrdup( + TopMemoryContext, datcollate); + default_locale.info.libc.ctype = MemoryContextStrdup( + TopMemoryContext, datctype); + default_locale.info.libc.lt = loc; } default_locale.provider = dbform->datlocprovider; @@ -1750,7 +1750,11 @@ pg_newlocale_from_collation(Oid collid) #endif } - result.info.lt = loc; + result.info.libc.collate = MemoryContextStrdup( + TopMemoryContext, collcollate); + result.info.libc.ctype = MemoryContextStrdup( + TopMemoryContext, collctype); + result.info.libc.lt = loc; } else if (collform->collprovider == COLLPROVIDER_ICU) { @@ -1990,7 +1994,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, ((LPWSTR) a2p)[r] = 0; errno = 0; - result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); + result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.libc.lt); if (result == 2147483647) /* _NLSCMPERROR; missing from mingw headers */ ereport(ERROR, (errmsg("could not compare Unicode strings: %m"))); @@ -2027,7 +2031,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) } else #endif /* WIN32 */ - result = strcoll_l(arg1, arg2, locale->info.lt); + result = strcoll_l(arg1, arg2, locale->info.libc.lt); return result; } @@ -2252,7 +2256,7 @@ pg_strxfrm_libc(char *dest, const char *src, size_t destsize, Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef TRUST_STRXFRM - return strxfrm_l(dest, src, destsize, locale->info.lt); + return strxfrm_l(dest, src, destsize, locale->info.libc.lt); #else /* shouldn't happen */ PGLOCALE_SUPPORT_ERROR(locale->provider); @@ -3185,7 +3189,7 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) else { /* Use wcstombs_l for nondefault locales */ - result = wcstombs_l(to, from, tolen, locale->info.lt); + result = wcstombs_l(to, from, tolen, locale->info.libc.lt); } return result; @@ -3247,7 +3251,7 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, else { /* Use mbstowcs_l for nondefault locales */ - result = mbstowcs_l(to, str, tolen, locale->info.lt); + result = mbstowcs_l(to, str, tolen, locale->info.libc.lt); } pfree(str); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 1c81a84124..1130f7b86f 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -80,7 +80,12 @@ struct pg_locale_struct { const char *locale; } builtin; - locale_t lt; + struct + { + const char *collate; + const char *ctype; + locale_t lt; + } libc; #ifdef USE_ICU struct { -- 2.34.1
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-06-06 18:37  Jeff Davis <pgsql@j-davis.com>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-06-06 18:37 UTC (permalink / raw)
  To: pgsql-hackers

On Wed, 2024-06-05 at 17:23 -0700, Jeff Davis wrote:
> A brief test shows that there may be a performance regression for
> libc
> default collations. But if so, I'm not sure that's avoidable if the
> goal is to take away setlocale. I'll see if removing the extra
> branches
> mitigates it.

I redid the test and didn't see a difference, and then I ran a
standalone microbenchmark to compare strcoll() vs strcoll_l(), and
didn't see a difference there, either.

Another implementation may show a difference, but it doesn't seem to be
a problem for glibc.

I think this patch series is a nice cleanup, as well, making libc more
like the other providers and not dependent on global state.

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-06-14 23:35  Jeff Davis <pgsql@j-davis.com>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-06-14 23:35 UTC (permalink / raw)
  To: pgsql-hackers

On Thu, 2024-06-06 at 11:37 -0700, Jeff Davis wrote:
> 
> I think this patch series is a nice cleanup, as well, making libc
> more
> like the other providers and not dependent on global state.

New rebased series attached with additional cleanup. Now that
pg_locale_t is never NULL, we can simplify the way the collation cache
works, eliminating ~100 lines.

-- 
Jeff Davis
PostgreSQL Contributor Team - AWS

Attachments: download all (6 files, .tar.gz)

v2-0001-Make-database-default-collation-internal-to-pg_lo.patch (text/x-patch, 5.7K) download From d3862b88d8df3372ebdd368489a86142bd11f42c Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:45:55 -0700 Subject: [PATCH v2 1/7] Make database default collation internal to pg_locale.c. --- src/backend/utils/adt/pg_locale.c | 64 ++++++++++++++++++++++++++++++- src/backend/utils/init/postinit.c | 35 ++--------------- src/include/utils/pg_locale.h | 3 +- 3 files changed, 66 insertions(+), 36 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 7e5bb2b703..29f16c49cb 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -56,6 +56,7 @@ #include "access/htup_details.h" #include "catalog/pg_collation.h" +#include "catalog/pg_database.h" #include "mb/pg_wchar.h" #include "miscadmin.h" #include "utils/builtins.h" @@ -116,6 +117,8 @@ char *localized_full_months[12 + 1]; /* is the databases's LC_CTYPE the C locale? */ bool database_ctype_is_c = false; +static struct pg_locale_struct default_locale; + /* indicates whether locale information cache is valid */ static bool CurrentLocaleConvValid = false; static bool CurrentLCTimeValid = false; @@ -1443,8 +1446,6 @@ lc_ctype_is_c(Oid collation) return (lookup_collation_cache(collation, true))->ctype_is_c; } -struct pg_locale_struct default_locale; - void make_icu_collator(const char *iculocstr, const char *icurules, @@ -1537,6 +1538,65 @@ pg_locale_deterministic(pg_locale_t locale) return locale->deterministic; } +void +pg_init_database_collation() +{ + HeapTuple tup; + Form_pg_database dbform; + Datum datum; + bool isnull; + + /* Fetch our pg_database row normally, via syscache */ + tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + if (!HeapTupleIsValid(tup)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + dbform = (Form_pg_database) GETSTRUCT(tup); + + if (dbform->datlocprovider == COLLPROVIDER_BUILTIN) + { + char *datlocale; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); + + builtin_validate_locale(dbform->encoding, datlocale); + + default_locale.info.builtin.locale = MemoryContextStrdup( + TopMemoryContext, datlocale); + } + else if (dbform->datlocprovider == COLLPROVIDER_ICU) + { + char *datlocale; + char *icurules; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); + + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); + if (!isnull) + icurules = TextDatumGetCString(datum); + else + icurules = NULL; + + make_icu_collator(datlocale, icurules, &default_locale); + } + else + { + Assert(dbform->datlocprovider == COLLPROVIDER_LIBC); + } + + default_locale.provider = dbform->datlocprovider; + + /* + * Default locale is currently always deterministic. Nondeterministic + * locales currently don't support pattern matching, which would break a + * lot of things if applied globally. + */ + default_locale.deterministic = true; + + ReleaseSysCache(tup); +} + /* * Create a locale_t from a collation OID. Results are cached for the * lifetime of the backend. Thus, do not free the result with freelocale(). diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 0805398e24..6347efdd5a 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -423,43 +423,14 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect strcmp(ctype, "POSIX") == 0) database_ctype_is_c = true; - if (dbform->datlocprovider == COLLPROVIDER_BUILTIN) - { - datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); - datlocale = TextDatumGetCString(datum); - - builtin_validate_locale(dbform->encoding, datlocale); - - default_locale.info.builtin.locale = MemoryContextStrdup( - TopMemoryContext, datlocale); - } - else if (dbform->datlocprovider == COLLPROVIDER_ICU) - { - char *icurules; + pg_init_database_collation(); - datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_datlocale, &isnull); + if (!isnull) datlocale = TextDatumGetCString(datum); - - datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); - if (!isnull) - icurules = TextDatumGetCString(datum); - else - icurules = NULL; - - make_icu_collator(datlocale, icurules, &default_locale); - } else datlocale = NULL; - default_locale.provider = dbform->datlocprovider; - - /* - * Default locale is currently always deterministic. Nondeterministic - * locales currently don't support pattern matching, which would break a - * lot of things if applied globally. - */ - default_locale.deterministic = true; - /* * Check collation version. See similar code in * pg_newlocale_from_collation(). Note that here we warn instead of error diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 040968d6ff..3a9026e7b7 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -93,13 +93,12 @@ struct pg_locale_struct typedef struct pg_locale_struct *pg_locale_t; -extern PGDLLIMPORT struct pg_locale_struct default_locale; - extern void make_icu_collator(const char *iculocstr, const char *icurules, struct pg_locale_struct *resultp); extern bool pg_locale_deterministic(pg_locale_t locale); +extern void pg_init_database_collation(void); extern pg_locale_t pg_newlocale_from_collation(Oid collid); extern char *get_collation_actual_version(char collprovider, const char *collcollate); -- 2.34.1
v2-0002-Make-database-collation-pg_locale_t-always-non-NU.patch (text/x-patch, 2.6K) download From 6bce19a5ce602f718eb29c6d8c24adc4b648c2ee Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 15:02:26 -0700 Subject: [PATCH v2 2/7] Make database collation pg_locale_t always non-NULL. Previously, the database collation's pg_locale_t was NULL for the libc provider. This commit properly initializes a pg_locale_t object in all cases. --- src/backend/utils/adt/pg_locale.c | 58 +++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 29f16c49cb..185b860dad 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1582,7 +1582,58 @@ pg_init_database_collation() } else { + const char *datcollate; + const char *datctype pg_attribute_unused(); + locale_t loc; + Assert(dbform->datlocprovider == COLLPROVIDER_LIBC); + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datcollate); + datcollate = TextDatumGetCString(datum); + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype); + datctype = TextDatumGetCString(datum); + + if (strcmp(datcollate, datctype) == 0) + { + /* Normal case where they're the same */ + errno = 0; +#ifndef WIN32 + loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, datcollate, + NULL); +#else + loc = _create_locale(LC_ALL, datcollate); +#endif + if (!loc) + report_newlocale_failure(datcollate); + } + else + { +#ifndef WIN32 + /* We need two newlocale() steps */ + locale_t loc1; + + errno = 0; + loc1 = newlocale(LC_COLLATE_MASK, datcollate, NULL); + if (!loc1) + report_newlocale_failure(datcollate); + errno = 0; + loc = newlocale(LC_CTYPE_MASK, datctype, loc1); + if (!loc) + report_newlocale_failure(datctype); +#else + + /* + * XXX The _create_locale() API doesn't appear to support + * this. Could perhaps be worked around by changing + * pg_locale_t to contain two separate fields. + */ + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("collations with different collate and ctype values are not supported on this platform"))); +#endif + } + + default_locale.info.lt = loc; } default_locale.provider = dbform->datlocprovider; @@ -1616,12 +1667,7 @@ pg_newlocale_from_collation(Oid collid) Assert(OidIsValid(collid)); if (collid == DEFAULT_COLLATION_OID) - { - if (default_locale.provider == COLLPROVIDER_LIBC) - return (pg_locale_t) 0; - else - return &default_locale; - } + return &default_locale; cache_entry = lookup_collation_cache(collid, false); -- 2.34.1
v2-0003-ts_locale.c-do-not-use-NULL-to-mean-the-database-.patch (text/x-patch, 3.2K) download From 53ec3ac3d061a5397657c943569593e98150b3a4 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 14:48:07 -0700 Subject: [PATCH v2 3/7] ts_locale.c: do not use NULL to mean the database collation. Use pg_newlocale_from_collation(DEFAULT_COLLATION_OID) to explicitly get the database collation. --- src/backend/tsearch/ts_locale.c | 13 +++++++------ src/backend/tsearch/wparser_def.c | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/backend/tsearch/ts_locale.c b/src/backend/tsearch/ts_locale.c index bc44599de6..86c3d6e790 100644 --- a/src/backend/tsearch/ts_locale.c +++ b/src/backend/tsearch/ts_locale.c @@ -13,6 +13,7 @@ */ #include "postgres.h" +#include "catalog/pg_collation.h" #include "common/string.h" #include "storage/fd.h" #include "tsearch/ts_locale.h" @@ -36,7 +37,7 @@ t_isdigit(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isdigit(TOUCHAR(ptr)); @@ -51,7 +52,7 @@ t_isspace(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isspace(TOUCHAR(ptr)); @@ -66,7 +67,7 @@ t_isalpha(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isalpha(TOUCHAR(ptr)); @@ -81,7 +82,7 @@ t_isalnum(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isalnum(TOUCHAR(ptr)); @@ -96,7 +97,7 @@ t_isprint(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID);; if (clen == 1 || database_ctype_is_c) return isprint(TOUCHAR(ptr)); @@ -266,7 +267,7 @@ char * lowerstr_with_len(const char *str, int len) { char *out; - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (len == 0) return pstrdup(""); diff --git a/src/backend/tsearch/wparser_def.c b/src/backend/tsearch/wparser_def.c index 3919ef27b5..a333d46802 100644 --- a/src/backend/tsearch/wparser_def.c +++ b/src/backend/tsearch/wparser_def.c @@ -17,6 +17,7 @@ #include <limits.h> #include <wctype.h> +#include "catalog/pg_collation.h" #include "commands/defrem.h" #include "mb/pg_wchar.h" #include "miscadmin.h" @@ -299,7 +300,7 @@ TParserInit(char *str, int len) */ if (prs->charmaxlen > 1) { - pg_locale_t mylocale = 0; /* TODO */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); prs->usewide = true; if (database_ctype_is_c) -- 2.34.1
v2-0004-Remove-support-for-null-pg_locale_t.patch (text/x-patch, 10.3K) download From 8d267620c1b0551e81e6ecc6dea6d58cd05df0c2 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:58:59 -0700 Subject: [PATCH v2 4/7] Remove support for null pg_locale_t. Previously, passing NULL for pg_locale_t meant "use the libc provider and the server environment". Now that the database collation is represented as a proper pg_locale_t (not dependent on setlocale()), remove special cases for NULL. --- src/backend/access/hash/hashfunc.c | 4 +-- src/backend/utils/adt/like.c | 2 +- src/backend/utils/adt/pg_locale.c | 57 +++++++++++------------------- src/backend/utils/adt/varchar.c | 4 +-- src/backend/utils/adt/varlena.c | 4 +-- 5 files changed, 27 insertions(+), 44 deletions(-) diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index ce8ee0ea2e..ec2133d4e1 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -280,7 +280,7 @@ hashtext(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); @@ -334,7 +334,7 @@ hashtextextended(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any_extended((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key), diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 57ead66b5a..0807b89b17 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -194,7 +194,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) else locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale_is_c && !pg_locale_deterministic(locale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for ILIKE"))); diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 185b860dad..2ffc551913 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1531,11 +1531,7 @@ report_newlocale_failure(const char *localename) bool pg_locale_deterministic(pg_locale_t locale) { - /* default locale must always be deterministic */ - if (locale == NULL) - return true; - else - return locale->deterministic; + return locale->deterministic; } void @@ -1954,7 +1950,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, int r; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); Assert(GetDatabaseEncoding() == PG_UTF8); #ifndef WIN32 Assert(false); @@ -1994,10 +1990,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, ((LPWSTR) a2p)[r] = 0; errno = 0; - if (locale) - result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); - else - result = wcscoll((LPWSTR) a1p, (LPWSTR) a2p); + result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); if (result == 2147483647) /* _NLSCMPERROR; missing from mingw headers */ ereport(ERROR, (errmsg("could not compare Unicode strings: %m"))); @@ -2023,7 +2016,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 if (GetDatabaseEncoding() == PG_UTF8) { @@ -2034,10 +2027,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) } else #endif /* WIN32 */ - if (locale) result = strcoll_l(arg1, arg2, locale->info.lt); - else - result = strcoll(arg1, arg2); return result; } @@ -2059,7 +2049,7 @@ pg_strncoll_libc(const char *arg1, size_t len1, const char *arg2, size_t len2, char *arg2n; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 /* check for this case before doing the work for nul-termination */ @@ -2205,7 +2195,7 @@ pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strcoll_libc(arg1, arg2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2241,7 +2231,7 @@ pg_strncoll(const char *arg1, size_t len1, const char *arg2, size_t len2, { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strncoll_libc(arg1, len1, arg2, len2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2259,13 +2249,10 @@ static size_t pg_strxfrm_libc(char *dest, const char *src, size_t destsize, pg_locale_t locale) { - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef TRUST_STRXFRM - if (locale) - return strxfrm_l(dest, src, destsize, locale->info.lt); - else - return strxfrm(dest, src, destsize); + return strxfrm_l(dest, src, destsize, locale->info.lt); #else /* shouldn't happen */ PGLOCALE_SUPPORT_ERROR(locale->provider); @@ -2282,7 +2269,7 @@ pg_strnxfrm_libc(char *dest, const char *src, size_t srclen, size_t destsize, size_t bufsize = srclen + 1; size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (bufsize > TEXTBUFLEN) buf = palloc(bufsize); @@ -2454,7 +2441,7 @@ pg_strnxfrm_prefix_icu(char *dest, const char *src, int32_t srclen, bool pg_strxfrm_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) #ifdef TRUST_STRXFRM return true; #else @@ -2488,7 +2475,7 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale) { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strxfrm_libc(dest, src, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2525,7 +2512,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strnxfrm_libc(dest, src, srclen, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2545,7 +2532,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, bool pg_strxfrm_prefix_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) return false; else if (locale->provider == COLLPROVIDER_ICU) return true; @@ -2575,13 +2562,11 @@ pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -2610,13 +2595,11 @@ pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -3166,7 +3149,7 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; @@ -3223,7 +3206,7 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 02dfe219f5..60b1372227 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -1014,7 +1014,7 @@ hashbpchar(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any((unsigned char *) keydata, keylen); } @@ -1069,7 +1069,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any_extended((unsigned char *) keydata, keylen, PG_GETARG_INT64(1)); diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index d2e2e9bbba..9abae63221 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1224,7 +1224,7 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (mylocale && !pg_locale_deterministic(mylocale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); @@ -1803,7 +1803,7 @@ text_starts_with(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (mylocale && !pg_locale_deterministic(mylocale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); -- 2.34.1
v2-0005-Avoid-setlocale-in-lc_collate_is_c-and-lc_ctype_i.patch (text/x-patch, 14.6K) download From 3b208a189ecc0d6942c357f8b616ad0aa9ee5848 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 15:22:04 -0700 Subject: [PATCH v2 5/7] Avoid setlocale() in lc_collate_is_c() and lc_ctype_is_c(). Store the collate and ctype strings in the pg_locale_t structure, which requires some minor refactoring. --- src/backend/regex/regc_pg_locale.c | 44 ++++++++++++++-------------- src/backend/utils/adt/formatting.c | 20 ++++++------- src/backend/utils/adt/like.c | 2 +- src/backend/utils/adt/like_support.c | 2 +- src/backend/utils/adt/pg_locale.c | 30 +++++++++++-------- src/include/utils/pg_locale.h | 7 ++++- 6 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 85f3238eb0..1e180b844f 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -309,11 +309,11 @@ pg_wc_isdigit(pg_wchar c) isdigit((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswdigit_l((wint_t) c, pg_regex_locale->info.lt); + return iswdigit_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isdigit_l((unsigned char) c, pg_regex_locale->info.lt)); + isdigit_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -343,11 +343,11 @@ pg_wc_isalpha(pg_wchar c) isalpha((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalpha_l((wint_t) c, pg_regex_locale->info.lt); + return iswalpha_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isalpha_l((unsigned char) c, pg_regex_locale->info.lt)); + isalpha_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -377,11 +377,11 @@ pg_wc_isalnum(pg_wchar c) isalnum((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalnum_l((wint_t) c, pg_regex_locale->info.lt); + return iswalnum_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isalnum_l((unsigned char) c, pg_regex_locale->info.lt)); + isalnum_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -420,11 +420,11 @@ pg_wc_isupper(pg_wchar c) isupper((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswupper_l((wint_t) c, pg_regex_locale->info.lt); + return iswupper_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isupper_l((unsigned char) c, pg_regex_locale->info.lt)); + isupper_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -454,11 +454,11 @@ pg_wc_islower(pg_wchar c) islower((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswlower_l((wint_t) c, pg_regex_locale->info.lt); + return iswlower_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - islower_l((unsigned char) c, pg_regex_locale->info.lt)); + islower_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -488,11 +488,11 @@ pg_wc_isgraph(pg_wchar c) isgraph((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswgraph_l((wint_t) c, pg_regex_locale->info.lt); + return iswgraph_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isgraph_l((unsigned char) c, pg_regex_locale->info.lt)); + isgraph_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -522,11 +522,11 @@ pg_wc_isprint(pg_wchar c) isprint((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswprint_l((wint_t) c, pg_regex_locale->info.lt); + return iswprint_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isprint_l((unsigned char) c, pg_regex_locale->info.lt)); + isprint_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -556,11 +556,11 @@ pg_wc_ispunct(pg_wchar c) ispunct((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswpunct_l((wint_t) c, pg_regex_locale->info.lt); + return iswpunct_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - ispunct_l((unsigned char) c, pg_regex_locale->info.lt)); + ispunct_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -590,11 +590,11 @@ pg_wc_isspace(pg_wchar c) isspace((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswspace_l((wint_t) c, pg_regex_locale->info.lt); + return iswspace_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: return (c <= (pg_wchar) UCHAR_MAX && - isspace_l((unsigned char) c, pg_regex_locale->info.lt)); + isspace_l((unsigned char) c, pg_regex_locale->info.libc.lt)); break; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -632,11 +632,11 @@ pg_wc_toupper(pg_wchar c) return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towupper_l((wint_t) c, pg_regex_locale->info.lt); + return towupper_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: if (c <= (pg_wchar) UCHAR_MAX) - return toupper_l((unsigned char) c, pg_regex_locale->info.lt); + return toupper_l((unsigned char) c, pg_regex_locale->info.libc.lt); return c; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU @@ -674,11 +674,11 @@ pg_wc_tolower(pg_wchar c) return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towlower_l((wint_t) c, pg_regex_locale->info.lt); + return towlower_l((wint_t) c, pg_regex_locale->info.libc.lt); /* FALL THRU */ case PG_REGEX_LOCALE_1BYTE_L: if (c <= (pg_wchar) UCHAR_MAX) - return tolower_l((unsigned char) c, pg_regex_locale->info.lt); + return tolower_l((unsigned char) c, pg_regex_locale->info.libc.lt); return c; case PG_REGEX_LOCALE_ICU: #ifdef USE_ICU diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 8736ada4be..2c3a28ca25 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1732,7 +1732,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) for (curr_char = 0; workspace[curr_char] != 0; curr_char++) { if (mylocale) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.libc.lt); else workspace[curr_char] = towlower(workspace[curr_char]); } @@ -1763,7 +1763,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) for (p = result; *p; p++) { if (mylocale) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); + *p = tolower_l((unsigned char) *p, mylocale->info.libc.lt); else *p = pg_tolower((unsigned char) *p); } @@ -1880,7 +1880,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) for (curr_char = 0; workspace[curr_char] != 0; curr_char++) { if (mylocale) - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.libc.lt); else workspace[curr_char] = towupper(workspace[curr_char]); } @@ -1911,7 +1911,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) for (p = result; *p; p++) { if (mylocale) - *p = toupper_l((unsigned char) *p, mylocale->info.lt); + *p = toupper_l((unsigned char) *p, mylocale->info.libc.lt); else *p = pg_toupper((unsigned char) *p); } @@ -2084,10 +2084,10 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) if (mylocale) { if (wasalnum) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.libc.lt); else - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); - wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt); + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.libc.lt); + wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.libc.lt); } else { @@ -2127,10 +2127,10 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) if (mylocale) { if (wasalnum) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); + *p = tolower_l((unsigned char) *p, mylocale->info.libc.lt); else - *p = toupper_l((unsigned char) *p, mylocale->info.lt); - wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt); + *p = toupper_l((unsigned char) *p, mylocale->info.libc.lt); + wasalnum = isalnum_l((unsigned char) *p, mylocale->info.libc.lt); } else { diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 0807b89b17..4d9a207816 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -96,7 +96,7 @@ SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) if (locale_is_c) return pg_ascii_tolower(c); else if (locale) - return tolower_l(c, locale->info.lt); + return tolower_l(c, locale->info.libc.lt); else return pg_tolower(c); } diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c index 2635050861..6a1c64176d 100644 --- a/src/backend/utils/adt/like_support.c +++ b/src/backend/utils/adt/like_support.c @@ -1509,7 +1509,7 @@ pattern_char_isalpha(char c, bool is_multibyte, return IS_HIGHBIT_SET(c) || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); else if (locale && locale->provider == COLLPROVIDER_LIBC) - return isalpha_l((unsigned char) c, locale->info.lt); + return isalpha_l((unsigned char) c, locale->info.libc.lt); else return isalpha((unsigned char) c); } diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 2ffc551913..060dcbcb14 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1350,9 +1350,7 @@ lc_collate_is_c(Oid collation) } else if (default_locale.provider == COLLPROVIDER_LIBC) { - localeptr = setlocale(LC_CTYPE, NULL); - if (!localeptr) - elog(ERROR, "invalid LC_CTYPE setting"); + localeptr = default_locale.info.libc.collate; } else elog(ERROR, "unexpected collation provider '%c'", @@ -1416,9 +1414,7 @@ lc_ctype_is_c(Oid collation) } else if (default_locale.provider == COLLPROVIDER_LIBC) { - localeptr = setlocale(LC_CTYPE, NULL); - if (!localeptr) - elog(ERROR, "invalid LC_CTYPE setting"); + localeptr = default_locale.info.libc.ctype; } else elog(ERROR, "unexpected collation provider '%c'", @@ -1629,7 +1625,11 @@ pg_init_database_collation() #endif } - default_locale.info.lt = loc; + default_locale.info.libc.collate = MemoryContextStrdup( + TopMemoryContext, datcollate); + default_locale.info.libc.ctype = MemoryContextStrdup( + TopMemoryContext, datctype); + default_locale.info.libc.lt = loc; } default_locale.provider = dbform->datlocprovider; @@ -1750,7 +1750,11 @@ pg_newlocale_from_collation(Oid collid) #endif } - result.info.lt = loc; + result.info.libc.collate = MemoryContextStrdup( + TopMemoryContext, collcollate); + result.info.libc.ctype = MemoryContextStrdup( + TopMemoryContext, collctype); + result.info.libc.lt = loc; } else if (collform->collprovider == COLLPROVIDER_ICU) { @@ -1990,7 +1994,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, ((LPWSTR) a2p)[r] = 0; errno = 0; - result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); + result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.libc.lt); if (result == 2147483647) /* _NLSCMPERROR; missing from mingw headers */ ereport(ERROR, (errmsg("could not compare Unicode strings: %m"))); @@ -2027,7 +2031,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) } else #endif /* WIN32 */ - result = strcoll_l(arg1, arg2, locale->info.lt); + result = strcoll_l(arg1, arg2, locale->info.libc.lt); return result; } @@ -2252,7 +2256,7 @@ pg_strxfrm_libc(char *dest, const char *src, size_t destsize, Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef TRUST_STRXFRM - return strxfrm_l(dest, src, destsize, locale->info.lt); + return strxfrm_l(dest, src, destsize, locale->info.libc.lt); #else /* shouldn't happen */ PGLOCALE_SUPPORT_ERROR(locale->provider); @@ -3185,7 +3189,7 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) else { /* Use wcstombs_l for nondefault locales */ - result = wcstombs_l(to, from, tolen, locale->info.lt); + result = wcstombs_l(to, from, tolen, locale->info.libc.lt); } return result; @@ -3247,7 +3251,7 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, else { /* Use mbstowcs_l for nondefault locales */ - result = mbstowcs_l(to, str, tolen, locale->info.lt); + result = mbstowcs_l(to, str, tolen, locale->info.libc.lt); } pfree(str); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 3a9026e7b7..63b724dc1d 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -80,7 +80,12 @@ struct pg_locale_struct { const char *locale; } builtin; - locale_t lt; + struct + { + const char *collate; + const char *ctype; + locale_t lt; + } libc; #ifdef USE_ICU struct { -- 2.34.1
v2-0006-Simplify-collation-cache.patch (text/x-patch, 8.9K) download From f8532d5d287ff190917f4ac8bdc3de85ae697e8e Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Fri, 14 Jun 2024 15:13:59 -0700 Subject: [PATCH v2 6/7] Simplify collation cache. Now that the result of pg_newlocale_from_collation() is always non-NULL, move the collate_is_c and ctype_is_c flags into pg_locale_t, and always use that. This commit eliminates the multi-stage initialization of the cache and the extra code in lc_collate_is_c() and lc_ctype_is_c(). --- src/backend/utils/adt/pg_locale.c | 160 +++++------------------------- src/include/utils/pg_locale.h | 2 + 2 files changed, 28 insertions(+), 134 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 060dcbcb14..435a37a0e3 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -128,9 +128,6 @@ static bool CurrentLCTimeValid = false; typedef struct { Oid collid; /* hash key: pg_collation OID */ - bool collate_is_c; /* is collation's LC_COLLATE C? */ - bool ctype_is_c; /* is collation's LC_CTYPE C? */ - bool flags_valid; /* true if above flags are valid */ pg_locale_t locale; /* locale_t struct, or 0 if not valid */ } collation_cache_entry; @@ -1230,7 +1227,7 @@ IsoLocaleName(const char *winlocname) */ static collation_cache_entry * -lookup_collation_cache(Oid collation, bool set_flags) +lookup_collation_cache(Oid collation) { collation_cache_entry *cache_entry; bool found; @@ -1256,59 +1253,9 @@ lookup_collation_cache(Oid collation, bool set_flags) * Make sure cache entry is marked invalid, in case we fail before * setting things. */ - cache_entry->flags_valid = false; cache_entry->locale = 0; } - if (set_flags && !cache_entry->flags_valid) - { - /* Attempt to set the flags */ - HeapTuple tp; - Form_pg_collation collform; - - tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation)); - if (!HeapTupleIsValid(tp)) - elog(ERROR, "cache lookup failed for collation %u", collation); - collform = (Form_pg_collation) GETSTRUCT(tp); - - if (collform->collprovider == COLLPROVIDER_BUILTIN) - { - Datum datum; - const char *colllocale; - - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); - colllocale = TextDatumGetCString(datum); - - cache_entry->collate_is_c = true; - cache_entry->ctype_is_c = (strcmp(colllocale, "C") == 0); - } - else if (collform->collprovider == COLLPROVIDER_LIBC) - { - Datum datum; - const char *collcollate; - const char *collctype; - - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate); - collcollate = TextDatumGetCString(datum); - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collctype); - collctype = TextDatumGetCString(datum); - - cache_entry->collate_is_c = ((strcmp(collcollate, "C") == 0) || - (strcmp(collcollate, "POSIX") == 0)); - cache_entry->ctype_is_c = ((strcmp(collctype, "C") == 0) || - (strcmp(collctype, "POSIX") == 0)); - } - else - { - cache_entry->collate_is_c = false; - cache_entry->ctype_is_c = false; - } - - cache_entry->flags_valid = true; - - ReleaseSysCache(tp); - } - return cache_entry; } @@ -1326,45 +1273,6 @@ lc_collate_is_c(Oid collation) if (!OidIsValid(collation)) return false; - /* - * If we're asked about the default collation, we have to inquire of the C - * library. Cache the result so we only have to compute it once. - */ - if (collation == DEFAULT_COLLATION_OID) - { - static int result = -1; - const char *localeptr; - - if (result >= 0) - return (bool) result; - - if (default_locale.provider == COLLPROVIDER_BUILTIN) - { - result = true; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_ICU) - { - result = false; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_LIBC) - { - localeptr = default_locale.info.libc.collate; - } - else - elog(ERROR, "unexpected collation provider '%c'", - default_locale.provider); - - if (strcmp(localeptr, "C") == 0) - result = true; - else if (strcmp(localeptr, "POSIX") == 0) - result = true; - else - result = false; - return (bool) result; - } - /* * If we're asked about the built-in C/POSIX collations, we know that. */ @@ -1375,7 +1283,7 @@ lc_collate_is_c(Oid collation) /* * Otherwise, we have to consult pg_collation, but we cache that. */ - return (lookup_collation_cache(collation, true))->collate_is_c; + return pg_newlocale_from_collation(collation)->collate_is_c; } /* @@ -1391,44 +1299,6 @@ lc_ctype_is_c(Oid collation) if (!OidIsValid(collation)) return false; - /* - * If we're asked about the default collation, we have to inquire of the C - * library. Cache the result so we only have to compute it once. - */ - if (collation == DEFAULT_COLLATION_OID) - { - static int result = -1; - const char *localeptr; - - if (result >= 0) - return (bool) result; - - if (default_locale.provider == COLLPROVIDER_BUILTIN) - { - localeptr = default_locale.info.builtin.locale; - } - else if (default_locale.provider == COLLPROVIDER_ICU) - { - result = false; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_LIBC) - { - localeptr = default_locale.info.libc.ctype; - } - else - elog(ERROR, "unexpected collation provider '%c'", - default_locale.provider); - - if (strcmp(localeptr, "C") == 0) - result = true; - else if (strcmp(localeptr, "POSIX") == 0) - result = true; - else - result = false; - return (bool) result; - } - /* * If we're asked about the built-in C/POSIX collations, we know that. */ @@ -1439,7 +1309,7 @@ lc_ctype_is_c(Oid collation) /* * Otherwise, we have to consult pg_collation, but we cache that. */ - return (lookup_collation_cache(collation, true))->ctype_is_c; + return pg_newlocale_from_collation(collation)->ctype_is_c; } void @@ -1553,6 +1423,9 @@ pg_init_database_collation() builtin_validate_locale(dbform->encoding, datlocale); + default_locale.collate_is_c = true; + default_locale.ctype_is_c = (strcmp(datlocale, "C") == 0); + default_locale.info.builtin.locale = MemoryContextStrdup( TopMemoryContext, datlocale); } @@ -1564,6 +1437,9 @@ pg_init_database_collation() datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); datlocale = TextDatumGetCString(datum); + default_locale.collate_is_c = false; + default_locale.ctype_is_c = false; + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); if (!isnull) icurules = TextDatumGetCString(datum); @@ -1585,6 +1461,11 @@ pg_init_database_collation() datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype); datctype = TextDatumGetCString(datum); + default_locale.collate_is_c = (strcmp(datcollate, "C") == 0) || + (strcmp(datcollate, "POSIX") == 0); + default_locale.ctype_is_c = (strcmp(datctype, "C") == 0) || + (strcmp(datctype, "POSIX") == 0); + if (strcmp(datcollate, datctype) == 0) { /* Normal case where they're the same */ @@ -1665,7 +1546,7 @@ pg_newlocale_from_collation(Oid collid) if (collid == DEFAULT_COLLATION_OID) return &default_locale; - cache_entry = lookup_collation_cache(collid, false); + cache_entry = lookup_collation_cache(collid); if (cache_entry->locale == 0) { @@ -1694,6 +1575,9 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); locstr = TextDatumGetCString(datum); + result.collate_is_c = true; + result.collate_is_c = (strcmp(locstr, "C") == 0); + builtin_validate_locale(GetDatabaseEncoding(), locstr); result.info.builtin.locale = MemoryContextStrdup(TopMemoryContext, @@ -1710,6 +1594,11 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collctype); collctype = TextDatumGetCString(datum); + result.collate_is_c = (strcmp(collcollate, "C") == 0) || + (strcmp(collcollate, "POSIX") == 0); + result.ctype_is_c = (strcmp(collctype, "C") == 0) || + (strcmp(collctype, "POSIX") == 0); + if (strcmp(collcollate, collctype) == 0) { /* Normal case where they're the same */ @@ -1764,6 +1653,9 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); iculocstr = TextDatumGetCString(datum); + result.collate_is_c = false; + result.ctype_is_c = false; + datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collicurules, &isnull); if (!isnull) icurules = TextDatumGetCString(datum); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 63b724dc1d..a177b09e09 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -74,6 +74,8 @@ struct pg_locale_struct { char provider; bool deterministic; + bool collate_is_c; + bool ctype_is_c; union { struct -- 2.34.1
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-06-19 09:15  Peter Eisentraut <peter@eisentraut.org>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Peter Eisentraut @ 2024-06-19 09:15 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; pgsql-hackers

On 15.06.24 01:35, Jeff Davis wrote:
> On Thu, 2024-06-06 at 11:37 -0700, Jeff Davis wrote:
>>
>> I think this patch series is a nice cleanup, as well, making libc
>> more
>> like the other providers and not dependent on global state.
> 
> New rebased series attached with additional cleanup. Now that
> pg_locale_t is never NULL, we can simplify the way the collation cache
> works, eliminating ~100 lines.

Overall, this is great.  I have wished for a simplification like this
for a long time.  It is the logical continuation of relying on
locale_t stuff rather than process-global settings.


* v2-0001-Make-database-default-collation-internal-to-pg_lo.patch

+void
+pg_init_database_collation()

The function argument should be (void).

The prefix pg_ makes it sound like it's a user-facing function of some
sort.  Is there a reason for it?

Maybe add a small comment before the function.


* v2-0002-Make-database-collation-pg_locale_t-always-non-NU.patch

There is quite a bit of code duplication from
pg_newlocale_from_collation().  Can we do this better?


* v2-0003-ts_locale.c-do-not-use-NULL-to-mean-the-database-.patch

The "TODO" markers should be left, because what they refer to is that
these functions just use the default collation rather than something
passed in from the expression machinery.  This is not addressed by
this change.  (Obviously, the comments could have been clearer about
this.)


* v2-0004-Remove-support-for-null-pg_locale_t.patch

I found a few more places that should be adjusted in a similar way.

- In src/backend/regex/regc_pg_locale.c, the whole business with
   pg_regex_locale, in particular in pg_set_regex_collation().

- In src/backend/utils/adt/formatting.c, various places such as
   str_tolower().

- In src/backend/utils/adt/pg_locale.c, wchar2char() and char2wchar().
   (Also, for wchar2char() there is one caller that explicitly passes
   NULL.)

The changes at the call sites of pg_locale_deterministic() are
unfortunate, because they kind of go into the opposite direction: They
add checks for NULL locales instead of removing them.  (For a minute I
was thinking I was reading your patch backwards.)  We should think of
a way to write this clearer.

Looking for example at hashtext() after 0001..0004 applied, it is

     if (!lc_collate_is_c(collid))
         mylocale = pg_newlocale_from_collation(collid);

     if (!mylocale || pg_locale_deterministic(mylocale))
     {

But then after your 0006 patch, lc_locale_is_c() internally also calls
pg_newlocale_from_collation(), so this code just becomes redundant.
Better might be if pg_locale_deterministic() itself checked if collate
is C, and then hashtext() would just need to write:

     mylocale = pg_newlocale_from_collation(collid);

     if (pg_locale_deterministic(mylocale))
     {

The patch sequencing might be a bit tricky here.  Maybe it's ok if
patch 0004 stays as is in this respect if 0006 were to fix it back.


* v2-0005-Avoid-setlocale-in-lc_collate_is_c-and-lc_ctype_i.patch

Nothing uses this, AFAICT, so why?

Also, this creates more duplication between
pg_init_database_collation() and pg_newlocale_from_collation(), as
mentioned at patch 0002.


* v2-0006-Simplify-collation-cache.patch

ok






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-07-26 06:07  Jeff Davis <pgsql@j-davis.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-07-26 06:07 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On Wed, 2024-06-19 at 11:15 +0200, Peter Eisentraut wrote:
> > * v2-0001-Make-database-default-collation-internal-to-pg_lo.patch
> > 
> > +void
> > +pg_init_database_collation()
> > 
> > The function argument should be (void).
> > 
> > The prefix pg_ makes it sound like it's a user-facing function of >
> > some
> > sort.  Is there a reason for it?
> > 
> > Maybe add a small comment before the function.

Agreed, done.

> > 
> > * v2-0002-Make-database-collation-pg_locale_t-always-non-NU.patch
> > 
> > There is quite a bit of code duplication from
> > pg_newlocale_from_collation().  Can we do this better?

I refactored it into make_libc_collator().

> > * v2-0003-ts_locale.c-do-not-use-NULL-to-mean-the-database-.patch
> > 
> > The "TODO" markers should be left, because what they refer to is
> > that
> > these functions just use the default collation rather than
> > something
> > passed in from the expression machinery.  This is not addressed by
> > this change.  (Obviously, the comments could have been clearer
> > about
> > this.)

Done.

> > * v2-0004-Remove-support-for-null-pg_locale_t.patch
> > 
> > I found a few more places that should be adjusted in a similar way.

Fixed, thank you.

> > The changes at the call sites of pg_locale_deterministic() are
> > unfortunate

Yeah, that part was a bit annoying.

> > But then after your 0006 patch, lc_locale_is_c() internally also >
> > calls
> > pg_newlocale_from_collation()

Not always. It still returns early for C_COLLATION_OID or
POSIX_COLLATION_OID, and that's actually required because
pg_regcomp(..., C_COLLATION_OID) is called when parsing pg_hba.conf,
before catalog access is available. I don't think that detail is
relevant in the cases you brought up, but it got in the way of some
other refactoring I was trying to do.

> > , so this code just becomes redundant.
> > Better might be if pg_locale_deterministic() itself checked if >
> > collate
> > is C, and then hashtext() would just need to write:
> > 
> >      mylocale = pg_newlocale_from_collation(collid);
> > 
> >      if (pg_locale_deterministic(mylocale))
> >      {
> > 
> > The patch sequencing might be a bit tricky here.  Maybe it's ok if
> > patch 0004 stays as is in this respect if 0006 were to fix it back.

Addressed in v3-0006.

> > * v2-0005-Avoid-setlocale-in-lc_collate_is_c-and-lc_ctype_i.patch
> > 
> > Nothing uses this, AFAICT, so why?

You're right. It was used to avoid setlocale() in lc_collate_is_c(),
but that's eliminated in the next patch anyway.


Also, in v3-0005, I had to also check for "C" or "POSIX" in
make_libc_collator(), so that it wouldn't try to actually create the
locale_t in that case.


Regards,
	Jeff Davis

Attachments: download all (6 files, .tar.gz)

v3-0001-Make-database-default-collation-internal-to-pg_lo.patch (text/x-patch, 5.8K) download From bdef8da100a3fed87bce95957c62d9f3d77b0660 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:45:55 -0700 Subject: [PATCH v3 1/6] Make database default collation internal to pg_locale.c. --- src/backend/utils/adt/pg_locale.c | 67 ++++++++++++++++++++++++++++++- src/backend/utils/init/postinit.c | 35 ++-------------- src/include/utils/pg_locale.h | 3 +- 3 files changed, 69 insertions(+), 36 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 38c40a40489..83e09d371d9 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -56,6 +56,7 @@ #include "access/htup_details.h" #include "catalog/pg_collation.h" +#include "catalog/pg_database.h" #include "mb/pg_wchar.h" #include "miscadmin.h" #include "utils/builtins.h" @@ -116,6 +117,8 @@ char *localized_full_months[12 + 1]; /* is the databases's LC_CTYPE the C locale? */ bool database_ctype_is_c = false; +static struct pg_locale_struct default_locale; + /* indicates whether locale information cache is valid */ static bool CurrentLocaleConvValid = false; static bool CurrentLCTimeValid = false; @@ -1443,8 +1446,6 @@ lc_ctype_is_c(Oid collation) return (lookup_collation_cache(collation, true))->ctype_is_c; } -struct pg_locale_struct default_locale; - void make_icu_collator(const char *iculocstr, const char *icurules, @@ -1538,6 +1539,68 @@ pg_locale_deterministic(pg_locale_t locale) return locale->deterministic; } +/* + * Initialize default_locale with database locale settings. + */ +void +init_database_collation(void) +{ + HeapTuple tup; + Form_pg_database dbform; + Datum datum; + bool isnull; + + /* Fetch our pg_database row normally, via syscache */ + tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + if (!HeapTupleIsValid(tup)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + dbform = (Form_pg_database) GETSTRUCT(tup); + + if (dbform->datlocprovider == COLLPROVIDER_BUILTIN) + { + char *datlocale; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); + + builtin_validate_locale(dbform->encoding, datlocale); + + default_locale.info.builtin.locale = MemoryContextStrdup( + TopMemoryContext, datlocale); + } + else if (dbform->datlocprovider == COLLPROVIDER_ICU) + { + char *datlocale; + char *icurules; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); + + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); + if (!isnull) + icurules = TextDatumGetCString(datum); + else + icurules = NULL; + + make_icu_collator(datlocale, icurules, &default_locale); + } + else + { + Assert(dbform->datlocprovider == COLLPROVIDER_LIBC); + } + + default_locale.provider = dbform->datlocprovider; + + /* + * Default locale is currently always deterministic. Nondeterministic + * locales currently don't support pattern matching, which would break a + * lot of things if applied globally. + */ + default_locale.deterministic = true; + + ReleaseSysCache(tup); +} + /* * Create a locale_t from a collation OID. Results are cached for the * lifetime of the backend. Thus, do not free the result with freelocale(). diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 25867c8bd5b..20932d3e592 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -423,43 +423,14 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect strcmp(ctype, "POSIX") == 0) database_ctype_is_c = true; - if (dbform->datlocprovider == COLLPROVIDER_BUILTIN) - { - datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); - datlocale = TextDatumGetCString(datum); - - builtin_validate_locale(dbform->encoding, datlocale); - - default_locale.info.builtin.locale = MemoryContextStrdup( - TopMemoryContext, datlocale); - } - else if (dbform->datlocprovider == COLLPROVIDER_ICU) - { - char *icurules; + init_database_collation(); - datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_datlocale, &isnull); + if (!isnull) datlocale = TextDatumGetCString(datum); - - datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); - if (!isnull) - icurules = TextDatumGetCString(datum); - else - icurules = NULL; - - make_icu_collator(datlocale, icurules, &default_locale); - } else datlocale = NULL; - default_locale.provider = dbform->datlocprovider; - - /* - * Default locale is currently always deterministic. Nondeterministic - * locales currently don't support pattern matching, which would break a - * lot of things if applied globally. - */ - default_locale.deterministic = true; - /* * Check collation version. See similar code in * pg_newlocale_from_collation(). Note that here we warn instead of error diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 040968d6ff2..3e14a261b16 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -93,13 +93,12 @@ struct pg_locale_struct typedef struct pg_locale_struct *pg_locale_t; -extern PGDLLIMPORT struct pg_locale_struct default_locale; - extern void make_icu_collator(const char *iculocstr, const char *icurules, struct pg_locale_struct *resultp); extern bool pg_locale_deterministic(pg_locale_t locale); +extern void init_database_collation(void); extern pg_locale_t pg_newlocale_from_collation(Oid collid); extern char *get_collation_actual_version(char collprovider, const char *collcollate); -- 2.34.1
v3-0002-Make-database-collation-pg_locale_t-always-non-NU.patch (text/x-patch, 6.6K) download From 3b46b159415a388adf877d5224b139a4685d2105 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 15:02:26 -0700 Subject: [PATCH v3 2/6] Make database collation pg_locale_t always non-NULL. Previously, the database collation's pg_locale_t was NULL for the libc provider. This commit properly initializes a pg_locale_t object in all cases. --- src/backend/utils/adt/pg_locale.c | 171 ++++++++++++++++-------------- 1 file changed, 92 insertions(+), 79 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 83e09d371d9..eec5fb90ed8 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1446,6 +1446,85 @@ lc_ctype_is_c(Oid collation) return (lookup_collation_cache(collation, true))->ctype_is_c; } +/* simple subroutine for reporting errors from newlocale() */ +static void +report_newlocale_failure(const char *localename) +{ + int save_errno; + + /* + * Windows doesn't provide any useful error indication from + * _create_locale(), and BSD-derived platforms don't seem to feel they + * need to set errno either (even though POSIX is pretty clear that + * newlocale should do so). So, if errno hasn't been set, assume ENOENT + * is what to report. + */ + if (errno == 0) + errno = ENOENT; + + /* + * ENOENT means "no such locale", not "no such file", so clarify that + * errno with an errdetail message. + */ + save_errno = errno; /* auxiliary funcs might change errno */ + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("could not create locale \"%s\": %m", + localename), + (save_errno == ENOENT ? + errdetail("The operating system could not find any locale data for the locale name \"%s\".", + localename) : 0))); +} + +static void +make_libc_collator(const char *collate, const char *ctype, + pg_locale_t result) +{ + locale_t loc; + + if (strcmp(collate, ctype) == 0) + { + /* Normal case where they're the same */ + errno = 0; +#ifndef WIN32 + loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collate, + NULL); +#else + loc = _create_locale(LC_ALL, collate); +#endif + if (!loc) + report_newlocale_failure(collate); + } + else + { +#ifndef WIN32 + /* We need two newlocale() steps */ + locale_t loc1; + + errno = 0; + loc1 = newlocale(LC_COLLATE_MASK, collate, NULL); + if (!loc1) + report_newlocale_failure(collate); + errno = 0; + loc = newlocale(LC_CTYPE_MASK, ctype, loc1); + if (!loc) + report_newlocale_failure(ctype); +#else + + /* + * XXX The _create_locale() API doesn't appear to support this. Could + * perhaps be worked around by changing pg_locale_t to contain two + * separate fields. + */ + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("collations with different collate and ctype values are not supported on this platform"))); +#endif + } + + result->info.lt = loc; +} + void make_icu_collator(const char *iculocstr, const char *icurules, @@ -1499,36 +1578,6 @@ make_icu_collator(const char *iculocstr, } -/* simple subroutine for reporting errors from newlocale() */ -static void -report_newlocale_failure(const char *localename) -{ - int save_errno; - - /* - * Windows doesn't provide any useful error indication from - * _create_locale(), and BSD-derived platforms don't seem to feel they - * need to set errno either (even though POSIX is pretty clear that - * newlocale should do so). So, if errno hasn't been set, assume ENOENT - * is what to report. - */ - if (errno == 0) - errno = ENOENT; - - /* - * ENOENT means "no such locale", not "no such file", so clarify that - * errno with an errdetail message. - */ - save_errno = errno; /* auxiliary funcs might change errno */ - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("could not create locale \"%s\": %m", - localename), - (save_errno == ENOENT ? - errdetail("The operating system could not find any locale data for the locale name \"%s\".", - localename) : 0))); -} - bool pg_locale_deterministic(pg_locale_t locale) { @@ -1586,7 +1635,17 @@ init_database_collation(void) } else { + const char *datcollate; + const char *datctype; + Assert(dbform->datlocprovider == COLLPROVIDER_LIBC); + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datcollate); + datcollate = TextDatumGetCString(datum); + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype); + datctype = TextDatumGetCString(datum); + + make_libc_collator(datcollate, datctype, &default_locale); } default_locale.provider = dbform->datlocprovider; @@ -1620,12 +1679,7 @@ pg_newlocale_from_collation(Oid collid) Assert(OidIsValid(collid)); if (collid == DEFAULT_COLLATION_OID) - { - if (default_locale.provider == COLLPROVIDER_LIBC) - return (pg_locale_t) 0; - else - return &default_locale; - } + return &default_locale; cache_entry = lookup_collation_cache(collid, false); @@ -1664,55 +1718,14 @@ pg_newlocale_from_collation(Oid collid) else if (collform->collprovider == COLLPROVIDER_LIBC) { const char *collcollate; - const char *collctype pg_attribute_unused(); - locale_t loc; + const char *collctype; datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate); collcollate = TextDatumGetCString(datum); datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collctype); collctype = TextDatumGetCString(datum); - if (strcmp(collcollate, collctype) == 0) - { - /* Normal case where they're the same */ - errno = 0; -#ifndef WIN32 - loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collcollate, - NULL); -#else - loc = _create_locale(LC_ALL, collcollate); -#endif - if (!loc) - report_newlocale_failure(collcollate); - } - else - { -#ifndef WIN32 - /* We need two newlocale() steps */ - locale_t loc1; - - errno = 0; - loc1 = newlocale(LC_COLLATE_MASK, collcollate, NULL); - if (!loc1) - report_newlocale_failure(collcollate); - errno = 0; - loc = newlocale(LC_CTYPE_MASK, collctype, loc1); - if (!loc) - report_newlocale_failure(collctype); -#else - - /* - * XXX The _create_locale() API doesn't appear to support - * this. Could perhaps be worked around by changing - * pg_locale_t to contain two separate fields. - */ - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("collations with different collate and ctype values are not supported on this platform"))); -#endif - } - - result.info.lt = loc; + make_libc_collator(collcollate, collctype, &result); } else if (collform->collprovider == COLLPROVIDER_ICU) { -- 2.34.1
v3-0003-ts_locale.c-do-not-use-NULL-to-mean-the-database-.patch (text/x-patch, 3.5K) download From 634a14ba78329b52de84b4ca0cd546297d2dc212 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 14:48:07 -0700 Subject: [PATCH v3 3/6] ts_locale.c: do not use NULL to mean the database collation. Use pg_newlocale_from_collation(DEFAULT_COLLATION_OID) to explicitly get the database collation. --- src/backend/tsearch/ts_locale.c | 25 +++++++++++++++++++------ src/backend/tsearch/wparser_def.c | 4 +++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/backend/tsearch/ts_locale.c b/src/backend/tsearch/ts_locale.c index bc44599de6a..2d5e3905b41 100644 --- a/src/backend/tsearch/ts_locale.c +++ b/src/backend/tsearch/ts_locale.c @@ -13,6 +13,7 @@ */ #include "postgres.h" +#include "catalog/pg_collation.h" #include "common/string.h" #include "storage/fd.h" #include "tsearch/ts_locale.h" @@ -36,7 +37,9 @@ t_isdigit(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isdigit(TOUCHAR(ptr)); @@ -51,7 +54,9 @@ t_isspace(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isspace(TOUCHAR(ptr)); @@ -66,7 +71,9 @@ t_isalpha(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isalpha(TOUCHAR(ptr)); @@ -81,7 +88,9 @@ t_isalnum(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isalnum(TOUCHAR(ptr)); @@ -96,7 +105,9 @@ t_isprint(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID);; if (clen == 1 || database_ctype_is_c) return isprint(TOUCHAR(ptr)); @@ -266,7 +277,9 @@ char * lowerstr_with_len(const char *str, int len) { char *out; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (len == 0) return pstrdup(""); diff --git a/src/backend/tsearch/wparser_def.c b/src/backend/tsearch/wparser_def.c index 3919ef27b57..03b4a08244f 100644 --- a/src/backend/tsearch/wparser_def.c +++ b/src/backend/tsearch/wparser_def.c @@ -17,6 +17,7 @@ #include <limits.h> #include <wctype.h> +#include "catalog/pg_collation.h" #include "commands/defrem.h" #include "mb/pg_wchar.h" #include "miscadmin.h" @@ -299,7 +300,8 @@ TParserInit(char *str, int len) */ if (prs->charmaxlen > 1) { - pg_locale_t mylocale = 0; /* TODO */ + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); prs->usewide = true; if (database_ctype_is_c) -- 2.34.1
v3-0004-Remove-support-for-null-pg_locale_t.patch (text/x-patch, 26.5K) download From f42e6dd4fbcbe873c527e3de8d100077b7732ccc Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:58:59 -0700 Subject: [PATCH v3 4/6] Remove support for null pg_locale_t. Previously, passing NULL for pg_locale_t meant "use the libc provider and the server environment". Now that the database collation is represented as a proper pg_locale_t (not dependent on setlocale()), remove special cases for NULL. --- src/backend/access/hash/hashfunc.c | 4 +- src/backend/regex/regc_pg_locale.c | 115 ++--------------------------- src/backend/utils/adt/formatting.c | 84 ++++++--------------- src/backend/utils/adt/like.c | 2 +- src/backend/utils/adt/pg_locale.c | 77 ++++++------------- src/backend/utils/adt/varchar.c | 4 +- src/backend/utils/adt/varlena.c | 4 +- 7 files changed, 56 insertions(+), 234 deletions(-) diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index ce8ee0ea2ef..ec2133d4e11 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -280,7 +280,7 @@ hashtext(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); @@ -334,7 +334,7 @@ hashtextextended(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any_extended((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key), diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 85f3238eb07..dfea0e75ad7 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -67,8 +67,6 @@ typedef enum { PG_REGEX_LOCALE_C, /* C locale (encoding independent) */ PG_REGEX_BUILTIN, /* built-in Unicode semantics */ - PG_REGEX_LOCALE_WIDE, /* Use <wctype.h> functions */ - PG_REGEX_LOCALE_1BYTE, /* Use <ctype.h> functions */ PG_REGEX_LOCALE_WIDE_L, /* Use locale_t <wctype.h> functions */ PG_REGEX_LOCALE_1BYTE_L, /* Use locale_t <ctype.h> functions */ PG_REGEX_LOCALE_ICU, /* Use ICU uchar.h functions */ @@ -262,29 +260,19 @@ pg_set_regex_collation(Oid collation) errmsg("nondeterministic collations are not supported for regular expressions"))); #ifdef USE_ICU - if (pg_regex_locale && pg_regex_locale->provider == COLLPROVIDER_ICU) + if (pg_regex_locale->provider == COLLPROVIDER_ICU) pg_regex_strategy = PG_REGEX_LOCALE_ICU; else #endif if (GetDatabaseEncoding() == PG_UTF8) { - if (pg_regex_locale) - { - if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN) - pg_regex_strategy = PG_REGEX_BUILTIN; - else - pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L; - } + if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN) + pg_regex_strategy = PG_REGEX_BUILTIN; else - pg_regex_strategy = PG_REGEX_LOCALE_WIDE; + pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L; } else - { - if (pg_regex_locale) - pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L; - else - pg_regex_strategy = PG_REGEX_LOCALE_1BYTE; - } + pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L; pg_regex_collation = collation; } @@ -300,13 +288,6 @@ pg_wc_isdigit(pg_wchar c) (pg_char_properties[c] & PG_ISDIGIT)); case PG_REGEX_BUILTIN: return pg_u_isdigit(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswdigit((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isdigit((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswdigit_l((wint_t) c, pg_regex_locale->info.lt); @@ -334,13 +315,6 @@ pg_wc_isalpha(pg_wchar c) (pg_char_properties[c] & PG_ISALPHA)); case PG_REGEX_BUILTIN: return pg_u_isalpha(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalpha((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isalpha((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswalpha_l((wint_t) c, pg_regex_locale->info.lt); @@ -368,13 +342,6 @@ pg_wc_isalnum(pg_wchar c) (pg_char_properties[c] & PG_ISALNUM)); case PG_REGEX_BUILTIN: return pg_u_isalnum(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalnum((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isalnum((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswalnum_l((wint_t) c, pg_regex_locale->info.lt); @@ -411,13 +378,6 @@ pg_wc_isupper(pg_wchar c) (pg_char_properties[c] & PG_ISUPPER)); case PG_REGEX_BUILTIN: return pg_u_isupper(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswupper((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isupper((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswupper_l((wint_t) c, pg_regex_locale->info.lt); @@ -445,13 +405,6 @@ pg_wc_islower(pg_wchar c) (pg_char_properties[c] & PG_ISLOWER)); case PG_REGEX_BUILTIN: return pg_u_islower(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswlower((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - islower((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswlower_l((wint_t) c, pg_regex_locale->info.lt); @@ -479,13 +432,6 @@ pg_wc_isgraph(pg_wchar c) (pg_char_properties[c] & PG_ISGRAPH)); case PG_REGEX_BUILTIN: return pg_u_isgraph(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswgraph((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isgraph((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswgraph_l((wint_t) c, pg_regex_locale->info.lt); @@ -513,13 +459,6 @@ pg_wc_isprint(pg_wchar c) (pg_char_properties[c] & PG_ISPRINT)); case PG_REGEX_BUILTIN: return pg_u_isprint(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswprint((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isprint((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswprint_l((wint_t) c, pg_regex_locale->info.lt); @@ -547,13 +486,6 @@ pg_wc_ispunct(pg_wchar c) (pg_char_properties[c] & PG_ISPUNCT)); case PG_REGEX_BUILTIN: return pg_u_ispunct(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswpunct((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - ispunct((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswpunct_l((wint_t) c, pg_regex_locale->info.lt); @@ -581,13 +513,6 @@ pg_wc_isspace(pg_wchar c) (pg_char_properties[c] & PG_ISSPACE)); case PG_REGEX_BUILTIN: return pg_u_isspace(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswspace((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isspace((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswspace_l((wint_t) c, pg_regex_locale->info.lt); @@ -616,20 +541,6 @@ pg_wc_toupper(pg_wchar c) return c; case PG_REGEX_BUILTIN: return unicode_uppercase_simple(c); - case PG_REGEX_LOCALE_WIDE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_toupper((unsigned char) c); - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towupper((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_toupper((unsigned char) c); - if (c <= (pg_wchar) UCHAR_MAX) - return toupper((unsigned char) c); - return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return towupper_l((wint_t) c, pg_regex_locale->info.lt); @@ -658,20 +569,6 @@ pg_wc_tolower(pg_wchar c) return c; case PG_REGEX_BUILTIN: return unicode_lowercase_simple(c); - case PG_REGEX_LOCALE_WIDE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_tolower((unsigned char) c); - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towlower((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_tolower((unsigned char) c); - if (c <= (pg_wchar) UCHAR_MAX) - return tolower((unsigned char) c); - return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return towlower_l((wint_t) c, pg_regex_locale->info.lt); @@ -825,11 +722,9 @@ pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode) case PG_REGEX_BUILTIN: max_chr = (pg_wchar) MAX_SIMPLE_CHR; break; - case PG_REGEX_LOCALE_WIDE: case PG_REGEX_LOCALE_WIDE_L: max_chr = (pg_wchar) MAX_SIMPLE_CHR; break; - case PG_REGEX_LOCALE_1BYTE: case PG_REGEX_LOCALE_1BYTE_L: #if MAX_SIMPLE_CHR >= UCHAR_MAX max_chr = (pg_wchar) UCHAR_MAX; diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 8736ada4be2..68069fcfd3b 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1665,7 +1665,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar; int32_t len_conv; @@ -1681,7 +1681,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -1710,7 +1710,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -1730,12 +1730,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale); for (curr_char = 0; workspace[curr_char] != 0; curr_char++) - { - if (mylocale) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towlower(workspace[curr_char]); - } + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); /* * Make result large enough; case change might change number @@ -1761,12 +1756,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) * collations you get exactly what the collation says. */ for (p = result; *p; p++) - { - if (mylocale) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); - else - *p = pg_tolower((unsigned char) *p); - } + *p = tolower_l((unsigned char) *p, mylocale->info.lt); } } } @@ -1813,7 +1803,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar, len_conv; @@ -1829,7 +1819,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -1858,7 +1848,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -1878,12 +1868,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale); for (curr_char = 0; workspace[curr_char] != 0; curr_char++) - { - if (mylocale) - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towupper(workspace[curr_char]); - } + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); /* * Make result large enough; case change might change number @@ -1909,12 +1894,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) * collations you get exactly what the collation says. */ for (p = result; *p; p++) - { - if (mylocale) - *p = toupper_l((unsigned char) *p, mylocale->info.lt); - else - *p = pg_toupper((unsigned char) *p); - } + *p = toupper_l((unsigned char) *p, mylocale->info.lt); } } } @@ -2003,7 +1983,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar, len_conv; @@ -2019,7 +1999,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -2060,7 +2040,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -2081,22 +2061,11 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) for (curr_char = 0; workspace[curr_char] != 0; curr_char++) { - if (mylocale) - { - if (wasalnum) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); - wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt); - } + if (wasalnum) + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); else - { - if (wasalnum) - workspace[curr_char] = towlower(workspace[curr_char]); - else - workspace[curr_char] = towupper(workspace[curr_char]); - wasalnum = iswalnum(workspace[curr_char]); - } + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); + wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt); } /* @@ -2124,22 +2093,11 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) */ for (p = result; *p; p++) { - if (mylocale) - { - if (wasalnum) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); - else - *p = toupper_l((unsigned char) *p, mylocale->info.lt); - wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt); - } + if (wasalnum) + *p = tolower_l((unsigned char) *p, mylocale->info.lt); else - { - if (wasalnum) - *p = pg_tolower((unsigned char) *p); - else - *p = pg_toupper((unsigned char) *p); - wasalnum = isalnum((unsigned char) *p); - } + *p = toupper_l((unsigned char) *p, mylocale->info.lt); + wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt); } } } diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 57ead66b5aa..0807b89b172 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -194,7 +194,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) else locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale_is_c && !pg_locale_deterministic(locale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for ILIKE"))); diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index eec5fb90ed8..6bb29846581 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1157,7 +1157,7 @@ get_iso_localename(const char *winlocname) char *hyphen; /* Locale names use only ASCII, any conversion locale suffices. */ - rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), NULL); + rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), default_locale); if (rc == -1 || rc == sizeof(iso_lc_messages)) return NULL; @@ -1581,11 +1581,7 @@ make_icu_collator(const char *iculocstr, bool pg_locale_deterministic(pg_locale_t locale) { - /* default locale must always be deterministic */ - if (locale == NULL) - return true; - else - return locale->deterministic; + return locale->deterministic; } /* @@ -1925,7 +1921,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, int r; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); Assert(GetDatabaseEncoding() == PG_UTF8); #ifndef WIN32 Assert(false); @@ -1965,10 +1961,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, ((LPWSTR) a2p)[r] = 0; errno = 0; - if (locale) - result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); - else - result = wcscoll((LPWSTR) a1p, (LPWSTR) a2p); + result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); if (result == 2147483647) /* _NLSCMPERROR; missing from mingw headers */ ereport(ERROR, (errmsg("could not compare Unicode strings: %m"))); @@ -1994,7 +1987,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 if (GetDatabaseEncoding() == PG_UTF8) { @@ -2005,10 +1998,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) } else #endif /* WIN32 */ - if (locale) result = strcoll_l(arg1, arg2, locale->info.lt); - else - result = strcoll(arg1, arg2); return result; } @@ -2030,7 +2020,7 @@ pg_strncoll_libc(const char *arg1, size_t len1, const char *arg2, size_t len2, char *arg2n; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 /* check for this case before doing the work for nul-termination */ @@ -2176,7 +2166,7 @@ pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strcoll_libc(arg1, arg2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2212,7 +2202,7 @@ pg_strncoll(const char *arg1, size_t len1, const char *arg2, size_t len2, { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strncoll_libc(arg1, len1, arg2, len2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2230,13 +2220,10 @@ static size_t pg_strxfrm_libc(char *dest, const char *src, size_t destsize, pg_locale_t locale) { - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef TRUST_STRXFRM - if (locale) - return strxfrm_l(dest, src, destsize, locale->info.lt); - else - return strxfrm(dest, src, destsize); + return strxfrm_l(dest, src, destsize, locale->info.lt); #else /* shouldn't happen */ PGLOCALE_SUPPORT_ERROR(locale->provider); @@ -2253,7 +2240,7 @@ pg_strnxfrm_libc(char *dest, const char *src, size_t srclen, size_t destsize, size_t bufsize = srclen + 1; size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (bufsize > TEXTBUFLEN) buf = palloc(bufsize); @@ -2425,7 +2412,7 @@ pg_strnxfrm_prefix_icu(char *dest, const char *src, int32_t srclen, bool pg_strxfrm_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) #ifdef TRUST_STRXFRM return true; #else @@ -2459,7 +2446,7 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale) { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strxfrm_libc(dest, src, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2496,7 +2483,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strnxfrm_libc(dest, src, srclen, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2516,7 +2503,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, bool pg_strxfrm_prefix_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) return false; else if (locale->provider == COLLPROVIDER_ICU) return true; @@ -2546,13 +2533,11 @@ pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -2581,13 +2566,11 @@ pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -3144,7 +3127,7 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; @@ -3172,12 +3155,6 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) } else #endif /* WIN32 */ - if (locale == (pg_locale_t) 0) - { - /* Use wcstombs directly for the default locale */ - result = wcstombs(to, from, tolen); - } - else { /* Use wcstombs_l for nondefault locales */ result = wcstombs_l(to, from, tolen, locale->info.lt); @@ -3201,7 +3178,7 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; @@ -3234,16 +3211,8 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, /* mbstowcs requires ending '\0' */ char *str = pnstrdup(from, fromlen); - if (locale == (pg_locale_t) 0) - { - /* Use mbstowcs directly for the default locale */ - result = mbstowcs(to, str, tolen); - } - else - { - /* Use mbstowcs_l for nondefault locales */ - result = mbstowcs_l(to, str, tolen, locale->info.lt); - } + /* Use mbstowcs_l for nondefault locales */ + result = mbstowcs_l(to, str, tolen, locale->info.lt); pfree(str); } diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 02dfe219f54..60b13722279 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -1014,7 +1014,7 @@ hashbpchar(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any((unsigned char *) keydata, keylen); } @@ -1069,7 +1069,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (!mylocale || pg_locale_deterministic(mylocale)) { result = hash_any_extended((unsigned char *) keydata, keylen, PG_GETARG_INT64(1)); diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index d2e2e9bbba0..9abae632218 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1224,7 +1224,7 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (mylocale && !pg_locale_deterministic(mylocale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); @@ -1803,7 +1803,7 @@ text_starts_with(PG_FUNCTION_ARGS) if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (mylocale && !pg_locale_deterministic(mylocale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); -- 2.34.1
v3-0005-Simplify-collation-cache.patch (text/x-patch, 10.8K) download From c6b6a72ce80f4a840de29b2d7c116e722f7254d1 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Fri, 14 Jun 2024 15:13:59 -0700 Subject: [PATCH v3 5/6] Simplify collation cache. Now that the result of pg_newlocale_from_collation() is always non-NULL, move the collate_is_c and ctype_is_c flags into pg_locale_t, and always use that. This commit eliminates the multi-stage initialization of the cache and the extra code in lc_collate_is_c() and lc_ctype_is_c(). --- src/backend/utils/adt/pg_locale.c | 218 +++++++++--------------------- src/include/utils/pg_locale.h | 2 + 2 files changed, 64 insertions(+), 156 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 6bb29846581..62a1fc5510b 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -128,9 +128,6 @@ static bool CurrentLCTimeValid = false; typedef struct { Oid collid; /* hash key: pg_collation OID */ - bool collate_is_c; /* is collation's LC_COLLATE C? */ - bool ctype_is_c; /* is collation's LC_CTYPE C? */ - bool flags_valid; /* true if above flags are valid */ pg_locale_t locale; /* locale_t struct, or 0 if not valid */ } collation_cache_entry; @@ -1230,7 +1227,7 @@ IsoLocaleName(const char *winlocname) */ static collation_cache_entry * -lookup_collation_cache(Oid collation, bool set_flags) +lookup_collation_cache(Oid collation) { collation_cache_entry *cache_entry; bool found; @@ -1256,59 +1253,9 @@ lookup_collation_cache(Oid collation, bool set_flags) * Make sure cache entry is marked invalid, in case we fail before * setting things. */ - cache_entry->flags_valid = false; cache_entry->locale = 0; } - if (set_flags && !cache_entry->flags_valid) - { - /* Attempt to set the flags */ - HeapTuple tp; - Form_pg_collation collform; - - tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation)); - if (!HeapTupleIsValid(tp)) - elog(ERROR, "cache lookup failed for collation %u", collation); - collform = (Form_pg_collation) GETSTRUCT(tp); - - if (collform->collprovider == COLLPROVIDER_BUILTIN) - { - Datum datum; - const char *colllocale; - - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); - colllocale = TextDatumGetCString(datum); - - cache_entry->collate_is_c = true; - cache_entry->ctype_is_c = (strcmp(colllocale, "C") == 0); - } - else if (collform->collprovider == COLLPROVIDER_LIBC) - { - Datum datum; - const char *collcollate; - const char *collctype; - - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate); - collcollate = TextDatumGetCString(datum); - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collctype); - collctype = TextDatumGetCString(datum); - - cache_entry->collate_is_c = ((strcmp(collcollate, "C") == 0) || - (strcmp(collcollate, "POSIX") == 0)); - cache_entry->ctype_is_c = ((strcmp(collctype, "C") == 0) || - (strcmp(collctype, "POSIX") == 0)); - } - else - { - cache_entry->collate_is_c = false; - cache_entry->ctype_is_c = false; - } - - cache_entry->flags_valid = true; - - ReleaseSysCache(tp); - } - return cache_entry; } @@ -1326,47 +1273,6 @@ lc_collate_is_c(Oid collation) if (!OidIsValid(collation)) return false; - /* - * If we're asked about the default collation, we have to inquire of the C - * library. Cache the result so we only have to compute it once. - */ - if (collation == DEFAULT_COLLATION_OID) - { - static int result = -1; - const char *localeptr; - - if (result >= 0) - return (bool) result; - - if (default_locale.provider == COLLPROVIDER_BUILTIN) - { - result = true; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_ICU) - { - result = false; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_LIBC) - { - localeptr = setlocale(LC_CTYPE, NULL); - if (!localeptr) - elog(ERROR, "invalid LC_CTYPE setting"); - } - else - elog(ERROR, "unexpected collation provider '%c'", - default_locale.provider); - - if (strcmp(localeptr, "C") == 0) - result = true; - else if (strcmp(localeptr, "POSIX") == 0) - result = true; - else - result = false; - return (bool) result; - } - /* * If we're asked about the built-in C/POSIX collations, we know that. */ @@ -1377,7 +1283,7 @@ lc_collate_is_c(Oid collation) /* * Otherwise, we have to consult pg_collation, but we cache that. */ - return (lookup_collation_cache(collation, true))->collate_is_c; + return pg_newlocale_from_collation(collation)->collate_is_c; } /* @@ -1393,46 +1299,6 @@ lc_ctype_is_c(Oid collation) if (!OidIsValid(collation)) return false; - /* - * If we're asked about the default collation, we have to inquire of the C - * library. Cache the result so we only have to compute it once. - */ - if (collation == DEFAULT_COLLATION_OID) - { - static int result = -1; - const char *localeptr; - - if (result >= 0) - return (bool) result; - - if (default_locale.provider == COLLPROVIDER_BUILTIN) - { - localeptr = default_locale.info.builtin.locale; - } - else if (default_locale.provider == COLLPROVIDER_ICU) - { - result = false; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_LIBC) - { - localeptr = setlocale(LC_CTYPE, NULL); - if (!localeptr) - elog(ERROR, "invalid LC_CTYPE setting"); - } - else - elog(ERROR, "unexpected collation provider '%c'", - default_locale.provider); - - if (strcmp(localeptr, "C") == 0) - result = true; - else if (strcmp(localeptr, "POSIX") == 0) - result = true; - else - result = false; - return (bool) result; - } - /* * If we're asked about the built-in C/POSIX collations, we know that. */ @@ -1443,7 +1309,7 @@ lc_ctype_is_c(Oid collation) /* * Otherwise, we have to consult pg_collation, but we cache that. */ - return (lookup_collation_cache(collation, true))->ctype_is_c; + return pg_newlocale_from_collation(collation)->ctype_is_c; } /* simple subroutine for reporting errors from newlocale() */ @@ -1476,39 +1342,57 @@ report_newlocale_failure(const char *localename) localename) : 0))); } +/* + * Initialize the locale_t field. + * + * The "C" and "POSIX" locales are not actually handled by libc, so set the + * locale_t to zero in that case. + */ static void make_libc_collator(const char *collate, const char *ctype, pg_locale_t result) { - locale_t loc; + locale_t loc = 0; if (strcmp(collate, ctype) == 0) { - /* Normal case where they're the same */ - errno = 0; + if (strcmp(ctype, "C") != 0 && strcmp(ctype, "POSIX") != 0) + { + /* Normal case where they're the same */ + errno = 0; #ifndef WIN32 - loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collate, - NULL); + loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collate, + NULL); #else - loc = _create_locale(LC_ALL, collate); + loc = _create_locale(LC_ALL, collate); #endif - if (!loc) - report_newlocale_failure(collate); + if (!loc) + report_newlocale_failure(collate); + } } else { #ifndef WIN32 /* We need two newlocale() steps */ - locale_t loc1; - - errno = 0; - loc1 = newlocale(LC_COLLATE_MASK, collate, NULL); - if (!loc1) - report_newlocale_failure(collate); - errno = 0; - loc = newlocale(LC_CTYPE_MASK, ctype, loc1); - if (!loc) - report_newlocale_failure(ctype); + locale_t loc1 = 0; + + if (strcmp(collate, "C") != 0 && strcmp(collate, "POSIX") != 0) + { + errno = 0; + loc1 = newlocale(LC_COLLATE_MASK, collate, NULL); + if (!loc1) + report_newlocale_failure(collate); + } + + if (strcmp(ctype, "C") != 0 && strcmp(ctype, "POSIX") != 0) + { + errno = 0; + loc = newlocale(LC_CTYPE_MASK, ctype, loc1); + if (!loc) + report_newlocale_failure(ctype); + } + else + loc = loc1; #else /* @@ -1610,6 +1494,9 @@ init_database_collation(void) builtin_validate_locale(dbform->encoding, datlocale); + default_locale.collate_is_c = true; + default_locale.ctype_is_c = (strcmp(datlocale, "C") == 0); + default_locale.info.builtin.locale = MemoryContextStrdup( TopMemoryContext, datlocale); } @@ -1621,6 +1508,9 @@ init_database_collation(void) datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); datlocale = TextDatumGetCString(datum); + default_locale.collate_is_c = false; + default_locale.ctype_is_c = false; + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); if (!isnull) icurules = TextDatumGetCString(datum); @@ -1641,6 +1531,11 @@ init_database_collation(void) datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype); datctype = TextDatumGetCString(datum); + default_locale.collate_is_c = (strcmp(datcollate, "C") == 0) || + (strcmp(datcollate, "POSIX") == 0); + default_locale.ctype_is_c = (strcmp(datctype, "C") == 0) || + (strcmp(datctype, "POSIX") == 0); + make_libc_collator(datcollate, datctype, &default_locale); } @@ -1677,7 +1572,7 @@ pg_newlocale_from_collation(Oid collid) if (collid == DEFAULT_COLLATION_OID) return &default_locale; - cache_entry = lookup_collation_cache(collid, false); + cache_entry = lookup_collation_cache(collid); if (cache_entry->locale == 0) { @@ -1706,6 +1601,9 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); locstr = TextDatumGetCString(datum); + result.collate_is_c = true; + result.collate_is_c = (strcmp(locstr, "C") == 0); + builtin_validate_locale(GetDatabaseEncoding(), locstr); result.info.builtin.locale = MemoryContextStrdup(TopMemoryContext, @@ -1721,6 +1619,11 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collctype); collctype = TextDatumGetCString(datum); + result.collate_is_c = (strcmp(collcollate, "C") == 0) || + (strcmp(collcollate, "POSIX") == 0); + result.ctype_is_c = (strcmp(collctype, "C") == 0) || + (strcmp(collctype, "POSIX") == 0); + make_libc_collator(collcollate, collctype, &result); } else if (collform->collprovider == COLLPROVIDER_ICU) @@ -1731,6 +1634,9 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); iculocstr = TextDatumGetCString(datum); + result.collate_is_c = false; + result.ctype_is_c = false; + datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collicurules, &isnull); if (!isnull) icurules = TextDatumGetCString(datum); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 3e14a261b16..29d078fafaa 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -74,6 +74,8 @@ struct pg_locale_struct { char provider; bool deterministic; + bool collate_is_c; + bool ctype_is_c; union { struct -- 2.34.1
v3-0006-Clean-up-more-checks-for-NULL-pg_locale_t.patch (text/x-patch, 7.4K) download From 9d1897fbc19fc39a4a918a5eba5b2f168dbf0597 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Thu, 25 Jul 2024 16:41:51 -0700 Subject: [PATCH v3 6/6] Clean up more checks for NULL pg_locale_t. --- src/backend/access/hash/hashfunc.c | 14 ++++++------- src/backend/utils/adt/like.c | 12 ++++------- src/backend/utils/adt/varchar.c | 14 ++++++------- src/backend/utils/adt/varlena.c | 32 ++++++++++-------------------- 4 files changed, 27 insertions(+), 45 deletions(-) diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index ec2133d4e11..d151751e185 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -268,7 +268,7 @@ hashtext(PG_FUNCTION_ARGS) { text *key = PG_GETARG_TEXT_PP(0); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -277,10 +277,9 @@ hashtext(PG_FUNCTION_ARGS) errmsg("could not determine which collation to use for string hashing"), errhint("Use the COLLATE clause to set the collation explicitly."))); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (!mylocale || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { result = hash_any((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); @@ -322,7 +321,7 @@ hashtextextended(PG_FUNCTION_ARGS) { text *key = PG_GETARG_TEXT_PP(0); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -331,10 +330,9 @@ hashtextextended(PG_FUNCTION_ARGS) errmsg("could not determine which collation to use for string hashing"), errhint("Use the COLLATE clause to set the collation explicitly."))); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (!mylocale || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { result = hash_any_extended((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key), diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 0807b89b172..0ecc96d48e5 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -174,8 +174,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) *p; int slen, plen; - pg_locale_t locale = 0; - bool locale_is_c = false; + pg_locale_t locale; if (!OidIsValid(collation)) { @@ -189,12 +188,9 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) errhint("Use the COLLATE clause to set the collation explicitly."))); } - if (lc_ctype_is_c(collation)) - locale_is_c = true; - else - locale = pg_newlocale_from_collation(collation); + locale = pg_newlocale_from_collation(collation); - if (!locale_is_c && !pg_locale_deterministic(locale)) + if (!pg_locale_deterministic(locale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for ILIKE"))); @@ -228,7 +224,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) plen = VARSIZE_ANY_EXHDR(pat); s = VARDATA_ANY(str); slen = VARSIZE_ANY_EXHDR(str); - return SB_IMatchText(s, slen, p, plen, locale, locale_is_c); + return SB_IMatchText(s, slen, p, plen, locale, locale->ctype_is_c); } } diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 60b13722279..829375cd1a3 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -999,7 +999,7 @@ hashbpchar(PG_FUNCTION_ARGS) Oid collid = PG_GET_COLLATION(); char *keydata; int keylen; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -1011,10 +1011,9 @@ hashbpchar(PG_FUNCTION_ARGS) keydata = VARDATA_ANY(key); keylen = bcTruelen(key); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (!mylocale || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { result = hash_any((unsigned char *) keydata, keylen); } @@ -1054,7 +1053,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) Oid collid = PG_GET_COLLATION(); char *keydata; int keylen; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -1066,10 +1065,9 @@ hashbpcharextended(PG_FUNCTION_ARGS) keydata = VARDATA_ANY(key); keylen = bcTruelen(key); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (!mylocale || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { result = hash_any_extended((unsigned char *) keydata, keylen, PG_GETARG_INT64(1)); diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 9abae632218..52ab8c43c66 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1217,14 +1217,13 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) { int len1 = VARSIZE_ANY_EXHDR(t1); int len2 = VARSIZE_ANY_EXHDR(t2); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; check_collation_set(collid); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (mylocale && !pg_locale_deterministic(mylocale)) + if (!pg_locale_deterministic(mylocale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); @@ -1619,18 +1618,14 @@ Datum texteq(PG_FUNCTION_ARGS) { Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; pg_locale_t mylocale = 0; bool result; check_collation_set(collid); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1678,18 +1673,14 @@ Datum textne(PG_FUNCTION_ARGS) { Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; bool result; check_collation_set(collid); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1793,17 +1784,16 @@ text_starts_with(PG_FUNCTION_ARGS) Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; bool result; Size len1, len2; check_collation_set(collid); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (mylocale && !pg_locale_deterministic(mylocale)) + if (!pg_locale_deterministic(mylocale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); -- 2.34.1
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-07-26 17:38  Andreas Karlsson <andreas@proxel.se>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Andreas Karlsson @ 2024-07-26 17:38 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

Nice refactoring!

Two small comments about CheckMyDatabase().

- Shouldn't we look at the default_locale.ctype_is_c when setting 
database_ctype_is_c instead of doing a strcmp()? or maybe we should even 
remove the global variable and always look at the default_locale?

- I think that the lookup of Anum_pg_database_datlocale could be done 
later in the code since it is not needed when we use a libc locale. E.g. 
as below.

     if (dbform->datlocprovider == COLLPROVIDER_LIBC)
         locale = collate;
     else
     {
         datum = SysCacheGetAttr(DATABASEOID, tup, 
Anum_pg_database_datlocale, &isnull);
         if (!isnull)
         locale = TextDatumGetCString(datum);
     }

Also is there any reaosn you do not squash th 4th and the 6th patch?

Andreas





^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-07-26 20:35  Jeff Davis <pgsql@j-davis.com>
  parent: Andreas Karlsson <andreas@proxel.se>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-07-26 20:35 UTC (permalink / raw)
  To: Andreas Karlsson <andreas@proxel.se>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On Fri, 2024-07-26 at 19:38 +0200, Andreas Karlsson wrote:
> Nice refactoring!
> 
> Two small comments about CheckMyDatabase().
> 
> - Shouldn't we look at the default_locale.ctype_is_c when setting 
> database_ctype_is_c instead of doing a strcmp()? or maybe we should
> even 
> remove the global variable and always look at the default_locale?

database_ctype_is_c refers to the LC_CTYPE environment of the database
-- pg_database.datctype. default_locale.ctype_is_c is the ctype of the
database's default collation.

Confusing, I know, but it matters for a few things that still depend on
the LC_CTYPE, such as tsearch and maybe a few extensions. See
f413941f41.

> - I think that the lookup of Anum_pg_database_datlocale could be done
> later in the code since it is not needed when we use a libc locale.
> E.g. 
> as below.

Done, thank you.

> Also is there any reaosn you do not squash th 4th and the 6th patch?

Done. I had to rearrange the patch ordering a bit because prior to the
cache refactoring patch, it's unsafe to call
pg_newlocale_from_collation() without checking lc_collate_is_c() or
lc_ctype_is_c() first.

Regards,
	Jeff Davis

Attachments: download all (5 files, .tar.gz)

v4-0001-Make-database-default-collation-internal-to-pg_lo.patch (text/x-patch, 6.5K) download From 8a98af04912afedcb481d0e3851a485a63baf3d9 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:45:55 -0700 Subject: [PATCH v4 1/5] Make database default collation internal to pg_locale.c. --- src/backend/utils/adt/pg_locale.c | 69 +++++++++++++++++++++++++++++-- src/backend/utils/init/postinit.c | 44 ++++---------------- src/include/utils/pg_locale.h | 3 +- 3 files changed, 74 insertions(+), 42 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 38c40a40489..1653e997d9b 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -56,6 +56,7 @@ #include "access/htup_details.h" #include "catalog/pg_collation.h" +#include "catalog/pg_database.h" #include "mb/pg_wchar.h" #include "miscadmin.h" #include "utils/builtins.h" @@ -116,6 +117,8 @@ char *localized_full_months[12 + 1]; /* is the databases's LC_CTYPE the C locale? */ bool database_ctype_is_c = false; +static struct pg_locale_struct default_locale; + /* indicates whether locale information cache is valid */ static bool CurrentLocaleConvValid = false; static bool CurrentLCTimeValid = false; @@ -1443,8 +1446,6 @@ lc_ctype_is_c(Oid collation) return (lookup_collation_cache(collation, true))->ctype_is_c; } -struct pg_locale_struct default_locale; - void make_icu_collator(const char *iculocstr, const char *icurules, @@ -1539,7 +1540,69 @@ pg_locale_deterministic(pg_locale_t locale) } /* - * Create a locale_t from a collation OID. Results are cached for the + * Initialize default_locale with database locale settings. + */ +void +init_database_collation(void) +{ + HeapTuple tup; + Form_pg_database dbform; + Datum datum; + bool isnull; + + /* Fetch our pg_database row normally, via syscache */ + tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + if (!HeapTupleIsValid(tup)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + dbform = (Form_pg_database) GETSTRUCT(tup); + + if (dbform->datlocprovider == COLLPROVIDER_BUILTIN) + { + char *datlocale; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); + + builtin_validate_locale(dbform->encoding, datlocale); + + default_locale.info.builtin.locale = MemoryContextStrdup( + TopMemoryContext, datlocale); + } + else if (dbform->datlocprovider == COLLPROVIDER_ICU) + { + char *datlocale; + char *icurules; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); + + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); + if (!isnull) + icurules = TextDatumGetCString(datum); + else + icurules = NULL; + + make_icu_collator(datlocale, icurules, &default_locale); + } + else + { + Assert(dbform->datlocprovider == COLLPROVIDER_LIBC); + } + + default_locale.provider = dbform->datlocprovider; + + /* + * Default locale is currently always deterministic. Nondeterministic + * locales currently don't support pattern matching, which would break a + * lot of things if applied globally. + */ + default_locale.deterministic = true; + + ReleaseSysCache(tup); +} + +/* + * Create a pg_locale_t from a collation OID. Results are cached for the * lifetime of the backend. Thus, do not free the result with freelocale(). * * As a special optimization, the default/database collation returns 0. diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 25867c8bd5b..3537df37056 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -318,7 +318,6 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect bool isnull; char *collate; char *ctype; - char *datlocale; /* Fetch our pg_database row normally, via syscache */ tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); @@ -423,42 +422,7 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect strcmp(ctype, "POSIX") == 0) database_ctype_is_c = true; - if (dbform->datlocprovider == COLLPROVIDER_BUILTIN) - { - datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); - datlocale = TextDatumGetCString(datum); - - builtin_validate_locale(dbform->encoding, datlocale); - - default_locale.info.builtin.locale = MemoryContextStrdup( - TopMemoryContext, datlocale); - } - else if (dbform->datlocprovider == COLLPROVIDER_ICU) - { - char *icurules; - - datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); - datlocale = TextDatumGetCString(datum); - - datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); - if (!isnull) - icurules = TextDatumGetCString(datum); - else - icurules = NULL; - - make_icu_collator(datlocale, icurules, &default_locale); - } - else - datlocale = NULL; - - default_locale.provider = dbform->datlocprovider; - - /* - * Default locale is currently always deterministic. Nondeterministic - * locales currently don't support pattern matching, which would break a - * lot of things if applied globally. - */ - default_locale.deterministic = true; + init_database_collation(); /* * Check collation version. See similar code in @@ -478,7 +442,13 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect if (dbform->datlocprovider == COLLPROVIDER_LIBC) locale = collate; else + { + char *datlocale; + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); + datlocale = TextDatumGetCString(datum); locale = datlocale; + } actual_versionstr = get_collation_actual_version(dbform->datlocprovider, locale); if (!actual_versionstr) diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 040968d6ff2..3e14a261b16 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -93,13 +93,12 @@ struct pg_locale_struct typedef struct pg_locale_struct *pg_locale_t; -extern PGDLLIMPORT struct pg_locale_struct default_locale; - extern void make_icu_collator(const char *iculocstr, const char *icurules, struct pg_locale_struct *resultp); extern bool pg_locale_deterministic(pg_locale_t locale); +extern void init_database_collation(void); extern pg_locale_t pg_newlocale_from_collation(Oid collid); extern char *get_collation_actual_version(char collprovider, const char *collcollate); -- 2.34.1
v4-0002-Make-database-collation-pg_locale_t-always-non-NU.patch (text/x-patch, 7.5K) download From b26ffe549028e204e564582fc486759bcdc5ab5b Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 15:02:26 -0700 Subject: [PATCH v4 2/5] Make database collation pg_locale_t always non-NULL. Previously, the database collation's pg_locale_t was NULL for the libc provider. This commit properly initializes a pg_locale_t object in all cases. --- src/backend/utils/adt/pg_locale.c | 191 +++++++++++++++++------------- 1 file changed, 110 insertions(+), 81 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 1653e997d9b..598b42b1767 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1446,6 +1446,103 @@ lc_ctype_is_c(Oid collation) return (lookup_collation_cache(collation, true))->ctype_is_c; } +/* simple subroutine for reporting errors from newlocale() */ +static void +report_newlocale_failure(const char *localename) +{ + int save_errno; + + /* + * Windows doesn't provide any useful error indication from + * _create_locale(), and BSD-derived platforms don't seem to feel they + * need to set errno either (even though POSIX is pretty clear that + * newlocale should do so). So, if errno hasn't been set, assume ENOENT + * is what to report. + */ + if (errno == 0) + errno = ENOENT; + + /* + * ENOENT means "no such locale", not "no such file", so clarify that + * errno with an errdetail message. + */ + save_errno = errno; /* auxiliary funcs might change errno */ + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("could not create locale \"%s\": %m", + localename), + (save_errno == ENOENT ? + errdetail("The operating system could not find any locale data for the locale name \"%s\".", + localename) : 0))); +} + +/* + * Initialize the locale_t field. + * + * The "C" and "POSIX" locales are not actually handled by libc, so set the + * locale_t to zero in that case. + */ +static void +make_libc_collator(const char *collate, const char *ctype, + pg_locale_t result) +{ + locale_t loc = 0; + + if (strcmp(collate, ctype) == 0) + { + if (strcmp(ctype, "C") != 0 && strcmp(ctype, "POSIX") != 0) + { + /* Normal case where they're the same */ + errno = 0; +#ifndef WIN32 + loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collate, + NULL); +#else + loc = _create_locale(LC_ALL, collate); +#endif + if (!loc) + report_newlocale_failure(collate); + } + } + else + { +#ifndef WIN32 + /* We need two newlocale() steps */ + locale_t loc1 = 0; + + if (strcmp(collate, "C") != 0 && strcmp(collate, "POSIX") != 0) + { + errno = 0; + loc1 = newlocale(LC_COLLATE_MASK, collate, NULL); + if (!loc1) + report_newlocale_failure(collate); + } + + if (strcmp(ctype, "C") != 0 && strcmp(ctype, "POSIX") != 0) + { + errno = 0; + loc = newlocale(LC_CTYPE_MASK, ctype, loc1); + if (!loc) + report_newlocale_failure(ctype); + } + else + loc = loc1; +#else + + /* + * XXX The _create_locale() API doesn't appear to support this. Could + * perhaps be worked around by changing pg_locale_t to contain two + * separate fields. + */ + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("collations with different collate and ctype values are not supported on this platform"))); +#endif + } + + result->info.lt = loc; +} + void make_icu_collator(const char *iculocstr, const char *icurules, @@ -1499,36 +1596,6 @@ make_icu_collator(const char *iculocstr, } -/* simple subroutine for reporting errors from newlocale() */ -static void -report_newlocale_failure(const char *localename) -{ - int save_errno; - - /* - * Windows doesn't provide any useful error indication from - * _create_locale(), and BSD-derived platforms don't seem to feel they - * need to set errno either (even though POSIX is pretty clear that - * newlocale should do so). So, if errno hasn't been set, assume ENOENT - * is what to report. - */ - if (errno == 0) - errno = ENOENT; - - /* - * ENOENT means "no such locale", not "no such file", so clarify that - * errno with an errdetail message. - */ - save_errno = errno; /* auxiliary funcs might change errno */ - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("could not create locale \"%s\": %m", - localename), - (save_errno == ENOENT ? - errdetail("The operating system could not find any locale data for the locale name \"%s\".", - localename) : 0))); -} - bool pg_locale_deterministic(pg_locale_t locale) { @@ -1586,7 +1653,17 @@ init_database_collation(void) } else { + const char *datcollate; + const char *datctype; + Assert(dbform->datlocprovider == COLLPROVIDER_LIBC); + + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datcollate); + datcollate = TextDatumGetCString(datum); + datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype); + datctype = TextDatumGetCString(datum); + + make_libc_collator(datcollate, datctype, &default_locale); } default_locale.provider = dbform->datlocprovider; @@ -1605,8 +1682,6 @@ init_database_collation(void) * Create a pg_locale_t from a collation OID. Results are cached for the * lifetime of the backend. Thus, do not free the result with freelocale(). * - * As a special optimization, the default/database collation returns 0. - * * For simplicity, we always generate COLLATE + CTYPE even though we * might only need one of them. Since this is called only once per session, * it shouldn't cost much. @@ -1620,12 +1695,7 @@ pg_newlocale_from_collation(Oid collid) Assert(OidIsValid(collid)); if (collid == DEFAULT_COLLATION_OID) - { - if (default_locale.provider == COLLPROVIDER_LIBC) - return (pg_locale_t) 0; - else - return &default_locale; - } + return &default_locale; cache_entry = lookup_collation_cache(collid, false); @@ -1664,55 +1734,14 @@ pg_newlocale_from_collation(Oid collid) else if (collform->collprovider == COLLPROVIDER_LIBC) { const char *collcollate; - const char *collctype pg_attribute_unused(); - locale_t loc; + const char *collctype; datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate); collcollate = TextDatumGetCString(datum); datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collctype); collctype = TextDatumGetCString(datum); - if (strcmp(collcollate, collctype) == 0) - { - /* Normal case where they're the same */ - errno = 0; -#ifndef WIN32 - loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collcollate, - NULL); -#else - loc = _create_locale(LC_ALL, collcollate); -#endif - if (!loc) - report_newlocale_failure(collcollate); - } - else - { -#ifndef WIN32 - /* We need two newlocale() steps */ - locale_t loc1; - - errno = 0; - loc1 = newlocale(LC_COLLATE_MASK, collcollate, NULL); - if (!loc1) - report_newlocale_failure(collcollate); - errno = 0; - loc = newlocale(LC_CTYPE_MASK, collctype, loc1); - if (!loc) - report_newlocale_failure(collctype); -#else - - /* - * XXX The _create_locale() API doesn't appear to support - * this. Could perhaps be worked around by changing - * pg_locale_t to contain two separate fields. - */ - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("collations with different collate and ctype values are not supported on this platform"))); -#endif - } - - result.info.lt = loc; + make_libc_collator(collcollate, collctype, &result); } else if (collform->collprovider == COLLPROVIDER_ICU) { -- 2.34.1
v4-0003-Refactor-collation-cache.patch (text/x-patch, 11.1K) download From 6c083830ae4c5be22801b5670866689e84eb0510 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Fri, 14 Jun 2024 15:13:59 -0700 Subject: [PATCH v4 3/5] Refactor collation cache. Now that the result of pg_newlocale_from_collation() is always non-NULL, move the collate_is_c and ctype_is_c flags into pg_locale_t, and always use that. This commit eliminates the multi-stage initialization of the cache and the extra code in lc_collate_is_c() and lc_ctype_is_c(). It also makes it safe to call pg_newlocale_from_collation() before checking lc_collate_is_c() or lc_ctype_is_c(). --- src/backend/utils/adt/pg_locale.c | 180 +++++------------------------- src/include/utils/pg_locale.h | 14 +++ 2 files changed, 40 insertions(+), 154 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 598b42b1767..42d8bc5deda 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -128,9 +128,6 @@ static bool CurrentLCTimeValid = false; typedef struct { Oid collid; /* hash key: pg_collation OID */ - bool collate_is_c; /* is collation's LC_COLLATE C? */ - bool ctype_is_c; /* is collation's LC_CTYPE C? */ - bool flags_valid; /* true if above flags are valid */ pg_locale_t locale; /* locale_t struct, or 0 if not valid */ } collation_cache_entry; @@ -1208,29 +1205,13 @@ IsoLocaleName(const char *winlocname) /* * Cache mechanism for collation information. * - * We cache two flags: whether the collation's LC_COLLATE or LC_CTYPE is C - * (or POSIX), so we can optimize a few code paths in various places. - * For the built-in C and POSIX collations, we can know that without even - * doing a cache lookup, but we want to support aliases for C/POSIX too. - * For the "default" collation, there are separate static cache variables, - * since consulting the pg_collation catalog doesn't tell us what we need. - * - * Also, if a pg_locale_t has been requested for a collation, we cache that - * for the life of a backend. - * - * Note that some code relies on the flags not reporting false negatives - * (that is, saying it's not C when it is). For example, char2wchar() - * could fail if the locale is C, so str_tolower() shouldn't call it - * in that case. - * * Note that we currently lack any way to flush the cache. Since we don't * support ALTER COLLATION, this is OK. The worst case is that someone * drops a collation, and a useless cache entry hangs around in existing * backends. */ - static collation_cache_entry * -lookup_collation_cache(Oid collation, bool set_flags) +lookup_collation_cache(Oid collation) { collation_cache_entry *cache_entry; bool found; @@ -1256,59 +1237,9 @@ lookup_collation_cache(Oid collation, bool set_flags) * Make sure cache entry is marked invalid, in case we fail before * setting things. */ - cache_entry->flags_valid = false; cache_entry->locale = 0; } - if (set_flags && !cache_entry->flags_valid) - { - /* Attempt to set the flags */ - HeapTuple tp; - Form_pg_collation collform; - - tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation)); - if (!HeapTupleIsValid(tp)) - elog(ERROR, "cache lookup failed for collation %u", collation); - collform = (Form_pg_collation) GETSTRUCT(tp); - - if (collform->collprovider == COLLPROVIDER_BUILTIN) - { - Datum datum; - const char *colllocale; - - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); - colllocale = TextDatumGetCString(datum); - - cache_entry->collate_is_c = true; - cache_entry->ctype_is_c = (strcmp(colllocale, "C") == 0); - } - else if (collform->collprovider == COLLPROVIDER_LIBC) - { - Datum datum; - const char *collcollate; - const char *collctype; - - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate); - collcollate = TextDatumGetCString(datum); - datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collctype); - collctype = TextDatumGetCString(datum); - - cache_entry->collate_is_c = ((strcmp(collcollate, "C") == 0) || - (strcmp(collcollate, "POSIX") == 0)); - cache_entry->ctype_is_c = ((strcmp(collctype, "C") == 0) || - (strcmp(collctype, "POSIX") == 0)); - } - else - { - cache_entry->collate_is_c = false; - cache_entry->ctype_is_c = false; - } - - cache_entry->flags_valid = true; - - ReleaseSysCache(tp); - } - return cache_entry; } @@ -1326,47 +1257,6 @@ lc_collate_is_c(Oid collation) if (!OidIsValid(collation)) return false; - /* - * If we're asked about the default collation, we have to inquire of the C - * library. Cache the result so we only have to compute it once. - */ - if (collation == DEFAULT_COLLATION_OID) - { - static int result = -1; - const char *localeptr; - - if (result >= 0) - return (bool) result; - - if (default_locale.provider == COLLPROVIDER_BUILTIN) - { - result = true; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_ICU) - { - result = false; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_LIBC) - { - localeptr = setlocale(LC_CTYPE, NULL); - if (!localeptr) - elog(ERROR, "invalid LC_CTYPE setting"); - } - else - elog(ERROR, "unexpected collation provider '%c'", - default_locale.provider); - - if (strcmp(localeptr, "C") == 0) - result = true; - else if (strcmp(localeptr, "POSIX") == 0) - result = true; - else - result = false; - return (bool) result; - } - /* * If we're asked about the built-in C/POSIX collations, we know that. */ @@ -1377,7 +1267,7 @@ lc_collate_is_c(Oid collation) /* * Otherwise, we have to consult pg_collation, but we cache that. */ - return (lookup_collation_cache(collation, true))->collate_is_c; + return pg_newlocale_from_collation(collation)->collate_is_c; } /* @@ -1393,46 +1283,6 @@ lc_ctype_is_c(Oid collation) if (!OidIsValid(collation)) return false; - /* - * If we're asked about the default collation, we have to inquire of the C - * library. Cache the result so we only have to compute it once. - */ - if (collation == DEFAULT_COLLATION_OID) - { - static int result = -1; - const char *localeptr; - - if (result >= 0) - return (bool) result; - - if (default_locale.provider == COLLPROVIDER_BUILTIN) - { - localeptr = default_locale.info.builtin.locale; - } - else if (default_locale.provider == COLLPROVIDER_ICU) - { - result = false; - return (bool) result; - } - else if (default_locale.provider == COLLPROVIDER_LIBC) - { - localeptr = setlocale(LC_CTYPE, NULL); - if (!localeptr) - elog(ERROR, "invalid LC_CTYPE setting"); - } - else - elog(ERROR, "unexpected collation provider '%c'", - default_locale.provider); - - if (strcmp(localeptr, "C") == 0) - result = true; - else if (strcmp(localeptr, "POSIX") == 0) - result = true; - else - result = false; - return (bool) result; - } - /* * If we're asked about the built-in C/POSIX collations, we know that. */ @@ -1443,7 +1293,7 @@ lc_ctype_is_c(Oid collation) /* * Otherwise, we have to consult pg_collation, but we cache that. */ - return (lookup_collation_cache(collation, true))->ctype_is_c; + return pg_newlocale_from_collation(collation)->ctype_is_c; } /* simple subroutine for reporting errors from newlocale() */ @@ -1632,6 +1482,9 @@ init_database_collation(void) builtin_validate_locale(dbform->encoding, datlocale); + default_locale.collate_is_c = true; + default_locale.ctype_is_c = (strcmp(datlocale, "C") == 0); + default_locale.info.builtin.locale = MemoryContextStrdup( TopMemoryContext, datlocale); } @@ -1643,6 +1496,9 @@ init_database_collation(void) datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale); datlocale = TextDatumGetCString(datum); + default_locale.collate_is_c = false; + default_locale.ctype_is_c = false; + datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull); if (!isnull) icurules = TextDatumGetCString(datum); @@ -1663,6 +1519,11 @@ init_database_collation(void) datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype); datctype = TextDatumGetCString(datum); + default_locale.collate_is_c = (strcmp(datcollate, "C") == 0) || + (strcmp(datcollate, "POSIX") == 0); + default_locale.ctype_is_c = (strcmp(datctype, "C") == 0) || + (strcmp(datctype, "POSIX") == 0); + make_libc_collator(datcollate, datctype, &default_locale); } @@ -1697,7 +1558,7 @@ pg_newlocale_from_collation(Oid collid) if (collid == DEFAULT_COLLATION_OID) return &default_locale; - cache_entry = lookup_collation_cache(collid, false); + cache_entry = lookup_collation_cache(collid); if (cache_entry->locale == 0) { @@ -1726,6 +1587,9 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); locstr = TextDatumGetCString(datum); + result.collate_is_c = true; + result.collate_is_c = (strcmp(locstr, "C") == 0); + builtin_validate_locale(GetDatabaseEncoding(), locstr); result.info.builtin.locale = MemoryContextStrdup(TopMemoryContext, @@ -1741,6 +1605,11 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collctype); collctype = TextDatumGetCString(datum); + result.collate_is_c = (strcmp(collcollate, "C") == 0) || + (strcmp(collcollate, "POSIX") == 0); + result.ctype_is_c = (strcmp(collctype, "C") == 0) || + (strcmp(collctype, "POSIX") == 0); + make_libc_collator(collcollate, collctype, &result); } else if (collform->collprovider == COLLPROVIDER_ICU) @@ -1751,6 +1620,9 @@ pg_newlocale_from_collation(Oid collid) datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale); iculocstr = TextDatumGetCString(datum); + result.collate_is_c = false; + result.ctype_is_c = false; + datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collicurules, &isnull); if (!isnull) icurules = TextDatumGetCString(datum); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 3e14a261b16..f41d33975be 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -69,11 +69,25 @@ extern void cache_locale_time(void); /* * We use a discriminated union to hold either a locale_t or an ICU collator. * pg_locale_t is occasionally checked for truth, so make it a pointer. + * + * Also, hold two flags: whether the collation's LC_COLLATE or LC_CTYPE is C + * (or POSIX), so we can optimize a few code paths in various places. For the + * built-in C and POSIX collations, we can know that without even doing a + * cache lookup, but we want to support aliases for C/POSIX too. For the + * "default" collation, there are separate static cache variables, since + * consulting the pg_collation catalog doesn't tell us what we need. + * + * Note that some code relies on the flags not reporting false negatives + * (that is, saying it's not C when it is). For example, char2wchar() + * could fail if the locale is C, so str_tolower() shouldn't call it + * in that case. */ struct pg_locale_struct { char provider; bool deterministic; + bool collate_is_c; + bool ctype_is_c; union { struct -- 2.34.1
v4-0004-ts_locale.c-do-not-use-NULL-to-mean-the-database-.patch (text/x-patch, 3.5K) download From 9327537087a1c5327cd524ffa2ad4d934b53f6d2 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 14:48:07 -0700 Subject: [PATCH v4 4/5] ts_locale.c: do not use NULL to mean the database collation. Use pg_newlocale_from_collation(DEFAULT_COLLATION_OID) to explicitly get the database collation. --- src/backend/tsearch/ts_locale.c | 25 +++++++++++++++++++------ src/backend/tsearch/wparser_def.c | 4 +++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/backend/tsearch/ts_locale.c b/src/backend/tsearch/ts_locale.c index bc44599de6a..2d5e3905b41 100644 --- a/src/backend/tsearch/ts_locale.c +++ b/src/backend/tsearch/ts_locale.c @@ -13,6 +13,7 @@ */ #include "postgres.h" +#include "catalog/pg_collation.h" #include "common/string.h" #include "storage/fd.h" #include "tsearch/ts_locale.h" @@ -36,7 +37,9 @@ t_isdigit(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isdigit(TOUCHAR(ptr)); @@ -51,7 +54,9 @@ t_isspace(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isspace(TOUCHAR(ptr)); @@ -66,7 +71,9 @@ t_isalpha(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isalpha(TOUCHAR(ptr)); @@ -81,7 +88,9 @@ t_isalnum(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (clen == 1 || database_ctype_is_c) return isalnum(TOUCHAR(ptr)); @@ -96,7 +105,9 @@ t_isprint(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID);; if (clen == 1 || database_ctype_is_c) return isprint(TOUCHAR(ptr)); @@ -266,7 +277,9 @@ char * lowerstr_with_len(const char *str, int len) { char *out; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); if (len == 0) return pstrdup(""); diff --git a/src/backend/tsearch/wparser_def.c b/src/backend/tsearch/wparser_def.c index 3919ef27b57..03b4a08244f 100644 --- a/src/backend/tsearch/wparser_def.c +++ b/src/backend/tsearch/wparser_def.c @@ -17,6 +17,7 @@ #include <limits.h> #include <wctype.h> +#include "catalog/pg_collation.h" #include "commands/defrem.h" #include "mb/pg_wchar.h" #include "miscadmin.h" @@ -299,7 +300,8 @@ TParserInit(char *str, int len) */ if (prs->charmaxlen > 1) { - pg_locale_t mylocale = 0; /* TODO */ + /* TODO: determine collation properly */ + pg_locale_t mylocale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID); prs->usewide = true; if (database_ctype_is_c) -- 2.34.1
v4-0005-Remove-support-for-null-pg_locale_t.patch (text/x-patch, 29.2K) download From 5cdad53fc73e7de9d5c85eb50cd6ae27e0697698 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:58:59 -0700 Subject: [PATCH v4 5/5] Remove support for null pg_locale_t. Previously, passing NULL for pg_locale_t meant "use the libc provider and the server environment". Now that the database collation is represented as a proper pg_locale_t (not dependent on setlocale()), remove special cases for NULL. --- src/backend/access/hash/hashfunc.c | 10 +-- src/backend/regex/regc_pg_locale.c | 115 ++--------------------------- src/backend/utils/adt/formatting.c | 84 ++++++--------------- src/backend/utils/adt/like.c | 10 +-- src/backend/utils/adt/pg_locale.c | 77 ++++++------------- src/backend/utils/adt/varchar.c | 10 +-- src/backend/utils/adt/varlena.c | 28 +++---- 7 files changed, 69 insertions(+), 265 deletions(-) diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index ce8ee0ea2ef..d151751e185 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -268,7 +268,7 @@ hashtext(PG_FUNCTION_ARGS) { text *key = PG_GETARG_TEXT_PP(0); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -277,8 +277,7 @@ hashtext(PG_FUNCTION_ARGS) errmsg("could not determine which collation to use for string hashing"), errhint("Use the COLLATE clause to set the collation explicitly."))); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (pg_locale_deterministic(mylocale)) { @@ -322,7 +321,7 @@ hashtextextended(PG_FUNCTION_ARGS) { text *key = PG_GETARG_TEXT_PP(0); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -331,8 +330,7 @@ hashtextextended(PG_FUNCTION_ARGS) errmsg("could not determine which collation to use for string hashing"), errhint("Use the COLLATE clause to set the collation explicitly."))); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (pg_locale_deterministic(mylocale)) { diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 85f3238eb07..dfea0e75ad7 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -67,8 +67,6 @@ typedef enum { PG_REGEX_LOCALE_C, /* C locale (encoding independent) */ PG_REGEX_BUILTIN, /* built-in Unicode semantics */ - PG_REGEX_LOCALE_WIDE, /* Use <wctype.h> functions */ - PG_REGEX_LOCALE_1BYTE, /* Use <ctype.h> functions */ PG_REGEX_LOCALE_WIDE_L, /* Use locale_t <wctype.h> functions */ PG_REGEX_LOCALE_1BYTE_L, /* Use locale_t <ctype.h> functions */ PG_REGEX_LOCALE_ICU, /* Use ICU uchar.h functions */ @@ -262,29 +260,19 @@ pg_set_regex_collation(Oid collation) errmsg("nondeterministic collations are not supported for regular expressions"))); #ifdef USE_ICU - if (pg_regex_locale && pg_regex_locale->provider == COLLPROVIDER_ICU) + if (pg_regex_locale->provider == COLLPROVIDER_ICU) pg_regex_strategy = PG_REGEX_LOCALE_ICU; else #endif if (GetDatabaseEncoding() == PG_UTF8) { - if (pg_regex_locale) - { - if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN) - pg_regex_strategy = PG_REGEX_BUILTIN; - else - pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L; - } + if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN) + pg_regex_strategy = PG_REGEX_BUILTIN; else - pg_regex_strategy = PG_REGEX_LOCALE_WIDE; + pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L; } else - { - if (pg_regex_locale) - pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L; - else - pg_regex_strategy = PG_REGEX_LOCALE_1BYTE; - } + pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L; pg_regex_collation = collation; } @@ -300,13 +288,6 @@ pg_wc_isdigit(pg_wchar c) (pg_char_properties[c] & PG_ISDIGIT)); case PG_REGEX_BUILTIN: return pg_u_isdigit(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswdigit((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isdigit((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswdigit_l((wint_t) c, pg_regex_locale->info.lt); @@ -334,13 +315,6 @@ pg_wc_isalpha(pg_wchar c) (pg_char_properties[c] & PG_ISALPHA)); case PG_REGEX_BUILTIN: return pg_u_isalpha(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalpha((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isalpha((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswalpha_l((wint_t) c, pg_regex_locale->info.lt); @@ -368,13 +342,6 @@ pg_wc_isalnum(pg_wchar c) (pg_char_properties[c] & PG_ISALNUM)); case PG_REGEX_BUILTIN: return pg_u_isalnum(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalnum((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isalnum((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswalnum_l((wint_t) c, pg_regex_locale->info.lt); @@ -411,13 +378,6 @@ pg_wc_isupper(pg_wchar c) (pg_char_properties[c] & PG_ISUPPER)); case PG_REGEX_BUILTIN: return pg_u_isupper(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswupper((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isupper((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswupper_l((wint_t) c, pg_regex_locale->info.lt); @@ -445,13 +405,6 @@ pg_wc_islower(pg_wchar c) (pg_char_properties[c] & PG_ISLOWER)); case PG_REGEX_BUILTIN: return pg_u_islower(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswlower((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - islower((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswlower_l((wint_t) c, pg_regex_locale->info.lt); @@ -479,13 +432,6 @@ pg_wc_isgraph(pg_wchar c) (pg_char_properties[c] & PG_ISGRAPH)); case PG_REGEX_BUILTIN: return pg_u_isgraph(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswgraph((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isgraph((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswgraph_l((wint_t) c, pg_regex_locale->info.lt); @@ -513,13 +459,6 @@ pg_wc_isprint(pg_wchar c) (pg_char_properties[c] & PG_ISPRINT)); case PG_REGEX_BUILTIN: return pg_u_isprint(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswprint((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isprint((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswprint_l((wint_t) c, pg_regex_locale->info.lt); @@ -547,13 +486,6 @@ pg_wc_ispunct(pg_wchar c) (pg_char_properties[c] & PG_ISPUNCT)); case PG_REGEX_BUILTIN: return pg_u_ispunct(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswpunct((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - ispunct((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswpunct_l((wint_t) c, pg_regex_locale->info.lt); @@ -581,13 +513,6 @@ pg_wc_isspace(pg_wchar c) (pg_char_properties[c] & PG_ISSPACE)); case PG_REGEX_BUILTIN: return pg_u_isspace(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswspace((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isspace((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswspace_l((wint_t) c, pg_regex_locale->info.lt); @@ -616,20 +541,6 @@ pg_wc_toupper(pg_wchar c) return c; case PG_REGEX_BUILTIN: return unicode_uppercase_simple(c); - case PG_REGEX_LOCALE_WIDE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_toupper((unsigned char) c); - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towupper((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_toupper((unsigned char) c); - if (c <= (pg_wchar) UCHAR_MAX) - return toupper((unsigned char) c); - return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return towupper_l((wint_t) c, pg_regex_locale->info.lt); @@ -658,20 +569,6 @@ pg_wc_tolower(pg_wchar c) return c; case PG_REGEX_BUILTIN: return unicode_lowercase_simple(c); - case PG_REGEX_LOCALE_WIDE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_tolower((unsigned char) c); - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towlower((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_tolower((unsigned char) c); - if (c <= (pg_wchar) UCHAR_MAX) - return tolower((unsigned char) c); - return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return towlower_l((wint_t) c, pg_regex_locale->info.lt); @@ -825,11 +722,9 @@ pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode) case PG_REGEX_BUILTIN: max_chr = (pg_wchar) MAX_SIMPLE_CHR; break; - case PG_REGEX_LOCALE_WIDE: case PG_REGEX_LOCALE_WIDE_L: max_chr = (pg_wchar) MAX_SIMPLE_CHR; break; - case PG_REGEX_LOCALE_1BYTE: case PG_REGEX_LOCALE_1BYTE_L: #if MAX_SIMPLE_CHR >= UCHAR_MAX max_chr = (pg_wchar) UCHAR_MAX; diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 8736ada4be2..68069fcfd3b 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1665,7 +1665,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar; int32_t len_conv; @@ -1681,7 +1681,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -1710,7 +1710,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -1730,12 +1730,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale); for (curr_char = 0; workspace[curr_char] != 0; curr_char++) - { - if (mylocale) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towlower(workspace[curr_char]); - } + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); /* * Make result large enough; case change might change number @@ -1761,12 +1756,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) * collations you get exactly what the collation says. */ for (p = result; *p; p++) - { - if (mylocale) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); - else - *p = pg_tolower((unsigned char) *p); - } + *p = tolower_l((unsigned char) *p, mylocale->info.lt); } } } @@ -1813,7 +1803,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar, len_conv; @@ -1829,7 +1819,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -1858,7 +1848,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -1878,12 +1868,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale); for (curr_char = 0; workspace[curr_char] != 0; curr_char++) - { - if (mylocale) - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towupper(workspace[curr_char]); - } + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); /* * Make result large enough; case change might change number @@ -1909,12 +1894,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) * collations you get exactly what the collation says. */ for (p = result; *p; p++) - { - if (mylocale) - *p = toupper_l((unsigned char) *p, mylocale->info.lt); - else - *p = pg_toupper((unsigned char) *p); - } + *p = toupper_l((unsigned char) *p, mylocale->info.lt); } } } @@ -2003,7 +1983,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar, len_conv; @@ -2019,7 +1999,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -2060,7 +2040,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -2081,22 +2061,11 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) for (curr_char = 0; workspace[curr_char] != 0; curr_char++) { - if (mylocale) - { - if (wasalnum) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); - wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt); - } + if (wasalnum) + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); else - { - if (wasalnum) - workspace[curr_char] = towlower(workspace[curr_char]); - else - workspace[curr_char] = towupper(workspace[curr_char]); - wasalnum = iswalnum(workspace[curr_char]); - } + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); + wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt); } /* @@ -2124,22 +2093,11 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) */ for (p = result; *p; p++) { - if (mylocale) - { - if (wasalnum) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); - else - *p = toupper_l((unsigned char) *p, mylocale->info.lt); - wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt); - } + if (wasalnum) + *p = tolower_l((unsigned char) *p, mylocale->info.lt); else - { - if (wasalnum) - *p = pg_tolower((unsigned char) *p); - else - *p = pg_toupper((unsigned char) *p); - wasalnum = isalnum((unsigned char) *p); - } + *p = toupper_l((unsigned char) *p, mylocale->info.lt); + wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt); } } } diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 57ead66b5aa..0ecc96d48e5 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -174,8 +174,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) *p; int slen, plen; - pg_locale_t locale = 0; - bool locale_is_c = false; + pg_locale_t locale; if (!OidIsValid(collation)) { @@ -189,10 +188,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) errhint("Use the COLLATE clause to set the collation explicitly."))); } - if (lc_ctype_is_c(collation)) - locale_is_c = true; - else - locale = pg_newlocale_from_collation(collation); + locale = pg_newlocale_from_collation(collation); if (!pg_locale_deterministic(locale)) ereport(ERROR, @@ -228,7 +224,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) plen = VARSIZE_ANY_EXHDR(pat); s = VARDATA_ANY(str); slen = VARSIZE_ANY_EXHDR(str); - return SB_IMatchText(s, slen, p, plen, locale, locale_is_c); + return SB_IMatchText(s, slen, p, plen, locale, locale->ctype_is_c); } } diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 42d8bc5deda..2e6f624798f 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1154,7 +1154,7 @@ get_iso_localename(const char *winlocname) char *hyphen; /* Locale names use only ASCII, any conversion locale suffices. */ - rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), NULL); + rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), default_locale); if (rc == -1 || rc == sizeof(iso_lc_messages)) return NULL; @@ -1449,11 +1449,7 @@ make_icu_collator(const char *iculocstr, bool pg_locale_deterministic(pg_locale_t locale) { - /* default locale must always be deterministic */ - if (locale == NULL) - return true; - else - return locale->deterministic; + return locale->deterministic; } /* @@ -1813,7 +1809,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, int r; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); Assert(GetDatabaseEncoding() == PG_UTF8); #ifndef WIN32 Assert(false); @@ -1853,10 +1849,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, ((LPWSTR) a2p)[r] = 0; errno = 0; - if (locale) - result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); - else - result = wcscoll((LPWSTR) a1p, (LPWSTR) a2p); + result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); if (result == 2147483647) /* _NLSCMPERROR; missing from mingw headers */ ereport(ERROR, (errmsg("could not compare Unicode strings: %m"))); @@ -1882,7 +1875,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 if (GetDatabaseEncoding() == PG_UTF8) { @@ -1893,10 +1886,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) } else #endif /* WIN32 */ - if (locale) result = strcoll_l(arg1, arg2, locale->info.lt); - else - result = strcoll(arg1, arg2); return result; } @@ -1918,7 +1908,7 @@ pg_strncoll_libc(const char *arg1, size_t len1, const char *arg2, size_t len2, char *arg2n; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 /* check for this case before doing the work for nul-termination */ @@ -2064,7 +2054,7 @@ pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strcoll_libc(arg1, arg2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2100,7 +2090,7 @@ pg_strncoll(const char *arg1, size_t len1, const char *arg2, size_t len2, { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strncoll_libc(arg1, len1, arg2, len2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2118,13 +2108,10 @@ static size_t pg_strxfrm_libc(char *dest, const char *src, size_t destsize, pg_locale_t locale) { - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef TRUST_STRXFRM - if (locale) - return strxfrm_l(dest, src, destsize, locale->info.lt); - else - return strxfrm(dest, src, destsize); + return strxfrm_l(dest, src, destsize, locale->info.lt); #else /* shouldn't happen */ PGLOCALE_SUPPORT_ERROR(locale->provider); @@ -2141,7 +2128,7 @@ pg_strnxfrm_libc(char *dest, const char *src, size_t srclen, size_t destsize, size_t bufsize = srclen + 1; size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (bufsize > TEXTBUFLEN) buf = palloc(bufsize); @@ -2313,7 +2300,7 @@ pg_strnxfrm_prefix_icu(char *dest, const char *src, int32_t srclen, bool pg_strxfrm_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) #ifdef TRUST_STRXFRM return true; #else @@ -2347,7 +2334,7 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale) { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strxfrm_libc(dest, src, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2384,7 +2371,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strnxfrm_libc(dest, src, srclen, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2404,7 +2391,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, bool pg_strxfrm_prefix_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) return false; else if (locale->provider == COLLPROVIDER_ICU) return true; @@ -2434,13 +2421,11 @@ pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -2469,13 +2454,11 @@ pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -3032,7 +3015,7 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; @@ -3060,12 +3043,6 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) } else #endif /* WIN32 */ - if (locale == (pg_locale_t) 0) - { - /* Use wcstombs directly for the default locale */ - result = wcstombs(to, from, tolen); - } - else { /* Use wcstombs_l for nondefault locales */ result = wcstombs_l(to, from, tolen, locale->info.lt); @@ -3089,7 +3066,7 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; @@ -3122,16 +3099,8 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, /* mbstowcs requires ending '\0' */ char *str = pnstrdup(from, fromlen); - if (locale == (pg_locale_t) 0) - { - /* Use mbstowcs directly for the default locale */ - result = mbstowcs(to, str, tolen); - } - else - { - /* Use mbstowcs_l for nondefault locales */ - result = mbstowcs_l(to, str, tolen, locale->info.lt); - } + /* Use mbstowcs_l for nondefault locales */ + result = mbstowcs_l(to, str, tolen, locale->info.lt); pfree(str); } diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 02dfe219f54..829375cd1a3 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -999,7 +999,7 @@ hashbpchar(PG_FUNCTION_ARGS) Oid collid = PG_GET_COLLATION(); char *keydata; int keylen; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -1011,8 +1011,7 @@ hashbpchar(PG_FUNCTION_ARGS) keydata = VARDATA_ANY(key); keylen = bcTruelen(key); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (pg_locale_deterministic(mylocale)) { @@ -1054,7 +1053,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) Oid collid = PG_GET_COLLATION(); char *keydata; int keylen; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -1066,8 +1065,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) keydata = VARDATA_ANY(key); keylen = bcTruelen(key); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (pg_locale_deterministic(mylocale)) { diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index d2e2e9bbba0..52ab8c43c66 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1217,12 +1217,11 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) { int len1 = VARSIZE_ANY_EXHDR(t1); int len2 = VARSIZE_ANY_EXHDR(t2); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; check_collation_set(collid); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (!pg_locale_deterministic(mylocale)) ereport(ERROR, @@ -1619,18 +1618,14 @@ Datum texteq(PG_FUNCTION_ARGS) { Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; pg_locale_t mylocale = 0; bool result; check_collation_set(collid); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1678,18 +1673,14 @@ Datum textne(PG_FUNCTION_ARGS) { Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; bool result; check_collation_set(collid); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1793,15 +1784,14 @@ text_starts_with(PG_FUNCTION_ARGS) Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; bool result; Size len1, len2; check_collation_set(collid); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (!pg_locale_deterministic(mylocale)) ereport(ERROR, -- 2.34.1
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-07-27 19:03  Andreas Karlsson <andreas@proxel.se>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Andreas Karlsson @ 2024-07-27 19:03 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On 7/26/24 10:35 PM, Jeff Davis wrote:
> database_ctype_is_c refers to the LC_CTYPE environment of the database
> -- pg_database.datctype. default_locale.ctype_is_c is the ctype of the
> database's default collation.
> 
> Confusing, I know, but it matters for a few things that still depend on
> the LC_CTYPE, such as tsearch and maybe a few extensions. See
> f413941f41.

Ah, right! That was thinko on my behalf.

The set of patches looks good to me now. There is further refactoring 
that can be done in this area (and should be done given all calls e.g to 
isalpha()) but I think this set of patches improves code readability 
while moving us away from setlocale().

And even if we take a tiny performance hit here, which your tests did 
not measure, I would say it is worth it both due to code clarity and due 
to not relying on thread unsafe state.

I do not see these patches in the commitfest app but if they were I 
would have marked them as ready for committer.

Andreas






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-07-29 19:45  Peter Eisentraut <peter@eisentraut.org>
  parent: Andreas Karlsson <andreas@proxel.se>
  0 siblings, 1 reply; 33+ messages in thread

From: Peter Eisentraut @ 2024-07-29 19:45 UTC (permalink / raw)
  To: Andreas Karlsson <andreas@proxel.se>; Jeff Davis <pgsql@j-davis.com>; pgsql-hackers

On 27.07.24 21:03, Andreas Karlsson wrote:
> On 7/26/24 10:35 PM, Jeff Davis wrote:
>> database_ctype_is_c refers to the LC_CTYPE environment of the database
>> -- pg_database.datctype. default_locale.ctype_is_c is the ctype of the
>> database's default collation.
>>
>> Confusing, I know, but it matters for a few things that still depend on
>> the LC_CTYPE, such as tsearch and maybe a few extensions. See
>> f413941f41.
> 
> Ah, right! That was thinko on my behalf.
> 
> The set of patches looks good to me now. There is further refactoring 
> that can be done in this area (and should be done given all calls e.g to 
> isalpha()) but I think this set of patches improves code readability 
> while moving us away from setlocale().
> 
> And even if we take a tiny performance hit here, which your tests did 
> not measure, I would say it is worth it both due to code clarity and due 
> to not relying on thread unsafe state.
> 
> I do not see these patches in the commitfest app but if they were I 
> would have marked them as ready for committer.

Here: https://commitfest.postgresql.org/48/5023/

I have also re-reviewed the patches and I agree they are good to go.






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-07-30 19:13  Jeff Davis <pgsql@j-davis.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-07-30 19:13 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On Mon, 2024-07-29 at 21:45 +0200, Peter Eisentraut wrote:
> I have also re-reviewed the patches and I agree they are good to go.

I found a couple issues with the later patches:

* There was still some confusion about the default collation vs.
datcollate/datctype for callers of wchar2char() and char2wchar() (those
functions only work for libc). I introduced a new pg_locale_t structure
to represent datcollate/datctype regardless of datlocprovider to solve
this.

* Another loose end relying on setlocale(): in selfuncs.c, there's
still a call directly to strxfrm(), which depends on setlocale(). I
changed this to lookup the collation and then use pg_strxfrm(). That
should improve histogram selectivity estimates because it uses the
correct provider, rather than relying on setlocale(), right?

New series attached.

Regards,
	Jeff Davis

Attachments: download all (3 files, .tar.gz)

v6-0001-Make-datcollate-datctype-accessible-as-a-pg_local.patch (text/x-patch, 8.8K) download From 5b903c82f34f5da9cab58ecd0a2683454d6ac9ed Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 14:48:07 -0700 Subject: [PATCH v6 1/3] Make datcollate/datctype accessible as a pg_locale_t. get_db_env_locale() returns a libc locale representing the LC_COLLATE / LC_CTYPE environment, which is the same as the database default collation if and only if the datlocprovider is libc. Update callers in ts_locale.c to use get_db_env_locale() instead of NULL. Discussion: https://postgr.es/m/cfd9eb85-c52a-4ec9-a90e-a5e4de56e57d@eisentraut.org Reviewed-by: Peter Eisentraut, Andreas Karlsson --- src/backend/tsearch/ts_locale.c | 37 ++++++++++++++++--------- src/backend/tsearch/wparser_def.c | 6 +++-- src/backend/utils/adt/pg_locale.c | 45 ++++++++++++++++++++++++++++--- src/backend/utils/init/postinit.c | 5 +--- src/include/utils/pg_locale.h | 5 ++-- 5 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/backend/tsearch/ts_locale.c b/src/backend/tsearch/ts_locale.c index bc44599de6a..6befd8e82d6 100644 --- a/src/backend/tsearch/ts_locale.c +++ b/src/backend/tsearch/ts_locale.c @@ -13,6 +13,7 @@ */ #include "postgres.h" +#include "catalog/pg_collation.h" #include "common/string.h" #include "storage/fd.h" #include "tsearch/ts_locale.h" @@ -36,9 +37,11 @@ t_isdigit(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ - if (clen == 1 || database_ctype_is_c) + /* TODO: determine collation properly */ + pg_locale_t mylocale = get_db_env_locale(); + + if (clen == 1 || mylocale->ctype_is_c) return isdigit(TOUCHAR(ptr)); char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale); @@ -51,9 +54,11 @@ t_isspace(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ - if (clen == 1 || database_ctype_is_c) + /* TODO: determine collation properly */ + pg_locale_t mylocale = get_db_env_locale(); + + if (clen == 1 || mylocale->ctype_is_c) return isspace(TOUCHAR(ptr)); char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale); @@ -66,9 +71,11 @@ t_isalpha(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ - if (clen == 1 || database_ctype_is_c) + /* TODO: determine collation properly */ + pg_locale_t mylocale = get_db_env_locale(); + + if (clen == 1 || mylocale->ctype_is_c) return isalpha(TOUCHAR(ptr)); char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale); @@ -81,9 +88,11 @@ t_isalnum(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ - if (clen == 1 || database_ctype_is_c) + /* TODO: determine collation properly */ + pg_locale_t mylocale = get_db_env_locale(); + + if (clen == 1 || mylocale->ctype_is_c) return isalnum(TOUCHAR(ptr)); char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale); @@ -96,9 +105,11 @@ t_isprint(const char *ptr) { int clen = pg_mblen(ptr); wchar_t character[WC_BUF_LEN]; - pg_locale_t mylocale = 0; /* TODO */ - if (clen == 1 || database_ctype_is_c) + /* TODO: determine collation properly */ + pg_locale_t mylocale = get_db_env_locale(); + + if (clen == 1 || mylocale->ctype_is_c) return isprint(TOUCHAR(ptr)); char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale); @@ -266,7 +277,9 @@ char * lowerstr_with_len(const char *str, int len) { char *out; - pg_locale_t mylocale = 0; /* TODO */ + + /* TODO: determine collation properly */ + pg_locale_t mylocale = get_db_env_locale(); if (len == 0) return pstrdup(""); @@ -277,7 +290,7 @@ lowerstr_with_len(const char *str, int len) * Also, for a C locale there is no need to process as multibyte. From * backend/utils/adt/oracle_compat.c Teodor */ - if (pg_database_encoding_max_length() > 1 && !database_ctype_is_c) + if (pg_database_encoding_max_length() > 1 && !mylocale->ctype_is_c) { wchar_t *wstr, *wptr; diff --git a/src/backend/tsearch/wparser_def.c b/src/backend/tsearch/wparser_def.c index 3919ef27b57..45caec0c4f0 100644 --- a/src/backend/tsearch/wparser_def.c +++ b/src/backend/tsearch/wparser_def.c @@ -17,6 +17,7 @@ #include <limits.h> #include <wctype.h> +#include "catalog/pg_collation.h" #include "commands/defrem.h" #include "mb/pg_wchar.h" #include "miscadmin.h" @@ -299,10 +300,11 @@ TParserInit(char *str, int len) */ if (prs->charmaxlen > 1) { - pg_locale_t mylocale = 0; /* TODO */ + /* TODO: determine collation properly */ + pg_locale_t mylocale = get_db_env_locale(); prs->usewide = true; - if (database_ctype_is_c) + if (mylocale->ctype_is_c) { /* * char2wchar doesn't work for C-locale and sizeof(pg_wchar) could diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 627ab89d7cc..0295d834cc5 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -114,10 +114,8 @@ char *localized_full_days[7 + 1]; char *localized_abbrev_months[12 + 1]; char *localized_full_months[12 + 1]; -/* is the databases's LC_CTYPE the C locale? */ -bool database_ctype_is_c = false; - static struct pg_locale_struct default_locale; +static struct pg_locale_struct database_env_locale; /* indicates whether locale information cache is valid */ static bool CurrentLocaleConvValid = false; @@ -1471,6 +1469,42 @@ pg_locale_deterministic(pg_locale_t locale) return locale->deterministic; } +/* + * Initialize the database environment locale and store in a pg_locale_t. + */ +void +init_db_env_locale(const char *datcollate, const char *datctype) +{ + Assert(database_env_locale.provider == (char) 0); + + database_env_locale.provider = COLLPROVIDER_LIBC; + database_env_locale.deterministic = true; + database_env_locale.collate_is_c = (strcmp(datcollate, "C") == 0) || + (strcmp(datcollate, "POSIX") == 0); + database_env_locale.ctype_is_c = (strcmp(datctype, "C") == 0) || + (strcmp(datctype, "POSIX") == 0); + + make_libc_collator(datcollate, datctype, &database_env_locale); +} + +/* + * Return pg_locale_t representing the database environment locale. + * + * The provider is always libc, and it represents the server environment + * LC_COLLATE and LC_CTYPE. + * + * Most callers should use pg_newlocale_from_collation(DEFAULT_COLLATION_OID) + * instead to get a pg_locale_t representing the database default collation + * (which might be any provider). Use get_db_env_locale() only if the libc + * provider is needed, such as with wchar2char()/char2wchar(). + */ +pg_locale_t +get_db_env_locale(void) +{ + Assert(database_env_locale.provider != (char) 0); + return &database_env_locale; +} + /* * Initialize default_locale with database locale settings. */ @@ -1482,6 +1516,8 @@ init_database_collation(void) Datum datum; bool isnull; + Assert(default_locale.provider == (char) 0); + /* Fetch our pg_database row normally, via syscache */ tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); if (!HeapTupleIsValid(tup)) @@ -1571,7 +1607,10 @@ pg_newlocale_from_collation(Oid collid) Assert(OidIsValid(collid)); if (collid == DEFAULT_COLLATION_OID) + { + Assert(default_locale.provider != (char) 0); return &default_locale; + } cache_entry = lookup_collation_cache(collid); diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 13524ea488a..23ac403e390 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -418,10 +418,7 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect " which is not recognized by setlocale().", ctype), errhint("Recreate the database with another locale or install the missing locale."))); - if (strcmp(ctype, "C") == 0 || - strcmp(ctype, "POSIX") == 0) - database_ctype_is_c = true; - + init_db_env_locale(collate, ctype); init_database_collation(); /* diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index f41d33975be..47b2942c9d8 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -48,9 +48,6 @@ extern PGDLLIMPORT char *localized_full_days[]; extern PGDLLIMPORT char *localized_abbrev_months[]; extern PGDLLIMPORT char *localized_full_months[]; -/* is the databases's LC_CTYPE the C locale? */ -extern PGDLLIMPORT bool database_ctype_is_c; - extern bool check_locale(int category, const char *locale, char **canonname); extern char *pg_perm_setlocale(int category, const char *locale); @@ -112,6 +109,8 @@ extern void make_icu_collator(const char *iculocstr, struct pg_locale_struct *resultp); extern bool pg_locale_deterministic(pg_locale_t locale); +extern void init_db_env_locale(const char *datcollate, const char *datctype); +extern pg_locale_t get_db_env_locale(void); extern void init_database_collation(void); extern pg_locale_t pg_newlocale_from_collation(Oid collid); -- 2.34.1
v6-0002-Remove-support-for-null-pg_locale_t.patch (text/x-patch, 29.5K) download From 1eea055318b07155fe025d9f6cf56dadcea040a0 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Wed, 5 Jun 2024 11:58:59 -0700 Subject: [PATCH v6 2/3] Remove support for null pg_locale_t. Previously, passing NULL for pg_locale_t meant "use the libc provider and the server environment". Now that the database collation is represented as a proper pg_locale_t (not dependent on setlocale()), remove special cases for NULL. Discussion: https://postgr.es/m/cfd9eb85-c52a-4ec9-a90e-a5e4de56e57d@eisentraut.org Reviewed-by: Peter Eisentraut, Andreas Karlsson --- src/backend/access/hash/hashfunc.c | 10 +-- src/backend/regex/regc_pg_locale.c | 113 +---------------------------- src/backend/utils/adt/formatting.c | 84 ++++++--------------- src/backend/utils/adt/like.c | 10 +-- src/backend/utils/adt/pg_locale.c | 78 ++++++-------------- src/backend/utils/adt/varchar.c | 10 +-- src/backend/utils/adt/varlena.c | 28 +++---- 7 files changed, 69 insertions(+), 264 deletions(-) diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index ce8ee0ea2ef..d151751e185 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -268,7 +268,7 @@ hashtext(PG_FUNCTION_ARGS) { text *key = PG_GETARG_TEXT_PP(0); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -277,8 +277,7 @@ hashtext(PG_FUNCTION_ARGS) errmsg("could not determine which collation to use for string hashing"), errhint("Use the COLLATE clause to set the collation explicitly."))); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (pg_locale_deterministic(mylocale)) { @@ -322,7 +321,7 @@ hashtextextended(PG_FUNCTION_ARGS) { text *key = PG_GETARG_TEXT_PP(0); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -331,8 +330,7 @@ hashtextextended(PG_FUNCTION_ARGS) errmsg("could not determine which collation to use for string hashing"), errhint("Use the COLLATE clause to set the collation explicitly."))); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (pg_locale_deterministic(mylocale)) { diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 9d98d10a285..947d73f3e0f 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -67,8 +67,6 @@ typedef enum { PG_REGEX_LOCALE_C, /* C locale (encoding independent) */ PG_REGEX_BUILTIN, /* built-in Unicode semantics */ - PG_REGEX_LOCALE_WIDE, /* Use <wctype.h> functions */ - PG_REGEX_LOCALE_1BYTE, /* Use <ctype.h> functions */ PG_REGEX_LOCALE_WIDE_L, /* Use locale_t <wctype.h> functions */ PG_REGEX_LOCALE_1BYTE_L, /* Use locale_t <ctype.h> functions */ PG_REGEX_LOCALE_ICU, /* Use ICU uchar.h functions */ @@ -261,13 +259,13 @@ pg_set_regex_collation(Oid collation) (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for regular expressions"))); - if (pg_regex_locale && pg_regex_locale->provider == COLLPROVIDER_BUILTIN) + if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN) { Assert(GetDatabaseEncoding() == PG_UTF8); pg_regex_strategy = PG_REGEX_BUILTIN; } #ifdef USE_ICU - else if (pg_regex_locale && pg_regex_locale->provider == COLLPROVIDER_ICU) + else if (pg_regex_locale->provider == COLLPROVIDER_ICU) { pg_regex_strategy = PG_REGEX_LOCALE_ICU; } @@ -275,19 +273,9 @@ pg_set_regex_collation(Oid collation) else { if (GetDatabaseEncoding() == PG_UTF8) - { - if (pg_regex_locale) - pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L; - else - pg_regex_strategy = PG_REGEX_LOCALE_WIDE; - } + pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L; else - { - if (pg_regex_locale) - pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L; - else - pg_regex_strategy = PG_REGEX_LOCALE_1BYTE; - } + pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L; } pg_regex_collation = collation; @@ -304,13 +292,6 @@ pg_wc_isdigit(pg_wchar c) (pg_char_properties[c] & PG_ISDIGIT)); case PG_REGEX_BUILTIN: return pg_u_isdigit(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswdigit((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isdigit((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswdigit_l((wint_t) c, pg_regex_locale->info.lt); @@ -338,13 +319,6 @@ pg_wc_isalpha(pg_wchar c) (pg_char_properties[c] & PG_ISALPHA)); case PG_REGEX_BUILTIN: return pg_u_isalpha(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalpha((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isalpha((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswalpha_l((wint_t) c, pg_regex_locale->info.lt); @@ -372,13 +346,6 @@ pg_wc_isalnum(pg_wchar c) (pg_char_properties[c] & PG_ISALNUM)); case PG_REGEX_BUILTIN: return pg_u_isalnum(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswalnum((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isalnum((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswalnum_l((wint_t) c, pg_regex_locale->info.lt); @@ -415,13 +382,6 @@ pg_wc_isupper(pg_wchar c) (pg_char_properties[c] & PG_ISUPPER)); case PG_REGEX_BUILTIN: return pg_u_isupper(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswupper((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isupper((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswupper_l((wint_t) c, pg_regex_locale->info.lt); @@ -449,13 +409,6 @@ pg_wc_islower(pg_wchar c) (pg_char_properties[c] & PG_ISLOWER)); case PG_REGEX_BUILTIN: return pg_u_islower(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswlower((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - islower((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswlower_l((wint_t) c, pg_regex_locale->info.lt); @@ -483,13 +436,6 @@ pg_wc_isgraph(pg_wchar c) (pg_char_properties[c] & PG_ISGRAPH)); case PG_REGEX_BUILTIN: return pg_u_isgraph(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswgraph((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isgraph((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswgraph_l((wint_t) c, pg_regex_locale->info.lt); @@ -517,13 +463,6 @@ pg_wc_isprint(pg_wchar c) (pg_char_properties[c] & PG_ISPRINT)); case PG_REGEX_BUILTIN: return pg_u_isprint(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswprint((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isprint((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswprint_l((wint_t) c, pg_regex_locale->info.lt); @@ -551,13 +490,6 @@ pg_wc_ispunct(pg_wchar c) (pg_char_properties[c] & PG_ISPUNCT)); case PG_REGEX_BUILTIN: return pg_u_ispunct(c, true); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswpunct((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - ispunct((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswpunct_l((wint_t) c, pg_regex_locale->info.lt); @@ -585,13 +517,6 @@ pg_wc_isspace(pg_wchar c) (pg_char_properties[c] & PG_ISSPACE)); case PG_REGEX_BUILTIN: return pg_u_isspace(c); - case PG_REGEX_LOCALE_WIDE: - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return iswspace((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - return (c <= (pg_wchar) UCHAR_MAX && - isspace((unsigned char) c)); case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return iswspace_l((wint_t) c, pg_regex_locale->info.lt); @@ -620,20 +545,6 @@ pg_wc_toupper(pg_wchar c) return c; case PG_REGEX_BUILTIN: return unicode_uppercase_simple(c); - case PG_REGEX_LOCALE_WIDE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_toupper((unsigned char) c); - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towupper((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_toupper((unsigned char) c); - if (c <= (pg_wchar) UCHAR_MAX) - return toupper((unsigned char) c); - return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return towupper_l((wint_t) c, pg_regex_locale->info.lt); @@ -662,20 +573,6 @@ pg_wc_tolower(pg_wchar c) return c; case PG_REGEX_BUILTIN: return unicode_lowercase_simple(c); - case PG_REGEX_LOCALE_WIDE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_tolower((unsigned char) c); - if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) - return towlower((wint_t) c); - /* FALL THRU */ - case PG_REGEX_LOCALE_1BYTE: - /* force C behavior for ASCII characters, per comments above */ - if (c <= (pg_wchar) 127) - return pg_ascii_tolower((unsigned char) c); - if (c <= (pg_wchar) UCHAR_MAX) - return tolower((unsigned char) c); - return c; case PG_REGEX_LOCALE_WIDE_L: if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF) return towlower_l((wint_t) c, pg_regex_locale->info.lt); @@ -829,11 +726,9 @@ pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode) case PG_REGEX_BUILTIN: max_chr = (pg_wchar) MAX_SIMPLE_CHR; break; - case PG_REGEX_LOCALE_WIDE: case PG_REGEX_LOCALE_WIDE_L: max_chr = (pg_wchar) MAX_SIMPLE_CHR; break; - case PG_REGEX_LOCALE_1BYTE: case PG_REGEX_LOCALE_1BYTE_L: #if MAX_SIMPLE_CHR >= UCHAR_MAX max_chr = (pg_wchar) UCHAR_MAX; diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 8736ada4be2..68069fcfd3b 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1665,7 +1665,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar; int32_t len_conv; @@ -1681,7 +1681,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -1710,7 +1710,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -1730,12 +1730,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale); for (curr_char = 0; workspace[curr_char] != 0; curr_char++) - { - if (mylocale) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towlower(workspace[curr_char]); - } + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); /* * Make result large enough; case change might change number @@ -1761,12 +1756,7 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) * collations you get exactly what the collation says. */ for (p = result; *p; p++) - { - if (mylocale) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); - else - *p = pg_tolower((unsigned char) *p); - } + *p = tolower_l((unsigned char) *p, mylocale->info.lt); } } } @@ -1813,7 +1803,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar, len_conv; @@ -1829,7 +1819,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -1858,7 +1848,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -1878,12 +1868,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) char2wchar(workspace, nbytes + 1, buff, nbytes, mylocale); for (curr_char = 0; workspace[curr_char] != 0; curr_char++) - { - if (mylocale) - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towupper(workspace[curr_char]); - } + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); /* * Make result large enough; case change might change number @@ -1909,12 +1894,7 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) * collations you get exactly what the collation says. */ for (p = result; *p; p++) - { - if (mylocale) - *p = toupper_l((unsigned char) *p, mylocale->info.lt); - else - *p = pg_toupper((unsigned char) *p); - } + *p = toupper_l((unsigned char) *p, mylocale->info.lt); } } } @@ -2003,7 +1983,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) mylocale = pg_newlocale_from_collation(collid); #ifdef USE_ICU - if (mylocale && mylocale->provider == COLLPROVIDER_ICU) + if (mylocale->provider == COLLPROVIDER_ICU) { int32_t len_uchar, len_conv; @@ -2019,7 +1999,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) } else #endif - if (mylocale && mylocale->provider == COLLPROVIDER_BUILTIN) + if (mylocale->provider == COLLPROVIDER_BUILTIN) { const char *src = buff; size_t srclen = nbytes; @@ -2060,7 +2040,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) } else { - Assert(!mylocale || mylocale->provider == COLLPROVIDER_LIBC); + Assert(mylocale->provider == COLLPROVIDER_LIBC); if (pg_database_encoding_max_length() > 1) { @@ -2081,22 +2061,11 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) for (curr_char = 0; workspace[curr_char] != 0; curr_char++) { - if (mylocale) - { - if (wasalnum) - workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); - else - workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); - wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt); - } + if (wasalnum) + workspace[curr_char] = towlower_l(workspace[curr_char], mylocale->info.lt); else - { - if (wasalnum) - workspace[curr_char] = towlower(workspace[curr_char]); - else - workspace[curr_char] = towupper(workspace[curr_char]); - wasalnum = iswalnum(workspace[curr_char]); - } + workspace[curr_char] = towupper_l(workspace[curr_char], mylocale->info.lt); + wasalnum = iswalnum_l(workspace[curr_char], mylocale->info.lt); } /* @@ -2124,22 +2093,11 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) */ for (p = result; *p; p++) { - if (mylocale) - { - if (wasalnum) - *p = tolower_l((unsigned char) *p, mylocale->info.lt); - else - *p = toupper_l((unsigned char) *p, mylocale->info.lt); - wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt); - } + if (wasalnum) + *p = tolower_l((unsigned char) *p, mylocale->info.lt); else - { - if (wasalnum) - *p = pg_tolower((unsigned char) *p); - else - *p = pg_toupper((unsigned char) *p); - wasalnum = isalnum((unsigned char) *p); - } + *p = toupper_l((unsigned char) *p, mylocale->info.lt); + wasalnum = isalnum_l((unsigned char) *p, mylocale->info.lt); } } } diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 57ead66b5aa..0ecc96d48e5 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -174,8 +174,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) *p; int slen, plen; - pg_locale_t locale = 0; - bool locale_is_c = false; + pg_locale_t locale; if (!OidIsValid(collation)) { @@ -189,10 +188,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) errhint("Use the COLLATE clause to set the collation explicitly."))); } - if (lc_ctype_is_c(collation)) - locale_is_c = true; - else - locale = pg_newlocale_from_collation(collation); + locale = pg_newlocale_from_collation(collation); if (!pg_locale_deterministic(locale)) ereport(ERROR, @@ -228,7 +224,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) plen = VARSIZE_ANY_EXHDR(pat); s = VARDATA_ANY(str); slen = VARSIZE_ANY_EXHDR(str); - return SB_IMatchText(s, slen, p, plen, locale, locale_is_c); + return SB_IMatchText(s, slen, p, plen, locale, locale->ctype_is_c); } } diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 0295d834cc5..ccd6180a743 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1169,7 +1169,8 @@ get_iso_localename(const char *winlocname) char *hyphen; /* Locale names use only ASCII, any conversion locale suffices. */ - rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), NULL); + rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), + get_db_env_locale()); if (rc == -1 || rc == sizeof(iso_lc_messages)) return NULL; @@ -1462,11 +1463,7 @@ make_icu_collator(const char *iculocstr, bool pg_locale_deterministic(pg_locale_t locale) { - /* default locale must always be deterministic */ - if (locale == NULL) - return true; - else - return locale->deterministic; + return locale->deterministic; } /* @@ -1867,7 +1864,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, int r; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); Assert(GetDatabaseEncoding() == PG_UTF8); #ifndef WIN32 Assert(false); @@ -1907,10 +1904,7 @@ pg_strncoll_libc_win32_utf8(const char *arg1, size_t len1, const char *arg2, ((LPWSTR) a2p)[r] = 0; errno = 0; - if (locale) - result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); - else - result = wcscoll((LPWSTR) a1p, (LPWSTR) a2p); + result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, locale->info.lt); if (result == 2147483647) /* _NLSCMPERROR; missing from mingw headers */ ereport(ERROR, (errmsg("could not compare Unicode strings: %m"))); @@ -1936,7 +1930,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 if (GetDatabaseEncoding() == PG_UTF8) { @@ -1947,10 +1941,7 @@ pg_strcoll_libc(const char *arg1, const char *arg2, pg_locale_t locale) } else #endif /* WIN32 */ - if (locale) result = strcoll_l(arg1, arg2, locale->info.lt); - else - result = strcoll(arg1, arg2); return result; } @@ -1972,7 +1963,7 @@ pg_strncoll_libc(const char *arg1, size_t len1, const char *arg2, size_t len2, char *arg2n; int result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef WIN32 /* check for this case before doing the work for nul-termination */ @@ -2118,7 +2109,7 @@ pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale) { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strcoll_libc(arg1, arg2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2154,7 +2145,7 @@ pg_strncoll(const char *arg1, size_t len1, const char *arg2, size_t len2, { int result; - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strncoll_libc(arg1, len1, arg2, len2, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2172,13 +2163,10 @@ static size_t pg_strxfrm_libc(char *dest, const char *src, size_t destsize, pg_locale_t locale) { - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); #ifdef TRUST_STRXFRM - if (locale) - return strxfrm_l(dest, src, destsize, locale->info.lt); - else - return strxfrm(dest, src, destsize); + return strxfrm_l(dest, src, destsize, locale->info.lt); #else /* shouldn't happen */ PGLOCALE_SUPPORT_ERROR(locale->provider); @@ -2195,7 +2183,7 @@ pg_strnxfrm_libc(char *dest, const char *src, size_t srclen, size_t destsize, size_t bufsize = srclen + 1; size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (bufsize > TEXTBUFLEN) buf = palloc(bufsize); @@ -2367,7 +2355,7 @@ pg_strnxfrm_prefix_icu(char *dest, const char *src, int32_t srclen, bool pg_strxfrm_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) #ifdef TRUST_STRXFRM return true; #else @@ -2401,7 +2389,7 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale) { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strxfrm_libc(dest, src, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2438,7 +2426,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, { size_t result = 0; /* keep compiler quiet */ - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) result = pg_strnxfrm_libc(dest, src, srclen, destsize, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -2458,7 +2446,7 @@ pg_strnxfrm(char *dest, size_t destsize, const char *src, size_t srclen, bool pg_strxfrm_prefix_enabled(pg_locale_t locale) { - if (!locale || locale->provider == COLLPROVIDER_LIBC) + if (locale->provider == COLLPROVIDER_LIBC) return false; else if (locale->provider == COLLPROVIDER_ICU) return true; @@ -2488,13 +2476,11 @@ pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -2523,13 +2509,11 @@ pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, { size_t result = 0; /* keep compiler quiet */ - if (!locale) - PGLOCALE_SUPPORT_ERROR(COLLPROVIDER_LIBC); #ifdef USE_ICU - else if (locale->provider == COLLPROVIDER_ICU) + if (locale->provider == COLLPROVIDER_ICU) result = pg_strnxfrm_prefix_icu(dest, src, -1, destsize, locale); -#endif else +#endif PGLOCALE_SUPPORT_ERROR(locale->provider); return result; @@ -3086,7 +3070,7 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; @@ -3114,12 +3098,6 @@ wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale) } else #endif /* WIN32 */ - if (locale == (pg_locale_t) 0) - { - /* Use wcstombs directly for the default locale */ - result = wcstombs(to, from, tolen); - } - else { /* Use wcstombs_l for nondefault locales */ result = wcstombs_l(to, from, tolen, locale->info.lt); @@ -3143,7 +3121,7 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, { size_t result; - Assert(!locale || locale->provider == COLLPROVIDER_LIBC); + Assert(locale->provider == COLLPROVIDER_LIBC); if (tolen == 0) return 0; @@ -3176,16 +3154,8 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, /* mbstowcs requires ending '\0' */ char *str = pnstrdup(from, fromlen); - if (locale == (pg_locale_t) 0) - { - /* Use mbstowcs directly for the default locale */ - result = mbstowcs(to, str, tolen); - } - else - { - /* Use mbstowcs_l for nondefault locales */ - result = mbstowcs_l(to, str, tolen, locale->info.lt); - } + /* Use mbstowcs_l for nondefault locales */ + result = mbstowcs_l(to, str, tolen, locale->info.lt); pfree(str); } diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 02dfe219f54..829375cd1a3 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -999,7 +999,7 @@ hashbpchar(PG_FUNCTION_ARGS) Oid collid = PG_GET_COLLATION(); char *keydata; int keylen; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -1011,8 +1011,7 @@ hashbpchar(PG_FUNCTION_ARGS) keydata = VARDATA_ANY(key); keylen = bcTruelen(key); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (pg_locale_deterministic(mylocale)) { @@ -1054,7 +1053,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) Oid collid = PG_GET_COLLATION(); char *keydata; int keylen; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; Datum result; if (!collid) @@ -1066,8 +1065,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) keydata = VARDATA_ANY(key); keylen = bcTruelen(key); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (pg_locale_deterministic(mylocale)) { diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index d2e2e9bbba0..52ab8c43c66 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1217,12 +1217,11 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) { int len1 = VARSIZE_ANY_EXHDR(t1); int len2 = VARSIZE_ANY_EXHDR(t2); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; check_collation_set(collid); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (!pg_locale_deterministic(mylocale)) ereport(ERROR, @@ -1619,18 +1618,14 @@ Datum texteq(PG_FUNCTION_ARGS) { Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; pg_locale_t mylocale = 0; bool result; check_collation_set(collid); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1678,18 +1673,14 @@ Datum textne(PG_FUNCTION_ARGS) { Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; bool result; check_collation_set(collid); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1793,15 +1784,14 @@ text_starts_with(PG_FUNCTION_ARGS) Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); Oid collid = PG_GET_COLLATION(); - pg_locale_t mylocale = 0; + pg_locale_t mylocale; bool result; Size len1, len2; check_collation_set(collid); - if (!lc_collate_is_c(collid)) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); if (!pg_locale_deterministic(mylocale)) ereport(ERROR, -- 2.34.1
v6-0003-selfuncs.c-use-pg_strxfrm-instead-of-strxfrm.patch (text/x-patch, 4.7K) download From 9bd779fb711c902b33cfd3a5350e0736d7ceb138 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Mon, 29 Jul 2024 23:58:29 -0700 Subject: [PATCH v6 3/3] selfuncs.c: use pg_strxfrm() instead of strxfrm(). pg_strxfrm() takes a pg_locale_t, so it works properly with other providers and does not rely on setlocale(). Discussion: https://postgr.es/m/cfd9eb85-c52a-4ec9-a90e-a5e4de56e57d@eisentraut.org Reviewed-by: Peter Eisentraut, Andreas Karlsson --- src/backend/utils/adt/pg_locale.c | 23 ++++++++++++++++------- src/backend/utils/adt/selfuncs.c | 9 +++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index ccd6180a743..17c55c5ab17 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -2164,14 +2164,7 @@ pg_strxfrm_libc(char *dest, const char *src, size_t destsize, pg_locale_t locale) { Assert(locale->provider == COLLPROVIDER_LIBC); - -#ifdef TRUST_STRXFRM return strxfrm_l(dest, src, destsize, locale->info.lt); -#else - /* shouldn't happen */ - PGLOCALE_SUPPORT_ERROR(locale->provider); - return 0; /* keep compiler quiet */ -#endif } static size_t @@ -2380,6 +2373,10 @@ pg_strxfrm_enabled(pg_locale_t locale) * The provided 'src' must be nul-terminated. If 'destsize' is zero, 'dest' * may be NULL. * + * Not all providers support pg_strxfrm() safely. The caller should check + * pg_strxfrm_enabled() first, otherwise this function may return wrong + * results or an error. + * * Returns the number of bytes needed to store the transformed string, * excluding the terminating nul byte. If the value returned is 'destsize' or * greater, the resulting contents of 'dest' are undefined. @@ -2412,6 +2409,10 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale) * 'src' does not need to be nul-terminated. If 'destsize' is zero, 'dest' may * be NULL. * + * Not all providers support pg_strnxfrm() safely. The caller should check + * pg_strxfrm_enabled() first, otherwise this function may return wrong + * results or an error. + * * Returns the number of bytes needed to store the transformed string, * excluding the terminating nul byte. If the value returned is 'destsize' or * greater, the resulting contents of 'dest' are undefined. @@ -2466,6 +2467,10 @@ pg_strxfrm_prefix_enabled(pg_locale_t locale) * * The provided 'src' must be nul-terminated. * + * Not all providers support pg_strxfrm_prefix() safely. The caller should + * check pg_strxfrm_prefix_enabled() first, otherwise this function may return + * wrong results or an error. + * * If destsize is not large enough to hold the resulting byte sequence, stores * only the first destsize bytes in 'dest'. Returns the number of bytes * actually copied to 'dest'. @@ -2495,6 +2500,10 @@ pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, * * The provided 'src' must be nul-terminated. * + * Not all providers support pg_strnxfrm_prefix() safely. The caller should + * check pg_strxfrm_prefix_enabled() first, otherwise this function may return + * wrong results or an error. + * * If destsize is not large enough to hold the resulting byte sequence, stores * only the first destsize bytes in 'dest'. Returns the number of bytes * actually copied to 'dest'. diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index 877a62a62ec..673cfd9e703 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -4673,6 +4673,7 @@ convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure) if (!lc_collate_is_c(collid)) { + pg_locale_t mylocale = pg_newlocale_from_collation(collid); char *xfrmstr; size_t xfrmlen; size_t xfrmlen2 PG_USED_FOR_ASSERTS_ONLY; @@ -4685,8 +4686,12 @@ convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure) * bogus data or set an error. This is not really a problem unless it * crashes since it will only give an estimation error and nothing * fatal. + * + * XXX: we do not check pg_strxfrm_enabled(). On some platforms and in + * some cases, libc strxfrm() may return the wrong results, but that + * will only lead to an estimation error. */ - xfrmlen = strxfrm(NULL, val, 0); + xfrmlen = pg_strxfrm(NULL, val, 0, mylocale); #ifdef WIN32 /* @@ -4698,7 +4703,7 @@ convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure) return val; #endif xfrmstr = (char *) palloc(xfrmlen + 1); - xfrmlen2 = strxfrm(xfrmstr, val, xfrmlen + 1); + xfrmlen2 = pg_strxfrm(xfrmstr, val, xfrmlen + 1, mylocale); /* * Some systems (e.g., glibc) can return a smaller value from the -- 2.34.1
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-06 21:40  Jeff Davis <pgsql@j-davis.com>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 3 replies; 33+ messages in thread

From: Jeff Davis @ 2024-08-06 21:40 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On Tue, 2024-07-30 at 12:13 -0700, Jeff Davis wrote:
> I found a couple issues with the later patches:
> 
> * There was still some confusion about the default collation vs.
> datcollate/datctype for callers of wchar2char() and char2wchar()
> (those
> functions only work for libc). I introduced a new pg_locale_t
> structure
> to represent datcollate/datctype regardless of datlocprovider to
> solve
> this.

I didn't quite like the API I introduced in 0001, so I skipped 0001.

For 0002 I left char2wchar() and wchar2char() as-is, where they expect
libc and accept a NULL pg_locale_t. I committed the rest of 0002.

> * Another loose end relying on setlocale(): in selfuncs.c, there's
> still a call directly to strxfrm(), which depends on setlocale(). I
> changed this to lookup the collation and then use pg_strxfrm(). That
> should improve histogram selectivity estimates because it uses the
> correct provider, rather than relying on setlocale(), right?

Committed 0003.

With these changes, collations are no longer dependent on the
environment locale (setlocale()) at all for either collation behavior
(ORDER BY) or ctype behavior (LOWER(), etc.).

Additionally, unless I missed something, nothing in the server is
dependent on LC_COLLATE at all.

There are still some things that depend on setlocale() in one way or
another:

  - char2wchar() & wchar2char()
  - ts_locale.c
  - various places that depend on LC_CTYPE unrelated to the collation
infrastructure
  - things that depend on other locale settings, like LC_NUMERIC

We can address those as part of a separate thread. I'll count this as
committed.

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-07 20:44  Peter Eisentraut <peter@eisentraut.org>
  parent: Jeff Davis <pgsql@j-davis.com>
  2 siblings, 2 replies; 33+ messages in thread

From: Peter Eisentraut @ 2024-08-07 20:44 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On 06.08.24 23:40, Jeff Davis wrote:
> With these changes, collations are no longer dependent on the
> environment locale (setlocale()) at all for either collation behavior
> (ORDER BY) or ctype behavior (LOWER(), etc.).
> 
> Additionally, unless I missed something, nothing in the server is
> dependent on LC_COLLATE at all.
> 
> There are still some things that depend on setlocale() in one way or
> another:
> 
>    - char2wchar() & wchar2char()
>    - ts_locale.c
>    - various places that depend on LC_CTYPE unrelated to the collation
> infrastructure
>    - things that depend on other locale settings, like LC_NUMERIC
> 
> We can address those as part of a separate thread. I'll count this as
> committed.

I have a couple of small follow-up patches for this.

First, in like.c, SB_lower_char() now reads:

static char
SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c)
{
     if (locale_is_c)
         return pg_ascii_tolower(c);
     else if (locale)
         return tolower_l(c, locale->info.lt);
     else
         return pg_tolower(c);
}

But after this patch set, locale cannot be NULL anymore, so the third 
branch is obsolete.

(Now that I look at it, pg_tolower() has some short-circuiting for ASCII 
letters, so it would not handle Turkish-i correctly if that had been the 
global locale.  By removing the use of pg_tolower(), we fix that issue 
in passing.)

Second, there are a number of functions in like.c like the above that 
take separate arguments like pg_locale_t locale, bool locale_is_c. 
Because pg_locale_t now contains the locale_is_c information, these can 
be combined.

Attachments: download all (2 files, .tar.gz)

0001-Remove-dead-code.patch (text/plain, 887B) download From 7de0b6c3069c5fbef7729b7a86e40b75dfb616fd Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <peter@eisentraut.org> Date: Wed, 7 Aug 2024 22:24:39 +0200 Subject: [PATCH 1/2] Remove dead code After e9931bfb751, the locale argument of SB_lower_char() is never not NULL, so the branch that deals with NULL can be removed. --- src/backend/utils/adt/like.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 0ecc96d48e5..bb4d73fb22e 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -95,10 +95,8 @@ SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) { if (locale_is_c) return pg_ascii_tolower(c); - else if (locale) - return tolower_l(c, locale->info.lt); else - return pg_tolower(c); + return tolower_l(c, locale->info.lt); } -- 2.46.0
0002-Remove-separate-locale_is_c-arguments.patch (text/plain, 5.0K) download From 5d3b595fc08c3a013b5ebe532b3922bf9db7d963 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <peter@eisentraut.org> Date: Wed, 7 Aug 2024 22:30:30 +0200 Subject: [PATCH 2/2] Remove separate locale_is_c arguments Since e9931bfb751, ctype_is_c is part of pg_locale_t. Some functions passed a pg_locale_t and a bool argument separately. This can now be combined into one argument. --- src/backend/utils/adt/like.c | 30 +++++++++++++++--------------- src/backend/utils/adt/like_match.c | 6 ++---- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index bb4d73fb22e..a3a1178887c 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -33,18 +33,18 @@ static int SB_MatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static text *SB_do_like_escape(text *pat, text *esc); static int MB_MatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static text *MB_do_like_escape(text *pat, text *esc); static int UTF8_MatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static int SB_IMatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation); static int Generic_Text_IC_like(text *str, text *pat, Oid collation); @@ -91,9 +91,9 @@ wchareq(const char *p1, const char *p2) * fold-on-the-fly processing, however. */ static char -SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) +SB_lower_char(unsigned char c, pg_locale_t locale) { - if (locale_is_c) + if (locale->ctype_is_c) return pg_ascii_tolower(c); else return tolower_l(c, locale->info.lt); @@ -129,7 +129,7 @@ SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) #include "like_match.c" /* setup to compile like_match.c for single byte case insensitive matches */ -#define MATCH_LOWER(t) SB_lower_char((unsigned char) (t), locale, locale_is_c) +#define MATCH_LOWER(t) SB_lower_char((unsigned char) (t), locale) #define NextChar(p, plen) NextByte((p), (plen)) #define MatchText SB_IMatchText @@ -158,11 +158,11 @@ GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation } if (pg_database_encoding_max_length() == 1) - return SB_MatchText(s, slen, p, plen, 0, true); + return SB_MatchText(s, slen, p, plen, 0); else if (GetDatabaseEncoding() == PG_UTF8) - return UTF8_MatchText(s, slen, p, plen, 0, true); + return UTF8_MatchText(s, slen, p, plen, 0); else - return MB_MatchText(s, slen, p, plen, 0, true); + return MB_MatchText(s, slen, p, plen, 0); } static inline int @@ -212,9 +212,9 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) s = VARDATA_ANY(str); slen = VARSIZE_ANY_EXHDR(str); if (GetDatabaseEncoding() == PG_UTF8) - return UTF8_MatchText(s, slen, p, plen, 0, true); + return UTF8_MatchText(s, slen, p, plen, 0); else - return MB_MatchText(s, slen, p, plen, 0, true); + return MB_MatchText(s, slen, p, plen, 0); } else { @@ -222,7 +222,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) plen = VARSIZE_ANY_EXHDR(pat); s = VARDATA_ANY(str); slen = VARSIZE_ANY_EXHDR(str); - return SB_IMatchText(s, slen, p, plen, locale, locale->ctype_is_c); + return SB_IMatchText(s, slen, p, plen, locale); } } @@ -330,7 +330,7 @@ bytealike(PG_FUNCTION_ARGS) p = VARDATA_ANY(pat); plen = VARSIZE_ANY_EXHDR(pat); - result = (SB_MatchText(s, slen, p, plen, 0, true) == LIKE_TRUE); + result = (SB_MatchText(s, slen, p, plen, 0) == LIKE_TRUE); PG_RETURN_BOOL(result); } @@ -351,7 +351,7 @@ byteanlike(PG_FUNCTION_ARGS) p = VARDATA_ANY(pat); plen = VARSIZE_ANY_EXHDR(pat); - result = (SB_MatchText(s, slen, p, plen, 0, true) != LIKE_TRUE); + result = (SB_MatchText(s, slen, p, plen, 0) != LIKE_TRUE); PG_RETURN_BOOL(result); } diff --git a/src/backend/utils/adt/like_match.c b/src/backend/utils/adt/like_match.c index f2990edff7e..928a1e99fc5 100644 --- a/src/backend/utils/adt/like_match.c +++ b/src/backend/utils/adt/like_match.c @@ -77,8 +77,7 @@ #endif static int -MatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c) +MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale) { /* Fast path for match-everything pattern */ if (plen == 1 && *p == '%') @@ -175,8 +174,7 @@ MatchText(const char *t, int tlen, const char *p, int plen, { if (GETCHAR(*t) == firstpat) { - int matched = MatchText(t, tlen, p, plen, - locale, locale_is_c); + int matched = MatchText(t, tlen, p, plen, locale); if (matched != LIKE_FALSE) return matched; /* TRUE or ABORT */ -- 2.46.0
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-08 20:00  Jeff Davis <pgsql@j-davis.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  1 sibling, 2 replies; 33+ messages in thread

From: Jeff Davis @ 2024-08-08 20:00 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On Wed, 2024-08-07 at 22:44 +0200, Peter Eisentraut wrote:
> But after this patch set, locale cannot be NULL anymore, so the third
> branch is obsolete.

...

> Second, there are a number of functions in like.c like the above that
> take separate arguments like pg_locale_t locale, bool locale_is_c. 
> Because pg_locale_t now contains the locale_is_c information, these
> can 
> be combined.

I believe these patches are correct, but the reasoning is fairly
complex:

1. Some MatchText variants are called with 0 for locale. But that's OK
because ...

2. A MatchText variant only cares about the locale if MATCH_LOWER(t) is
defined, and ...

3. Only one variant, SB_IMatchText() defines MATCH_LOWER(), and ...

4. SB_IMatchText() is called with a non-zero locale.

All of these are a bit confusing to follow because it's generated code.
#2 is particularly non-obvious, because "locale" is not even an
argument of the MATCH_LOWER(t) or GETCHAR(t) macros, it's taken
implicitly from the outer scope.

I don't think your patches cause this confusion, but is there a way you
can clarify some of this along the way?

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-08 21:09  Peter Eisentraut <peter@eisentraut.org>
  parent: Peter Eisentraut <peter@eisentraut.org>
  1 sibling, 1 reply; 33+ messages in thread

From: Peter Eisentraut @ 2024-08-08 21:09 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On 07.08.24 22:44, Peter Eisentraut wrote:
> (Now that I look at it, pg_tolower() has some short-circuiting for ASCII 
> letters, so it would not handle Turkish-i correctly if that had been the 
> global locale.  By removing the use of pg_tolower(), we fix that issue 
> in passing.)

It occurred to me that this issue also surfaces in a more prominent 
place.  These arguably-wrong pg_tolower() and pg_toupper() calls were 
also used by the normal SQL lower() and upper() functions before commit 
e9931bfb751 if you used a single byte encoding.

For example, in PG17, multi-byte encoding:

initdb --locale=tr_TR.utf8

select upper('hij'); --> HİJ

PG17, single-byte encoding:

initdb --locale=tr_TR  # uses LATIN5

select upper('hij'); --> HIJ

With current master, after commit e9931bfb751, you get the first result 
in both cases.

So this could break indexes across pg_upgrade in such configurations.






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-08 21:18  Peter Eisentraut <peter@eisentraut.org>
  parent: Jeff Davis <pgsql@j-davis.com>
  1 sibling, 0 replies; 33+ messages in thread

From: Peter Eisentraut @ 2024-08-08 21:18 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On 08.08.24 22:00, Jeff Davis wrote:
> On Wed, 2024-08-07 at 22:44 +0200, Peter Eisentraut wrote:
>> But after this patch set, locale cannot be NULL anymore, so the third
>> branch is obsolete.
> 
> ...
> 
>> Second, there are a number of functions in like.c like the above that
>> take separate arguments like pg_locale_t locale, bool locale_is_c.
>> Because pg_locale_t now contains the locale_is_c information, these
>> can
>> be combined.
> 
> I believe these patches are correct, but the reasoning is fairly
> complex:
> 
> 1. Some MatchText variants are called with 0 for locale. But that's OK
> because ...
> 
> 2. A MatchText variant only cares about the locale if MATCH_LOWER(t) is
> defined, and ...
> 
> 3. Only one variant, SB_IMatchText() defines MATCH_LOWER(), and ...
> 
> 4. SB_IMatchText() is called with a non-zero locale.
> 
> All of these are a bit confusing to follow because it's generated code.
> #2 is particularly non-obvious, because "locale" is not even an
> argument of the MATCH_LOWER(t) or GETCHAR(t) macros, it's taken
> implicitly from the outer scope.
> 
> I don't think your patches cause this confusion, but is there a way you
> can clarify some of this along the way?

Yes, this is also my analysis.  The patch in 
<https://www.postgresql.org/message-id/flat/700d2e86-bf75-4607-9cf2-f5b7802f6e88@eisentraut.org; 
would replace passing 0 with an actual locale object.  The changes in 
GenericMatchText() could also be applied independently so that we'd 
always pass in a non-zero locale value, even if it would not be used in 
some cases.  I need to update that patch to cover your latest changes. 
I'll see if I can propose something here that looks a bit nicer.






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-08 22:38  Jeff Davis <pgsql@j-davis.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 0 replies; 33+ messages in thread

From: Jeff Davis @ 2024-08-08 22:38 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On Thu, 2024-08-08 at 23:09 +0200, Peter Eisentraut wrote:
> With current master, after commit e9931bfb751, you get the first
> result 
> in both cases.
> 
> So this could break indexes across pg_upgrade in such configurations.

Good observation. Is there any action we should take here, or just add
that to the release notes for 18?

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-11 16:33  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Jeff Davis <pgsql@j-davis.com>
  2 siblings, 1 reply; 33+ messages in thread

From: Tom Lane @ 2024-08-11 16:33 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; +Cc: Peter Eisentraut <peter@eisentraut.org>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

Jeff Davis <pgsql@j-davis.com> writes:
> We can address those as part of a separate thread. I'll count this as
> committed.

Coverity has a nit about this:

*** CID 1616189:  Null pointer dereferences  (REVERSE_INULL)
/srv/coverity/git/pgsql-git/postgresql/src/backend/utils/adt/like.c: 206 in Generic_Text_IC_like()
200     	 * on the pattern and text, but instead call SB_lower_char on each
201     	 * character.  In the multi-byte case we don't have much choice :-(. Also,
202     	 * ICU does not support single-character case folding, so we go the long
203     	 * way.
204     	 */
205     
>>>     CID 1616189:  Null pointer dereferences  (REVERSE_INULL)
>>>     Null-checking "locale" suggests that it may be null, but it has already been dereferenced on all paths leading to the check.
206     	if (pg_database_encoding_max_length() > 1 || (locale && locale->provider == COLLPROVIDER_ICU))
207     	{
208     		pat = DatumGetTextPP(DirectFunctionCall1Coll(lower, collation,
209     													 PointerGetDatum(pat)));
210     		p = VARDATA_ANY(pat);
211     		plen = VARSIZE_ANY_EXHDR(pat);

I assume it would now be okay to take out "locale &&" here?

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-12 07:00  Peter Eisentraut <peter@eisentraut.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 33+ messages in thread

From: Peter Eisentraut @ 2024-08-12 07:00 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; Jeff Davis <pgsql@j-davis.com>; +Cc: Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On 11.08.24 18:33, Tom Lane wrote:
> Jeff Davis <pgsql@j-davis.com> writes:
>> We can address those as part of a separate thread. I'll count this as
>> committed.
> 
> Coverity has a nit about this:
> 
> *** CID 1616189:  Null pointer dereferences  (REVERSE_INULL)
> /srv/coverity/git/pgsql-git/postgresql/src/backend/utils/adt/like.c: 206 in Generic_Text_IC_like()
> 200     	 * on the pattern and text, but instead call SB_lower_char on each
> 201     	 * character.  In the multi-byte case we don't have much choice :-(. Also,
> 202     	 * ICU does not support single-character case folding, so we go the long
> 203     	 * way.
> 204     	 */
> 205
>>>>      CID 1616189:  Null pointer dereferences  (REVERSE_INULL)
>>>>      Null-checking "locale" suggests that it may be null, but it has already been dereferenced on all paths leading to the check.
> 206     	if (pg_database_encoding_max_length() > 1 || (locale && locale->provider == COLLPROVIDER_ICU))
> 207     	{
> 208     		pat = DatumGetTextPP(DirectFunctionCall1Coll(lower, collation,
> 209     													 PointerGetDatum(pat)));
> 210     		p = VARDATA_ANY(pat);
> 211     		plen = VARSIZE_ANY_EXHDR(pat);
> 
> I assume it would now be okay to take out "locale &&" here?

Correct, that check is no longer necessary.






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-12 19:47  Jeff Davis <pgsql@j-davis.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 0 replies; 33+ messages in thread

From: Jeff Davis @ 2024-08-12 19:47 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On Mon, 2024-08-12 at 09:00 +0200, Peter Eisentraut wrote:
> > I assume it would now be okay to take out "locale &&" here?
> 
> Correct, that check is no longer necessary.

Thank you, fixed.

Regards,
	Jeff Davis







^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-13 15:11  Peter Eisentraut <peter@eisentraut.org>
  parent: Jeff Davis <pgsql@j-davis.com>
  2 siblings, 1 reply; 33+ messages in thread

From: Peter Eisentraut @ 2024-08-13 15:11 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

I'm wondering if after this patch series, lc_collate_is_c() and 
lc_ctype_is_c() are still useful.

They used to be completely separate from pg_newlocale_from_collation(), 
but now they are just mostly a thin wrapper around it.  Except there is 
some hardcoded handling of C_COLLATION_OID and POSIX_COLLATION_OID.  Do 
we care about that?

In many places, the notational and structural complexity would be 
significantly improved if we changed code like

     if (pg_collate_is_c(colloid))
     {
         ...
     }
     else
     {
         pg_locale_t locale = pg_newlocale_from_collation(colloid);

         if (locale->provider == ...)
         {
             ...
     }

to more like

     pg_locale_t locale = pg_newlocale_from_collation(colloid);

     if (locale->collate_is_c)
     {
         ...
     }
     else if (locale->provider == ...)
         ...
     }
     ...

However, it's not clear whether the hardcoded handling of some 
collations is needed for performance parity or perhaps some 
bootstrapping reasons.  It would be useful to get that cleared up. 
Thoughts?






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-13 17:56  Jeff Davis <pgsql@j-davis.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-08-13 17:56 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On Tue, 2024-08-13 at 17:11 +0200, Peter Eisentraut wrote:

> They used to be completely separate from
> pg_newlocale_from_collation(), 
> but now they are just mostly a thin wrapper around it.  Except there
> is 
> some hardcoded handling of C_COLLATION_OID and POSIX_COLLATION_OID. 
> Do 
> we care about that?

...

> However, it's not clear whether the hardcoded handling of some 
> collations is needed for performance parity or perhaps some 
> bootstrapping reasons.  It would be useful to get that cleared up. 
> Thoughts?

There's at least one place where we expect lc_collate_is_c() to work
without catalog access at all: libpq/hba.c uses regexes with
C_COLLATION_OID.

But I don't think that's a major problem -- we can just move the
hardcoded test into pg_newlocale_from_collation() and return a
predefined struct with collate_is_c/ctype_is_c already set.

+1.

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-13 23:31  Andreas Karlsson <andreas@proxel.se>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Andreas Karlsson @ 2024-08-13 23:31 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On 8/13/24 7:56 PM, Jeff Davis wrote:
> But I don't think that's a major problem -- we can just move the
> hardcoded test into pg_newlocale_from_collation() and return a
> predefined struct with collate_is_c/ctype_is_c already set.

I tried that out but thought it felt cleaner to do the hardcoding in 
pg_set_regex_collation(). What do you think?

I have attached patches removing lc_collate_is_c() and lc_ctype_is_c(). 
I have not checked if there are any performance regressions when using 
the C and POSIX locales but remove these special cases makes the code a 
lot cleaner in my book.

I also attach some other clean up patches I did while touching this code.

0001-Remove-lc_collate_is_c.patch

Removes lc_collate_is_c().

0002-Remove-lc_ctype_is_c.patch

Removes lc_ctype_is_c() and POSIX_COLLATION_OID which is no longer 
necessary.

0003-Remove-dubious-check-against-default-locale.patch

This patch removes a check against DEFAULT_COLLATION_OID which I thought 
looked really dubious. Shouldn't this just be a simple check for if the 
locale is deterministic? Since we know we have a valid locale that 
should be enough, right?

0004-Do-not-check-both-for-collate_is_c-and-deterministic.patch

It is redundant to check both for "collation_is_c && deterministic", right?

0005-Remove-pg_collate_deterministic-and-check-field-dire.patch

Since after my patches we look a lot directly at the collation_is_c and 
ctype_is_c fields I think the thin wrapper around the deterministic 
field makes it seem like there is more to it so I suggest that we should 
just remove it.

0006-Slightly-refactor-varstr_sortsupport-to-improve-read.patch

Small refactor to make a hard to read function a bit easier to read.

Andreas

Attachments: download all (6 files, .tar.gz)

0001-Remove-lc_collate_is_c.patch (text/x-patch, 9.8K) download From 99ffa0b76ce810846af09f4430903dd59ed06366 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:37:23 +0200 Subject: [PATCH 1/6] Remove lc_collate_is_c() Instead always look up the collation and check to collate_is_c field. TODO: Check for performance regression? --- src/backend/access/spgist/spgtextproc.c | 2 +- src/backend/commands/collationcmds.c | 8 ++----- src/backend/utils/adt/like_support.c | 4 ++-- src/backend/utils/adt/pg_locale.c | 27 --------------------- src/backend/utils/adt/selfuncs.c | 6 +++-- src/backend/utils/adt/varchar.c | 20 +++++----------- src/backend/utils/adt/varlena.c | 32 ++++++++++++------------- src/include/utils/pg_locale.h | 1 - 8 files changed, 30 insertions(+), 70 deletions(-) diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c index 3f08d330b6..d5237a68b5 100644 --- a/src/backend/access/spgist/spgtextproc.c +++ b/src/backend/access/spgist/spgtextproc.c @@ -427,7 +427,7 @@ spg_text_inner_consistent(PG_FUNCTION_ARGS) { spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0); spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1); - bool collate_is_c = lc_collate_is_c(PG_GET_COLLATION()); + bool collate_is_c = pg_newlocale_from_collation(PG_GET_COLLATION())->collate_is_c; text *reconstructedValue; text *reconstrText; int maxReconstrLen; diff --git a/src/backend/commands/collationcmds.c b/src/backend/commands/collationcmds.c index 63ef9a0841..53b6a479aa 100644 --- a/src/backend/commands/collationcmds.c +++ b/src/backend/commands/collationcmds.c @@ -377,13 +377,9 @@ DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_e if (!OidIsValid(newoid)) return InvalidObjectAddress; - /* - * Check that the locales can be loaded. NB: pg_newlocale_from_collation - * is only supposed to be called on non-C-equivalent locales. - */ + /* Check that the locales can be loaded. */ CommandCounterIncrement(); - if (!lc_collate_is_c(newoid) || !lc_ctype_is_c(newoid)) - (void) pg_newlocale_from_collation(newoid); + (void) pg_newlocale_from_collation(newoid); ObjectAddressSet(address, CollationRelationId, newoid); diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c index 2635050861..a579fa0157 100644 --- a/src/backend/utils/adt/like_support.c +++ b/src/backend/utils/adt/like_support.c @@ -433,7 +433,7 @@ match_pattern_prefix(Node *leftop, * collation. */ if (collation_aware && - !lc_collate_is_c(indexcollation)) + !pg_newlocale_from_collation(indexcollation)->collate_is_c) return NIL; /* @@ -1603,7 +1603,7 @@ make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation) else workstr = TextDatumGetCString(str_const->constvalue); len = strlen(workstr); - if (lc_collate_is_c(collation) || len == 0) + if (pg_newlocale_from_collation(collation)->collate_is_c || len == 0) cmpstr = str_const->constvalue; else { diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index cd3661e727..7512ac055e 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1258,33 +1258,6 @@ lookup_collation_cache(Oid collation) return cache_entry; } - -/* - * Detect whether collation's LC_COLLATE property is C - */ -bool -lc_collate_is_c(Oid collation) -{ - /* - * If we're asked about "collation 0", return false, so that the code will - * go into the non-C path and report that the collation is bogus. - */ - if (!OidIsValid(collation)) - return false; - - /* - * If we're asked about the built-in C/POSIX collations, we know that. - */ - if (collation == C_COLLATION_OID || - collation == POSIX_COLLATION_OID) - return true; - - /* - * Otherwise, we have to consult pg_collation, but we cache that. - */ - return pg_newlocale_from_collation(collation)->collate_is_c; -} - /* * Detect whether collation's LC_CTYPE property is C */ diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index bf42393bec..03d7fb5f48 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -4646,6 +4646,7 @@ static char * convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure) { char *val; + pg_locale_t mylocale; switch (typid) { @@ -4671,9 +4672,10 @@ convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure) return NULL; } - if (!lc_collate_is_c(collid)) + mylocale = pg_newlocale_from_collation(collid); + + if (!mylocale->collate_is_c) { - pg_locale_t mylocale = pg_newlocale_from_collation(collid); char *xfrmstr; size_t xfrmlen; size_t xfrmlen2 PG_USED_FOR_ASSERTS_ONLY; diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 0c219dcc77..d2d1c5709d 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -748,20 +748,16 @@ bpchareq(PG_FUNCTION_ARGS) len2; bool result; Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; check_collation_set(collid); len1 = bcTruelen(arg1); len2 = bcTruelen(arg2); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (mylocale->collate_is_c || pg_locale_deterministic(mylocale)) { /* * Since we only care about equality or not-equality, we can avoid all @@ -793,20 +789,16 @@ bpcharne(PG_FUNCTION_ARGS) len2; bool result; Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; check_collation_set(collid); len1 = bcTruelen(arg1); len2 = bcTruelen(arg2); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (mylocale->collate_is_c || pg_locale_deterministic(mylocale)) { /* * Since we only care about equality or not-equality, we can avoid all diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 52ab8c43c6..81343bd0e1 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1538,16 +1538,19 @@ int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid) { int result; + pg_locale_t mylocale; check_collation_set(collid); + mylocale = pg_newlocale_from_collation(collid); + /* * Unfortunately, there is no strncoll(), so in the non-C locale case we * have to do some memory copying. This turns out to be significantly * slower, so we optimize the case where LC_COLLATE is C. We also try to * optimize relatively-short strings by avoiding palloc/pfree overhead. */ - if (lc_collate_is_c(collid)) + if (mylocale->collate_is_c) { result = memcmp(arg1, arg2, Min(len1, len2)); if ((result == 0) && (len1 != len2)) @@ -1555,10 +1558,6 @@ varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid) } else { - pg_locale_t mylocale; - - mylocale = pg_newlocale_from_collation(collid); - /* * memcmp() can't tell us which of two unequal strings sorts first, * but it's a cheap way to tell if they're equal. Testing shows that @@ -1865,10 +1864,12 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) bool abbreviate = ssup->abbreviate; bool collate_c = false; VarStringSortSupport *sss; - pg_locale_t locale = 0; + pg_locale_t locale; check_collation_set(collid); + locale = pg_newlocale_from_collation(collid); + /* * If possible, set ssup->comparator to a function which can be used to * directly compare two datums. If we can do this, we'll avoid the @@ -1882,7 +1883,7 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) * varstrfastcmp_c, bpcharfastcmp_c, or namefastcmp_c, all of which use * memcmp() rather than strcoll(). */ - if (lc_collate_is_c(collid)) + if (locale->collate_is_c) { if (typid == BPCHAROID) ssup->comparator = bpcharfastcmp_c; @@ -1899,13 +1900,6 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) } else { - /* - * We need a collation-sensitive comparison. To make things faster, - * we'll figure out the collation based on the locale id and cache the - * result. - */ - locale = pg_newlocale_from_collation(collid); - /* * We use varlenafastcmp_locale except for type NAME. */ @@ -1956,7 +1950,8 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) sss->last_len2 = -1; /* Initialize */ sss->last_returned = 0; - sss->locale = locale; + if (!collate_c) + sss->locale = locale; /* * To avoid somehow confusing a strxfrm() blob and an original string, @@ -2546,12 +2541,15 @@ btvarstrequalimage(PG_FUNCTION_ARGS) { /* Oid opcintype = PG_GETARG_OID(0); */ Oid collid = PG_GET_COLLATION(); + pg_locale_t locale; check_collation_set(collid); - if (lc_collate_is_c(collid) || + locale = pg_newlocale_from_collation(collid); + + if (locale->collate_is_c || collid == DEFAULT_COLLATION_OID || - get_collation_isdeterministic(collid)) + pg_locale_deterministic(locale)) PG_RETURN_BOOL(true); else PG_RETURN_BOOL(false); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index f41d33975b..8ec24437f4 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -54,7 +54,6 @@ extern PGDLLIMPORT bool database_ctype_is_c; extern bool check_locale(int category, const char *locale, char **canonname); extern char *pg_perm_setlocale(int category, const char *locale); -extern bool lc_collate_is_c(Oid collation); extern bool lc_ctype_is_c(Oid collation); /* -- 2.43.0
0002-Remove-lc_ctype_is_c.patch (text/x-patch, 9.9K) download From c7486013aca89bab8178cb7ca0944c52d5805820 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:37:48 +0200 Subject: [PATCH 2/6] Remove lc_ctype_is_c() Instead always fetch the locale and look at the ctype_is_c field. Since regular expressions are used by pg_hba we need to hardcode the logic for regular expression using C_COLLATE_OID. --- src/backend/regex/regc_pg_locale.c | 20 ++++++++++++++------ src/backend/utils/adt/formatting.c | 27 ++++++++++++--------------- src/backend/utils/adt/like.c | 2 +- src/backend/utils/adt/like_support.c | 15 +++++---------- src/backend/utils/adt/pg_locale.c | 26 -------------------------- src/include/catalog/pg_collation.dat | 3 +-- src/include/utils/pg_locale.h | 2 -- 7 files changed, 33 insertions(+), 62 deletions(-) diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 947d73f3e0..21f640a7e1 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -243,29 +243,36 @@ pg_set_regex_collation(Oid collation) errhint("Use the COLLATE clause to set the collation explicitly."))); } - if (lc_ctype_is_c(collation)) + if (collation == C_COLLATION_OID) { - /* C/POSIX collations use this path regardless of database encoding */ + /* Allow using regex in C locale without catalog */ pg_regex_strategy = PG_REGEX_LOCALE_C; pg_regex_locale = 0; pg_regex_collation = C_COLLATION_OID; } else { - pg_regex_locale = pg_newlocale_from_collation(collation); + pg_locale_t locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(pg_regex_locale)) + if (!pg_locale_deterministic(locale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for regular expressions"))); - if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN) + if (locale->ctype_is_c) + { + /* C/POSIX collations use this path regardless of database encoding */ + pg_regex_strategy = PG_REGEX_LOCALE_C; + locale = 0; + collation = C_COLLATION_OID; + } + else if (locale->provider == COLLPROVIDER_BUILTIN) { Assert(GetDatabaseEncoding() == PG_UTF8); pg_regex_strategy = PG_REGEX_BUILTIN; } #ifdef USE_ICU - else if (pg_regex_locale->provider == COLLPROVIDER_ICU) + else if (locale->provider == COLLPROVIDER_ICU) { pg_regex_strategy = PG_REGEX_LOCALE_ICU; } @@ -278,6 +285,7 @@ pg_set_regex_collation(Oid collation) pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L; } + pg_regex_locale = locale; pg_regex_collation = collation; } } diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 68069fcfd3..dc5178f56b 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1636,6 +1636,7 @@ char * str_tolower(const char *buff, size_t nbytes, Oid collid) { char *result; + pg_locale_t mylocale; if (!buff) return NULL; @@ -1653,17 +1654,15 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) errhint("Use the COLLATE clause to set the collation explicitly."))); } + mylocale = pg_newlocale_from_collation(collid); + /* C/POSIX collations use this path regardless of database encoding */ - if (lc_ctype_is_c(collid)) + if (mylocale->ctype_is_c) { result = asc_tolower(buff, nbytes); } else { - pg_locale_t mylocale; - - mylocale = pg_newlocale_from_collation(collid); - #ifdef USE_ICU if (mylocale->provider == COLLPROVIDER_ICU) { @@ -1774,6 +1773,7 @@ char * str_toupper(const char *buff, size_t nbytes, Oid collid) { char *result; + pg_locale_t mylocale; if (!buff) return NULL; @@ -1791,17 +1791,15 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) errhint("Use the COLLATE clause to set the collation explicitly."))); } + mylocale = pg_newlocale_from_collation(collid); + /* C/POSIX collations use this path regardless of database encoding */ - if (lc_ctype_is_c(collid)) + if (mylocale->ctype_is_c) { result = asc_toupper(buff, nbytes); } else { - pg_locale_t mylocale; - - mylocale = pg_newlocale_from_collation(collid); - #ifdef USE_ICU if (mylocale->provider == COLLPROVIDER_ICU) { @@ -1954,6 +1952,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) { char *result; int wasalnum = false; + pg_locale_t mylocale; if (!buff) return NULL; @@ -1971,17 +1970,15 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) errhint("Use the COLLATE clause to set the collation explicitly."))); } + mylocale = pg_newlocale_from_collation(collid); + /* C/POSIX collations use this path regardless of database encoding */ - if (lc_ctype_is_c(collid)) + if (mylocale->ctype_is_c) { result = asc_initcap(buff, nbytes); } else { - pg_locale_t mylocale; - - mylocale = pg_newlocale_from_collation(collid); - #ifdef USE_ICU if (mylocale->provider == COLLPROVIDER_ICU) { diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 131616fa6b..f87675d755 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -147,7 +147,7 @@ SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) static inline int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation) { - if (collation && !lc_ctype_is_c(collation)) + if (collation) { pg_locale_t locale = pg_newlocale_from_collation(collation); diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c index a579fa0157..fcfd2576b2 100644 --- a/src/backend/utils/adt/like_support.c +++ b/src/backend/utils/adt/like_support.c @@ -100,7 +100,7 @@ static Selectivity regex_selectivity(const char *patt, int pattlen, bool case_insensitive, int fixed_prefix_len); static int pattern_char_isalpha(char c, bool is_multibyte, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation); static Datum string_to_datum(const char *str, Oid datatype); @@ -1000,7 +1000,6 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, match_pos; bool is_multibyte = (pg_database_encoding_max_length() > 1); pg_locale_t locale = 0; - bool locale_is_c = false; /* the right-hand const is type text or bytea */ Assert(typeid == BYTEAOID || typeid == TEXTOID); @@ -1024,11 +1023,7 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, errhint("Use the COLLATE clause to set the collation explicitly."))); } - /* If case-insensitive, we need locale info */ - if (lc_ctype_is_c(collation)) - locale_is_c = true; - else - locale = pg_newlocale_from_collation(collation); + locale = pg_newlocale_from_collation(collation); } if (typeid != BYTEAOID) @@ -1065,7 +1060,7 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, /* Stop if case-varying character (it's sort of a wildcard) */ if (case_insensitive && - pattern_char_isalpha(patt[pos], is_multibyte, locale, locale_is_c)) + pattern_char_isalpha(patt[pos], is_multibyte, locale)) break; match[match_pos++] = patt[pos]; @@ -1499,9 +1494,9 @@ regex_selectivity(const char *patt, int pattlen, bool case_insensitive, */ static int pattern_char_isalpha(char c, bool is_multibyte, - pg_locale_t locale, bool locale_is_c) + pg_locale_t locale) { - if (locale_is_c) + if (locale->ctype_is_c) return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); else if (is_multibyte && IS_HIGHBIT_SET(c)) return true; diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 7512ac055e..b1663e2b6e 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1258,32 +1258,6 @@ lookup_collation_cache(Oid collation) return cache_entry; } -/* - * Detect whether collation's LC_CTYPE property is C - */ -bool -lc_ctype_is_c(Oid collation) -{ - /* - * If we're asked about "collation 0", return false, so that the code will - * go into the non-C path and report that the collation is bogus. - */ - if (!OidIsValid(collation)) - return false; - - /* - * If we're asked about the built-in C/POSIX collations, we know that. - */ - if (collation == C_COLLATION_OID || - collation == POSIX_COLLATION_OID) - return true; - - /* - * Otherwise, we have to consult pg_collation, but we cache that. - */ - return pg_newlocale_from_collation(collation)->ctype_is_c; -} - /* simple subroutine for reporting errors from newlocale() */ static void report_newlocale_failure(const char *localename) diff --git a/src/include/catalog/pg_collation.dat b/src/include/catalog/pg_collation.dat index f126201276..af5c9aa582 100644 --- a/src/include/catalog/pg_collation.dat +++ b/src/include/catalog/pg_collation.dat @@ -19,8 +19,7 @@ descr => 'standard C collation', collname => 'C', collprovider => 'c', collencoding => '-1', collcollate => 'C', collctype => 'C' }, -{ oid => '951', oid_symbol => 'POSIX_COLLATION_OID', - descr => 'standard POSIX collation', +{ oid => '951', descr => 'standard POSIX collation', collname => 'POSIX', collprovider => 'c', collencoding => '-1', collcollate => 'POSIX', collctype => 'POSIX' }, { oid => '962', descr => 'sorts by Unicode code point, C character semantics', diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 8ec24437f4..ab1c37a44b 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -54,8 +54,6 @@ extern PGDLLIMPORT bool database_ctype_is_c; extern bool check_locale(int category, const char *locale, char **canonname); extern char *pg_perm_setlocale(int category, const char *locale); -extern bool lc_ctype_is_c(Oid collation); - /* * Return the POSIX lconv struct (contains number/money formatting * information) with locale information for all categories. -- 2.43.0
0003-Remove-dubious-check-against-default-locale.patch (text/x-patch, 741B) download From 6a1c264ff11a1d745a789c06ea7d13a8c5e28363 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:47:08 +0200 Subject: [PATCH 3/6] Remove dubious check against default locale --- src/backend/utils/adt/varlena.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 81343bd0e1..1c02a67d72 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -2548,7 +2548,6 @@ btvarstrequalimage(PG_FUNCTION_ARGS) locale = pg_newlocale_from_collation(collid); if (locale->collate_is_c || - collid == DEFAULT_COLLATION_OID || pg_locale_deterministic(locale)) PG_RETURN_BOOL(true); else -- 2.43.0
0004-Do-not-check-both-for-collate_is_c-and-deterministic.patch (text/x-patch, 1.7K) download From 4e3dd6fcf2f33d709699216a7c7fa7f7dca54d44 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:49:46 +0200 Subject: [PATCH 4/6] Do not check both for collate_is_c and deterministic Since deterimistic will always be true if collate_is_c we can just check for deterimistic and make the code cleaner to read. --- src/backend/utils/adt/varchar.c | 4 ++-- src/backend/utils/adt/varlena.c | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index d2d1c5709d..03e91c0333 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -757,7 +757,7 @@ bpchareq(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (mylocale->collate_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { /* * Since we only care about equality or not-equality, we can avoid all @@ -798,7 +798,7 @@ bpcharne(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (mylocale->collate_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { /* * Since we only care about equality or not-equality, we can avoid all diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 1c02a67d72..2086c47e0f 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -2547,8 +2547,7 @@ btvarstrequalimage(PG_FUNCTION_ARGS) locale = pg_newlocale_from_collation(collid); - if (locale->collate_is_c || - pg_locale_deterministic(locale)) + if (pg_locale_deterministic(locale)) PG_RETURN_BOOL(true); else PG_RETURN_BOOL(false); -- 2.43.0
0005-Remove-pg_collate_deterministic-and-check-field-dire.patch (text/x-patch, 7.5K) download From 86d66751699a232beaf49e469ff6029a8b9caa21 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:52:53 +0200 Subject: [PATCH 5/6] Remove pg_collate_deterministic() and check field directly There is no clear benefit from using this thin accessor function, espeically since other fields like collate_is_c and ctype_is_c are accessed directly. --- src/backend/access/hash/hashfunc.c | 4 ++-- src/backend/regex/regc_pg_locale.c | 2 +- src/backend/utils/adt/like.c | 4 ++-- src/backend/utils/adt/pg_locale.c | 7 ------- src/backend/utils/adt/varchar.c | 8 ++++---- src/backend/utils/adt/varlena.c | 14 +++++++------- src/include/utils/pg_locale.h | 1 - 7 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index 86f1adecb7..01bdf997e3 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -279,7 +279,7 @@ hashtext(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { result = hash_any((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); @@ -334,7 +334,7 @@ hashtextextended(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { result = hash_any_extended((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key), diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 21f640a7e1..1683d706b9 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -254,7 +254,7 @@ pg_set_regex_collation(Oid collation) { pg_locale_t locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for regular expressions"))); diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index f87675d755..286b737fd7 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -151,7 +151,7 @@ GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation { pg_locale_t locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for LIKE"))); @@ -188,7 +188,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for ILIKE"))); diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index b1663e2b6e..8e952c7f16 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1407,13 +1407,6 @@ make_icu_collator(const char *iculocstr, #endif /* not USE_ICU */ } - -bool -pg_locale_deterministic(pg_locale_t locale) -{ - return locale->deterministic; -} - /* * Initialize default_locale with database locale settings. */ diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 03e91c0333..8f86aca297 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -757,7 +757,7 @@ bpchareq(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { /* * Since we only care about equality or not-equality, we can avoid all @@ -798,7 +798,7 @@ bpcharne(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { /* * Since we only care about equality or not-equality, we can avoid all @@ -1005,7 +1005,7 @@ hashbpchar(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { result = hash_any((unsigned char *) keydata, keylen); } @@ -1061,7 +1061,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { result = hash_any_extended((unsigned char *) keydata, keylen, PG_GETARG_INT64(1)); diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 2086c47e0f..bc79d71d70 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1223,7 +1223,7 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (!mylocale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); @@ -1573,7 +1573,7 @@ varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid) result = pg_strncoll(arg1, len1, arg2, len2, mylocale); /* Break tie if necessary. */ - if (result == 0 && pg_locale_deterministic(mylocale)) + if (result == 0 && mylocale->deterministic) { result = memcmp(arg1, arg2, Min(len1, len2)); if ((result == 0) && (len1 != len2)) @@ -1624,7 +1624,7 @@ texteq(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1679,7 +1679,7 @@ textne(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1792,7 +1792,7 @@ text_starts_with(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (!mylocale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); @@ -2204,7 +2204,7 @@ varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup) result = pg_strcoll(sss->buf1, sss->buf2, sss->locale); /* Break tie if necessary. */ - if (result == 0 && pg_locale_deterministic(sss->locale)) + if (result == 0 && sss->locale->deterministic) result = strcmp(sss->buf1, sss->buf2); /* Cache result, perhaps saving an expensive strcoll() call next time */ @@ -2547,7 +2547,7 @@ btvarstrequalimage(PG_FUNCTION_ARGS) locale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(locale)) + if (locale->deterministic) PG_RETURN_BOOL(true); else PG_RETURN_BOOL(false); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index ab1c37a44b..faae868bfc 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -108,7 +108,6 @@ extern void make_icu_collator(const char *iculocstr, const char *icurules, struct pg_locale_struct *resultp); -extern bool pg_locale_deterministic(pg_locale_t locale); extern void init_database_collation(void); extern pg_locale_t pg_newlocale_from_collation(Oid collid); -- 2.43.0
0006-Slightly-refactor-varstr_sortsupport-to-improve-read.patch (text/x-patch, 2.5K) download From 9b47f1a5190dd8dcd182c9f8b380242bbc510ff9 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:03:17 +0200 Subject: [PATCH 6/6] Slightly refactor varstr_sortsupport() to improve readability --- src/backend/utils/adt/varlena.c | 36 ++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index bc79d71d70..a6a150e90e 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1911,25 +1911,25 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) } else ssup->comparator = varlenafastcmp_locale; - } - /* - * Unfortunately, it seems that abbreviation for non-C collations is - * broken on many common platforms; see pg_strxfrm_enabled(). - * - * Even apart from the risk of broken locales, it's possible that there - * are platforms where the use of abbreviated keys should be disabled at - * compile time. Having only 4 byte datums could make worst-case - * performance drastically more likely, for example. Moreover, macOS's - * strxfrm() implementation is known to not effectively concentrate a - * significant amount of entropy from the original string in earlier - * transformed blobs. It's possible that other supported platforms are - * similarly encumbered. So, if we ever get past disabling this - * categorically, we may still want or need to disable it for particular - * platforms. - */ - if (!collate_c && !pg_strxfrm_enabled(locale)) - abbreviate = false; + /* + * Unfortunately, it seems that abbreviation for non-C collations is + * broken on many common platforms; see pg_strxfrm_enabled(). + * + * Even apart from the risk of broken locales, it's possible that there + * are platforms where the use of abbreviated keys should be disabled at + * compile time. Having only 4 byte datums could make worst-case + * performance drastically more likely, for example. Moreover, macOS's + * strxfrm() implementation is known to not effectively concentrate a + * significant amount of entropy from the original string in earlier + * transformed blobs. It's possible that other supported platforms are + * similarly encumbered. So, if we ever get past disabling this + * categorically, we may still want or need to disable it for particular + * platforms. + */ + if (!pg_strxfrm_enabled(locale)) + abbreviate = false; + } /* * If we're using abbreviated keys, or if we're using a locale-aware -- 2.43.0
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-14 22:55  Jeff Davis <pgsql@j-davis.com>
  parent: Andreas Karlsson <andreas@proxel.se>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-08-14 22:55 UTC (permalink / raw)
  To: Andreas Karlsson <andreas@proxel.se>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On Wed, 2024-08-14 at 01:31 +0200, Andreas Karlsson wrote:
> 0001-Remove-lc_collate_is_c.patch
> 
> Removes lc_collate_is_c().

This could use some performance testing, as the commit message says,
otherwise it looks good.

> 0002-Remove-lc_ctype_is_c.patch
> 
> Removes lc_ctype_is_c() and POSIX_COLLATION_OID which is no longer 
> necessary.

This overlaps a bit with what Peter already proposed here:

https://www.postgresql.org/message-id/4f562d84-87f4-44dc-8946-01d6c437936f%40eisentraut.org

right?

> 0003-Remove-dubious-check-against-default-locale.patch
> 
> This patch removes a check against DEFAULT_COLLATION_OID which I
> thought 
> looked really dubious. Shouldn't this just be a simple check for if
> the 
> locale is deterministic? Since we know we have a valid locale that 
> should be enough, right?

Looks good to me. The code was correct in the sense that the default
collation is always deterministic, but (a) Peter is working on non-
deterministic default collations; and (b) it was a redundant check.

> 0004-Do-not-check-both-for-collate_is_c-and-deterministic.patch
> 
> It is redundant to check both for "collation_is_c && deterministic",
> right?

+1.

> 0005-Remove-pg_collate_deterministic-and-check-field-dire.patch
> 
> Since after my patches we look a lot directly at the collation_is_c
> and 
> ctype_is_c fields I think the thin wrapper around the deterministic 
> field makes it seem like there is more to it so I suggest that we
> should 
> just remove it.

+1. When I added that, there was also a NULL check, but that was
removed so we might as well just read the field.

> 0006-Slightly-refactor-varstr_sortsupport-to-improve-read.patch
> 
> Small refactor to make a hard to read function a bit easier to read.

Looks good to me.

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-08-28 16:43  Andreas Karlsson <andreas@proxel.se>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Andreas Karlsson @ 2024-08-28 16:43 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On 8/15/24 12:55 AM, Jeff Davis wrote:
> This overlaps a bit with what Peter already proposed here:
> 
> https://www.postgresql.org/message-id/4f562d84-87f4-44dc-8946-01d6c437936f%40eisentraut.org
> 
> right?

Maybe I am missing something but not as far as I can see. It is related 
but I do not see any overlap.

>> 0003-Remove-dubious-check-against-default-locale.patch
>>
>> This patch removes a check against DEFAULT_COLLATION_OID which I
>> thought
>> looked really dubious. Shouldn't this just be a simple check for if
>> the
>> locale is deterministic? Since we know we have a valid locale that
>> should be enough, right?
> 
> Looks good to me. The code was correct in the sense that the default
> collation is always deterministic, but (a) Peter is working on non-
> deterministic default collations; and (b) it was a redundant check.

Suspected so.

I have attached a set of rebased patches with an added assert. Maybe I 
should start a new thread though.

Andreas

Attachments: download all (5 files, .tar.gz)

v2-0005-Remove-pg_collate_deterministic-and-check-field-d.patch (text/x-patch, 7.5K) download From 9715b6f429907a9f284284e697727bfc7b4082e7 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:52:53 +0200 Subject: [PATCH v2 5/5] Remove pg_collate_deterministic() and check field directly There is no clear benefit from using this thin accessor function, espeically since other fields like collate_is_c and ctype_is_c are accessed directly. --- src/backend/access/hash/hashfunc.c | 4 ++-- src/backend/regex/regc_pg_locale.c | 2 +- src/backend/utils/adt/like.c | 4 ++-- src/backend/utils/adt/pg_locale.c | 7 ------- src/backend/utils/adt/varchar.c | 8 ++++---- src/backend/utils/adt/varlena.c | 14 +++++++------- src/include/utils/pg_locale.h | 1 - 7 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index 86f1adecb7..01bdf997e3 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -279,7 +279,7 @@ hashtext(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { result = hash_any((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)); @@ -334,7 +334,7 @@ hashtextextended(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { result = hash_any_extended((unsigned char *) VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key), diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 749ae6b4b2..1b8008a03e 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -254,7 +254,7 @@ pg_set_regex_collation(Oid collation) { pg_locale_t locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for regular expressions"))); diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index f87675d755..286b737fd7 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -151,7 +151,7 @@ GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation { pg_locale_t locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for LIKE"))); @@ -188,7 +188,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(locale)) + if (!locale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for ILIKE"))); diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 3e37837b8d..6af78301b7 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1408,13 +1408,6 @@ make_icu_collator(const char *iculocstr, #endif /* not USE_ICU */ } - -bool -pg_locale_deterministic(pg_locale_t locale) -{ - return locale->deterministic; -} - /* * Initialize default_locale with database locale settings. */ diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 03e91c0333..8f86aca297 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -757,7 +757,7 @@ bpchareq(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { /* * Since we only care about equality or not-equality, we can avoid all @@ -798,7 +798,7 @@ bpcharne(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { /* * Since we only care about equality or not-equality, we can avoid all @@ -1005,7 +1005,7 @@ hashbpchar(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { result = hash_any((unsigned char *) keydata, keylen); } @@ -1061,7 +1061,7 @@ hashbpcharextended(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { result = hash_any_extended((unsigned char *) keydata, keylen, PG_GETARG_INT64(1)); diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 3a0f8ab6b5..fe8ac9a756 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1223,7 +1223,7 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (!mylocale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); @@ -1567,7 +1567,7 @@ varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid) result = pg_strncoll(arg1, len1, arg2, len2, mylocale); /* Break tie if necessary. */ - if (result == 0 && pg_locale_deterministic(mylocale)) + if (result == 0 && mylocale->deterministic) { result = memcmp(arg1, arg2, Min(len1, len2)); if ((result == 0) && (len1 != len2)) @@ -1618,7 +1618,7 @@ texteq(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1673,7 +1673,7 @@ textne(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(mylocale)) + if (mylocale->deterministic) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1786,7 +1786,7 @@ text_starts_with(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (!pg_locale_deterministic(mylocale)) + if (!mylocale->deterministic) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for substring searches"))); @@ -2198,7 +2198,7 @@ varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup) result = pg_strcoll(sss->buf1, sss->buf2, sss->locale); /* Break tie if necessary. */ - if (result == 0 && pg_locale_deterministic(sss->locale)) + if (result == 0 && sss->locale->deterministic) result = strcmp(sss->buf1, sss->buf2); /* Cache result, perhaps saving an expensive strcoll() call next time */ @@ -2537,7 +2537,7 @@ btvarstrequalimage(PG_FUNCTION_ARGS) locale = pg_newlocale_from_collation(collid); - if (pg_locale_deterministic(locale)) + if (locale->deterministic) PG_RETURN_BOOL(true); else PG_RETURN_BOOL(false); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index ab1c37a44b..faae868bfc 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -108,7 +108,6 @@ extern void make_icu_collator(const char *iculocstr, const char *icurules, struct pg_locale_struct *resultp); -extern bool pg_locale_deterministic(pg_locale_t locale); extern void init_database_collation(void); extern pg_locale_t pg_newlocale_from_collation(Oid collid); -- 2.43.0
v2-0004-Do-not-check-both-for-collate_is_c-and-determinis.patch (text/x-patch, 1.7K) download From e4742d692fbf8657e6710cd9d54173f890155249 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:49:46 +0200 Subject: [PATCH v2 4/5] Do not check both for collate_is_c and deterministic Since deterimistic will always be true if collate_is_c we can just check for deterimistic and make the code cleaner to read. --- src/backend/utils/adt/varchar.c | 4 ++-- src/backend/utils/adt/varlena.c | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index d2d1c5709d..03e91c0333 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -757,7 +757,7 @@ bpchareq(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (mylocale->collate_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { /* * Since we only care about equality or not-equality, we can avoid all @@ -798,7 +798,7 @@ bpcharne(PG_FUNCTION_ARGS) mylocale = pg_newlocale_from_collation(collid); - if (mylocale->collate_is_c || pg_locale_deterministic(mylocale)) + if (pg_locale_deterministic(mylocale)) { /* * Since we only care about equality or not-equality, we can avoid all diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 306f876e94..3a0f8ab6b5 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -2537,8 +2537,7 @@ btvarstrequalimage(PG_FUNCTION_ARGS) locale = pg_newlocale_from_collation(collid); - if (locale->collate_is_c || - pg_locale_deterministic(locale)) + if (pg_locale_deterministic(locale)) PG_RETURN_BOOL(true); else PG_RETURN_BOOL(false); -- 2.43.0
v2-0003-Remove-dubious-check-against-default-locale.patch (text/x-patch, 744B) download From b79c102dc45ccf55dd4d2383624ae434c6c590e8 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:47:08 +0200 Subject: [PATCH v2 3/5] Remove dubious check against default locale --- src/backend/utils/adt/varlena.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 6b664dfdd8..306f876e94 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -2538,7 +2538,6 @@ btvarstrequalimage(PG_FUNCTION_ARGS) locale = pg_newlocale_from_collation(collid); if (locale->collate_is_c || - collid == DEFAULT_COLLATION_OID || pg_locale_deterministic(locale)) PG_RETURN_BOOL(true); else -- 2.43.0
v2-0002-Remove-lc_ctype_is_c.patch (text/x-patch, 10.0K) download From b8df8e4dd7fcaa20c9beb7710f91c96142d87a71 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:37:48 +0200 Subject: [PATCH v2 2/5] Remove lc_ctype_is_c() Instead always fetch the locale and look at the ctype_is_c field. Since regular expressions are used by pg_hba we need to hardcode the logic for regular expression using C_COLLATE_OID. --- src/backend/regex/regc_pg_locale.c | 21 +++++++++++++++------ src/backend/utils/adt/formatting.c | 27 ++++++++++++--------------- src/backend/utils/adt/like.c | 2 +- src/backend/utils/adt/like_support.c | 15 +++++---------- src/backend/utils/adt/pg_locale.c | 26 -------------------------- src/include/catalog/pg_collation.dat | 3 +-- src/include/utils/pg_locale.h | 2 -- 7 files changed, 34 insertions(+), 62 deletions(-) diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index 947d73f3e0..749ae6b4b2 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -243,41 +243,50 @@ pg_set_regex_collation(Oid collation) errhint("Use the COLLATE clause to set the collation explicitly."))); } - if (lc_ctype_is_c(collation)) + if (collation == C_COLLATION_OID) { - /* C/POSIX collations use this path regardless of database encoding */ + /* Allow using regex in C locale without catalog */ pg_regex_strategy = PG_REGEX_LOCALE_C; pg_regex_locale = 0; pg_regex_collation = C_COLLATION_OID; } else { - pg_regex_locale = pg_newlocale_from_collation(collation); + pg_locale_t locale = pg_newlocale_from_collation(collation); - if (!pg_locale_deterministic(pg_regex_locale)) + if (!pg_locale_deterministic(locale)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("nondeterministic collations are not supported for regular expressions"))); - if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN) + if (locale->ctype_is_c) + { + /* C/POSIX collations use this path regardless of database encoding */ + pg_regex_strategy = PG_REGEX_LOCALE_C; + locale = 0; + collation = C_COLLATION_OID; + } + else if (locale->provider == COLLPROVIDER_BUILTIN) { Assert(GetDatabaseEncoding() == PG_UTF8); pg_regex_strategy = PG_REGEX_BUILTIN; } #ifdef USE_ICU - else if (pg_regex_locale->provider == COLLPROVIDER_ICU) + else if (locale->provider == COLLPROVIDER_ICU) { pg_regex_strategy = PG_REGEX_LOCALE_ICU; } #endif else { + Assert(pg_regex_locale->provider == COLLPROVIDER_LIBC); if (GetDatabaseEncoding() == PG_UTF8) pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L; else pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L; } + pg_regex_locale = locale; pg_regex_collation = collation; } } diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 68069fcfd3..dc5178f56b 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1636,6 +1636,7 @@ char * str_tolower(const char *buff, size_t nbytes, Oid collid) { char *result; + pg_locale_t mylocale; if (!buff) return NULL; @@ -1653,17 +1654,15 @@ str_tolower(const char *buff, size_t nbytes, Oid collid) errhint("Use the COLLATE clause to set the collation explicitly."))); } + mylocale = pg_newlocale_from_collation(collid); + /* C/POSIX collations use this path regardless of database encoding */ - if (lc_ctype_is_c(collid)) + if (mylocale->ctype_is_c) { result = asc_tolower(buff, nbytes); } else { - pg_locale_t mylocale; - - mylocale = pg_newlocale_from_collation(collid); - #ifdef USE_ICU if (mylocale->provider == COLLPROVIDER_ICU) { @@ -1774,6 +1773,7 @@ char * str_toupper(const char *buff, size_t nbytes, Oid collid) { char *result; + pg_locale_t mylocale; if (!buff) return NULL; @@ -1791,17 +1791,15 @@ str_toupper(const char *buff, size_t nbytes, Oid collid) errhint("Use the COLLATE clause to set the collation explicitly."))); } + mylocale = pg_newlocale_from_collation(collid); + /* C/POSIX collations use this path regardless of database encoding */ - if (lc_ctype_is_c(collid)) + if (mylocale->ctype_is_c) { result = asc_toupper(buff, nbytes); } else { - pg_locale_t mylocale; - - mylocale = pg_newlocale_from_collation(collid); - #ifdef USE_ICU if (mylocale->provider == COLLPROVIDER_ICU) { @@ -1954,6 +1952,7 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) { char *result; int wasalnum = false; + pg_locale_t mylocale; if (!buff) return NULL; @@ -1971,17 +1970,15 @@ str_initcap(const char *buff, size_t nbytes, Oid collid) errhint("Use the COLLATE clause to set the collation explicitly."))); } + mylocale = pg_newlocale_from_collation(collid); + /* C/POSIX collations use this path regardless of database encoding */ - if (lc_ctype_is_c(collid)) + if (mylocale->ctype_is_c) { result = asc_initcap(buff, nbytes); } else { - pg_locale_t mylocale; - - mylocale = pg_newlocale_from_collation(collid); - #ifdef USE_ICU if (mylocale->provider == COLLPROVIDER_ICU) { diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 131616fa6b..f87675d755 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -147,7 +147,7 @@ SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) static inline int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation) { - if (collation && !lc_ctype_is_c(collation)) + if (collation) { pg_locale_t locale = pg_newlocale_from_collation(collation); diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c index a579fa0157..fcfd2576b2 100644 --- a/src/backend/utils/adt/like_support.c +++ b/src/backend/utils/adt/like_support.c @@ -100,7 +100,7 @@ static Selectivity regex_selectivity(const char *patt, int pattlen, bool case_insensitive, int fixed_prefix_len); static int pattern_char_isalpha(char c, bool is_multibyte, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation); static Datum string_to_datum(const char *str, Oid datatype); @@ -1000,7 +1000,6 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, match_pos; bool is_multibyte = (pg_database_encoding_max_length() > 1); pg_locale_t locale = 0; - bool locale_is_c = false; /* the right-hand const is type text or bytea */ Assert(typeid == BYTEAOID || typeid == TEXTOID); @@ -1024,11 +1023,7 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, errhint("Use the COLLATE clause to set the collation explicitly."))); } - /* If case-insensitive, we need locale info */ - if (lc_ctype_is_c(collation)) - locale_is_c = true; - else - locale = pg_newlocale_from_collation(collation); + locale = pg_newlocale_from_collation(collation); } if (typeid != BYTEAOID) @@ -1065,7 +1060,7 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, /* Stop if case-varying character (it's sort of a wildcard) */ if (case_insensitive && - pattern_char_isalpha(patt[pos], is_multibyte, locale, locale_is_c)) + pattern_char_isalpha(patt[pos], is_multibyte, locale)) break; match[match_pos++] = patt[pos]; @@ -1499,9 +1494,9 @@ regex_selectivity(const char *patt, int pattlen, bool case_insensitive, */ static int pattern_char_isalpha(char c, bool is_multibyte, - pg_locale_t locale, bool locale_is_c) + pg_locale_t locale) { - if (locale_is_c) + if (locale->ctype_is_c) return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); else if (is_multibyte && IS_HIGHBIT_SET(c)) return true; diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 4438b3817a..3e37837b8d 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1259,32 +1259,6 @@ lookup_collation_cache(Oid collation) return cache_entry; } -/* - * Detect whether collation's LC_CTYPE property is C - */ -bool -lc_ctype_is_c(Oid collation) -{ - /* - * If we're asked about "collation 0", return false, so that the code will - * go into the non-C path and report that the collation is bogus. - */ - if (!OidIsValid(collation)) - return false; - - /* - * If we're asked about the built-in C/POSIX collations, we know that. - */ - if (collation == C_COLLATION_OID || - collation == POSIX_COLLATION_OID) - return true; - - /* - * Otherwise, we have to consult pg_collation, but we cache that. - */ - return pg_newlocale_from_collation(collation)->ctype_is_c; -} - /* simple subroutine for reporting errors from newlocale() */ static void report_newlocale_failure(const char *localename) diff --git a/src/include/catalog/pg_collation.dat b/src/include/catalog/pg_collation.dat index f126201276..af5c9aa582 100644 --- a/src/include/catalog/pg_collation.dat +++ b/src/include/catalog/pg_collation.dat @@ -19,8 +19,7 @@ descr => 'standard C collation', collname => 'C', collprovider => 'c', collencoding => '-1', collcollate => 'C', collctype => 'C' }, -{ oid => '951', oid_symbol => 'POSIX_COLLATION_OID', - descr => 'standard POSIX collation', +{ oid => '951', descr => 'standard POSIX collation', collname => 'POSIX', collprovider => 'c', collencoding => '-1', collcollate => 'POSIX', collctype => 'POSIX' }, { oid => '962', descr => 'sorts by Unicode code point, C character semantics', diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index 8ec24437f4..ab1c37a44b 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -54,8 +54,6 @@ extern PGDLLIMPORT bool database_ctype_is_c; extern bool check_locale(int category, const char *locale, char **canonname); extern char *pg_perm_setlocale(int category, const char *locale); -extern bool lc_ctype_is_c(Oid collation); - /* * Return the POSIX lconv struct (contains number/money formatting * information) with locale information for all categories. -- 2.43.0
v2-0001-Remove-lc_collate_is_c.patch (text/x-patch, 9.5K) download From 849ab262a27017f5bbe1472b8f9888fbcab068f2 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson <andreas@proxel.se> Date: Wed, 14 Aug 2024 00:37:23 +0200 Subject: [PATCH v2 1/5] Remove lc_collate_is_c() Instead always look up the collation and check to collate_is_c field. TODO: Check for performance regression? --- src/backend/access/spgist/spgtextproc.c | 2 +- src/backend/commands/collationcmds.c | 8 ++----- src/backend/utils/adt/like_support.c | 4 ++-- src/backend/utils/adt/pg_locale.c | 27 --------------------- src/backend/utils/adt/selfuncs.c | 6 +++-- src/backend/utils/adt/varchar.c | 20 +++++----------- src/backend/utils/adt/varlena.c | 32 ++++++++++++------------- src/include/utils/pg_locale.h | 1 - 8 files changed, 30 insertions(+), 70 deletions(-) diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c index 3f08d330b6..d5237a68b5 100644 --- a/src/backend/access/spgist/spgtextproc.c +++ b/src/backend/access/spgist/spgtextproc.c @@ -427,7 +427,7 @@ spg_text_inner_consistent(PG_FUNCTION_ARGS) { spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0); spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1); - bool collate_is_c = lc_collate_is_c(PG_GET_COLLATION()); + bool collate_is_c = pg_newlocale_from_collation(PG_GET_COLLATION())->collate_is_c; text *reconstructedValue; text *reconstrText; int maxReconstrLen; diff --git a/src/backend/commands/collationcmds.c b/src/backend/commands/collationcmds.c index 63ef9a0841..53b6a479aa 100644 --- a/src/backend/commands/collationcmds.c +++ b/src/backend/commands/collationcmds.c @@ -377,13 +377,9 @@ DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_e if (!OidIsValid(newoid)) return InvalidObjectAddress; - /* - * Check that the locales can be loaded. NB: pg_newlocale_from_collation - * is only supposed to be called on non-C-equivalent locales. - */ + /* Check that the locales can be loaded. */ CommandCounterIncrement(); - if (!lc_collate_is_c(newoid) || !lc_ctype_is_c(newoid)) - (void) pg_newlocale_from_collation(newoid); + (void) pg_newlocale_from_collation(newoid); ObjectAddressSet(address, CollationRelationId, newoid); diff --git a/src/backend/utils/adt/like_support.c b/src/backend/utils/adt/like_support.c index 2635050861..a579fa0157 100644 --- a/src/backend/utils/adt/like_support.c +++ b/src/backend/utils/adt/like_support.c @@ -433,7 +433,7 @@ match_pattern_prefix(Node *leftop, * collation. */ if (collation_aware && - !lc_collate_is_c(indexcollation)) + !pg_newlocale_from_collation(indexcollation)->collate_is_c) return NIL; /* @@ -1603,7 +1603,7 @@ make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation) else workstr = TextDatumGetCString(str_const->constvalue); len = strlen(workstr); - if (lc_collate_is_c(collation) || len == 0) + if (pg_newlocale_from_collation(collation)->collate_is_c || len == 0) cmpstr = str_const->constvalue; else { diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 643cca05d3..4438b3817a 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1259,33 +1259,6 @@ lookup_collation_cache(Oid collation) return cache_entry; } - -/* - * Detect whether collation's LC_COLLATE property is C - */ -bool -lc_collate_is_c(Oid collation) -{ - /* - * If we're asked about "collation 0", return false, so that the code will - * go into the non-C path and report that the collation is bogus. - */ - if (!OidIsValid(collation)) - return false; - - /* - * If we're asked about the built-in C/POSIX collations, we know that. - */ - if (collation == C_COLLATION_OID || - collation == POSIX_COLLATION_OID) - return true; - - /* - * Otherwise, we have to consult pg_collation, but we cache that. - */ - return pg_newlocale_from_collation(collation)->collate_is_c; -} - /* * Detect whether collation's LC_CTYPE property is C */ diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index bf42393bec..03d7fb5f48 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -4646,6 +4646,7 @@ static char * convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure) { char *val; + pg_locale_t mylocale; switch (typid) { @@ -4671,9 +4672,10 @@ convert_string_datum(Datum value, Oid typid, Oid collid, bool *failure) return NULL; } - if (!lc_collate_is_c(collid)) + mylocale = pg_newlocale_from_collation(collid); + + if (!mylocale->collate_is_c) { - pg_locale_t mylocale = pg_newlocale_from_collation(collid); char *xfrmstr; size_t xfrmlen; size_t xfrmlen2 PG_USED_FOR_ASSERTS_ONLY; diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 0c219dcc77..d2d1c5709d 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -748,20 +748,16 @@ bpchareq(PG_FUNCTION_ARGS) len2; bool result; Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; check_collation_set(collid); len1 = bcTruelen(arg1); len2 = bcTruelen(arg2); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (mylocale->collate_is_c || pg_locale_deterministic(mylocale)) { /* * Since we only care about equality or not-equality, we can avoid all @@ -793,20 +789,16 @@ bpcharne(PG_FUNCTION_ARGS) len2; bool result; Oid collid = PG_GET_COLLATION(); - bool locale_is_c = false; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; check_collation_set(collid); len1 = bcTruelen(arg1); len2 = bcTruelen(arg2); - if (lc_collate_is_c(collid)) - locale_is_c = true; - else - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); - if (locale_is_c || pg_locale_deterministic(mylocale)) + if (mylocale->collate_is_c || pg_locale_deterministic(mylocale)) { /* * Since we only care about equality or not-equality, we can avoid all diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 7c6391a276..6b664dfdd8 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1538,10 +1538,13 @@ int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid) { int result; + pg_locale_t mylocale; check_collation_set(collid); - if (lc_collate_is_c(collid)) + mylocale = pg_newlocale_from_collation(collid); + + if (mylocale->collate_is_c) { result = memcmp(arg1, arg2, Min(len1, len2)); if ((result == 0) && (len1 != len2)) @@ -1549,10 +1552,6 @@ varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid) } else { - pg_locale_t mylocale; - - mylocale = pg_newlocale_from_collation(collid); - /* * memcmp() can't tell us which of two unequal strings sorts first, * but it's a cheap way to tell if they're equal. Testing shows that @@ -1859,10 +1858,12 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) bool abbreviate = ssup->abbreviate; bool collate_c = false; VarStringSortSupport *sss; - pg_locale_t locale = 0; + pg_locale_t locale; check_collation_set(collid); + locale = pg_newlocale_from_collation(collid); + /* * If possible, set ssup->comparator to a function which can be used to * directly compare two datums. If we can do this, we'll avoid the @@ -1876,7 +1877,7 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) * varstrfastcmp_c, bpcharfastcmp_c, or namefastcmp_c, all of which use * memcmp() rather than strcoll(). */ - if (lc_collate_is_c(collid)) + if (locale->collate_is_c) { if (typid == BPCHAROID) ssup->comparator = bpcharfastcmp_c; @@ -1893,13 +1894,6 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) } else { - /* - * We need a collation-sensitive comparison. To make things faster, - * we'll figure out the collation based on the locale id and cache the - * result. - */ - locale = pg_newlocale_from_collation(collid); - /* * We use varlenafastcmp_locale except for type NAME. */ @@ -1950,7 +1944,8 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) sss->last_len2 = -1; /* Initialize */ sss->last_returned = 0; - sss->locale = locale; + if (!collate_c) + sss->locale = locale; /* * To avoid somehow confusing a strxfrm() blob and an original string, @@ -2536,12 +2531,15 @@ btvarstrequalimage(PG_FUNCTION_ARGS) { /* Oid opcintype = PG_GETARG_OID(0); */ Oid collid = PG_GET_COLLATION(); + pg_locale_t locale; check_collation_set(collid); - if (lc_collate_is_c(collid) || + locale = pg_newlocale_from_collation(collid); + + if (locale->collate_is_c || collid == DEFAULT_COLLATION_OID || - get_collation_isdeterministic(collid)) + pg_locale_deterministic(locale)) PG_RETURN_BOOL(true); else PG_RETURN_BOOL(false); diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index f41d33975b..8ec24437f4 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -54,7 +54,6 @@ extern PGDLLIMPORT bool database_ctype_is_c; extern bool check_locale(int category, const char *locale, char **canonname); extern char *pg_perm_setlocale(int category, const char *locale); -extern bool lc_collate_is_c(Oid collation); extern bool lc_ctype_is_c(Oid collation); /* -- 2.43.0
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-09-04 05:04  Jeff Davis <pgsql@j-davis.com>
  parent: Andreas Karlsson <andreas@proxel.se>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-09-04 05:04 UTC (permalink / raw)
  To: Andreas Karlsson <andreas@proxel.se>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On Wed, 2024-08-28 at 18:43 +0200, Andreas Karlsson wrote:
> On 8/15/24 12:55 AM, Jeff Davis wrote:
> > This overlaps a bit with what Peter already proposed here:
> > 
> > https://www.postgresql.org/message-id/4f562d84-87f4-44dc-8946-01d6c437936f%40eisentraut.org
> > 
> > right?
> 
> Maybe I am missing something but not as far as I can see. It is
> related 
> but I do not see any overlap.

v2-0002:

Oh, I see, pattern_char_isalpha() only has one caller, and it never
passes a NULL for pg_locale_t, so my complaint doesn't affect your
patch. This is about ready, then.

> > > 0003-Remove-dubious-check-against-default-locale.patch
> > > 
> > > This patch removes a check against DEFAULT_COLLATION_OID which I
> > > thought
> > > looked really dubious. Shouldn't this just be a simple check for
> > > if
> > > the
> > > locale is deterministic? Since we know we have a valid locale
> > > that
> > > should be enough, right?
> > 
> > Looks good to me. The code was correct in the sense that the
> > default
> > collation is always deterministic, but (a) Peter is working on non-
> > deterministic default collations; and (b) it was a redundant check.
> 
> Suspected so.
> 
> I have attached a set of rebased patches with an added assert. Maybe
> I 
> should start a new thread though.

v2-0001:

* There was a performance regression for repeated lookups, such as a "t
< 'xyz'" predicate. I committed 12d3345c0d (to remember the last
collation lookup), which will prevent that regression.

* This patch may change the handling of collation oid 0, and I'm not
sure whether that was intentional or not. lc_collate_is_c(0) returned
false, whereas pg_newlocale_from_collation(0)->collate_is_c raised
Assert or threw an cache lookup error. I'm not sure if this is an
actual problem, but looking at the callers, they should be more
defensive if they expect a collation oid of 0 to work at all.

* The comment in lc_collate_is_c() said that it returned false with the
idea that the caller would throw a useful error, and I think that
comment has been wrong for a while. If it returns false, the caller is
expected to call pg_newlocale_from_collation(), which would Assert on a
debug build. Should we remove that Assert, too, so that it will
consistently throw a cache lookup failure?

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-09-04 21:45  Jeff Davis <pgsql@j-davis.com>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 2 replies; 33+ messages in thread

From: Jeff Davis @ 2024-09-04 21:45 UTC (permalink / raw)
  To: Andreas Karlsson <andreas@proxel.se>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

Committed v2-0001.

On Tue, 2024-09-03 at 22:04 -0700, Jeff Davis wrote:

> * This patch may change the handling of collation oid 0, and I'm not
> sure whether that was intentional or not. lc_collate_is_c(0) returned
> false, whereas pg_newlocale_from_collation(0)->collate_is_c raised
> Assert or threw an cache lookup error. I'm not sure if this is an
> actual problem, but looking at the callers, they should be more
> defensive if they expect a collation oid of 0 to work at all.

For functions that do call pg_newlocale_from_collation() when
lc_collation_is_c() returns false, the behavior is unchanged.

There are only 3 callers which don't follow that pattern:

  * spg_text_inner_consistent: gets collation ID from the index, and
text is a collatable type so it will be valid

  * match_pattern_prefix: same

  * make_greater_string: I changed the order of the tests in
make_greater_string so that if len=0 and collation=0, it won't throw an
error. If len !=0, it goes into varstr_cmp(), which will call
pg_newlocale_from_collation().

> * The comment in lc_collate_is_c() said that it returned false with
> the
> idea that the caller would throw a useful error, and I think that
> comment has been wrong for a while. If it returns false, the caller
> is
> expected to call pg_newlocale_from_collation(), which would Assert on
> a
> debug build. Should we remove that Assert, too, so that it will
> consistently throw a cache lookup failure?

I fixed this by replacing the assert with an elog(ERROR, ...), so that
it will consistently show a "cache lookup failed for collation 0"
regardless of whether it's a debug build or not. It's not expected that
the error will be encountered.

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-09-09 13:37  Peter Eisentraut <peter@eisentraut.org>
  parent: Jeff Davis <pgsql@j-davis.com>
  1 sibling, 1 reply; 33+ messages in thread

From: Peter Eisentraut @ 2024-09-09 13:37 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On 08.08.24 22:00, Jeff Davis wrote:
> On Wed, 2024-08-07 at 22:44 +0200, Peter Eisentraut wrote:
>> But after this patch set, locale cannot be NULL anymore, so the third
>> branch is obsolete.
> 
> ...
> 
>> Second, there are a number of functions in like.c like the above that
>> take separate arguments like pg_locale_t locale, bool locale_is_c.
>> Because pg_locale_t now contains the locale_is_c information, these
>> can
>> be combined.
> 
> I believe these patches are correct, but the reasoning is fairly
> complex:
> 
> 1. Some MatchText variants are called with 0 for locale. But that's OK
> because ...
> 
> 2. A MatchText variant only cares about the locale if MATCH_LOWER(t) is
> defined, and ...
> 
> 3. Only one variant, SB_IMatchText() defines MATCH_LOWER(), and ...
> 
> 4. SB_IMatchText() is called with a non-zero locale.
> 
> All of these are a bit confusing to follow because it's generated code.
> #2 is particularly non-obvious, because "locale" is not even an
> argument of the MATCH_LOWER(t) or GETCHAR(t) macros, it's taken
> implicitly from the outer scope.
> 
> I don't think your patches cause this confusion, but is there a way you
> can clarify some of this along the way?

In the end, I figured the best thing to do here is to add an explicit 
locale argument to MATCH_LOWER() and GETCHAR() so one can actually 
understand this code by reading it.  New patch attached.

Attachments:

v2-0001-Remove-separate-locale_is_c-arguments.patch (text/plain, 6.5K) download From 53c5dd60ca8a7d9500624a430c7776bfa345a396 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <peter@eisentraut.org> Date: Mon, 9 Sep 2024 15:33:25 +0200 Subject: [PATCH v2] Remove separate locale_is_c arguments Since e9931bfb751, ctype_is_c is part of pg_locale_t. Some functions passed a pg_locale_t and a bool argument separately. This can now be combined into one argument. Since some callers call MatchText() with locale 0, it is a bit confusing whether this is all correct. But it is the case that only callers that pass a non-zero locale object to MatchText() end up checking locale->ctype_is_c. To make that flow a bit more understandable, add the locale argument to MATCH_LOWER() and GETCHAR() in like_match.c, instead of implicitly taking it from the outer scope. --- src/backend/utils/adt/like.c | 30 +++++++++++++++--------------- src/backend/utils/adt/like_match.c | 20 +++++++++----------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index f87675d7557..9fb036ad014 100644 --- a/src/backend/utils/adt/like.c +++ b/src/backend/utils/adt/like.c @@ -33,18 +33,18 @@ static int SB_MatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static text *SB_do_like_escape(text *pat, text *esc); static int MB_MatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static text *MB_do_like_escape(text *pat, text *esc); static int UTF8_MatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static int SB_IMatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c); + pg_locale_t locale); static int GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation); static int Generic_Text_IC_like(text *str, text *pat, Oid collation); @@ -91,9 +91,9 @@ wchareq(const char *p1, const char *p2) * fold-on-the-fly processing, however. */ static char -SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) +SB_lower_char(unsigned char c, pg_locale_t locale) { - if (locale_is_c) + if (locale->ctype_is_c) return pg_ascii_tolower(c); else return tolower_l(c, locale->info.lt); @@ -129,7 +129,7 @@ SB_lower_char(unsigned char c, pg_locale_t locale, bool locale_is_c) #include "like_match.c" /* setup to compile like_match.c for single byte case insensitive matches */ -#define MATCH_LOWER(t) SB_lower_char((unsigned char) (t), locale, locale_is_c) +#define MATCH_LOWER(t, locale) SB_lower_char((unsigned char) (t), locale) #define NextChar(p, plen) NextByte((p), (plen)) #define MatchText SB_IMatchText @@ -158,11 +158,11 @@ GenericMatchText(const char *s, int slen, const char *p, int plen, Oid collation } if (pg_database_encoding_max_length() == 1) - return SB_MatchText(s, slen, p, plen, 0, true); + return SB_MatchText(s, slen, p, plen, 0); else if (GetDatabaseEncoding() == PG_UTF8) - return UTF8_MatchText(s, slen, p, plen, 0, true); + return UTF8_MatchText(s, slen, p, plen, 0); else - return MB_MatchText(s, slen, p, plen, 0, true); + return MB_MatchText(s, slen, p, plen, 0); } static inline int @@ -212,9 +212,9 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) s = VARDATA_ANY(str); slen = VARSIZE_ANY_EXHDR(str); if (GetDatabaseEncoding() == PG_UTF8) - return UTF8_MatchText(s, slen, p, plen, 0, true); + return UTF8_MatchText(s, slen, p, plen, 0); else - return MB_MatchText(s, slen, p, plen, 0, true); + return MB_MatchText(s, slen, p, plen, 0); } else { @@ -222,7 +222,7 @@ Generic_Text_IC_like(text *str, text *pat, Oid collation) plen = VARSIZE_ANY_EXHDR(pat); s = VARDATA_ANY(str); slen = VARSIZE_ANY_EXHDR(str); - return SB_IMatchText(s, slen, p, plen, locale, locale->ctype_is_c); + return SB_IMatchText(s, slen, p, plen, locale); } } @@ -330,7 +330,7 @@ bytealike(PG_FUNCTION_ARGS) p = VARDATA_ANY(pat); plen = VARSIZE_ANY_EXHDR(pat); - result = (SB_MatchText(s, slen, p, plen, 0, true) == LIKE_TRUE); + result = (SB_MatchText(s, slen, p, plen, 0) == LIKE_TRUE); PG_RETURN_BOOL(result); } @@ -351,7 +351,7 @@ byteanlike(PG_FUNCTION_ARGS) p = VARDATA_ANY(pat); plen = VARSIZE_ANY_EXHDR(pat); - result = (SB_MatchText(s, slen, p, plen, 0, true) != LIKE_TRUE); + result = (SB_MatchText(s, slen, p, plen, 0) != LIKE_TRUE); PG_RETURN_BOOL(result); } diff --git a/src/backend/utils/adt/like_match.c b/src/backend/utils/adt/like_match.c index f2990edff7e..f561cc15e4c 100644 --- a/src/backend/utils/adt/like_match.c +++ b/src/backend/utils/adt/like_match.c @@ -71,14 +71,13 @@ */ #ifdef MATCH_LOWER -#define GETCHAR(t) MATCH_LOWER(t) +#define GETCHAR(t, locale) MATCH_LOWER(t, locale) #else -#define GETCHAR(t) (t) +#define GETCHAR(t, locale) (t) #endif static int -MatchText(const char *t, int tlen, const char *p, int plen, - pg_locale_t locale, bool locale_is_c) +MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale) { /* Fast path for match-everything pattern */ if (plen == 1 && *p == '%') @@ -106,7 +105,7 @@ MatchText(const char *t, int tlen, const char *p, int plen, ereport(ERROR, (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE), errmsg("LIKE pattern must not end with escape character"))); - if (GETCHAR(*p) != GETCHAR(*t)) + if (GETCHAR(*p, locale) != GETCHAR(*t, locale)) return LIKE_FALSE; } else if (*p == '%') @@ -166,17 +165,16 @@ MatchText(const char *t, int tlen, const char *p, int plen, ereport(ERROR, (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE), errmsg("LIKE pattern must not end with escape character"))); - firstpat = GETCHAR(p[1]); + firstpat = GETCHAR(p[1], locale); } else - firstpat = GETCHAR(*p); + firstpat = GETCHAR(*p, locale); while (tlen > 0) { - if (GETCHAR(*t) == firstpat) + if (GETCHAR(*t, locale) == firstpat) { - int matched = MatchText(t, tlen, p, plen, - locale, locale_is_c); + int matched = MatchText(t, tlen, p, plen, locale); if (matched != LIKE_FALSE) return matched; /* TRUE or ABORT */ @@ -198,7 +196,7 @@ MatchText(const char *t, int tlen, const char *p, int plen, NextByte(p, plen); continue; } - else if (GETCHAR(*p) != GETCHAR(*t)) + else if (GETCHAR(*p, locale) != GETCHAR(*t, locale)) { /* non-wildcard pattern char fails to match text char */ return LIKE_FALSE; -- 2.46.0
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-09-12 20:01  Jeff Davis <pgsql@j-davis.com>
  parent: Peter Eisentraut <peter@eisentraut.org>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2024-09-12 20:01 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On Mon, 2024-09-09 at 15:37 +0200, Peter Eisentraut wrote:
> In the end, I figured the best thing to do here is to add an explicit
> locale argument to MATCH_LOWER() and GETCHAR() so one can actually 
> understand this code by reading it.  New patch attached.

Looks good to me, thank you.

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-09-12 21:09  Andreas Karlsson <andreas@proxel.se>
  parent: Jeff Davis <pgsql@j-davis.com>
  1 sibling, 0 replies; 33+ messages in thread

From: Andreas Karlsson @ 2024-09-12 21:09 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On 9/4/24 11:45 PM, Jeff Davis wrote:
> Committed v2-0001.
>
 > [...]
>
> I fixed this by replacing the assert with an elog(ERROR, ...), so that
> it will consistently show a "cache lookup failed for collation 0"
> regardless of whether it's a debug build or not. It's not expected that
> the error will be encountered.

Thanks!

Andreas






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2024-09-13 14:39  Peter Eisentraut <peter@eisentraut.org>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 0 replies; 33+ messages in thread

From: Peter Eisentraut @ 2024-09-13 14:39 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; Andreas Karlsson <andreas@proxel.se>; pgsql-hackers

On 12.09.24 22:01, Jeff Davis wrote:
> On Mon, 2024-09-09 at 15:37 +0200, Peter Eisentraut wrote:
>> In the end, I figured the best thing to do here is to add an explicit
>> locale argument to MATCH_LOWER() and GETCHAR() so one can actually
>> understand this code by reading it.  New patch attached.
> 
> Looks good to me, thank you.

committed, thanks






^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2025-09-25 09:31  Alexander Kukushkin <cyberdemn@gmail.com>
  parent: Jeff Davis <pgsql@j-davis.com>
  1 sibling, 1 reply; 33+ messages in thread

From: Alexander Kukushkin @ 2025-09-25 09:31 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; +Cc: Andreas Karlsson <andreas@proxel.se>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

Hi Jeff,

On Thu, 25 Sept 2025 at 11:08, Jeff Davis <pgsql@j-davis.com> wrote:

> Committed v2-0001.
>

06421b084364 commit broke possibility to call GetSharedSecurityLabel()
from ClientAuthentication_hook.
Now GetSharedSecurityLabel() calls fail with the error "cannot read
pg_class without having selected a database".

Here is gdb stack trace:
#0  ScanPgRelation (targetRelId=3456, indexOK=true,
force_non_historic=false) at relcache.c:354
#1  0x0000565eec153a1e in RelationBuildDesc (targetRelId=3456,
insertIt=<optimized out>) at relcache.c:1113
#2  0x0000565eec155a7d in RelationIdGetRelation (relationId=<optimized
out>, relationId@entry=3456) at relcache.c:2142
#3  0x0000565eebcbafb7 in relation_open (relationId=3456,
lockmode=lockmode@entry=1) at relation.c:58
#4  0x0000565eebd2ea69 in table_open (relationId=<optimized out>,
lockmode=lockmode@entry=1) at table.c:44
#5  0x0000565eec1434e4 in CatalogCacheInitializeCache
(cache=0x565f17d35d80) at catcache.c:1124
#6  0x0000565eec144610 in ConditionalCatalogCacheInitializeCache
(cache=0x565f17d35d80) at catcache.c:1084
#7  SearchCatCacheInternal (cache=0x565f17d35d80, nkeys=nkeys@entry=1,
v1=v1@entry=950, v2=v2@entry=0, v3=v3@entry=0, v4=v4@entry=0) at
catcache.c:1410
#8  0x0000565eec144f65 in SearchCatCache1 (cache=<optimized out>,
v1=v1@entry=950) at catcache.c:1360
#9  0x0000565eec158033 in SearchSysCache1 (cacheId=cacheId@entry=16,
key1=key1@entry=950) at syscache.c:228
#10 0x0000565eec0d72e8 in create_pg_locale (context=0x565f17cd0b00,
collid=950) at ../../../../src/include/postgres.h:259
#11 pg_newlocale_from_collation (collid=950) at pg_locale.c:1226
#12 0x0000565eec1333de in varstr_cmp (arg1=0x770ce64fdf61 "<redacted>",
len1=13, arg2=0x565f17ccaacc "<redacted>\252\314\027_V", len2=13,
collid=<optimized out>)
    at varlena.c:1617
#13 0x0000565eec133c74 in bttextcmp (fcinfo=0x7ffd34181740) at
varlena.c:1892
#14 0x0000565eec165ab6 in FunctionCall2Coll
(flinfo=flinfo@entry=0x7ffd34182358,
collation=<optimized out>, arg1=<optimized out>, arg2=<optimized out>) at
fmgr.c:1161
#15 0x0000565eebd13bfe in _bt_compare (rel=<optimized out>,
key=0x7ffd341822a0, page=<optimized out>, offnum=<optimized out>) at
nbtsearch.c:770
#16 0x0000565eebd13eb2 in _bt_binsrch (rel=rel@entry=0x770ce1cdd0c0,
key=key@entry=0x7ffd341822a0, buf=<optimized out>) at nbtsearch.c:402
#17 0x0000565eebd14bab in _bt_first (scan=scan@entry=0x565f17ccaaf0,
dir=dir@entry=ForwardScanDirection) at nbtsearch.c:1551
#18 0x0000565eebd10e46 in btgettuple (scan=0x565f17ccaaf0,
dir=ForwardScanDirection) at nbtree.c:245
#19 0x0000565eebd063c9 in index_getnext_tid (scan=0x565f17ccaaf0,
direction=<optimized out>) at indexam.c:637
#20 0x0000565eebd0651d in index_getnext_slot (scan=0x565f17ccaaf0,
direction=direction@entry=ForwardScanDirection, slot=0x565f17cca680) at
indexam.c:729
#21 0x0000565eebd05627 in systable_getnext
(sysscan=sysscan@entry=0x565f17cca638)
at genam.c:520
#22 0x0000565eebe14c65 in GetSharedSecurityLabel (provider=0x770d6bc2a7dd
"<redacted>", object=0x7ffd34182e14) at seclabel.c:252
#23 0x0000770d6bc1fb0b in custom_check_auth (port=0x565f17b50788,
am_walsender=<optimized out>) at custom_auth.c:2414
#24 0x0000565eebea37fc in ClientAuthentication (port=port@entry=0x565f17b50788)
at auth.c:750
#25 0x0000565eec16cc95 in PerformAuthentication (port=0x565f17b50788) at
postinit.c:265
#26 InitPostgres (in_dbname=in_dbname@entry=0x565f17c9a930 "postgres",
dboid=dboid@entry=0, username=username@entry=0x565f17b51468 "<redacted>",
    useroid=useroid@entry=0, flags=1, out_dbname=out_dbname@entry=0x0) at
postinit.c:923
#27 0x0000565eec01b2d0 in PostgresMain (dbname=0x565f17c9a930 "postgres",
username=0x565f17b51468 "<redacted>") at postgres.c:4289
#28 0x0000565eec0174ef in BackendMain (startup_data=<optimized out>,
startup_data_len=<optimized out>) at backend_startup.c:124
#29 0x0000565eebf80152 in postmaster_child_launch (child_type=B_BACKEND,
child_slot=1, startup_data=startup_data@entry=0x7ffd34184300,
    startup_data_len=startup_data_len@entry=24,
client_sock=client_sock@entry=0x7ffd34184320) at launch_backend.c:290
#30 0x0000565eebf83ab7 in BackendStartup (client_sock=0x7ffd34184320) at
postmaster.c:3587
#31 ServerLoop () at postmaster.c:1702
#32 0x0000565eebf853e1 in PostmasterMain (argc=argc@entry=1,
argv=argv@entry=0x565f17b4f450)
at postmaster.c:1400
#33 0x0000565eebcab360 in main (argc=1, argv=0x565f17b4f450) at main.c:227

Line numbers could be a bit off, but I think it is not very important.

pg_shseclabel.provider data type is TEXT with collation C.
When doing btree lookup by (objoid, classoid, provider) we need to take
into account collation.
varstr_cmp() was used to make an exception for C collation, and now it has
to do a cache lookup for pg_collation to determine that this is a C
collation.
However, caches are not yet initialized at the moment
when ClientAuthentication_hook is executed, and they can't be, because
database access is not yet allowed.

Or, in other words, this commit made shared pg_catalog relations depend on
non shared relations.

Regards,
--
Alexander Kukushkin

^ permalink  raw  reply  [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2025-10-24 02:37  Jeff Davis <pgsql@j-davis.com>
  parent: Alexander Kukushkin <cyberdemn@gmail.com>
  0 siblings, 1 reply; 33+ messages in thread

From: Jeff Davis @ 2025-10-24 02:37 UTC (permalink / raw)
  To: Alexander Kukushkin <cyberdemn@gmail.com>; +Cc: Andreas Karlsson <andreas@proxel.se>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

On Thu, 2025-09-25 at 11:31 +0200, Alexander Kukushkin wrote:
> Hi Jeff,
> 
> On Thu, 25 Sept 2025 at 11:08, Jeff Davis <pgsql@j-davis.com> wrote:
> > Committed v2-0001.
> > 
> 
> 
> 06421b084364 commit broke possibility to
> call GetSharedSecurityLabel() from ClientAuthentication_hook.
> Now GetSharedSecurityLabel() calls fail with the error "cannot read
> pg_class without having selected a database".

...

> pg_shseclabel.provider data type is TEXT with collation C.
> When doing btree lookup by (objoid, classoid, provider) we need to
> take into account collation.
> varstr_cmp() was used to make an exception for C collation, and now
> it has to do a cache lookup for pg_collation to determine that this
> is a C collation.
> However, caches are not yet initialized at the moment
> when ClientAuthentication_hook is executed, and they can't be,
> because database access is not yet allowed.
> 
> Or, in other words, this commit made shared pg_catalog relations
> depend on non shared relations.

Thank you, patch attached, intended for backport to 18.

Are you aware of any way to encounter this bug without using an
extension?

Regards,
	Jeff Davis

Attachments:

v1-0001-Special-case-C_COLLATION_OID-in-pg_newlocale_from.patch (text/x-patch, 2.9K) download From b6dd158d4a2a5a070634e56142c4b1ad8b9b1859 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Thu, 23 Oct 2025 19:24:14 -0700 Subject: [PATCH v1] Special case C_COLLATION_OID in pg_newlocale_from_collation(). Discussion: https://postgr.es/m/CAFh8B=nj966ECv5vi_u3RYij12v0j-7NPZCXLYzNwOQp9AcPWQ@mail.gmail.com Backpatch-through: 18 --- src/backend/regex/regc_pg_locale.c | 36 +++++------------------------- src/backend/utils/adt/pg_locale.c | 13 +++++++++++ 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/backend/regex/regc_pg_locale.c b/src/backend/regex/regc_pg_locale.c index e0c892db713..4698f110a0c 100644 --- a/src/backend/regex/regc_pg_locale.c +++ b/src/backend/regex/regc_pg_locale.c @@ -23,11 +23,6 @@ static pg_locale_t pg_regex_locale; -static struct pg_locale_struct dummy_c_locale = { - .collate_is_c = true, - .ctype_is_c = true, -}; - /* * pg_set_regex_collation: set collation for these functions to obey @@ -53,33 +48,12 @@ pg_set_regex_collation(Oid collation) errhint("Use the COLLATE clause to set the collation explicitly."))); } - if (collation == C_COLLATION_OID) - { - /* - * Some callers expect regexes to work for C_COLLATION_OID before - * catalog access is available, so we can't call - * pg_newlocale_from_collation(). - */ - locale = &dummy_c_locale; - } - else - { - locale = pg_newlocale_from_collation(collation); - - if (!locale->deterministic) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("nondeterministic collations are not supported for regular expressions"))); + locale = pg_newlocale_from_collation(collation); - if (locale->ctype_is_c) - { - /* - * C/POSIX collations use this path regardless of database - * encoding - */ - locale = &dummy_c_locale; - } - } + if (!locale->deterministic) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("nondeterministic collations are not supported for regular expressions"))); pg_regex_locale = locale; } diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 67299c55ed8..b14c7837938 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -106,6 +106,12 @@ static pg_locale_t default_locale = NULL; static bool CurrentLocaleConvValid = false; static bool CurrentLCTimeValid = false; +static struct pg_locale_struct c_locale = { + .deterministic = true, + .collate_is_c = true, + .ctype_is_c = true, +}; + /* Cache for collation-related knowledge */ typedef struct @@ -1185,6 +1191,13 @@ pg_newlocale_from_collation(Oid collid) if (collid == DEFAULT_COLLATION_OID) return default_locale; + /* + * Some callers expect C_COLLATION_OID to succeed even without catalog + * access. + */ + if (collid == C_COLLATION_OID) + return &c_locale; + if (!OidIsValid(collid)) elog(ERROR, "cache lookup failed for collation %u", collid); -- 2.43.0
^ permalink raw reply [nested|flat] 33+ messages in thread

* Re: tiny step toward threading: reduce dependence on setlocale()
@ 2025-10-28 09:24  Alexander Kukushkin <cyberdemn@gmail.com>
  parent: Jeff Davis <pgsql@j-davis.com>
  0 siblings, 0 replies; 33+ messages in thread

From: Alexander Kukushkin @ 2025-10-28 09:24 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; +Cc: Andreas Karlsson <andreas@proxel.se>; Peter Eisentraut <peter@eisentraut.org>; pgsql-hackers

Hi Jeff,

On Fri, 24 Oct 2025 at 04:37, Jeff Davis <pgsql@j-davis.com> wrote:

>
> Thank you, patch attached, intended for backport to 18.
>

I didn't check it with 18 because changes in regc_pg_locale.c heavily rely
on 5a38104.
However, I think changes to pg_locale.c should be enough to fix the problem
in 18.


> Are you aware of any way to encounter this bug without using an
> extension?
>

I don't think so, but it would be nice to have a test for it anyway.
Unfortunately this test will require a shared_preload_library module.

Regards,
--
Alexander Kukushkin

^ permalink  raw  reply  [nested|flat] 33+ messages in thread


end of thread, other threads:[~2025-10-28 09:24 UTC | newest]

Thread overview: 33+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-06-06 00:23 tiny step toward threading: reduce dependence on setlocale() Jeff Davis <pgsql@j-davis.com>
2024-06-06 18:37 ` Jeff Davis <pgsql@j-davis.com>
2024-06-14 23:35   ` Jeff Davis <pgsql@j-davis.com>
2024-06-19 09:15     ` Peter Eisentraut <peter@eisentraut.org>
2024-07-26 06:07       ` Jeff Davis <pgsql@j-davis.com>
2024-07-26 17:38         ` Andreas Karlsson <andreas@proxel.se>
2024-07-26 20:35           ` Jeff Davis <pgsql@j-davis.com>
2024-07-27 19:03             ` Andreas Karlsson <andreas@proxel.se>
2024-07-29 19:45               ` Peter Eisentraut <peter@eisentraut.org>
2024-07-30 19:13                 ` Jeff Davis <pgsql@j-davis.com>
2024-08-06 21:40                   ` Jeff Davis <pgsql@j-davis.com>
2024-08-07 20:44                     ` Peter Eisentraut <peter@eisentraut.org>
2024-08-08 20:00                       ` Jeff Davis <pgsql@j-davis.com>
2024-08-08 21:18                         ` Peter Eisentraut <peter@eisentraut.org>
2024-09-09 13:37                         ` Peter Eisentraut <peter@eisentraut.org>
2024-09-12 20:01                           ` Jeff Davis <pgsql@j-davis.com>
2024-09-13 14:39                             ` Peter Eisentraut <peter@eisentraut.org>
2024-08-08 21:09                       ` Peter Eisentraut <peter@eisentraut.org>
2024-08-08 22:38                         ` Jeff Davis <pgsql@j-davis.com>
2024-08-11 16:33                     ` Tom Lane <tgl@sss.pgh.pa.us>
2024-08-12 07:00                       ` Peter Eisentraut <peter@eisentraut.org>
2024-08-12 19:47                         ` Jeff Davis <pgsql@j-davis.com>
2024-08-13 15:11                     ` Peter Eisentraut <peter@eisentraut.org>
2024-08-13 17:56                       ` Jeff Davis <pgsql@j-davis.com>
2024-08-13 23:31                         ` Andreas Karlsson <andreas@proxel.se>
2024-08-14 22:55                           ` Jeff Davis <pgsql@j-davis.com>
2024-08-28 16:43                             ` Andreas Karlsson <andreas@proxel.se>
2024-09-04 05:04                               ` Jeff Davis <pgsql@j-davis.com>
2024-09-04 21:45                                 ` Jeff Davis <pgsql@j-davis.com>
2024-09-12 21:09                                   ` Andreas Karlsson <andreas@proxel.se>
2025-09-25 09:31                                   ` Alexander Kukushkin <cyberdemn@gmail.com>
2025-10-24 02:37                                     ` Jeff Davis <pgsql@j-davis.com>
2025-10-28 09:24                                       ` Alexander Kukushkin <cyberdemn@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox