pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Fujii Masao <masao.fujii@oss.nttdata.com>
To: ikedamsh <ikedamsh@oss.nttdata.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Cc: Masahiko Sawada <sawada.mshk@gmail.com>
Cc: Li Japin <japinli@hotmail.com>
Cc: kuroda.hayato@fujitsu.com
Subject: Re: About to add WAL write/fsync statistics to pg_stat_wal view
Date: Mon, 22 Mar 2021 16:50:52 +0900
Message-ID: <940c12fd-96dc-67d2-afe9-dbb679eda010@oss.nttdata.com> (raw)
In-Reply-To: <82ccde1a-ead9-598c-f472-004a01ff23e9@oss.nttdata.com>
References: <0509ad67b585a5b86a83d445dfa75392@oss.nttdata.com>
	<a7c255be-ca6e-b31f-b836-74eccf9510a8@oss.nttdata.com>
	<930cae989a0fe8db6870cbeb5f24cf97@oss.nttdata.com>
	<9a70efda-6f47-8b1c-0513-419e1185000a@oss.nttdata.com>
	<6c8932c2b0756971e4b28f06af07be9a@oss.nttdata.com>
	<4a81fc95615db54b75f59759752b2cef@oss.nttdata.com>
	<49d412cc-2e39-a487-1861-a8560ebb88cb@oss.nttdata.com>
	<da0059e5e7282573c52ecffd3554a810@oss.nttdata.com>
	<b10edd92-b428-25e9-89d4-e36592a67363@oss.nttdata.com>
	<99fbf195e4b02a629cbf8190a0b9d47a@oss.nttdata.com>
	<7c08820c-06d6-0968-100a-5df71d67768b@oss.nttdata.com>
	<f2ea17310b046cc6688b6f868a780cc8@oss.nttdata.com>
	<550de3c1-3408-de76-6fc1-a46eedc217a3@oss.nttdata.com>
	<b53fc36fbc55fe6c08d4fa0df0d29a5b@oss.nttdata.com>
	<a965d3ed-47d3-7d30-c15f-fc0f98810e4d@oss.nttdata.com>
	<90d8baf25655a08299185cb1a616bda1@oss.nttdata.com>
	<ce0f7bf6-9c7d-0a80-c580-3952c49d87e1@oss.nttdata.com>
	<82ccde1a-ead9-598c-f472-004a01ff23e9@oss.nttdata.com>



On 2021/03/22 9:50, ikedamsh wrote:
> Agreed. I separated the patches.
> 
> If only the former is committed, my trivial concern is that there may be
> a disadvantage, but no advantage for the standby server. It may lead to
> performance degradation to the wal receiver by calling
> INSTR_TIME_SET_CURRENT(), but the stats can't visible for users until the
> latter patch is committed.

Your concern is valid, so let's polish and commit also the 0003 patch to v14.
I'm still thinking that it's better to separate wal_xxx columns into
walreceiver's and the others. But if we count even walreceiver activity on
the existing columns, regarding 0003 patch, we need to update the document?
For example, "Number of times WAL buffers were written out to disk via
XLogWrite request." should be "Number of times WAL buffers were written
out to disk via XLogWrite request and by WAL receiver process."? Maybe
we need to append some descriptions about this into "WAL configuration"
section?


> I followed the argument of pg_pwrite().
> But, I think "char *" is better, so fixed it.

Thanks for updating the patch!

+extern int	XLogWriteFile(int fd, char *buf,
+						  size_t nbyte, off_t offset,
+						  TimeLineID timelineid, XLogSegNo segno,
+						  bool write_all);

write_all seems not to be necessary. You added this flag for walreceiver,
I guess. But even without the argument, walreceiver seems to work expectedly.
So, what about the attached patch? I applied some cosmetic changes to the patch.

Regards,

-- 
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION

Attachments:

