Hi,

On Wed, Nov 18, 2026 at 9:29 AM Bharath Rupireddy
<[email protected]> wrote:
>
> On Mon, Nov 9, 2026 at 3:13 PM surya poondla <[email protected]> wrote:
>
> > 1. v3 now runs lazy_check_wraparound_failsafe() after the parallel index
> > pass only when the shared flag is set;
>
> > matters in one corner case which is parallel workers use the GUC values
> > serialized when the parallel context was set up, while an autovacuum
> > leader can reload vacuum_failsafe_age while it waits for them. If
> > vacuum_failsafe_age is lowered at that point, only the leader's recheck
> > would notice it. I can share a reproducer if that's useful (doesn't happen 
> > in normal scenarios)
>
> Yeah, that's right. And that window, where users can lower the
> failsafe age to avoid wraparound, can be long in some cases. This can
> happen when the leader waits for the workers to finish index vacuuming
> (i.e. the leader gets small indexes compared to the workers).
>
> I don't think the fix is to keep an extra check in the leader. The
> issue is that we don't pass the failsafe age down to the workers like
> we do for the other cost delay params. Without it, the workers can't
> honor a changed failsafe age, and lowering it can be a valid way to
> avoid wraparound.
>
> So I think a better fix is to pass the failsafe age from the leader to
> the workers. That way both the leader and the workers check after each
> index. If the leader is waiting for the workers and the failsafe age
> changes, the workers will catch it, set the shmem flag, and the leader
> will report it. So we wouldn't need an extra check in the leader.

Please find the attached v4 patch with the above change.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com
From cbe17ef530dc040372d612d04290efad46427126 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <[email protected]>
Date: Fri, 20 Nov 2026 02:51:35 +0000
Subject: [PATCH v4] Check XID wraparound failsafe between indexes in parallel
 vacuum.

In a parallel vacuum the XID wraparound failsafe is checked once
before the indexes are vacuumed and then only after the parallel
workers have vacuumed all the indexes, and cost-based delays are
applied until then. This is unlike non-parallel vacuum. With large
indexes, this can delay advancing relfrozenxid and relminmxid,
bringing the database closer to XID wraparound and eventually to
downtime for writes. With parallel autovacuum in PG19 and its
default cost delay parameters, the advancement can be delayed even
further.

Fix this by having parallel vacuum check the wraparound failsafe
after each index, as non-parallel vacuum does. The table's cutoffs
are shared with the parallel workers, and each parallel worker and
the leader check the wraparound failsafe after each index they
vacuum and set a shared flag if the failsafe has triggered. They
check this shared flag at every vacuum delay point and, once it is
set, ignore the cost-based delays, so that the index being vacuumed
finishes sooner, and they skip the remaining indexes, so that
relfrozenxid and relminmxid can be advanced sooner to avoid XID
wraparound.

For parallel autovacuum, also pass the failsafe ages from the
leader down to the parallel workers, as is done for the cost delay
parameters. Without this, the parallel workers keep the failsafe
ages they got when the parallel vacuum was set up and cannot honor
a failsafe age changed with a reload, and lowering it can be a
valid way to avoid XID wraparound. The VACUUM command does not
reload in the middle of the command, so this is needed only for
parallel autovacuum.

