pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Zhijie Hou <houzhijie22@gmail.com>
To: Atisss hh <125atishprak@gmail.com>
To: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #19839: Assert in ReorderBufferRestoreCleanup for aborted, spilled transaction
Date: Sun, 8 Nov 2026 23:11:09 +0800
Message-ID: <1da45744-968f-4f40-addf-dc085e928ec5@gmail.com> (raw)
In-Reply-To: <CAGqfTtvQB4J0dJFnNMAF9LCAJhGBe5THd2R_=MxTHP5C5vwD=w@mail.gmail.com>
References: <19839-ead02c2208a3f8bd@postgresql.org>
	<CAGqfTtvQB4J0dJFnNMAF9LCAJhGBe5THd2R_=MxTHP5C5vwD=w@mail.gmail.com>

Hi,

On Sat, Nov 7, 2026 at 09:26 AM Atisss hh <125atishprak@gmail.com>wrote:
> 
> 
> On Sat, 7 Nov 2026 at 06:54, PG Bug reporting form 
> <noreply@postgresql.org <mailto:noreply@postgresql.org>> wrote:
> 
>     The following bug has been logged on the website:
> 
>     Bug reference:      19839
>     Logged by:          Atish Prakash
>     Email address: 125atishprak@gmail.com <mailto:125atishprak@gmail.com>
>     PostgreSQL version: 18.6
>     Operating system:   Linux x86_64 (kernel 7.0.0, glibc 2.43)
>     Description:
> 
>     On an assert-enabled build, logical decoding can fail this assertion and
>     take
>     the server through crash recovery:
> 
>          TRAP: failed Assert("XLogRecPtrIsValid(txn->final_lsn)"), File:
>     "reorderbuffer.c", Line: 5001
> 
>     Expected: decoding discards the aborted transaction and continues.
> 
>     The sequence is:
> 
>     1. A transaction has a subtransaction with an XID but no decoded
>     changes,
>     for
>         example one that only ran SELECT ... FOR UPDATE.
>     2. The parent exceeds logical_decoding_work_mem while still in progress.
>         ReorderBufferSerializeTXN() recurses into the subtransaction, writes
>     nothing,
>         and still sets RBTXN_IS_SERIALIZED on it. Its final_lsn stays
>     invalid.
>     3. The parent aborts while the decoder is behind.
>     4. The parent is chosen for eviction before its abort record is decoded.
>         ReorderBufferCheckAndTruncateAbortedTXN() finds it aborted and
>     truncates
>     it,
>         and ReorderBufferRestoreCleanup() fails on the subtransaction's
>     final_lsn.
> 
>     ReorderBufferCheckAndTruncateAbortedTXN() came in with 072ee847ad, so I
>     expect
>     17 and earlier are not affected, but I have not tested them.

Thanks for the report and patch.

I think this issue affects can all supported branches (back to PG 14), 
and does not require an aborted transaction to reproduce.

For example, if a top-level transaction has a subtransaction with an XID 
but no decoded changes, and then spills to disk while queuing a partial 
change (such as TOAST chunks), the empty subtransaction is marked 
RBTXN_IS_SERIALIZED with final_lsn == InvalidXLogRecPtr. Once the main 
table insert completes the partial change, 
ReorderBufferProcessPartialChange() streams the transaction via 
ReorderBufferStreamTXN(), and ReorderBufferTruncateTXN() -> 
ReorderBufferRestoreCleanup() hits the same assertion on the empty 
subtransaction.

