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.96) (envelope-from ) id 1x4FPs-0071jk-1M for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Sep 2026 10:20:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x4FPq-00DnrF-35 for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Sep 2026 10:20:30 +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.96) (envelope-from ) id 1x4FPq-00Dnr7-1p for pgsql-hackers@lists.postgresql.org; Wed, 09 Sep 2026 10:20:30 +0000 Received: from flow-a4-smtp.messagingengine.com ([103.168.172.139]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x4FPn-00000003k1V-3Dha for pgsql-hackers@lists.postgresql.org; Wed, 09 Sep 2026 10:20:30 +0000 Received: from phl-compute-06.internal (phl-compute-06.internal [10.202.2.46]) by mailflow.phl.internal (Postfix) with ESMTP id 2BCFA1380236; Wed, 9 Sep 2026 06:20:25 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Wed, 09 Sep 2026 06:20:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kurilemu.de; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to; s=fm2; t=1788949225; x= 1788956425; bh=IUUhdEoL8PNEBM4RxpdP5rynUp2IGC+ueXJuljzLq9U=; b=l sg28E9LjKYrhv86VS1ugrlWBgmWh/6wyxDkaBr64yhZD0JTkWPPaVbXRgXgVAIoH ee6BU++OeulFnIcCMUgzDfC6qEEA3t49cRuk268WmnGpdpJa9GCApYi3BG/aUJ2S j5yKuZchL07/LpKTZTEzE7o7dqUn+YbPTh6vcJxXfTpsIGG1glA9OayiY9HZc8BA ZcLEOl0uhp53Bw8rWQKYfWghsSR4cvHl/C4E6TJnF2MKNE4+RzparckVQUYE9jsV CAFjPqW9PoiHuX/tTTXYz8o+R3Xl+mvV+FuR9Wb9WpZWncENhTHTzVhS2rpYGIVJ 7K1WmjM1vzlZP8JHcHxtw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1788949225; x=1788956425; bh=I UUhdEoL8PNEBM4RxpdP5rynUp2IGC+ueXJuljzLq9U=; b=lrSVhAu95PzEmsRUq xD8lQIgvVyCOWNcEbgujNEalw50Ynq8zzVPMveE3VQLddm2rbF7m2nU7TvOIMIGn B1VgDGr8CZ9D0BITJyTWof8MaVE2Qx6WCn1UnNmUMzwjOGecIQQbpJp0nmOmGV1X OFacbXoBIgnpNBYVYOUAsLC7CwphbOjrPJmWTBDyV4vgJDeq/7rNtlL4nNeBu1Q9 G6StqHbWCQA33MkUQ9PuNqAAhczZILRZ3PjFPIALJKWcrq+DGaTjrIKx/bNgAJ4N L99J6SdpRE0/W2fLBw/KUGIobY5W/2QQ+wNq6cyuuwDH/1ur+9cYj1GYdM5XLYZd zfg7g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTGjsuamFgXiEEvCTYJ51xAYAyk+oiIzMrVcqcbE3MwQbgkM2uWFVU07z6w9lp9eTm jZ//hWwY+9Rk6VHCEp/rYWZbucy3+SGePz++yCzBWw4hrlE7RjoYP4hzk5MmBsS+U+zzB9 galVFbfqaf2H32PafIPCpzOIdn8vv1Sv5MaLSqxsTgkSdiNQ+7gj4HrZKz6kwH8oZLRjAo Ato7QrBuBHr7AZ176ZhEbooAC8ISd/Gq9ATfl2nTqd01upRCI0LJic2cu/w4GraFTiI/7X 3nx3S1zqAq3kdROU6MKvBMoqTKOPA2Auw51aJPMUtbBUyDa1proxfSpfIIL+ZLq1GoxHr4 R9bU0S2Wc8jFfIDzujertaG1NmTmTe7i8u8DQ9Dh3YbLeg/E0QuFmokxGm2RfhLW8BseqZ hNT/zI7zxS5sgByx3jakD8eWHQAGivQZOd2qlcpM2GJ7Ll4O9WfgQHVmiMOs2c0fZFFpw7 5bF7QTdLwddhlrTyTgTUWDxfEgr5ZbOYSJjMWpUmc6hyJHazbilyzmle6AyGpCGcnuh+Nc s3skGcj/X9mRvDkSp3X0wGpNRjb7o8Uo9etbQQ5oZb59jkD1VTbNjUcNVBs/v/t2shdwF/ Oo8l0k/HchT4ndWsesq1N2pUo5FRSDQaWNLydsjbe7cnZT6/Y5Dumb6LpsLA X-ME-Proxy: Feedback-ID: ie3de48e3:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 9 Sep 2026 06:20:24 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kurilemu.de; s=schmee; t=1788949223; bh=xH6v1kopQzoQu9RP6vFa3CgQiMGe5jiXxbTClGpEy9s=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=5ieL+ZP/XjJWacdDTmCJ3rWBFYg0xgegOXLitZjg/nTUZqIEsPw4lKLlhmoUKF5ev 4mgR5htANB01SqBrBuCLtX4AjPx6ObTmRUzebx2h/Ksr84P1nmgSLuG1Lxxuek8dd/ YTpGgu5LBHjblmV2EuJFptfwiPrRdKXWD5XT27ouhFNigVUVwKBWNf+wLyRelJMBw3 tzZhI5St6GkcO/rlxus6UVe1Ccp7GZULsrD7rAfVwvrpMMeF3I8w4IwjFV6JnHhFg8 C0iFfaAZJT7NvQDHfUQLc8N0ygtHTR4EkMtX7Eff+cs8sNZ3zCn/zb1DX0JLAjaBBC 8Tv6b0VIDoYKw== Received: by ida.kurilemu.internal (Postfix, from userid 1000) id F3F89B00039; Wed, 09 Sep 2026 12:20:22 +0200 (CEST) Date: Wed, 9 Sep 2026 12:20:22 +0200 From: =?utf-8?Q?=C3=81lvaro?= Herrera To: "Zhijie Hou (Fujitsu)" Cc: Antonin Houska , "pgsql-hackers@lists.postgresql.org" , Mihail Nikalayeu , Andres Freund Subject: Re: Race conditions in logical decoding Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, replying to Hou and Houska emails in one. On 2026-Aug-22, Zhijie Hou (Fujitsu) wrote: > I think the cache might be better placed in the SnapBuild struct (at least on > HEAD) rather than in static variables. As currently written, it persists across > decoding sessions in the same backend - a session could build a snapshot, drop > the slot, and later create a new slot and build another snapshot, potentially > consulting stale entries from the first builder. For example, it has a wraparound > concern: after XID wrap, a cached value could refer to a different transaction, > causing us to skip the CLOG wait and reintroduce the inconsistency this patch > aims to fix. Hmm, yeah, there's definitely a problem with that. Now, this bug can affect logical decoding in existing releases as well, so we should backpatch this fix, and I'm not sure about changing SnapBuild in backbranches. Maybe another approach is to use file-level statics (rather than function-level) so that they can be reset by slot drop routines. > Besides, just to confirm one note: IIUC, for exported snapshots by logicalrep, a > transaction could be treated as committed while still in PGPROC, while > concurrent MVCC snapshots still see it as in progress which looks inconsistent. > I understand that waiting for ProcArray removal in the general case could > deadlock against synchronous replication, so it's probably acceptable to leave > it unchanged for internal usage in active replication processes. OK. TBH I'm somewhat unease about this inconsistency; I wondered about doing the CLOG-based test only in sync replication and using XidIsInProgress otherwise, but didn't really try (which is to say: I'm not even sure if it's _possible_ at all.) > However, for cases where the snapshot is exported, would it be possible to > additionally wait for it in SnapBuildInitialSnapshot() (which is used only by > CREATE_REPLICATION_SLOT and REPACK)? Since that runs before START_REPLICATION, > the process isn't streaming or feeding any subscriber, so I believe the deadlock > wouldn't occur there. (I think that the walsender executing > CREATE_REPLICATION_SLOT shouldn't be added to sync_standby_names, otherwise > building the initial snapshot itself would already have a deadlock risk via > SnapBuildWaitSnapshot->XactLockTableWait.) Yeah, we could do that. Do you want to try and write a patch? On 2026-Aug-25, Antonin Houska wrote: > Besides, that, it occurred to me that a sorted array might be appropriate > instead of a list, so that bsearch() can be used, but I'm not sure about that. I think we should absolutely do something like that, because repeated list_member_oid() are unlikely to be great. Maybe as output of each run we end up with an unsorted array; when SnapBuildBuildSnapshot runs next time, the first thing we do is sort the array for bsearch. That way, we don't have to sort unless absolutely necessary. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ "Para tener más hay que desear menos"