Author: Bharath Rupireddy <[email protected]>
Reviewed-by: Sami Imseih <[email protected]>
Reviewed-by: Zsolt Parragi <[email protected]>
Reviewed-by: Surya Poondla <[email protected]>
Discussion: https://postgr.es/m/CALj2ACWkgvoOp0XxY-PNvYo7Y625_eBUHOg8B_0FrhHu%2BAY35A%40mail.gmail.com
---
 src/backend/access/heap/vacuumlazy.c          |  30 ++++--
 src/backend/commands/vacuum.c                 |  16 +++
 src/backend/commands/vacuumparallel.c         |  84 +++++++++++++--
 src/include/commands/vacuum.h                 |   9 +-
 src/test/modules/test_autovacuum/Makefile     |   3 +-
 src/test/modules/test_autovacuum/meson.build  |   1 +
 .../t/001_parallel_autovacuum.pl              |   6 +-
 .../t/003_parallel_vacuum_failsafe.pl         | 101 ++++++++++++++++++
 8 files changed, 224 insertions(+), 26 deletions(-)
 create mode 100644 src/test/modules/test_autovacuum/t/003_parallel_vacuum_failsafe.pl

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 1393c57ad8f..b78a71ae84f 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -2570,16 +2570,18 @@ lazy_vacuum_all_indexes(LVRelState *vacrel)
 	else
 	{
 		/* Outsource everything to parallel variant */
-		parallel_vacuum_bulkdel_all_indexes(vacrel->pvs, old_live_tuples,
-											vacrel->num_index_scans,
-											&(vacrel->worker_usage.vacuum));
-
-		/*
-		 * Do a postcheck to consider applying wraparound failsafe now.  Note
-		 * that parallel VACUUM only gets the precheck and this postcheck.
-		 */
-		if (lazy_check_wraparound_failsafe(vacrel))
+		if (!parallel_vacuum_bulkdel_all_indexes(vacrel->pvs, old_live_tuples,
+												 vacrel->num_index_scans,
+												 &(vacrel->worker_usage.vacuum)))
+		{
+			/*
+			 * The failsafe was triggered by either the leader or one of the
+			 * parallel workers, so do the reporting and the resetting work
+			 * here, once per VACUUM.
+			 */
+			lazy_check_wraparound_failsafe(vacrel);
 			allindexes = false;
+		}
 	}
 
 	/*
@@ -2902,6 +2904,9 @@ lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
  * up when the failsafe triggers.  VACUUM stops applying any cost-based delay
  * that it started out with.
  *
+ * In a parallel vacuum, this also triggers the failsafe if the leader or a
+ * worker has detected it, without rechecking the age here.
+ *
  * Returns true when failsafe has been triggered.
  */
 static bool
@@ -2911,7 +2916,9 @@ lazy_check_wraparound_failsafe(LVRelState *vacrel)
 	if (VacuumFailsafeActive)
 		return true;
 
-	if (unlikely(vacuum_xid_failsafe_check(&vacrel->cutoffs)))
+	if (unlikely((ParallelVacuumIsActive(vacrel) &&
+				  parallel_vacuum_failsafe_triggered(vacrel->pvs)) ||
+				 vacuum_xid_failsafe_check(&vacrel->cutoffs)))
 	{
 		const int	progress_index[] = {
 			PROGRESS_VACUUM_INDEXES_TOTAL,
@@ -3501,7 +3508,8 @@ dead_items_alloc(LVRelState *vacrel, int nworkers)
 											   vacrel->nindexes, nworkers,
 											   vac_work_mem,
 											   vacrel->verbose ? INFO : DEBUG2,
-											   vacrel->bstrategy);
+											   vacrel->bstrategy,
+											   &vacrel->cutoffs);
 
 		/*
 		 * If parallel mode started, dead_items and dead_items_info spaces are
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index 63f6b8a733a..1437d7e2967 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -119,6 +119,9 @@ pg_atomic_uint32 *VacuumSharedCostBalance = NULL;
 pg_atomic_uint32 *VacuumActiveNWorkers = NULL;
 int			VacuumCostBalanceLocal = 0;
 
+/* Shared wraparound failsafe flag, set while vacuuming indexes in parallel */
+pg_atomic_uint32 *VacuumSharedFailsafe = NULL;
+
 /* non-export function prototypes */
 static List *expand_vacuum_rel(VacuumRelation *vrel,
 							   MemoryContext vac_context, int options);
@@ -687,6 +690,7 @@ vacuum(List *relations, const VacuumParams *params, BufferAccessStrategy bstrate
 		VacuumCostBalanceLocal = 0;
 		VacuumSharedCostBalance = NULL;
 		VacuumActiveNWorkers = NULL;
+		VacuumSharedFailsafe = NULL;
 	}
 	PG_END_TRY();
 
@@ -2551,6 +2555,18 @@ vacuum_delay_point(bool is_analyze)
 		parallel_vacuum_propagate_shared_delay_params();
 	}
 
