v10-0001-Add-progress-reporting-callback-to-ParallelConte.patch

application/octet-stream

Filename: v10-0001-Add-progress-reporting-callback-to-ParallelConte.patch
Type: application/octet-stream
Part: 0
Message: Re: Add index scan progress to pg_stat_progress_vacuum

Patch

Format: format-patch
Series: patch v10-0001
Subject: Add progress reporting callback to ParallelContext
File+
src/backend/access/transam/parallel.c 16 0
src/include/access/parallel.h 5 0
From 94fc3655b55cff3346a8afdf0bac2ee9b26fd035 Mon Sep 17 00:00:00 2001
From: "Sami Imseih (AWS)" <simseih@amazon.com>
Date: Thu, 14 Apr 2022 00:06:14 +0000
Subject: [PATCH v10 1/2] Add progress reporting callback to ParallelContext

The purpose of supporting a progress reporting
callback in ParallelContext is to allow for the
leader process to report progress while waiting
for workers to complete.

The first use-case for this is to report index
progress in pg_stat_progress_vacuum.
---
 src/backend/access/transam/parallel.c | 16 ++++++++++++++++
 src/include/access/parallel.h         |  5 +++++
 2 files changed, 21 insertions(+)

diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c
index df0cd77558..bfe3275d8b 100644
--- a/src/backend/access/transam/parallel.c
+++ b/src/backend/access/transam/parallel.c
@@ -774,6 +774,22 @@ WaitForParallelWorkersToFinish(ParallelContext *pcxt)
 		 */
 		CHECK_FOR_INTERRUPTS();
 
+		/*
+		 * We call the parallel progress callback while
+		 * waiting for the parallel workers to finish.
+		 * This is to ensure that the leader keeps
+		 * updating progress when waiting for
+		 * parallel workers to finish.
+		 *
+		 * We must ensure that pcxt->parallel_progress_callback
+		 * is set before calling as not all parallel
+		 * operations will set a callback.
+		 */
+		if (pcxt->parallel_progress_callback)
+		{
+			pcxt->parallel_progress_callback(pcxt->parallel_progress_callback_arg);
+		}
+
 		for (i = 0; i < pcxt->nworkers_launched; ++i)
 		{
 			/*
diff --git a/src/include/access/parallel.h b/src/include/access/parallel.h
index 983841d45e..53a3d13ec0 100644
--- a/src/include/access/parallel.h
+++ b/src/include/access/parallel.h
@@ -20,6 +20,9 @@
 #include "storage/shm_mq.h"
 #include "storage/shm_toc.h"
 
+/* progress callback definition */
+typedef void (*ParallelProgressCallback) (void *parallel_progress_callback_state);
+
 typedef void (*parallel_worker_main_type) (dsm_segment *seg, shm_toc *toc);
 
 typedef struct ParallelWorkerInfo
@@ -46,6 +49,8 @@ typedef struct ParallelContext
 	ParallelWorkerInfo *worker;
 	int			nknown_attached_workers;
 	bool	   *known_attached_workers;
+	ParallelProgressCallback parallel_progress_callback;
+	void            *parallel_progress_callback_arg;
 } ParallelContext;
 
 typedef struct ParallelWorkerContext
-- 
2.32.0