From a4b0b0324900d12d487370a08b4ddba20552e230 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Wed, 7 Aug 2024 10:23:05 +1200
Subject: [PATCH v6 3/5] Don't allow UTF-8 with non-UTF-8 locales on Windows.

Historically, we allowed contradictions such as:

initdb.exe --locale="French_France.1252" --encoding="UTF-8"

That's because Windows didn't support UTF-8 directly, and PostgreSQL
had to perform UTF-8 char -> wchar_t conversions at various places on
that OS, and still does.  Therefore it was never actually passing
UTF-8 text to operating system facilities.

In preparation for removing those code paths, harmonizing the code
and behavior with Unix builds, and adapting to modern Windows
interfaces, ban such contradictions.  Locale names should ideally be
specified as BCP 47 tags.  If UTF-8 is desired, the name should have
".UTF-8" on the end, but otherwise the traditional encoding of that
language is implied.  Now only the following are valid:

initdb.exe --locale="fr-FR" --encoding="WIN1252"
initdb.exe --locale="fr-FR.UTF-8" --encoding="UTF-8"
initdb.exe --locale="French_France.1252" --encoding="WIN1252"

(The last form is not recommended, but still accepted.)

XXX This will cause problems for clusters upgraded with pg_upgrade from
a system using locales with the wrong encoding.  We'll need a way to
translate to the correct modern locale names.

Discussion: https://postgr.es/m/CA%2BhUKGJ%3DXThErgAQRoqfCy1bKPxXVuF0%3D2zDbB%2BSxDs59pv7Fw%40mail.gmail.com
---
 src/backend/commands/dbcommands.c | 12 +-----------
 src/bin/initdb/initdb.c           | 11 -----------
 2 files changed, 1 insertion(+), 22 deletions(-)

diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 7026352bc9..566085fecc 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -1555,11 +1555,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
  * 2. locale encoding = -1, which means that we couldn't determine the
  * locale's encoding and have to trust the user to get it right.
  *
- * 3. selected encoding is UTF8 and platform is win32. This is because
- * UTF8 is a pseudo codepage that is supported in all locales since it's
- * converted to UTF16 before being used.
- *
- * 4. selected encoding is SQL_ASCII, but only if you're a superuser. This
+ * 3. selected encoding is SQL_ASCII, but only if you're a superuser. This
  * is risky but we have historically allowed it --- notably, the
  * regression tests require it.
  *
@@ -1574,9 +1570,6 @@ check_encoding_locale_matches(int encoding, const char *collate, const char *cty
 	if (!(ctype_encoding == encoding ||
 		  ctype_encoding == PG_SQL_ASCII ||
 		  ctype_encoding == -1 ||
-#ifdef WIN32
-		  encoding == PG_UTF8 ||
-#endif
 		  (encoding == PG_SQL_ASCII && superuser())))
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -1589,9 +1582,6 @@ check_encoding_locale_matches(int encoding, const char *collate, const char *cty
 	if (!(collate_encoding == encoding ||
 		  collate_encoding == PG_SQL_ASCII ||
 		  collate_encoding == -1 ||
-#ifdef WIN32
-		  encoding == PG_UTF8 ||
-#endif
 		  (encoding == PG_SQL_ASCII && superuser())))
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 393232b6ce..77bf815919 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -2228,9 +2228,6 @@ check_locale_encoding(const char *locale, int user_enc)
 	if (!(locale_enc == user_enc ||
 		  locale_enc == PG_SQL_ASCII ||
 		  locale_enc == -1 ||
-#ifdef WIN32
-		  user_enc == PG_UTF8 ||
-#endif
 		  user_enc == PG_SQL_ASCII))
 	{
 		pg_log_error("encoding mismatch");
@@ -2695,13 +2692,6 @@ setup_locale_encoding(void)
 			 * Windows, UTF-8 works with any locale, so we can fall back to
 			 * UTF-8.
 			 */
-#ifdef WIN32
-			encodingid = PG_UTF8;
-			printf(_("Encoding \"%s\" implied by locale is not allowed as a server-side encoding.\n"
-					 "The default database encoding will be set to \"%s\" instead.\n"),
-				   pg_encoding_to_char(ctype_enc),
-				   pg_encoding_to_char(encodingid));
-#else
 			pg_log_error("locale \"%s\" requires unsupported encoding \"%s\"",
 						 lc_ctype, pg_encoding_to_char(ctype_enc));
 			pg_log_error_detail("Encoding \"%s\" is not allowed as a server-side encoding.",
@@ -2709,7 +2699,6 @@ setup_locale_encoding(void)
 			pg_log_error_hint("Rerun %s with a different locale selection.",
 							  progname);
 			exit(1);
-#endif
 		}
 		else
 		{
-- 
2.39.2

