From 8cc72e4455b724c9e891b2c71924102e192657cd Mon Sep 17 00:00:00 2001
From: Andrey Borodin <x4m@flight.local>
Date: Sat, 13 Nov 2021 23:40:30 +0500
Subject: [PATCH] Get rid of background_pgbench() in CIC tests

This is an adaptation of commit 7f580aa for the REL_11_STABLE
that lacks \gset pgbench command. To emulate conditional REINDEX
depending on result of advisory lock attemp we just do REINDEX
only when :client_id = 0.
---
 src/bin/pgbench/t/022_cic.pl     | 40 ++++++++++----------------
 src/bin/pgbench/t/023_cic_2pc.pl | 46 ++++++++++--------------------
 src/test/perl/PostgresNode.pm    | 49 --------------------------------
 3 files changed, 30 insertions(+), 105 deletions(-)

diff --git a/src/bin/pgbench/t/022_cic.pl b/src/bin/pgbench/t/022_cic.pl
index a1cda5b47ae..55288f0058f 100644
--- a/src/bin/pgbench/t/022_cic.pl
+++ b/src/bin/pgbench/t/022_cic.pl
@@ -9,7 +9,7 @@ use Config;
 use PostgresNode;
 use TestLib;
 
-use Test::More tests => 4;
+use Test::More tests => 3;
 
 my ($node, $result);
 
@@ -42,26 +42,11 @@ $node->safe_psql(
 ));
 
 #
-# Stress CIC with pgbench
+# Stress CIC with pgbench.
 #
