aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/pg_locale.c
diff options
context:
space:
mode:
authorJeff Davis <jdavis@postgresql.org>2024-07-28 13:07:25 -0700
committerJeff Davis <jdavis@postgresql.org>2024-07-28 13:07:25 -0700
commit1c461a8d8d3c7a4655fdb944ffca94b1e49e5b3d (patch)
treedac5da64d9684260c24d8fe601be2b97b14da083 /src/backend/utils/adt/pg_locale.c
parent005c6b833f7866b71b50a5382e30d6c3f695306e (diff)
downloadpostgresql-1c461a8d8d3c7a4655fdb944ffca94b1e49e5b3d.tar.gz
postgresql-1c461a8d8d3c7a4655fdb944ffca94b1e49e5b3d.zip
Refactor: make default_locale internal to pg_locale.c.
Discussion: https://postgr.es/m/2228884bb1f1a02614b39f71a90c94d2cc8a3a2f.camel@j-davis.com Reviewed-by: Peter Eisentraut, Andreas Karlsson
Diffstat (limited to 'src/backend/utils/adt/pg_locale.c')
-rw-r--r--src/backend/utils/adt/pg_locale.c69
1 files changed, 66 insertions, 3 deletions
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index f386945ee0c..c1b7ecced48 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 "common/hashfn.h"
#include "mb/pg_wchar.h"
#include "miscadmin.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;
@@ -1458,8 +1461,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,
@@ -1554,7 +1555,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.