From 19b1a9b4814fdf411a34a0ca86ce224079c857cc Mon Sep 17 00:00:00 2001 From: "Imseih (AWS)" Date: Thu, 26 May 2022 07:45:26 -0500 Subject: [PATCH v11 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. Author: Sami Imseih, based on suggestions by Nathan Bossart, Peter Geoghegan and Masahiko Sawada Reviewed by: Nathan Bossart, Masahiko Sawada Discussion: https://www.postgresql.org/message-id/flat/5478DFCD-2333-401A-B2F0-0D186AB09228@amazon.com --- 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 df0cd77..bfe3275 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 983841d..53a3d13 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.1 (Apple Git-133)