See the attachment for a test to reproduce this issue. (Feel free to 
merge if it's worth adding a test to cover this path)

> 
>      From reading the code, a non-assert build goes through an empty
>     cleanup loop
>     for
>     this subtransaction, so I expect no user-visible effect there. I
>     have not
>     run a
>     non-assert build.

I think in a rare case, this can also affect the stats collection for 
spilled txns. For example, if an empty subtransaction is marked as 
serialized, we skip incrementing spillTxns because no changes were 
written; if real changes are later serialized in this subtransaction, we 
still do not increment spillTxns because the subtransaction is already 
marked as serialized and we avoid counting it "twice".

> 
>     Proposed fix, attached as 0001-fix-aborted-spilled-txn-assert.patch: set
>     RBTXN_IS_SERIALIZED only if something was written.

And I have one minor comment for the patch:

+	if (spilled)
+		txn->txn_flags |= RBTXN_IS_SERIALIZED;

ReorderBufferSerializeTXN() already has an "if (spilled)" block right 
above this, so we shall directly move the assignment into that block.

-- 
Best Regards,
Zhijie Hou

Attachments:

nocfbot-0001-Top-up-for-0001-Add-regression-test-for-spilled-txn-.patch (text/plain, 2.8K) download From d57466124f66ed276b7ab1f009fa659bd9987454 Mon Sep 17 00:00:00 2001 From: Zhijie Hou <houzhijie22@gmail.com> Date: Sun, 8 Nov 2026 22:44:19 +0800 Subject: [PATCH] Top-up for 0001: Add regression test for spilled txn with empty subxact Author: Zhijie Hou <houzhijie22@gmail.com> --- contrib/test_decoding/expected/stream.out | 24 +++++++++++++++++++++++ contrib/test_decoding/sql/stream.sql | 15 ++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out index 0ec5c933610..9c164847b03 100644 --- a/contrib/test_decoding/expected/stream.out +++ b/contrib/test_decoding/expected/stream.out @@ -157,6 +157,30 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc COMMIT (3 rows) +-- bug #19839 +-- +-- A subtransaction without decoded changes must not be marked as serialized +-- when its parent transaction spills to disk during a partial TOAST change +-- (stream_test.data has EXTERNAL storage set above). Otherwise, truncating +-- the transaction after streaming fails on the subtransaction's invalid +-- final_lsn. +BEGIN; +SAVEPOINT s; +SELECT 1 FROM stream_test LIMIT 1 FOR UPDATE; + ?column? +---------- + 1 +(1 row) + +RELEASE SAVEPOINT s; +INSERT INTO stream_test VALUES (repeat('a', 100000)); +COMMIT; +SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1'); + count +------- + 4 +(1 row) + DROP TABLE stream_test; SELECT pg_drop_replication_slot('regression_slot'); pg_drop_replication_slot diff --git a/contrib/test_decoding/sql/stream.sql b/contrib/test_decoding/sql/stream.sql index 5e45a8e9b64..f3159335564 100644 --- a/contrib/test_decoding/sql/stream.sql +++ b/contrib/test_decoding/sql/stream.sql @@ -82,5 +82,20 @@ ROLLBACK; INSERT INTO stream_test VALUES ('after-abort'); SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); +-- bug #19839 +-- +-- A subtransaction without decoded changes must not be marked as serialized +-- when its parent transaction spills to disk during a partial TOAST change +-- (stream_test.data has EXTERNAL storage set above). Otherwise, truncating +-- the transaction after streaming fails on the subtransaction's invalid +-- final_lsn. +BEGIN; +SAVEPOINT s; +SELECT 1 FROM stream_test LIMIT 1 FOR UPDATE; +RELEASE SAVEPOINT s; +INSERT INTO stream_test VALUES (repeat('a', 100000)); +COMMIT; +SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1'); + DROP TABLE stream_test; SELECT pg_drop_replication_slot('regression_slot'); -- 2.54.0 (Apple Git-157)

view thread (7+ messages)  latest in thread

Message-ID: <1da45744-968f-4f40-addf-dc085e928ec5@gmail.com>
Permalink:  ../1da45744-968f-4f40-addf-dc085e928ec5@gmail.com/
Also on:    postgresql.org/message-id/1da45744-968f-4f40-addf-dc085e928ec5@gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: houzhijie22@gmail.com, 125atishprak@gmail.com
  Subject: Re: BUG #19839: Assert in ReorderBufferRestoreCleanup for aborted, spilled transaction
  In-Reply-To: <1da45744-968f-4f40-addf-dc085e928ec5@gmail.com>
  References: <19839-ead02c2208a3f8bd@postgresql.org> <CAGqfTtvQB4J0dJFnNMAF9LCAJhGBe5THd2R_=MxTHP5C5vwD=w@mail.gmail.com> <1da45744-968f-4f40-addf-dc085e928ec5@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox