v25-0004-Use-version-for-builtin-collations.patch

text/x-patch

Filename: v25-0004-Use-version-for-builtin-collations.patch
Type: text/x-patch
Part: 3
Message: Re: Built-in CTYPE provider

Patch

Format: format-patch
Series: patch v25-0004
Subject: Use version for builtin collations.
File+
src/backend/utils/adt/pg_locale.c 14 2
From 3789f5d6483b650f4957d358d6974108c6c0b260 Mon Sep 17 00:00:00 2001
From: Jeff Davis <jeff@j-davis.com>
Date: Thu, 14 Mar 2024 12:07:45 -0700
Subject: [PATCH v25 4/7] Use version for builtin collations.

------ CATVERSION ------

Even though we don't expect any changes to memcmp() semantics, it's
plausible that the surrounding optimizations (such as abbreviated
keys, or future optimizations that don't exist today) may have bugs.

Given that the version field already exists, there's little reason not
to use it. Suggestion from Peter Eisentraut.

Discussion: https://postgr.es/m/613c120a-5413-4fa7-a501-6590eae558f8@eisentraut.org
---
 src/backend/utils/adt/pg_locale.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index 0ea79f9b4f..793984053f 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -1732,13 +1732,25 @@ get_collation_actual_version(char collprovider, const char *collcollate)
 
 	/*
 	 * The only two supported locales (C and C.UTF-8) are both based on memcmp
-	 * and are not expected to change.
+	 * and are not expected to change. However, track version anyway, which
+	 * may be useful in the event of bugs.
 	 *
 	 * Note that the character semantics may change for some locales, but the
 	 * collation version only tracks changes to sort order.
 	 */
 	if (collprovider == COLLPROVIDER_BUILTIN)
-		return NULL;
+	{
+		Assert(collcollate != NULL);
+		if (strcmp(collcollate, "C") == 0)
+			return "1.0";
+		else if (strcmp(collcollate, "C.UTF-8") == 0)
+			return "1.0";
+		else
+			ereport(ERROR,
+					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+					 errmsg("invalid locale name \"%s\" for builtin provider",
+							collcollate)));
+	}
 
 #ifdef USE_ICU
 	if (collprovider == COLLPROVIDER_ICU)
-- 
2.34.1