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.94.2) (envelope-from ) id 1vHKe2-0041wU-2X for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Nov 2025 11:28:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vHKe0-00Dq3w-KT for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Nov 2025 11:28:40 +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.94.2) (envelope-from ) id 1vHKdz-00Dq3m-OT for pgsql-hackers@lists.postgresql.org; Fri, 07 Nov 2025 11:28:40 +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.96) (envelope-from ) id 1vHKdt-005taZ-1f for pgsql-hackers@lists.postgresql.org; Fri, 07 Nov 2025 11:28:38 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-429eb7fafc7so480425f8f.2 for ; Fri, 07 Nov 2025 03:28:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762514912; x=1763119712; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=n5TuHFAqdW9Of6We/4JJs09XVcls0llbbeQBxsu2vx0=; b=fyaKnAGwwytOcTTIhPxO/INk5zrLQPNd01ukBJoKnMz4rKX7iBFUD1+TSJmim8WmQI ghT7wDdYIbCkgTIx+iQ6iKEk03fy7ZkFdPMMqCOFWyBkxqhYAxWSj/g4ST7AubtGMySR Esqzn+PlB6LuMEe4CJt/S4q7wJCTIG5WJu82aKvZO8m5/o6HytoAKbo1mySwn6orIat2 zilLJ582RKam/qmbm/Nqdlj+LIYzbujaIzZbVG6hjWOIg85KRCXkm90h1D/38huws7Hs 66Jl3dIHjJPCQ6LhPdMkpoGYarFtWL8XK3OEqNVQBfDdpPbx+JcwC1gG+CoGzZAH38Qe ub5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762514912; x=1763119712; h=in-reply-to:content-disposition: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; bh=n5TuHFAqdW9Of6We/4JJs09XVcls0llbbeQBxsu2vx0=; b=Egw9Jke7hKgq+bYOv8zw9bVMbji7uBlKsmmLpF+fampHc89JLBJYwBXQcSfVsgb+4X 70cyHBq6z8u3l/vj+5q5tfaoYZgPClzIWdcCouLrL2XD9rxKVyKW1ruxAf6JBcnXjFpq kR9aOxTZX8BgJMy+Mc4KG8EZdVgb48f1zW6SWWWJgUv9DueYEcENq9NAo59a2xmJf6dK VM00IflvaAXVYawaapukFAwFM6GBw2IGOAyoLhzmTaYI8r5RLk9dohtwcHoLir3UImY0 tZc7OlU8e5EJ2T7FtG53t4l13nLf6ldK1pb2/6qPRIILP/1XAle6EOQoWVS+hQFJtPsE aXGw== X-Forwarded-Encrypted: i=1; AJvYcCVB+uiELXnZ1Q8ScWI2WmGtz20XxDzJYAmgH9EBNKEcXL70T1PyHC9h1MJMVj0B864+fvt9Xml07xk2deVe@lists.postgresql.org X-Gm-Message-State: AOJu0YzEYGsxRcCpMPGRKcA8zSvfK9m5diYzydkG9cp/sEzUzw08hGlN N8BipAkNtE0s4QNaHVrh02YhAzmjRTKkmpvzzJqBg8FpN4BRliqVAX0D X-Gm-Gg: ASbGncvi955ZgNmY0nzcjRcI5THHCgP5GxErwcb55MjhgYig2pSJNw1AJWb7MLfZ97i OrS0n641+kAIKMp4LXfrnXAF9KwFdr9hbFwN7eOrUDZyJQhu9GooGarVJEP69gos1NIGzb1yrIE K2cpxOvkxnaB0y7MHJEqixOvo5z3bCLVYs1vmNJDzjIo4fGzcftNvrGrzNl23rN4whhgfCrdgDk bZGL7TNCOYPV2CL+UpDjUbwX3Fx0lK6A5Iz1ipiT7TFc0FzlodjCjoJjbLwv8dUJSUiTkKMO613 81ppxuuyLdO23vsntxoV7oAvUTU2ebc30ud/3JAy/uLByCUJZcycFyqQkoXoeAcQeuWhnoifkw5 MFJJPAW9sHVP9BR1H8s1Pu9kUMqKiGjBL39hCSQFQM50j02YYVKQZlOlyWY4CWyuj2H56Cfw2GI k1xOlskIVgS0RVteYrcYYNu9XWQXbVqkn2pDJ00ewDdloRU3WGChZ3uIsKHVlUeQaK9L25e/wTC XE8Z56tDQO4fY9+HYI1NRSX23blN912LV2+HXOjJcqHrw== X-Google-Smtp-Source: AGHT+IEdC/ETOXUPzg36T73QuJNsKGVPtc+k+PdaCVXZZdeDObMBKuA2dcxVdSzi1hEYlSKwuRvtIg== X-Received: by 2002:a05:6000:22ca:b0:429:b7cd:47ff with SMTP id ffacd0b85a97d-42ae5afbcc8mr2230346f8f.40.1762514911718; Fri, 07 Nov 2025 03:28:31 -0800 (PST) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-237-181-182.eu-west-3.compute.amazonaws.com. [15.237.181.182]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42abe62b28asm4778020f8f.6.2025.11.07.03.28.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Nov 2025 03:28:30 -0800 (PST) Date: Fri, 7 Nov 2025 11:28:27 +0000 From: Bertrand Drouvot To: Michael Paquier Cc: Kirill Reshke , Robert Haas , pgsql-hackers@lists.postgresql.org Subject: Re: relfilenode statistics Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="KMYjwlsACqdYx2P8" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --KMYjwlsACqdYx2P8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On Thu, Oct 02, 2025 at 10:23:11AM +0900, Michael Paquier wrote: > On Wed, Oct 01, 2025 at 02:33:11PM +0000, Bertrand Drouvot wrote: > > I think the first step is to replace (i.e get rid) PGSTAT_KIND_RELATION by a brand > > new PGSTAT_KIND_RELFILENODE and move all the existing stats that are currently > > under the PGSTAT_KIND_RELATION to this new PGSTAT_KIND_RELFILENODE. > > Likely so, yes. PFA the new implementation. It does not introduce a new PGSTAT_KIND_RELFILENODE, instead it keys the PGSTAT_KIND_RELATION by relfile locator. We may want to rename PGSTAT_KIND_RELATION to PGSTAT_KIND_RELFILENODE as a next step. The patch is structured that way: ==== 0001 Add stats tests related to rewrite While there are existing rewrite tests, the stats behavior during rewrites doesn't have a good coverage. This patch adds some tests to record some stats after different rewrite scenarios. That way, we'll be able to test that the stats are still the ones we expect after rewrites. Note that it generates a new stats_1.out (which is quite large), so we may want to move those new tests to "isolation" instead. ==== 0002 Key PGSTAT_KIND_RELATION by relfile locator This patch changes the key used for the PGSTAT_KIND_RELATION statistic kind. Instead of the relation oid, it now relies on: - dboid (linked to RelFileLocator's dbOid) - objoid which is the result of a new macro (namely RelFileLocatorToPgStatObjid()) that computes an objoid based on the RelFileLocator's spcOid and the RelFileLocator's relNumber. That will allow us to add new stats (add writes counters) and ensure that some counters (n_dead_tup and friends) are replicated. The patch introduces pgstat_reloid_to_relfilelocator() to 1) avoid calling RelationIdGetRelation() to get the relfilelocator based on the relation oid and 2) handle the partitioned table case. Please note that: - when running pg_stat_have_stats('relation',...) we now need to be connected to the database that hosts the relation. As pg_stat_have_stats() is not documented publicly, then the changes done in 029_stats_restart.pl look enough. - this patch does not handle rewrites so some tests are failing. It's only intent is to ease the review and should not be pushed without being merged with the following patch that handles the rewrites. - it can be used to test that stats are incremented correctly and that we're able to retrieve them as long as rewrites are not involved. ==== 0003 handle relation statistics correctly during rewrites Now that PGSTAT_KIND_RELATION is keyed by refilenode, we need to handle rewrites. To do so, this patch: - Adds PgStat_PendingRewrite, a new struct to track rewrite operations within a transaction, storing the old locator, new locator, and original locator (for rewrite chains). This allows stats to be copied from the original location to the final location at commit time. - Adds a new function, pgstat_mark_rewrite(), called when a table rewrite begins. It records the rewrite operation in a local list and detects rewrite chains by checking if the old_locator matches any existing new_locator, preserving the chain's original_locator. - Modifies pgstat_copy_relation_stats(), to accept RelFileLocators instead of Relations, with a new increment parameter to accumulate stats (needed for rewrite chains with DML between rewrites). - Ensures that AtEOXact_PgStat_Relations(), AtPrepare_PgStat_Relations(), pgstat_twophase_postcommit()/postabort() pgstat_drop_relation() handle the PgStat_PendingRewrite list correctly. Note that due to the new flush call in pgstat_twophase_postcommit() we can not call GetCurrentTransactionStopTimestamp() in pgstat_relation_flush_cb(). So, adding a check to handle this special case and call GetCurrentTimestamp() instead. Note that we'd call GetCurrentTimestamp() only if there is a rewrite, so that the GetCurrentTimestamp() extra cost should be negligible. Another solution could be to trigger the flush from FinishPreparedTransaction() but that's not worth the extra complexity. The new pending_rewrites list is traversed in multiple places. The overhead should be negligible in comparison to a rewrite and the list should not contain a lot of rewrites in practice. Another design that I tried was to copy the stats in pgstat_mark_rewrite() but that lead to difficulties during abort, subtransactions. It looks to me that the list approach proposed here makes more sense. We could also imagine adding a function similar to pg_stat_have_stats() that would take relfile locator as arguments. That could help validate that after a rewrite the old stats are gone. > Do you think it is OK to define non-transactional pending stats as > being always a subset of the transactional stats? I don't quite see > if there would be a case to have stats that are only flushed in a > non-transactional path, while being discarded at the stats report done > at transaction commit time. This means that it may be possible to > structure things so as the pending non-transaction stats structure are > always part of the transactional bits, and that the other way around > is not possible. Perhaps that influences the design choices, at least > a bit. The proposed patch does not change anything it that regard. It keeps the relation's behavior as it is. This patch just ensure that a relation rewrite keeps its stats. Adding new stats (add writes counters) and ensure that some counters (n_dead_tup and friends) are replicated will be done once this one gets in. Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com --KMYjwlsACqdYx2P8 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v7-0001-Add-stats-tests-related-to-rewrite.patch"