From 6b64542ea666784b2574167b1429e4a72adbfa7b Mon Sep 17 00:00:00 2001
From: Stepan Neretin <slpmcf@gmail.com>
Date: Wed, 4 Nov 2026 13:35:40 +0700
Subject: [PATCH 2/2] Add archive_command_jobs.

Allow up to eight concurrent short lived archive_command children on
Unix.  Default 1 keeps the sync system() path.  GUC changes restart the
archiver.  Windows rejects values above 1.  TAP covers reload, failure,
hang jobs, and shutdown cancel.
---
 src/backend/access/transam/xlog.c             |  15 +
 src/backend/archive/shell_archive.c           | 348 +++++++++++++++---
 src/backend/postmaster/pgarch.c               |  10 +
 src/backend/utils/misc/guc_parameters.dat     |  10 +
 src/backend/utils/misc/postgresql.conf.sample |   1 +
 src/include/access/xlog.h                     |   1 +
 src/include/utils/guc_hooks.h                 |   2 +
 src/test/recovery/meson.build                 |   1 +
 .../recovery/t/062_archive_command_jobs.pl    | 198 ++++++++++
 9 files changed, 533 insertions(+), 53 deletions(-)
 create mode 100644 src/test/recovery/t/062_archive_command_jobs.pl

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fd37ea4f607..f99f379f383 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -125,6 +125,7 @@ int			XLOGbuffers = -1;
 int			XLogArchiveTimeout = 0;
 int			XLogArchiveMode = ARCHIVE_MODE_OFF;
 char	   *XLogArchiveCommand = NULL;
+int			XLogArchiveCommandJobs = 1;
 bool		EnableHotStandby = false;
 bool		fullPageWrites = true;
 bool		wal_log_hints = false;
@@ -5446,6 +5447,20 @@ show_archive_command(void)
 		return "(disabled)";
 }
 
+bool
+check_archive_command_jobs(int *newval, void **extra, GucSource source)
+{
+#ifdef WIN32
+	if (*newval > 1)
+	{
+		GUC_check_errcode(ERRCODE_FEATURE_NOT_SUPPORTED);
+		GUC_check_errmsg("\"archive_command_jobs\" greater than 1 is not supported on Windows");
+		return false;
+	}
+#endif
+	return true;
+}
+
 /*
  * GUC show_hook for in_hot_standby
  */
diff --git a/src/backend/archive/shell_archive.c b/src/backend/archive/shell_archive.c
index 0b427a68809..b1f4ab1b3b9 100644
--- a/src/backend/archive/shell_archive.c
+++ b/src/backend/archive/shell_archive.c
@@ -15,32 +15,84 @@
  */
 #include "postgres.h"
 
+#include <signal.h>
 #include <sys/wait.h>
+#ifndef WIN32
+#include <unistd.h>
+#endif
 
 #include "access/xlog.h"
+#include "access/xlog_internal.h"
 #include "archive/archive_module.h"
 #include "archive/shell_archive.h"
 #include "common/percentrepl.h"
+#include "libpq/pqsignal.h"
 #include "pgstat.h"
+#ifndef WIN32
+#include "postmaster/fork_process.h"
+#endif
+#include "miscadmin.h"
+#include "storage/latch.h"
 #include "utils/wait_event.h"
 
+/* Keep in sync with archive_command_jobs GUC maximum. */
+#define ARCHIVE_COMMAND_JOBS_MAX	8
+
+#ifndef WIN32
+static struct
+{
+	pid_t		pid;
+	char		file[MAX_XFN_CHARS + 1];
+}				shell_jobs[ARCHIVE_COMMAND_JOBS_MAX];
+static int	shell_nactive = 0;
+static char shell_done_file[MAX_XFN_CHARS + 1];
+static bool shell_sigchld_armed = false;
+
+static void shell_sigchld_handler(SIGNAL_ARGS);
+static void shell_prepare_child(void);
+static void shell_kill_job(pid_t pid);
+#endif
+
 static bool shell_archive_configured(ArchiveModuleState *state);
 static bool shell_archive_file(ArchiveModuleState *state,
 							   const char *file,
 							   const char *path);
 static void shell_archive_shutdown(ArchiveModuleState *state);
+#ifndef WIN32
+static bool shell_archive_submit(ArchiveModuleState *state,
+								 const char *file,
+								 const char *path);
+static bool shell_archive_poll(ArchiveModuleState *state,
+							   const char **file,
+							   ArchiveResultStatus *status,
+							   long timeout_ms);
+static void shell_archive_cancel(ArchiveModuleState *state);
+#endif
 
