Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb052d52

Browse files
committed
Fix traversal of half-frozen update chains
When some tuple versions in an update chain are frozen due to them beingolder than freeze_min_age, the xmax/xmin trail can become broken. Thisbreaks HOT (and probably other things). A subsequent VACUUM can breakthings in more serious ways, such as leaving orphan heap-only tupleswhose root HOT redirect items were removed. This can be seen becauseindex 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 theearly tuples, so we must cope with the results: whenever we see an Xminof FrozenTransactionId, consider it a match for whatever the previousXmax 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 commit37484ad "Change the way we mark tuples asfrozen", so the fix is simple: just compare the raw Xmin (still storedin the tuple header, since freezing merely set an infomask bit) to theXmax. But in 9.3 we rewrite the Xmin value to FrozenTransactionId, sothe 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 canpg_upgrade a 9.3 instance containing half-frozen pages to newerreleases, we need to keep the old check in newer versions too, whichseems a bit brittle; I hope we can somehow get rid of that.I didn't optimize the new function for performance. The new coding isprobably a bit slower than before, since there is a function call ratherthan a straight comparison, but I'd rather have it work correctly thanbe fast but wrong.This is a followup after20b6552 fixed a few related problems.Apparently, in 9.6 and up there are more ways to get into trouble, butin 9.3 - 9.5 I cannot reproduce a problem anymore with this patch, sothere must be a separate bug.Reported-by: Peter GeogheganDiagnosed-by: Peter Geoghegan, Michael Paquier, Daniel Wood,Yi Wen Wong, ÁlvaroDiscussion:https://postgr.es/m/CAH2-Wznm4rCrhFAiwKPWTpEw2bXDtgROZK7jWWGucXeH3D1fmA@mail.gmail.com
1 parentb24f15f commitb052d52

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

‎src/backend/access/heap/heapam.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,8 +1718,7 @@ heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer,
17181718
* broken.
17191719
*/
17201720
if (TransactionIdIsValid(prev_xmax)&&
1721-
!TransactionIdEquals(prev_xmax,
1722-
HeapTupleHeaderGetXmin(heapTuple->t_data)))
1721+
!HeapTupleUpdateXmaxMatchesXmin(prev_xmax,heapTuple->t_data))
17231722
break;
17241723

17251724
/*
@@ -1888,7 +1887,7 @@ heap_get_latest_tid(Relation relation,
18881887
* tuple. Check for XMIN match.
18891888
*/
18901889
if (TransactionIdIsValid(priorXmax)&&
1891-
!TransactionIdEquals(priorXmax,HeapTupleHeaderGetXmin(tp.t_data)))
1890+
!HeapTupleUpdateXmaxMatchesXmin(priorXmax,tp.t_data))
18921891
{
18931892
UnlockReleaseBuffer(buffer);
18941893
break;
@@ -1920,6 +1919,39 @@ heap_get_latest_tid(Relation relation,
19201919
}/* end of loop */
19211920
}
19221921

1922+
/*
1923+
* HeapTupleUpdateXmaxMatchesXmin - verify update chain xmax/xmin lineage
1924+
*
1925+
* Given the new version of a tuple after some update, verify whether the
1926+
* given Xmax (corresponding to the previous version) matches the tuple's
1927+
* Xmin, taking into account that the Xmin might have been frozen after the
1928+
* update.
1929+
*/
1930+
bool
1931+
HeapTupleUpdateXmaxMatchesXmin(TransactionIdxmax,HeapTupleHeaderhtup)
1932+
{
1933+
TransactionIdxmin=HeapTupleHeaderGetXmin(htup);
1934+
1935+
/*
1936+
* If the xmax of the old tuple is identical to the xmin of the new one,
1937+
* it's a match.
1938+
*/
1939+
if (TransactionIdEquals(xmax,xmin))
1940+
return true;
1941+
1942+
/*
1943+
* When a tuple is frozen, the original Xmin is lost, but we know it's a
1944+
* committed transaction. So unless the Xmax is InvalidXid, we don't know
1945+
* for certain that there is a match, but there may be one; and we must
1946+
* return true so that a HOT chain that is half-frozen can be walked
1947+
* correctly.
1948+
*/
1949+
if (TransactionIdEquals(xmin,FrozenTransactionId)&&
1950+
TransactionIdIsValid(xmax))
1951+
return true;
1952+
1953+
return false;
1954+
}
19231955

