v1-0008-Set-process-LC_COLLATE-C-and-LC_CTYPE-C.patch

text/x-patch

Filename: v1-0008-Set-process-LC_COLLATE-C-and-LC_CTYPE-C.patch
Type: text/x-patch
Part: 7
Message: Re: Remaining dependency on setlocale()

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v1-0008
Subject: Set process LC_COLLATE=C and LC_CTYPE=C.
File+
src/backend/utils/init/postinit.c 11 13
From 9dc49121a391ccdefa19904329171e1fbe9a8a3d Mon Sep 17 00:00:00 2001
From: Jeff Davis <jeff@j-davis.com>
Date: Fri, 6 Jun 2025 14:14:22 -0700
Subject: [PATCH v1 8/8] Set process LC_COLLATE=C and LC_CTYPE=C.

Now that locale-aware functions use global_lc_locale rather than
relying on setlocale(), set LC_COLLATE and LC_CTYPE to C for
consistency.
---
 src/backend/utils/init/postinit.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 3eaa1486f6f..9841a33689a 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -417,19 +417,17 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect
 	datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype);
 	ctype = TextDatumGetCString(datum);
 
-	if (pg_perm_setlocale(LC_COLLATE, collate) == NULL)
-		ereport(FATAL,
-				(errmsg("database locale is incompatible with operating system"),
-				 errdetail("The database was initialized with LC_COLLATE \"%s\", "
-						   " which is not recognized by setlocale().", collate),
-				 errhint("Recreate the database with another locale or install the missing locale.")));
-
-	if (pg_perm_setlocale(LC_CTYPE, ctype) == NULL)
-		ereport(FATAL,
-				(errmsg("database locale is incompatible with operating system"),
-				 errdetail("The database was initialized with LC_CTYPE \"%s\", "
-						   " which is not recognized by setlocale().", ctype),
-				 errhint("Recreate the database with another locale or install the missing locale.")));
+	/*
+	 * Set LC_COLLATE and LC_CTYPE both to "C" for consistency.
+	 *
+	 * Historically, these were set to datcollate and datctype, respectively,
+	 * but that made it too easy to depend on setlocale() at odd places
+	 * throughout the server.
+	 */
+	if (pg_perm_setlocale(LC_COLLATE, "C") == NULL)
+		elog(ERROR, "failure setting LC_COLLATE=\"C\"");
+	if (pg_perm_setlocale(LC_CTYPE, "C") == NULL)
+		elog(ERROR, "failure setting LC_CTYPE=\"C\"");
 
 	init_global_lc_ctype(ctype);
 
-- 
2.43.0