Move wal_buffers_full from PgStat_PendingWalStats to WalUsage
authorMichael Paquier <[email protected]>
Mon, 17 Feb 2025 04:14:28 +0000 (13:14 +0900)
committerMichael Paquier <[email protected]>
Mon, 17 Feb 2025 04:14:28 +0000 (13:14 +0900)
wal_buffers_full has been introduced in pg_stat_wal in 8d9a935965f, as
some information providing metrics for the tuning of the GUC
wal_buffers.  WalUsage has been introduced before that in df3b181499.

Moving this field is proving to be beneficial for several reasons:
- This information can now be made available in more layers, providing
more granularity than just pg_stat_wal, on a per-query basis: EXPLAIN,
pgss and VACUUM/ANALYZE logs.
- A patch is under discussion to provide statistics for WAL at backend
level, and this move simplifies a bit the handling of pending
statistics.  The remaining data in PgStat_PendingWalStats now relates to
write/sync counters and times, with equivalents present in pg_stat_io,
that backend statistics are able to already track.  So this should cut
all the dependencies between PgStat_PendingWalStats and WAL stats at
backend level.

As of this change, wal_buffers_full only shows in pg_stat_wal.

Author: Bertrand Drouvot
Reviewed-by: Ilia Evdokimov
Discussion: https://postgr.es/m/[email protected]

src/backend/access/transam/xlog.c
src/backend/executor/instrument.c
src/backend/utils/activity/pgstat_wal.c
src/include/executor/instrument.h
src/include/pgstat.h

index 87f9fb7a654fc99d8eaf31ccce9979fb283b38ce..75d5554c77c2a3f7f02f74ecaa2423ba240a39a3 100644 (file)
@@ -2093,7 +2093,7 @@ AdvanceXLInsertBuffer(XLogRecPtr upto, TimeLineID tli, bool opportunistic)
                    WriteRqst.Flush = 0;
                    XLogWrite(WriteRqst, tli, false);
                    LWLockRelease(WALWriteLock);
-                   PendingWalStats.wal_buffers_full++;
+                   pgWalUsage.wal_buffers_full++;
                    TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_DONE();
                }
            }
index 2d3569b374828bd2232a0c8e90b3050ee952f760..56e635f47000dd8e74e3f32b55235b8f2a036100 100644 (file)
@@ -280,6 +280,7 @@ WalUsageAdd(WalUsage *dst, WalUsage *add)
    dst->wal_bytes += add->wal_bytes;
    dst->wal_records += add->wal_records;
    dst->wal_fpi += add->wal_fpi;
+   dst->wal_buffers_full += add->wal_buffers_full;
 }
 
 void
@@ -288,4 +289,5 @@ WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
    dst->wal_bytes += add->wal_bytes - sub->wal_bytes;
    dst->wal_records += add->wal_records - sub->wal_records;
    dst->wal_fpi += add->wal_fpi - sub->wal_fpi;
+   dst->wal_buffers_full += add->wal_buffers_full - sub->wal_buffers_full;
 }
index 18fa6b2936a63cdb7272834f31c608e5e8680675..c1ca65eb9918c55f7b525dc57674aa07d1f9911d 100644 (file)
@@ -123,7 +123,7 @@ pgstat_wal_flush_cb(bool nowait)
    WALSTAT_ACC(wal_records, wal_usage_diff);
    WALSTAT_ACC(wal_fpi, wal_usage_diff);
    WALSTAT_ACC(wal_bytes, wal_usage_diff);
-   WALSTAT_ACC(wal_buffers_full, PendingWalStats);
+   WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
    WALSTAT_ACC(wal_write, PendingWalStats);
    WALSTAT_ACC(wal_sync, PendingWalStats);
    WALSTAT_ACC_INSTR_TIME(wal_write_time);
index 5a6eff75c69601575a9385c9b4511169bfa8997e..03653ab6c6cde6f7e17434e212327d9271f71659 100644 (file)
@@ -53,6 +53,7 @@ typedef struct WalUsage
    int64       wal_records;    /* # of WAL records produced */
    int64       wal_fpi;        /* # of WAL full page images produced */
    uint64      wal_bytes;      /* size of WAL records produced */
+   int64       wal_buffers_full;   /* # of times the WAL buffers became full */
 } WalUsage;
 
 /* Flag bits included in InstrAlloc's instrument_options bitmask */
index dd823d3f56e37768c4079418b84ba5e633b8f09a..53f2a8458e6fcd27c5cd21b3ca3ac07c38499072 100644 (file)
@@ -495,7 +495,6 @@ typedef struct PgStat_WalStats
  */
 typedef struct PgStat_PendingWalStats
 {
-   PgStat_Counter wal_buffers_full;
    PgStat_Counter wal_write;
    PgStat_Counter wal_sync;
    instr_time  wal_write_time;