+	/*
+	 * Stop the delay if a process of a parallel vacuum hit the wraparound
+	 * failsafe, so that the index this process is vacuuming finishes sooner.
+	 * This comes after the reload above, which can turn the delay back on.
+	 */
+	if (VacuumSharedFailsafe != NULL &&
+		pg_atomic_read_u32(VacuumSharedFailsafe) != 0)
+	{
+		VacuumCostActive = false;
+		VacuumCostBalance = 0;
+	}
+
 	/*
 	 * If we disabled cost-based delays after reloading the config file,
 	 * return.
diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c
index 339d876d6e2..c217b3d406a 100644
--- a/src/backend/commands/vacuumparallel.c
+++ b/src/backend/commands/vacuumparallel.c
@@ -88,6 +88,10 @@ typedef struct PVSharedCostParams
 	int			cost_page_hit;
 	int			cost_page_miss;
 	bool		track_cost_delay_timing;
+
+	/* Failsafe ages, for the failsafe check between indexes */
+	int			failsafe_age;
+	int			multixact_failsafe_age;
 } PVSharedCostParams;
 
 /*
@@ -151,6 +155,10 @@ typedef struct PVShared
 	/* Counter for vacuuming and cleanup */
 	pg_atomic_uint32 idx;
 
+	/* For the failsafe check between indexes, the flag never goes back to 0 */
+	struct VacuumCutoffs cutoffs;
+	pg_atomic_uint32 failsafe_triggered;
+
 	/* DSA handle where the TidStore lives */
 	dsa_handle	dead_items_dsa_handle;
 
@@ -286,7 +294,7 @@ static uint32 shared_params_generation_local = 0;
 
 static int	parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested,
 											bool *will_parallel_vacuum);
-static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans,
+static bool parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans,
 												bool vacuum, PVWorkerStats *wstats);
 static void parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs);
 static void parallel_vacuum_process_unsafe_indexes(ParallelVacuumState *pvs);
@@ -302,12 +310,15 @@ static void parallel_vacuum_dsm_detach(dsm_segment *seg, Datum arg);
  * Try to enter parallel mode and create a parallel context.  Then initialize
  * shared memory state.
  *
+ * cutoffs are shared with the workers for the failsafe check between indexes.
+ *
  * On success, return parallel vacuum state.  Otherwise return NULL.
  */
 ParallelVacuumState *
 parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes,
 					 int nrequested_workers, int vac_work_mem,
