Commit 01469241 authored by Fujii Masao's avatar Fujii Masao

Track total number of WAL records, FPIs and bytes generated in the cluster.

Commit 6b466bf5 allowed pg_stat_statements to track the number of
WAL records, full page images and bytes that each statement generated.
Similarly this commit allows us to track the cluster-wide WAL statistics
counters.

New columns wal_records, wal_fpi and wal_bytes are added into the
pg_stat_wal view, and reports the total number of WAL records,
full page images and bytes generated in the , respectively.

Author: Masahiro Ikeda
Reviewed-by: Amit Kapila, Movead Li, Kyotaro Horiguchi, Fujii Masao
Discussion: https://postgr.es/m/35ef960128b90bfae3b3fdf60a3a860f@oss.nttdata.com
parent 91624c2f
...@@ -3447,6 +3447,33 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i ...@@ -3447,6 +3447,33 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i
</thead> </thead>
<tbody> <tbody>
<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>wal_records</structfield> <type>bigint</type>
</para>
<para>
Total number of WAL records generated
</para></entry>
</row>
<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>wal_fpi</structfield> <type>bigint</type>
</para>
<para>
Total number of WAL full page images generated
</para></entry>
</row>
<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>wal_bytes</structfield> <type>numeric</type>
</para>
<para>
Total amount of WAL bytes generated
</para></entry>
</row>
<row> <row>
<entry role="catalog_table_entry"><para role="column_definition"> <entry role="catalog_table_entry"><para role="column_definition">
<structfield>wal_buffers_full</structfield> <type>bigint</type> <structfield>wal_buffers_full</structfield> <type>bigint</type>
......
...@@ -993,6 +993,9 @@ CREATE VIEW pg_stat_bgwriter AS ...@@ -993,6 +993,9 @@ CREATE VIEW pg_stat_bgwriter AS
CREATE VIEW pg_stat_wal AS CREATE VIEW pg_stat_wal AS
SELECT SELECT
w.wal_records,
w.wal_fpi,
w.wal_bytes,
w.wal_buffers_full, w.wal_buffers_full,
w.stats_reset w.stats_reset
FROM pg_stat_get_wal() w; FROM pg_stat_get_wal() w;
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include "catalog/pg_database.h" #include "catalog/pg_database.h"
#include "catalog/pg_proc.h" #include "catalog/pg_proc.h"
#include "common/ip.h" #include "common/ip.h"
#include "executor/instrument.h"
#include "libpq/libpq.h" #include "libpq/libpq.h"
#include "libpq/pqsignal.h" #include "libpq/pqsignal.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
...@@ -143,6 +144,14 @@ char *pgstat_stat_tmpname = NULL; ...@@ -143,6 +144,14 @@ char *pgstat_stat_tmpname = NULL;
PgStat_MsgBgWriter BgWriterStats; PgStat_MsgBgWriter BgWriterStats;
PgStat_MsgWal WalStats; PgStat_MsgWal WalStats;
/*
* WAL usage counters saved from pgWALUsage at the previous call to
* pgstat_send_wal(). This is used to calculate how much WAL usage
* happens between pgstat_send_wal() calls, by substracting
* the previous counters from the current ones.
*/
static WalUsage prevWalUsage;
/* /*
* List of SLRU names that we keep stats for. There is no central registry of * List of SLRU names that we keep stats for. There is no central registry of
* SLRUs, so we use this fixed list instead. The "other" entry is used for * SLRUs, so we use this fixed list instead. The "other" entry is used for
...@@ -3048,6 +3057,13 @@ pgstat_initialize(void) ...@@ -3048,6 +3057,13 @@ pgstat_initialize(void)
MyBEEntry = &BackendStatusArray[MaxBackends + MyAuxProcType]; MyBEEntry = &BackendStatusArray[MaxBackends + MyAuxProcType];
} }
/*
* Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
* calculate how much pgWalUsage counters are increased by substracting
* prevWalUsage from pgWalUsage.
*/
prevWalUsage = pgWalUsage;
/* Set up a process-exit hook to clean up */ /* Set up a process-exit hook to clean up */
on_shmem_exit(pgstat_beshutdown_hook, 0); on_shmem_exit(pgstat_beshutdown_hook, 0);
} }
...@@ -4577,6 +4593,20 @@ pgstat_send_wal(void) ...@@ -4577,6 +4593,20 @@ pgstat_send_wal(void)
/* We assume this initializes to zeroes */ /* We assume this initializes to zeroes */
static const PgStat_MsgWal all_zeroes; static const PgStat_MsgWal all_zeroes;
WalUsage walusage;
/*
* Calculate how much WAL usage counters are increased by substracting the
* previous counters from the current ones. Fill the results in WAL stats
* message.
*/
MemSet(&walusage, 0, sizeof(WalUsage));
WalUsageAccumDiff(&walusage, &pgWalUsage, &prevWalUsage);
WalStats.m_wal_records = walusage.wal_records;
WalStats.m_wal_fpi = walusage.wal_fpi;
WalStats.m_wal_bytes = walusage.wal_bytes;
/* /*
* This function can be called even if nothing at all has happened. In * This function can be called even if nothing at all has happened. In
* this case, avoid sending a completely empty message to the stats * this case, avoid sending a completely empty message to the stats
...@@ -4591,6 +4621,11 @@ pgstat_send_wal(void) ...@@ -4591,6 +4621,11 @@ pgstat_send_wal(void)
pgstat_setheader(&WalStats.m_hdr, PGSTAT_MTYPE_WAL); pgstat_setheader(&WalStats.m_hdr, PGSTAT_MTYPE_WAL);
pgstat_send(&WalStats, sizeof(WalStats)); pgstat_send(&WalStats, sizeof(WalStats));
/*
* Save the current counters for the subsequent calculation of WAL usage.
*/
prevWalUsage = pgWalUsage;
/* /*
* Clear out the statistics buffer, so it can be re-used. * Clear out the statistics buffer, so it can be re-used.
*/ */
...@@ -6759,6 +6794,9 @@ pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len) ...@@ -6759,6 +6794,9 @@ pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len)
static void static void
pgstat_recv_wal(PgStat_MsgWal *msg, int len) pgstat_recv_wal(PgStat_MsgWal *msg, int len)
{ {
walStats.wal_records += msg->m_wal_records;
walStats.wal_fpi += msg->m_wal_fpi;
walStats.wal_bytes += msg->m_wal_bytes;
walStats.wal_buffers_full += msg->m_wal_buffers_full; walStats.wal_buffers_full += msg->m_wal_buffers_full;
} }
......
...@@ -1703,10 +1703,11 @@ pg_stat_get_buf_alloc(PG_FUNCTION_ARGS) ...@@ -1703,10 +1703,11 @@ pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
Datum Datum
pg_stat_get_wal(PG_FUNCTION_ARGS) pg_stat_get_wal(PG_FUNCTION_ARGS)
{ {
#define PG_STAT_GET_WAL_COLS 2 #define PG_STAT_GET_WAL_COLS 5
TupleDesc tupdesc; TupleDesc tupdesc;
Datum values[PG_STAT_GET_WAL_COLS]; Datum values[PG_STAT_GET_WAL_COLS];
bool nulls[PG_STAT_GET_WAL_COLS]; bool nulls[PG_STAT_GET_WAL_COLS];
char buf[256];
PgStat_WalStats *wal_stats; PgStat_WalStats *wal_stats;
/* Initialise values and NULL flags arrays */ /* Initialise values and NULL flags arrays */
...@@ -1715,9 +1716,15 @@ pg_stat_get_wal(PG_FUNCTION_ARGS) ...@@ -1715,9 +1716,15 @@ pg_stat_get_wal(PG_FUNCTION_ARGS)
/* Initialise attributes information in the tuple descriptor */ /* Initialise attributes information in the tuple descriptor */
tupdesc = CreateTemplateTupleDesc(PG_STAT_GET_WAL_COLS); tupdesc = CreateTemplateTupleDesc(PG_STAT_GET_WAL_COLS);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_buffers_full", TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
INT8OID, -1, 0); INT8OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "stats_reset", TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
INT8OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "wal_bytes",
NUMERICOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_buffers_full",
INT8OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stats_reset",
TIMESTAMPTZOID, -1, 0); TIMESTAMPTZOID, -1, 0);
BlessTupleDesc(tupdesc); BlessTupleDesc(tupdesc);
...@@ -1726,8 +1733,18 @@ pg_stat_get_wal(PG_FUNCTION_ARGS) ...@@ -1726,8 +1733,18 @@ pg_stat_get_wal(PG_FUNCTION_ARGS)
wal_stats = pgstat_fetch_stat_wal(); wal_stats = pgstat_fetch_stat_wal();
/* Fill values and NULLs */ /* Fill values and NULLs */
values[0] = Int64GetDatum(wal_stats->wal_buffers_full); values[0] = Int64GetDatum(wal_stats->wal_records);
values[1] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp); values[1] = Int64GetDatum(wal_stats->wal_fpi);
/* Convert to numeric. */
snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
values[2] = DirectFunctionCall3(numeric_in,
CStringGetDatum(buf),
ObjectIdGetDatum(0),
Int32GetDatum(-1));
values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
values[4] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp);
/* Returns the record as Datum */ /* Returns the record as Datum */
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
......
...@@ -5500,8 +5500,9 @@ ...@@ -5500,8 +5500,9 @@
{ oid => '1136', descr => 'statistics: information about WAL activity', { oid => '1136', descr => 'statistics: information about WAL activity',
proname => 'pg_stat_get_wal', proisstrict => 'f', provolatile => 's', proname => 'pg_stat_get_wal', proisstrict => 'f', provolatile => 's',
proparallel => 'r', prorettype => 'record', proargtypes => '', proparallel => 'r', prorettype => 'record', proargtypes => '',
proallargtypes => '{int8,timestamptz}', proargmodes => '{o,o}', proallargtypes => '{int8,int8,numeric,int8,timestamptz}',
proargnames => '{wal_buffers_full,stats_reset}', proargmodes => '{o,o,o,o,o}',
proargnames => '{wal_records,wal_fpi,wal_bytes,wal_buffers_full,stats_reset}',
prosrc => 'pg_stat_get_wal' }, prosrc => 'pg_stat_get_wal' },
{ oid => '2306', descr => 'statistics: information about SLRU caches', { oid => '2306', descr => 'statistics: information about SLRU caches',
......
...@@ -459,6 +459,9 @@ typedef struct PgStat_MsgBgWriter ...@@ -459,6 +459,9 @@ typedef struct PgStat_MsgBgWriter
typedef struct PgStat_MsgWal typedef struct PgStat_MsgWal
{ {
PgStat_MsgHdr m_hdr; PgStat_MsgHdr m_hdr;
PgStat_Counter m_wal_records;
PgStat_Counter m_wal_fpi;
uint64 m_wal_bytes;
PgStat_Counter m_wal_buffers_full; PgStat_Counter m_wal_buffers_full;
} PgStat_MsgWal; } PgStat_MsgWal;
...@@ -798,6 +801,9 @@ typedef struct PgStat_GlobalStats ...@@ -798,6 +801,9 @@ typedef struct PgStat_GlobalStats
*/ */
typedef struct PgStat_WalStats typedef struct PgStat_WalStats
{ {
PgStat_Counter wal_records;
PgStat_Counter wal_fpi;
uint64 wal_bytes;
PgStat_Counter wal_buffers_full; PgStat_Counter wal_buffers_full;
TimestampTz stat_reset_timestamp; TimestampTz stat_reset_timestamp;
} PgStat_WalStats; } PgStat_WalStats;
......
...@@ -2138,9 +2138,12 @@ pg_stat_user_tables| SELECT pg_stat_all_tables.relid, ...@@ -2138,9 +2138,12 @@ pg_stat_user_tables| SELECT pg_stat_all_tables.relid,
pg_stat_all_tables.autoanalyze_count pg_stat_all_tables.autoanalyze_count
FROM pg_stat_all_tables FROM pg_stat_all_tables
WHERE ((pg_stat_all_tables.schemaname <> ALL (ARRAY['pg_catalog'::name, 'information_schema'::name])) AND (pg_stat_all_tables.schemaname !~ '^pg_toast'::text)); WHERE ((pg_stat_all_tables.schemaname <> ALL (ARRAY['pg_catalog'::name, 'information_schema'::name])) AND (pg_stat_all_tables.schemaname !~ '^pg_toast'::text));
pg_stat_wal| SELECT w.wal_buffers_full, pg_stat_wal| SELECT w.wal_records,
w.wal_fpi,
w.wal_bytes,
w.wal_buffers_full,
w.stats_reset w.stats_reset
FROM pg_stat_get_wal() w(wal_buffers_full, stats_reset); FROM pg_stat_get_wal() w(wal_records, wal_fpi, wal_bytes, wal_buffers_full, stats_reset);
pg_stat_wal_receiver| SELECT s.pid, pg_stat_wal_receiver| SELECT s.pid,
s.status, s.status,
s.receive_start_lsn, s.receive_start_lsn,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment