From: Michael Paquier <michael@paquier.xyz>
To: Zhijie Hou <houzhijie22@gmail.com>
Cc: shihao zhong <zhong950419@gmail.com>
Cc: Takeshi Ideriha <iderihatakeshi@gmail.com>
Cc: 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: Thu, 22 Oct 2026 11:22:08 +0900
Message-ID: <atlzUChbZatun_HQ@paquier.xyz> (raw)
In-Reply-To: <CAFvd2n_qVC1yqVEZXrh3W1gandgWUcYsOgsiY7xzU_Vy=qxWdQ@mail.gmail.com>
References: <CAJTYsWXQgkqEpecZK9MCFVV+504oOdueuenRf+xTfNf9BcKE-g@mail.gmail.com>
<CAGRkXqRMvz-ZzC88_eXsyJZRpPXhE4_Fo6ueYdm5tpH+CHc6EQ@mail.gmail.com>
<5fff280b-96c4-497f-bbfc-0af95b9844b3@gmail.com>
<CAGRkXqSo9vZ=7mw_X0tjH1-Wwdz8nWu9xpdX1x8hVM8tCazKAA@mail.gmail.com>
<949ad7b3-2b0e-40dc-b6ac-e84da432149b@gmail.com>
<atcbPTfcO9rihmbD@paquier.xyz>
<atceNofaGIE2dOFn@paquier.xyz>
<CAGRkXqStu17-_568HNTqz_Qh7=FKTaRO2A572hizQuhMBbExvQ@mail.gmail.com>
<athl6okS1xEgasD3@paquier.xyz>
<CAFvd2n_qVC1yqVEZXrh3W1gandgWUcYsOgsiY7xzU_Vy=qxWdQ@mail.gmail.com>
On Wed, Oct 21, 2026 at 06:01:28PM +0800, Zhijie Hou wrote:
> + if (!(tuple->t_infomask & HEAP_XMAX_INVALID) &&
> + !HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask) &&
> + !HeapTupleHeaderIsOnlyLocked(tuple))
> + {
> + TransactionId xmax = HeapTupleHeaderGetUpdateXid(tuple);
> +
> + if (!TransactionIdIsInProgress(xmax) &&
> + !TransactionIdDidCommit(xmax))
> + SetHintBits(tuple, buffer, HEAP_XMAX_INVALID,
> + InvalidTransactionId);
>
> If a tuple's Xmax is a MultiXact and the updating subtransaction is still in
> progress when HeapTupleHeaderIsOnlyLocked() is called, it returns false.
> However, if that updating subtransaction aborts before the second
> TransactionIdIsInProgress() check, the second check returns false and sets
> HEAP_XMAX_INVALID on the tuple even though the MultiXact still has active
> members, which I think violates the MultiXact hint-bit standard.
Perhaps also check for HEAP_XMAX_IS_MULTI to skip the multixacts and
make sure that active members are not lost, then? I am not completely
sure if this would be always right, though, these code paths are the
hard-level definition of tricky.
What I am really wondering is why do we actually need to fix *two*
problems at once? The first SetHintBits() looks like a no-brainer to
me, and links to the second permutation in the test "s1_plain
s1_count" where we do a CLUSTER after an UPDATE rollbacked to a
previous savepoint. I don't think that there is a need for an
isolation test on this one, either, wouldn't a plain SQL sequence be
sufficient?
That's not to mention that the first one can be hit with DDLs that do
some update on catalogs, as well. Say, an ANALYZE with a CLUSTER
(this SQL sequence is a courtesy of Claude, I've just pointed to it
the possibility and the agent has confirmed the sequence):
BEGIN;
-- force the index-scan path
SET LOCAL random_page_cost = 0.0001;
SET LOCAL seq_page_cost = 1000;
CREATE TABLE s (a int);
INSERT INTO s VALUES (1);
ANALYZE s;
SAVEPOINT sp;
INSERT INTO s SELECT g FROM generate_series(2, 1000) g;
-- updates pg_statistic row
ANALYZE s;
ROLLBACK TO sp;
RELEASE sp;
CLUSTER VERBOSE pg_statistic USING pg_statistic_relid_att_inh_index;
COMMIT;
-- one row expected, none on HEAD.
SELECT count(*) FROM pg_statistic WHERE starelid = 's'::regclass;
The VACUUM FULL on pg_class is less exciting, points to the second
SetHintBits(). And as far as I can see, this one is actually the
source of a measurable regression. To prove my point, see the SQL
scripts attached. Use them like that:
1) Run setup.sql, for some relations.
2) Run holders.sql through a bunch of sessions (64 here, could be
more). This is important to stress TransactionIdIsInProgress():
for i in $(seq 64); do
psql -X -f holders.sql > /dev/null 2>&1 &
done
3) In a new session, run either subxact_updates.sql, delete.sql or
insert_only.sql.
3) Run vacuum.sql, measure its runtime.
Then with 500k rows, here are some VACUUM average runtimes in ms from
one of my laptops:
HEAD Shihao's v2
subxact_updates 1408 2123
delete 686 1369
insert_only 689 693
The subxact_updates is an edge case, fine, still it regresses badly.
The DELETE case is a much more common case. It regresses even more
badly, and that does not look acceptable to me. That should be
optimizable, of course, I don't really know yet how to do that. I
know however that regressions like these ones are never good.
So the first step to me would be a "Divide and Conquer" strategy,
where we should look at each problem independently instead of trying
to do too much at once.
--
Michael
Attachments:
setup.sql (application/sql, 152B) download
delete.sql (application/sql, 193B) download
insert_only.sql (application/sql, 145B) download
subxact_updates.sql (application/sql, 412B) download
vacuum.sql (application/sql, 173B) download
holders.sql (application/sql, 346B) download
signature.asc (application/pgp-signature, 832B) download
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: michael@paquier.xyz, houzhijie22@gmail.com, zhong950419@gmail.com, iderihatakeshi@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: <atlzUChbZatun_HQ@paquier.xyz>
References: <CAJTYsWXQgkqEpecZK9MCFVV+504oOdueuenRf+xTfNf9BcKE-g@mail.gmail.com> <CAGRkXqRMvz-ZzC88_eXsyJZRpPXhE4_Fo6ueYdm5tpH+CHc6EQ@mail.gmail.com> <5fff280b-96c4-497f-bbfc-0af95b9844b3@gmail.com> <CAGRkXqSo9vZ=7mw_X0tjH1-Wwdz8nWu9xpdX1x8hVM8tCazKAA@mail.gmail.com> <949ad7b3-2b0e-40dc-b6ac-e84da432149b@gmail.com> <atcbPTfcO9rihmbD@paquier.xyz> <atceNofaGIE2dOFn@paquier.xyz> <CAGRkXqStu17-_568HNTqz_Qh7=FKTaRO2A572hizQuhMBbExvQ@mail.gmail.com> <athl6okS1xEgasD3@paquier.xyz> <CAFvd2n_qVC1yqVEZXrh3W1gandgWUcYsOgsiY7xzU_Vy=qxWdQ@mail.gmail.com> <atlzUChbZatun_HQ@paquier.xyz>
* 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