-
-# Run background pgbench with CIC. We cannot mix-in this script into single
-# pgbench: CIC will deadlock with itself occasionally.
-my $pgbench_out   = '';
-my $pgbench_timer = IPC::Run::timeout(180);
-my $pgbench_h     = $node->background_pgbench(
-	'--no-vacuum --client=1 --transactions=200',
-	{
-		'002_pgbench_concurrent_cic' => q(
-			DROP INDEX CONCURRENTLY idx;
-			CREATE INDEX CONCURRENTLY idx ON tbl(i);
-			BEGIN ISOLATION LEVEL REPEATABLE READ;
-			SELECT heapallindexed();
-			ROLLBACK;
-		   )
-	},
-	\$pgbench_out,
-	$pgbench_timer);
+# pgbench might try to launch more than one instance of the CIC
+# transaction concurrently.  That would deadlock, so use 0 = :client_id
+# to ensure only one CIC runs at a time.
 
 # Run pgbench.
 $node->pgbench(
@@ -71,7 +56,7 @@ $node->pgbench(
 	[qr{^$}],
 	'concurrent INSERTs',
 	{
-		'002_pgbench_concurrent_transaction' => q(
+		'001_pgbench_concurrent_transaction' => q(
 			BEGIN;
 			INSERT INTO tbl VALUES(0);
 			COMMIT;
@@ -81,13 +66,18 @@ $node->pgbench(
 			SAVEPOINT s1;
 			INSERT INTO tbl VALUES(0);
 			COMMIT;
+		  ),
+		'003_pgbench_concurrent_cic' => q(
+			\if 0 = :client_id
+				DROP INDEX CONCURRENTLY idx;
+				CREATE INDEX CONCURRENTLY idx ON tbl(i);
+				BEGIN ISOLATION LEVEL REPEATABLE READ;
+				SELECT heapallindexed();
+				ROLLBACK;
+			\endif
 		  )
 	});
 
-$pgbench_h->pump_nb;
-$pgbench_h->finish();
-unlike($pgbench_out, qr/aborted in command/, "pgbench with CIC works");
-
 # done
 $node->stop;
 done_testing();
diff --git a/src/bin/pgbench/t/023_cic_2pc.pl b/src/bin/pgbench/t/023_cic_2pc.pl
index e29c2564b77..81f0f75349f 100644
--- a/src/bin/pgbench/t/023_cic_2pc.pl
+++ b/src/bin/pgbench/t/023_cic_2pc.pl
@@ -9,7 +9,7 @@ use Config;
 use PostgresNode;
 use TestLib;
 
-use Test::More tests => 6;
+use Test::More tests => 5;
 
 my ($node, $result);
 
@@ -149,29 +149,15 @@ is($result, '0', 'all indexed after 2PC and restart');
 
 
 #
-# Stress CIC+2PC with pgbench
+# Stress CIC+2PC with pgbench.
 #
+# pgbench might try to launch more than one instance of the CIC
+# transaction concurrently.  That would deadlock, so use 0 = :client_id
+# to ensure only one CIC runs at a time.
 
 # Fix broken index first
 $node->safe_psql('postgres', q(REINDEX TABLE tbl;));
 
-# Run background pgbench with CIC. We cannot mix-in this script into single
-# pgbench: CIC will deadlock with itself occasionally.
-my $pgbench_out   = '';
-my $pgbench_timer = IPC::Run::timeout(180);
-my $pgbench_h     = $node->background_pgbench(
-	'--no-vacuum --client=1 --transactions=100',
-	{
-		'002_pgbench_concurrent_cic' => q(
-			DROP INDEX CONCURRENTLY idx;
-			CREATE INDEX CONCURRENTLY idx ON tbl(i);
-			BEGIN ISOLATION LEVEL REPEATABLE READ;
-			SELECT heapallindexed();
-			ROLLBACK;
-		   )
-	},
-	\$pgbench_out,
-	$pgbench_timer);
 
 # Run pgbench.
 $node->pgbench(
@@ -179,27 +165,25 @@ $node->pgbench(
 	0,
 	[qr{actually processed}],
 	[qr{^$}],
-	'concurrent INSERTs w/ 2PC',
+	'concurrent INSERTs w/ 2PC and CIC',
 	{
-		'002_pgbench_concurrent_2pc' => q(
+		'001_pgbench_concurrent_2pc' => q(
 			BEGIN;
 			INSERT INTO tbl VALUES(0);
 			PREPARE TRANSACTION 'c:client_id';
 			COMMIT PREPARED 'c:client_id';
 		  ),
-		'002_pgbench_concurrent_2pc_savepoint' => q(
-			BEGIN;
-			SAVEPOINT s1;
-			INSERT INTO tbl VALUES(0);
-			PREPARE TRANSACTION 'c:client_id';
-			COMMIT PREPARED 'c:client_id';
+		'002_pgbench_concurrent_cic' => q(
+			\if 0 = :client_id
+				DROP INDEX CONCURRENTLY idx;
+				CREATE INDEX CONCURRENTLY idx ON tbl(i);
+				BEGIN ISOLATION LEVEL REPEATABLE READ;
+				SELECT heapallindexed();
+				ROLLBACK;
+			\endif
 		  )
 	});
 
-$pgbench_h->pump_nb;
-$pgbench_h->finish();
-unlike($pgbench_out, qr/aborted in command/, "pgbench with CIC works");
-
 # done
 $node->stop;
 done_testing();
diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
index 76ef3826d15..bf4b9f96905 100644
--- a/src/test/perl/PostgresNode.pm
+++ b/src/test/perl/PostgresNode.pm
@@ -1659,55 +1659,6 @@ sub pgbench
 
 =pod
 
-=item $node->background_pgbench($opts, $files, \$stdout, $timer) => harness
-
-Invoke B<pgbench> and return an IPC::Run harness object.  The process's stdin
-is empty, and its stdout and stderr go to the $stdout scalar reference.  This
-allows the caller to act on other parts of the system while B<pgbench> is
-running.  Errors from B<pgbench> are the caller's problem.
-
-The specified timer object is attached to the harness, as well.  It's caller's
-responsibility to select the timeout length, and to restart the timer after
-each command if the timeout is per-command.
-
-Be sure to "finish" the harness when done with it.
-
-=over
-
-=item $opts
-
-Options as a string to be split on spaces.
-
-=item $files
-
-Reference to filename/contents dictionary.
-
-=back
-
-=cut
-
-sub background_pgbench
-{
-	my ($self, $opts, $files, $stdout, $timer) = @_;
-
-	my @cmd =
-	  ('pgbench', split(/\s+/, $opts), $self->_pgbench_make_files($files));
-
-	local $ENV{PGHOST} = $self->host;
-	local $ENV{PGPORT} = $self->port;
-
-	my $stdin = "";
-	# IPC::Run would otherwise append to existing contents:
-	$$stdout = "" if ref($stdout);
-
-	my $harness = IPC::Run::start \@cmd, '<', \$stdin, '>', $stdout, '2>&1',
-	  $timer;
-
-	return $harness;
-}
-
-=pod
-
 =item $node->poll_query_until($dbname, $query [, $expected ])
 
 Run B<$query> repeatedly, until it returns the B<$expected> result
-- 
2.33.1

