v1-0002-Add-test-for-pg_recvlogical-reconnection.patch

text/plain

Filename: v1-0002-Add-test-for-pg_recvlogical-reconnection.patch
Type: text/plain
Part: 0
Message: Re: pg_recvlogical: Prevent flushed data from being re-sent after restarting replication
From 34842f494304f323622a5c12d46300634c0e839b Mon Sep 17 00:00:00 2001
From: Mircea Cadariu <cadariu.mircea@gmail.com>
Date: Wed, 26 Nov 2025 12:44:32 +0000
Subject: [PATCH v1 2/2] add test for pg_recvlogical reconnection

---
 src/bin/pg_basebackup/t/030_pg_recvlogical.pl | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/src/bin/pg_basebackup/t/030_pg_recvlogical.pl b/src/bin/pg_basebackup/t/030_pg_recvlogical.pl
index 1b7a6f6f43..255c361aa6 100644
--- a/src/bin/pg_basebackup/t/030_pg_recvlogical.pl
+++ b/src/bin/pg_basebackup/t/030_pg_recvlogical.pl
@@ -151,4 +151,67 @@ my $result = $node->safe_psql('postgres',
 );
 is($result, 't', "failover is enabled for the new slot");
 
+my $outfile = $node->basedir . '/reconnect.out';
+
+$node->command_ok(
+    [
+        'pg_recvlogical',
+        '--slot' => 'reconnect_test',
+        '--dbname' => $node->connstr('postgres'),
+        '--create-slot',
+    ],
+    'slot created for reconnection test');
+
+$node->safe_psql('postgres', 'CREATE TABLE t(x int);');
+$node->safe_psql('postgres', 'INSERT INTO t VALUES (1);');
+
+my $recv = IPC::Run::start [
+    'pg_recvlogical',
+    '--slot', 'reconnect_test',
+    '--dbname', $node->connstr('postgres'),
+    '--start',
+    '--file', $outfile,
+    '--fsync-interval', '1',
+    '--status-interval', '100',
+    '--verbose'
+], '>', \my $out, '2>', \my $err;
+
+# Wait for file to contain first inserts
+$node->poll_query_until('postgres',
+    "SELECT (SELECT pg_read_file('$outfile') ~ 'INSERT') AS first_insert");
+
+# Terminate the backend
+my $backend_pid = $node->safe_psql('postgres',
+    "SELECT active_pid FROM pg_replication_slots WHERE slot_name = 'reconnect_test'");
+$node->safe_psql('postgres', "SELECT pg_terminate_backend($backend_pid)");
+
+# Wait for reconnection
+$node->poll_query_until('postgres',
+    "SELECT active_pid IS NOT NULL AND active_pid != $backend_pid FROM pg_replication_slots WHERE slot_name = 'reconnect_test'");
+
+# Insert after reconnection
+$node->safe_psql('postgres', 'INSERT INTO t VALUES (2);');
+
+# Wait for file to contain both inserts
+$node->poll_query_until('postgres',
+    "SELECT (SELECT pg_read_file('$outfile') ~ 'INSERT.*INSERT') AS has_both");
+
+$recv->signal('TERM');
+$recv->finish();
+
+open(my $file, '<', $outfile);
+my $count = () = do { local $/; <$file> } =~ /INSERT/g;
+close($file);
+
+cmp_ok($count, '==', 2, 'two INSERTs');
+
+$node->command_ok(
+    [
+        'pg_recvlogical',
+        '--slot' => 'reconnect_test',
+        '--dbname' => $node->connstr('postgres'),
+        '--drop-slot'
+    ],
+    'reconnect_test slot dropped');
+
 done_testing();
-- 
2.39.5 (Apple Git-154)