Hi,

On Fri, Apr 3, 2026 at 6:00 AM Masahiko Sawada <[email protected]> wrote:
>
> On Thu, Apr 2, 2026 at 8:10 AM Daniil Davydov <[email protected]> wrote:
> >
> > I think we should add some cleanup for autovacuum near the ParallelContext
> > cleanup, since they are interconnected. I also want to return our tests that
> > are triggering ERROR/PANIC in the leader worker in order to check whether 
> > all
> > resources are released. I hope I will be able to get to that by tomorrow
> > evening.
>
> I think that the beginning of vacuum loop (in PG_TRY() block in
> vacuum()) seems better place as we're resetting vacuum delay
> parameters:
>
>         in_vacuum = true;
>         VacuumFailsafeActive = false;
>         VacuumUpdateCosts();
>         VacuumCostBalance = 0;
>         VacuumCostBalanceLocal = 0;
>         VacuumSharedCostBalance = NULL;
>         VacuumActiveNWorkers = NULL;
>

I am still thinking that this pointer is related to the ParallelContext, and it
is a bit confusing that we can manipulate it outside all "parallel" logic.
Since this variable points to the DSM it looks very natural for me if its
lifetime will be similar to the DSM. Please, see attached patch, that resets
this pointer during dsm detaching.

--
Best regards,
Daniil Davydov
From 50052b41b9a8dc270720c0325b3afc270e8f9b5c Mon Sep 17 00:00:00 2001
From: Daniil Davidov <[email protected]>
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

Reply via email to