v20-0006-merge-wal-write-function.patch (text/plain, 6.3K) download diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 6f8810e149..9d8ea7edca 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2534,63 +2534,15 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible) { char *from; Size nbytes; - Size nleft; int written; - instr_time start; /* OK to write the page(s) */ from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ; nbytes = npages * (Size) XLOG_BLCKSZ; - nleft = nbytes; - do - { - errno = 0; - - /* Measure I/O timing to write WAL data */ - if (track_wal_io_timing) - INSTR_TIME_SET_CURRENT(start); - - pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); - written = pg_pwrite(openLogFile, from, nleft, startoffset); - pgstat_report_wait_end(); - - /* - * Increment the I/O timing and the number of times WAL data - * were written out to disk. - */ - if (track_wal_io_timing) - { - instr_time duration; - - INSTR_TIME_SET_CURRENT(duration); - INSTR_TIME_SUBTRACT(duration, start); - WalStats.m_wal_write_time += INSTR_TIME_GET_MICROSEC(duration); - } - - WalStats.m_wal_write++; - - if (written <= 0) - { - char xlogfname[MAXFNAMELEN]; - int save_errno; - - if (errno == EINTR) - continue; - - save_errno = errno; - XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo, - wal_segment_size); - errno = save_errno; - ereport(PANIC, - (errcode_for_file_access(), - errmsg("could not write to log file %s " - "at offset %u, length %zu: %m", - xlogfname, startoffset, nleft))); - } - nleft -= written; - from += written; - startoffset += written; - } while (nleft > 0); + written = XLogWriteFile(openLogFile, from, nbytes, (off_t) startoffset, + ThisTimeLineID, openLogSegNo); + Assert(written == nbytes); + startoffset += written; npages = 0; @@ -2707,6 +2659,94 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible) } } +/* + * Write the WAL data to the WAL file. + * + * 'fd' is the file descriptor for the WAL file to write WAL to. 'buf' is + * the starting address of the buffer storing WAL data to write. + * 'nbytes' is the number of bytes to write WAL data up to. 'offset' + * is the offset of WAL file to be set. 'tli' and 'segno' are the + * timeline ID and segment number of WAL file. + * + * Return the total number of bytes written. This must be the same as + * 'nbytes'. PANIC error is thrown if WAL data fails to be written. + */ +int +XLogWriteFile(int fd, char *buf, Size nbytes, off_t offset, + TimeLineID tli, XLogSegNo segno) +{ + Size total_written = 0; + + /* + * Loop until 'nbytes' bytes of the buffer data have been written or an + * error occurs. + */ + do + { + int written; + instr_time start; + + errno = 0; + + /* Measure I/O timing to write WAL data */ + if (track_wal_io_timing) + INSTR_TIME_SET_CURRENT(start); + + pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE); + written = pg_pwrite(fd, buf, nbytes, offset); + pgstat_report_wait_end(); + + if (written <= 0) + { + char xlogfname[MAXFNAMELEN]; + int save_errno; + + /* + * Retry on EINTR. All signals used in the backend and background + * processes are flagged SA_RESTART, so it shouldn't happen, but + * better to be defensive. + */ + if (errno == EINTR) + continue; + + /* if write didn't set errno, assume no disk space */ + if (errno == 0) + errno = ENOSPC; + + save_errno = errno; + XLogFileName(xlogfname, tli, segno, wal_segment_size); + errno = save_errno; + ereport(PANIC, + (errcode_for_file_access(), + errmsg("could not write to log file %s " + "at offset %u, length %zu: %m", + xlogfname, (unsigned int) offset, nbytes))); + } + + /* + * Increment the I/O timing and the number of times WAL data were + * written out to disk. + */ + if (track_wal_io_timing) + { + instr_time duration; + + INSTR_TIME_SET_CURRENT(duration); + INSTR_TIME_SUBTRACT(duration, start); + WalStats.m_wal_write_time += INSTR_TIME_GET_MICROSEC(duration); + } + + WalStats.m_wal_write++; + + nbytes -= written; + buf += written; + offset += written; + total_written += written; + } while (nbytes > 0); + + return total_written; +} + /* * Record the LSN for an asynchronous transaction commit/abort * and nudge the WALWriter if there is work for it to do. diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index f719ab4f6d..e33565d859 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -871,7 +871,7 @@ XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len) static void XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr) { - int startoff; + uint32 startoff; int byteswritten; while (nbytes > 0) @@ -938,27 +938,9 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr) segbytes = nbytes; /* OK to write the logs */ - errno = 0; - - byteswritten = pg_pwrite(recvFile, buf, segbytes, (off_t) startoff); - if (byteswritten <= 0) - { - char xlogfname[MAXFNAMELEN]; - int save_errno; - - /* if write didn't set errno, assume no disk space */ - if (errno == 0) - errno = ENOSPC; - - save_errno = errno; - XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size); - errno = save_errno; - ereport(PANIC, - (errcode_for_file_access(), - errmsg("could not write to log segment %s " - "at offset %u, length %lu: %m", - xlogfname, startoff, (unsigned long) segbytes))); - } + byteswritten = XLogWriteFile(recvFile, buf, segbytes, (off_t) startoff, + recvFileTLI, recvSegNo); + Assert(byteswritten == segbytes); /* Update state for write */ recptr += byteswritten; diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h index 77187c12be..c1f3ddab89 100644 --- a/src/include/access/xlog.h +++ b/src/include/access/xlog.h @@ -298,6 +298,8 @@ extern bool XLogBackgroundFlush(void); extern bool XLogNeedsFlush(XLogRecPtr RecPtr); extern int XLogFileInit(XLogSegNo segno, bool *use_existent, bool use_lock); extern int XLogFileOpen(XLogSegNo segno); +extern int XLogWriteFile(int fd, char *buf, Size nbyte, off_t offset, + TimeLineID tli, XLogSegNo segno); extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli); extern XLogSegNo XLogGetLastRemovedSegno(void);

