Add command column to pg_stat_progress_create_index

Started by Peter Eisentrautover 6 years ago7 messages
#1Peter Eisentraut
peter.eisentraut@2ndquadrant.com
1 attachment(s)

I propose to add a column "command" to pg_stat_progress_create_index.
The sibling view pg_stat_progress_cluster already contains such a
column. This can help distinguish which command is running and thus
which phases to expect. It seems reasonable to keep these views
consistent, too. (They are both new in PG12.) Patch attached.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

0001-Add-command-column-to-pg_stat_progress_create_index.patchtext/plain; charset=UTF-8; name=0001-Add-command-column-to-pg_stat_progress_create_index.patch; x-mac-creator=0; x-mac-type=0Download
From aa08a89ef9d99d0af2b22566626be2b13f09e292 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Mon, 27 May 2019 14:06:17 -0400
Subject: [PATCH] Add command column to pg_stat_progress_create_index

This allows determining which command is running, similar to
pg_stat_progress_cluster.
---
 doc/src/sgml/monitoring.sgml         | 11 ++++++++++-
 src/backend/catalog/index.c          |  2 ++
 src/backend/catalog/system_views.sql |  5 +++++
 src/backend/commands/indexcmds.c     |  8 ++++++++
 src/include/commands/progress.h      |  7 +++++++
 src/test/regress/expected/rules.out  |  7 +++++++
 6 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index 570ac5e06f..bf72d0c303 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -3534,6 +3534,15 @@ <title><structname>pg_stat_progress_create_index</structname> View</title>
       <entry>OID of the index being created or reindexed.  During a
       non-concurrent <command>CREATE INDEX</command>, this is 0.</entry>
      </row>
+     <row>
+      <entry><structfield>command</structfield></entry>
+      <entry><type>text</type></entry>
+      <entry>
+       The command that is running: <literal>CREATE INDEX</literal>,
+       <literal>CREATE INDEX CONCURRENTLY</literal>,
+       <literal>REINDEX</literal>, or <literal>REINDEX CONCURRENTLY</literal>.
+      </entry>
+     </row>
      <row>
       <entry><structfield>phase</structfield></entry>
       <entry><type>text</type></entry>
@@ -3965,7 +3974,7 @@ <title><structname>pg_stat_progress_cluster</structname> View</title>
      <entry><structfield>command</structfield></entry>
      <entry><type>text</type></entry>
      <entry>
-       The command that is running. Either CLUSTER or VACUUM FULL.
+      The command that is running. Either <literal>CLUSTER</literal> or <literal>VACUUM FULL</literal>.
      </entry>
     </row>
     <row>
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 50c8bb9ce6..d2e4f53a80 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -3278,6 +3278,8 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
 
 	pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
 								  heapId);
+	pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
+								 PROGRESS_CREATEIDX_COMMAND_REINDEX);
 	pgstat_progress_update_param(PROGRESS_CREATEIDX_INDEX_OID,
 								 indexId);
 
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index 52a6c31584..78a103cdb9 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -956,6 +956,11 @@ CREATE VIEW pg_stat_progress_create_index AS
         S.pid AS pid, S.datid AS datid, D.datname AS datname,
         S.relid AS relid,
         CAST(S.param7 AS oid) AS index_relid,
+        CASE S.param1 WHEN 1 THEN 'CREATE INDEX'
+                      WHEN 2 THEN 'CREATE INDEX CONCURRENTLY'
+                      WHEN 3 THEN 'REINDEX'
+                      WHEN 4 THEN 'REINDEX CONCURRENTLY'
+                      END AS command,
         CASE S.param10 WHEN 0 THEN 'initializing'
                        WHEN 1 THEN 'waiting for writers before build'
                        WHEN 2 THEN 'building index' ||
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 40ea629ffe..4d76da8293 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -498,8 +498,14 @@ DefineIndex(Oid relationId,
 	 * done.
 	 */
 	if (!OidIsValid(parentIndexId))
+	{
 		pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
 									  relationId);
+		pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
+									 stmt->concurrent ?
+									 PROGRESS_CREATEIDX_COMMAND_CREATE_CONCURRENTLY :
+									 PROGRESS_CREATEIDX_COMMAND_CREATE);
+	}
 
 	/*
 	 * No index OID to report yet
@@ -2923,6 +2929,8 @@ ReindexRelationConcurrently(Oid relationOid, int options)
 
 		pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
 									  RelationGetRelid(heapRel));
+		pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
+									 PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY);
 		pgstat_progress_update_param(PROGRESS_CREATEIDX_INDEX_OID,
 									 indexId);
 		pgstat_progress_update_param(PROGRESS_CREATEIDX_ACCESS_METHOD_OID,
diff --git a/src/include/commands/progress.h b/src/include/commands/progress.h
index 37043e926d..acd1313cb3 100644
--- a/src/include/commands/progress.h
+++ b/src/include/commands/progress.h
@@ -59,6 +59,7 @@
 
 /* Progress parameters for CREATE INDEX */
 /* 3, 4 and 5 reserved for "waitfor" metrics */
