pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Yushi Ogiwara <btogiwarayuushi@oss.nttdata.com>
To: Pgsql Hackers <pgsql-hackers@postgresql.org>
Subject: Fix for consume_xids advancing XIDs incorrectly
Date: Tue, 15 Oct 2024 10:51:33 +0900
Message-ID: <2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com> (raw)

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

Attachments:

skip_id_correctly.diff (text/x-diff, 909B) download From 59bc3f300b8447a407c8f90ebb542d1c0ef0e932 Mon Sep 17 00:00:00 2001 From: Yushi Ogiwara <yushiogiwara@keio.jp> Date: Fri, 11 Oct 2024 14:45:26 +0900 Subject: [PATCH] fix xid --- src/test/modules/xid_wraparound/xid_wraparound.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/modules/xid_wraparound/xid_wraparound.c b/src/test/modules/xid_wraparound/xid_wraparound.c index dce81c0c6d..c5f3b717cf 100644 --- a/src/test/modules/xid_wraparound/xid_wraparound.c +++ b/src/test/modules/xid_wraparound/xid_wraparound.c @@ -89,7 +89,11 @@ consume_xids_common(FullTransactionId untilxid, uint64 nxids) * the cache overflows, but beyond that, we don't keep track of the * consumed XIDs. */ - (void) GetTopTransactionId(); + if(!FullTransactionIdIsValid(GetTopFullTransactionIdIfAny())) + { + (void) GetTopTransactionId(); + consumed++; + } for (;;) { -- 2.40.1

view thread (19+ messages)  latest in thread

Message-ID: <2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com>
Permalink:  ../2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com/
Also on:    postgresql.org/message-id/2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: btogiwarayuushi@oss.nttdata.com
  Subject: Re: Fix for consume_xids advancing XIDs incorrectly
  In-Reply-To: <2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com>
  References: <2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox