On Mon, Nov 23, 2026 at 8:39 PM Manu <[email protected]> wrote:
> One nit on its commit message: the empty-list return does not make
> pure DML transactions faster than today, since master already does
> nothing with an empty list.  What it does is keep the new subxacts[]
> lookups from costing anything there.

Good catch, thanks for testing v1 and pointing that out!

Attached is v2 with the commit message updated to clarify that the
dlist_is_empty(&toptxn->tuplecids) check avoids adding subxacts[] lookup
overhead when there are no tuplecids to clean up (no code changes from
v1).

Thanks Michael and Dilip for taking a look as well.

Regards,
Rajesh Kumar Thandapani
From 956906fe8317101b06838f6ca8ae1d01cf264674 Mon Sep 17 00:00:00 2001
From: Rajesh Kumar Thandapani <[email protected]>
Date: Thu, 19 Nov 2026 04:40:08 +0000
Subject: [PATCH v2] Skip tuplecid cleanup scan when aborted subxacts have no
 catalog changes.

Commit beff3cd0098 added ReorderBufferCleanupAbortedTupleCids() to drop
TUPLECID entries queued by an aborted subtransaction or its subcommitted
children from toptxn->tuplecids.  However, it currently scans
toptxn->tuplecids on every known subtransaction abort, even when none of
the aborting xids modified any catalogs.

When a transaction accumulates many tuplecids (for example from creating
temporary tables) and later rolls back many DML-only savepoints or
PL/pgSQL EXCEPTION blocks, decoding spends O(tuplecids * aborts) walking
toptxn->tuplecids without removing anything.

Since SnapBuildProcessNewCid() marks subxid with
RBTXN_HAS_CATALOG_CHANGES before calling ReorderBufferAddNewTupleCids()
and the flag is never cleared, skip the walk when none of the aborting
xids has that flag set.  Return early when toptxn->tuplecids is empty so
the new subxacts[] lookups add no overhead when there are no tuplecids
to clean up.  Assert this invariant in ReorderBufferAddNewTupleCids(),
and add a permutation to tuplecid_nested.spec where the outer savepoint
writes to a regular table while its released inner savepoint modifies a
catalog table.

Author: Ayush Tiwari <[email protected]>
Author: Rajesh Kumar Thandapani <[email protected]>
Reviewed-by: Manu <[email protected]>
Discussion: https://postgr.es/m/CAJTYsWXAQmRfNi1zg7-yeuMc56yeOkq22eyxDiQa_Q+2tD+sGg@mail.gmail.com
---
 .../expected/tuplecid_nested.out              | 32 +++++++++++++++++++
 .../test_decoding/specs/tuplecid_nested.spec  |  5 +++
 .../replication/logical/reorderbuffer.c       | 21 ++++++++++++
 3 files changed, 58 insertions(+)

diff --git a/contrib/test_decoding/expected/tuplecid_nested.out b/contrib/test_decoding/expected/tuplecid_nested.out
index 5d816374..a706db11 100644
--- a/contrib/test_decoding/expected/tuplecid_nested.out
+++ b/contrib/test_decoding/expected/tuplecid_nested.out
@@ -30,3 +30,35 @@ COMMIT
 stop    
 (1 row)
 
