Previously, (auto)analyze used global variables VacuumPageHit,
VacuumPageMiss, and VacuumPageDirty to track buffer usage. However,
pgBufferUsage provides a more generic way to track buffer usage with
support functions.
This change replaces those global variables with pgBufferUsage in
analyze. Since analyze was the sole user of those variables, it
removes their declarations. Vacuum previously used those variables but
replaced them with pgBufferUsage as part of a bug fix, commit
5cd72cc0c.
Additionally, it adjusts the buffer usage message in both vacuum and
analyze for better consistency.
Author: Anthonin Bonnefoy
Reviewed-by: Masahiko Sawada, Michael Paquier
Discussion: https://postgr.es/m/CAO6_Xqr__kTTCLkftqS0qSCm-J7_xbRG3Ge2rWhucxQJMJhcRA%40mail.gmail.com
                        int32           diff;
                        double          read_rate = 0,
                                                write_rate = 0;
+                       int64           total_blks_hit;
+                       int64           total_blks_read;
+                       int64           total_blks_dirtied;
 
                        TimestampDifference(starttime, endtime, &secs_dur, &usecs_dur);
                        memset(&walusage, 0, sizeof(WalUsage));
                        memset(&bufferusage, 0, sizeof(BufferUsage));
                        BufferUsageAccumDiff(&bufferusage, &pgBufferUsage, &startbufferusage);
 
+                       total_blks_hit = bufferusage.shared_blks_hit +
+                               bufferusage.local_blks_hit;
+                       total_blks_read = bufferusage.shared_blks_read +
+                               bufferusage.local_blks_read;
+                       total_blks_dirtied = bufferusage.shared_blks_dirtied +
+                               bufferusage.local_blks_dirtied;
+
                        initStringInfo(&buf);
                        if (verbose)
                        {
                        }
                        if (secs_dur > 0 || usecs_dur > 0)
                        {
-                               read_rate = (double) BLCKSZ * (bufferusage.shared_blks_read + bufferusage.local_blks_read) /
+                               read_rate = (double) BLCKSZ * total_blks_read /
                                        (1024 * 1024) / (secs_dur + usecs_dur / 1000000.0);
-                               write_rate = (double) BLCKSZ * (bufferusage.shared_blks_dirtied + bufferusage.local_blks_dirtied) /
+                               write_rate = (double) BLCKSZ * total_blks_dirtied /
                                        (1024 * 1024) / (secs_dur + usecs_dur / 1000000.0);
                        }
                        appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"),
                                                         read_rate, write_rate);
                        appendStringInfo(&buf,
-                                                        _("buffer usage: %lld hits, %lld misses, %lld dirtied\n"),
-                                                        (long long) (bufferusage.shared_blks_hit + bufferusage.local_blks_hit),
-                                                        (long long) (bufferusage.shared_blks_read + bufferusage.local_blks_read),
-                                                        (long long) (bufferusage.shared_blks_dirtied + bufferusage.local_blks_dirtied));
+                                                        _("buffer usage: %lld hits, %lld reads, %lld dirtied\n"),
+                                                        (long long) total_blks_hit,
+                                                        (long long) total_blks_read,
+                                                        (long long) total_blks_dirtied);
                        appendStringInfo(&buf,
                                                         _("WAL usage: %lld records, %lld full page images, %llu bytes\n"),
                                                         (long long) walusage.wal_records,
 
        Oid                     save_userid;
        int                     save_sec_context;
        int                     save_nestlevel;
-       int64           AnalyzePageHit = VacuumPageHit;
-       int64           AnalyzePageMiss = VacuumPageMiss;
-       int64           AnalyzePageDirty = VacuumPageDirty;
+       BufferUsage startbufferusage = pgBufferUsage;
+       BufferUsage bufferusage;
        PgStat_Counter startreadtime = 0;
        PgStat_Counter startwritetime = 0;
 
                        double          read_rate = 0;
                        double          write_rate = 0;
                        StringInfoData buf;
+                       int64           total_blks_hit;
+                       int64           total_blks_read;
+                       int64           total_blks_dirtied;
 
-                       /*
-                        * Calculate the difference in the Page Hit/Miss/Dirty that
-                        * happened as part of the analyze by subtracting out the
-                        * pre-analyze values which we saved above.
-                        */
-                       AnalyzePageHit = VacuumPageHit - AnalyzePageHit;
-                       AnalyzePageMiss = VacuumPageMiss - AnalyzePageMiss;
-                       AnalyzePageDirty = VacuumPageDirty - AnalyzePageDirty;
+                       memset(&bufferusage, 0, sizeof(BufferUsage));
+                       BufferUsageAccumDiff(&bufferusage, &pgBufferUsage, &startbufferusage);
+
+                       total_blks_hit = bufferusage.shared_blks_hit +
+                               bufferusage.local_blks_hit;
+                       total_blks_read = bufferusage.shared_blks_read +
+                               bufferusage.local_blks_read;
+                       total_blks_dirtied = bufferusage.shared_blks_dirtied +
+                               bufferusage.local_blks_dirtied;
 
                        /*
                         * We do not expect an analyze to take > 25 days and it simplifies
 
                        if (delay_in_ms > 0)
                        {
-                               read_rate = (double) BLCKSZ * AnalyzePageMiss / (1024 * 1024) /
-                                       (delay_in_ms / 1000.0);
-                               write_rate = (double) BLCKSZ * AnalyzePageDirty / (1024 * 1024) /
-                                       (delay_in_ms / 1000.0);
+                               read_rate = (double) BLCKSZ * total_blks_read /
+                                       (1024 * 1024) / (delay_in_ms / 1000.0);
+                               write_rate = (double) BLCKSZ * total_blks_dirtied /
+                                       (1024 * 1024) / (delay_in_ms / 1000.0);
                        }
 
                        /*
                        }
                        appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"),
                                                         read_rate, write_rate);
-                       appendStringInfo(&buf, _("buffer usage: %lld hits, %lld misses, %lld dirtied\n"),
-                                                        (long long) AnalyzePageHit,
-                                                        (long long) AnalyzePageMiss,
-                                                        (long long) AnalyzePageDirty);
+                       appendStringInfo(&buf, _("buffer usage: %lld hits, %lld reads, %lld dirtied\n"),
+                                                        (long long) total_blks_hit,
+                                                        (long long) total_blks_read,
+                                                        (long long) total_blks_dirtied);
                        appendStringInfo(&buf, _("system usage: %s"), pg_rusage_show(&ru0));
 
                        ereport(LOG,
 
                VacuumFailsafeActive = false;
                VacuumUpdateCosts();
                VacuumCostBalance = 0;
-               VacuumPageHit = 0;
-               VacuumPageMiss = 0;
-               VacuumPageDirty = 0;
                VacuumCostBalanceLocal = 0;
                VacuumSharedCostBalance = NULL;
                VacuumActiveNWorkers = NULL;
 
        /* Set cost-based vacuum delay */
        VacuumUpdateCosts();
        VacuumCostBalance = 0;
-       VacuumPageHit = 0;
-       VacuumPageMiss = 0;
-       VacuumPageDirty = 0;
        VacuumCostBalanceLocal = 0;
        VacuumSharedCostBalance = &(shared->cost_balance);
        VacuumActiveNWorkers = &(shared->active_nworkers);
 
        }
        if (*foundPtr)
        {
-               VacuumPageHit++;
                pgstat_count_io_op(io_object, io_context, IOOP_HIT);
                if (VacuumCostActive)
                        VacuumCostBalance += VacuumCostPageHit;
                                                                                          false);
                }
 
