Fix concurrent-abort detection for subtransactions of prepared transactions

Started by Vaijayanti Bharadwajabout 1 month ago3 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Vaijayanti Bharadwaj
vaijayanti.bharadwaj@enterprisedb.com

Hi hackers,

While testing prepared transactions with subtransactions, I saw a prepared
transaction whose changes were all sent downstream even though it had been
rolled back while it was being decoded.
I found that the concurrent-abort check in ReorderBufferProcessTXN() is
skipped for changes made in a subtransaction of a prepared transaction.

When decoding a prepared transaction, ReorderBufferProcessTXN() is meant to
set CheckXidAlive for each change, so a catalog scan notices if the
transaction aborts concurrently. The check does SetupCheckXidLive here:

if (streaming || rbtxn_is_prepared(change->txn))
{
curtxn = change->txn;
SetupCheckXidLive(curtxn->xid);
}
RBTXN_IS_PREPARED is set only on the top-level transaction, in
ReorderBufferRememberPrepareInfo(). For a change made in a subtransaction,
change->txn is the subtransaction's own ReorderBufferTXN, so the test is
false and CheckXidAlive is not set for it. It stays invalid, unless an
earlier top-level change set it. Because it is invalid,
HandleConcurrentAbort() does nothing, so decoding carries on and sends the
changes. The comment above SetupCheckXidLive() says the check covers the
"(sub)transaction", so I believe this is unintended.

The attached patch tests the flag on the top-level transaction:

if (streaming || rbtxn_is_prepared(rbtxn_get_toptxn(change->txn)))

Once the transaction is prepared, its subtransactions commit or abort
together with it. The error handler already tests rbtxn_is_prepared(txn) on
the top-level transaction, so it needs no change.

The patch adds an injection point, and a test using test_decoding that
fails without the fix and passes with it.

I searched the archives but did not see any earlier report. If there is
one, kindly point me to it.

Regards,
Vaijayanti

Attachments:

t254331_1
v1-0001-Fix-concurrent-abort-detection-for-subtransaction.patchapplication/octet-stream; name=v1-0001-Fix-concurrent-abort-detection-for-subtransaction.patchDownload+128-3
#2Rahila Syed
rahilasyed90@gmail.com
In reply to: Vaijayanti Bharadwaj (#1)
Re: Fix concurrent-abort detection for subtransactions of prepared transactions

Hi Vaijayanti,

Thank you for reporting this bug. I verified that the patch indeed
fixes the problem.

Please see a couple of comments below:

1. The commit that introduced the underlying code is a271a1b50e9, which was
released with PG14; however, the patch mentions backporting the fix up
to PG15. Is this
intentional?

if (streaming || rbtxn_is_prepared(rbtxn_get_toptxn(change->txn)))

Once the transaction is prepared, its subtransactions commit or abort together with it. The error handler already tests rbtxn_is_prepared(txn) on the top-level transaction, so it needs no change.

2. A nitpick: Should the check be replaced by rbtxn_is_prepared(txn)
instead of the rbxtn_get_toptxn(change->txn). The `txn` in
`ReorderBufferProcessTXN` is a top level transaction and this function
calls the exact same check further below which would make it
consistent.

Apart from that, the changes look good to me.

Adding Amit Kapila to CC since he is the author of the original commit.

Thank you,
Rahila Syed

#3Vaijayanti Bharadwaj
vaijayanti.bharadwaj@enterprisedb.com
In reply to: Rahila Syed (#2)
Re: Fix concurrent-abort detection for subtransactions of prepared transactions

Hi Rahila,

Thanks for the review. You are right on both the points.

1. I separated the code change and the test into two patches. The code
change needs the fix backported up to PG 14. And the test to PG 17 since it
uses injection points.
2. I made the change to use `txn` directly: rbtxn_is_prepared(txn)

I have attached the updated patches.

Thanks,
Vaijayanti

On Tue, Oct 27, 2026 at 8:29 PM Rahila Syed <rahilasyed90@gmail.com> wrote:

Show quoted text

Hi Vaijayanti,

Thank you for reporting this bug. I verified that the patch indeed
fixes the problem.

Please see a couple of comments below:

1. The commit that introduced the underlying code is a271a1b50e9, which was
released with PG14; however, the patch mentions backporting the fix up
to PG15. Is this
intentional?

if (streaming || rbtxn_is_prepared(rbtxn_get_toptxn(change->txn)))

Once the transaction is prepared, its subtransactions commit or abort

together with it. The error handler already tests rbtxn_is_prepared(txn) on
the top-level transaction, so it needs no change.

2. A nitpick: Should the check be replaced by rbtxn_is_prepared(txn)
instead of the rbxtn_get_toptxn(change->txn). The `txn` in
`ReorderBufferProcessTXN` is a top level transaction and this function
calls the exact same check further below which would make it
consistent.

Apart from that, the changes look good to me.

Adding Amit Kapila to CC since he is the author of the original commit.

Thank you,
Rahila Syed

Attachments:

t254331_3
v2-0001-Fix-concurrent-abort-detection-for-subtransaction.patchapplication/octet-stream; name=v2-0001-Fix-concurrent-abort-detection-for-subtransaction.patchDownload+1-2
v2-0002-Add-test-for-concurrent-abort-of-a-prepared-trans.patchapplication/octet-stream; name=v2-0002-Add-test-for-concurrent-abort-of-a-prepared-trans.patchDownload+127-2