+#define PROGRESS_CREATEIDX_COMMAND				0
 #define PROGRESS_CREATEIDX_INDEX_OID			6
 #define PROGRESS_CREATEIDX_ACCESS_METHOD_OID	8
 #define PROGRESS_CREATEIDX_PHASE				9	/* AM-agnostic phase # */
@@ -86,6 +87,12 @@
 #define PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE	1
 /* Additional phases are defined by each AM */
 
+/* Commands of PROGRESS_CREATEIDX */
+#define PROGRESS_CREATEIDX_COMMAND_CREATE			1
+#define PROGRESS_CREATEIDX_COMMAND_CREATE_CONCURRENTLY	2
+#define PROGRESS_CREATEIDX_COMMAND_REINDEX		3
+#define PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY	4
+
 /* Lock holder wait counts */
 #define PROGRESS_WAITFOR_TOTAL					3
 #define PROGRESS_WAITFOR_DONE					4
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index 4363ca1663..7d365c48d1 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -1879,6 +1879,13 @@ pg_stat_progress_create_index| SELECT s.pid,
     d.datname,
     s.relid,
     (s.param7)::oid AS index_relid,
+        CASE s.param1
+            WHEN 1 THEN 'CREATE INDEX'::text
+            WHEN 2 THEN 'CREATE INDEX CONCURRENTLY'::text
+            WHEN 3 THEN 'REINDEX'::text
+            WHEN 4 THEN 'REINDEX CONCURRENTLY'::text
+            ELSE NULL::text
+        END AS command,
         CASE s.param10
             WHEN 0 THEN 'initializing'::text
             WHEN 1 THEN 'waiting for writers before build'::text
-- 
2.21.0

#2Andres Freund
andres@anarazel.de
In reply to: Peter Eisentraut (#1)
Re: Add command column to pg_stat_progress_create_index

Hi,

On 2019-05-27 14:18:12 -0400, Peter Eisentraut wrote:

I propose to add a column "command" to pg_stat_progress_create_index.
The sibling view pg_stat_progress_cluster already contains such a
column. This can help distinguish which command is running and thus
which phases to expect. It seems reasonable to keep these views
consistent, too. (They are both new in PG12.) Patch attached.

Seems like we should do that for v12 then?

Greetings,

Andres Freund

#3Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Peter Eisentraut (#1)
Re: Add command column to pg_stat_progress_create_index

On 2019-May-27, Peter Eisentraut wrote:

I propose to add a column "command" to pg_stat_progress_create_index.
The sibling view pg_stat_progress_cluster already contains such a
column. This can help distinguish which command is running and thus
which phases to expect. It seems reasonable to keep these views
consistent, too. (They are both new in PG12.) Patch attached.

+1.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#4Fabrízio de Royes Mello
fabriziomello@gmail.com
In reply to: Alvaro Herrera (#3)
Re: Add command column to pg_stat_progress_create_index

On Mon, May 27, 2019 at 4:51 PM Alvaro Herrera <alvherre@2ndquadrant.com>
wrote:

On 2019-May-27, Peter Eisentraut wrote:

I propose to add a column "command" to pg_stat_progress_create_index.
The sibling view pg_stat_progress_cluster already contains such a
column. This can help distinguish which command is running and thus
which phases to expect. It seems reasonable to keep these views
consistent, too. (They are both new in PG12.) Patch attached.

+1.

+1

--
Fabrízio de Royes Mello Timbira - http://www.timbira.com.br/
PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento

#5Michael Paquier
michael@paquier.xyz
In reply to: Andres Freund (#2)
Re: Add command column to pg_stat_progress_create_index

On Mon, May 27, 2019 at 11:20:28AM -0700, Andres Freund wrote:

On 2019-05-27 14:18:12 -0400, Peter Eisentraut wrote:

I propose to add a column "command" to pg_stat_progress_create_index.
The sibling view pg_stat_progress_cluster already contains such a
column. This can help distinguish which command is running and thus
which phases to expect. It seems reasonable to keep these views
consistent, too. (They are both new in PG12.) Patch attached.

Seems like we should do that for v12 then?

+1.
--
Michael
#6David Fetter
david@fetter.org
In reply to: Andres Freund (#2)
Re: Add command column to pg_stat_progress_create_index

On Mon, May 27, 2019 at 11:20:28AM -0700, Andres Freund wrote:

Hi,

On 2019-05-27 14:18:12 -0400, Peter Eisentraut wrote:

I propose to add a column "command" to pg_stat_progress_create_index.
The sibling view pg_stat_progress_cluster already contains such a
column. This can help distinguish which command is running and thus
which phases to expect. It seems reasonable to keep these views
consistent, too. (They are both new in PG12.) Patch attached.

Seems like we should do that for v12 then?

+1

Best,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

#7Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Peter Eisentraut (#1)
Re: Add command column to pg_stat_progress_create_index

On 2019-05-27 20:18, Peter Eisentraut wrote:

I propose to add a column "command" to pg_stat_progress_create_index.
The sibling view pg_stat_progress_cluster already contains such a
column. This can help distinguish which command is running and thus
which phases to expect. It seems reasonable to keep these views
consistent, too. (They are both new in PG12.) Patch attached.

committed

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services