Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bb42bfb

Browse files
committed
Assert redirect pointers are sensible after heap_page_prune().
Corruption of redirect item pointers often only becomes visible well after being corrupted, as e.g. bug #17255 shows: In the original reproducer, gigabyte of WAL were between the source of the corruption and the corruption becoming visible. To make it easier to find / prevent such bugs, verify whether redirect pointers are sensible at the end of heap_page_prune_execute(). 5cd7eb1 introduced related assertions while modifying the page, but they can't easily detect marking the target of an existing redirect as unused. Sometimes the corruption will be detected later, but that's harder to diagnose. Author: Andres Freund <andres@andres@anarazel.de> Reviewed-By: Peter Geoghegan <pg@bowt.ie> Discussion: https://postgr.es/m/20211122175914.ayk6gg6nvdwuhrzb@alap3.anarazel.de
1 parent 18b87b2 commit bb42bfb

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/backend/access/heap/pruneheap.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static void heap_prune_record_redirect(PruneState *prstate,
8888
OffsetNumber offnum, OffsetNumber rdoffnum);
8989
static void heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum);
9090
static void heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum);
91+
static void page_verify_redirects(Page page);
9192

9293

9394
/*
@@ -959,6 +960,10 @@ heap_page_prune_execute(Buffer buffer,
959960
* indexes when an entire HOT chain becomes dead. A heap-only tuple
960961
* can never become LP_DEAD; an LP_REDIRECT item or a regular heap
961962
* tuple can.
963+
*
964+
* This check may miss problems, e.g. the target of a redirect could
965+
* be marked as unused subsequently. The page_verify_redirects() check
966+
* below will catch such problems.
962967
*/
963968
tolp = PageGetItemId(page, tooff);
964969
Assert(ItemIdHasStorage(tolp) && ItemIdIsNormal(tolp));
@@ -1028,6 +1033,58 @@ heap_page_prune_execute(Buffer buffer,
10281033
* whether it has free pointers.
10291034
*/
10301035
PageRepairFragmentation(page);
1036+
1037+
/*
1038+
* Now that the page has been modified, assert that redirect items still
1039+
* point to valid targets.
1040+
*/
1041+
page_verify_redirects(page);
1042+
}
1043+
1044+
1045+
/*
1046+
* If built with assertions, verify that all LP_REDIRECT items point to a
1047+
* valid item.
1048+
*
1049+
* One way that bugs related to HOT pruning show is redirect items pointing to
1050+
* removed tuples. It's not trivial to reliably check that marking an item
1051+
* unused will not orphan a redirect item during heap_prune_chain() /
1052+
* heap_page_prune_execute(), so we additionally check the whole page after
1053+
* pruning. Without this check such bugs would typically only cause asserts
1054+
* later, potentially well after the corruption has been introduced.
1055+
*
1056+
* Also check comments in heap_page_prune_execute()'s redirection loop.
1057+
*/
1058+
static void
1059+
page_verify_redirects(Page page)
1060+
{
1061+
#ifdef USE_ASSERT_CHECKING
1062+
OffsetNumber offnum;
1063+
OffsetNumber maxoff;
1064+
1065+
maxoff = PageGetMaxOffsetNumber(page);
1066+
for (offnum = FirstOffsetNumber;
1067+
offnum <= maxoff;
1068+
offnum = OffsetNumberNext(offnum))
1069+
{
1070+
ItemId itemid = PageGetItemId(page, offnum);
1071+
OffsetNumber targoff;
1072+
ItemId targitem;
1073+
HeapTupleHeader htup;
1074+
1075+
if (!ItemIdIsRedirected(itemid))
1076+
continue;
1077+
1078+
targoff = ItemIdGetRedirect(itemid);
1079+
targitem = PageGetItemId(page, targoff);
1080+
1081+
Assert(ItemIdIsUsed(targitem));
1082+
Assert(ItemIdIsNormal(targitem));
1083+
Assert(ItemIdHasStorage(targitem));
1084+
htup = (HeapTupleHeader) PageGetItem(page, targitem);
1085+
Assert(HeapTupleHeaderIsHeapOnly(htup));
1086+
}
1087+
#endif
10311088
}
10321089

10331090

0 commit comments

Comments
 (0)