From c12c8c712e641dfea6f9f960006b032201dac9a5 Mon Sep 17 00:00:00 2001
From: Stepan Neretin <slpmcf@gmail.com>
Date: Mon, 23 Nov 2026 22:56:42 +0700
Subject: [PATCH 3/3] Add TAP coverage for persistent archive_command.

Cover basic archive success, %f/%p rejection, mode toggles, retry,
helper death with outstanding work, and cancel of a hung helper.

Discussion: https://postgr.es/m/CA+Yyo5TfMpUE3s=WKrWcaYbBWbO2VNyRnRt_5hy-VPrMhjXVWg@mail.gmail.com
---
 src/test/recovery/meson.build                 |   1 +
 .../t/062_archive_command_persistent.pl       | 267 ++++++++++++++++++
 2 files changed, 268 insertions(+)
 create mode 100644 src/test/recovery/t/062_archive_command_persistent.pl

diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index cb3df6e..650a5e6 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -70,6 +70,7 @@ tests += {
       't/059_remote_apply_status_interval.pl',
       't/060_multixact_crashed_updater.pl',
       't/061_recovery_target_prefetch.pl',
+      't/062_archive_command_persistent.pl',
     ],
   },
 }
diff --git a/src/test/recovery/t/062_archive_command_persistent.pl b/src/test/recovery/t/062_archive_command_persistent.pl
new file mode 100644
index 0000000..e358dee
--- /dev/null
+++ b/src/test/recovery/t/062_archive_command_persistent.pl
@@ -0,0 +1,267 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+#
+# Tests for archive_command_persistent: one long-lived helper fed over stdin.
+#
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+use Time::HiRes qw(usleep);
+
+my $node = PostgreSQL::Test::Cluster->new('persistent');
+$node->init(has_archiving => 1, allows_streaming => 1);
+
+if ($PostgreSQL::Test::Utils::windows_os)
+{
+	$node->start;
+	my $offset = -s $node->logfile;
+	$node->append_conf('postgresql.conf',
+		'archive_command_persistent = on');
+	$node->reload;
+	$node->wait_for_log(
+		qr/"archive_command_persistent" is not supported on Windows/,
+		$offset);
+	is( $node->safe_psql('postgres', 'SHOW archive_command_persistent'),
+		'off', 'archive_command_persistent rejected on Windows');
+	$node->stop;
+	done_testing();
+	exit;
+}
+
+my $helper = $node->basedir . '/archive_persistent_helper.pl';
+my $archive_dir = $node->archive_dir;
+my $hang_log = $node->basedir . '/hang_helper.log';
+
+append_to_file(
+	$helper, <<'EOF');
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use File::Copy qw(copy);
+use IO::Handle;
+
+my $dest = $ENV{ARCHIVE_DEST} // die "ARCHIVE_DEST not set\n";
+my $delay_ms = $ENV{ARCHIVE_DELAY_MS} // 0;
+my $hang = ($ENV{ARCHIVE_HANG} // '') eq '1';
+my $fail_once = $ENV{ARCHIVE_FAIL_ONCE_FILE} // '';
+my $fail_done = 0;
+my $exit_after = $ENV{ARCHIVE_EXIT_AFTER} // 0;
+my $seen = 0;
+
+STDIN->autoflush(1);
+STDOUT->autoflush(1);
+
+if ($hang)
+{
+	my $log = $ENV{ARCHIVE_HANG_LOG} // die "ARCHIVE_HANG_LOG not set\n";
+	open my $fh, '>>', $log or die $!;
+	print $fh "$$\n";
+	close $fh;
+	sleep 1000;
+	exit 0;
+}
+
+while (my $line = <STDIN>)
+{
+	chomp $line;
+	next if $line eq '';
+	my ($cmd, $file, $path) = split /\t/, $line, 3;
+	$cmd eq 'archive' or die "bad request: $line\n";
+	defined $file and defined $path or die "bad request: $line\n";
+
+	if ($delay_ms > 0)
+	{
+		select(undef, undef, undef, $delay_ms / 1000.0);
+	}
+
+	if ($fail_once ne '' && !$fail_done && $file eq $fail_once)
+	{
+		$fail_done = 1;
+		print "archived\t$file\tretry\n";
+		next;
+	}
+
+	my $target = "$dest/$file";
+	copy($path, $target) or die "copy $path -> $target: $!\n";
+	print "archived\t$file\tok\n";
+
+	$seen++;
+	exit 0 if $exit_after > 0 && $seen >= $exit_after;
+}
+exit 0;
+EOF
+chmod 0755, $helper or die "chmod $helper: $!";
+
+my $helper_cmd = "env ARCHIVE_DEST=$archive_dir $helper";
+my $classic_cmd = "cp \"%p\" \"$archive_dir/%f\"";
+
+$node->append_conf(
+	'postgresql.conf', qq{
+archive_command_persistent = on
+archive_async_max = 4
+archive_command = '$helper_cmd'
+});
+$node->start;
+$node->safe_psql('postgres', 'CREATE TABLE t_arch(i int)');
+
+sub switch_wal
+{
+	my ($tag) = @_;
+	$node->safe_psql('postgres',
+		"INSERT INTO t_arch VALUES ($tag); SELECT pg_switch_wal()");
+}
+
+sub archived_count
+{
+	return $node->safe_psql('postgres',
+		q{SELECT archived_count FROM pg_stat_archiver});
+}
+
+sub wait_archived_gt
+{
+	my ($before, $msg) = @_;
+	ok(
+		$node->poll_query_until(
+			'postgres',
+			qq{SELECT archived_count > $before FROM pg_stat_archiver},
+			't'),
+		$msg);
+}
+
+sub wait_archiver_alive
+{
+	$node->poll_query_until(
+		'postgres',
+		q{SELECT count(*) = 1 FROM pg_stat_activity WHERE backend_type = 'archiver'},
+		't')
+	  or die 'timed out waiting for archiver';
+}
+
+sub reload_persistent
+{
+	my ($on, $extra) = @_;
+	my $val = $on ? 'on' : 'off';
+	my $offset = -s $node->logfile;
+	$node->append_conf(
+		'postgresql.conf',
+		"archive_command_persistent = $val\n" . ($extra // ''));
+	$node->reload;
+	$node->wait_for_log(
+		qr/restarting archiver process because value of "archive_command_persistent" was changed/,
+		$offset);
+	wait_archiver_alive();
+}
+
+sub reload_archive_command
+{
+	my ($cmd) = @_;
+	my $offset = -s $node->logfile;
+	$node->append_conf('postgresql.conf',
+		"archive_command = '$cmd'\n");
+	$node->reload;
+	$node->wait_for_log(
+		qr/restarting archiver process because value of "archive_command" was changed/,
+		$offset);
+	wait_archiver_alive();
+}
+
+# Basic persistent archive.
+for (1 .. 4) { switch_wal($_); }
+ok(
+	$node->poll_query_until(
+		'postgres', q{SELECT archived_count >= 4 FROM pg_stat_archiver}, 't'),
+	'persistent archive_command archives WAL');
+
+# %f/%p are rejected while persistent is on.
+{
+	my $offset = -s $node->logfile;
+	$node->append_conf('postgresql.conf',
+		"archive_command = 'cp \"%p\" \"/tmp/%f\"'\n");
+	$node->reload;
+	$node->wait_for_log(
+		qr/restarting archiver process because value of "archive_command" was changed/,
+		$offset);
+	$node->wait_for_log(
+		qr/archive_command.*must not contain %f or %p/,
+		$offset);
+	reload_archive_command($helper_cmd);
+}
+
+# Toggle persistent off/on and keep archiving.
+reload_persistent(0, "archive_command = '$classic_cmd'\n");
+my $before = archived_count();
+switch_wal(100);
+wait_archived_gt($before, 'classic archive_command works after persistent=off');
+
+reload_persistent(1, "archive_command = '$helper_cmd'\n");
+$before = archived_count();
+switch_wal(101);
+wait_archived_gt($before, 'persistent mode archives again after re-enable');
+
+# Helper can ask for retry once.
+{
+	my $seg = $node->safe_psql('postgres',
+		q{SELECT pg_walfile_name(pg_current_wal_lsn())});
+	my $retry_cmd =
+		"env ARCHIVE_DEST=$archive_dir ARCHIVE_FAIL_ONCE_FILE=$seg $helper";
+	reload_archive_command($retry_cmd);
+	$before = archived_count();
+	switch_wal(102);
+	wait_archived_gt($before, 'retry response still allows eventual success');
+	reload_archive_command($helper_cmd);
+}
+
+# Helper death with outstanding submits still drains via RETRY.
+{
+	my $die_cmd =
+		"env ARCHIVE_DEST=$archive_dir ARCHIVE_DELAY_MS=200 ARCHIVE_EXIT_AFTER=1 $helper";
+	reload_archive_command($die_cmd);
+	$before = archived_count();
+	for my $i (1 .. 4) { switch_wal(150 + $i); }
+	ok(
+		$node->poll_query_until(
+			'postgres',
+			qq{SELECT archived_count >= $before + 4 FROM pg_stat_archiver},
+			't'),
+		'helper death mid-flight still archives via RETRY');
+	reload_archive_command($helper_cmd);
+}
+
+# Hung helper is cancelled on shutdown.
+#
+# Trailing "; true" keeps /bin/sh from exec'ing the helper (bash would
+# otherwise replace itself).  That matches Debian's dash, which forks even
+# for a simple command, and ensures cancel must kill the process group.
+unlink $hang_log if -f $hang_log;
+my $hang_cmd =
+	"env ARCHIVE_DEST=$archive_dir ARCHIVE_HANG=1 ARCHIVE_HANG_LOG=$hang_log $helper; true";
+reload_persistent(0, "archive_command = '$classic_cmd'\n");
+reload_persistent(1, "archive_command = '$hang_cmd'\n");
+switch_wal(200);
+switch_wal(201);
+
+my $hang_pid;
+for (1 .. 10 * $PostgreSQL::Test::Utils::timeout_default)
+{
+	if (-f $hang_log)
+	{
+		($hang_pid) = grep { length } split /\n/, slurp_file($hang_log);
+		last if defined $hang_pid;
+	}
+	usleep(100_000);
+}
+ok(defined $hang_pid, 'hung persistent helper started');
+
+my $offset = -s $node->logfile;
+ok($node->stop('fast', timeout => 120),
+	'stop with hung persistent archive_command');
+like(
+	slurp_file($node->logfile, $offset),
+	qr/canceling in flight archive commands due to shutdown/,
+	'shutdown canceled in-flight persistent archive work');
+ok(!kill(0, $hang_pid), "hang helper pid $hang_pid gone after stop")
+  if defined $hang_pid;
+
+done_testing();
-- 
2.56.1

