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 1tlUWd-00CQgD-RI for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Feb 2025 15:01:11 +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 1tlUWb-00BAMv-Vj for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Feb 2025 15:01:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tlUWb-00BAKF-KF for pgsql-hackers@lists.postgresql.org; Fri, 21 Feb 2025 15:01:09 +0000 Received: from relay8-d.mail.gandi.net ([217.70.183.201]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tlUWW-00276U-0s for pgsql-hackers@postgresql.org; Fri, 21 Feb 2025 15:01:08 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 5D48B443FF; Fri, 21 Feb 2025 15:01:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vondra.me; s=gm1; t=1740150061; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BZMCl018OvJSrVE1jHKgGP6zQ2ZBq6FRRLO6b+Lw+sw=; b=nc/TXO521gutnB7Ct6rF9vdbFppugxHwczGSKwOihdzDVeEbqrOdLRbon4w3BrsEGESfv4 YkuQ4G25sUXlh19tI+Wx39b1nW3mvEgl6sLAHQPlnJQ52A4gaZJ3Cb5rWOO9obNUrKPlmV /iVgy9BXgfxxMXZZLSfdPB+XzeQu/PlyGygzkg8NmMJPOp8pxECpSH7BxpASiIJrVJJ+qw ucJuw+GTqGpN4M/LA0F0GpAkxvGtvLFHJdjLRFYEuwZ+cmhOKNQVQrcZ1T/QjmnLAzomgg 6qdSAy67vHbDO2ASsJ6Qxeq/vU7gBIOivU8WRpgf/JhiIAmNjthpuLmxh3N1sA== Message-ID: <4c2b5401-41ad-43b9-9215-c7b2d901f8ed@vondra.me> Date: Fri, 21 Feb 2025 16:01:00 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Enhancing Memory Context Statistics Reporting To: Rahila Syed , PostgreSQL-development Cc: Daniel Gustafsson References: <7d4db5cf-8d72-4ee1-8f2e-f88ef9615f27@vondra.me> <637b3ae3-b9eb-4d83-a942-5f7483911ea4@vondra.me> <29ced66e28ec696d1ac29747bd901df2@oss.nttdata.com> <5a899047-65fc-47c5-9c35-1035f44956c6@vondra.me> <70146bd0-da0f-48ee-bece-1e35536189e0@oss.nttdata.com> <2eb8fd76-89f0-42e8-bc79-152d50c63016@vondra.me> <81607c8c04a21b5cac87b21e46566074@oss.nttdata.com> Content-Language: en-US From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdejtdefgecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepkfffgggfuffvvehfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpefvohhmrghsucggohhnughrrgcuoehtohhmrghssehvohhnughrrgdrmhgvqeenucggtffrrghtthgvrhhnpeeuvddvieefffefkedugefgtdeigeelgfegudehffevieehgffghefgvdduteffveenucfkphepkeeirdegledrvdefiedrudelfeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeekiedrgeelrddvfeeirdduleefpdhhvghloheplgdutddrudefjedrtddrvdgnpdhmrghilhhfrhhomhepthhomhgrshesvhhonhgurhgrrdhmvgdpnhgspghrtghpthhtohepfedprhgtphhtthhopehrrghhihhlrghshigvugeltdesghhmrghilhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehpohhsthhgrhgvshhqlhdrohhrghdprhgtphhtthhopegurghnihgvlheshigvshhqlhdrshgv X-GND-Sasl: tomas@vondra.me List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/20/25 14:26, Rahila Syed wrote: > Hi, > > Please find attached the updated patches after some cleanup and test > fixes.   > > Thank you, > Rahila Syed > > On Tue, Feb 18, 2025 at 6:35 PM Rahila Syed > wrote: > > Hi, > > > Thanks for updating the patch! > > The below comments would be a bit too detailed at this stage, > but I’d > like to share the points I noticed. > > Thanks for sharing the detailed comments. I have incorporated some > of them > into the new version of the patch. I will include the rest when I > refine and > comment the code further. > > Meanwhile, I have fixed the following outstanding issues: > > 1.  Currently one DSA  is created per backend when the first > request for > statistics is made and remains for the lifetime of the server. > I think I should add logic to periodically destroy DSAs, when memory > context statistics are not being *actively* queried from the > backend, > as determined by the statistics timestamp. > >    > After an offline discussion with Andres and Tomas, I have fixed this > to use > only one DSA for all the publishing backends/processes. Each backend >  allocates smaller chunks of memory within the DSA while publishing > statistics. > These chunks are tracked independently by each backend, ensuring > that two > publishing backends/processes do not block each other despite using > the same > DSA. This approach eliminates the overhead of creating multiple DSAs, > one for each backend.  > > I am not destroying the DSA area because it stores the previously > published > statistics for each process. This allows the system to display older > statistics > when the latest data cannot be retrieved within a reasonable time. > Only the most recently updated statistics are kept, while all > earlier ones > are freed using dsa_free by each backend when they are no longer needed. > .   I think something is not quite right, because if I try running a simple pgbench script that does pg_get_process_memory_contexts() on PIDs of random postgres process (just like in the past), I immediately get this: pgbench: error: client 28 script 0 aborted in command 0 query 0: ERROR: can't attach the same segment more than once pgbench: error: client 10 script 0 aborted in command 0 query 0: ERROR: can't attach the same segment more than once pgbench: error: client 5 script 0 aborted in command 0 query 0: ERROR: can't attach the same segment more than once pgbench: error: client 8 script 0 aborted in command 0 query 0: ERROR: can't attach the same segment more than once ... Perhaps the backends need to synchronize creation of the DSA? > > 2. The two issues reported by Fujii-san here: [1]. > i. I have proposed a fix for the first issue here [2]. > ii. I am able to reproduce the second issue. This happens when > we try  > to query statistics of a backend running infinite_recurse.sql. > While I am  > working on finding a root-cause, I think it happens due to some > memory  > being overwritten due to to stack-depth violation, as the issue > is not seen  > when I reduce the max_stack_depth to 100kb. >  } >  } > > > The second issue is also resolved by using smaller allocations > within a DSA. > Previously, it occurred because a few statically allocated strings > were placed > within a single large chunk of DSA allocation. I have changed this > to use > dynamically allocated chunks with dsa_allocate0 within the same DSA.   > Sounds good. Do you have any measurements how much this reduced the size of the entries written to the DSA? How many entries will fit into 1MB of shared memory? regards -- Tomas Vondra