Removes ICU includes from pg_locale.h, and instead include them in the
few C files that need them.

There used to be more files that needed to use ICU functions, but after
refactoring, there are only a few files that access ICU APIs directly.

Regards,
        Jeff Davis

From 95da41fa75da3f0e2f76ca2bd9832d4f75f741b7 Mon Sep 17 00:00:00 2001
From: Jeff Davis <[email protected]>
Date: Tue, 6 Jan 2026 14:31:18 -0800
Subject: [PATCH v1] Clean up ICU includes.

Remove ICU includes from pg_locale.h, and instead include them in the
few C files that need ICU. Clean up a few other includes in passing.
---
 src/backend/commands/collationcmds.c  |  4 ++++
 src/backend/utils/adt/formatting.c    |  8 --------
 src/backend/utils/adt/pg_locale.c     | 14 ++++++++++++++
 src/backend/utils/adt/pg_locale_icu.c |  2 ++
 src/backend/utils/adt/varlena.c       | 11 ++++++-----
 src/include/utils/pg_locale.h         | 16 ++++------------
 6 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/src/backend/commands/collationcmds.c b/src/backend/commands/collationcmds.c
index 8f3dddc619e..980cc3ac627 100644
--- a/src/backend/commands/collationcmds.c
+++ b/src/backend/commands/collationcmds.c
@@ -14,6 +14,10 @@
  */
 #include "postgres.h"
 
+#ifdef USE_ICU
+#include <unicode/uloc.h>
+#endif
+
 #include "access/htup_details.h"
 #include "access/table.h"
 #include "access/xact.h"
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index f5f4a110e12..6affdd86624 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -68,17 +68,9 @@
 #include <math.h>
 #include <float.h>
 #include <limits.h>
-#include <wctype.h>
 
-#ifdef USE_ICU
-#include <unicode/ustring.h>
-#endif
-
-#include "catalog/pg_collation.h"
 #include "catalog/pg_type.h"
 #include "common/int.h"
-#include "common/unicode_case.h"
-#include "common/unicode_category.h"
 #include "mb/pg_wchar.h"
 #include "nodes/miscnodes.h"
 #include "parser/scansup.h"
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index 9a92f2fc959..ac324ecaad2 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -32,6 +32,9 @@
 #include "postgres.h"
 
 #include <time.h>
+#ifdef USE_ICU
+#include <unicode/ucol.h>
+#endif
 
 #include "access/htup_details.h"
 #include "catalog/pg_collation.h"
@@ -1645,6 +1648,17 @@ pg_towlower(pg_wchar wc, pg_locale_t locale)
 		return locale->ctype->wc_tolower(wc, locale);
 }
 
+/* version of Unicode used by ICU */
+const char *
+pg_icu_unicode_version()
+{
+#ifdef USE_ICU
+	return U_UNICODE_VERSION;
+#else
+	return NULL;
+#endif
+}
+
 /*
  * Return required encoding ID for the given locale, or -1 if any encoding is
  * valid for the locale.
diff --git a/src/backend/utils/adt/pg_locale_icu.c b/src/backend/utils/adt/pg_locale_icu.c
index 68491666738..88f86ab18a3 100644
--- a/src/backend/utils/adt/pg_locale_icu.c
+++ b/src/backend/utils/adt/pg_locale_icu.c
@@ -12,7 +12,9 @@
 #include "postgres.h"
 
 #ifdef USE_ICU
+#include <unicode/ucasemap.h>
 #include <unicode/ucnv.h>
+#include <unicode/ucol.h>
 #include <unicode/ustring.h>
 
 /*
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index cfcc35592e3..c80191f0a22 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -5381,11 +5381,12 @@ unicode_version(PG_FUNCTION_ARGS)
 Datum
 icu_unicode_version(PG_FUNCTION_ARGS)
 {
-#ifdef USE_ICU
-	PG_RETURN_TEXT_P(cstring_to_text(U_UNICODE_VERSION));
-#else
-	PG_RETURN_NULL();
-#endif
+	const char *version = pg_icu_unicode_version();
+
+	if (version)
+		PG_RETURN_TEXT_P(cstring_to_text(version));
+	else
+		PG_RETURN_NULL();
 }
 
 /*
diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h
index 465f170ba79..444350bb803 100644
--- a/src/include/utils/pg_locale.h
+++ b/src/include/utils/pg_locale.h
@@ -14,16 +14,6 @@
 
 #include "mb/pg_wchar.h"
 
-#ifdef USE_ICU
-/* only include the C APIs, to avoid errors in cpluspluscheck */
-#undef U_SHOW_CPLUSPLUS_API
-#define U_SHOW_CPLUSPLUS_API 0
-#undef U_SHOW_CPLUSPLUS_HEADER_API
-#define U_SHOW_CPLUSPLUS_HEADER_API 0
-#include <unicode/ucol.h>
-#include <unicode/ucasemap.h>
-#endif
-
 /* use for libc locale names */
 #define LOCALE_NAME_BUFLEN 128
 
@@ -167,9 +157,9 @@ struct pg_locale_struct
 		struct
 		{
 			const char *locale;
-			UCollator  *ucol;
+			struct UCollator *ucol;
+			struct UCaseMap *ucasemap;
 			locale_t	lt;
-			UCaseMap   *ucasemap;
 		}			icu;
 #endif
 	};
@@ -223,6 +213,8 @@ extern bool pg_iswcased(pg_wchar wc, pg_locale_t locale);
 extern pg_wchar pg_towupper(pg_wchar wc, pg_locale_t locale);
 extern pg_wchar pg_towlower(pg_wchar wc, pg_locale_t locale);
 
+extern const char *pg_icu_unicode_version(void);
+
 extern int	builtin_locale_encoding(const char *locale);
 extern const char *builtin_validate_locale(int encoding, const char *locale);
 extern void icu_validate_locale(const char *loc_str);
-- 
2.43.0

Reply via email to