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 1waDRR-001c0U-2y for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Jun 2026 14:10:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1waDRQ-00BkpC-2p for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Jun 2026 14:10:00 +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 1waDRQ-00Bkp4-1p for pgsql-hackers@lists.postgresql.org; Thu, 18 Jun 2026 14:10:00 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1waDRO-000000016Tx-1L7L for pgsql-hackers@lists.postgresql.org; Thu, 18 Jun 2026 14:10:00 +0000 Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-68f36e1663cso1676522a12.3 for ; Thu, 18 Jun 2026 07:09:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1781791797; cv=none; d=google.com; s=arc-20240605; b=ZJ/81y9yydQVfL/qx4LFl2PGqUGTqrNNEkpQlVyEn/P+IYT5ltyLDVxfpjtJlDAzkt 5Tqe7DKNvsQ5T9Z7pVAsh3Rvsrnn0kpZf9l9+hCotYCBOeFHWV5+Qdo8QPUqj9btJc0B xm6BpoChCQSWpX3pdfHaccL1O9cKwsKxCvDewg1QHrX2m44OJ4u3TVIP6cGqwa9+hDk3 aLcDo0x+jwGyczWzJOveo1h9ZbUTLRynb4uHHLCf6oap+ZpbWpXZ/Npr37b5IYYTi4IP cgswQK4/OcMBuLzsKMrYsPI9mtSTmil6GeXZUiN6fHBtBm9upeDcstWCq1EmaYuEdg0i Vr0Q== 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=ZdzXBBGWmfUVeevtgqOCVv6/zCDcFVc88fDr6BgRh60=; fh=RizNOyAn47oOb3upO0BX6RzoEwTuM8oX2m9sPk+7NCA=; b=aJR3cmupQuDKCmCnN6Ofy2lsCw8VJ+gfp1RsqlgkHBCXTWPtH5gr6dIQgtbwLAzAbe sdGbrtJ1BM+GaQzmPq9fsNQ2HljDcE+Bt0IzftePhSmklMOezkFXEsOrzV3oLjvtjPiP fnN9GPJnliK6dJRduTVGoE2FczY+Q97iSD2+aMovDqYw+gmJESr9RG/FcIF4AiRsfDhU aX7Nou+Ov+9VdcYk8FCsvBCQPsH7uvVvWveHAyOxEKntk6OHeXwPit8pIezR/A8pcsGM NbH/mYyZHylqq8vE+eKz8pVH4Ox+cB0SATt7SPzLm3VCQhN1qnCk5WlR0nWBkOyZYO/D NwRw==; 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=1781791797; x=1782396597; 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=ZdzXBBGWmfUVeevtgqOCVv6/zCDcFVc88fDr6BgRh60=; b=ABRwOXda7j1TJPeRRXhkUIg6vaxmvGta/qQqE4YltUm6QIV2H8n9eQ0jnrvcVS/L31 stuBpFei6Z/If5CBxERx1xRhYXdOb6AfzpSreJj/AsaUVtPRVM4Ik2Hx24J4k97cB35V spGjHWXGY1a5haFQ8wkwQdc6pWaZWRwqgOsvsTewCbamlk0jQlsaB95ZAYwAYk1uqk1L 8mN3WFgc1LopJKj90s6c6jKbmfTaUIP8V8lskrh91DIRgyj7ZRcd2qM1zahVB+v4oKsY kLj8npTE8qpWFysE/Nx4mh6xlc/nO+j9aIDRWkLbp7yuGcs5hU1UcTYcYHD81ARbfDj4 l5mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781791797; x=1782396597; 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=ZdzXBBGWmfUVeevtgqOCVv6/zCDcFVc88fDr6BgRh60=; b=P8njIcExLs07P9dkTeUtzptETgzpg3yZ8pJQta/zxtq3e+y0UcSt/nm9ZFb5jv1/Ma /dbamFoa4oQVCDtfjua54ZM7huOt0zQZrXhctrHBAvE5Sm37qZLBR4NvJMQqchCXYucq ycfT6JN3i07hEWOKggGi3m8TgA1pz6+rwjBfVUmdi1L0Eb+hdu06c5WBWMRj7X3Vc5Bm 48OHkw5xjTfsovxCesPachGuQxbywV3+8eZkZzFuA3g8FZHe7VHmfjTyviC6aRGuzMv/ 4KEK8TZdJnYxcTZZc2JeyI/9wjGCdtDya6lyHFNmvviQPJeewIIyGnWWb4vMl7K7VKF8 rNvg== X-Forwarded-Encrypted: i=1; AFNElJ/IeLqPXXtamz93Ffo0xT3HWTXAV2R0NK3yD1A+Bt1km19aiEfZZZtLTgFCB2L+iNRCi9ik20NjLhNxH0Ki@lists.postgresql.org X-Gm-Message-State: AOJu0YxA0hgFTIm5iQVDCUw4RDtvxg3d8vx7ddMl3nnWAZpHbQLGGedK MYiIRXVXM7QGYTGb+Oi11ZCRonfrsgY3vff252tTAGeo8+dRDlDZ4ZYJzQkGN+irKBUTe7Ohomk SHB6pdbNUeLb5oTL1Cemfr02SVrGAafY= X-Gm-Gg: AfdE7ckf5XLFWyvbmy9Qw/U9zvDttEX6Lze5wOiQ3WSu8+UfyqQ/a9zd3OAzoYuU/6H Hdj0cUl1qRTHcXut7cmVMz/gIKgQgLNjgIkRkH2I9pLGZW7Zpqu4chixiSgLc06m2yu9UJCO/sY QNOUA5qg7zp5Kufr7eZK3YcameWccZjY1ird+erjHLiBSzSwo81fKgFIBu5uadaDx8nynDX70d0 iQb7M5iFjVY3XjSCBdIKnjWf0AP5HFxlWw32xVEwUsMoE7Ov6LCjvqCUAOtrDRaRQMb2TwbU2sA N+yxQFsT4u5ugohuQDwIIWwEwJBnsErqiOTEkZ4= X-Received: by 2002:a17:906:794a:b0:be2:cf3b:acb0 with SMTP id a640c23a62f3a-c05a283daa9mr587532666b.12.1781791796222; Thu, 18 Jun 2026 07:09:56 -0700 (PDT) MIME-Version: 1.0 References: <670b867e-6518-48b0-b7c2-bd5f10ab58c6@vondra.me> In-Reply-To: <670b867e-6518-48b0-b7c2-bd5f10ab58c6@vondra.me> From: Robert Haas Date: Thu, 18 Jun 2026 10:09:43 -0400 X-Gm-Features: AVVi8CfYvXrUZ8UzYVKHOr291AlK10Oyzrb8LKaiFvDIhkB0kE4CUkMQ9F7j04k Message-ID: Subject: Re: pg_buffercache: Add per-relation summary stats To: Tomas Vondra Cc: Ashutosh Bapat , 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 Fri, Mar 27, 2026 at 6:59=E2=80=AFPM Tomas Vondra wrot= e: > The main argument here seems to be the performance, and the initial > message demonstrates a 10x speedup (2ms vs. 20ms) on a cluster with > 128MB shared buffers. Unless I misunderstood what config it uses. So, my opinion on this point is that the results Lukas shows later in the thread are compelling. The query takes 13s and writes 1.2GB for what should be a trivial monitoring query. That seems like it's pretty clearly enough overhead to be a problem for a monitoring query. The problem isn't even just that you can't afford to wait 13s for a query you run every 10m -- it's that the query itself is consuming enough system resources to skew your other monitoring. For example, if you're monitoring your system load average or CPU usage or disk usage over time, you're going to see spikes when this query runs. That's not the worst thing that has ever happened to anyone, but it's definitely not great, and I can totally understand someone not being willing to incur that much overhead. I don't believe we should accept the argument that this patch doesn't save enough to matter; I think it does. > Let's assume it's worth it. I wonder what similar summaries might be > interesting for users. I'd probably want to see a per-database summary, > especially on a shared / multi-tenant cluster. But AFAICS I can > calculate that from the pg_buffercache_relations() result, except that > I'll have to recalculate the usagecount. I think it would be better to return the total usagecount and let the caller divide if they want, rather the average. But more generally, I agree that we don't want something that is overly specific to one person's use case. Thinking about how to make a function like this useful to as many people as possible is a worthwhile activity. I don't know what more we can do that makes sense. For instance, we could add a database OID argument that can be NULL or the OID of a database and it filters out everything else. I'm not sure that would pull its weight -- the big gains are probably coming from doing the aggregation using bespoke code, rather than filtering out rows beforehand -- but maybe. On the whole, I'm inclined to think we should accept this. There's plenty of cases where it won't save much, and it is also true that it would be nice to improve the core infrastructure so that queries like this can be better-optimized. But I don't think that's going to happen right away, and even when it does happen I bet the savings from a patch like this will still be pretty significant. I also believe that aggregating the pg_buffercache results by relation is probably a very common use case, so it's doesn't seem to me as though there would be ten other equally-compelling versions of this. > One thing we lose by doing ad hoc aggregation (instead of just relying > on the regular SQL aggregation operators) is lack of memory limit. > There's a simple in-memory hash table, no spilling to disk etc. The > simple pg_buffercache view does not have this issue, because the > tuplestore will spill to disk after hitting work_mem. Simplehash won't. > > The entries are ~48B, so there would need to be buffers for ~100k > (relfilenode,forknum) combinations to overflow 4MB. It's not very > common, but I've seen systems with more relations that this. Would be > good to show some numbers showing it's not an issue. This is a good point, but I'm not sure I believe there's a real issue here. It seems as though the kinds of systems where this function is likely to be important for performance are probably those with 100GB+ of shared_buffers, so 12m+ buffers, so ... half a gigabyte? Maybe somewhat more with memory allocation overheads and so forth? I feel like if you have 100GB of shared_buffers, you probably have work_mem set to 1GB+, or at least have that much memory free. And even then you only need that if every single shared buffer belongs to a different relation, which seems like a thing that will not occur in practice. Obviously, there are things we could do to limit memory consumption here. I think the easiest thing might be to just write out the entire hash table in hash value order to a temporary file every time we exhaust work_mem, and then do a merge pass over all those temporary files at the end. I don't think we can plausibly need more than one merge pass to keep memory usage within acceptable limits, and I don't think this would need to be a crazy amount of code. But I'm also not sure I believe we really need it. The concern about code maintenance that has been raised is valid here as it is for all patches, so we shouldn't bloat the patch with code that it doesn't really need, and I think it's worth considering whether spill-to-disk code falls into that category. --=20 Robert Haas EDB: http://www.enterprisedb.com