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 1wKXZm-0016pw-02 for pgsql-hackers@arkaria.postgresql.org; Wed, 06 May 2026 08:25:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wKXZk-00FmhC-2t for pgsql-hackers@arkaria.postgresql.org; Wed, 06 May 2026 08:25:48 +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 1wKXZk-00Fmft-1e for pgsql-hackers@lists.postgresql.org; Wed, 06 May 2026 08:25:48 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wKXZi-00000000n0X-1RxH for pgsql-hackers@lists.postgresql.org; Wed, 06 May 2026 08:25:48 +0000 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-4890d945eb4so3808195e9.0 for ; Wed, 06 May 2026 01:25:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1778055945; x=1778660745; darn=lists.postgresql.org; h=message-id:date:mime-version:comments:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=QCVx4LF+l4ThcYHyd1uBbAgEIjBOFKKDd61h7Wtoiww=; b=VYwux81viC2PRHUm0gQ/bqeB00jYnT6z3417DtS1yzx7hcM+SOxBkT1wlGPDcXhgPU MDuwdZxLpOZSwIapLf3+v9Ku55DgHgMEhlkz9tGteuwGATFogPE95awx6nLJJvX3pmZf QLHQZbxWhW3/m9HUio6BZZcXQFbDAExE8OTy1jjWLxHAtGf1LBk+YVbb1Iaso2LJwJLu UC8XLyZPA4jbnys+e/QfiNCXUW8IArhY4axX7Xmp1OJj4SlZBI+GAA2/tDiyW5et1j2D o+L3Zu9ibl3KPQi2VCFMVkvgMpgczH638gxnVt4af2oGRG0hSL5Jm83qrFNZjtMPky6I o3Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778055945; x=1778660745; h=message-id:date:mime-version:comments:references:in-reply-to :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=QCVx4LF+l4ThcYHyd1uBbAgEIjBOFKKDd61h7Wtoiww=; b=Q4X861TxSDWsEsh7oe6VwjM1zP8KCmlQ8kRAkuXGuf+He5sFKF90Y3a/rPCjFgnARw lYuJAmYrb8NgpQmN0FsywVACjepCbUNdsFMQqKnSONQmBv7h/cJZux9oAh0qZvTlttKy UOq/cXFIdx0FvHZHCjnjmq+C/JosV9SfyhZ/Ld5LCTh1sJrZvK0jtH2WbRcatsQNzpLm so4h3MqdCMpLvoD3X0ConKrbyLI6Qhi5hlPKPEoh/Qk6qMhk87ow2rWu314xRUKWhi2L 9LXFo6g3wEHL2UlE1X617kUbOj0AkU8NvXitd3klyefVSRLIAr7+FbWiAmlonw+r47Eb 6A0Q== X-Forwarded-Encrypted: i=1; AFNElJ+FiMRdWI3NMO3kNMFr78wcgyhgqKD6rxfXQNREO1fePR68KA6GemDXCaHqgTJttC3Oah9p15DFr73YUVE2@lists.postgresql.org X-Gm-Message-State: AOJu0YwBArG1xLxULJt25UkbPEDNIEgj01I/7CSLvJyvAH7uBNAhCDwo cPZ0QqehEaDPN/7zIDcIEQU6ZzY0+Ge06n+U78dvYYX2JMnPB/yGpQuXiVA0hUXTEg51Os2729e 9+bBl X-Gm-Gg: AeBDiesmgCwe6h2Xwhi63m6AUfSiOl7LRsWt+sv2ocjv5GmGVsAKxZXjNyFzEjotZ8T kFJ1JoKxdR91f2YHO6CtTXPl5NUkAu1XLrTzMB3+iTCMudd46GiayNwkgqmtmi51/2hZ8OzNcRU uwMeJdvjVx3aN1YNrEfMcAw6r8umStpA0nRgqMOxRv/moqhWU6dr3ANDBSFCSOcHdOEvTi1vzlD ec5xZ6z8fvMHaRECBhJ7q3BYcedaPjIZpPDglGXzBPAzd79Io/GBeoys3En82tvyAJNnn/1NL3O 4Bd9vKIQzVNBAvXVEh2oJJILgqYzlSSNULFUMOe1a2tR7F90aQMuaNLyJUU+BLqsVaHjcX3sBOf EeTtd9QnZ9sK40PAoXUSCfs8AkIAuP5Eem86u4mw3MCd+F6bwGJqN372JKmlBRyPcYe4ir+CnRE 808P6bxjDAwdHuQqIhe/IFi3O0MRoYEblYcYIsxPqpv/YV6RU= X-Received: by 2002:a05:600c:348f:b0:485:3cef:d6ea with SMTP id 5b1f17b1804b1-48e522c0854mr35720245e9.13.1778055945066; Wed, 06 May 2026 01:25:45 -0700 (PDT) Received: from localhost (109-81-168-142.rct.o2.cz. [109.81.168.142]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e538a547bsm33653995e9.5.2026.05.06.01.25.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 01:25:44 -0700 (PDT) From: Antonin Houska To: Alvaro Herrera cc: Mihail Nikalayeu , Amit Kapila , Andres Freund , Srinath Reddy Sadipiralla , Matthias van de Meent , Pg Hackers , Robert Treat Subject: Re: Adding REPACK [concurrently] In-reply-to: References: Comments: In-reply-to Alvaro Herrera message dated "Tue, 05 May 2026 17:02:39 +0200." X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.3 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Wed, 06 May 2026 10:25:44 +0200 Message-ID: <77611.1778055944@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain Alvaro Herrera wrote: > On 2026-May-05, Antonin Houska wrote: > > > However, I failed to notice that COMMIT record of > > a transaction listed in the xl_running_xacts WAL record is not guaranteed to > > follow the xl_running_xacts record in WAL. In other words, even if > > xl_running_xacts is created before a COMMIT record of the contained > > transaction, it may end up at higher LSN in WAL. So the cleanup I relied on > > might not take place. > > That's pretty bad news. > > > I've got no good idea how to fix that. One idea occurred to me yet, effectively it's just a cleanup. Part of it was already proposed [1]. [1] https://www.postgresql.org/message-id/flat/CAHg%2BQDcQak4jx_6X2_Ws98rzG%3DxBARLjqm_%3D56wTRUtNsY4DZQ%40mail.gmail.com -- Antonin Houska Web: https://www.cybertec-postgresql.com --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Distinguish-properly-when-database-specific-transact.patch From 33715b1fbba9131744864f23d0310070eaa9c91d Mon Sep 17 00:00:00 2001 From: Antonin Houska Date: Wed, 6 May 2026 09:38:11 +0200 Subject: [PATCH] Distinguish properly when database-specific transaction list should be used. Currently, decoding session that relies on database-specific xl_running_xacts WAL records can also use some information of cluster-wide records and vice versa. Although this approach might reduce the total number of xl_running_xacts records, it's simply not correct. SnapBuildProcessRunningXacts() now decides at the very beginning whether particular record can be used or not. --- src/backend/replication/logical/snapbuild.c | 55 +++++++++++++-------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c index c8309b96ed4..b9661b7d70a 100644 --- a/src/backend/replication/logical/snapbuild.c +++ b/src/backend/replication/logical/snapbuild.c @@ -1157,6 +1157,39 @@ SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn, xl_running_xact ReorderBufferTXN *txn; TransactionId xmin; + /* + * Each decoding session should use either cluster-wide snapshots or + * database-specific ones, but not both. Since the latter can have more + * recent value of oldestRunningXid, builder->xmin could go backwards if + * it was followed by cluster-wide snapshot - see the ->xmin adjustment + * below. + */ + if (!db_specific && OidIsValid(running->dbid)) + return; + + if (db_specific) + { + /* + * Make sure that we have the snapshots needed for startup, as well as + * those for regular cleanup: each time the cluster-wide snapshot is + * created (typically on slot creation or by checkpointer), the + * database-specific snapshot is requested here if the current session + * needs it. + */ + if (!OidIsValid(running->dbid)) + { + LogStandbySnapshot(MyDatabaseId); + + return; + } + /* + * Snapshots issued for other databases do not contain the information + * about transactions in our database. + */ + else if (running->dbid != MyDatabaseId) + return; + } + /* * If we're not consistent yet, inspect the record to see whether it * allows to get closer to being consistent. If we are consistent, dump @@ -1171,17 +1204,7 @@ SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn, xl_running_xact */ if (db_specific) { - /* - * If we must only keep track of transactions running in the - * current database, we need transaction info from exactly that - * database. - */ - if (running->dbid != MyDatabaseId) - { - LogStandbySnapshot(MyDatabaseId); - - return; - } + Assert(running->dbid == MyDatabaseId); /* * We'd better be able to check during scan if the plugin does not @@ -1198,16 +1221,6 @@ SnapBuildProcessRunningXacts(SnapBuild *builder, XLogRecPtr lsn, xl_running_xact else SnapBuildSerialize(builder, lsn); - /* - * Database specific transaction info may exist to reach CONSISTENT state - * faster, however the code below makes no use of it. Moreover, such - * record might cause problems because the following normal (cluster-wide) - * record can have lower value of oldestRunningXid. In that case, let's - * wait with the cleanup for the next regular cluster-wide record. - */ - if (OidIsValid(running->dbid)) - return; - /* * Update range of interesting xids based on the running xacts * information. We don't increase ->xmax using it, because once we are in -- 2.47.3 --=-=-=--