-static const ArchiveModuleCallbacks shell_archive_callbacks = {
-	.startup_cb = NULL,
+static const ArchiveModuleCallbacks shell_archive_sync_callbacks = {
 	.check_configured_cb = shell_archive_configured,
 	.archive_file_cb = shell_archive_file,
 	.shutdown_cb = shell_archive_shutdown
 };
 
+#ifndef WIN32
+static const ArchiveModuleCallbacks shell_archive_async_callbacks = {
+	.check_configured_cb = shell_archive_configured,
+	.shutdown_cb = shell_archive_shutdown,
+	.submit_cb = shell_archive_submit,
+	.poll_cb = shell_archive_poll,
+	.cancel_cb = shell_archive_cancel
+};
+#endif
+
 const ArchiveModuleCallbacks *
 shell_archive_init(void)
 {
-	return &shell_archive_callbacks;
+#ifndef WIN32
+	if (XLogArchiveCommandJobs > 1)
+		return &shell_archive_async_callbacks;
+#endif
+	return &shell_archive_sync_callbacks;
 }
 
 static bool
@@ -54,13 +106,11 @@ shell_archive_configured(ArchiveModuleState *state)
 	return false;
 }
 
-static bool
-shell_archive_file(ArchiveModuleState *state, const char *file,
-				   const char *path)
+static char *
+shell_build_command(const char *file, const char *path)
 {
-	char	   *xlogarchcmd;
 	char	   *nativePath = NULL;
-	int			rc;
+	char	   *cmd;
 
 	if (path)
 	{
@@ -68,76 +118,268 @@ shell_archive_file(ArchiveModuleState *state, const char *file,
 		make_native_path(nativePath);
 	}
 
-	xlogarchcmd = replace_percent_placeholders(XLogArchiveCommand,
-											   "archive_command", "fp",
-											   file, nativePath);
+	cmd = replace_percent_placeholders(XLogArchiveCommand,
+									   "archive_command", "fp",
+									   file, nativePath);
+	if (nativePath)
+		pfree(nativePath);
+	return cmd;
+}
+
+static void
+shell_report_failure(const char *cmd, int rc)
+{
+	int			lev = wait_result_is_any_signal(rc, true) ? FATAL : LOG;
+
+	if (WIFEXITED(rc))
+	{
+		ereport(lev,
+				(errmsg("archive command failed with exit code %d",
+						WEXITSTATUS(rc)),
+				 errdetail("The failed archive command was: %s",
+						   cmd)));
+	}
+	else if (WIFSIGNALED(rc))
+	{
+#if defined(WIN32)
+		ereport(lev,
+				(errmsg("archive command was terminated by exception 0x%X",
+						WTERMSIG(rc)),
+				 errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
+				 errdetail("The failed archive command was: %s",
+						   cmd)));
+#else
+		ereport(lev,
+				(errmsg("archive command was terminated by signal %d: %s",
+						WTERMSIG(rc), pg_strsignal(WTERMSIG(rc))),
+				 errdetail("The failed archive command was: %s",
+						   cmd)));
+#endif
+	}
+	else
+	{
+		ereport(lev,
+				(errmsg("archive command exited with unrecognized status %d",
+						rc),
+				 errdetail("The failed archive command was: %s",
+						   cmd)));
+	}
+}
+
+static bool
+shell_archive_file(ArchiveModuleState *state, const char *file,
+				   const char *path)
+{
+	char	   *cmd = shell_build_command(file, path);
+	int			rc;
 
 	ereport(DEBUG3,
 			(errmsg_internal("executing archive command \"%s\"",
-							 xlogarchcmd)));
+							 cmd)));
 
 	fflush(NULL);
 	pgstat_report_wait_start(WAIT_EVENT_ARCHIVE_COMMAND);
-	rc = system(xlogarchcmd);
+	rc = system(cmd);
 	pgstat_report_wait_end();
 
 	if (rc != 0)
