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 1t18J5-00Erwb-0F for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Oct 2024 17:59:35 +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 1t18J3-007HWD-9Y for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Oct 2024 17:59:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t18J2-007HVt-Vk for pgsql-hackers@lists.postgresql.org; Wed, 16 Oct 2024 17:59:33 +0000 Received: from mail-lf1-x136.google.com ([2a00:1450:4864:20::136]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t18J0-001OdN-SX for pgsql-hackers@postgresql.org; Wed, 16 Oct 2024 17:59:32 +0000 Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-539e7e73740so77993e87.3 for ; Wed, 16 Oct 2024 10:59:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729101569; x=1729706369; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=C3t98fPCJaVru8LXTelFsh5dtWK2HDMd3OO77YUTTYI=; b=jhNuzhnPdzX14LS3j83ReQtTceJPym4lY5YKlRkc8+kPtV2inA4c0f9ndgW7Ah7cnG EpmitCIUT0Qw6fWUuy5P55TeeOrg2hUs6iSGcRMEp5DK2TuzXHaEmtF0gyrd808bVbHh 3/llIDc1/Z9YNCcHB84AxXmEH1VKsJJomB2DjipmwIKgltYkHn3wGrrFnvuou7jYz2u0 jieIUV1X2BObUaHU76q4pKl5Ah+Rjm++nOYrQ7BviR0jCb+DxKi3KiC1UBZwtm9XAr8C XxTLfZsMr17seRt4j3aM18kAR8Co49SbCUABPUIOxa/V7pLqsY1v7UYsbVGGr7NIb6K8 geHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729101569; x=1729706369; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=C3t98fPCJaVru8LXTelFsh5dtWK2HDMd3OO77YUTTYI=; b=kqsTo57AVnHBnrEWvsllXwr1aJ0VeNEZJykCuq0NeKL2baXmiqkpXHMRDkq4wK20K6 pQxY45fQbVy+Ud8db3aDkiOxum+MUMD19iPkZkzAOAL7HSI2Cxo9Brb98nPcD8kHis2G +mv8TZbCOnJdd7qx0mPrNKY6Tk1QuG6e1zp89Gr6Axm2uDbGdbmZNH44vAdyudoZHS+p jCPKuK1yBwpyPzEXwPzQU8DwQEeUDSzgCOX1yUT6QRxAPzCsxaGwWtq+wKtGrn8O17+7 RqzOJ/o25zopxR7nzjv33qqYE3bGn56dBkTEDFAxbxTUOpYGBCrY3zWewrwjBkFkGFOe uJyQ== X-Gm-Message-State: AOJu0YzTRxOcsZbYa4Qvk8e/oz2dAQ6zmPclXDIxtURBp+14p9bXtB+x KfQ0PfG42F6yrJeo5nwuaDe5rN6kssUhPomzKryuWtEwnDaHvjZVjJ+PUO/Fb1LyiT0fxQS3SRa dGNFVx5SrRTeK+tv5FVm8s/nDXVG8AGbq X-Google-Smtp-Source: AGHT+IHh81uuf+hjufZEp3b5hy52Ir0Z96DRmT2h4otjJNK7QHlUTs8CLlgXoEc2NT34eMja2R0ovOv/RwAAYeCIayo= X-Received: by 2002:a05:6512:3ba6:b0:539:fdee:fe04 with SMTP id 2adb3069b0e04-539fdef1287mr4936785e87.11.1729101568632; Wed, 16 Oct 2024 10:59:28 -0700 (PDT) MIME-Version: 1.0 References: <2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com> <50ee82a96ac0935de88f9a5ef021f04d@oss.nttdata.com> In-Reply-To: <50ee82a96ac0935de88f9a5ef021f04d@oss.nttdata.com> From: Masahiko Sawada Date: Wed, 16 Oct 2024 10:58:50 -0700 Message-ID: Subject: Re: Fix for consume_xids advancing XIDs incorrectly To: Yushi Ogiwara Cc: Pgsql Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Oct 15, 2024 at 10:06=E2=80=AFPM Yushi Ogiwara wrote: > > Hi, > > Thank you for your comment. > > Regarding the first patch, I believe it works correctly when > consume_xids(1) is called. This is because the lastxid variable in the > consume_xids_common function is initialized as lastxid =3D > ReadNextFullTransactionId(), where the ReadNextFullTransactionId > function returns the (current XID) + 1. But it's possible that concurrent transactions consume XIDs in meanwhile, n= o? > > Separately, I found that consume_xids(0) does not behave as expected. > Below is an example: > > postgres=3D# select txid_current(); > txid_current > -------------- > 45496 > (1 row) > > postgres=3D# select consume_xids(0); > consume_xids > -------------- > 45497 > (1 row) > > postgres=3D# select consume_xids(0); > consume_xids > -------------- > 45497 > (1 row) > > In the example, the argument to consume_xids is 0, meaning it should not > consume any XIDs. However, the first invocation of consume_xids(0) looks > like unexpectedly consuming 1 XID though it's not consuming actually. > This happens because consume_xids(0) returns the value from > ReadNextFullTransactionId. > > I have updated the patch (skip.diff, attached to this e-mail) to address > this issue. Now, when consume_xids(0) is called, it returns > ReadNextFullTransactionId().value - 1, ensuring no XID is consumed as > shown below: > > postgres=3D# select txid_current(); > txid_current > -------------- > 45498 > (1 row) > > postgres=3D# select consume_xids(0); > consume_xids > -------------- > 45498 > (1 row) Hmm, I think if we expect this function to return the last XID that the function actually consumed, calling consume_xids with 0 should raise an error instead. Even if it returns ReadNextFullTransactionId().value - 1 as you proposed, other concurrent transactions might consume XIDs between txid_current() and consume_xids(0), resulting in consume_xids() appearing to have consumed XIDs. Regards, --=20 Masahiko Sawada Amazon Web Services: https://aws.amazon.com