diff --git a/src/port/win32error.c b/src/port/win32error.c
index 2222be89071839417f571cfda3c0a118183c9e3d..5c49b38a5e70cf72a43acdc7d06eedd8dc78353f 100644
--- a/src/port/win32error.c
+++ b/src/port/win32error.c
@@ -179,14 +179,16 @@ _dosmaperr(unsigned long e)
 	{
 		if (doserrors[i].winerr == e)
 		{
-			errno = doserrors[i].doserr;
+			int			doserr = doserrors[i].doserr;
+
 #ifndef FRONTEND
 			ereport(DEBUG5,
 					(errmsg_internal("mapped win32 error code %lu to %d",
-									 e, errno)));
+									 e, doserr)));
 #elif FRONTEND_DEBUG
-			fprintf(stderr, _("mapped win32 error code %lu to %d"), e, errno);
+			fprintf(stderr, _("mapped win32 error code %lu to %d"), e, doserr);
 #endif
+			errno = doserr;
 			return;
 		}
 	}