Le lun. 7 oct. 2024 à 02:18, Michael Paquier <mich...@paquier.xyz> a écrit :

> On Sun, Oct 06, 2024 at 03:32:02PM +0200, Guillaume Lelarge wrote:
> > I'm not sure I follow. That would mean that every time a query is
> executed,
> > it always gets the same amount of workers. Which is not guaranteed to be
> > true.
> >
> > I would agree, though, that parallelized_queries_launched is probably not
> > that interesting. I could get rid of it if you think it should go away.
>
> My point is that these stats are useful to know which action may have
> to be taken when reaching a mean, and numbers in pg_stat_statements
> offer hints that something is going wrong and that a closer lookup at
> an EXPLAIN plan may be required, particularly if the total number of
> workers planned and launched aggregated in the counters is unbalanced
> across queries.  If the planned/launched ratio is balanced across most
> queries queries, a GUC adjustment may be OK.  If the ratio is very
> unbalanced in a lower set of queries, I'd also look at tweaking GUCs
> instead like the per_gather.  These counters give information that one
> or the other may be required.
>
> > Well, I don't see this as an overlap. Rather more information.
>
> Later versions of Benoit's patch have been accumulating this data in
> the executor state.  v4 posted at [1] has the following diffs:
> --- a/src/include/nodes/execnodes.h
> +++ b/src/include/nodes/execnodes.h
> @@ -724,6 +724,9 @@ typedef struct EState
>          */
>         List       *es_insert_pending_result_relations;
>         List       *es_insert_pending_modifytables;
> +
> +       int                     es_workers_launched;
> +       int                     es_workers_planned;
>  } EState;
>
> Your v2 posted on this thread has that:
> @@ -707,6 +707,12 @@ typedef struct EState
>         struct EPQState *es_epq_active;
>
>         bool            es_use_parallel_mode;   /* can we use parallel
> workers? */
> +       bool            es_used_parallel_mode;  /* was executed in
> parallel */
> +       int                     es_parallelized_workers_launched;
> +       int                     es_parallelized_workers_planned;
> +       int                     es_parallelized_nodes; /* # of
> parallelized nodes */
> +       int                     es_parallelized_nodes_all_workers; /* # of
> nodes with all workers launched */
> +       int                     es_parallelized_nodes_no_worker; /* # of
> nodes with no workers launched */
>
> es_parallelized_workers_launched and es_workers_launched are the same
> thing in both.
>
>
My bad. I agree this is the way to go. See patch v3-0001 attached.


> > On this, I would agree with you. They are not that particularly useful to
> > get better setting for parallel GUCs. I can drop them if you want.
>
> Yep.  I would remove them for now.  This leads to more bloat.
>
>
Done. See patch v3-0002 attached.


> > Did this on the v2 version of the patch (attached here).
> >
> > Thanks for your review. If you want the parallelized_queries_launched
> > column and the parallelized_nodes_* columns dropped, I can do that on a
> v3
> > patch.
>
> I'd recommend to split that into more independent patches:
> - Introduce the two counters in EState with the incrementations done
> in nodeGatherMerge.c and nodeGather.c (mentioned that at [2], you may
> want to coordinate with Benoit to avoid duplicating the work).
> - Expand pg_stat_statements to use them for DMLs, SELECTs, well where
> they matter.
> - Look at expanding that for utilities that can do parallel jobs:
> CREATE INDEX and VACUUM, but this has lower priority to me, and this
> can reuse the same counters as the ones added by patch 2.
>
>
The first two are done. The last one is beyond my scope.

I'm now working on Benoit's patch to make it work with my v3-0001 patch.
I'll send the resulting patch on his thread.


> [1]:
> https://www.postgresql.org/message-id/6ecad3ad-835c-486c-9ebd-da87a9a97...@dalibo.com
> [2]: https://www.postgresql.org/message-id/zv46wtmjltuu2...@paquier.xyz
> --
> Michael
>

Regards.


-- 
Guillaume.
From 95b300eeff0168f2618418102df660f1ba9b9113 Mon Sep 17 00:00:00 2001
From: Guillaume Lelarge <guillaume.lela...@dalibo.com>
Date: Mon, 7 Oct 2024 08:45:36 +0200
Subject: [PATCH v3 1/2] Introduce two new counters in EState

