From f118a9373d18861eaac19516b082c1e0b003a6e0 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 4 Apr 2021 18:07:44 -0700
Subject: [PATCH v60 11/17] pgstat: pgstat_copy_relation_stats.

---
 src/include/pgstat.h                         |  2 ++
 src/backend/catalog/index.c                  | 26 ++-------------
 src/backend/utils/activity/pgstat_relation.c | 34 ++++++++++++++++++++
 3 files changed, 38 insertions(+), 24 deletions(-)

diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index a34bb328e60..e07f119c71b 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -1000,6 +1000,8 @@ extern void pgstat_report_stat(bool force);
 extern void pgstat_vacuum_stat(void);
 extern void pgstat_drop_database(Oid databaseid);
 
+extern void pgstat_copy_relation_stats(Relation dstrel, Relation srcrel);
+
 extern void pgstat_clear_snapshot(void);
 extern void pgstat_reset_counters(void);
 extern void pgstat_reset_shared_counters(const char *);
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 7c5c2093328..3c43d8e5e67 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -1868,30 +1868,8 @@ index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
 	CommandCounterIncrement();
 	index_update_collation_versions(newIndexId, InvalidOid);
 
-	/*
-	 * Copy over statistics from old to new index
-	 */
-	{
-		PgStat_StatTabEntry *tabentry;
-
-		tabentry = pgstat_fetch_stat_tabentry(oldIndexId);
-		if (tabentry)
-		{
-			if (pgstat_relation_should_count(newClassRel))
-			{
-				newClassRel->pgstat_info->t_counts.t_numscans = tabentry->numscans;
-				newClassRel->pgstat_info->t_counts.t_tuples_returned = tabentry->tuples_returned;
-				newClassRel->pgstat_info->t_counts.t_tuples_fetched = tabentry->tuples_fetched;
-				newClassRel->pgstat_info->t_counts.t_blocks_fetched = tabentry->blocks_fetched;
-				newClassRel->pgstat_info->t_counts.t_blocks_hit = tabentry->blocks_hit;
-
-				/*
-				 * The data will be sent by the next pgstat_report_stat()
-				 * call.
-				 */
-			}
-		}
-	}
+	/* copy over statistics from old to new index */
+	pgstat_copy_relation_stats(newClassRel, oldClassRel);
 
 	/* Copy data of pg_statistic from the old index to the new one */
 	CopyStatistics(oldIndexId, newIndexId);
diff --git a/src/backend/utils/activity/pgstat_relation.c b/src/backend/utils/activity/pgstat_relation.c
index bc4ca2270e3..1a943eb7a2f 100644
--- a/src/backend/utils/activity/pgstat_relation.c
+++ b/src/backend/utils/activity/pgstat_relation.c
@@ -91,6 +91,40 @@ bool have_relation_stats;
 static HTAB *pgStatTabHash = NULL;
 
 
+/*
+ * Copy stats between relations. This is used for things like REINDEX
+ * CONCURRENTLY.
+ */
+void
+pgstat_copy_relation_stats(Relation dst, Relation src)
+{
+	PgStat_StatTabEntry *srcstats;
+
+	srcstats = pgstat_fetch_stat_tabentry(RelationGetRelid(src));
+
+	if (!srcstats)
+		return;
+
+	if (pgstat_relation_should_count(dst))
+	{
+		/*
+		 * XXX: temporarily this does not actually quite do what the name says,
+		 * and just copy index related fields. A subsequent commit will do more.
+		 */
+
+		dst->pgstat_info->t_counts.t_numscans = srcstats->numscans;
+		dst->pgstat_info->t_counts.t_tuples_returned = srcstats->tuples_returned;
+		dst->pgstat_info->t_counts.t_tuples_fetched = srcstats->tuples_fetched;
+		dst->pgstat_info->t_counts.t_blocks_fetched = srcstats->blocks_fetched;
+		dst->pgstat_info->t_counts.t_blocks_hit = srcstats->blocks_hit;
+
+		/*
+		 * The data will be sent by the next pgstat_report_stat()
+		 * call.
+		 */
+	}
+}
+
 /* ----------
  * pgstat_relation_init() -
  *
-- 
2.31.0.121.g9198c13e34

