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 1wA6mC-0024OD-2x for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Apr 2026 13:47:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wA6mB-000EKL-0K for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Apr 2026 13:47:31 +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 1wA6mA-000EKD-2Y for pgsql-hackers@lists.postgresql.org; Tue, 07 Apr 2026 13:47:31 +0000 Received: from fout-a2-smtp.messagingengine.com ([103.168.172.145]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wA6m8-0000000173J-34tf for pgsql-hackers@lists.postgresql.org; Tue, 07 Apr 2026 13:47:30 +0000 Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfout.phl.internal (Postfix) with ESMTP id 55F02EC0470; Tue, 7 Apr 2026 09:47:27 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Tue, 07 Apr 2026 09:47:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc:content-transfer-encoding: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=fm2; t=1775569647; x=1775656047; bh=ej4Gf7DrfTF551b1QxBuIDxLZCHMjibIE07uUGcUZ+8=; b= F+p3y24de5xDQ1pEQsYUC9gNtLN3k7cKQ370QZ1xOB+JZTdo7zZJ0hzlK1lNT7me TTH7elwUrLHIZsZJqbgzuRHy2YEV18g/uEN7fubpOIYz2NIkPJu9paqmI4VePMye rWCxmbiE/Mkl8PXVVcNJ83xvbZ3vxyAEvn31smN4kq5xELWy1aUCS0Aaa0CAfG6m petb/XamfY7ZzYgPpyT/2039kX2Gm+iMXqXUH4DWSVEE42wDDcsYRcAAZJ02qe6w j0IPxYovFJv+S2qm4QyTw5X4PtoVUHjAUhLqEg7pxBuUqFqY8YCBp0FI4eH6VVkr sKmu7FRIVYs7nyK2LDCROw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :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=fm2; t=1775569647; x= 1775656047; bh=ej4Gf7DrfTF551b1QxBuIDxLZCHMjibIE07uUGcUZ+8=; b=Z qo3XyN3A2QCZYjQOS/exu9olrhFgJPWkwIZCDSaqMmEWNDxcRoBKGwAWSKJysYp7 4eyc5qb4PzHv48t+bQw9uyYhDutMJMs6Njmy3gJrf1PW9KbAcFt07r2dAMKTOWLi zpr/oRlJfGNatNGykdlDy3tw35ts+AwfkIL8OsD0+hZRDw52Gs6qK67ziaB2deBm uuL9IgOcvAkK0W98Ai3yYKPWLVomRzyQFToVEwOK21v6mdJwb8iEmlfAJ/MePDVi dQ/e/Ro6l+P5nKlFfKm0kGclRewKMfbSCj08r13RcZbSmTVHVF93OVfpxeLpeBPG nx6lwvP2ShCGHNbvfxsUw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgddvtdektdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtugfgjgestheksfdttddtjeenucfhrhhomheptehnughrvghs ucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecuggftrfgrth htvghrnheptdelledvgfejvdffieeukeefueelfffhgeffhffgffekveeuheeihefhiefg hfdvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprg hnughrvghssegrnhgrrhgriigvlhdruggvpdhnsggprhgtphhtthhopeelpdhmohguvgep shhmthhpohhuthdprhgtphhtthhopehluhhkrghssehfihhtthhlrdgtohhmpdhrtghpth htoheprghshhhuthhoshhhrdgsrghprghtrdhoshhssehgmhgrihhlrdgtohhmpdhrtghp thhtoheptghhrghtuhhrvhgvughiphgrlhgrkhduleduudesghhmrghilhdrtghomhdprh gtphhtthhopehkhhhorgguuhihnhhguhihvghnsehgmhgrihhlrdgtohhmpdhrtghpthht ohepshgrfigruggrrdhmshhhkhesghhmrghilhdrtghomhdprhgtphhtthhopehhlhhinh hnrghkrgesihhkihdrfhhipdhrtghpthhtohepphhjsehilhhluhhmihhnrghtvggutgho mhhpuhhtihhnghdrtghomhdprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehlih hsthhsrdhpohhsthhgrhgvshhqlhdrohhrghdprhgtphhtthhopehtohhmrghssehvohhn ughrrgdrmhgv X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 7 Apr 2026 09:47:26 -0400 (EDT) Date: Tue, 7 Apr 2026 09:47:26 -0400 From: Andres Freund To: Heikki Linnakangas Cc: Ashutosh Bapat , Tomas Vondra , chaturvedipalak1911@gmail.com, Masahiko Sawada , Lukas Fittl , PostgreSQL Hackers , Paul A Jungwirth , Khoa Nguyen Subject: Re: pg_buffercache: Add per-relation summary stats Message-ID: References: <670b867e-6518-48b0-b7c2-bd5f10ab58c6@vondra.me> <7ab3914f-da59-4c1f-b809-225637b586e8@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <7ab3914f-da59-4c1f-b809-225637b586e8@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2026-04-07 16:07:45 +0300, Heikki Linnakangas wrote: > On 28/03/2026 06:18, Ashutosh Bapat wrote: > > Parallely myself and Palak Chaturvedi developed a quick patch to > > modernise pg_buffercache_pages() and use tuplestore so that it doesn't > > have to rely on NBuffers being the same between start of the scan, > > when memory allocated, when the scan ends - a condition possible with > > resizing buffer cache. It seems to improve the timings by about 10-30% > > on my laptop for 128MB buffercache size. Without this patch the time > > taken to execute Lukas's query varies between 10-15ms on my laptop. > > With this patch it varies between 8-9ms. So the timing is more stable > > as a side effect. It's not a 10x improvement that we are looking for > > but it looks like a step in the right direction. That improvement > > seems to come purely because we avoid creating a heap tuple. I wonder > > if there are some places up in the execution tree where full > > heaptuples get formed again instead of continuing to use minimal > > tuples or places where we perform some extra actions that are not > > required. I don't think that's the reason for the improvement - tuplestore_putvalues() forms a minimal tuple, and the cost to form a minimal tuple and a heap tuple aren't meaningfully different. I think the problem is that we materialize rowmode SRFs as a tuplestore if they are in the from list. You can easily see this even with just generate_series(): postgres[1520825][1]=# SELECT count(*) FROM generate_series(1, 1000000); ┌─────────┐ │ count │ ├─────────┤ │ 1000000 │ └─────────┘ (1 row) Time: 117.939 ms postgres[1520825][1]=# SELECT count(*) FROM (SELECT generate_series(1, 1000000)); ┌─────────┐ │ count │ ├─────────┤ │ 1000000 │ └─────────┘ (1 row) Time: 58.914 ms Of course, because pg_buffercache_pages() is archaicially defined without defininig its output columns, you can't actually use it in the select list. But that can be fixed: CREATE FUNCTION pg_buffercache_pages_fast(OUT bufferid integer, OUT relfilenode oid, OUT reltablespace oid, OUT reldatabase oid, OUT relforknumber int2, OUT relblocknumber int8, OUT isdirty bool, OUT usagecount int2, OUT pinning_backends int4) RETURNS SETOF RECORD AS '$libdir/pg_buffercache', 'pg_buffercache_pages' LANGUAGE C PARALLEL SAFE; 60GB of s_b, mostly filled, with 257c8231bf97a77378f6fedb826b1243f0a41612 reverted. SELECT count(*) FROM (SELECT pg_buffercache_pages_fast()); Time: 1518.704 ms (00:01.519) SELECT count(*) FROM pg_buffercache_pages_fast(); Time: 2008.101 ms (00:02.008) > > I didn't dig into the history to find out why we didn't modernize > > pg_buffercache_pages(). I don't see any hazard though. > > Committed this modernization patch, thanks! > > It would be nice to have a proper row-at-a-time mode that would avoid > materializing the result, but collecting all the data in a temporary array > is clearly worse than just putting them to the tuplestore directly. The only > reason I can think of why we'd prefer to use a temporary array like that is > to get a more consistent snapshot of all the buffers, by keeping the time > spent scanning the buffers as short as possible. But we're not getting a > consistent view anyway, it's just a matter of degree. Seems like a reasonably large difference in degree whether you have a snapshot collected in one loop, or you do things like spilling a tuplestore to disk in between. Greetings, Andres Freund