Thread: pgstat: stats added in ReadPageInternal() aren't getting reported via pg_stat_wal

pgstat: stats added in ReadPageInternal() aren't getting reported via pg_stat_wal

From
Bharath Rupireddy
Date:
Hi,

I added xlogreader cache stats (hits/misses) to pg_stat_wal in
ReadPageInternal() for some of my work and ran some tests with logical
replication subscribers. I had expected that the new stats generated
by walsenders serving the subscribers would be accumulated and shown
via pg_stat_wal view in another session, but that didn't happen. Upon
looking around, I thought adding
pgstat_flush_wal()/pgstat_report_wal() around proc_exit() in
walsener.c might work, but that didn't help either. Can anyone please
let me know why the stats that I added aren't shown via pg_stat_wal
view despite the walsender doing pgstat_initialize() ->
pgstat_init_wal()? Am I missing something?

I'm attaching here with the patch that I was using.

--
Bharath Rupireddy
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachment

Re: pgstat: stats added in ReadPageInternal() aren't getting reported via pg_stat_wal

From
Kyotaro Horiguchi
Date:
At Mon, 19 Sep 2022 18:46:17 +0530, Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> wrote in 
> walsener.c might work, but that didn't help either. Can anyone please
> let me know why the stats that I added aren't shown via pg_stat_wal
> view despite the walsender doing pgstat_initialize() ->
> pgstat_init_wal()? Am I missing something?
> 
> I'm attaching here with the patch that I was using.

pgstat_report_wal() is supposed to be called on user-facing (or query
executing) backends. The function is called while in idle state on
such backends. Other processes need to call one or more friend
functions as needed. Checkpointer calls pgstat_report_checkpoint/wal()
at the end of an iteration of the main waiting loop. Walwriter does
the same with pgstat_report_wal(). Thus we need to do the same on
startup process somewhere within the redo loop but where that call
doesn't cause contention on shared stats area.

About what the patch does, it increments the xlogreader_cache_hits
counter when the WAL page to read have been already loaded, but it's
not a cache but a buffer. The counter would approximately shows the
number about mean-(raw)-records-per-wal-page *
wal-page-ever-read. (mean-records-per-wal-page = page-size /
mean-record-length).  So I don't think that the value offers something
valuable. I guess that the same value (for the moment) can be
calculated from the result of pg_get_wal_records_info().

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center