Use pgBufferUsage for buffer usage tracking in analyze.
authorMasahiko Sawada <[email protected]>
Wed, 14 Aug 2024 01:49:45 +0000 (18:49 -0700)
committerMasahiko Sawada <[email protected]>
Wed, 14 Aug 2024 01:49:45 +0000 (18:49 -0700)
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

src/backend/access/heap/vacuumlazy.c
src/backend/commands/analyze.c
src/backend/commands/vacuum.c
src/backend/commands/vacuumparallel.c
src/backend/storage/buffer/bufmgr.c
src/backend/utils/init/globals.c
src/include/miscadmin.h

index 835b53415d0b7b5861f3966944e622016432dcb4..d82aa3d489636f896d99aac98ab442214a399d8d 100644 (file)
@@ -608,6 +608,9 @@ heap_vacuum_rel(Relation rel, VacuumParams *params,
            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));
@@ -615,6 +618,13 @@ heap_vacuum_rel(Relation rel, VacuumParams *params,
            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)
            {
@@ -740,18 +750,18 @@ heap_vacuum_rel(Relation rel, VacuumParams *params,
            }
            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,
index c590a2adc35c05e1be88e11554f24f862761ce97..501592322182adf8d8f225bb65ef59b1d64cd30e 100644 (file)
@@ -303,9 +303,8 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
    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;
 
@@ -736,15 +735,19 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
            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
@@ -770,10 +773,10 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
 
            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);
            }
 
            /*
@@ -796,10 +799,10 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
            }
            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,
index 48f8eab20226417bf979cd36d51f7170007a72ca..7d8e9d20454293518d9fc952076d398f5d572f85 100644 (file)
@@ -603,9 +603,6 @@ vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy,
        VacuumFailsafeActive = false;
        VacuumUpdateCosts();
        VacuumCostBalance = 0;
-       VacuumPageHit = 0;
-       VacuumPageMiss = 0;
-       VacuumPageDirty = 0;
        VacuumCostBalanceLocal = 0;
        VacuumSharedCostBalance = NULL;
        VacuumActiveNWorkers = NULL;
index f26070bff2a12b3b3809ac798d37e1a403cba027..22c057fe61be1d1c1e094bf14dab7814e27da4ee 100644 (file)
@@ -1043,9 +1043,6 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc)
    /* Set cost-based vacuum delay */
    VacuumUpdateCosts();
    VacuumCostBalance = 0;
-   VacuumPageHit = 0;
-   VacuumPageMiss = 0;
-   VacuumPageDirty = 0;
    VacuumCostBalanceLocal = 0;
    VacuumSharedCostBalance = &(shared->cost_balance);
    VacuumActiveNWorkers = &(shared->active_nworkers);
index 4415ba648ae43b5f801a2188dd752bfaf398b230..fb38c7bdd4577fac265595d1e8b0d9bd007a4ade 100644 (file)
@@ -1192,7 +1192,6 @@ PinBufferForBlock(Relation rel,
    }
    if (*foundPtr)
    {
-       VacuumPageHit++;
        pgstat_count_io_op(io_object, io_context, IOOP_HIT);
        if (VacuumCostActive)
            VacuumCostBalance += VacuumCostPageHit;
@@ -1588,7 +1587,6 @@ WaitReadBuffers(ReadBuffersOperation *operation)
                                              false);
        }
 
-       VacuumPageMiss += io_buffers_len;
        if (VacuumCostActive)
            VacuumCostBalance += VacuumCostPageMiss * io_buffers_len;
    }
@@ -2582,7 +2580,6 @@ MarkBufferDirty(Buffer buffer)
     */
    if (!(old_buf_state & BM_DIRTY))
    {
-       VacuumPageDirty++;
        pgBufferUsage.shared_blks_dirtied++;
        if (VacuumCostActive)
            VacuumCostBalance += VacuumCostPageDirty;
@@ -5122,7 +5119,6 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
 
        if (dirtied)
        {
-           VacuumPageDirty++;
            pgBufferUsage.shared_blks_dirtied++;
            if (VacuumCostActive)
                VacuumCostBalance += VacuumCostPageDirty;
index d8debd160e4513262f61eb36f03ea8fe75568db0..03a54451ac212c15e9748e62ce2260730911b6be 100644 (file)
@@ -153,10 +153,6 @@ int            VacuumCostPageDirty = 20;
 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;
 
index ac16233b71f59d1cfa005d7559f8ff71b43dd590..25348e71eb9c8e7029df7ad90d205fecaf842084 100644 (file)
@@ -284,10 +284,6 @@ extern PGDLLIMPORT int VacuumCostPageDirty;
 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;