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 1wAMWw-002JD1-2F for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Apr 2026 06:36:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wAMWu-005O4f-0D for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Apr 2026 06:36:48 +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 1wAMWt-005O4R-1o for pgsql-hackers@lists.postgresql.org; Wed, 08 Apr 2026 06:36:48 +0000 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wAMWr-00000001ArW-3K5r for pgsql-hackers@lists.postgresql.org; Wed, 08 Apr 2026 06:36:46 +0000 Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-43d17bb1c65so3397293f8f.0 for ; Tue, 07 Apr 2026 23:36:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775630204; cv=none; d=google.com; s=arc-20240605; b=MGlqNNTxXmQBVgYtlTjTYsaLmdh4aZN3r7zFTVPrxmemccDXiBi0GCddgrXAEw73kv ymxdXVV3Nk/F++0/mBGd6oAKgmjtvfYhG4CYbYCoI905uePFxyakZ/Po29OjQwHBS4Xs WHPxqMnaTBKStjBlOJEd8jhL0eT1XiHJMKIQJcbmqgr8eTRFVB4qLt94JirPgJ+J0IZS 6v0/5pm9mUYFk/NoEkeVpIucZdajY42oLrAGVsjeL83utYlnXrsl5vq97MKsoaGwCgEt 6pqunojcoXe+Uxo5yQW7oDi9ZHWZEDuPAZwcG51HHGCdq3dQ3UC9JiYtarf+Ekau2kAc SWEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=RNnseVwg7tLgER1vH87NBrMauVVRdgmIdZ1gr62dqrg=; fh=WcKW3jr9WlGGxNYwdfXt3FxUqxrjckq2XMYF22ze3C8=; b=FCCw9eMVgb67tRvSSmzBiXZXOrU9SAzTKWsTnSaKjzLngaog/gIznMbRUVJbEbmOAM dS3GjAhcPVX84XJ6WpipnabcFbZL5LM3gZoIFPXcUy52jp/P+65O/V8WZrNH0yoDFEs1 h3sQvFLcv78kMa6JjoYE9DYemeJyXrJctO08t2FMr/xacCKhHE9L41934uTRZHk78SRs vYnJs6yRZLm9pz6WtjuO+2aGLJG59PIsVPf/iU5lvRMGfsXE+0FiJFpTjNvPP2aiaivD UBxULPLKard88nsh11qllJy4CzKDfu5jBhDZoUJjfffY/rZCVpndQFAAtZkKo8f8ryh/ rI1Q==; 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=20251104; t=1775630204; x=1776235004; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=RNnseVwg7tLgER1vH87NBrMauVVRdgmIdZ1gr62dqrg=; b=EL3mpM0zwQTN+M+x+9xxTAevurvoI0VBsrjpiBanaz1S6MlDSvAjo75R+bxDwAGFle EWlH2BWdtfpPEf9xIycEFCX0263YEfzQMlEg4ZCfVfcU7gT+sDchTEH/KXeqRnXVhlwm HQMsA2j4aTi26R81ZQN9qA+9ssZ2V8xXCrNgnZkAmOu1mEuvTnwR4nLecJRz/H0afvBe HeI0bjrTgkaRAC6b9dHt1DHnutZbDBWe6u97sMDWh39RJj/S0Rs9wEPptE7s774avH/J qM1OFgjy49aNSyVVA5inz3+Gwn4lEcNRIoCn3yu80y09q0U5Qf7KUdQ/9Qod6gCE1BmO GOVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775630204; x=1776235004; h=content-transfer-encoding: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=RNnseVwg7tLgER1vH87NBrMauVVRdgmIdZ1gr62dqrg=; b=oSd6Z06mk6p1pI2OpQZZoBEYWVs/hd7kvgo4bgSJxXXXBjBYBE/t0gSiJITPoUEI03 YWmcPRNEF++5+zvcKOSwJDSd3eNLzYPKduvyglvh0AzYtng9NtznDewSGl7R/hZsokeS b3RvDmJI4LyOs8JonSxJ3sC1ygaBSqxcqAKblGQdeZ0Anx0hSg5sObiVQma4N5XT/sEu TadC9+R+Y4bi7uekLua0pZPlJFygWPpXeeHb+fx4ENLgNciNPHb5KOA6rV6kXO40H5IP CIpCl58xXkgfQjKQOipx55GUjDy/vxnJPYwEe9hGGGfeBxSil7g2pNDUn1YGtEuIE6oa HA7g== X-Forwarded-Encrypted: i=1; AJvYcCXObY5xzNfHqd5gH1Jy5eFLYzikeYDrViswvy8bIuW1Z4+Q07I7/3JYtGEX46WJkn+13mQkYS3D6TgcacWO@lists.postgresql.org X-Gm-Message-State: AOJu0YwN5vTIApFVIDPlANddNpBNyHIpfjH1ujMKq9Ss988qJX8ym1x6 eVF3PxhGdx0us7ZIrbVpp8Qkd5RNX7yBVzHjZloGzUAQ4aln4fhFtftOOSckepRlzFPyW9PVTWE JP+i5mmuuU1+2s1+ZMPwRFeiLoCeVJeA= X-Gm-Gg: AeBDiesB68pGmwWMJ4mMtPGYfajCqZFPBxJWL1QUSJ4iur4P6TfUmOkVeGg3mtbysF2 ZgFE5dAdN6HVwFoDXN2X50dX8TXSs4UwMrCYnpR3N/gSC5WbeN2EYS5ChdrEjOyJC6s0I/iXU3J c3O1qQwhfc5rP140MRG2JDP+aGTp4uIF9i2ZOcs1kK6zXii08BIMlJDv4XSuuzEYgGENSmhtt2Q Ft+kPrpHiI7SrlgEresHAk2YH6Yxwn5aAFbcZo+I6FQ25U4MYcxDPBhlbTZK8EffHq9VhIvtylz l34vRgDvDCeaesyQPRRDOmNXS8asazbDL42aClB/l3anba6xwkkzbg== X-Received: by 2002:a05:6000:2586:b0:43d:21a:9a3e with SMTP id ffacd0b85a97d-43d292e168emr27209217f8f.32.1775630204276; Tue, 07 Apr 2026 23:36:44 -0700 (PDT) MIME-Version: 1.0 References: <670b867e-6518-48b0-b7c2-bd5f10ab58c6@vondra.me> <7ab3914f-da59-4c1f-b809-225637b586e8@iki.fi> In-Reply-To: From: Ashutosh Bapat Date: Wed, 8 Apr 2026 12:06:32 +0530 X-Gm-Features: AQROBzAhEdStwztXRR0I9J36wU58it4InHd28UQpzG4_6yP4ozw2BEYYGyjglxg Message-ID: Subject: Re: pg_buffercache: Add per-relation summary stats To: Heikki Linnakangas Cc: Andres Freund , Tomas Vondra , chaturvedipalak1911@gmail.com, Masahiko Sawada , Lukas Fittl , PostgreSQL Hackers , Paul A Jungwirth , Khoa Nguyen Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Apr 7, 2026 at 9:25=E2=80=AFPM Heikki Linnakangas = wrote: > > On 07/04/2026 16:47, Andres Freund wrote: > > 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_putval= ues() > > forms a minimal tuple, and the cost to form a minimal tuple and a heap = tuple > > aren't meaningfully different. > > Yeah, I wasn't fully convinced of that part either, which is why I left > it out of the commit message. I mostly wanted to get rid of the > double-buffering where we first accumulated all the data in an array. > Just because of the name of the function, I thought tuplestore_puttuple stores virtual tuple. But looking at the function it's clearly using minimal tuples. Sorry for my misunderstanding. > > 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]=3D# SELECT count(*) FROM generate_series(1, 100000= 0); > > =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=90 > > =E2=94=82 count =E2=94=82 > > =E2=94=9C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=A4 > > =E2=94=82 1000000 =E2=94=82 > > =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=98 > > (1 row) > > > > Time: 117.939 ms > > postgres[1520825][1]=3D# SELECT count(*) FROM (SELECT generate_series(1= , 1000000)); > > =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=90 > > =E2=94=82 count =E2=94=82 > > =E2=94=9C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=A4 > > =E2=94=82 1000000 =E2=94=82 > > =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=98 > > (1 row) > > > > Time: 58.914 ms > > Oh, to be honest I didn't remember that we *don't* materialize when it's > in the target list. > IIUC, query using SRF in targetlist runs faster because it does "not" use tuplestore. I consistently saw 10%-30% performance improvement after using tuplestore in pg_buffercache_pages(). Is that purely because of avoiding an in-memory array? -- Best Wishes, Ashutosh Bapat