They will be used by two other patchs to populate new columns in
pg_stat_database and pg_statements.
---
 src/backend/executor/execUtils.c       | 3 +++
 src/backend/executor/nodeGather.c      | 3 +++
 src/backend/executor/nodeGatherMerge.c | 3 +++
 src/include/nodes/execnodes.h          | 3 +++
 4 files changed, 12 insertions(+)

diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c
index 5737f9f4eb..1908481999 100644
--- a/src/backend/executor/execUtils.c
+++ b/src/backend/executor/execUtils.c
@@ -162,6 +162,9 @@ CreateExecutorState(void)
 	estate->es_jit_flags = 0;
 	estate->es_jit = NULL;
 
+	estate->es_parallelized_workers_launched = 0;
+	estate->es_parallelized_workers_planned = 0;
+
 	/*
 	 * Return the executor state structure
 	 */
diff --git a/src/backend/executor/nodeGather.c b/src/backend/executor/nodeGather.c
index 5d4ffe989c..0fb915175a 100644
--- a/src/backend/executor/nodeGather.c
+++ b/src/backend/executor/nodeGather.c
@@ -182,6 +182,9 @@ ExecGather(PlanState *pstate)
 			/* We save # workers launched for the benefit of EXPLAIN */
 			node->nworkers_launched = pcxt->nworkers_launched;
 
+			estate->es_parallelized_workers_launched += pcxt->nworkers_launched;
+			estate->es_parallelized_workers_planned += pcxt->nworkers_to_launch;
+
 			/* Set up tuple queue readers to read the results. */
 			if (pcxt->nworkers_launched > 0)
 			{
diff --git a/src/backend/executor/nodeGatherMerge.c b/src/backend/executor/nodeGatherMerge.c
index 45f6017c29..149ab23d90 100644
--- a/src/backend/executor/nodeGatherMerge.c
+++ b/src/backend/executor/nodeGatherMerge.c
@@ -223,6 +223,9 @@ ExecGatherMerge(PlanState *pstate)
 			/* We save # workers launched for the benefit of EXPLAIN */
 			node->nworkers_launched = pcxt->nworkers_launched;
 
+			estate->es_parallelized_workers_launched += pcxt->nworkers_launched;
+			estate->es_parallelized_workers_planned += pcxt->nworkers_to_launch;
+
 			/* Set up tuple queue readers to read the results. */
 			if (pcxt->nworkers_launched > 0)
 			{
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index aab59d681c..f898590ece 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -708,6 +708,9 @@ typedef struct EState
 
 	bool		es_use_parallel_mode;	/* can we use parallel workers? */
 
+	int			es_parallelized_workers_launched;
+	int			es_parallelized_workers_planned;
+
 	/* The per-query shared memory area to use for parallel execution. */
 	struct dsa_area *es_query_dsa;
 
-- 
2.46.2

From bf882530dbbc4423a24ae7f9a0bd22f674664832 Mon Sep 17 00:00:00 2001
From: Guillaume Lelarge <guillaume.lela...@dalibo.com>
Date: Mon, 7 Oct 2024 09:47:09 +0200
Subject: [PATCH v3 2/2] Add parallel columns to pg_stat_statements

There are four new columns:
 * parallelized_queries_planned (number of times the query has been planned to
   be parallelized),
 * parallel_ized_querieslaunched (number of times the query has been executed
   with parallelization),
 * parallelized_workers_planned (number of parallel workers planned for
   this query),
 * parallelized_workers_launched (number of parallel workers executed for
   this query).

These new columns will help to monitor and better configure query
parallelization.
---
 contrib/pg_stat_statements/Makefile           |  4 +-
 .../expected/oldextversions.out               | 66 +++++++++++++++
 .../pg_stat_statements/expected/parallel.out  | 35 ++++++++
 .../pg_stat_statements--1.11--1.12.sql        | 77 ++++++++++++++++++
 .../pg_stat_statements/pg_stat_statements.c   | 81 +++++++++++++++++--
 .../pg_stat_statements.control                |  2 +-
 .../pg_stat_statements/sql/oldextversions.sql |  5 ++
 contrib/pg_stat_statements/sql/parallel.sql   | 24 ++++++
 doc/src/sgml/pgstatstatements.sgml            | 36 +++++++++
 src/backend/executor/execUtils.c              |  1 +
 src/backend/executor/nodeGather.c             |  2 +
 src/backend/executor/nodeGatherMerge.c        |  2 +
 src/include/nodes/execnodes.h                 |  1 +
 13 files changed, 326 insertions(+), 10 deletions(-)
 create mode 100644 contrib/pg_stat_statements/expected/parallel.out
 create mode 100644 contrib/pg_stat_statements/pg_stat_statements--1.11--1.12.sql
 create mode 100644 contrib/pg_stat_statements/sql/parallel.sql

diff --git a/contrib/pg_stat_statements/Makefile b/contrib/pg_stat_statements/Makefile
index 1622b43ded..0caeea0c03 100644
--- a/contrib/pg_stat_statements/Makefile
+++ b/contrib/pg_stat_statements/Makefile
@@ -7,7 +7,7 @@ OBJS = \
 
 EXTENSION = pg_stat_statements
 DATA = pg_stat_statements--1.4.sql \
-	pg_stat_statements--1.10--1.11.sql \
+	pg_stat_statements--1.11--1.12.sql pg_stat_statements--1.10--1.11.sql \
 	pg_stat_statements--1.9--1.10.sql pg_stat_statements--1.8--1.9.sql \
 	pg_stat_statements--1.7--1.8.sql pg_stat_statements--1.6--1.7.sql \
 	pg_stat_statements--1.5--1.6.sql pg_stat_statements--1.4--1.5.sql \
@@ -19,7 +19,7 @@ LDFLAGS_SL += $(filter -lm, $(LIBS))
 
 REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_stat_statements/pg_stat_statements.conf
 REGRESS = select dml cursors utility level_tracking planning \
-	user_activity wal entry_timestamp privileges extended cleanup \
+	user_activity wal entry_timestamp privileges extended parallel cleanup \
 	oldextversions
 # Disabled because these tests require "shared_preload_libraries=pg_stat_statements",
 # which typical installcheck users do not have (e.g. buildfarm clients).
diff --git a/contrib/pg_stat_statements/expected/oldextversions.out b/contrib/pg_stat_statements/expected/oldextversions.out
index 5842c930e5..1937224f56 100644
--- a/contrib/pg_stat_statements/expected/oldextversions.out
+++ b/contrib/pg_stat_statements/expected/oldextversions.out
@@ -342,4 +342,70 @@ SELECT pg_stat_statements_reset() IS NOT NULL AS t;
  t
 (1 row)
 
+-- New functions and views for pg_stat_statements in 1.12
+AlTER EXTENSION pg_stat_statements UPDATE TO '1.12';
+\d pg_stat_statements
+                             View "public.pg_stat_statements"
+            Column             |           Type           | Collation | Nullable | Default 
+-------------------------------+--------------------------+-----------+----------+---------
+ userid                        | oid                      |           |          | 
+ dbid                          | oid                      |           |          | 
+ toplevel                      | boolean                  |           |          | 
+ queryid                       | bigint                   |           |          | 
+ query                         | text                     |           |          | 
+ plans                         | bigint                   |           |          | 
+ total_plan_time               | double precision         |           |          | 
+ min_plan_time                 | double precision         |           |          | 
+ max_plan_time                 | double precision         |           |          | 
+ mean_plan_time                | double precision         |           |          | 
+ stddev_plan_time              | double precision         |           |          | 
+ calls                         | bigint                   |           |          | 
+ total_exec_time               | double precision         |           |          | 
+ min_exec_time                 | double precision         |           |          | 
+ max_exec_time                 | double precision         |           |          | 
+ mean_exec_time                | double precision         |           |          | 
+ stddev_exec_time              | double precision         |           |          | 
+ rows                          | bigint                   |           |          | 
+ shared_blks_hit               | bigint                   |           |          | 
+ shared_blks_read              | bigint                   |           |          | 
+ shared_blks_dirtied           | bigint                   |           |          | 
+ shared_blks_written           | bigint                   |           |          | 
+ local_blks_hit                | bigint                   |           |          | 
+ local_blks_read               | bigint                   |           |          | 
+ local_blks_dirtied            | bigint                   |           |          | 
+ local_blks_written            | bigint                   |           |          | 
+ temp_blks_read                | bigint                   |           |          | 
+ temp_blks_written             | bigint                   |           |          | 
+ shared_blk_read_time          | double precision         |           |          | 
+ shared_blk_write_time         | double precision         |           |          | 
+ local_blk_read_time           | double precision         |           |          | 
+ local_blk_write_time          | double precision         |           |          | 
+ temp_blk_read_time            | double precision         |           |          | 
+ temp_blk_write_time           | double precision         |           |          | 
+ wal_records                   | bigint                   |           |          | 
+ wal_fpi                       | bigint                   |           |          | 
+ wal_bytes                     | numeric                  |           |          | 
+ jit_functions                 | bigint                   |           |          | 
+ jit_generation_time           | double precision         |           |          | 
+ jit_inlining_count            | bigint                   |           |          | 
+ jit_inlining_time             | double precision         |           |          | 
+ jit_optimization_count        | bigint                   |           |          | 
+ jit_optimization_time         | double precision         |           |          | 
+ jit_emission_count            | bigint                   |           |          | 
+ jit_emission_time             | double precision         |           |          | 
+ jit_deform_count              | bigint                   |           |          | 
+ jit_deform_time               | double precision         |           |          | 
+ parallelized_queries_planned  | bigint                   |           |          | 
+ parallelized_queries_launched | bigint                   |           |          | 
+ parallelized_workers_planned  | bigint                   |           |          | 
+ parallelized_workers_launched | bigint                   |           |          | 
+ stats_since                   | timestamp with time zone |           |          | 
+ minmax_stats_since            | timestamp with time zone |           |          | 
+
+SELECT count(*) > 0 AS has_data FROM pg_stat_statements;
+ has_data 
+----------
+ t
+(1 row)
+
 DROP EXTENSION pg_stat_statements;
diff --git a/contrib/pg_stat_statements/expected/parallel.out b/contrib/pg_stat_statements/expected/parallel.out
new file mode 100644
index 0000000000..6e7c8ffb46
--- /dev/null
+++ b/contrib/pg_stat_statements/expected/parallel.out
@@ -0,0 +1,35 @@
+--
+-- Validate parallelization generation metrics
+--
+SET parallel_setup_cost TO 0;
+SET min_parallel_table_scan_size TO '100kB';
+SET pg_stat_statements.track_utility = FALSE;
+CREATE TABLE pgss_parallel_tab (a int);
+INSERT INTO pgss_parallel_tab SELECT generate_series(1, 10_000);
+SELECT count(*) FROM pgss_parallel_tab;
+ count 
+-------
+ 10000
+(1 row)
+
+DROP TABLE pgss_parallel_tab;
+-- Check parallelization metrics are generated for the SELECT statement
+SELECT query,
+  parallelized_queries_planned > 0 AS parallelized_queries_planned_generated,
+  parallelized_queries_launched > 0 AS parallelized_queries_launched_generated,
+  parallelized_workers_planned > 0 AS parallelized_workers_planned_generated,
+  parallelized_workers_launched > 0 AS parallelized_workers_launched_generated
+FROM pg_stat_statements
+WHERE query LIKE 'SELECT count%'
+ORDER BY query COLLATE "C";
+                 query                  | parallelized_queries_planned_generated | parallelized_queries_launched_generated | parallelized_workers_planned_generated | parallelized_workers_launched_generated 
+----------------------------------------+----------------------------------------+-----------------------------------------+----------------------------------------+-----------------------------------------
+ SELECT count(*) FROM pgss_parallel_tab | t                                      | t                                       | t                                      | t
+(1 row)
+
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
diff --git a/contrib/pg_stat_statements/pg_stat_statements--1.11--1.12.sql b/contrib/pg_stat_statements/pg_stat_statements--1.11--1.12.sql
new file mode 100644
index 0000000000..fe6e81eb0f
--- /dev/null
+++ b/contrib/pg_stat_statements/pg_stat_statements--1.11--1.12.sql
@@ -0,0 +1,77 @@
+/* contrib/pg_stat_statements/pg_stat_statements--1.11--1.12.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION pg_stat_statements UPDATE TO '1.12'" to load this file. \quit
+
+/* First we have to remove them from the extension */
+ALTER EXTENSION pg_stat_statements DROP VIEW pg_stat_statements;
+ALTER EXTENSION pg_stat_statements DROP FUNCTION pg_stat_statements(boolean);
+
+/* Then we can drop them */
+DROP VIEW pg_stat_statements;
+DROP FUNCTION pg_stat_statements(boolean);
+
+/* Now redefine */
+CREATE FUNCTION pg_stat_statements(IN showtext boolean,
+    OUT userid oid,
+    OUT dbid oid,
+    OUT toplevel bool,
+    OUT queryid bigint,
+    OUT query text,
+    OUT plans int8,
+    OUT total_plan_time float8,
+    OUT min_plan_time float8,
+    OUT max_plan_time float8,
+    OUT mean_plan_time float8,
+    OUT stddev_plan_time float8,
+    OUT calls int8,
+    OUT total_exec_time float8,
+    OUT min_exec_time float8,
+    OUT max_exec_time float8,
+    OUT mean_exec_time float8,
+    OUT stddev_exec_time float8,
+    OUT rows int8,
+    OUT shared_blks_hit int8,
+    OUT shared_blks_read int8,
+    OUT shared_blks_dirtied int8,
+    OUT shared_blks_written int8,
+    OUT local_blks_hit int8,
+    OUT local_blks_read int8,
+    OUT local_blks_dirtied int8,
+    OUT local_blks_written int8,
+    OUT temp_blks_read int8,
+    OUT temp_blks_written int8,
+    OUT shared_blk_read_time float8,
+    OUT shared_blk_write_time float8,
+    OUT local_blk_read_time float8,
+    OUT local_blk_write_time float8,
+    OUT temp_blk_read_time float8,
+    OUT temp_blk_write_time float8,
+    OUT wal_records int8,
+    OUT wal_fpi int8,
+    OUT wal_bytes numeric,
+    OUT jit_functions int8,
+    OUT jit_generation_time float8,
+    OUT jit_inlining_count int8,
+    OUT jit_inlining_time float8,
+    OUT jit_optimization_count int8,
+    OUT jit_optimization_time float8,
+    OUT jit_emission_count int8,
+    OUT jit_emission_time float8,
+    OUT jit_deform_count int8,
+    OUT jit_deform_time float8,
+    OUT parallelized_queries_planned int8,
+    OUT parallelized_queries_launched int8,
+    OUT parallelized_workers_planned int8,
+    OUT parallelized_workers_launched int8,
+    OUT stats_since timestamp with time zone,
+    OUT minmax_stats_since timestamp with time zone
+)
+RETURNS SETOF record
+AS 'MODULE_PATHNAME', 'pg_stat_statements_1_12'
+LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
+
+CREATE VIEW pg_stat_statements AS
+  SELECT * FROM pg_stat_statements(true);
+
+GRANT SELECT ON pg_stat_statements TO PUBLIC;
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 5765ef49b4..939a27e829 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -113,6 +113,7 @@ typedef enum pgssVersion
 	PGSS_V1_9,
 	PGSS_V1_10,
 	PGSS_V1_11,
+	PGSS_V1_12,
 } pgssVersion;
 
 typedef enum pgssStoreKind
@@ -204,6 +205,10 @@ typedef struct Counters
 	int64		jit_emission_count; /* number of times emission time has been
 									 * > 0 */
 	double		jit_emission_time;	/* total time to emit jit code */
+	int64       parallelized_queries_planned;	/* # of times query was planned to use parallelism */
+	int64       parallelized_queries_launched;	/* # of times query was executed using parallelism */
+	int64       parallelized_workers_planned;	/* # of parallel workers planned */
+	int64       parallelized_workers_launched;	/* # of parallel workers launched */
 } Counters;
 
 /*
@@ -317,6 +322,7 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_8);
 PG_FUNCTION_INFO_V1(pg_stat_statements_1_9);
 PG_FUNCTION_INFO_V1(pg_stat_statements_1_10);
 PG_FUNCTION_INFO_V1(pg_stat_statements_1_11);
+PG_FUNCTION_INFO_V1(pg_stat_statements_1_12);
 PG_FUNCTION_INFO_V1(pg_stat_statements);
 PG_FUNCTION_INFO_V1(pg_stat_statements_info);
 
@@ -347,7 +353,11 @@ static void pgss_store(const char *query, uint64 queryId,
 					   const BufferUsage *bufusage,
 					   const WalUsage *walusage,
 					   const struct JitInstrumentation *jitusage,
-					   JumbleState *jstate);
+					   JumbleState *jstate,
+					   bool parallelized_queries_planned,
+					   bool parallelized_queries_launched,
+					   int parallelized_workers_planned,
+					   int parallelized_workers_launched);
 static void pg_stat_statements_internal(FunctionCallInfo fcinfo,
 										pgssVersion api_version,
 										bool showtext);
@@ -867,7 +877,11 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
 				   NULL,
 				   NULL,
 				   NULL,
-				   jstate);
+				   jstate,
+				   false,
+				   false,
+				   0,
+				   0);
 }
 
 /*
@@ -945,7 +959,11 @@ pgss_planner(Query *parse,
 				   &bufusage,
 				   &walusage,
 				   NULL,
-				   NULL);
+				   NULL,
+				   false,
+				   false,
+				   0,
+				   0);
 	}
 	else
 	{
@@ -1078,7 +1096,11 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
 				   &queryDesc->totaltime->bufusage,
 				   &queryDesc->totaltime->walusage,
 				   queryDesc->estate->es_jit ? &queryDesc->estate->es_jit->instr : NULL,
-				   NULL);
+				   NULL,
+				   queryDesc->plannedstmt->parallelModeNeeded,
+				   queryDesc->estate->es_used_parallel_mode,
+				   queryDesc->estate->es_parallelized_workers_planned,
+				   queryDesc->estate->es_parallelized_workers_launched);
 	}
 
 	if (prev_ExecutorEnd)
@@ -1209,7 +1231,11 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
 				   &bufusage,
 				   &walusage,
 				   NULL,
-				   NULL);
+				   NULL,
+				   false,
+				   false,
+				   0,
+				   0);
 	}
 	else
 	{
@@ -1270,7 +1296,11 @@ pgss_store(const char *query, uint64 queryId,
 		   const BufferUsage *bufusage,
 		   const WalUsage *walusage,
 		   const struct JitInstrumentation *jitusage,
-		   JumbleState *jstate)
+		   JumbleState *jstate,
+		   bool parallelized_queries_planned,
+		   bool parallelized_queries_launched,
+		   int parallelized_workers_planned,
+		   int parallelized_workers_launched)
 {
 	pgssHashKey key;
 	pgssEntry  *entry;
@@ -1472,6 +1502,17 @@ pgss_store(const char *query, uint64 queryId,
 				entry->counters.jit_emission_count++;
 			entry->counters.jit_emission_time += INSTR_TIME_GET_MILLISEC(jitusage->emission_counter);
 		}
+		/* inc parallel counters */
+		if (parallelized_queries_planned)
+		{
+			entry->counters.parallelized_queries_planned += 1;
+		}
+		if (parallelized_queries_launched)
+		{
+			entry->counters.parallelized_queries_launched += 1;
+		}
+		entry->counters.parallelized_workers_planned += parallelized_workers_planned;
+		entry->counters.parallelized_workers_launched += parallelized_workers_launched;
 
 		SpinLockRelease(&entry->mutex);
 	}
