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 1wUIeM-0014KD-0I for pgsql-hackers@arkaria.postgresql.org; Tue, 02 Jun 2026 06:30:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wUIeK-00DAK9-2r for pgsql-hackers@arkaria.postgresql.org; Tue, 02 Jun 2026 06:30:52 +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 1wUIeK-00DAK1-0h for pgsql-hackers@lists.postgresql.org; Tue, 02 Jun 2026 06:30:52 +0000 Received: from fout-b1-smtp.messagingengine.com ([202.12.124.144]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wUIeH-00000000o2N-2Agn for pgsql-hackers@postgresql.org; Tue, 02 Jun 2026 06:30:51 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfout.stl.internal (Postfix) with ESMTP id B0B0D1D000BD; Tue, 2 Jun 2026 02:30:46 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Tue, 02 Jun 2026 02:30:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1780381846; x=1780468246; bh=5VSKFVsXYu 43M1MyyhMQjG7QUKBkeVK++zmwRuzroTM=; b=tcIbGjwya8ENCS+8afTkiNHf2d vvqratOw81NgWQoV4ufI8EVJY6jqwzS1+VCi8Jrgp7XYM3Y+Wb9ZaOObQxVTiLhk frox0XBMcLUeUFySKYREZwUWwBnxJ7E0w+Lo14Z6IfiSSEZQ7iMs4ZchXCSrHODV DVoO40NfepHsLlGlnaPE8xtkIC0iCzTFF3tBTh7G1NVEktpggDSf7Zr1tbbpNRQD dCis+xa02r/Ka5FlShMe1PI7Bne+svyZkXf55/hgu6ygUdcEc9m9Sr9Mak9coLdY M1rkR/WNc4+LCocViYrV5hYn7q0xgrRn07dIMFA9545dNUYsIOKVsFnmiqnQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1780381846; x=1780468246; bh=5VSKFVsXYu43M1MyyhMQjG7QUKBkeVK++zm wRuzroTM=; b=UoERNT09USUkUZmtR5+nkJteQLWy5Ntqx/O5jdEx5WcTA/MTVAV 34qN3AkqTtWKAJIlYVPxHzrMc+m+i4PfgvC3PVxWg0N+D27+UXJ0EYh+9qFEOnt2 OPZRWeg34SLH2kU6RM/Bt6tHhKE6Q0Lb9WnrtVLNoq8/JwcB7iaF2uUc9W3DjQFa wjT1JrhVz+2Olh9swbgSGz+EmOKX+wz+aEl4PNfuafoDtJuRhXTt39YJQbMR61qD m5lrom3wRryONL09E61ru29id8Y7TsmFT1boSWfU6q3iz8cohR/fHiY4kReIY57c NLL52AJI/P7rw5+WeTBoEY5+OZO63ptH5qg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTE6df8PPsW5WgMsIp/7JIM3rWtqdv11G930hH4AvwB8wQ+Er2PZ9AvgjENvBF8PLo vJngwgoPlQd1fa5x3aTTHfbwXyb8ILzKQSlhSZhDnAGowO7poZgJOuIq+qm1b5PJtJhE2e 8dHVLsDMHj6UX/dGbUGogdkniALury5SBhzoI/Xsr90Sct+eXI2FqZxqZkxn5VkP+l9Sh5 3TRXaDb8BVF1/dnioadHvTtpu7Rimu7okKiv/32eTCTX01gTbj1CKJomG2wQzivj0myjma nw3QC7uf8PoZBNZkO7cHERwvpf+LCMa1dep639099cG7wGmZWbTmCb2GlwgbKQgBdzn4I3 FsmioRbwVVOD7o43cdWo2fxr6O2Q9rmCPjz+bL7LmptT6KvUZPkDmI4vHunlwab0yg9Hy6 FlIyMx0LwaKtSp2/PazTf8+JGfUoot0xdVOZgX2Q6qbw2TnkFgaua88t1ApjbzMFOnut3Z Qk74PzrC75Ov8Mwo/NpSEdG18Ho9eaaY1FuQGRlwnon4rRcjf3GSTOpdouNO0DWO4qQI+O tgaRGEgH8e0277QGWsC4F9d40Op9m+tCXXDY7xLCQRhUcd2uLPYNooISOw4MNx3kcpvvW/ QzAwMkzXNaXuPN2oEtx//6oroYLCu5HfKNl34bpAbJ4FiM8NK+LN+rZvt7Fg X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 2 Jun 2026 02:30:44 -0400 (EDT) Date: Tue, 2 Jun 2026 15:30:40 +0900 From: Michael Paquier To: Kyotaro Horiguchi Cc: samimseih@gmail.com, lukas@fittl.com, pgsql-hackers@postgresql.org Subject: Re: Improve pg_stat_statements scalability Message-ID: References: <20260601.135858.1116584574478485492.horikyota.ntt@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="pcSJjTYQvTResfOD" Content-Disposition: inline In-Reply-To: <20260601.135858.1116584574478485492.horikyota.ntt@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --pcSJjTYQvTResfOD Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jun 01, 2026 at 01:58:58PM +0900, Kyotaro Horiguchi wrote: > As far as I can see, the current implementation does not apply any > throttling to calls of this API. In theory, a large number of backends > could invoke it frequently and generate a high flush rate. For > example, if 1000 backends were to call it once per second, that would > result in 1000 shared-stats updates per second. >=20 > Whether such a usage pattern would occur in practice is a separate > question. However, given that existing pgstat code uses > PGSTAT_MIN_INTERVAL to limit flush frequency, it seems to me that > anytime stats should probably retain a similar restriction. Hmm, OK. One cost in this decision is that it could randomly make the tests randomly slower, which is one reason why this patch has been added to this thread. > Historically, flushing and freeing an entry were effectively the same > decision because flushing only happened after transaction end. With > anytime flushes, however, those become separate concerns. A callback > may be able to flush everything that is appropriate for the current > flush context, while the caller may still need to keep the entry > around for later transaction-end processing. Noted. > That makes it hard to reason about checks such as > pg_memory_is_all_zeros(&lstats->counts, ...). This check still appears > to tell whether the counters themselves are zero, but the proposed > change makes it look as if that is no longer enough to determine whether > the entry is "empty", because entry lifetime is also folded into the > callback result. >=20 > I wonder whether it would be cleaner for the caller to make the lifetime > decision, based on the kind of flush being performed, and for the > callback to be told that flush context explicitly. For example, the > callback could be passed whether this is an anytime flush or a > transaction-boundary flush, flush the counters that are appropriate for > that context, and then return whether any counters remain. So you would suggest to extend the flush callback(s) with a context value instead of having each flush callback do their own decisions depending on if we are in a transaction block or not, right? Another question that I would have for you is: do you think that we should try to keep pgstat_report_stat() as the sole entry point for the flush of the stats? Or should we try to keep the same approach as what this patch is doing with a new routine that loops through the flush callbacks? FWIW, I am kind of finding the approach of having a single entry point rather than two as more appealing with the long-term picture in mind. That's just a single take, where we could just pgstat_report_stat() an extra argument based on the context where it is called, lifting its current !IsTransactionOrTransactionBlock() requirement. As you are the original author of this area of this code, I'd be really interested in your opinion here. -- Michael --pcSJjTYQvTResfOD Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmoeeJAACgkQnvQgOdby QH1e0xAAiaRd4o3fQ0bhjA1AYTw1im6CntZtPS6ECfOaN4teI5HnNaBsn6+4IO7k qXAxnT5BzFh4y16RHPCT60SPKiqWE4Y0/FWgdd1SN3JJWpJE62SFFCAakbeqyUL5 QFpW8Sv3JkRYaub7XAvtFxcJ18oqYTkhO3P5wv4iYwG4LLN3Wnv4o5lf4hdZE4Ko qNIiYsphmlZi5781R/kHJB2bROGf29C04IiWvaivnq8ZFe2CyVIXb68TcP1aTp+j 1TP6kC8T0XjoSKzmuNlakqxMwgDXIPB4MSk5g+5MlEswLzl/SSThHU3cFIdU7HZy 8UZgt2zYTCO0jseuTMlkYAc+30yTxjZ8qz9HfiC2Gnv3o54bVA29MHE/Zr2Pd0mL vY4PbYwBDKEDfiOe3gha41oGQGGtKpHt02tgzYMI1IfjGpJ9YNG6zuT1tFsOpf9N Ehhh/7EuVF/yaiQ+tcJNtVe/KFkMdKGIWEkYIzEb6m2QlymGzVJcsIzghzgxWGFb 83c+4kXE9CRSH6TJd3eZY9v2Z2SsmCOHxDsriocYUnsNHm3L5seeiZUuZrpSden1 4QN9wTl80BAuBM4pNHfM1o0LWRaByfz3q27TQ0B5pm5BMpQWjqHvH1a+thr97AWz UyOqEvO29mf3JbjTXh0HdlpfmhmzyN/MLDI7J1YrvcoHPvhL5Lg= =Un+0 -----END PGP SIGNATURE----- --pcSJjTYQvTResfOD--