Skip to content

Commit 89c546c

Browse files
committed
pgstat: split relation, database handling out of pgstat_report_stat().
pgstat_report_stat() handles several types of stats, yet relation stats have so far been handled directly in pgstat_report_stat(). A later commit will move the handling of the different kinds of stats into separate files. By splitting out relation handling in this commit that later move will just move code around without other changes. Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20220303021600.hs34ghqcw6zcokdh@alap3.anarazel.de
1 parent a3a75b9 commit 89c546c

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

src/backend/postmaster/pgstat.c

+56-23
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ static HTAB *pgStatTabHash = NULL;
222222
*/
223223
static HTAB *pgStatFunctions = NULL;
224224

225+
/*
226+
* Indicates if backend has some relation stats that it hasn't yet
227+
* sent to the collector.
228+
*/
229+
static bool have_relation_stats = false;
230+
225231
/*
226232
* Indicates if backend has some function stats that it hasn't yet
227233
* sent to the collector.
@@ -338,7 +344,9 @@ static bool pgstat_db_requested(Oid databaseid);
338344
static PgStat_StatReplSlotEntry *pgstat_get_replslot_entry(NameData name, bool create_it);
339345
static void pgstat_reset_replslot(PgStat_StatReplSlotEntry *slotstats, TimestampTz ts);
340346

347+
static void pgstat_send_tabstats(TimestampTz now, bool disconnect);
341348
static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now);
349+
static void pgstat_update_dbstats(PgStat_MsgTabstat *tsmsg, TimestampTz now);
342350
static void pgstat_send_funcstats(void);
343351
static void pgstat_send_slru(void);
344352
static HTAB *pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid);
@@ -866,15 +874,9 @@ allow_immediate_pgstat_restart(void)
866874
void
867875
pgstat_report_stat(bool disconnect)
868876
{
869-
/* we assume this inits to all zeroes: */
870-
static const PgStat_TableCounts all_zeroes;
871877
static TimestampTz last_report = 0;
872878

873879
TimestampTz now;
874-
PgStat_MsgTabstat regular_msg;
875-
PgStat_MsgTabstat shared_msg;
876-
TabStatusArray *tsa;
877-
int i;
878880

879881
pgstat_assert_is_up();
880882

@@ -887,7 +889,7 @@ pgstat_report_stat(bool disconnect)
887889
* generates no WAL records can write or sync WAL data when flushing the
888890
* data pages.
889891
*/
890-
if ((pgStatTabList == NULL || pgStatTabList->tsa_used == 0) &&
892+
if (!have_relation_stats &&
891893
pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
892894
pgWalUsage.wal_records == prevWalUsage.wal_records &&
893895
WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0 &&
@@ -908,6 +910,32 @@ pgstat_report_stat(bool disconnect)
908910
if (disconnect)
909911
pgstat_report_disconnect(MyDatabaseId);
910912

913+
/* First, send relation statistics */
914+
pgstat_send_tabstats(now, disconnect);
915+
916+
/* Now, send function statistics */
917+
pgstat_send_funcstats();
918+
919+
/* Send WAL statistics */
920+
pgstat_send_wal(true);
921+
922+
/* Finally send SLRU statistics */
923+
pgstat_send_slru();
924+
}
925+
926+
/*
927+
* Subroutine for pgstat_report_stat: Send relation statistics
928+
*/
929+
static void
930+
pgstat_send_tabstats(TimestampTz now, bool disconnect)
931+
{
932+
/* we assume this inits to all zeroes: */
933+
static const PgStat_TableCounts all_zeroes;
934+
PgStat_MsgTabstat regular_msg;
935+
PgStat_MsgTabstat shared_msg;
936+
TabStatusArray *tsa;
937+
int i;
938+
911939
/*
912940
* Destroy pgStatTabHash before we start invalidating PgStat_TableEntry
913941
* entries it points to. (Should we fail partway through the loop below,
@@ -980,18 +1008,11 @@ pgstat_report_stat(bool disconnect)
9801008
if (shared_msg.m_nentries > 0)
9811009
pgstat_send_tabstat(&shared_msg, now);
9821010

983-
/* Now, send function statistics */
984-
pgstat_send_funcstats();
985-
986-
/* Send WAL statistics */
987-
pgstat_send_wal(true);
988-
989-
/* Finally send SLRU statistics */
990-
pgstat_send_slru();
1011+
have_relation_stats = false;
9911012
}
9921013

9931014
/*
994-
* Subroutine for pgstat_report_stat: finish and send a tabstat message
1015+
* Subroutine for pgstat_send_tabstats: finish and send one tabstat message
9951016
*/
9961017
static void
9971018
pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
@@ -1007,6 +1028,23 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
10071028
* Report and reset accumulated xact commit/rollback and I/O timings
10081029
* whenever we send a normal tabstat message
10091030
*/
1031+
pgstat_update_dbstats(tsmsg, now);
1032+
1033+
n = tsmsg->m_nentries;
1034+
len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
1035+
n * sizeof(PgStat_TableEntry);
1036+
1037+
pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
1038+
pgstat_send(tsmsg, len);
1039+
}
1040+
1041+
/*
1042+
* Subroutine for pgstat_send_tabstat: Handle xact commit/rollback and I/O
1043+
* timings.
1044+
*/
1045+
static void
1046+
pgstat_update_dbstats(PgStat_MsgTabstat *tsmsg, TimestampTz now)
1047+
{
10101048
if (OidIsValid(tsmsg->m_databaseid))
10111049
{
10121050
tsmsg->m_xact_commit = pgStatXactCommit;
@@ -1052,13 +1090,6 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
10521090
tsmsg->m_active_time = 0;
10531091
tsmsg->m_idle_in_xact_time = 0;
10541092
}
1055-
1056-
n = tsmsg->m_nentries;
1057-
len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
1058-
n * sizeof(PgStat_TableEntry);
1059-
1060-
pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
1061-
pgstat_send(tsmsg, len);
10621093
}
10631094

10641095
/*
@@ -2179,6 +2210,8 @@ get_tabstat_entry(Oid rel_id, bool isshared)
21792210

21802211
pgstat_assert_is_up();
21812212

2213+
have_relation_stats = true;
2214+
21822215
/*
21832216
* Create hash table if we don't have it already.
21842217
*/

0 commit comments

Comments
 (0)