LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_wal.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 50 51 98.0 %
Date: 2022-08-17 05:10:35 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -------------------------------------------------------------------------
       2             :  *
       3             :  * pgstat_wal.c
       4             :  *    Implementation of WAL statistics.
       5             :  *
       6             :  * This file contains the implementation of WAL statistics. It is kept
       7             :  * separate from pgstat.c to enforce the line between the statistics access /
       8             :  * storage implementation and the details about individual types of
       9             :  * statistics.
      10             :  *
      11             :  * Copyright (c) 2001-2022, PostgreSQL Global Development Group
      12             :  *
      13             :  * IDENTIFICATION
      14             :  *    src/backend/utils/activity/pgstat_wal.c
      15             :  * -------------------------------------------------------------------------
      16             :  */
      17             : 
      18             : #include "postgres.h"
      19             : 
      20             : #include "utils/pgstat_internal.h"
      21             : #include "executor/instrument.h"
      22             : 
      23             : 
      24             : PgStat_WalStats PendingWalStats = {0};
      25             : 
      26             : /*
      27             :  * WAL usage counters saved from pgWALUsage at the previous call to
      28             :  * pgstat_report_wal(). This is used to calculate how much WAL usage
      29             :  * happens between pgstat_report_wal() calls, by subtracting
      30             :  * the previous counters from the current ones.
      31             :  */
      32             : static WalUsage prevWalUsage;
      33             : 
      34             : 
      35             : /*
      36             :  * Calculate how much WAL usage counters have increased and update
      37             :  * shared statistics.
      38             :  *
      39             :  * Must be called by processes that generate WAL, that do not call
      40             :  * pgstat_report_stat(), like walwriter.
      41             :  */
      42             : void
      43       30626 : pgstat_report_wal(bool force)
      44             : {
      45       30626 :     pgstat_flush_wal(force);
      46       30626 : }
      47             : 
      48             : /*
      49             :  * Support function for the SQL-callable pgstat* functions. Returns
      50             :  * a pointer to the WAL statistics struct.
      51             :  */
      52             : PgStat_WalStats *
      53          84 : pgstat_fetch_stat_wal(void)
      54             : {
      55          84 :     pgstat_snapshot_fixed(PGSTAT_KIND_WAL);
      56             : 
      57          84 :     return &pgStatLocal.snapshot.wal;
      58             : }
      59             : 
      60             : /*
      61             :  * Calculate how much WAL usage counters have increased by subtracting the
      62             :  * previous counters from the current ones.
      63             :  *
      64             :  * If nowait is true, this function returns true if the lock could not be
      65             :  * acquired. Otherwise return false.
      66             :  */
      67             : bool
      68       71662 : pgstat_flush_wal(bool nowait)
      69             : {
      70       71662 :     PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
      71       71662 :     WalUsage    diff = {0};
      72             : 
      73             :     Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
      74             :     Assert(pgStatLocal.shmem != NULL &&
      75             :            !pgStatLocal.shmem->is_shutdown);
      76             : 
      77             :     /*
      78             :      * This function can be called even if nothing at all has happened. Avoid
      79             :      * taking lock for nothing in that case.
      80             :      */
      81       71662 :     if (!pgstat_have_pending_wal())
      82       50614 :         return false;
      83             : 
      84             :     /*
      85             :      * We don't update the WAL usage portion of the local WalStats elsewhere.
      86             :      * Calculate how much WAL usage counters were increased by subtracting the
      87             :      * previous counters from the current ones.
      88             :      */
      89       21048 :     WalUsageAccumDiff(&diff, &pgWalUsage, &prevWalUsage);
      90       21048 :     PendingWalStats.wal_records = diff.wal_records;
      91       21048 :     PendingWalStats.wal_fpi = diff.wal_fpi;
      92       21048 :     PendingWalStats.wal_bytes = diff.wal_bytes;
      93             : 
      94       21048 :     if (!nowait)
      95       18610 :         LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
      96        2438 :     else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
      97           0 :         return true;
      98             : 
      99             : #define WALSTAT_ACC(fld) stats_shmem->stats.fld += PendingWalStats.fld
     100       21048 :     WALSTAT_ACC(wal_records);
     101       21048 :     WALSTAT_ACC(wal_fpi);
     102       21048 :     WALSTAT_ACC(wal_bytes);
     103       21048 :     WALSTAT_ACC(wal_buffers_full);
     104       21048 :     WALSTAT_ACC(wal_write);
     105       21048 :     WALSTAT_ACC(wal_sync);
     106       21048 :     WALSTAT_ACC(wal_write_time);
     107       21048 :     WALSTAT_ACC(wal_sync_time);
     108             : #undef WALSTAT_ACC
     109             : 
     110       21048 :     LWLockRelease(&stats_shmem->lock);
     111             : 
     112             :     /*
     113             :      * Save the current counters for the subsequent calculation of WAL usage.
     114             :      */
     115       21048 :     prevWalUsage = pgWalUsage;
     116             : 
     117             :     /*
     118             :      * Clear out the statistics buffer, so it can be re-used.
     119             :      */
     120      210480 :     MemSet(&PendingWalStats, 0, sizeof(PendingWalStats));
     121             : 
     122       21048 :     return false;
     123             : }
     124             : 
     125             : void
     126       24072 : pgstat_init_wal(void)
     127             : {
     128             :     /*
     129             :      * Initialize prevWalUsage with pgWalUsage so that pgstat_flush_wal() can
     130             :      * calculate how much pgWalUsage counters are increased by subtracting
     131             :      * prevWalUsage from pgWalUsage.
     132             :      */
     133       24072 :     prevWalUsage = pgWalUsage;
     134       24072 : }
     135             : 
     136             : /*
     137             :  * To determine whether any WAL activity has occurred since last time, not
     138             :  * only the number of generated WAL records but also the numbers of WAL
     139             :  * writes and syncs need to be checked. Because even transaction that
     140             :  * generates no WAL records can write or sync WAL data when flushing the
     141             :  * data pages.
     142             :  */
     143             : bool
     144       82822 : pgstat_have_pending_wal(void)
     145             : {
     146      152222 :     return pgWalUsage.wal_records != prevWalUsage.wal_records ||
     147      144530 :         PendingWalStats.wal_write != 0 ||
     148       61708 :         PendingWalStats.wal_sync != 0;
     149             : }
     150             : 
     151             : void
     152         818 : pgstat_wal_reset_all_cb(TimestampTz ts)
     153             : {
     154         818 :     PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
     155             : 
     156         818 :     LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
     157         818 :     memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats));
     158         818 :     stats_shmem->stats.stat_reset_timestamp = ts;
     159         818 :     LWLockRelease(&stats_shmem->lock);
     160         818 : }
     161             : 
     162             : void
     163        1894 : pgstat_wal_snapshot_cb(void)
     164             : {
     165        1894 :     PgStatShared_Wal *stats_shmem = &pgStatLocal.shmem->wal;
     166             : 
     167        1894 :     LWLockAcquire(&stats_shmem->lock, LW_SHARED);
     168        1894 :     memcpy(&pgStatLocal.snapshot.wal, &stats_shmem->stats,
     169             :            sizeof(pgStatLocal.snapshot.wal));
     170        1894 :     LWLockRelease(&stats_shmem->lock);
     171        1894 : }

Generated by: LCOV version 1.14