@@ -1539,7 +1580,8 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
 #define PG_STAT_STATEMENTS_COLS_V1_9	33
 #define PG_STAT_STATEMENTS_COLS_V1_10	43
 #define PG_STAT_STATEMENTS_COLS_V1_11	49
-#define PG_STAT_STATEMENTS_COLS			49	/* maximum of above */
+#define PG_STAT_STATEMENTS_COLS_V1_12	53
+#define PG_STAT_STATEMENTS_COLS			53	/* maximum of above */
 
 /*
  * Retrieve statement statistics.
@@ -1551,6 +1593,16 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
  * expected API version is identified by embedding it in the C name of the
  * function.  Unfortunately we weren't bright enough to do that for 1.1.
  */
+Datum
+pg_stat_statements_1_12(PG_FUNCTION_ARGS)
+{
+	bool		showtext = PG_GETARG_BOOL(0);
+
+	pg_stat_statements_internal(fcinfo, PGSS_V1_12, showtext);
+
+	return (Datum) 0;
+}
+
 Datum
 pg_stat_statements_1_11(PG_FUNCTION_ARGS)
 {
@@ -1695,6 +1747,10 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 			if (api_version != PGSS_V1_11)
 				elog(ERROR, "incorrect number of output arguments");
 			break;
+		case PG_STAT_STATEMENTS_COLS_V1_12:
+			if (api_version != PGSS_V1_12)
+				elog(ERROR, "incorrect number of output arguments");
+			break;
 		default:
 			elog(ERROR, "incorrect number of output arguments");
 	}
