Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1unzyH-00EKCk-09 for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Aug 2025 13:32:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1unzyG-00Gd5g-Bg for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Aug 2025 13:32:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1unzyG-00Gd4w-2C for pgsql-hackers@lists.postgresql.org; Mon, 18 Aug 2025 13:32:20 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1unzyD-000WP1-2I for pgsql-hackers@postgresql.org; Mon, 18 Aug 2025 13:32:20 +0000 Received: from oss.nttdata.com (localhost [127.0.0.1]) by oss.nttdata.com (Postfix) with ESMTPA id 718BB61A88; Mon, 18 Aug 2025 22:32:10 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at oss.nttdata.com MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Date: Mon, 18 Aug 2025 22:32:09 +0900 From: torikoshia To: Rahila Syed Cc: PostgreSQL Hackers , Daniel Gustafsson Subject: Re: Enhancing Memory Context Statistics Reporting In-Reply-To: References: <54FAE93D-E156-4684-9352-1CEDE6BD4A1C@yesql.se> <5bxhxniyvjyfldi7yjxcnxkl3i2ghci2grjyeclrbkfqnyhowk@dfkqzbvtbpml> <3eb40b3e-45c7-426a-b7f8-81f7d05a9b53@oss.nttdata.com> <74105887-E4EF-4CA2-9F0A-3C0CE05BF632@yesql.se> <437100f2-dd86-42e6-830c-5ff413a25e8e@oss.nttdata.com> <2a796830-de2d-4030-b480-d673f6cc5d94@eisentraut.org> <02E42650-0C66-4B49-93CA-1B0A5B73E7FD@yesql.se> User-Agent: Roundcube Webmail/1.4.11 Message-ID: X-Sender: torikoshia@oss.nttdata.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2025-08-14 07:35, Rahila Syed wrote: > Hi Torikoshia, > > Thank you for reviewing the patch. > >> This function is added at the end of Table "9.96. Server Signaling >> Functions", but since pg_get_process_memory_contexts outputs >> essentially >> the same information as pg_log_backend_memory_contexts, it might be >> better to place them next to each other in the table. > > The idea was to place the new addition at the end of the table instead > of in the middle. > I’m fine with putting them together, though. I’ll do that in the > next version unless there’s a > reason not to. > >>> + stats_timestamp >> timestamptz ) >> >>> +typedef struct MemoryStatsDSHashEntry >>> +{ >>> + char key[64]; >>> + ConditionVariable memcxt_cv; >>> + int proc_id; >>> + int total_stats; >>> + bool summary; >>> + dsa_pointer memstats_dsa_pointer; >>> + TimestampTz stats_timestamp; >>> +} MemoryStatsDSHashEntry; >> >> stats_timestamp appears only in the two places below in the patch, >> but >> it does not seem to be actually output. >> Is this column unnecessary? > > Thank you for pointing this out. This is removed in the attached > patch, as it was a > remnant from the previous design. As old statistics are discarded in > the new design, > a timestamp field is not needed anymore. > >> Specifying 0 for timeout causes a crash: >> Should 0 be handled safely and treated as “no timeout”, or >> rejected as >> an error? > > Good catch. > The crash has been resolved in the attached patch. It was caused by a > missing > ConditionVariableCancelSleep() call when exiting without statistics > due to a timeout value of 0. > A 0 timeout means that statistics should only be retrieved if they are > immediately available, > without waiting. We could exit with a warning/error saying "too low > timeout", but I think it's worthwhile > to try fetching the statistics if possible. > >> Similarly, specifying a negative value for timeout still works: >> >> =# select * from pg_get_process_memory_contexts(30590, true, >> -10); >> >> It might be better to reject negative values similar to >> pg_terminate_backend(). > > Fixed as suggested by you in the attached patch. > Currently, negative values are interpreted as an indefinite wait for > statistics. > This could cause the client to hang if the server process exits > without providing statistics. > To avoid this, it would be better to exit after displaying a warning > when the user specifies > negative timeouts. > >>> + /* Retreive the client key fo publishing statistics */ >> >> fo -> for? > > Fixed. > >>> + */ >>> +#define MEMSTATS_WAIT_TIMEOUT 100 >> >> MEMSTATS_WAIT_TIMEOUT is defined, but it doesn’t seem to be used. > > This is removed now as it was a leftover from the previous design. > > The attached patch also fixes an assertion failure I observed when a > client times out > before the last requested process can publish its statistics. A client > frees the memory > reserved for storing the statistics when it exits the function after > timeout. Since a > server process was notified, it might attempt to read the same client > entry and access the dsa > memory reserved for statistics resulting in the assertion > failure. I resolved this by including a check for this scenario and > then exiting the handler > function accordingly. Thanks for updating the patch! However, when I ran pg_get_process_memory_contexts() with summary = true, it took a while and returned nothing: =# select pg_get_process_memory_contexts(pg_backend_pid(), true, 1) from pg_stat_activity ; pg_get_process_memory_contexts -------------------------------- (0 rows) Time: 6026.291 ms (00:06.026) Since v32 patch quickly returned the memory contexts as expected with the same parameter specified, there seems to be some degradation. Could you check it? -- Regards, -- Atsushi Torikoshi Seconded from NTT DATA Japan Corporation to SRA OSS K.K.