diff --git a/.cirrus.tasks.yml b/.cirrus.tasks.yml
index a22cef063f3..c02dbae8e30 100644
--- a/.cirrus.tasks.yml
+++ b/.cirrus.tasks.yml
@@ -30,6 +30,7 @@ env:
   PGCTLTIMEOUT: 120 # avoids spurious failures during parallel tests
   TEMP_CONFIG: ${CIRRUS_WORKING_DIR}/src/tools/ci/pg_ci_base.conf
   PG_TEST_EXTRA: kerberos ldap ssl libpq_encryption load_balance oauth
+  PG_REGRESS_DIFF_EXTRACT: 1
 
   # Postgres config args for the meson builds, shared between all meson tasks
   # except the 'SanityCheck' task
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 9a918156437..a5e9d25b1fb 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -1549,16 +1549,9 @@ results_differ(const char *testname, const char *resultsfile, const char *defaul
 				 "diff %s \"%s\" \"%s\" >> \"%s\"",
 				 pretty_diff_opts, best_expect_file, resultsfile, difffilename);
 		run_diff(cmd, difffilename);
-
-		/*
-		 * Reopen the file for reading to emit the diff as TAP diagnostics. We
-		 * can't keep the file open while diff appends to it, because on
-		 * Windows the file lock prevents diff from writing.
-		 */
-		difffile = fopen(difffilename, "r");
 	}
 
-	if (difffile)
+	if (difffile && getenv("PG_REGRESS_DIFF_EXTRACT"))
 	{
 		/*
 		 * In case of a crash the diff can be huge and all of the subsequent
@@ -1574,6 +1567,9 @@ results_differ(const char *testname, const char *resultsfile, const char *defaul
 		const int	max_diff_lines = 80;
 		char		line[1024];
 
+		/* Reopen the file for reading to emit the diff as TAP diagnostics. */
+		difffile = fopen(difffilename, "r");
+
 		fseek(difffile, startpos, SEEK_SET);
 		while (nlines < max_diff_lines &&
 			   fgets(line, sizeof(line), difffile))