view thread (67+ messages)  latest in thread

Message-ID: <940c12fd-96dc-67d2-afe9-dbb679eda010@oss.nttdata.com>
Permalink:  ../940c12fd-96dc-67d2-afe9-dbb679eda010@oss.nttdata.com/
Also on:    postgresql.org/message-id/940c12fd-96dc-67d2-afe9-dbb679eda010@oss.nttdata.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: masao.fujii@oss.nttdata.com, ikedamsh@oss.nttdata.com, david.g.johnston@gmail.com, sawada.mshk@gmail.com, japinli@hotmail.com, kuroda.hayato@fujitsu.com
  Subject: Re: About to add WAL write/fsync statistics to pg_stat_wal view
  In-Reply-To: <940c12fd-96dc-67d2-afe9-dbb679eda010@oss.nttdata.com>
  References: <0509ad67b585a5b86a83d445dfa75392@oss.nttdata.com> <930cae989a0fe8db6870cbeb5f24cf97@oss.nttdata.com> <9a70efda-6f47-8b1c-0513-419e1185000a@oss.nttdata.com> <6c8932c2b0756971e4b28f06af07be9a@oss.nttdata.com> <4a81fc95615db54b75f59759752b2cef@oss.nttdata.com> <49d412cc-2e39-a487-1861-a8560ebb88cb@oss.nttdata.com> <da0059e5e7282573c52ecffd3554a810@oss.nttdata.com> <b10edd92-b428-25e9-89d4-e36592a67363@oss.nttdata.com> <99fbf195e4b02a629cbf8190a0b9d47a@oss.nttdata.com> <7c08820c-06d6-0968-100a-5df71d67768b@oss.nttdata.com> <f2ea17310b046cc6688b6f868a780cc8@oss.nttdata.com> <550de3c1-3408-de76-6fc1-a46eedc217a3@oss.nttdata.com> <b53fc36fbc55fe6c08d4fa0df0d29a5b@oss.nttdata.com> <a965d3ed-47d3-7d30-c15f-fc0f98810e4d@oss.nttdata.com> <90d8baf25655a08299185cb1a616bda1@oss.nttdata.com> <ce0f7bf6-9c7d-0a80-c580-3952c49d87e1@oss.nttdata.com> <82ccde1a-ead9-598c-f472-004a01ff23e9@oss.nttdata.com> <940c12fd-96dc-67d2-afe9-dbb679eda010@oss.nttdata.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox