From b99ccd2cb2178b62b613edaf427fda8deace5bbf Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 20 Jan 2022 09:38:05 +0100 Subject: Call pg_newlocale_from_collation() also with default collation Previously, callers of pg_newlocale_from_collation() did not call it if the collation was DEFAULT_COLLATION_OID and instead proceeded with a pg_locale_t of 0. Instead, now we call it anyway and have it return 0 if the default collation was passed. It already did this, so we just have to adjust the callers. This simplifies all the call sites and also makes future enhancements easier. After discussion and testing, the previous comment in pg_locale.c about avoiding this for performance reasons may have been mistaken since it was testing a very different patch version way back when. Reviewed-by: Julien Rouhaud Discussion: https://www.postgresql.org/message-id/ed3baa81-7fac-7788-cc12-41e3f7917e34@enterprisedb.com --- src/backend/utils/adt/varlena.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'src/backend/utils/adt/varlena.c') diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index b3eb39761d..a8db8080e2 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -1200,7 +1200,7 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state) check_collation_set(collid); - if (!lc_collate_is_c(collid) && collid != DEFAULT_COLLATION_OID) + if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); if (mylocale && !mylocale->deterministic) @@ -1556,10 +1556,9 @@ varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid) char a2buf[TEXTBUFLEN]; char *a1p, *a2p; - pg_locale_t mylocale = 0; + pg_locale_t mylocale; - if (collid != DEFAULT_COLLATION_OID) - mylocale = pg_newlocale_from_collation(collid); + mylocale = pg_newlocale_from_collation(collid); /* * memcmp() can't tell us which of two unequal strings sorts first, @@ -1776,13 +1775,18 @@ 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) || - collid == DEFAULT_COLLATION_OID || - pg_newlocale_from_collation(collid)->deterministic) + if (lc_collate_is_c(collid)) + locale_is_c = true; + else + mylocale = pg_newlocale_from_collation(collid); + + if (locale_is_c || !mylocale || mylocale->deterministic) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1830,13 +1834,18 @@ Datum textne(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) || - collid == DEFAULT_COLLATION_OID || - pg_newlocale_from_collation(collid)->deterministic) + if (lc_collate_is_c(collid)) + locale_is_c = true; + else + mylocale = pg_newlocale_from_collation(collid); + + if (locale_is_c || !mylocale || mylocale->deterministic) { Datum arg1 = PG_GETARG_DATUM(0); Datum arg2 = PG_GETARG_DATUM(1); @@ -1947,7 +1956,7 @@ text_starts_with(PG_FUNCTION_ARGS) check_collation_set(collid); - if (!lc_collate_is_c(collid) && collid != DEFAULT_COLLATION_OID) + if (!lc_collate_is_c(collid)) mylocale = pg_newlocale_from_collation(collid); if (mylocale && !mylocale->deterministic) @@ -2061,8 +2070,7 @@ varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid) * we'll figure out the collation based on the locale id and cache the * result. */ - if (collid != DEFAULT_COLLATION_OID) - locale = pg_newlocale_from_collation(collid); + locale = pg_newlocale_from_collation(collid); /* * There is a further exception on Windows. When the database -- cgit v1.2.1