This is the v11 of the patch (rebase).

---
Benoit Lobréau
Consultant
http://dalibo.com
From 49e4d0c154b4841c6f0c111bad2e4bab3b0ce8fe Mon Sep 17 00:00:00 2001
From: benoit <[email protected]>
Date: Tue, 8 Oct 2024 12:39:41 +0200
Subject: [PATCH 1/3] Add a guc for parallel worker logging

The new guc log_parallel_workers controls whether a log message is
produced to display information on the number of workers spawned when a
parallel query or utility is executed.

The default value is `none` which disables logging. `all` displays
information for all parallel queries, whereas `shortage` displays
information only when the number of workers launched is lower than the
number of planned workers.

This new parameter can help database administrators and developers
diagnose performance issues related to parallelism and optimize the
configuration of the system accordingly.
---
 src/backend/utils/misc/guc_parameters.dat | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat
index 83af594d4af..72ef1354091 100644
--- a/src/backend/utils/misc/guc_parameters.dat
+++ b/src/backend/utils/misc/guc_parameters.dat
@@ -1785,6 +1785,14 @@
   assign_hook => 'assign_log_min_messages',
 },
 
+{ name => 'log_parallel_workers', type => 'enum', context => 'PGC_SUSET', group => 'LOGGING_WHAT',
+  short_desc => 'Log information about parallel worker usage.',
+  long_desc => '"none" doesn\'t log anything, "all" logs all parallel worker usage and "shortage" logs only when the planned workers couldn\'t be acquired',
+  variable => 'log_parallel_workers',
+  boot_val => 'LOG_PARALLEL_WORKERS_NONE',
+  options => 'log_parallel_workers_options',
+},
+
 { name => 'log_parameter_max_length', type => 'int', context => 'PGC_SUSET', group => 'LOGGING_WHAT',
   short_desc => 'Sets the maximum length in bytes of data logged for bind parameter values when logging statements.',
   long_desc => '-1 means log values in full.',
-- 
2.47.3

From 9f17faacb5c3e5f71abbb360bc33654d28531b81 Mon Sep 17 00:00:00 2001
From: benoit <[email protected]>
Date: Wed, 29 Jan 2025 17:10:57 +0100
Subject: [PATCH 2/3] Implements logging for parallel worker usage in utilities

This patch implements logging of parallel worker usage for:
* the index cleanup and bulkdelete phases of vacuum;
* btree, brin and gin index builds.
---
 src/backend/access/brin/brin.c        |  4 ++++
 src/backend/access/gin/gininsert.c    |  4 ++++
 src/backend/access/nbtree/nbtsort.c   |  4 ++++
 src/backend/commands/vacuumparallel.c | 13 +++++++++++++
 4 files changed, 25 insertions(+)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index bdb30752e09..3bb04c117d8 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -2568,6 +2568,10 @@ _brin_end_parallel(BrinLeader *brinleader, BrinBuildState *state)
 	/* Shutdown worker processes */
 	WaitForParallelWorkersToFinish(brinleader->pcxt);
 
+	LogParallelWorkersIfNeeded(log_parallel_workers,
+							   brinleader->pcxt->nworkers_to_launch,
+							   brinleader->pcxt->nworkers_launched);
+
 	/*
 	 * Next, accumulate WAL usage.  (This must wait for the workers to finish,
 	 * or we might get incomplete data.)
diff --git a/src/backend/access/gin/gininsert.c b/src/backend/access/gin/gininsert.c
index 9d83a495775..025d39b6e7d 100644
--- a/src/backend/access/gin/gininsert.c
+++ b/src/backend/access/gin/gininsert.c
@@ -1120,6 +1120,10 @@ _gin_end_parallel(GinLeader *ginleader, GinBuildState *state)
 	/* Shutdown worker processes */
 	WaitForParallelWorkersToFinish(ginleader->pcxt);
 
+	LogParallelWorkersIfNeeded(log_parallel_workers,
+							   ginleader->pcxt->nworkers_to_launch,
+							   ginleader->pcxt->nworkers_launched);
+
 	/*
 	 * Next, accumulate WAL usage.  (This must wait for the workers to finish,
 	 * or we might get incomplete data.)
diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c
index 756dfa3dcf4..2670e88cd22 100644
--- a/src/backend/access/nbtree/nbtsort.c
+++ b/src/backend/access/nbtree/nbtsort.c
@@ -1615,6 +1615,10 @@ _bt_end_parallel(BTLeader *btleader)
 	/* Shutdown worker processes */
 	WaitForParallelWorkersToFinish(btleader->pcxt);
 
+	LogParallelWorkersIfNeeded(log_parallel_workers,
+							   btleader->pcxt->nworkers_to_launch,
+							   btleader->pcxt->nworkers_launched);
+
 	/*
 	 * Next, accumulate WAL usage.  (This must wait for the workers to finish,
 	 * or we might get incomplete data.)
diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c
index 979c2be4abd..ffe0fcf076c 100644
--- a/src/backend/commands/vacuumparallel.c
+++ b/src/backend/commands/vacuumparallel.c
@@ -257,6 +257,9 @@ struct ParallelVacuumState
 	int			nindexes_parallel_cleanup;
 	int			nindexes_parallel_condcleanup;
 
+	int			nworkers_to_launch;
+	int			nworkers_launched;
+
 	/* Buffer access strategy used by leader process */
 	BufferAccessStrategy bstrategy;
 
@@ -424,6 +427,9 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes,
 		if ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) != 0)
 			pvs->nindexes_parallel_condcleanup++;
 	}
