diff --git a/contrib/pg_upgrade/exec.c b/contrib/pg_upgrade/exec.c
index 99f5006..f84d857 100644
--- a/contrib/pg_upgrade/exec.c
+++ b/contrib/pg_upgrade/exec.c
@@ -63,7 +63,25 @@ exec_prog(const char *log_file, const char *opt_log_file,
 	if (written >= MAXCMDLEN)
 		pg_log(PG_FATAL, "command too long\n");
 
-	if ((log = fopen_priv(log_file, "a")) == NULL)
+#ifdef WIN32
+	{
+		/* 
+		 * Try to open the log file a few times in case the
+		 * server takes a bit longer than we'd like to release it.
+		 */
+		int iter;
+		for (iter = 0; iter < 5; iter++)
+		{
+			log = fopen_priv(log_file, "a");
+			if (log != NULL || iter == 4)
+				break;
+			sleep(1);
+		}
+	}
+#else
+	log = fopen_priv(log_file, "a");
+#endif
+	if (log == NULL)
 		pg_log(PG_FATAL, "cannot write to log file %s\n", log_file);
 #ifdef WIN32
 	fprintf(log, "\n\n");
