From b65c9317c790b333118b8e737a9f523503ed8637 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 2 Mar 2022 18:02:03 -0800
Subject: [PATCH v65 07/11] pgstat: Add 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 8d3e8ebf9cf..0fbf198fdd1 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -1162,6 +1162,8 @@ extern PgStat_MsgWal WalStats;
  * ----------
  */
 
+extern void pgstat_copy_relation_stats(Relation dstrel, Relation srcrel);
+
 extern void pgstat_relation_init(Relation rel);
 extern void pgstat_relation_assoc(Relation rel);
 
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index a9e69b8122b..ec36716cb4f 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -1734,30 +1734,8 @@ index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
 	changeDependenciesOf(RelationRelationId, oldIndexId, newIndexId);
 	changeDependenciesOn(RelationRelationId, oldIndexId, newIndexId);
 
-	/*
-	 * 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 018f6a4d68d..074c4b8958d 100644
--- a/src/backend/utils/activity/pgstat_relation.c
+++ b/src/backend/utils/activity/pgstat_relation.c
@@ -96,6 +96,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.35.1.354.g715d08a9e5