+	pvs->nworkers_to_launch = 0;
+	pvs->nworkers_launched = 0;
+
 	shm_toc_insert(pcxt->toc, PARALLEL_VACUUM_KEY_INDEX_STATS, indstats);
 	pvs->indstats = indstats;
 
@@ -516,6 +522,10 @@ parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats)
 {
 	Assert(!IsParallelWorker());
 
+	LogParallelWorkersIfNeeded(log_parallel_workers,
+							   pvs->nworkers_to_launch,
+							   pvs->nworkers_launched);
+
 	/* Copy the updated statistics */
 	for (int i = 0; i < pvs->nindexes; i++)
 	{
@@ -946,6 +956,9 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan
 
 		for (int i = 0; i < pvs->pcxt->nworkers_launched; i++)
 			InstrAccumParallelQuery(&pvs->buffer_usage[i], &pvs->wal_usage[i]);
+
+		pvs->nworkers_to_launch += pvs->pcxt->nworkers_to_launch;
+		pvs->nworkers_launched += pvs->pcxt->nworkers_launched;
 	}
 
 	/*
-- 
2.47.3

From f255b603c8af6bf6dd8e434a25c9771a1d4b7fce Mon Sep 17 00:00:00 2001
From: benoit <[email protected]>
Date: Wed, 29 Jan 2025 17:15:25 +0100
Subject: [PATCH 3/3] Implements logging for parallel worker usage in queries

---
 src/backend/executor/execMain.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index 4b30f768680..e478b21094c 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -499,6 +499,10 @@ standard_ExecutorEnd(QueryDesc *queryDesc)
 		pgstat_update_parallel_workers_stats((PgStat_Counter) estate->es_parallel_workers_to_launch,
 											 (PgStat_Counter) estate->es_parallel_workers_launched);
 
+	LogParallelWorkersIfNeeded(log_parallel_workers,
+								estate->es_parallel_workers_to_launch,
+								estate->es_parallel_workers_launched);
+
 	/*
 	 * Check that ExecutorFinish was called, unless in EXPLAIN-only mode. This
 	 * Assert is needed because ExecutorFinish is new as of 9.1, and callers
-- 
2.47.3

Reply via email to