-					 int elevel, BufferAccessStrategy bstrategy)
+					 int elevel, BufferAccessStrategy bstrategy,
+					 const struct VacuumCutoffs *cutoffs)
 {
 	ParallelVacuumState *pvs;
 	ParallelContext *pcxt;
@@ -457,6 +468,9 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes,
 	pg_atomic_init_u32(&(shared->active_nworkers), 0);
 	pg_atomic_init_u32(&(shared->idx), 0);
 
+	memcpy(&(shared->cutoffs), cutoffs, sizeof(struct VacuumCutoffs));
+	pg_atomic_init_u32(&(shared->failsafe_triggered), 0);
+
 	shared->is_autovacuum = AmAutoVacuumWorkerProcess();
 
 	/*
@@ -595,10 +609,20 @@ parallel_vacuum_reset_dead_items(ParallelVacuumState *pvs)
 	dead_items_info->num_items = 0;
 }
 
+/* Has one of the processes triggered the wraparound failsafe? */
+bool
+parallel_vacuum_failsafe_triggered(ParallelVacuumState *pvs)
+{
+	return pg_atomic_read_u32(&(pvs->shared->failsafe_triggered)) != 0;
+}
+
 /*
  * Do parallel index bulk-deletion with parallel workers.
+ *
+ * Returns false when the failsafe was triggered by either the leader or one of
+ * the parallel workers, true otherwise.
  */
-void
+bool
 parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, double num_table_tuples,
 									int num_index_scans, PVWorkerStats *wstats)
 {
@@ -611,7 +635,8 @@ parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, double num_table_t
 	pvs->shared->reltuples = num_table_tuples;
 	pvs->shared->estimated_count = true;
 
-	parallel_vacuum_process_all_indexes(pvs, num_index_scans, true, wstats);
+	return parallel_vacuum_process_all_indexes(pvs, num_index_scans, true,
+											   wstats);
 }
 
 /*
@@ -647,6 +672,8 @@ parallel_vacuum_set_cost_parameters(PVSharedCostParams *params)
 	params->cost_page_hit = VacuumCostPageHit;
 	params->cost_page_miss = VacuumCostPageMiss;
 	params->track_cost_delay_timing = track_cost_delay_timing;
+	params->failsafe_age = vacuum_failsafe_age;
+	params->multixact_failsafe_age = vacuum_multixact_failsafe_age;
 }
 
 /*
@@ -680,6 +707,8 @@ parallel_vacuum_update_shared_delay_params(void)
 	VacuumCostPageHit = pv_shared_cost_params->cost_page_hit;
 	VacuumCostPageMiss = pv_shared_cost_params->cost_page_miss;
 	track_cost_delay_timing = pv_shared_cost_params->track_cost_delay_timing;
+	vacuum_failsafe_age = pv_shared_cost_params->failsafe_age;
+	vacuum_multixact_failsafe_age = pv_shared_cost_params->multixact_failsafe_age;
 	SpinLockRelease(&pv_shared_cost_params->mutex);
 
 	VacuumUpdateCosts();
@@ -687,13 +716,15 @@ parallel_vacuum_update_shared_delay_params(void)
 	shared_params_generation_local = params_generation;
 
 	elog(DEBUG2,
-		 "parallel autovacuum worker updated cost params: cost_limit=%d, cost_delay=%g, cost_page_miss=%d, cost_page_dirty=%d, cost_page_hit=%d, track_cost_delay_timing=%s",
+		 "parallel autovacuum worker updated cost params: cost_limit=%d, cost_delay=%g, cost_page_miss=%d, cost_page_dirty=%d, cost_page_hit=%d, track_cost_delay_timing=%s, failsafe_age=%d, multixact_failsafe_age=%d",
 		 vacuum_cost_limit,
 		 vacuum_cost_delay,
 		 VacuumCostPageMiss,
 		 VacuumCostPageDirty,
 		 VacuumCostPageHit,
-		 track_cost_delay_timing ? "yes" : "no");
+		 track_cost_delay_timing ? "yes" : "no",
+		 vacuum_failsafe_age,
+		 vacuum_multixact_failsafe_age);
 }
 
 /*
@@ -721,7 +752,9 @@ parallel_vacuum_propagate_shared_delay_params(void)
 		VacuumCostPageDirty == pv_shared_cost_params->cost_page_dirty &&
 		VacuumCostPageHit == pv_shared_cost_params->cost_page_hit &&
 		VacuumCostPageMiss == pv_shared_cost_params->cost_page_miss &&
-		track_cost_delay_timing == pv_shared_cost_params->track_cost_delay_timing)
+		track_cost_delay_timing == pv_shared_cost_params->track_cost_delay_timing &&
+		vacuum_failsafe_age == pv_shared_cost_params->failsafe_age &&
+		vacuum_multixact_failsafe_age == pv_shared_cost_params->multixact_failsafe_age)
 		return;
 
 	/* Update the shared delay parameters */
@@ -851,13 +884,16 @@ parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested,
  * must be used by the parallel vacuum leader process.
  *
  * If wstats is not NULL, the parallel worker statistics are updated.
+ *
+ * Returns false when failsafe has been triggered.
  */
