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 1ssXL2-0094SZ-JQ for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Sep 2024 00:54:05 +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 1ssXL1-00FMKH-Ke for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Sep 2024 00:54:03 +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 1ssXL0-00FMIt-OR for pgsql-hackers@lists.postgresql.org; Mon, 23 Sep 2024 00:54:03 +0000 Received: from m16.mail.163.com ([117.135.210.3]) by makus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ssXKu-000Yq7-3t for pgsql-hackers@lists.postgresql.org; Mon, 23 Sep 2024 00:54:00 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=ESEEKDPxrDyHR+kXhqMEmSU93iamMEC36vhUkfCrgOs=; b=ksVCRsk4/mCT9Le7pvivdJ0//QWFjluE16rul7bSEvngw1BnrsbgNsMq8GBu4j lXheWKn3GLwcEtnyS8vwY4SUjmtnTqSgKoedK+wK8d52NvzPoDxaxvxrSjh8ApQt szEuNYe2ZUs3Xi/fpa8P9fWxDEZlpGJDSSlq1mk+pF0ds= Received: from lovely-coding (unknown [101.227.46.166]) by gzga-smtp-mta-g2-5 (Coremail) with SMTP id _____wDnbwAEvPBmPujEAQ--.44622S3; Mon, 23 Sep 2024 08:53:25 +0800 (CST) From: Andy Fan To: Andres Freund Cc: Pg Hackers Subject: Re: FullTransactionIdAdvance question In-Reply-To: (Andres Freund's message of "Fri, 20 Sep 2024 14:17:05 -0400") References: <87frpu4qcf.fsf@163.com> Date: Mon, 23 Sep 2024 08:53:24 +0800 Message-ID: <87v7yn8a2j.fsf@163.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-CM-TRANSID:_____wDnbwAEvPBmPujEAQ--.44622S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Kw1kWr1rKF4Utw13Jw1fZwb_yoW8Zw4UpF Z0ka15tr1ktFyUAr1Iqr4xXFySvrn3tFy5AFySgFZ7Aa15Ww1vkFZ7K3yYk3ZrArZY9w1j qF4xuw1UCa1kZa7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zMOJY9UUUUU= X-Originating-IP: [101.227.46.166] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/xtbBZwpjU2V4JVbp8wAAsp List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain Hi Andres: > On 2024-09-20 17:38:40 +0800, Andy Fan wrote: >> static inline void >> FullTransactionIdAdvance(FullTransactionId *dest) >> { .. >> } >> >> I understand this functiona as: 'dest->value++' increases the epoch when >> necessary and we don't want use the TransactionId which is smaller than >> FirstNormalTransactionId. But what is the point of the below code: >> >> /* see FullTransactionIdAdvance() */ >> if (FullTransactionIdPrecedes(*dest, FirstNormalFullTransactionId)) >> return; >> >> It looks to me it will be never true(I added a 'Assert(false);' above >> the return, make check-world pass). > > Hm. I think in the past we did have some code that could end up calling > FullTransactionIdAdvance() on special xids for some reason, IIRC it was > related to BootstrapTransactionId. Turning those into a normal xid doesn't > seem quite right, I guess and could hide bugs. > > But I'm not sure it'd not better to simply assert out in those cases. Per my current understanding, special XIDs are special and better be used explicitly (vs advance special XID-1 to special XID-2)? Currently if the input is BootstrapTransactionId, then we would get FrozenTransactionId, which I'm still can't understand a reason of it. I checkout the code to the commit where FullTransactionIdAdvance was introduced, and then add the "Assert(false)". make clean .. make check-world still passed. >> IIUC, should we remove it to save a prediction on each GetNewTransactionId >> call? > > I could see adding an unlikely() to make sure the compiler orders the code to > make it statically predictable. Thanks, Attached is the version to add the 'unlikely' while I'm still searching the possibility to remove the code. I will defer to you after my understanding is expressed. Actually I'm more fan on the knowledge about the XIDs which I am not aware of. I think either the fix of 'prediction miss' or removing the code probably not make any noticeable improvements in this case. So thanks for checking this! -- Best Regards Andy Fan --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v20240923-0001-Add-unlikely-to-FullTransactionIdAdvanc-ch.patch From 92e0d65605b185588b57415a640187b4ccc2ae74 Mon Sep 17 00:00:00 2001 From: Andy Fan Date: Mon, 23 Sep 2024 08:42:31 +0800 Subject: [PATCH v20240923 1/1] Add unlikely to FullTransactionIdAdvanc check. The 'FullTransactionIdPrecedes(*dest, FirstNormalFullTransactionId)' are unlikely to be true, so add a 'unlikely' to make the code statically predictable. --- src/include/access/transam.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/access/transam.h b/src/include/access/transam.h index 28a2d287fd..8a0a67d4a4 100644 --- a/src/include/access/transam.h +++ b/src/include/access/transam.h @@ -130,7 +130,7 @@ FullTransactionIdAdvance(FullTransactionId *dest) dest->value++; /* see FullTransactionIdAdvance() */ - if (FullTransactionIdPrecedes(*dest, FirstNormalFullTransactionId)) + if (unlikely(FullTransactionIdPrecedes(*dest, FirstNormalFullTransactionId))) return; while (XidFromFullTransactionId(*dest) < FirstNormalTransactionId) -- 2.45.1 --=-=-=--