From 9adcb124349add21fb23fe26ba30dd7ad30c2b2c Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Fri, 6 Jun 2025 23:00:33 +1200
Subject: [PATCH 1/2] Require UCRT on Windows, remove MSVCRT traces.

In commit 1758d424 we decided to support only UCRT (the Windows 10+
"universal" C runtime), but compilation was apparently still possible
with the older MSVCRT C runtimes.  To avoid confusion about various
missing or stubbed out functionality, refuse to compile on the latter
with a clear error.

While here, also remove the list of MSVCRT versions from
src/port/win32env.c.  It was already incomplete, but we don't expect
postgres.exe to be linked against libraries that were linked against old
msvcrt libraries these days, per recommendations at
https://www.msys2.org/docs/environments/.  (It would probably works as
long as you don't pass thing like FILE * across library boundaries, but
it'd be anachronistic to bring that list up to date now.)

Reported-by: Joseph Ayers <joseph.ayers@crunchydata.com>
Discussion: https://postgr.es/m/18940-bbf045a8ceba1a95%40postgresql.org
---
 src/include/port/win32_port.h |  8 +++++---
 src/port/win32env.c           | 16 ----------------
 2 files changed, 5 insertions(+), 19 deletions(-)

diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h
index ff7028bdc81..de73ec94148 100644
--- a/src/include/port/win32_port.h
+++ b/src/include/port/win32_port.h
@@ -16,6 +16,10 @@
 #ifndef PG_WIN32_PORT_H
 #define PG_WIN32_PORT_H
 
+#ifndef _UCRT
+#error "UCRT is required (MingGW's MSVCRT environment is not supported)."
+#endif
+
 /*
  * Always build with SSPI support. Keep it as a #define in case
  * we want a switch to disable it sometime in the future.
@@ -458,9 +462,7 @@ extern int	_pglstat64(const char *name, struct stat *buf);
 
 /*
  * Versions of libintl >= 0.18? try to replace setlocale() with a macro
- * to their own versions.  Remove the macro, if it exists, because it
- * ends up calling the wrong version when the backend and libintl use
- * different versions of msvcrt.
+ * to their own versions, which gets in the way of our own.
  */
 #if defined(setlocale)
 #undef setlocale
diff --git a/src/port/win32env.c b/src/port/win32env.c
index b22fbafde40..f498cafbe49 100644
--- a/src/port/win32env.c
+++ b/src/port/win32env.c
@@ -30,22 +30,6 @@ pgwin32_putenv(const char *envval)
 	char	   *cp;
 	typedef int (_cdecl * PUTENVPROC) (const char *);
 	static const char *const modulenames[] = {
-		"msvcrt",				/* Visual Studio 6.0 / MinGW */
-		"msvcrtd",
-		"msvcr70",				/* Visual Studio 2002 */
-		"msvcr70d",
-		"msvcr71",				/* Visual Studio 2003 */
-		"msvcr71d",
-		"msvcr80",				/* Visual Studio 2005 */
-		"msvcr80d",
-		"msvcr90",				/* Visual Studio 2008 */
-		"msvcr90d",
-		"msvcr100",				/* Visual Studio 2010 */
-		"msvcr100d",
-		"msvcr110",				/* Visual Studio 2012 */
-		"msvcr110d",
-		"msvcr120",				/* Visual Studio 2013 */
-		"msvcr120d",
 		"ucrtbase",				/* Visual Studio 2015 and later */
 		"ucrtbased",
 		NULL
-- 
2.47.2

