From bd13c7dfec0e373eefa7f001fbe9de3206956b06 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Fri, 18 Jul 2025 10:34:41 +0300
Subject: [PATCH v4 2/2] Improve error reporting in 027_stream_regress test

Previously, the 027_stream_regress test only reported that the regression
test had failed, without showing the actual error. The detailed failure
information was hidden in the regression.diffs file.

This commit improves the situation by including the head and tail of
regression.diffs directly in the failure message. This helps quickly
identify the root cause without needing to open extra files.

Suggested-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAN55FZ1D6KXvjSs7YGsDeadqCxNF3UUhjRAfforzzP0k-cE%3DbA%40mail.gmail.com
---
 src/test/recovery/t/027_stream_regress.pl | 54 +++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/src/test/recovery/t/027_stream_regress.pl b/src/test/recovery/t/027_stream_regress.pl
index 5d2c06ba06e..d1198943ab9 100644
--- a/src/test/recovery/t/027_stream_regress.pl
+++ b/src/test/recovery/t/027_stream_regress.pl
@@ -97,6 +97,10 @@ if ($rc != 0 && $primary_alive && $standby_alive)
 		print "=== dumping $diffs ===\n";
 		print slurp_file($diffs);
 		print "=== EOF ===\n";
+
+		# Dump out 50 lines from head and tail of regression diffs to the
+		# failure message.
+		regression_log_helper($diffs, 50);
 	}
 }
 is($rc, 0, 'regression tests pass');
@@ -190,4 +194,54 @@ UPDATE|t), 'check contents of pg_stat_statements on regression database');
 $node_standby_1->stop;
 $node_primary->stop;
 
+sub regression_log_helper
+{
+	my ($diff_file, $lines_to_dump) = @_;
+	my @lines;
+
+	my $temp_env_var = $ENV{PG_TEST_FILE_READ_LINES};
+	if (defined $temp_env_var)
+	{
+		$lines_to_dump = $temp_env_var;
+	}
+
+	if ($lines_to_dump <= 0)
+	{
+		return;
+	}
+
+	open my $fh, '<', $diff_file or die "couldn't open file: $diff_file\n";
+
+	while (my $line = <$fh>)
+	{
+		push @lines, $line;
+	}
+	close $fh;
+
+	my $line_count = scalar @lines;
+
+	# If the diff_file has fewer lines than (2 * $lines_to_dump), dump out the
+	# entire file.
+	if ($line_count <= (2 * $lines_to_dump))
+	{
+		diag("\n=== dumping $diff_file ===\n");
+		foreach my $line (@lines)
+		{
+			diag($line);
+		}
+	}
+	else
+	{
+		diag(
+			"\n=== dumping $lines_to_dump lines from head of $diff_file ===\n"
+		);
+		diag(read_file_ends($diff_file, 'head', $lines_to_dump, 1));
+		diag(
+			"\n=== dumping $lines_to_dump lines from tail of $diff_file ===\n"
+		);
+		diag(read_file_ends($diff_file, 'tail', $lines_to_dump, 1));
+	}
+	diag("=== EOF ===\n\n");
+}
+
 done_testing();
-- 
2.50.0

