From f96e435a89f08a086437de991a017ce8aa176d2c 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 04/11] pgstat: Split out relation, database stats handling
 from pgstat_report_stat().

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/backend/postmaster/pgstat.c | 98 +++++++++++++++++++++++----------
 1 file changed, 69 insertions(+), 29 deletions(-)

diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 1f5d95e2f40..16fe2df4ec9 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -222,6 +222,12 @@ static HTAB *pgStatTabHash = NULL;
  */
 static HTAB *pgStatFunctions = NULL;
 
+/*
+ * Indicates if backend has some relation stats that it hasn't yet
+ * sent to the collector.
+ */
+static bool have_relation_stats = false;
+
 /*
  * Indicates if backend has some function stats that it hasn't yet
  * sent to the collector.
@@ -346,8 +352,11 @@ static bool pgstat_db_requested(Oid databaseid);
 static PgStat_StatReplSlotEntry *pgstat_get_replslot_entry(NameData name, bool create_it);
 static void pgstat_reset_replslot(PgStat_StatReplSlotEntry *slotstats, TimestampTz ts);
 
+static void pgstat_send_tabstats(TimestampTz now, bool disconnect);
 static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now);
+static void pgstat_update_dbstats(PgStat_MsgTabstat *tsmsg, TimestampTz now);
 static void pgstat_send_funcstats(void);
+static void pgstat_wal_initialize(void);
 static void pgstat_send_slru(void);
 static HTAB *pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid);
 static bool pgstat_should_report_connstat(void);
@@ -874,15 +883,9 @@ allow_immediate_pgstat_restart(void)
 void
 pgstat_report_stat(bool disconnect)
 {
-	/* we assume this inits to all zeroes: */
-	static const PgStat_TableCounts all_zeroes;
 	static TimestampTz last_report = 0;
 
 	TimestampTz now;
-	PgStat_MsgTabstat regular_msg;
-	PgStat_MsgTabstat shared_msg;
-	TabStatusArray *tsa;
-	int			i;
 
 	pgstat_assert_is_up();
 
@@ -895,7 +898,7 @@ pgstat_report_stat(bool disconnect)
 	 * generates no WAL records can write or sync WAL data when flushing the
 	 * data pages.
 	 */
-	if ((pgStatTabList == NULL || pgStatTabList->tsa_used == 0) &&
+	if (!have_relation_stats &&
 		pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
 		pgWalUsage.wal_records == prevWalUsage.wal_records &&
 		WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0 &&
@@ -916,6 +919,32 @@ pgstat_report_stat(bool disconnect)
 	if (disconnect)
 		pgstat_report_disconnect(MyDatabaseId);
 
+	/* First, send relation statistics */
+	pgstat_send_tabstats(now, disconnect);
+
+	/* Now, send function statistics */
+	pgstat_send_funcstats();
+
+	/* Send WAL statistics */
+	pgstat_send_wal(true);
+
+	/* Finally send SLRU statistics */
+	pgstat_send_slru();
+}
+
+/*
+ * Subroutine for pgstat_report_stat: Send relation statistics
+ */
+static void
+pgstat_send_tabstats(TimestampTz now, bool disconnect)
+{
+	/* we assume this inits to all zeroes: */
+	static const PgStat_TableCounts all_zeroes;
+	PgStat_MsgTabstat regular_msg;
+	PgStat_MsgTabstat shared_msg;
+	TabStatusArray *tsa;
+	int			i;
+
 	/*
 	 * Destroy pgStatTabHash before we start invalidating PgStat_TableEntry
 	 * entries it points to.  (Should we fail partway through the loop below,
@@ -988,18 +1017,11 @@ pgstat_report_stat(bool disconnect)
 	if (shared_msg.m_nentries > 0)
 		pgstat_send_tabstat(&shared_msg, now);
 
-	/* Now, send function statistics */
-	pgstat_send_funcstats();
-
-	/* Send WAL statistics */
-	pgstat_send_wal(true);
-
-	/* Finally send SLRU statistics */
-	pgstat_send_slru();
+	have_relation_stats = false;
 }
 
 /*
- * Subroutine for pgstat_report_stat: finish and send a tabstat message
+ * Subroutine for pgstat_send_tabstats: finish and send one tabstat message
  */
 static void
 pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
@@ -1015,6 +1037,23 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
 	 * Report and reset accumulated xact commit/rollback and I/O timings
 	 * whenever we send a normal tabstat message
 	 */
+	pgstat_update_dbstats(tsmsg, now);
+
+	n = tsmsg->m_nentries;
+	len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
+		n * sizeof(PgStat_TableEntry);
+
+	pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
+	pgstat_send(tsmsg, len);
+}
+
+/*
+ * Subroutine for pgstat_send_tabstat: Handle xact commit/rollback and I/O
+ * timings.
+ */
+static void
+pgstat_update_dbstats(PgStat_MsgTabstat *tsmsg, TimestampTz now)
+{
 	if (OidIsValid(tsmsg->m_databaseid))
 	{
 		tsmsg->m_xact_commit = pgStatXactCommit;
@@ -1060,13 +1099,6 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
 		tsmsg->m_active_time = 0;
 		tsmsg->m_idle_in_xact_time = 0;
 	}
-
-	n = tsmsg->m_nentries;
-	len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
-		n * sizeof(PgStat_TableEntry);
-
-	pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
-	pgstat_send(tsmsg, len);
 }
 
 /*
@@ -2200,6 +2232,8 @@ get_tabstat_entry(Oid rel_id, bool isshared)
 
 	pgstat_assert_is_up();
 
+	have_relation_stats = true;
+
 	/*
 	 * Create hash table if we don't have it already.
 	 */
@@ -3152,12 +3186,7 @@ pgstat_initialize(void)
 {
 	Assert(!pgstat_is_initialized);
 
-	/*
-	 * Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
-	 * calculate how much pgWalUsage counters are increased by subtracting
-	 * prevWalUsage from pgWalUsage.
-	 */
-	prevWalUsage = pgWalUsage;
+	pgstat_wal_initialize();
 
 	/* Set up a process-exit hook to clean up */
 	before_shmem_exit(pgstat_shutdown_hook, 0);
@@ -3399,6 +3428,17 @@ pgstat_send_wal(bool force)
 	MemSet(&WalStats, 0, sizeof(WalStats));
 }
 
+static void
+pgstat_wal_initialize(void)
+{
+	/*
+	 * Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
+	 * calculate how much pgWalUsage counters are increased by subtracting
+	 * prevWalUsage from pgWalUsage.
+	 */
+	prevWalUsage = pgWalUsage;
+}
+
 /* ----------
  * pgstat_send_slru() -
  *
-- 
2.35.1.354.g715d08a9e5

