v27-0002-Simplify-lookup_collation_cache.patch
text/x-patch
Filename: v27-0002-Simplify-lookup_collation_cache.patch
Type: text/x-patch
Part: 1
Message:
Re: Built-in CTYPE provider
Patch
Format: format-patch
Series: patch v27-0002
Subject: Simplify lookup_collation_cache.
| File | + | − |
|---|---|---|
| src/backend/utils/adt/pg_locale.c | 14 | 63 |
| src/include/utils/pg_locale.h | 2 | 0 |
From 75ec4838af6618bdc408045369486497125d5aa7 Mon Sep 17 00:00:00 2001
From: Jeff Davis <jeff@j-davis.com>
Date: Wed, 20 Mar 2024 17:00:13 -0700
Subject: [PATCH v27 2/5] Simplify lookup_collation_cache.
Attach flags 'collate_is_c' and 'ctype_is_c' to pg_locale_t instead of
the collation cache entry. pg_newlocale_from_collation() already has
the catalog entry, so it's easy to set the flags from there, instead.
---
src/backend/utils/adt/pg_locale.c | 77 ++++++-------------------------
src/include/utils/pg_locale.h | 2 +
2 files changed, 16 insertions(+), 63 deletions(-)
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index aa40921d7d..999d78d01c 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -125,9 +125,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;
@@ -1227,7 +1224,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;
@@ -1248,63 +1245,7 @@ lookup_collation_cache(Oid collation, bool set_flags)
cache_entry = hash_search(collation_cache, &collation, HASH_ENTER, &found);
if (!found)
- {
- /*
- * 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;
}
@@ -1374,7 +1315,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;
}
/*
@@ -1440,7 +1381,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;
}
struct pg_locale_struct default_locale;
@@ -1563,7 +1504,7 @@ pg_newlocale_from_collation(Oid collid)
return &default_locale;
}
- cache_entry = lookup_collation_cache(collid, false);
+ cache_entry = lookup_collation_cache(collid);
if (cache_entry->locale == 0)
{
@@ -1592,6 +1533,8 @@ pg_newlocale_from_collation(Oid collid)
datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale);
locstr = TextDatumGetCString(datum);
+ result.collate_is_c = true;
+ result.ctype_is_c = (strcmp(locstr, "C") == 0);
builtin_validate_locale(GetDatabaseEncoding(), locstr);
result.info.builtin.locale = MemoryContextStrdup(TopMemoryContext,
@@ -1608,6 +1551,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 */
@@ -1658,6 +1606,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 205aa20067..41659cdbbd 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