From: Takeshi Ideriha <iderihatakeshi@gmail.com>
To: shihao zhong <zhong950419@gmail.com>
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Cc: alpkeles99@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #19760: CLUSTER loses a row the transaction inserted and then updated in a rolled-back subtransaction
Date: Mon, 19 Oct 2026 14:15:35 +0900
Message-ID: <5fff280b-96c4-497f-bbfc-0af95b9844b3@gmail.com> (raw)
In-Reply-To: <CAGRkXqRMvz-ZzC88_eXsyJZRpPXhE4_Fo6ueYdm5tpH+CHc6EQ@mail.gmail.com>
References: <19760-8f9a0eba17bc8c92@postgresql.org>
<41175da6-293f-4b8f-ad18-3636f3d8a2f6@gmail.com>
<CAJTYsWXQgkqEpecZK9MCFVV+504oOdueuenRf+xTfNf9BcKE-g@mail.gmail.com>
<CAGRkXqRMvz-ZzC88_eXsyJZRpPXhE4_Fo6ueYdm5tpH+CHc6EQ@mail.gmail.com>
Hi Shihao, Ayush
On 2026/10/19 12:31, shihao zhong wrote:
> Hi,
>
> Here is a patch. rewrite_heap_dead_tuple() drops the held tuple when its
> successor is dead, but that is only right if the successor's inserter
> committed. If it aborted, the update never happened, so 0001 keeps the
> tuple and adds its logical rewrite mapping.
> Setting the hint in HeapTupleSatisfiesVacuum() is not enough. VACUUM
> FULL on a catalog can see a transaction that is still open, and this
> loses the pg_class row of a committed table:
I was actually also writing a code to fix the issue and have just
noticed the patch was already submitted. Attached a patch for reference.
I haven't considered into logical_rewrite_heap_tuple(). I need to check
this... Thank you.
> {
>+ /*
>+ * If the inserter of this tuple aborted, the update >never happened
>+ * and the earlier tuple is not dead. It will be >written at the end.
>+ */
>+ if (!HeapTupleHeaderXminCommitted(old_tuple->t_data) &&
>+ !>TransactionIdDidCommit(HeapTupleHeaderGetXmin(old_tuple->t_data)))
>+ return false;
I am thinking like the below since HeapTupleSatisfiesVacuum set the xmin
invalid hint bit for the dead tuple. So I believe we can just rely on
HeapTupleHeaderXminInvalid hint bit.
+ if (HeapTupleHeaderXminInvalid(old_tuple->t_data))
+ return false;
What do you think above?
>0002 is an isolation test for that case, it is optional. But I want it
>in tree to prevent
>future regression in that area.
+1. I also think this is optional but I believe the issue is easy to
oversight, so adding a new test would be helpful.
Regards,
Takeshi Ideriha
Attachments:v1-0001-fix-cluster-misses-a-live-tuple-updated-by-subtra.patch (text/plain, 2.3K) download
From 4a2b03449fa148c944e3b682cef622b2bfa3aee5 Mon Sep 17 00:00:00 2001
From: Takeshi Ideriha <iderihatakeshi@gmail.com>
Date: Mon, 19 Oct 2026 13:58:40 +0900
Subject: [PATCH v2] fix cluster misses a live tuple updated by subtrasanction
which will be aborted
---
src/backend/access/heap/rewriteheap.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/src/backend/access/heap/rewriteheap.c b/src/backend/access/heap/rewriteheap.cindex 0648e433031..df9c17606a0 100644--- a/src/backend/access/heap/rewriteheap.c+++ b/src/backend/access/heap/rewriteheap.c@@ -554,6 +554,15 @@ rewrite_heap_dead_tuple(RewriteState state, HeapTuple old_tuple)
* when xmin of a tuple is greater than xmax, which sounds
* counter-intuitive but is perfectly valid.
*
+ * There is one exception. Suppose a tuple is created by a transaction, and+ * then updated in a subtransaction, and that subtransaction is aborted.+ * The explanation in the paragraph above assumes that the dead tuple was+ * once committed. In this case, the dead tuple was never committed, so the+ * earlier tuple is not nesessarily dead. Also, in this case, the earlier + * tuple is put in the unresolved table because no hint bit is assigned to + * this tuple and HeapTupleSatisfiesVacuum judges it as INSERT_IN_PROGRESS.+ * As a result, a tuple that is actually live would not be copied.+ *
* We don't bother to try to detect the situation the other way round,
* when we encounter the dead tuple first and then the recently dead one
* that points to it. If that happens, we'll have some unmatched entries
@@ -574,6 +583,17 @@ rewrite_heap_dead_tuple(RewriteState state, HeapTuple old_tuple)
if (unresolved != NULL)
{
+ /*+ * In this case, the earlier tuple is not necessarily dead. We should+ * copy this tuple to a new table. It's fine to leave this tuple in+ * unresolved table since end_heap_rewrite will copy the tuples remained+ * in the table with invalid ctid.+ *+ * We can rely on hint bit since HeapTupleSatisfiesVacuum set the hint + * bit for the dead tuple.+ */+ if (HeapTupleHeaderXminInvalid(old_tuple->t_data))+ return false;
/* Need to free the contained tuple as well as the hashtable entry */
heap_freetuple(unresolved->tuple);
hash_search(state->rs_unresolved_tups, &hashkey,
--
2.50.1 (Apple Git-155)
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: iderihatakeshi@gmail.com, zhong950419@gmail.com, ayushtiwari.slg01@gmail.com, alpkeles99@gmail.com
Subject: Re: BUG #19760: CLUSTER loses a row the transaction inserted and then updated in a rolled-back subtransaction
In-Reply-To: <5fff280b-96c4-497f-bbfc-0af95b9844b3@gmail.com>
References: <19760-8f9a0eba17bc8c92@postgresql.org> <41175da6-293f-4b8f-ad18-3636f3d8a2f6@gmail.com> <CAJTYsWXQgkqEpecZK9MCFVV+504oOdueuenRf+xTfNf9BcKE-g@mail.gmail.com> <CAGRkXqRMvz-ZzC88_eXsyJZRpPXhE4_Fo6ueYdm5tpH+CHc6EQ@mail.gmail.com> <5fff280b-96c4-497f-bbfc-0af95b9844b3@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