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 1t0Wis-00Bta2-TR for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Oct 2024 01:51: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 1t0Wir-001qWh-1x for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Oct 2024 01:51:41 +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 1t0Wiq-001qWY-OG for pgsql-hackers@lists.postgresql.org; Tue, 15 Oct 2024 01:51:41 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t0Win-000vWx-UI for pgsql-hackers@postgresql.org; Tue, 15 Oct 2024 01:51:39 +0000 Received: from oss.nttdata.com (localhost [127.0.0.1]) by oss.nttdata.com (Postfix) with ESMTPA id A1C066182A for ; Tue, 15 Oct 2024 10:51:33 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at oss.nttdata.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=_4ae20bb181b10f2bf335ef8342937bae" Content-Transfer-Encoding: 7bit Date: Tue, 15 Oct 2024 10:51:33 +0900 From: Yushi Ogiwara To: Pgsql Hackers Subject: Fix for consume_xids advancing XIDs incorrectly User-Agent: Roundcube Webmail/1.4.11 Message-ID: <2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com> X-Sender: btogiwarayuushi@oss.nttdata.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=_4ae20bb181b10f2bf335ef8342937bae Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Hi, I found that the consume_xids function incorrectly advances XIDs as shown: postgres=# select txid_current(); txid_current -------------- 746 (1 row) postgres=# select consume_xids('100'); consume_xids -------------- 847 (1 row) In the example, the consume_xids function consumes 100 XIDs when XID = 746, so the desired outcome from consume_xids should be 746 + 100 = 846, which differs from the actual outcome, 847. Behavior inside a transaction block: postgres=# select txid_current(); txid_current -------------- 1410 (1 row) postgres=# begin; BEGIN postgres=*# select consume_xids('100'); consume_xids -------------- 1511 (1 row) postgres=*# select consume_xids('100'); consume_xids -------------- 1521 (1 row) Here, the first call inside the transaction block consumes 100+1 XIDs (incorrect), while the second call consumes exactly 100 XIDs (as expected) Summary: The function performs incorrectly when: - Outside of a transaction block - The first call inside a transaction block But works correctly when: - After the second call inside a transaction block The issue arises because consume_xids does not correctly count the consumed XIDs when it calls the GetTopTransactionId function, which allocates a new XID when none has been assigned. With the attached patch, the consume_xids works as expected, both inside and outside of transaction blocks as shown below: postgres=# select txid_current(); txid_current -------------- 1546 (1 row) postgres=# select consume_xids('100'); consume_xids -------------- 1646 (1 row) postgres=# begin; BEGIN postgres=*# select consume_xids('100'); consume_xids -------------- 1746 (1 row) postgres=*# select consume_xids('100'); consume_xids -------------- 1846 (1 row) Regards, Yushi --=_4ae20bb181b10f2bf335ef8342937bae Content-Transfer-Encoding: base64 Content-Type: text/x-diff; name=skip_id_correctly.diff Content-Disposition: attachment; filename=skip_id_correctly.diff; size=909 RnJvbSA1OWJjM2YzMDBiODQ0N2E0MDdjOGY5MGViYjU0MmQxYzBlZjBlOTMyIE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBZdXNoaSBPZ2l3YXJhIDx5dXNoaW9naXdhcmFAa2Vpby5qcD4K RGF0ZTogRnJpLCAxMSBPY3QgMjAyNCAxNDo0NToyNiArMDkwMApTdWJqZWN0OiBbUEFUQ0hdI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