+		shell_report_failure(cmd, rc);
+	else
+		elog(DEBUG1, "archived write-ahead log file \"%s\"", file);
+
+	pfree(cmd);
+	return (rc == 0);
+}
+
+#ifndef WIN32
+static void
+shell_report_file_failure(const char *file, int rc)
+{
+	char		pathname[MAXPGPATH];
+	char	   *cmd;
+
+	snprintf(pathname, MAXPGPATH, XLOGDIR "/%s", file);
+	cmd = shell_build_command(file, pathname);
+	shell_report_failure(cmd, rc);
+	pfree(cmd);
+}
+
+static bool
+shell_archive_submit(ArchiveModuleState *state, const char *file,
+					 const char *path)
+{
+	char	   *cmd;
+	pid_t		pid;
+
+	if (shell_nactive >= XLogArchiveCommandJobs)
+		return false;
+
+	cmd = shell_build_command(file, path);
+	ereport(DEBUG3,
+			(errmsg_internal("submitting archive command \"%s\"",
+							 cmd)));
+
+	pid = fork_process();
+	if (pid == 0)
+	{
+		if (setpgid(0, 0) < 0)
+			_exit(1);
+		shell_prepare_child();
+		execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
+		_exit(127);
+	}
+	if (pid < 0)
 	{
-		/*
-		 * If either the shell itself, or a called command, died on a signal,
-		 * abort the archiver.  We do this because system() ignores SIGINT and
-		 * SIGQUIT while waiting; so a signal is very likely something that
-		 * should have interrupted us too.  Also die if the shell got a hard
-		 * "command not found" type of error.  If we overreact it's no big
-		 * deal, the postmaster will just start the archiver again.
-		 */
-		int			lev = wait_result_is_any_signal(rc, true) ? FATAL : LOG;
-
-		if (WIFEXITED(rc))
+		ereport(LOG,
+				(errmsg("could not fork archive command: %m"),
+				 errdetail("The failed archive command was: %s",
+						   cmd)));
+		pfree(cmd);
+		return false;
+	}
+
+	/* EACCES means the child already set its process group. */
+	if (setpgid(pid, pid) < 0 && errno != EACCES)
+		ereport(LOG,
+				(errmsg("could not set process group for archive command: %m")));
+
+	shell_jobs[shell_nactive].pid = pid;
+	strlcpy(shell_jobs[shell_nactive].file, file,
+			sizeof(shell_jobs[shell_nactive].file));
+	shell_nactive++;
+	pfree(cmd);
+	return true;
+}
+
+static bool
+shell_try_reap(const char **file, ArchiveResultStatus *status)
+{
+	int			i;
+
+	for (i = 0; i < shell_nactive; i++)
+	{
+		int			waitstatus;
+		pid_t		done;
+
+		for (;;)
+		{
+			done = waitpid(shell_jobs[i].pid, &waitstatus, WNOHANG);
+			if (!(done < 0 && errno == EINTR))
+				break;
+		}
+
+		if (done == 0)
+			continue;
+
+		strlcpy(shell_done_file, shell_jobs[i].file, sizeof(shell_done_file));
+		*file = shell_done_file;
+		shell_jobs[i] = shell_jobs[--shell_nactive];
+
+		if (done < 0)
 		{
-			ereport(lev,
-					(errmsg("archive command failed with exit code %d",
-							WEXITSTATUS(rc)),
-					 errdetail("The failed archive command was: %s",
-							   xlogarchcmd)));
+			ereport(LOG,
+					(errmsg("could not wait for archive command: %m"),
+					 errdetail("While waiting for write-ahead log file \"%s\"",
+							   shell_done_file)));
+			*status = ARCHIVE_RESULT_RETRY;
 		}
-		else if (WIFSIGNALED(rc))
+		else if (waitstatus == 0)
 		{
-#if defined(WIN32)
-			ereport(lev,
-					(errmsg("archive command was terminated by exception 0x%X",
-							WTERMSIG(rc)),
-					 errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
-					 errdetail("The failed archive command was: %s",
-							   xlogarchcmd)));
-#else
-			ereport(lev,
-					(errmsg("archive command was terminated by signal %d: %s",
-							WTERMSIG(rc), pg_strsignal(WTERMSIG(rc))),
-					 errdetail("The failed archive command was: %s",
-							   xlogarchcmd)));
-#endif
+			*status = ARCHIVE_RESULT_SUCCESS;
+			elog(DEBUG1, "archived write-ahead log file \"%s\"",
+				 shell_done_file);
 		}
 		else
 		{
-			ereport(lev,
-					(errmsg("archive command exited with unrecognized status %d",
-							rc),
-					 errdetail("The failed archive command was: %s",
-							   xlogarchcmd)));
+			if (wait_result_is_any_signal(waitstatus, true))
+				shell_archive_cancel(NULL);
+			shell_report_file_failure(shell_done_file, waitstatus);
+			*status = ARCHIVE_RESULT_RETRY;
 		}
-		pfree(xlogarchcmd);
+		return true;
+	}
+
+	return false;
+}
+
+static void
+shell_sigchld_handler(SIGNAL_ARGS)
+{
+	int			save_errno = errno;
 
+	SetLatch(MyLatch);
+	errno = save_errno;
+}
+
+static void
+shell_prepare_child(void)
+{
+	pqsignal(SIGHUP, PG_SIG_DFL);
+	pqsignal(SIGINT, PG_SIG_DFL);
+	pqsignal(SIGTERM, PG_SIG_DFL);
+	pqsignal(SIGQUIT, PG_SIG_DFL);
+	pqsignal(SIGALRM, PG_SIG_DFL);
+	pqsignal(SIGPIPE, PG_SIG_DFL);
+	pqsignal(SIGUSR1, PG_SIG_DFL);
+	pqsignal(SIGUSR2, PG_SIG_DFL);
+	pqsignal(SIGCHLD, PG_SIG_DFL);
+	sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
+}
+
+static void
+shell_kill_job(pid_t pid)
+{
+	int			status;
+
+	if (kill(-pid, SIGKILL) < 0 && errno != ESRCH)
+		kill(pid, SIGKILL);
+
+	while (waitpid(pid, &status, 0) < 0 && errno == EINTR)
+		;
+}
+
+static bool
+shell_archive_poll(ArchiveModuleState *state, const char **file,
+				   ArchiveResultStatus *status, long timeout_ms)
+{
+	if (shell_try_reap(file, status))
+		return true;
+	if (timeout_ms <= 0 || shell_nactive <= 0)
 		return false;
+
+	if (!shell_sigchld_armed)
+	{
+		pqsignal(SIGCHLD, shell_sigchld_handler);
+		shell_sigchld_armed = true;
 	}
-	pfree(xlogarchcmd);
+	if (shell_try_reap(file, status))
+		return true;
 
-	elog(DEBUG1, "archived write-ahead log file \"%s\"", file);
-	return true;
+	(void) WaitLatch(MyLatch,
+					 WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
+					 timeout_ms,
+					 WAIT_EVENT_ARCHIVE_COMMAND);
+	ResetLatch(MyLatch);
+
+	return shell_try_reap(file, status);
 }
 
