commit 036e9d372241e7046e7b83baef991325d39730c3
Author: Anton A. Melnikov <a.melnikov@postgrespro.ru>
Date:   Thu Dec 22 08:01:40 2022 +0300

    Additional dumps filtering during upgrade test.

diff --git a/src/bin/pg_upgrade/TESTING b/src/bin/pg_upgrade/TESTING
index 127dc30bbb..8309aaeb05 100644
--- a/src/bin/pg_upgrade/TESTING
+++ b/src/bin/pg_upgrade/TESTING
@@ -56,3 +56,8 @@ Once the dump is created, it can be repeatedly used with $olddump and
 the dump out of the new database and the comparison of the dumps between
 the old and new databases.  The contents of the dumps can also be manually
 compared.
+
+You can use additional dump filtering. To do this, you need to define the
+'filter' environment variable and specify the path to the file with
+filtering rules in it. There is an example of such a file in
+src/bin/pg_upgrade/filter/regex.
diff --git a/src/bin/pg_upgrade/t/002_pg_upgrade.pl b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
index 4cc1469306..027b3fa35c 100644
--- a/src/bin/pg_upgrade/t/002_pg_upgrade.pl
+++ b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
@@ -44,6 +44,33 @@ sub filter_dump
 	$dump_contents =~ s/^\-\-.*//mgx;
 	# Remove empty lines.
 	$dump_contents =~ s/^\n//mgx;
+	# Replace specific privilegies with ALL
+	$dump_contents =~ s/^(GRANT\s|REVOKE\s)(\S*)\s/$1ALL /mgx;
+
+	if (defined($ENV{filter}))
+	{
+		# Use the external filter
+		my $ext_filter_file = $ENV{filter};
+		die "no file with external filter found!" unless -e $ext_filter_file;
+
+		open my $ext_filter_handle, '<', $ext_filter_file;
+		chomp(my @ext_filter_lines = <$ext_filter_handle>);
+		close $ext_filter_handle;
+
+		foreach (@ext_filter_lines)
+		{
+			my @ext_filter = split('\/', $_);
+
+			if (defined $ext_filter[1] && defined $ext_filter[2])
+			{
+				$dump_contents =~ s/$ext_filter[1]/$ext_filter[2]/mgx;
+			}
+			elsif (defined $ext_filter[1])
+			{
+				$dump_contents =~ s/$ext_filter[1]//mgx;
+			}
+		}
+	}
 
 	my $dump_file_filtered = "${dump_file}_filtered";
 	open(my $dh, '>', $dump_file_filtered)
diff --git a/src/bin/pg_upgrade/t/filter.regex b/src/bin/pg_upgrade/t/filter.regex
new file mode 100644
index 0000000000..8b4c06c5f3
--- /dev/null
+++ b/src/bin/pg_upgrade/t/filter.regex
@@ -0,0 +1,5 @@
+# examples:
+# Remove all CREATE POLICY statements
+/^CREATE\sPOLICY.*//
+# Replace REFRESH with DROP for materialized views
+/^REFRESH\s(MATERIALIZED\sVIEW)/DROP $1/