+
+starting permutation: s1_init s0_begin s0_topwrite s0_outer s0_outerwrite s0_inner s0_catinsert1 s0_release_inner s0_rollback_outer s2_vacuum s0_catinsert2 s0_commit s1_get_changes
+step s1_init: SELECT 'init' FROM pg_create_logical_replication_slot('tuplecid_nested_slot', 'test_decoding');
+?column?
+--------
+init    
+(1 row)
+
+step s0_begin: BEGIN;
+step s0_topwrite: INSERT INTO tbl1 VALUES (0);
+step s0_outer: SAVEPOINT outer_sp;
+step s0_outerwrite: INSERT INTO tbl1 VALUES (1);
+step s0_inner: SAVEPOINT inner_sp;
+step s0_catinsert1: INSERT INTO user_cat VALUES (5, 'e');
+step s0_release_inner: RELEASE SAVEPOINT inner_sp;
+step s0_rollback_outer: ROLLBACK TO SAVEPOINT outer_sp;
+step s2_vacuum: VACUUM user_cat;
+step s0_catinsert2: INSERT INTO user_cat VALUES (6, 'f');
+step s0_commit: COMMIT;
+step s1_get_changes: SELECT data FROM pg_logical_slot_get_changes('tuplecid_nested_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0') WHERE data NOT LIKE '%user_cat%';
+data                                      
+------------------------------------------
+BEGIN                                     
+table public.tbl1: INSERT: val1[integer]:0
+COMMIT                                    
+(3 rows)
+
+?column?
+--------
+stop    
+(1 row)
+
diff --git a/contrib/test_decoding/specs/tuplecid_nested.spec b/contrib/test_decoding/specs/tuplecid_nested.spec
index 8dd52984..eec8673e 100644
--- a/contrib/test_decoding/specs/tuplecid_nested.spec
+++ b/contrib/test_decoding/specs/tuplecid_nested.spec
@@ -54,6 +54,7 @@ setup { SET synchronous_commit=on; }
 step "s0_begin" { BEGIN; }
 step "s0_topwrite" { INSERT INTO tbl1 VALUES (0); }
 step "s0_outer" { SAVEPOINT outer_sp; }
+step "s0_outerwrite" { INSERT INTO tbl1 VALUES (1); }
 step "s0_inner" { SAVEPOINT inner_sp; }
 step "s0_catinsert1" { INSERT INTO user_cat VALUES (5, 'e'); }
 step "s0_release_inner" { RELEASE SAVEPOINT inner_sp; }
@@ -75,3 +76,7 @@ step "s2_vacuum" { VACUUM user_cat; }
 # The slot is created before the transaction begins, so the final
 # s1_get_changes decodes the whole transaction through its commit.
 permutation "s1_init" "s0_begin" "s0_topwrite" "s0_outer" "s0_inner" "s0_catinsert1" "s0_release_inner" "s0_rollback_outer" "s2_vacuum" "s0_catinsert2" "s0_commit" "s1_get_changes"
+
+# Same as above, but outer_sp also does a regular write so it exists in
+# reorderbuffer without RBTXN_HAS_CATALOG_CHANGES.
+permutation "s1_init" "s0_begin" "s0_topwrite" "s0_outer" "s0_outerwrite" "s0_inner" "s0_catinsert1" "s0_release_inner" "s0_rollback_outer" "s2_vacuum" "s0_catinsert2" "s0_commit" "s1_get_changes"
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 2917d971..bbea2150 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -3300,6 +3300,24 @@ ReorderBufferCleanupAbortedTupleCids(ReorderBuffer *rb,
 
 	/* Get the top-level transaction whose tuplecid list we're to clean */
 	toptxn = rbtxn_get_toptxn(txn);
+	if (dlist_is_empty(&toptxn->tuplecids))
+		return;
+
+	/*
+	 * Skip the scan if none of the aborting xids has catalog changes (the
+	 * flag is set before queueing tuplecids and never cleared).
+	 */
+	if (!rbtxn_has_catalog_changes(txn))
+	{
+		bool		subtxn_has_catalog_changes = false;
+
+		for (int i = 0; !subtxn_has_catalog_changes && i < nsubxacts; i++)
+			subtxn_has_catalog_changes =
+				ReorderBufferXidHasCatalogChanges(rb, subxacts[i]);
+
+		if (!subtxn_has_catalog_changes)
+			return;
+	}
 
 	dlist_foreach_modify(it, &toptxn->tuplecids)
 	{
@@ -3600,6 +3618,9 @@ ReorderBufferAddNewTupleCids(ReorderBuffer *rb,
 	ReorderBufferChange *change = ReorderBufferAllocChange(rb);
 	ReorderBufferTXN *txn;
 
+	/* ReorderBufferCleanupAbortedTupleCids() relies on this flag being set */
+	Assert(ReorderBufferXidHasCatalogChanges(rb, subxid));
+
 	txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
 
 	change->data.tuplecid.locator = locator;
-- 
2.56.1.831.g720f3f5937-goog

Reply via email to