From 30bc31540100974dd1dd63251622cd99fa7d1992 Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Tue, 30 Nov 2021 20:56:57 +0900
Subject: [PATCH] Fix regression test failure caused by commit 8d74fc96db

The tests were not considering that an error unrelated to apply
changes, e.g. "replication origin with OID 2 is already active ...",
could occur on the table sync worker before starting to apply changes.

This commit makes the queries used to check error entries check also
a prefix of the error message as well as the source of logical
replication worker (tablesync or apply) so we can check the specific
error.

Per buildfarm member sidewinder.
---
 src/test/subscription/t/026_worker_stats.pl | 34 ++++++++++++---------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/src/test/subscription/t/026_worker_stats.pl b/src/test/subscription/t/026_worker_stats.pl
index e64e0a74b8..121d789c24 100644
--- a/src/test/subscription/t/026_worker_stats.pl
+++ b/src/test/subscription/t/026_worker_stats.pl
@@ -11,26 +11,30 @@ use Test::More tests => 5;
 # Test if the error reported on pg_stat_subscription_workers view is expected.
 sub test_subscription_error
 {
-    my ($node, $relname, $xid, $expected_error, $msg) = @_;
+    my ($node, $relname, $xid, $by_apply_worker, $errmsg_prefix, $expected, $msg) = @_;
 
-    my $check_sql = qq[
-SELECT count(1) > 0 FROM pg_stat_subscription_workers
-WHERE last_error_relid = '$relname'::regclass];
-    $check_sql .= " AND last_error_xid = '$xid'::xid;" if $xid ne '';
+    # Construct the part of query used below.
+    my $part_sql = qq[
+FROM pg_stat_subscription_workers
+WHERE last_error_relid = '$relname'::regclass
+    AND starts_with(last_error_message, '$errmsg_prefix')];
+    $part_sql .= $by_apply_worker
+	? qq[ AND subrelid IS NULL]
+	: qq[ AND subrelid = '$relname'::regclass];
+    $part_sql .= qq[ AND last_error_xid = '$xid'::xid] if $xid ne '';
 
     # Wait for the error statistics to be updated.
+    my $check_sql = qq[SELECT count(1) > 0 ] . $part_sql;
     $node->poll_query_until(
 	'postgres', $check_sql,
 ) or die "Timed out while waiting for statistics to be updated";
 
-    my $result = $node->safe_psql(
-	'postgres',
+    $check_sql =
 	qq[
-SELECT subname, last_error_command, last_error_relid::regclass, last_error_count > 0
-FROM pg_stat_subscription_workers
-WHERE last_error_relid = '$relname'::regclass;
-]);
-    is($result, $expected_error, $msg);
+SELECT subname, last_error_command, last_error_relid::regclass,
+last_error_count > 0 ] . $part_sql;
+    my $result = $node->safe_psql('postgres', $check_sql);
+    is($result, $expected, $msg);
 }
 
 # Create publisher node.
@@ -117,12 +121,14 @@ INSERT INTO test_tab1 VALUES (1);
 SELECT pg_current_xact_id()::xid;
 COMMIT;
 ]);
-test_subscription_error($node_subscriber, 'test_tab1', $xid,
+test_subscription_error($node_subscriber, 'test_tab1', $xid, 1,
+			qq(duplicate key value violates unique constraint),
 			qq(tap_sub|INSERT|test_tab1|t),
 			'check the error reported by the apply worker');
 
 # Check the table sync worker's error in the view.
-test_subscription_error($node_subscriber, 'test_tab2', '',
+test_subscription_error($node_subscriber, 'test_tab2', '', 0,
+			qq(duplicate key value violates unique constraint),
 			qq(tap_sub||test_tab2|t),
 			'check the error reported by the table sync worker');
 
-- 
2.24.3 (Apple Git-128)

