Skip to content

Commit d441cff

Browse files
committed
Fix traversal of half-frozen update chains
When some tuple versions in an update chain are frozen due to them being older than freeze_min_age, the xmax/xmin trail can become broken. This breaks HOT (and probably other things). A subsequent VACUUM can break things in more serious ways, such as leaving orphan heap-only tuples whose root HOT redirect items were removed. This can be seen because index creation (or REINDEX) complain like ERROR: XX000: failed to find parent tuple for heap-only tuple at (0,7) in table "t" Because of relfrozenxid contraints, we cannot avoid the freezing of the early tuples, so we must cope with the results: whenever we see an Xmin of FrozenTransactionId, consider it a match for whatever the previous Xmax value was. This problem seems to have appeared in 9.3 with multixact changes, though strictly speaking it seems unrelated. Since 9.4 we have commit 37484ad "Change the way we mark tuples as frozen", so the fix is simple: just compare the raw Xmin (still stored in the tuple header, since freezing merely set an infomask bit) to the Xmax. But in 9.3 we rewrite the Xmin value to FrozenTransactionId, so the original value is lost and we have nothing to compare the Xmax with. To cope with that case we need to compare the Xmin with FrozenXid, assume it's a match, and hope for the best. Sadly, since you can pg_upgrade a 9.3 instance containing half-frozen pages to newer releases, we need to keep the old check in newer versions too, which seems a bit brittle; I hope we can somehow get rid of that. I didn't optimize the new function for performance. The new coding is probably a bit slower than before, since there is a function call rather than a straight comparison, but I'd rather have it work correctly than be fast but wrong. This is a followup after 20b6552 fixed a few related problems. Apparently, in 9.6 and up there are more ways to get into trouble, but in 9.3 - 9.5 I cannot reproduce a problem anymore with this patch, so there must be a separate bug. Reported-by: Peter Geoghegan Diagnosed-by: Peter Geoghegan, Michael Paquier, Daniel Wood, Yi Wen Wong, Álvaro Discussion: https://postgr.es/m/CAH2-Wznm4rCrhFAiwKPWTpEw2bXDtgROZK7jWWGucXeH3D1fmA@mail.gmail.com
1 parent 9d742e1 commit d441cff

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

src/backend/access/heap/heapam.c

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,8 +2046,7 @@ heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer,
20462046
* broken.
20472047
*/
20482048
if (TransactionIdIsValid(prev_xmax) &&
2049-
!TransactionIdEquals(prev_xmax,
2050-
HeapTupleHeaderGetXmin(heapTuple->t_data)))
2049+
!HeapTupleUpdateXmaxMatchesXmin(prev_xmax, heapTuple->t_data))
20512050
break;
20522051

20532052
/*
@@ -2230,7 +2229,7 @@ heap_get_latest_tid(Relation relation,
22302229
* tuple. Check for XMIN match.
22312230
*/
22322231
if (TransactionIdIsValid(priorXmax) &&
2233-
!TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(tp.t_data)))
2232+
!HeapTupleUpdateXmaxMatchesXmin(priorXmax, tp.t_data))
22342233
{
22352234
UnlockReleaseBuffer(buffer);
22362235
break;
@@ -2262,6 +2261,50 @@ heap_get_latest_tid(Relation relation,
22622261
} /* end of loop */
22632262
}
22642263

