From 35e596da9c83e4a867bdd2232356cfc331fd14bd Mon Sep 17 00:00:00 2001 From: Peter Geoghegan Date: Sat, 30 Oct 2021 09:33:50 -0700 Subject: [PATCH v1 2/2] Add index-points-to-LP_UNUSED-item assertions. --- src/backend/access/heap/heapam.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 2da2be169..3f57d750b 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -7446,6 +7446,34 @@ heap_index_delete_tuples(Relation rel, TM_IndexDeleteOp *delstate) maxoff = PageGetMaxOffsetNumber(page); } +#ifdef USE_ASSERT_CHECKING + + /* + * Try to catch index corruption involving a TID from caller's index + * that points to an LP_UNUSED item in the heap, or points past the + * end of the heap page's line pointer array. This is always wrong + * because nothing prevents recycling of the heap TID/page offset for + * some totally unrelated logical row. Also try to catch TIDs that + * point to a heap-only tuple directly. + */ + { + OffsetNumber indexpagehoffnum = ItemPointerGetOffsetNumber(htid); + ItemId iid; + HeapTupleHeader htup; + + Assert(indexpagehoffnum <= maxoff); + iid = PageGetItemId(page, indexpagehoffnum); + + Assert(ItemIdIsUsed(iid)); + if (ItemIdHasStorage(iid)) + { + Assert(ItemIdIsNormal(iid)); + htup = (HeapTupleHeader) PageGetItem(page, iid); + Assert(!HeapTupleHeaderIsHeapOnly(htup)); + } + } +#endif + if (istatus->knowndeletable) Assert(!delstate->bottomup && !istatus->promising); else -- 2.30.2