From af8b5211697c43ec5a10b5492d2ac5238b53ca83 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Mon, 26 Jan 2026 00:15:30 +0100
Subject: [PATCH v2 1/5] pg_regress: Include diffs in output

Whenever pg_regress fails there's an indirection to actually get to the
failure reason. Locally it's possible to copy paste the filename and
open the file, but in CI it's necessary to manually traverse the
directory structure by clicking and scrolling a bunch of time.

This change starts printing the first 80 lines of the regression.diffs
file as TAP diagnostics. So it's not necessary to open the
regression.diffs file in many cases.
---
 src/test/regress/pg_regress.c | 52 +++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 8 deletions(-)

diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index b5c0cb647a8..5ce4d24d095 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -1414,6 +1414,7 @@ results_differ(const char *testname, const char *resultsfile, const char *defaul
 	int			best_line_count;
 	int			i;
 	int			l;
+	long		startpos;
 	const char *platform_expectfile;
 
 	/*
@@ -1521,22 +1522,57 @@ results_differ(const char *testname, const char *resultsfile, const char *defaul
 	 * append to the diffs summary file.
 	 */
 
-	/* Write diff header */
-	difffile = fopen(difffilename, "a");
+	difffile = fopen(difffilename, "a+");
 	if (difffile)
 	{
+		fseek(difffile, 0, SEEK_END);
+		startpos = ftell(difffile);
 		fprintf(difffile,
 				"diff %s %s %s\n",
 				pretty_diff_opts, best_expect_file, resultsfile);
+		fflush(difffile);
+
+		/* Run diff */
+		snprintf(cmd, sizeof(cmd),
+				 "diff %s \"%s\" \"%s\" >> \"%s\"",
+				 pretty_diff_opts, best_expect_file, resultsfile, difffilename);
+		run_diff(cmd, difffilename);
+
+		/* Emit diff as TAP diagnostics */
+		{
+			/*
+			 * In case of a crash the diff can be huge and all of the
+			 * subsequent tests will fail with essentially useless diffs too.
+			 * So to avoid flooding the output, while still providing useful
+			 * info in most cases we output only the first 80 lines of the
+			 * *combined* diff. The number 80 is chosen so that we output less
+			 * than 100 lines of diagnostics per pg_regress run. Otherwise if
+			 * meson is run with the --quiet flag only the last 100 lines are
+			 * shown and usually the most useful information is actually in
+			 * the first few lines
+			 */
+			static int	nlines = 0;
+			const int	max_diff_lines = 80;
+			char		line[1024];
+
+			fseek(difffile, startpos, SEEK_SET);
+			while (nlines <= max_diff_lines &&
+				   fgets(line, sizeof(line), difffile))
+			{
+				size_t		len = strlen(line);
+
+				if (len > 0 && line[len - 1] == '\n')
+					line[len - 1] = '\0';
+
+				if (++nlines > max_diff_lines)
+					diag("(diff output truncated and silencing output for further failing tests...)");
+				else
+					diag("%s", line);
+			}
+		}
 		fclose(difffile);
 	}
 
-	/* Run diff */
-	snprintf(cmd, sizeof(cmd),
-			 "diff %s \"%s\" \"%s\" >> \"%s\"",
-			 pretty_diff_opts, best_expect_file, resultsfile, difffilename);
-	run_diff(cmd, difffilename);
-
 	unlink(diff);
 	return true;
 }

base-commit: 851f6649cc18c4b482fa2b6afddb65b35d035370
-- 
2.52.0