-static void
+static bool
 parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans,
 									bool vacuum, PVWorkerStats *wstats)
 {
 	int			nworkers;
 	PVIndVacStatus new_status;
+	bool		failsafe;
 
 	Assert(!IsParallelWorker());
 
@@ -952,6 +988,9 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan
 			VacuumSharedCostBalance = &(pvs->shared->cost_balance);
 			VacuumActiveNWorkers = &(pvs->shared->active_nworkers);
 
+			/* The leader's own delay stops via this flag too */
+			VacuumSharedFailsafe = &(pvs->shared->failsafe_triggered);
+
 			/* Update the statistics, if we asked to */
 			if (wstats != NULL)
 				wstats->nlaunched += pvs->pcxt->nworkers_launched;
@@ -995,13 +1034,18 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan
 
 	/*
 	 * Reset all index status back to initial (while checking that we have
-	 * vacuumed all indexes).
+	 * vacuumed all indexes). Indexes not yet started when the failsafe
+	 * triggered are skipped, so those are still NEED_BULKDELETE here and that
+	 * is expected.
 	 */
+	failsafe = parallel_vacuum_failsafe_triggered(pvs);
 	for (int i = 0; i < pvs->nindexes; i++)
 	{
 		PVIndStats *indstats = &(pvs->indstats[i]);
 
-		if (indstats->status != PARALLEL_INDVAC_STATUS_COMPLETED)
+		if (indstats->status != PARALLEL_INDVAC_STATUS_COMPLETED &&
+			!(failsafe &&
+			  indstats->status == PARALLEL_INDVAC_STATUS_NEED_BULKDELETE))
 			elog(ERROR, "parallel index vacuum on index \"%s\" is not completed",
 				 RelationGetRelationName(pvs->indrels[i]));
 
@@ -1016,7 +1060,10 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan
 		VacuumCostBalance = pg_atomic_read_u32(VacuumSharedCostBalance);
 		VacuumSharedCostBalance = NULL;
 		VacuumActiveNWorkers = NULL;
+		VacuumSharedFailsafe = NULL;
 	}
+
+	return !failsafe;
 }
 
 /*
@@ -1133,6 +1180,17 @@ parallel_vacuum_process_one_index(ParallelVacuumState *pvs, Relation indrel,
 	};
 	const int64 reset_val[] = {(int64) InvalidOid, 0, 0};
 
+	/* Don't start another index once the failsafe has triggered */
+	if (indstats->status == PARALLEL_INDVAC_STATUS_NEED_BULKDELETE &&
+		parallel_vacuum_failsafe_triggered(pvs))
+	{
+		/* Show failsafe mode in the leader's row, workers just exit */
+		if (!IsParallelWorker())
+			pgstat_progress_update_param(PROGRESS_VACUUM_MODE,
+										 PROGRESS_VACUUM_MODE_FAILSAFE);
+		return;
+	}
+
 	/*
 	 * Update the pointer to the corresponding bulk-deletion result if someone
 	 * has already updated it
@@ -1166,6 +1224,11 @@ parallel_vacuum_process_one_index(ParallelVacuumState *pvs, Relation indrel,
 		case PARALLEL_INDVAC_STATUS_NEED_BULKDELETE:
 			istat_res = vac_bulkdel_one_index(&ivinfo, istat, pvs->dead_items,
 											  &pvs->shared->dead_items_info);
+
+			/* Check the failsafe as a non-parallel vacuum does */
+			INJECTION_POINT("parallel-vacuum-failsafe-check", NULL);
+			if (vacuum_xid_failsafe_check(&(pvs->shared->cutoffs)))
+				pg_atomic_write_u32(&(pvs->shared->failsafe_triggered), 1);
 			break;
 		case PARALLEL_INDVAC_STATUS_NEED_CLEANUP:
 			istat_res = vac_cleanup_one_index(&ivinfo, istat);
@@ -1356,6 +1419,7 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc)
 	VacuumCostBalanceLocal = 0;
 	VacuumSharedCostBalance = &(shared->cost_balance);
 	VacuumActiveNWorkers = &(shared->active_nworkers);
+	VacuumSharedFailsafe = &(shared->failsafe_triggered);
 
 	/* Set parallel vacuum state */
 	pvs.indrels = indrels;
diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h
index 404307b6f6c..977063e2f5a 100644
--- a/src/include/commands/vacuum.h
+++ b/src/include/commands/vacuum.h
@@ -362,6 +362,9 @@ extern PGDLLIMPORT pg_atomic_uint32 *VacuumSharedCostBalance;
 extern PGDLLIMPORT pg_atomic_uint32 *VacuumActiveNWorkers;
 extern PGDLLIMPORT int VacuumCostBalanceLocal;
 
+/* Shared wraparound failsafe flag, set while vacuuming indexes in parallel */
+extern PGDLLIMPORT pg_atomic_uint32 *VacuumSharedFailsafe;
+
 extern PGDLLIMPORT bool VacuumFailsafeActive;
 extern PGDLLIMPORT double vacuum_cost_delay;
 extern PGDLLIMPORT int vacuum_cost_limit;
@@ -417,12 +420,14 @@ extern void VacuumUpdateCosts(void);
 extern ParallelVacuumState *parallel_vacuum_init(Relation rel, Relation *indrels,
 												 int nindexes, int nrequested_workers,
 												 int vac_work_mem, int elevel,
-												 BufferAccessStrategy bstrategy);
+												 BufferAccessStrategy bstrategy,
+												 const struct VacuumCutoffs *cutoffs);
 extern void parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats);
 extern TidStore *parallel_vacuum_get_dead_items(ParallelVacuumState *pvs,
 												VacDeadItemsInfo **dead_items_info_p);
 extern void parallel_vacuum_reset_dead_items(ParallelVacuumState *pvs);
-extern void parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs,
+extern bool parallel_vacuum_failsafe_triggered(ParallelVacuumState *pvs);
+extern bool parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs,
 												double num_table_tuples,
 												int num_index_scans,
 												PVWorkerStats *wstats);
diff --git a/src/test/modules/test_autovacuum/Makefile b/src/test/modules/test_autovacuum/Makefile
index 15e83010c1c..f5077de515c 100644
--- a/src/test/modules/test_autovacuum/Makefile
+++ b/src/test/modules/test_autovacuum/Makefile
@@ -4,7 +4,8 @@ PGFILEDESC = "test_autovacuum - test code for autovacuum"
 
 TAP_TESTS = 1
 
-EXTRA_INSTALL = src/test/modules/injection_points
+EXTRA_INSTALL = src/test/modules/injection_points \
+	src/test/modules/xid_wraparound
 
 export enable_injection_points
 
diff --git a/src/test/modules/test_autovacuum/meson.build b/src/test/modules/test_autovacuum/meson.build
index 970b9aaae4b..f1a4123d50c 100644
--- a/src/test/modules/test_autovacuum/meson.build
+++ b/src/test/modules/test_autovacuum/meson.build
@@ -11,6 +11,7 @@ tests += {
     'tests': [
       't/001_parallel_autovacuum.pl',
       't/002_toast_relopts.pl',
+      't/003_parallel_vacuum_failsafe.pl',
     ],
   },
 }
