On Sat, Oct 17, 2026 at 12:02 PM Manu <[email protected]> wrote: > One thing worth documenting: the guard relies on the catalog-changes flag > never being cleared once set. That holds today, but a short comment or an > Assert at the skip would make the dependency explicit for whoever adds a > flag path later.
Hi Manu, Ayush, Since this did not seem to get picked up as a formal patch yet, I put together a v1 patch (attached) building on Ayush's snippet and your feedback. On top of the earlier snippet, it also returns early when toptxn->tuplecids is already empty (so pure DML transactions avoid the subxacts[] lookups), adds an Assert in ReorderBufferAddNewTupleCids() that subxid has RBTXN_HAS_CATALOG_CHANGES set, and adds the tuplecid_nested.spec permutation with a regular write in the outer savepoint. On an assert-enabled build of master, decoding a transaction with 50,000 catalog tuplecids followed by 5,000 rolled-back DML subtransactions drops from ~2310 ms to ~104 ms, and all test_decoding tests pass. Regards, Rajesh Kumar Thandapani
From 60c044d3e9030b3cea676c3a8601b721aca700f8 Mon Sep 17 00:00:00 2001 From: Rajesh Kumar Thandapani <[email protected]> Date: Thu, 19 Nov 2026 04:40:08 +0000 Subject: [PATCH v1] 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 the list is empty or 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, return early when toptxn->tuplecids is empty or none of the aborting xids has that flag set. Assert this 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 46843985..b52389fc 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -3299,6 +3299,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) { @@ -3599,6 +3617,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.750.gf70dc682cf-goog
