@@ -222,6 +222,12 @@ static HTAB *pgStatTabHash = NULL;
222
222
*/
223
223
static HTAB * pgStatFunctions = NULL ;
224
224
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
+
225
231
/*
226
232
* Indicates if backend has some function stats that it hasn't yet
227
233
* sent to the collector.
@@ -338,7 +344,9 @@ static bool pgstat_db_requested(Oid databaseid);
338
344
static PgStat_StatReplSlotEntry * pgstat_get_replslot_entry (NameData name , bool create_it );
339
345
static void pgstat_reset_replslot (PgStat_StatReplSlotEntry * slotstats , TimestampTz ts );
340
346
347
+ static void pgstat_send_tabstats (TimestampTz now , bool disconnect );
341
348
static void pgstat_send_tabstat (PgStat_MsgTabstat * tsmsg , TimestampTz now );
349
+ static void pgstat_update_dbstats (PgStat_MsgTabstat * tsmsg , TimestampTz now );
342
350
static void pgstat_send_funcstats (void );
343
351
static void pgstat_send_slru (void );
344
352
static HTAB * pgstat_collect_oids (Oid catalogid , AttrNumber anum_oid );
@@ -866,15 +874,9 @@ allow_immediate_pgstat_restart(void)
866
874
void
867
875
pgstat_report_stat (bool disconnect )
868
876
{
869
- /* we assume this inits to all zeroes: */
870
- static const PgStat_TableCounts all_zeroes ;
871
877
static TimestampTz last_report = 0 ;
872
878
873
879
TimestampTz now ;
874
- PgStat_MsgTabstat regular_msg ;
875
- PgStat_MsgTabstat shared_msg ;
876
- TabStatusArray * tsa ;
877
- int i ;
878
880
879
881
pgstat_assert_is_up ();
880
882
@@ -887,7 +889,7 @@ pgstat_report_stat(bool disconnect)
887
889
* generates no WAL records can write or sync WAL data when flushing the
888
890
* data pages.
889
891
*/
890
- if (( pgStatTabList == NULL || pgStatTabList -> tsa_used == 0 ) &&
892
+ if (! have_relation_stats &&
891
893
pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
892
894
pgWalUsage .wal_records == prevWalUsage .wal_records &&
893
895
WalStats .m_wal_write == 0 && WalStats .m_wal_sync == 0 &&
@@ -908,6 +910,32 @@ pgstat_report_stat(bool disconnect)
908
910
if (disconnect )
909
911
pgstat_report_disconnect (MyDatabaseId );
910
912
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
+
911
939
/*
912
940
* Destroy pgStatTabHash before we start invalidating PgStat_TableEntry
913
941
* entries it points to. (Should we fail partway through the loop below,
@@ -980,18 +1008,11 @@ pgstat_report_stat(bool disconnect)
980
1008
if (shared_msg .m_nentries > 0 )
981
1009
pgstat_send_tabstat (& shared_msg , now );
982
1010
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;
991
1012
}
992
1013
993
1014
/*
994
- * Subroutine for pgstat_report_stat : finish and send a tabstat message
1015
+ * Subroutine for pgstat_send_tabstats : finish and send one tabstat message
995
1016
*/
996
1017
static void
997
1018
pgstat_send_tabstat (PgStat_MsgTabstat * tsmsg , TimestampTz now )
@@ -1007,6 +1028,23 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
1007
1028
* Report and reset accumulated xact commit/rollback and I/O timings
1008
1029
* whenever we send a normal tabstat message
1009
1030
*/
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
+ {
1010
1048
if (OidIsValid (tsmsg -> m_databaseid ))
1011
1049
{
1012
1050
tsmsg -> m_xact_commit = pgStatXactCommit ;
@@ -1052,13 +1090,6 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now)
1052
1090
tsmsg -> m_active_time = 0 ;
1053
1091
tsmsg -> m_idle_in_xact_time = 0 ;
1054
1092
}
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 );
1062
1093
}
1063
1094
1064
1095
/*
@@ -2179,6 +2210,8 @@ get_tabstat_entry(Oid rel_id, bool isshared)
2179
2210
2180
2211
pgstat_assert_is_up ();
2181
2212
2213
+ have_relation_stats = true;
2214
+
2182
2215
/*
2183
2216
* Create hash table if we don't have it already.
2184
2217
*/
0 commit comments