On Thu, Dec 04, 2025 at 03:33:30PM +0300, Bilal Yavuz wrote:
> I applied these feedbacks in v5. I wanted to cover all possible cases
> so I think 0002 might be a bit more complicated than it needs to be.
> 
> What do you think about the current implementation?

I'm finding that a bit bloated.  My own attempt is the attached, which
is much simpler, returning only array for the lines of @tail and
@head.  I am not sure to see the use-case in favor of enforcing the
line count for the caller of the new routine based on what's on this
thread, so I have left that out to simplify the patch.

v5 had a mistake: slurp_file() on the full diffs should be removed
once we dump only the tail and head.  It is true that 027 would add an
extra line if regression.diffs is too short when written this way, but
the information reported is the same, while keeping the code simpler
in 027.  A portion of the comment at the top of the block printing the
diffs could be removed: with the limitation of lines in place we don't
bloat the output anymore.
--
Michael
From e644bb95ce68ee171e65e6d49c0b609d0ccfbf12 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <[email protected]>
Date: Thu, 4 Dec 2025 14:45:45 +0300
Subject: [PATCH v6 1/2] Add read_head_tail() helper to Utils.pm

This function reads lines from a file, from both head and tail
directions, with the size reported tuned depending on a new environment
variable named 'PG_TEST_FILE_READ_LINES', that can be used to override
the default of 50 lines.

Suggested-by: Michael Paquier <[email protected]>
Discussion: https://postgr.es/m/CAN55FZ1D6KXvjSs7YGsDeadqCxNF3UUhjRAfforzzP0k-cE%3DbA%40mail.gmail.com
---
 src/test/perl/PostgreSQL/Test/Utils.pm | 50 ++++++++++++++++++++++++++
 doc/src/sgml/regress.sgml              |  8 +++++
 2 files changed, 58 insertions(+)

diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm
index 85d36a3171e2..0332d28916eb 100644
--- a/src/test/perl/PostgreSQL/Test/Utils.pm
+++ b/src/test/perl/PostgreSQL/Test/Utils.pm
@@ -68,6 +68,7 @@ our @EXPORT = qw(
   slurp_file
   append_to_file
   string_replace_file
+  read_head_tail
   check_mode_recursive
   chmod_recursive
   check_pg_config
@@ -590,6 +591,55 @@ sub string_replace_file
 
 =pod
 
+=item read_head_tail(filename)
+
+Return lines from the head and the tail of a file.  If the file is smaller
+than the number of lines requested, all its contents are returned in @head,
+leaving @tail empty.
+
+If the PG_TEST_FILE_READ_LINES environment variable is set, use it instead
+of the default of 50 lines.
+
+=cut
+
+sub read_head_tail
+{
+	my $filename = shift;
+	my (@head, @tail);
+	my $line_count = 50;
+
+	# Use PG_TEST_FILE_READ_LINES if set.
+	if (defined $ENV{PG_TEST_FILE_READ_LINES})
+	{
+		$line_count = $ENV{PG_TEST_FILE_READ_LINES};
+	}
+
+	return ([], []) if $line_count <= 0;
+
+	open my $fh, '<', $filename or die "couldn't open file: $filename\n";
+	my @lines = <$fh>;
+	close $fh;
+
+	chomp @lines;
+
+	my $total = scalar @lines;
+
+	# If the file is small enough, return all lines in @head.
+	if (2 * $line_count >= $total)
+	{
+		@head = @lines;
+		@tail = ();
+		return (\@head, \@tail);
+	}
+
+	@head = @lines[ 0 .. $line_count - 1 ];
+	@tail = @lines[ $total - $line_count .. $total - 1 ];
+
+	return (\@head, \@tail);
+}
+
+=pod
+
 =item check_mode_recursive(dir, expected_dir_mode, expected_file_mode, ignore_list)
 
 Check that all file/dir modes in a directory match the expected values,
diff --git a/doc/src/sgml/regress.sgml b/doc/src/sgml/regress.sgml
index fd1e142d5591..d80dd46c5fdb 100644
--- a/doc/src/sgml/regress.sgml
+++ b/doc/src/sgml/regress.sgml
@@ -917,6 +917,14 @@ PG_TEST_NOCLEAN=1 make -C src/bin/pg_dump check
     <varname>PG_TEST_TIMEOUT_DEFAULT</varname> to a higher number will change
     the default to avoid this.
    </para>
+
+   <para>
+    For certain tests, the environment variable
+    <envar>PG_TEST_FILE_READ_LINES</envar> can be set to limit the number of
+    lines read from large output files (head and tail). This is useful when
+    the test output contains a lot of unnecessary content, allowing the test
+    framework to read only a limited number of lines for its reports.
+   </para>
   </sect2>
 
   </sect1>
-- 
2.51.0

From 8245606fce82aadb5fd07fcd8d22770fc73906f9 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <[email protected]>
Date: Thu, 4 Dec 2025 14:47:25 +0300
Subject: [PATCH v6 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 <[email protected]>
Reviewed-by: Michael Paquier <[email protected]>
Discussion: https://postgr.es/m/CAN55FZ1D6KXvjSs7YGsDeadqCxNF3UUhjRAfforzzP0k-cE%3DbA%40mail.gmail.com
---
 src/test/recovery/t/027_stream_regress.pl | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/test/recovery/t/027_stream_regress.pl b/src/test/recovery/t/027_stream_regress.pl
index 589c79d97d3a..195e1eb83473 100644
--- a/src/test/recovery/t/027_stream_regress.pl
+++ b/src/test/recovery/t/027_stream_regress.pl
@@ -83,9 +83,7 @@ my $rc =
 	  . "--outputdir=\"$outputdir\"");
 
 # Regression diffs are only meaningful if both the primary and the standby
-# are still alive after a regression test failure.  A crash would cause a
-# useless increase in the log quantity, mostly filled with information
-# related to queries that could not run.
+# are still alive after a regression test failure.
 my $primary_alive = $node_primary->is_alive;
 my $standby_alive = $node_standby_1->is_alive;
 if ($rc != 0 && $primary_alive && $standby_alive)
@@ -94,9 +92,21 @@ if ($rc != 0 && $primary_alive && $standby_alive)
 	my $diffs = "$outputdir/regression.diffs";
 	if (-e $diffs)
 	{
-		print "=== dumping $diffs ===\n";
-		print slurp_file($diffs);
-		print "=== EOF ===\n";
+		# Dump portions of the diff file.
+		my ($head, $tail) = read_head_tail($diffs);
+
+		diag("=== dumping $diffs (head) ===");
+		foreach my $line (@$head)
+		{
+			diag($line);
+		}
+
+		diag("=== dumping $diffs (tail) ===");
+		foreach my $line (@$tail)
+		{
+			diag($line);
+		}
+		diag("=== EOF ===");
 	}
 }
 is($rc, 0, 'regression tests pass');
-- 
2.51.0

Attachment: signature.asc
Description: PGP signature

Reply via email to