@@ -1932,6 +1988,16 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 		{
 			values[i++] = Int64GetDatumFast(tmp.jit_deform_count);
 			values[i++] = Float8GetDatumFast(tmp.jit_deform_time);
+		}
+		if (api_version >= PGSS_V1_12)
+		{
+			values[i++] = Int64GetDatumFast(tmp.parallelized_queries_planned);
+			values[i++] = Int64GetDatumFast(tmp.parallelized_queries_launched);
+			values[i++] = Int64GetDatumFast(tmp.parallelized_workers_planned);
+			values[i++] = Int64GetDatumFast(tmp.parallelized_workers_launched);
+		}
+		if (api_version >= PGSS_V1_11)
+		{
 			values[i++] = TimestampTzGetDatum(stats_since);
 			values[i++] = TimestampTzGetDatum(minmax_stats_since);
 		}
@@ -1944,6 +2010,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 					 api_version == PGSS_V1_9 ? PG_STAT_STATEMENTS_COLS_V1_9 :
 					 api_version == PGSS_V1_10 ? PG_STAT_STATEMENTS_COLS_V1_10 :
 					 api_version == PGSS_V1_11 ? PG_STAT_STATEMENTS_COLS_V1_11 :
+					 api_version == PGSS_V1_12 ? PG_STAT_STATEMENTS_COLS_V1_12 :
 					 -1 /* fail if you forget to update this assert */ ));
 
 		tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
