From 003f5e93db6d292a696f92d7ea02a0dd83dea687 Mon Sep 17 00:00:00 2001 From: Peter Geoghegan Date: Sun, 7 Jun 2020 19:15:48 -0700 Subject: [PATCH v2] Avoid aborted update serialization anomalies. SSI's HeapCheckForSerializableConflictOut() test failed to correctly handle conditions involving a concurrent update that aborts in all cases. A HeapCheckForSerializableConflictOut() caller (typically from a SELECT statement) could end up using the same updater's XID for both the original tuple, and the successor tuple, missing the XID of the xact that created the original tuple entirely. This only happened when neither tuple from the chain was visible to the HeapCheckForSerializableConflictOut() caller. The observable symptoms of this bug were subtle. A pair of transactions could commit, with the later transaction failing to observe the effects of the earlier transaction (because of the confusion created by the aborted update). The window here was typically rather small, which is presumably why this bug went unnoticed for so long. It dates all the way back to commit dafaa3ef, which added SSI. To fix, make sure that we check both the original tuple's xmin (i.e. the XID of the transaction that create the tuple), as well as the updater's XID. Author: Peter Geoghegan Reported-By: Kyle Kingsbury Discussion: https://postgr.es/m/db7b729d-0226-d162-a126-8a8ab2dc4443@jepsen.io Backpatch: All supported versions --- src/backend/access/heap/heapam.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 94eb37d48d..4fbaa847c1 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -9007,6 +9007,12 @@ HeapCheckForSerializableConflictOut(bool visible, Relation relation, * while it is visible to us. The "visible" bool indicates whether the * tuple is visible to us, while HeapTupleSatisfiesVacuum checks what else * is going on with it. + * + * In the event of an updated tuple that is not visible to us, the xmin of + * the tuple (which must be a committed xid) will be used -- not the + * updater's xid (which may itself have committed, or even aborted). The + * successor tuple is often handled by another call here, at which point + * we'll check the updater's xid. */ htsvResult = HeapTupleSatisfiesVacuum(tuple, TransactionXmin, buffer); switch (htsvResult) @@ -9017,17 +9023,17 @@ HeapCheckForSerializableConflictOut(bool visible, Relation relation, xid = HeapTupleHeaderGetXmin(tuple->t_data); break; case HEAPTUPLE_RECENTLY_DEAD: - if (!visible) - return; - xid = HeapTupleHeaderGetUpdateXid(tuple->t_data); - break; case HEAPTUPLE_DELETE_IN_PROGRESS: - xid = HeapTupleHeaderGetUpdateXid(tuple->t_data); + if (visible) + xid = HeapTupleHeaderGetUpdateXid(tuple->t_data); + else + xid = HeapTupleHeaderGetXmin(tuple->t_data); break; case HEAPTUPLE_INSERT_IN_PROGRESS: xid = HeapTupleHeaderGetXmin(tuple->t_data); break; case HEAPTUPLE_DEAD: + Assert(!visible); return; default: -- 2.25.1