+static void
+shell_archive_cancel(ArchiveModuleState *state)
+{
+	while (shell_nactive > 0)
+	{
+		shell_nactive--;
+		shell_kill_job(shell_jobs[shell_nactive].pid);
+	}
+}
+#endif															/* !WIN32 */
+
 static void
 shell_archive_shutdown(ArchiveModuleState *state)
 {
+#ifndef WIN32
+	shell_archive_cancel(state);
+#endif
 	elog(DEBUG1, "archiver process shutting down");
 }
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index 930e7a2f559..4d806597e97 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -147,6 +147,7 @@ typedef struct ArchInFlight
 static ArchInFlight arch_inflight[NUM_FILES_PER_DIRECTORY_SCAN];
 static int	arch_inflight_count = 0;
 static bool archiver_use_async = false;
+static int	archiver_shell_jobs = 1;
 
 /*
  * Flags set by interrupt handlers for later service in the main loop.
@@ -1287,6 +1288,14 @@ ProcessPgArchInterrupts(void)
 			proc_exit(0);
 		}
 
+		if (XLogArchiveLibrary[0] == '\0' &&
+			XLogArchiveCommandJobs != archiver_shell_jobs)
+		{
+			ereport(LOG,
+					(errmsg("restarting archiver process because value of "
+							"\"archive_command_jobs\" was changed")));
+			proc_exit(0);
+		}
 	}
 }
 
@@ -1329,6 +1338,7 @@ LoadArchiveLibrary(void)
 				(errmsg("archive modules must provide both submit and poll callbacks for asynchronous archiving")));
 
 	archiver_use_async = (ArchiveCallbacks->submit_cb != NULL);
+	archiver_shell_jobs = XLogArchiveCommandJobs;
 	if (archiver_use_async)
 	{
 		if (ArchiveCallbacks->archive_file_cb != NULL)
diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat
index 8a8f1cca097..0e67e1d32c8 100644
--- a/src/backend/utils/misc/guc_parameters.dat
+++ b/src/backend/utils/misc/guc_parameters.dat
@@ -81,6 +81,16 @@
   show_hook => 'show_archive_command',
 },
 
+{ name => 'archive_command_jobs', type => 'int', context => 'PGC_SIGHUP', group => 'WAL_ARCHIVING',
+  short_desc => 'Sets the maximum number of concurrent archive_command processes.',
+  long_desc => 'Values greater than 1 are not supported on Windows.',
+  variable => 'XLogArchiveCommandJobs',
+  boot_val => '1',
+  min => '1',
+  max => '8',
+  check_hook => 'check_archive_command_jobs',
+},
+
 { name => 'archive_library', type => 'string', context => 'PGC_SIGHUP', group => 'WAL_ARCHIVING',
   short_desc => 'Sets the library that will be called to archive a WAL file.',
   long_desc => 'An empty string means use "archive_command".',
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index b492b3af06c..90226cbe631 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -301,6 +301,7 @@
                                 # placeholders: %p = path of file to archive
                                 #               %f = file name only
                                 # e.g. 'test ! -f "/mnt/server/archivedir/%f" && cp "%p" "/mnt/server/archivedir/%f"'
+#archive_command_jobs = 1       # max concurrent archive_command processes (Unix)
 #archive_timeout = 0            # force a WAL file switch after this
                                 # number of seconds; 0 disables
 
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index 2bf5714b51e..c3a6f73fd1a 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -44,6 +44,7 @@ extern PGDLLIMPORT int XLOGbuffers;
 extern PGDLLIMPORT int XLogArchiveTimeout;
 extern PGDLLIMPORT int wal_retrieve_retry_interval;
 extern PGDLLIMPORT char *XLogArchiveCommand;
+extern PGDLLIMPORT int XLogArchiveCommandJobs;
 extern PGDLLIMPORT bool EnableHotStandby;
 extern PGDLLIMPORT bool fullPageWrites;
 extern PGDLLIMPORT bool wal_log_hints;
diff --git a/src/include/utils/guc_hooks.h b/src/include/utils/guc_hooks.h
index 8e3c067f760..10fdd6040f6 100644
--- a/src/include/utils/guc_hooks.h
+++ b/src/include/utils/guc_hooks.h
@@ -29,6 +29,8 @@ extern bool check_application_name(char **newval, void **extra,
 								   GucSource source);
 extern void assign_application_name(const char *newval, void *extra);
 extern const char *show_archive_command(void);
+extern bool check_archive_command_jobs(int *newval, void **extra,
+									   GucSource source);
 extern bool check_autovacuum_work_mem(int *newval, void **extra,
 									  GucSource source);
 extern bool check_vacuum_buffer_usage_limit(int *newval, void **extra,
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index cb3df6ed895..811ca481f86 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_jobs.pl',
     ],
   },
 }
diff --git a/src/test/recovery/t/062_archive_command_jobs.pl b/src/test/recovery/t/062_archive_command_jobs.pl
new file mode 100644
index 00000000000..3a458d542ee
--- /dev/null
+++ b/src/test/recovery/t/062_archive_command_jobs.pl
@@ -0,0 +1,198 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+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('jobs');
+$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_jobs = 2');
+	$node->reload;
+	$node->wait_for_log(
+		qr/"archive_command_jobs" greater than 1 is not supported on Windows/,
+		$offset);
+	is( $node->safe_psql('postgres', 'SHOW archive_command_jobs'),
+		'1', 'archive_command_jobs > 1 rejected on Windows');
+	$node->stop;
+	done_testing();
+	exit;
+}
+
+$node->append_conf('postgresql.conf', 'archive_command_jobs = 2');
+$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 reload_jobs
+{
+	my ($jobs, $extra) = @_;
+	my $offset = -s $node->logfile;
+	$node->append_conf(
+		'postgresql.conf',
+		qq{archive_command_jobs = $jobs\n} . ($extra // ''));
+	$node->reload;
+	$node->wait_for_log(
+		qr/restarting archiver process because value of "archive_command_jobs" was changed/,
+		$offset);
+	$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 after archive_command_jobs change';
+}
+
+sub wait_for_hang_pids
+{
+	my ($hang_log, $want) = @_;
+	my @pids;
+
+	for (1 .. 10 * $PostgreSQL::Test::Utils::timeout_default)
+	{
+		if (-f $hang_log)
+		{
+			@pids = grep { length } split /\n/, slurp_file($hang_log);
+			last if @pids >= $want;
+		}
+		usleep(100_000);
+	}
+	return @pids;
+}
+
+# Basic async archive.
+for (1 .. 3) { switch_wal(1); }
+ok(
+	$node->poll_query_until(
+		'postgres', q{SELECT archived_count >= 3 FROM pg_stat_archiver}, 't'),
+	'archive_command_jobs=2 archives WAL');
+
+# Reload between async and sync modes.
+reload_jobs(3);
+reload_jobs(1);
+is( $node->safe_psql('postgres', 'SHOW archive_command_jobs'),
+	'1', 'SHOW archive_command_jobs is 1 after reload');
+my $before = archived_count();
+switch_wal(10);
+wait_archived_gt($before, 'archive advances after reload to jobs=1');
+
+# Failure under jobs=2, then recover.
+reload_jobs(2);
+my $bad_cmd = 'cp "%p_does_not_exist" "%f_does_not_exist"';
+my $failed_before = $node->safe_psql('postgres',
+	q{SELECT failed_count FROM pg_stat_archiver});
+
+$node->safe_psql(
+	'postgres', qq{
+    ALTER SYSTEM SET archive_command TO '$bad_cmd';
+    SELECT pg_reload_conf();
+});
+switch_wal(11);
+ok(
+	$node->poll_query_until(
+		'postgres',
+		qq{SELECT failed_count > $failed_before FROM pg_stat_archiver},
+		't'),
+	'failing archive_command under jobs=2 increments failed_count');
+
+# Use the segment the archiver actually failed on, not the pre-switch LSN.
+my $segment_name = $node->safe_psql('postgres',
+	q{SELECT last_failed_wal FROM pg_stat_archiver});
+ok($segment_name ne '', 'last_failed_wal reported after failure');
+my $segment_ready =
+  $node->data_dir . "/pg_wal/archive_status/$segment_name.ready";
+ok(-f $segment_ready, ".ready retained for $segment_name after failure");
+
+$node->safe_psql(
+	'postgres', q{
+	ALTER SYSTEM RESET archive_command;
+	SELECT pg_reload_conf();
+});
+$before = archived_count();
+switch_wal(12);
+wait_archived_gt($before, 'archival recovers after good archive_command');
+{
+	my $cleared = 0;
+	for (1 .. 10 * $PostgreSQL::Test::Utils::timeout_default)
+	{
+		if (!-f $segment_ready)
+		{
+			$cleared = 1;
+			last;
+		}
+		usleep(100_000);
+	}
+	ok($cleared, ".ready cleared for $segment_name after recovery");
+}
+
+# Concurrent hang jobs and shutdown cancel.
+my $hang = $node->basedir . '/hang_archive.sh';
+my $hang_log = $node->basedir . '/hang_archive.log';
+my $good_cmd = 'cp "%p" "' . $node->archive_dir . '/%f"';
+append_to_file(
+	$hang, qq{#!/bin/sh
+echo \$\$ >>'$hang_log'
+sleep 1000
+});
+chmod 0755, $hang or die "chmod $hang: $!";
+
+unlink $hang_log if -f $hang_log;
+reload_jobs(3, qq{archive_command = '$hang "%p" "%f"'\n});
+switch_wal(2);
+switch_wal(3);
+my @hang_pids = wait_for_hang_pids($hang_log, 2);
+cmp_ok(scalar(@hang_pids), '>=', 2, 'two archive_command jobs ran concurrently');
+
+reload_jobs(2, qq{archive_command = '$good_cmd'\n});
+ok(!kill(0, $_), "hang archive pid $_ gone after reload") for @hang_pids;
+$before = archived_count();
+switch_wal(13);
+wait_archived_gt($before, 'archiving drains after restoring good archive_command');
+
+unlink $hang_log if -f $hang_log;
+reload_jobs(3, qq{archive_command = '$hang "%p" "%f"'\n});
+switch_wal(4);
+switch_wal(5);
+@hang_pids = wait_for_hang_pids($hang_log, 2);
+cmp_ok(scalar(@hang_pids), '>=', 2, 'two hang archive_command jobs before stop');
+
+my $offset = -s $node->logfile;
+# Allow headroom past PGARCH_ASYNC_STOP_GRACE_S.
+ok($node->stop('fast', timeout => 120),
+	'stop with hung archive_command jobs');
+like(
+	slurp_file($node->logfile, $offset),
+	qr/canceling in flight archive commands due to shutdown/,
+	'shutdown canceled in flight archive commands after hung job grace');
+ok(!kill(0, $_), "hang archive pid $_ gone after stop") for @hang_pids;
+
+done_testing();
-- 
2.56.0