diff --git a/contrib/pg_stat_statements/pg_stat_statements.control b/contrib/pg_stat_statements/pg_stat_statements.control
index 8a76106ec6..d45ebc12e3 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.control
+++ b/contrib/pg_stat_statements/pg_stat_statements.control
@@ -1,5 +1,5 @@
 # pg_stat_statements extension
 comment = 'track planning and execution statistics of all SQL statements executed'
-default_version = '1.11'
+default_version = '1.12'
 module_pathname = '$libdir/pg_stat_statements'
 relocatable = true
diff --git a/contrib/pg_stat_statements/sql/oldextversions.sql b/contrib/pg_stat_statements/sql/oldextversions.sql
index 38d5505d0d..13b8ca2858 100644
--- a/contrib/pg_stat_statements/sql/oldextversions.sql
+++ b/contrib/pg_stat_statements/sql/oldextversions.sql
@@ -58,4 +58,9 @@ SELECT count(*) > 0 AS has_data FROM pg_stat_statements;
 SELECT pg_get_functiondef('pg_stat_statements_reset'::regproc);
 SELECT pg_stat_statements_reset() IS NOT NULL AS t;
 
+-- New functions and views for pg_stat_statements in 1.12
+AlTER EXTENSION pg_stat_statements UPDATE TO '1.12';
+\d pg_stat_statements
+SELECT count(*) > 0 AS has_data FROM pg_stat_statements;
+
 DROP EXTENSION pg_stat_statements;