19241956
/*
19251957
* UpdateXmaxHintBits - update tuple hint bits after xmax transaction ends
@@ -5045,8 +5077,7 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
50455077
* the end of the chain, we're done, so return success.
50465078
*/
50475079
if (TransactionIdIsValid(priorXmax)&&
5048-
!TransactionIdEquals(HeapTupleHeaderGetXmin(mytup.t_data),
5049-
priorXmax))
5080+
!HeapTupleUpdateXmaxMatchesXmin(priorXmax,mytup.t_data))
50505081
{
50515082
UnlockReleaseBuffer(buf);
50525083
returnHeapTupleMayBeUpdated;
@@ -5500,7 +5531,10 @@ FreezeMultiXactId(MultiXactId multi, uint16 t_infomask,
55005531
if (TransactionIdPrecedes(xid,cutoff_xid))
55015532
{
55025533
if (TransactionIdDidCommit(xid))
5534+
{
5535+
xid=FrozenTransactionId;
55035536
*flags=FRM_MARK_COMMITTED |FRM_RETURN_IS_XID;
5537+
}
55045538
else
55055539
{
55065540
*flags |=FRM_INVALIDATE_XMAX;

‎src/backend/access/heap/pruneheap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ heap_prune_chain(Relation relation, Buffer buffer, OffsetNumber rootoffnum,
435435
* Check the tuple XMIN against prior XMAX, if any
436436
*/
437437
if (TransactionIdIsValid(priorXmax)&&
438-
!TransactionIdEquals(HeapTupleHeaderGetXmin(htup),priorXmax))
438+
!HeapTupleUpdateXmaxMatchesXmin(priorXmax,htup))
439439
break;
440440

441441
/*
@@ -774,7 +774,7 @@ heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
774774
htup= (HeapTupleHeader)PageGetItem(page,lp);
775775

776776
if (TransactionIdIsValid(priorXmax)&&
777-
!TransactionIdEquals(priorXmax,HeapTupleHeaderGetXmin(htup)))
777+
!HeapTupleUpdateXmaxMatchesXmin(priorXmax,htup))
778778
break;
779779

780780
/* 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
@@ -2019,8 +2019,7 @@ EvalPlanQualFetch(EState *estate, Relation relation, int lockmode,
20192019
* buffer's content lock, since xmin never changes in an existing
20202020
* tuple.)
20212021
*/
2022-
if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2023-
priorXmax))
2022+
if (!HeapTupleUpdateXmaxMatchesXmin(priorXmax,tuple.t_data))
20242023
{
20252024
ReleaseBuffer(buffer);
20262025
returnNULL;
@@ -2138,8 +2137,7 @@ EvalPlanQualFetch(EState *estate, Relation relation, int lockmode,
21382137
/*
21392138
* As above, if xmin isn't what we're expecting, do nothing.
21402139
*/
2141-
if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2142-
priorXmax))
2140+
if (!HeapTupleUpdateXmaxMatchesXmin(priorXmax,tuple.t_data))
21432141
{
21442142
ReleaseBuffer(buffer);
21452143
returnNULL;

‎src/include/access/heapam.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ extern void heap_get_latest_tid(Relation relation, Snapshot snapshot,
127127
ItemPointertid);
128128
externvoidsetLastTid(constItemPointertid);
129129

130+
externboolHeapTupleUpdateXmaxMatchesXmin(TransactionIdxmax,
131+
HeapTupleHeaderhtup);
132+
130133
externBulkInsertStateGetBulkInsertState(void);
131134
externvoidFreeBulkInsertState(BulkInsertState);
132135

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp