postgres.git / summary / log / commit / refs
commit 07d1b44181df03b6b16e372a1be4511fa94effdf
Author: Michael Paquier <michael@paquier.xyz>
Date: Mon Oct 26 00:54:50 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0
Set HEAP_XMAX_INVALID for tuples updated by aborted subtransaction
HeapTupleSatisfiesVacuumHorizon() did not set HEAP_XMAX_INVALID on a
tuple inserted, then updated by a subtransaction that rolled back.
rewriteheap.c relies on this hint to tell if such tuples should be
treated as updated and would drop them, causing rewrites to lose some
tuples.
HeapTupleSatisfiesVacuumHorizon() is updated to set the hint
HEAP_XMAX_INVALID, so as heap AM is able to redirect such a tuple to its
correct route, and does not lose it.
This issue impacted both CLUSTER (down to v14) and REPACK [USING] (down
to v19).
Reported-by: Alperen Keles <alpkeles99@gmail.com>
Author: Shihao Zhong <zhong950419@gmail.com>
Author: Michael Paquier <michael@paquier.xyz>
Reported-by: Zhijie Hou <houzhijie22@gmail.com>
Reported-by: Takeshi Ideriha <iderihatakeshi@gmail.com>
Reported-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/19760-8f9a0eba17bc8c92@postgresql.org
Backpatch-through: 14
src/backend/access/heap/heapam_visibility.c | 2 ++
src/test/regress/expected/cluster.out | 28 ++++++++++++++++++++++++++++
src/test/regress/sql/cluster.sql | 19 +++++++++++++++++++
3 files changed, 49 insertions(+)
diff --git a/src/backend/access/heap/heapam_visibility.c b/src/backend/access/heap/heapam_visibility.c
index a7160013419..55729d043b2 100644
--- a/src/backend/access/heap/heapam_visibility.c
+++ b/src/backend/access/heap/heapam_visibility.c
@@ -1264,6 +1264,8 @@ HeapTupleSatisfiesVacuumHorizon(HeapTuple htup, Buffer buffer, TransactionId *de
if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetUpdateXid(tuple)))
return HEAPTUPLE_DELETE_IN_PROGRESS;
/* deleting subtransaction must have aborted */
+ SetHintBits(tuple, buffer, HEAP_XMAX_INVALID,
+ InvalidTransactionId);
return HEAPTUPLE_INSERT_IN_PROGRESS;
}
else if (TransactionIdIsInProgress(HeapTupleHeaderGetRawXmin(tuple)))
diff --git a/src/test/regress/expected/cluster.out b/src/test/regress/expected/cluster.out
index 542c2e098c5..7f93d9ca7f4 100644
--- a/src/test/regress/expected/cluster.out
+++ b/src/test/regress/expected/cluster.out
@@ -657,6 +657,33 @@ SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
(4 rows)
COMMIT;
+-- CLUSTER with rollbacked subtransaction, where the same row is inserted,
+-- updated, then rollbacked.
+BEGIN;
+-- Index scan required for CLUSTER, force it.
+SET LOCAL random_page_cost = 0.0001;
+SET LOCAL seq_page_cost = 1000;
+CREATE TABLE cluster_aborted_upd (a int, b int);
+CREATE INDEX cluster_aborted_upd_idx ON cluster_aborted_upd (b);
+INSERT INTO cluster_aborted_upd VALUES (1, 3);
+SAVEPOINT sp;
+UPDATE cluster_aborted_upd SET a = 2;
+ROLLBACK TO sp;
+CLUSTER cluster_aborted_upd USING cluster_aborted_upd_idx;
+COMMIT;
+-- One row expected.
+SELECT count(*) FROM cluster_aborted_upd;
+ count
+-------
+ 1
+(1 row)
+
+SELECT * FROM cluster_aborted_upd;
+ a | b
+---+---
+ 1 | 3
+(1 row)
+
-- clean up
DROP TABLE clustertest;
DROP TABLE clstr_1;
@@ -664,4 +691,5 @@ DROP TABLE clstr_2;
DROP TABLE clstr_3;
DROP TABLE clstr_4;
DROP TABLE clstr_expression;
+DROP TABLE cluster_aborted_upd;
DROP USER regress_clstr_user;
diff --git a/src/test/regress/sql/cluster.sql b/src/test/regress/sql/cluster.sql
index 6cb9c926c06..1b0afe5673d 100644
--- a/src/test/regress/sql/cluster.sql
+++ b/src/test/regress/sql/cluster.sql
@@ -308,6 +308,24 @@ EXPLAIN (COSTS OFF) SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
COMMIT;
+-- CLUSTER with rollbacked subtransaction, where the same row is inserted,
+-- updated, then rollbacked.
+BEGIN;
+-- Index scan required for CLUSTER, force it.
+SET LOCAL random_page_cost = 0.0001;
+SET LOCAL seq_page_cost = 1000;
+CREATE TABLE cluster_aborted_upd (a int, b int);
+CREATE INDEX cluster_aborted_upd_idx ON cluster_aborted_upd (b);
+INSERT INTO cluster_aborted_upd VALUES (1, 3);
+SAVEPOINT sp;
+UPDATE cluster_aborted_upd SET a = 2;
+ROLLBACK TO sp;
+CLUSTER cluster_aborted_upd USING cluster_aborted_upd_idx;
+COMMIT;
+-- One row expected.
+SELECT count(*) FROM cluster_aborted_upd;
+SELECT * FROM cluster_aborted_upd;
+
-- clean up
DROP TABLE clustertest;
DROP TABLE clstr_1;
@@ -315,5 +333,6 @@ DROP TABLE clstr_2;
DROP TABLE clstr_3;
DROP TABLE clstr_4;
DROP TABLE clstr_expression;
+DROP TABLE cluster_aborted_upd;
DROP USER regress_clstr_user;
[parent: db45e1f1039e]