diff --git a/contrib/pg_stat_statements/sql/parallel.sql b/contrib/pg_stat_statements/sql/parallel.sql
new file mode 100644
index 0000000000..8b32edffb5
--- /dev/null
+++ b/contrib/pg_stat_statements/sql/parallel.sql
@@ -0,0 +1,24 @@
+--
+-- Validate parallelization generation metrics
+--
+
+SET parallel_setup_cost TO 0;
+SET min_parallel_table_scan_size TO '100kB';
+SET pg_stat_statements.track_utility = FALSE;
+
+CREATE TABLE pgss_parallel_tab (a int);
+INSERT INTO pgss_parallel_tab SELECT generate_series(1, 10_000);
+SELECT count(*) FROM pgss_parallel_tab;
+DROP TABLE pgss_parallel_tab;
+
+-- Check parallelization metrics are generated for the SELECT statement
+SELECT query,
+  parallelized_queries_planned > 0 AS parallelized_queries_planned_generated,
+  parallelized_queries_launched > 0 AS parallelized_queries_launched_generated,
+  parallelized_workers_planned > 0 AS parallelized_workers_planned_generated,
+  parallelized_workers_launched > 0 AS parallelized_workers_launched_generated
+FROM pg_stat_statements
+WHERE query LIKE 'SELECT count%'
+ORDER BY query COLLATE "C";
+
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
diff --git a/doc/src/sgml/pgstatstatements.sgml b/doc/src/sgml/pgstatstatements.sgml
index 9b0aff73b1..ce66fe1b16 100644
--- a/doc/src/sgml/pgstatstatements.sgml
+++ b/doc/src/sgml/pgstatstatements.sgml
@@ -527,6 +527,42 @@
       </para></entry>
      </row>
 
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>parallelized_queries_planned</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Number of times the statement was planned to use parallelism.
+      </para></entry>
+     </row>
+
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>parallel_queries_launched</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Number of times that the statement was executed using parallelism
+      </para></entry>
+     </row>
+
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>parallelized_workers_planned</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Number of parallel workers planned for the query.
+      </para></entry>
+     </row>
+
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>parallel_workers_launched</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Number of parallel workers executed for the query.
+      </para></entry>
+     </row>
+
      <row>
       <entry role="catalog_table_entry"><para role="column_definition">
        <structfield>stats_since</structfield> <type>timestamp with time zone</type>
diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c
index 1908481999..634ba5caed 100644
--- a/src/backend/executor/execUtils.c
+++ b/src/backend/executor/execUtils.c
@@ -158,6 +158,7 @@ CreateExecutorState(void)
 	estate->es_sourceText = NULL;
 
 	estate->es_use_parallel_mode = false;
+	estate->es_used_parallel_mode = false;
 
 	estate->es_jit_flags = 0;
 	estate->es_jit = NULL;
diff --git a/src/backend/executor/nodeGather.c b/src/backend/executor/nodeGather.c
index 0fb915175a..489bfbfe87 100644
--- a/src/backend/executor/nodeGather.c
+++ b/src/backend/executor/nodeGather.c
@@ -182,6 +182,8 @@ ExecGather(PlanState *pstate)
 			/* We save # workers launched for the benefit of EXPLAIN */
 			node->nworkers_launched = pcxt->nworkers_launched;
 
+			if(pcxt->nworkers_launched > 0)
+				estate->es_used_parallel_mode = true;
 			estate->es_parallelized_workers_launched += pcxt->nworkers_launched;
 			estate->es_parallelized_workers_planned += pcxt->nworkers_to_launch;
 
diff --git a/src/backend/executor/nodeGatherMerge.c b/src/backend/executor/nodeGatherMerge.c
index 149ab23d90..df0a36d3ac 100644
--- a/src/backend/executor/nodeGatherMerge.c
+++ b/src/backend/executor/nodeGatherMerge.c
@@ -223,6 +223,8 @@ ExecGatherMerge(PlanState *pstate)
 			/* We save # workers launched for the benefit of EXPLAIN */
 			node->nworkers_launched = pcxt->nworkers_launched;
 
+			if(pcxt->nworkers_launched > 0)
+				estate->es_used_parallel_mode = true;
 			estate->es_parallelized_workers_launched += pcxt->nworkers_launched;
 			estate->es_parallelized_workers_planned += pcxt->nworkers_to_launch;
 
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index f898590ece..9e67de4a4d 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -708,6 +708,7 @@ typedef struct EState
 
 	bool		es_use_parallel_mode;	/* can we use parallel workers? */
 
+	bool		es_used_parallel_mode;	/* was executed in parallel */
 	int			es_parallelized_workers_launched;
 	int			es_parallelized_workers_planned;
 
-- 
2.46.2

Reply via email to