2264+
/*
2265+
* HeapTupleUpdateXmaxMatchesXmin - verify update chain xmax/xmin lineage
2266+
*
2267+
* Given the new version of a tuple after some update, verify whether the
2268+
* given Xmax (corresponding to the previous version) matches the tuple's
2269+
* Xmin, taking into account that the Xmin might have been frozen after the
2270+
* update.
2271+
*/
2272+
bool
2273+
HeapTupleUpdateXmaxMatchesXmin(TransactionId xmax, HeapTupleHeader htup)
2274+
{
2275+
TransactionId xmin = HeapTupleHeaderGetXmin(htup);
2276+
2277+
/*
2278+
* If the xmax of the old tuple is identical to the xmin of the new one,
2279+
* it's a match.
2280+
*/
2281+
if (TransactionIdEquals(xmax, xmin))
2282+
return true;
2283+
2284+
/*
2285+
* If the Xmin that was in effect prior to a freeze matches the Xmax,
2286+
* it's good too.
2287+
*/
2288+
if (HeapTupleHeaderXminFrozen(htup) &&
2289+
TransactionIdEquals(HeapTupleHeaderGetRawXmin(htup), xmax))
2290+
return true;
2291+
2292+
/*
2293+
* When a tuple is frozen, the original Xmin is lost, but we know it's a
2294+
* committed transaction. So unless the Xmax is InvalidXid, we don't know
2295+
* for certain that there is a match, but there may be one; and we must
2296+
* return true so that a HOT chain that is half-frozen can be walked
2297+
* correctly.
2298+
*
2299+
* We no longer freeze tuples this way, but we must keep this in order to
2300+
* interpret pre-pg_upgrade pages correctly.
2301+
*/
2302+
if (TransactionIdEquals(xmin, FrozenTransactionId) &&
2303+
TransactionIdIsValid(xmax))
2304+
return true;
2305+
2306+
return false;
2307+
}
22652308

22662309
/*
22672310
* UpdateXmaxHintBits - update tuple hint bits after xmax transaction ends
@@ -5719,8 +5762,7 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
57195762
* end of the chain, we're done, so return success.
57205763
*/
57215764
if (TransactionIdIsValid(priorXmax) &&
5722-
!TransactionIdEquals(HeapTupleHeaderGetXmin(mytup.t_data),
5723-
priorXmax))
5765+
!HeapTupleUpdateXmaxMatchesXmin(priorXmax, mytup.t_data))
57245766
{
57255767
result = HeapTupleMayBeUpdated;
57265768
goto out_locked;

src/backend/access/heap/pruneheap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ heap_prune_chain(Relation relation, Buffer buffer, OffsetNumber rootoffnum,
474474
* Check the tuple XMIN against prior XMAX, if any
475475
*/
476476
if (TransactionIdIsValid(priorXmax) &&
477-
!TransactionIdEquals(HeapTupleHeaderGetXmin(htup), priorXmax))
477+
!HeapTupleUpdateXmaxMatchesXmin(priorXmax, htup))
478478
break;
479479

480480
/*
@@ -814,7 +814,7 @@ heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
814814
htup = (HeapTupleHeader) PageGetItem(page, lp);
815815

816816
if (TransactionIdIsValid(priorXmax) &&
817-
!TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(htup)))
817+
!HeapTupleUpdateXmaxMatchesXmin(priorXmax, htup))
818818
break;
819819

820820
/* Remember the root line pointer for this item */

src/backend/executor/execMain.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,8 +2278,7 @@ EvalPlanQualFetch(EState *estate, Relation relation, int lockmode,
22782278
* atomic, and Xmin never changes in an existing tuple, except to
22792279
* invalid or frozen, and neither of those can match priorXmax.)
22802280
*/
2281-
if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2282-
priorXmax))
2281+
if (!HeapTupleUpdateXmaxMatchesXmin(priorXmax, tuple.t_data))
22832282
{
22842283
ReleaseBuffer(buffer);
22852284
return NULL;
@@ -2426,8 +2425,7 @@ EvalPlanQualFetch(EState *estate, Relation relation, int lockmode,
24262425
/*
24272426
* As above, if xmin isn't what we're expecting, do nothing.
24282427
*/
2429-
if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2430-
priorXmax))
2428+
if (!HeapTupleUpdateXmaxMatchesXmin(priorXmax, tuple.t_data))
24312429
{
24322430
ReleaseBuffer(buffer);
24332431
return NULL;

src/include/access/heapam.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ extern void heap_get_latest_tid(Relation relation, Snapshot snapshot,
145145
ItemPointer tid);
146146
extern void setLastTid(const ItemPointer tid);
147147

148+
extern bool HeapTupleUpdateXmaxMatchesXmin(TransactionId xmax,
149+
HeapTupleHeader htup);
150+
148151
extern BulkInsertState GetBulkInsertState(void);
149152
extern void FreeBulkInsertState(BulkInsertState);
150153

0 commit comments

Comments
 (0)