Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jUB4b-0002hA-4Z for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Apr 2020 15:26:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jUB4Z-0001Hr-LU for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Apr 2020 15:25:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jUB4Z-0001Hk-Eh for pgsql-hackers@lists.postgresql.org; Thu, 30 Apr 2020 15:25:59 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jUB4R-0000ZU-8r for pgsql-hackers@postgresql.org; Thu, 30 Apr 2020 15:25:57 +0000 Received: from hnk.local (p2421237-ipbf2725funabasi.chiba.ocn.ne.jp [122.24.33.237]) by oss.nttdata.com (Postfix) with ESMTPSA id B6D3B600E3; Fri, 1 May 2020 00:25:46 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.2 at oss.nttdata.com Subject: Re: Why are wait events not reported even though it reads/writes a timeline history file? To: Masahiro Ikeda , Michael Paquier Cc: pgsql-hackers@postgresql.org References: <3480dda4f5a043d8819de9e8653dc331@oss.nttdata.com> <20200428060958.GG279958@paquier.xyz> <24cc681e7048f984e490f5d01f7ead19@oss.nttdata.com> From: Fujii Masao Message-ID: <688e355d-6a70-b127-87ed-0b1fa8403dda@oss.nttdata.com> Date: Fri, 1 May 2020 00:25:46 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <24cc681e7048f984e490f5d01f7ead19@oss.nttdata.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020/04/28 17:42, Masahiro Ikeda wrote: > On 2020-04-28 15:09, Michael Paquier wrote: >> On Tue, Apr 28, 2020 at 02:49:00PM +0900, Fujii Masao wrote: >>> Isn't it safer to report the wait event during fgets() rather than putting >>> those calls around the whole loop, like other code does? For example, >>> writeTimeLineHistory() reports the wait event during read() rather than >>> whole loop. >> >> Yeah, I/O wait events should be taken only during the duration of the >> system calls.  Particularly here, you may finish with an elog() that >> causes the wait event to be set longer than it should, leading to a >> rather incorrect state if a snapshot of pg_stat_activity is taken. >> -- > > Thanks for your comments. > > I fixed it to report the wait event during fgets() only. > Please review the v2 patch I attached. Thanks for updating the patch! Here are the review comments from me. + char *result; + pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_READ); + result = fgets(fline, sizeof(fline), fd); + pgstat_report_wait_end(); + if (result == NULL) + break; + /* skip leading whitespace and check for # comment */ char *ptr; Since the variable name "result" has been already used in this function, it should be renamed. The code should not be inject into the variable declaration block. When reading this patch, I found that IO-error in fgets() has not been checked there. Though this is not the issue that you reported, but it seems better to fix it together. So what about adding the following code? if (ferror(fd)) ereport(ERROR, (errcode_for_file_access(), errmsg("could not read file \"%s\": %m", path))); Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION