@@ -133,8 +133,8 @@ PgStat_MsgWal WalStats;
133
133
134
134
/*
135
135
* WAL usage counters saved from pgWALUsage at the previous call to
136
- * pgstat_report_wal (). This is used to calculate how much WAL usage
137
- * happens between pgstat_report_wal () calls, by substracting
136
+ * pgstat_send_wal (). This is used to calculate how much WAL usage
137
+ * happens between pgstat_send_wal () calls, by substracting
138
138
* the previous counters from the current ones.
139
139
*/
140
140
static WalUsage prevWalUsage ;
@@ -852,9 +852,19 @@ pgstat_report_stat(bool disconnect)
852
852
TabStatusArray * tsa ;
853
853
int i ;
854
854
855
- /* Don't expend a clock check if nothing to do */
855
+ /*
856
+ * Don't expend a clock check if nothing to do.
857
+ *
858
+ * To determine whether any WAL activity has occurred since last time, not
859
+ * only the number of generated WAL records but also the numbers of WAL
860
+ * writes and syncs need to be checked. Because even transaction that
861
+ * generates no WAL records can write or sync WAL data when flushing the
862
+ * data pages.
863
+ */
856
864
if ((pgStatTabList == NULL || pgStatTabList -> tsa_used == 0 ) &&
857
865
pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
866
+ pgWalUsage .wal_records == prevWalUsage .wal_records &&
867
+ WalStats .m_wal_write == 0 && WalStats .m_wal_sync == 0 &&
858
868
!have_function_stats && !disconnect )
859
869
return ;
860
870
@@ -948,7 +958,7 @@ pgstat_report_stat(bool disconnect)
948
958
pgstat_send_funcstats ();
949
959
950
960
/* Send WAL statistics */
951
- pgstat_report_wal ( );
961
+ pgstat_send_wal (true );
952
962
953
963
/* Finally send SLRU statistics */
954
964
pgstat_send_slru ();
@@ -2918,7 +2928,7 @@ void
2918
2928
pgstat_initialize (void )
2919
2929
{
2920
2930
/*
2921
- * Initialize prevWalUsage with pgWalUsage so that pgstat_report_wal () can
2931
+ * Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal () can
2922
2932
* calculate how much pgWalUsage counters are increased by substracting
2923
2933
* prevWalUsage from pgWalUsage.
2924
2934
*/
@@ -3030,83 +3040,88 @@ pgstat_send_bgwriter(void)
3030
3040
MemSet (& BgWriterStats , 0 , sizeof (BgWriterStats ));
3031
3041
}
3032
3042
3033
- /* ----------
3034
- * pgstat_report_wal() -
3035
- *
3036
- * Calculate how much WAL usage counters are increased and send
3037
- * WAL statistics to the collector.
3038
- *
3039
- * Must be called by processes that generate WAL.
3040
- * ----------
3041
- */
3042
- void
3043
- pgstat_report_wal (void )
3044
- {
3045
- WalUsage walusage ;
3046
-
3047
- /*
3048
- * Calculate how much WAL usage counters are increased by substracting the
3049
- * previous counters from the current ones. Fill the results in WAL stats
3050
- * message.
3051
- */
3052
- MemSet (& walusage , 0 , sizeof (WalUsage ));
3053
- WalUsageAccumDiff (& walusage , & pgWalUsage , & prevWalUsage );
3054
-
3055
- WalStats .m_wal_records = walusage .wal_records ;
3056
- WalStats .m_wal_fpi = walusage .wal_fpi ;
3057
- WalStats .m_wal_bytes = walusage .wal_bytes ;
3058
-
3059
- /*
3060
- * Send WAL stats message to the collector.
3061
- */
3062
- if (!pgstat_send_wal (true))
3063
- return ;
3064
-
3065
- /*
3066
- * Save the current counters for the subsequent calculation of WAL usage.
3067
- */
3068
- prevWalUsage = pgWalUsage ;
3069
- }
3070
-
3071
3043
/* ----------
3072
3044
* pgstat_send_wal() -
3073
3045
*
3074
3046
* Send WAL statistics to the collector.
3075
3047
*
3076
3048
* If 'force' is not set, WAL stats message is only sent if enough time has
3077
3049
* passed since last one was sent to reach PGSTAT_STAT_INTERVAL.
3078
- *
3079
- * Return true if the message is sent, and false otherwise.
3080
3050
* ----------
3081
3051
*/
3082
- bool
3052
+ void
3083
3053
pgstat_send_wal (bool force )
3084
3054
{
3085
- /* We assume this initializes to zeroes */
3086
- static const PgStat_MsgWal all_zeroes ;
3087
3055
static TimestampTz sendTime = 0 ;
3088
3056
3089
3057
/*
3090
3058
* This function can be called even if nothing at all has happened. In
3091
3059
* this case, avoid sending a completely empty message to the stats
3092
3060
* collector.
3061
+ *
3062
+ * Check wal_records counter to determine whether any WAL activity has
3063
+ * happened since last time. Note that other WalUsage counters don't need
3064
+ * to be checked because they are incremented always together with
3065
+ * wal_records counter.
3066
+ *
3067
+ * m_wal_buffers_full also doesn't need to be checked because it's
3068
+ * incremented only when at least one WAL record is generated (i.e.,
3069
+ * wal_records counter is incremented). But for safely, we assert that
3070
+ * m_wal_buffers_full is always zero when no WAL record is generated
3071
+ *
3072
+ * This function can be called by a process like walwriter that normally
3073
+ * generates no WAL records. To determine whether any WAL activity has
3074
+ * happened at that process since the last time, the numbers of WAL writes
3075
+ * and syncs are also checked.
3093
3076
*/
3094
- if (memcmp (& WalStats , & all_zeroes , sizeof (PgStat_MsgWal )) == 0 )
3095
- return false;
3077
+ if (pgWalUsage .wal_records == prevWalUsage .wal_records &&
3078
+ WalStats .m_wal_write == 0 && WalStats .m_wal_sync == 0 )
3079
+ {
3080
+ Assert (WalStats .m_wal_buffers_full == 0 );
3081
+ return ;
3082
+ }
3096
3083
3097
3084
if (!force )
3098
3085
{
3099
3086
TimestampTz now = GetCurrentTimestamp ();
3100
3087
3101
3088
/*
3102
3089
* Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
3103
- * msec since we last sent one.
3090
+ * msec since we last sent one to avoid overloading the stats
3091
+ * collector.
3104
3092
*/
3105
3093
if (!TimestampDifferenceExceeds (sendTime , now , PGSTAT_STAT_INTERVAL ))
3106
- return false ;
3094
+ return ;
3107
3095
sendTime = now ;
3108
3096
}
3109
3097
3098
+ /*
3099
+ * Set the counters related to generated WAL data if the counters were
3100
+ * updated.
3101
+ */
3102
+ if (pgWalUsage .wal_records != prevWalUsage .wal_records )
3103
+ {
3104
+ WalUsage walusage ;
3105
+
3106
+ /*
3107
+ * Calculate how much WAL usage counters were increased by
3108
+ * substracting the previous counters from the current ones. Fill the
3109
+ * results in WAL stats message.
3110
+ */
3111
+ MemSet (& walusage , 0 , sizeof (WalUsage ));
3112
+ WalUsageAccumDiff (& walusage , & pgWalUsage , & prevWalUsage );
3113
+
3114
+ WalStats .m_wal_records = walusage .wal_records ;
3115
+ WalStats .m_wal_fpi = walusage .wal_fpi ;
3116
+ WalStats .m_wal_bytes = walusage .wal_bytes ;
3117
+
3118
+ /*
3119
+ * Save the current counters for the subsequent calculation of WAL
3120
+ * usage.
3121
+ */
3122
+ prevWalUsage = pgWalUsage ;
3123
+ }
3124
+
3110
3125
/*
3111
3126
* Prepare and send the message
3112
3127
*/
@@ -3117,8 +3132,6 @@ pgstat_send_wal(bool force)
3117
3132
* Clear out the statistics buffer, so it can be re-used.
3118
3133
*/
3119
3134
MemSet (& WalStats , 0 , sizeof (WalStats ));
3120
-
3121
- return true;
3122
3135
}
3123
3136
3124
3137
/* ----------
0 commit comments