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.96) (envelope-from ) id 1w247z-000qgI-19 for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Mar 2026 09:20:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w247y-008n8y-1T for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Mar 2026 09:20:47 +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.96) (envelope-from ) id 1w247y-008n8q-0a for pgsql-hackers@lists.postgresql.org; Mon, 16 Mar 2026 09:20:46 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w247w-00000000OlZ-0nu4 for pgsql-hackers@lists.postgresql.org; Mon, 16 Mar 2026 09:20:46 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-439b73f4ab4so4866884f8f.1 for ; Mon, 16 Mar 2026 02:20:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773652844; x=1774257644; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=oL9Cwb/bfAcnFaQhyX7zYZq8TNdVA7q5d1qSLJ0gXN0=; b=Ygiy9r9ErrF65bK6nbNQpHW4NWP8iX5qTJ7TFN3rTCghDer1eGrANR835azjcpQ+67 Ff6kXYwZMqzQdwqZDjsvyoedG4tr898yVINLnryvJTING6tZzijxJYjBTMT28AxaG/Ef U9NMCHdaNG5nryT/oU9vNmeJQgshXcNLYF8UeaNPVFkCodGCiYDGTNQTq9SLLpTJoJI8 y+dLY8JcBbDsevLxX7kE7qdz8NpKq5NXv5zJNoY/CdaGvHKDTTJjcb1gKjQbEF9NpS2F biApNg+wnySK2LCt9wEo9X0ZP487Y/THD0YLuI7kRQSuVm3ajIggp1sdFUyK+kQ3d7fp UUkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773652844; x=1774257644; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oL9Cwb/bfAcnFaQhyX7zYZq8TNdVA7q5d1qSLJ0gXN0=; b=Gc8GA9NovVG/w2ebPxU3sFHbuQZVn0M8YgjU7evhUfECJUpp1UhqFyADVxW4UnntXx pXUSEB86ePXkGNptoU70F7vk52q48f57ADZYQPfRgP51t3YJjsDD0kGBXlL85bOiQzNy wk7Kth9ZXK0VBXMLsbb+OA26SULeDxmSUjBMW4ZgHwIiDg5LrmmdTGBg/ywf8hpv/19S laskc14nLTYROmXehvY2Ri7YqTAj8FUiq6qNOUEuKDfy/gOVUCHDnk3jJR0z+SXQku9E P8pcr5qYZOWDuZDiVgE740RmgHUXaArQAqHdPnHdgnpGfZULfZZxtRajhEWGbR2M2pTG 57HA== X-Forwarded-Encrypted: i=1; AJvYcCVWyQ0mxhuUHE84+RW3OQzYaRs+N7SpL2dqdMbHP23sVu0pRAt0qNAjoy3UOS/4ykxu+mhlO0xAV4rnmNwl@lists.postgresql.org X-Gm-Message-State: AOJu0YyaPAVE56NdOgO38LkbT2fYpZmI3FfbYmoLdpZ/q8xeekqzlnup jMzIKUaUYXGpipZspQmfzUstl/GCkSt+bRAqzBUba4QoR8yEFJEJI2U1 X-Gm-Gg: ATEYQzx6jxIc1BEBF7CnHhbhvoOGJxbCOt6S/fOwsKmAV48o3r+N85FSjIUeFooM1UY oD+bu3WycpkaS31IEX5owAv3yJ+Jc6dPzlOw1luA1he40y+Scm17rn5aDsKcKKo3TpxKGohE0zR Hvpz7i/aaQJG8bLsIFLKrbHtoaBgzOIDl2ApyjsOKjg/J/qWKR4C3InsCdmOavm8nu5RhGG5fF1 lCg4GmLOXSwEihLrn5as5I0T339xdrtbViq8rdgP72ap67Ji9ikr9u5U36UYL+UQBz6NNnpa9VB BhCP6wGKPd0tQfBIO/H0Un6h9b6I5E8DII0hlPuHjTrONpTcqIkFWbcBeBJSVzXLjrqCI09oQ1v l/B8eFiYw83MUMFSczhyxn9ifBc2yy5lxwmjrsISlx/Lcu7fZgOiMX1Y7eN6Z/HJMLrkEp2H4sG GejQK+l16hEHifLi4uvS0d/D2Zp6cgDlauhdj77BVMocBMulpOEYwsSsYB0yR5G+scbFsBDWsjd 3QqC+BOeW11OBw9Yg11uiyd7bKJTbjZ4hsPHkoow6LDtP2p/fMicQilL6ezJ/c0H+Lk X-Received: by 2002:a05:6000:2404:b0:439:be82:1fd9 with SMTP id ffacd0b85a97d-43a04d83e11mr22521341f8f.12.1773652843338; Mon, 16 Mar 2026 02:20:43 -0700 (PDT) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-237-197-144.eu-west-3.compute.amazonaws.com. [15.237.197.144]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43b3d87b1fbsm16622212f8f.7.2026.03.16.02.20.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2026 02:20:42 -0700 (PDT) Date: Mon, 16 Mar 2026 09:20:41 +0000 From: Bertrand Drouvot To: Michael Paquier Cc: Sami Imseih , Fujii Masao , pgsql-hackers@lists.postgresql.org, Zsolt Parragi Subject: Re: Flush some statistics within running transactions Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Mon, Mar 16, 2026 at 03:26:33PM +0900, Michael Paquier wrote: > On Tue, Feb 24, 2026 at 12:01:30PM +0000, Bertrand Drouvot wrote: > > Though I don't think that adresses Michael's concern: "main worries are > > mainly around 1), I guess, with the new SIGALRM handler requirements for all > > auxiliary processes" in [1]. > > FWIW, I am still concerned about that, and I have pondered about what > we could do here. While reviewing the existing code, one thing that I > have noticed we could do is rely on the existing interface of > pgstat_report_stat() without changing the existing callers, and not > touching at all the flush callbacks. If we begin to require the > "force" mode when the routine the called inside a transaction block, > things seem to work pretty smoothly in combination with a stats kind > property that allows the stats data to be flushed if we are inside a > transaction while a report happens. Yeah, "force" makes use of GetCurrentTimestamp() (and so we avoid a failed assertion that we would get if using GetCurrentTransactionStopTimestamp()). > So please find attached my shot at that: Thanks! > - Introduction of a new system function called pg_stat_report(), based > on a procsignal that gives a way to signal backends for a stats > update, reusing the existing code where we only do flushes when idle > and not in a transaction. > - Property that tracks under which contexts the reports are allowed. > Here I have decided to stick with simple, as in only allowing IO and > WAL stats to be flushed if we are inside a transaction. > > Using that, I have done a few tests with three backends: > - One with a long-running transaction. > - One that periodically triggers the reports. > - One that looks at IO and WAL stat. > And the third session is able to get refreshes for both of these stats > kinds, while the other stats remain the same. I did not look closely at the code but did some testing too. I confirm that pg_stat_io and pg_stat_wal are updated when pg_stat_report() is triggered. But the stats update is not visible if requested through pg_stat_get_backend_io() or pg_stat_get_backend_wal()). I guess that PGSTAT_KIND_BACKEND should also get the PGSTAT_REPORT_TRANSACTION report_context? > Note that this is a WIP, which is check-world stable. One thing that > sticks a bit in mind now is that perhaps we should not allow the > function for auxiliary processes at all. Why? > A second thing is the > requirement of allowing partial flushes at the end of the report path, > which is OK because the variable-sized stats can have pending data. Right. > Perhaps we should just have pgstat_flush_pending_entries() provide a > correct status in line with the property set in a stats kind when we > try a flush while in a transaction. The idea would be to avoid trying to flush stats that don't have pending entries? > Thoughts or tomatoes? That looks "simpler" that the previous proposal but who would be responsible to call pg_stat_report()? If that's the client responsabilty that kind of look weird to me. If that's the core, how would that be scheduled? I think that the end solution should prevent to find similar issues as 039549d70f6 fixed, without delegating to the client. Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com