From 50052b41b9a8dc270720c0325b3afc270e8f9b5c Mon Sep 17 00:00:00 2001
From: Daniil Davidov <d.davydov@postgrespro.ru>
Date: Fri, 3 Apr 2026 20:31:11 +0700
Subject: [PATCH] Reset pointer into the going away DSM

---
 src/backend/commands/vacuumparallel.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c
index bac3bd28214..a01ace9343c 100644
--- a/src/backend/commands/vacuumparallel.c
+++ b/src/backend/commands/vacuumparallel.c
@@ -294,6 +294,7 @@ static bool parallel_vacuum_index_is_parallel_safe(Relation indrel, int num_inde
 												   bool vacuum);
 static void parallel_vacuum_error_callback(void *arg);
 static inline void parallel_vacuum_set_cost_parameters(PVSharedCostParams *params);
+static void parallel_vacuum_cleanup(dsm_segment *seg, Datum arg);
 
 /*
  * Try to enter parallel mode and create a parallel context.  Then initialize
@@ -467,6 +468,7 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes,
 		SpinLockInit(&shared->cost_params.mutex);
 
 		pv_shared_cost_params = &(shared->cost_params);
+		on_dsm_detach(pcxt->seg, parallel_vacuum_cleanup, (Datum) 0);
 	}
 
 	shm_toc_insert(pcxt->toc, PARALLEL_VACUUM_KEY_SHARED, shared);
@@ -541,6 +543,17 @@ parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats)
 	pfree(pvs);
 }
 
+/*
+ * Cleanup for parallel autovacuum.
+ */
+static void
+parallel_vacuum_cleanup(dsm_segment *seg, Datum arg)
+{
+	/* We need to reset all pointers into the DSM that is going away */
+	Assert(AmAutoVacuumWorkerProcess());
+	pv_shared_cost_params = NULL;
+}
+
 /*
  * Returns the dead items space and dead items information.
  */
-- 
2.43.0

