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.98.2) (envelope-from ) id 1xOsf2-00000000Nef-37ih for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Nov 2026 08:17:30 +0000 Received: from localhost ([127.0.0.1]:41480 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xOsf2-0000000BIMr-46Ba for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Nov 2026 08:17:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:49800) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xOsf2-0000000BIMM-2BCp for pgsql-hackers@lists.postgresql.org; Thu, 05 Nov 2026 08:17:28 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1xOsez-00000000KRj-062w for pgsql-hackers@lists.postgresql.org; Thu, 05 Nov 2026 08:17:27 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-491f34b4e0fso35357f8f.2 for ; Thu, 05 Nov 2026 00:17:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793866645; x=1794471445; darn=lists.postgresql.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=FeibliOA0HuQ5XQ9QzBAoF7TtFvKKNe6Cq/zUk0fAQo=; b=EHI+8souJl9+H1mlSBAmGzw7e/DpputcgNlW/03TCbPcKeZe1R+Bg76CRHLjyEv7fT YvHlYctNht77RTABrEVm4tLw0JnXoxAx0Ei+1Azsr5ampZU0ObVUA0+QvlKTL+b2BBxX MNO8PxRw/++OG/o3hakdKuVe5Qm6tXM25WynODd5lz5WAsifqY9FIAuatclKnapQ4V2e SCGIffTzizOBWoI85wDtIqJHXJhf5BIbjUwOeCMdiPuLiTbzBxH3yyRGYEMGRe8cCvNH eBzoXLmgdtXM1tr2MWLsBqwoRSVyLiRhOIYwZczAVu6ZHV8vBmtGUBk1kBB+lwMoRvWp 91hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793866645; x=1794471445; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=FeibliOA0HuQ5XQ9QzBAoF7TtFvKKNe6Cq/zUk0fAQo=; b=ir0rdUvWkLd5ABciqspSfKhtEC0KkAEzr3FOu5xQsd7/r571F1cUjNadNjIMH5k10t Cit6YzJXxwvZY9T2isUSeMsnDRQ3JbSGfwvLQGeJbLx1pTnJmEmG/Yr7kQyLuJ573qwx xCvUcaginTJX32pHpwa+hCWAEQi5rcdBjLbquMddXAed9ECH99HQp1p08QXoT0qZVXw9 3NNUCdH9lcW6uVM+BMNQEsjlmSyROQXO4Qn4jG33vv5dg8BaAdvS/GAiCb8exn50wEkz XLHGLcZlh5zPU4oc1zL9lvAuspe/Nbm644iozIjriBNQ4k1eXCdIDoQ0dQDDErHSdCF3 Mmqg== X-Forwarded-Encrypted: i=1; AKwUvBx5QUmOab0bkfPcupGHKUKDYnmcXg25XxbZ7uli6QBA39of9hazlGhpIPt2UUwcuEoNtRcGSBVvA9PpKfkM@lists.postgresql.org X-Gm-Message-State: APuU4Z+xpJljoh8VtszOqv2yEttqvHHYrPtvPK2rBtTbjIS0GbMvTInD kJhEGrdQnunnpu314Zy0jzbs+AbGWwDGZozSWAEUY77Wt9IPXFEHQbkw X-Gm-Gg: AYBFou17MW+/IZi17/ZO0h5uYNKajZdiTBYxgOS9KsemzuOsXj5KUSH6hkRUvf4EnQY c6A6JPhI255iSWEjwCeEuvy92aYdbDcQ89hgtQ1TTd62bTKNVn8wMIl9YEX5XetPrPCgI6XIZWH u4Qc0He8pUIvI5JiUdONmRr2NaY7zbgW4h356XUFli5I9n0mpGu17Y3n6Pg3kBu4GTzH2NWixEP JmrSQtgmwL5gqJJe++Ibmnu86d6CRzzBTh+eijMiaIdyv0FxzQP6mW3FhgPw1hNdVjMgha6AiJt Za0aJa6PU6uSeiY8mIOvKSI4+ORNWLNMqeqdBvP/UKTUXpVOrZNoDiHgM8mFMUhtVanRKF3MMJz 1Bz5g/s8ZvtqFQfn9vPtUCbaeTy/K/I27m6C5kiHyFNq+7gUqy+1OijffW1VoaoVDywMu4d6C6c c4zFhTTb0qQcg6tYmTne5e4CfPIl4TODNYDRiSuEOmQ8XpQQ9IJwUjEEDYJDz/e8S6C5xvHRoGt 2UkBVBzuBnLKxRaPDzdRb1lMOYGs/NWHxj2K8BSueaSBmzJi0R4hVt6wGGN/RHjKQaKe2BTSp7J jZwWHmxWciU69mmYfeP6xng73GO6C3a4qAPsLyrS9ATQO13LP1SFOJZHsfQRapM+8zqa7lBJwBL qbl23CBkq97aQ X-Received: by 2002:a05:600c:8b06:b0:4a8:9a2f:d27 with SMTP id 5b1f17b1804b1-4ac17b2c907mr393522395e9.25.1793866644392; Thu, 05 Nov 2026 00:17:24 -0800 (PST) Received: from bdtpg (ec2-15-237-197-144.eu-west-3.compute.amazonaws.com. [15.237.197.144]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4b572505e84sm43680945e9.6.2026.11.05.00.17.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 00:17:23 -0800 (PST) Date: Thu, 5 Nov 2026 08:17:22 +0000 From: Bertrand Drouvot To: Michael Paquier Cc: Andres Freund , pgsql-hackers@lists.postgresql.org, Sami Imseih Subject: Re: Redesign per-backend statistics Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Thu, Nov 05, 2026 at 02:19:41PM +0900, Michael Paquier wrote: > On Mon, Oct 26, 2026 at 11:54:35AM +0000, Bertrand Drouvot wrote: > > I had a chat with Andres about this thread at PGConf.EU last week, and I agree > > that a fixed array is better than dshash. One reason is that dshash can also > > consume a non negligible amount of memory. > > I have some comments, spotted while reading through the patch. Thanks for looking at it! > In the new tests of 0001, multiple queries like these ones are > duplicated, with the only difference being a different alias. I get > that you are doing it to have different steps depending on the values > of the GUC, but the dups are not cool. I'd suggest to reduce that to > single queries, perhaps combined with a SHOW or a join on pg_settings > that show the value of stats_fetch_consistency. > > +session s2 > +step s2_generate { > + INSERT INTO stats_per_backend_data VALUES (1); > + SELECT pg_stat_force_next_flush(); > +} > +step s2_generate_more { > + INSERT INTO stats_per_backend_data VALUES (2); > + SELECT pg_stat_force_next_flush(); > +} > Or that could be one step, repeated twice? Agreed, will do. > +# Verify that a statistics snapshot excludes backends that started after the > +# snapshot was created. > +my $snapshot_session = $node->background_psql($connect_db); > > Hmm. Not sure that this one is worth the cycles spent for it. This one covers a real bug that I hit while working on the re-design, so it feel safer for me to keep it. That said if you feel strongly about it we could get rid of it. > + * - pgstat_per_backend.c - generic per-backend statistics infrastructure > > Could there be a better name for this file at the end? Or just > replace the contents of pgstat_backend.c that the last patch finishes > by removing? Not a big deal, but using "per" sounds a bit weird. Purpose was to ease to review. What about renaming pgstat_per_backend.c to pgstat_backend.c in the last patch? > + if (kind_info->per_backend_entry_size != 0) > + ereport(ERROR, > + (errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind), > + errdetail("Per-backend statistics are not supported for custom cumulative statistics."))); > + > > Blocking the registration sounds fine by me if you do not wish to > enable this part of the facility for custom stats. Now, question: > what would that take? I had a quick look at that and it seems doable. That said, custom statistics have no per-backend support today, so enabling it would be a new feature. Also the current design is a blocker for adding new per-backend statistics [1], so I think it would be better handled as a follow-up once this series is committed. What do you think? > + /* > + * Allocate and initialize arrays for built-in fixed-numbered kinds that > + * keep per-backend statistics. > + */ > + memset(ctl->per_backend_arrays, 0, sizeof(ctl->per_backend_arrays)); > + > + for (PgStat_Kind kind = PGSTAT_KIND_BUILTIN_MIN; kind <= PGSTAT_KIND_BUILTIN_MAX; kind++) > + { > + const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind); > + Size array_size; > + > + if (!kind_info || kind_info->per_backend_entry_size == 0) > + continue; > + > + array_size = mul_size(NumPerBackendStatSlots, kind_info->per_backend_entry_size); > [...] > + p += MAXALIGN(array_size); > [...] > + ctl->per_backend_arrays[kind] = p; > [...] > + /* > + * Per-backend arrays for built-in fixed-numbered statistics kinds. > + * Entries are indexed by ProcNumber. > + */ > + void *per_backend_arrays[PGSTAT_KIND_BUILTIN_SIZE]; > > Is that actually right? I think so because per_backend_entry_size is set to sizeof() of the corresponding entry type, so successive entries already have the required alignment. > Wondering if you are not missing at least one > MAXALIGN() here.. That said, using MAXALIGN(per_backend_entry_size) does not hurt and makes the layout more explicit, so I'll do that. > You may want to document a bit better the > structures of these arrays, I guess? At least you have one pointer > saved for each stats kind, each with a series of data worth what's > been registered in shmem. > > /* Transfer all live per-backend stats before writing the stats file */ > pgstat_wal_acc_all_backends(); > pgstat_lock_acc_all_backends(); > + pgstat_io_acc_all_backends(); > > Seems to me that these things should be loops over the stats kind IDs > instead of direct callback calls. Makes, sense will do. > + return array + procnum * kind_info->per_backend_entry_size; > > An index by procnum may feel slightly cleaner with less math. These > are numbered starting from 0, aren't they? Yeah, they start at 0. The byte offset calculation is needed though, as the entry size is kind specific, so there is no concrete array type to index here. I'll add a comment to clarify that. Regards, [1]: https://postgr.es/m/et272fdhdx6yphlgzvrgsf7bgwnf3vqciwp4gxqubro42uaflp%40ohslaocvwgvi -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com