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 1vuLQv-00GLy0-2y for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Feb 2026 02:12:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vuLQt-00BfD6-2W for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Feb 2026 02:12:23 +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.96) (envelope-from ) id 1vuLQt-00BfCx-0z for pgsql-hackers@lists.postgresql.org; Mon, 23 Feb 2026 02:12:23 +0000 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vuLQp-00000000jGz-3poT for pgsql-hackers@lists.postgresql.org; Mon, 23 Feb 2026 02:12:22 +0000 Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-6580dbdb41eso4989887a12.0 for ; Sun, 22 Feb 2026 18:12:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1771812738; cv=none; d=google.com; s=arc-20240605; b=h/oHQZgQaefdrZaHtOzf6MhiHYOS/eMOiMu585lc9j7LGss4MXAB+HxqwjHIzmdpbQ jJ93DR6RaXUa29yldllnn8vbUi6cbJww0sPWxzriSjfeVY0r9Z+tVlzH4tY7j8NyF7a8 b4lBywRHM2BhKANsPv+9cu5Fre6rKqlG8UC7VAw40dkxHnuIZ4hfYthAp2Xqvk8/LP4B 0n9fraQmlpC12w1XE0o4noFrxvXNC94z/XfWvPf8dQknKQ6GqhGgfE1Sd0OdU7MVhEwi 2N3MC9+RYGeh+gNOWAbk6besm7pCs1u/PEy0u5aY8SbGdN8Lr9DISFTeCQ8K3vcoSwi6 IzLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=vuVn/hC/bYb8+wFC6Rl+bWubJlYb2yE9cldWS/RcLuQ=; fh=ZYZuOCaAQAj+Lqah9RsGbcw44KygbGjjfGjZkFRTo4Q=; b=jXqFS223MFJr3cNrxH7fg2fS/VRcLsx62dn/jCi3iT+3dgx1WVj4wIAiJTHejh2Abh tMYiHekyLuzNlzebFS73X5E42pGG1/vMgN1WCgpvtNdXR/zC8TbbjOwcHjdqcDWIh1m0 rKQam9LrnAJTUAz5mgwPFKCX6l1TJLGATnd8B+/Sns1fy7CR634Ld9+qDkCO2RN2ozs8 oKtxZMvaRlbJS/RexF50eq4cyuQ1pD/eX4S7FZuFQ53NI8tWueTYCXmrXTSVgz+QZaGX LnIR7BgZylALsC/BVna9IgBf+dCd5MfIhbJCO+S3RblqjbNarIho/5HplGFOqW9kRUYS Dskw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771812738; x=1772417538; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=vuVn/hC/bYb8+wFC6Rl+bWubJlYb2yE9cldWS/RcLuQ=; b=H20a9YsdMbRxMBJCA4ugYv63wLfn/QUKhzSGxryOb8gmfaLiGLEkbq1qKG9IGgoVXY zbY8Zl6DeMGkBgPWeyxFnX6gglWDPurshQgnL9oU6yFSN6RPfwPXvp5KpFPYwoooqRSp 5dnXWXfPP3ZpuNAh9TM3Ld4XgpaMn7HXoYgEWPOjwqKpbMwl0EjV2M4NRysy5Qbh/Dub vSb95nvo1czS8Rzh1K4wKsbmnU9QpRW82LC76xK/X8svbp6hQk+9e5VIr6fTXsmsHJHy aucAmt3QL2P3yMXfX+Q1njmTkUvtcafXQAEa4gtKesME4JI2Wd5gpsBPkmuWaVN3uWGB 8Yow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771812738; x=1772417538; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=vuVn/hC/bYb8+wFC6Rl+bWubJlYb2yE9cldWS/RcLuQ=; b=aRQh/YC2y5vyMSF34qf8MNRrks2a2Mj1/5ePD69zlnPMji8ADaKQ5Wue1vC66+MrnU mchqsxi97ryVZd/vHXKFtjxAJ/5QtsMvmQ+PgrtFmicZhw4iN7Mim69Egt6OvTnL+BqA e5uySYCsBf4AjbIISxzYTCegB66lnwibVIvUhS9moFwKhFOQ81HQswME3R+dmPeXakvH rY+URqx50ikN0x3s2obJp2aGGFLxEedGKBe8zG81q7kJTn9ytAaah3LQ+YtYnL7D3Nvb rGRf44P5pO9fEhWS3S/1ZbVZREuGf3aqJnj6FlyvXFyA5wHhlNzBtwIA7PyQiSofO0JC ieVQ== X-Forwarded-Encrypted: i=1; AJvYcCXNaS58+/fk4EXLB7J7BLMY20eKGT2GmNjbsppTQscFz6wP7Wg/V1xPVehw25rq+1gqruSj+OSnYC1O8zQ5@lists.postgresql.org X-Gm-Message-State: AOJu0Yy528VqpMrzhuSc01KYuPsdluyibU3ugQTSBW4H4yRBN38q45KM aY/C76aNH1ixhOgRXkb5v8+U+MWj8JaexWuoasM4YYSyRJaQu9mdyk1j5ydquHahFAuMAA9ecnF IMO9KPGMW26a2vztMUuzDIEm2i8wyxBY= X-Gm-Gg: AZuq6aLdLvParHc7bscylJ/T8dxYYDprSobpVSZ3CFiqCE1vr3psOe1Ezm03fFoL8Zo TYSZk7flENrO9BMQSGJdFoWUMfbJMT40WjTZxRzS2a3EleM1w2fO5av1mLK6Paiq430CNsBuR+i 2cbVkMsN1fFPbXKsTdEDE8uVwe/2d6SPyesN0xfEqbagbvQKnEGLQEQUc7gDKOZSk0AQOHHbLhf a4zVeegLp6ViA5pMSaX61GUJaDfraC5KEj4qjpDLEjuLBD66v/g6dmRhIe2E0a+DPhnrpv4fHrB hqII7g== X-Received: by 2002:a17:907:841:b0:b87:fad:442f with SMTP id a640c23a62f3a-b9081b5dd88mr384614466b.42.1771812738060; Sun, 22 Feb 2026 18:12:18 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Sun, 22 Feb 2026 20:12:06 -0600 X-Gm-Features: AaiRm53wOHJKZN977ulo5L7_BSSnWV44itjcMhvTDbwLfK-5hhi8tx5PdMqu9Z8 Message-ID: Subject: Re: Flush some statistics within running transactions To: Bertrand Drouvot Cc: Michael Paquier , Fujii Masao , pgsql-hackers@lists.postgresql.org, Zsolt Parragi Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > > I took a look at this today out of interest, > > Thanks! > > > so, instead of calling IS_INJECTION_POINT_ATTACHED macro which is > > I think that not calling IS_INJECTION_POINT_ATTACHED() but only relying on > "ifdef USE_INJECTION_POINTS" would set the tiny timeout value to the entire test > suit. Yes, you are right about that. To Michael's earlier comments: > I don't find the design of this patch appealing, and my mind points > towards two pieces of it: > 1) The new requirement related to pgstat_schedule_anytime_update() > that a stats kind needs to call to enable a timeout. This partially > doubles with pgstat_report_fixed. And I suspect that this extra set > of requirements, introducing a new level of complexity for in-core > stats kinds as well as extension developers, would be the source of > more bugs. I don't see how for fixed stats, adding a pgstat_schedule_anytime_update() call for such kinds will be too complex or more error prone. it looks quite straightforward as this is done before pgstat_report_fixed is set to true. Also, because processes like wal-sender can loop forever, as mentioned here [1], a timeout at stats_flush_interval seems like a straightforward way to deal with this problem. For variable-length statistics, perhaps we can do things a bit differently than what is currently proposed. 0005 requires a relation anytime stat update to call pgstat_schedule_anytime_update(). This is done this way because it allows long-running queries to update their stats every stats_flush_interval using a timeout. But maybe what we should be doing for variable-numbered stats is to schedule an anytime update whenever a "transaction goes idle". This way, unlike the current state of things where we are only updating relation stats at the end of a transaction, we are now updating relation stats at the end of SQL execution, and within a transaction. Basically, we will continue scheduling anytime updates every stats_flush_interval for fixed stats (xlog, bgwriter, etc.), but for variable stats, we only update anytime stats after SQL execution. This is better than what we have now, where stats are only updated at the end of the transaction. The timeout will only be needed to schedule an update for fixed stats. For variable stats, we can use GetCurrentStatementStartTimestamp, which is the timestamp of the last query executed, to throttle pgstat_flush_pending_entries(). We can also flush variable number stats whenever we flush fixed number stats, in case we enter a long idle-in-transaction state after a few quick back-to-back queries. > 2) The timeout requirement itself, relying on a timeout threshold > controlled by a backend-side configuration. Perhaps we may not need the stats_flush_inteval and just force a 10 second timeout for fixed stats. > With that in mind, wouldn't it be simpler if we introduced an API that > could be used from client applications instead, in a model similar > what we do for procsignal.c/h? One such example is > LOG_MEMORY_CONTEXT, where we have a SQL function that is able to tell > to a backend that it needs to do something. I could see various > benefits to this approach, because it gives more flexibility with the > timing of the stats flushes, which may not be a backend-side only > policy: > - Use a cron bgworker in the backend, that scans pg_stat_activity, for > example for long-running transactions based on a threshold. > - Do the same periodic scan of pg_stat_activity, but from a client > application. I find it odd to ask applications/clients to trigger a flush. I am not saying that we should not offer such an API, especially if someone want to flush stats more frequently than stats_flush_interval, but there should be the ability for core to handle this automatically outside of the transaction boundaries. One comment about the current test. I think there is a bug that was missed in the earlier review. For the var_anytime_update, we need to have an escape before the pipe. Also, we should set stats_fetch_consistency=none in that test. -like($result, qr/^entry2|2|/m, +like($result, qr/^entry2\|2\|/m, Otherwise, the test is returning a false-positive. [1] https://www.postgresql.org/message-id/erpzwxoptqhuptdrtehqydzjapvroumkhh7lc6poclbhe7jk7l%40l3yfsq5q4pw7 -- Sami Imseih Amazon Web Services (AWS)