-               VacuumPageMiss += io_buffers_len;
                if (VacuumCostActive)
                        VacuumCostBalance += VacuumCostPageMiss * io_buffers_len;
        }
         */
        if (!(old_buf_state & BM_DIRTY))
        {
-               VacuumPageDirty++;
                pgBufferUsage.shared_blks_dirtied++;
                if (VacuumCostActive)
                        VacuumCostBalance += VacuumCostPageDirty;
 
                if (dirtied)
                {
-                       VacuumPageDirty++;
                        pgBufferUsage.shared_blks_dirtied++;
                        if (VacuumCostActive)
                                VacuumCostBalance += VacuumCostPageDirty;
 
 int                    VacuumCostLimit = 200;
 double         VacuumCostDelay = 0;
 
-int64          VacuumPageHit = 0;
-int64          VacuumPageMiss = 0;
-int64          VacuumPageDirty = 0;
-
 int                    VacuumCostBalance = 0;  /* working state for vacuum */
 bool           VacuumCostActive = false;
 
 
 extern PGDLLIMPORT int VacuumCostLimit;
 extern PGDLLIMPORT double VacuumCostDelay;
 
-extern PGDLLIMPORT int64 VacuumPageHit;
-extern PGDLLIMPORT int64 VacuumPageMiss;
-extern PGDLLIMPORT int64 VacuumPageDirty;
-
 extern PGDLLIMPORT int VacuumCostBalance;
 extern PGDLLIMPORT bool VacuumCostActive;