diff --git a/src/test/modules/test_autovacuum/t/001_parallel_autovacuum.pl b/src/test/modules/test_autovacuum/t/001_parallel_autovacuum.pl
index 33b50c13f7c..9f86cafa91f 100644
--- a/src/test/modules/test_autovacuum/t/001_parallel_autovacuum.pl
+++ b/src/test/modules/test_autovacuum/t/001_parallel_autovacuum.pl
@@ -142,7 +142,7 @@ $node->safe_psql(
 $node->wait_for_event('autovacuum worker',
 	'autovacuum-start-parallel-vacuum');
 
-# Update the shared cost-based delay parameters.
+# Update the shared cost-based delay parameters and the failsafe ages.
 $node->safe_psql(
 	'postgres', qq{
 	ALTER SYSTEM SET autovacuum_vacuum_cost_limit = 500;
@@ -151,6 +151,8 @@ $node->safe_psql(
 	ALTER SYSTEM SET vacuum_cost_page_dirty = 10;
 	ALTER SYSTEM SET vacuum_cost_page_hit = 10;
 	ALTER SYSTEM SET track_cost_delay_timing = on;
+	ALTER SYSTEM SET vacuum_failsafe_age = 1500000000;
+	ALTER SYSTEM SET vacuum_multixact_failsafe_age = 1500000000;
 	SELECT pg_reload_conf();
 });
 
@@ -165,7 +167,7 @@ $node->safe_psql(
 # Check whether parallel worker successfully updated all parameters during
 # index processing.
 $node->wait_for_log(
-	qr/parallel autovacuum worker updated cost params: cost_limit=500, cost_delay=5, cost_page_miss=10, cost_page_dirty=10, cost_page_hit=10, track_cost_delay_timing=yes/,
+	qr/parallel autovacuum worker updated cost params: cost_limit=500, cost_delay=5, cost_page_miss=10, cost_page_dirty=10, cost_page_hit=10, track_cost_delay_timing=yes, failsafe_age=1500000000, multixact_failsafe_age=1500000000/,
 	$log_offset);
 
 # Cleanup
diff --git a/src/test/modules/test_autovacuum/t/003_parallel_vacuum_failsafe.pl b/src/test/modules/test_autovacuum/t/003_parallel_vacuum_failsafe.pl
new file mode 100644
index 00000000000..6f32a1872f9
--- /dev/null
+++ b/src/test/modules/test_autovacuum/t/003_parallel_vacuum_failsafe.pl
@@ -0,0 +1,101 @@
+
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test the wraparound failsafe check between indexes in parallel vacuum
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+plan skip_all => 'Injection points not supported by this build'
+  unless $ENV{enable_injection_points} eq 'yes';
+
+my $node = PostgreSQL::Test::Cluster->new('main');
+$node->init;
+
+# Only the test table qualifies for the single autovacuum worker. The failsafe
+# limit here is 105000 XIDs (autovacuum_freeze_max_age * 1.05).
+$node->append_conf(
+	'postgresql.conf', qq{
+autovacuum_naptime = '1s'
+autovacuum_max_workers = 1
+autovacuum_max_parallel_workers = 1
+autovacuum_vacuum_threshold = 100000
+autovacuum_vacuum_insert_threshold = -1
+autovacuum_freeze_max_age = 100000
+vacuum_failsafe_age = 100000
+min_parallel_index_scan_size = 0
+});
+$node->start;
+
+# The modules may be absent under installcheck.
+if (   !$node->check_extension('injection_points')
+	|| !$node->check_extension('xid_wraparound'))
+{
+	plan skip_all => 'Required test extensions are not installed';
+}
+
+# Four indexes. The leader and the parallel worker vacuum one each and then
+# wait at the injection point.
+$node->safe_psql(
+	'postgres', qq{
+	CREATE EXTENSION injection_points;
+	CREATE EXTENSION xid_wraparound;
+	CREATE TABLE test_failsafe (col int)
+		WITH (autovacuum_enabled = false,
+			  autovacuum_parallel_workers = 1,
+			  autovacuum_vacuum_threshold = 1,
+			  autovacuum_vacuum_scale_factor = 0,
+			  log_autovacuum_min_duration = 0);
+	INSERT INTO test_failsafe SELECT generate_series(1, 100);
+	CREATE INDEX idx_1 ON test_failsafe (col);
+	CREATE INDEX idx_2 ON test_failsafe (col);
+	CREATE INDEX idx_3 ON test_failsafe (col);
+	CREATE INDEX idx_4 ON test_failsafe (col);
+	DELETE FROM test_failsafe;
+	SELECT injection_points_attach('parallel-vacuum-failsafe-check', 'wait');
+});
+
+my $log_offset = -s $node->logfile;
+$node->safe_psql('postgres',
+	'ALTER TABLE test_failsafe SET (autovacuum_enabled = true)');
+$node->wait_for_event('autovacuum worker', 'parallel-vacuum-failsafe-check');
+$node->wait_for_event('parallel worker', 'parallel-vacuum-failsafe-check');
+
+$node->safe_psql('postgres', 'SELECT consume_xids(150000)');
+$node->safe_psql('postgres',
+	"SELECT injection_points_detach('parallel-vacuum-failsafe-check')");
+
+# Release the two processes one by one as a wakeup releases only one
+foreach my $left (1, 0)
+{
+	$node->safe_psql('postgres',
+		"SELECT injection_points_wakeup('parallel-vacuum-failsafe-check')");
+	$node->poll_query_until(
+		'postgres',
+		"SELECT count(*) = $left FROM pg_stat_activity
+		 WHERE wait_event = 'parallel-vacuum-failsafe-check'"
+	) or die "timed out releasing a held vacuum process";
+}
+
+# Check if the remaining indexes are skipped
+my $report = qr/automatic vacuum of table "postgres\.public\.test_failsafe"/;
+$node->wait_for_log($report, $log_offset);
+my $log = slurp_file($node->logfile, $log_offset);
+my @vacuumed = $log =~ /index "(idx_\d)":/g;
+note("indexes vacuumed: @vacuumed");
+
+is(scalar(@vacuumed), 2,
+	'remaining indexes are skipped once the failsafe triggers');
+
+# Check if the leader reports the detected failsafe
+my ($before_report) = $log =~ /^(.*?)$report/s;
+like(
+	$before_report,
+	qr/bypassing nonessential maintenance of table "postgres\.public\.test_failsafe"/,
+	'leader reports the detected failsafe');
+
+$node->stop;
+done_testing();
-- 
2.47.3

Reply via email to