Report stats when replaying XLOG_RUNNING_XACTS
authorAndres Freund <[email protected]>
Mon, 12 Jun 2023 22:06:12 +0000 (15:06 -0700)
committerAndres Freund <[email protected]>
Mon, 12 Jun 2023 22:06:39 +0000 (15:06 -0700)
Previously stats in the startup process would only get reported during
shutdown of the startup process. It has been that way for a long time, but
became a lot more noticeable with the new pg_stat_io view, which separates out
IO done by different backend types...

While replaying after every XLOG_RUNNING_XACTS isn't the prettiest approach,
it has the advantage of being quite easy. Given that we're well past feature
freeze...

It's not a problem that we don't report stats more frequently with
wal_level=minimal, in that case stats can't be read before the stats process
has shut down.

Besides the above, this commit also changes pgstat_report_stat() to acquire
the timestamp with GetCurrentTimestamp() instead of
GetCurrentTransactionStopTimestamp().

Thanks to Melih Mutlu, Kyotaro Horiguchi for s of other approaches to
solving this issue.

Reported-by: Fujii Masao <[email protected]>
Discussion: https://postgr.es/m/5315aedc-fbca-1556-c5de-dc2e00b23a14@oss.nttdata.com

src/backend/storage/ipc/standby.c
src/backend/utils/activity/pgstat.c

index ffe5e1563f52cb0bfaf9776fba886f54334af903..70b0da50fc107ad124f23f09769d5d560cf38217 100644 (file)
@@ -1193,6 +1193,15 @@ standby_redo(XLogReaderState *record)
        running.xids = xlrec->xids;
 
        ProcArrayApplyRecoveryInfo(&running);
+
+       /*
+        * The startup process currently has no convenient way to schedule
+        * stats to be reported. XLOG_RUNNING_XACTS records issued at a
+        * regular cadence, making this a convenient location to report
+        * stats. While these records aren't generated with wal_level=minimal,
+        * stats also cannot be accessed during WAL replay.
+        */
+       pgstat_report_stat(true);
    }
    else if (info == XLOG_INVALIDATIONS)
    {
index 0cdb552631ee2dc51b60c6a31a6cd4f80d52320f..d743fc0b289e4c38424f6f22b153f4f1d9868161 100644 (file)
@@ -615,10 +615,21 @@ pgstat_report_stat(bool force)
     */
    Assert(!pgStatLocal.shmem->is_shutdown);
 
-   now = GetCurrentTransactionStopTimestamp();
-
-   if (!force)
+   if (force)
    {
+       /*
+        * Stats reports are forced either when it's been too long since stats
+        * have been reported or in processes that force stats reporting to
+        * happen at specific points (including shutdown). In the former case
+        * the transaction stop time might be quite old, in the latter it
+        * would never get cleared.
+        */
+       now = GetCurrentTimestamp();
+   }
+   else
+   {
+       now = GetCurrentTransactionStopTimestamp();
+
        if (pending_since > 0 &&
            TimestampDifferenceExceeds(pending_since, now, PGSTAT_MAX_INTERVAL))
        {