36#include "catalog/pg_opfamily_d.h"
55#define InvalidBtreeLevel ((uint32) InvalidBlockNumber)
56#define BTreeTupleGetNKeyAtts(itup, rel) \
57 Min(IndexRelationGetNumberOfKeyAttributes(rel), BTreeTupleGetNAtts(itup, rel))
180 void *
state,
bool readonly);
182 bool heapkeyspace,
bool readonly,
bool heapallindexed,
183 bool rootdescend,
bool checkunique);
214 bool tupleIsAlive,
void *checkstate);
257 args.heapallindexed =
false;
258 args.rootdescend =
false;
259 args.parentcheck =
false;
260 args.checkunique =
false;
289 args.heapallindexed =
false;
290 args.rootdescend =
false;
291 args.parentcheck =
true;
292 args.checkunique =
false;
320 (
errcode(ERRCODE_INDEX_CORRUPTED),
321 errmsg(
"index \"%s\" lacks a main relation fork",
326 if (allequalimage && !heapkeyspace)
328 (
errcode(ERRCODE_INDEX_CORRUPTED),
329 errmsg(
"index \"%s\" metapage has equalimage field set on unsupported nbtree version",
333 bool has_interval_ops =
false;
338 has_interval_ops =
true;
340 (
errcode(ERRCODE_INDEX_CORRUPTED),
341 errmsg(
"index \"%s\" metapage incorrectly indicates that deduplication is safe",
344 ?
errhint(
"This is known of \"interval\" indexes last built on a version predating 2023-11.")
351 args->heapallindexed, args->rootdescend, args->checkunique);
379 bool readonly,
bool heapallindexed,
bool rootdescend,
389 elog(
DEBUG1,
"verifying consistency of tree structure for index \"%s\"",
392 elog(
DEBUG1,
"verifying consistency of tree structure for index \"%s\" with cross-level checks",
406 state->heaprel = heaprel;
407 state->heapkeyspace = heapkeyspace;
408 state->readonly = readonly;
409 state->heapallindexed = heapallindexed;
410 state->rootdescend = rootdescend;
411 state->checkunique = checkunique;
414 if (
state->heapallindexed)
434 state->heaptuplespresent = 0;
459 state->snapshot->xmin))
462 errmsg(
"index \"%s\" cannot be verified using transaction snapshot",
471 if (
state->checkunique)
480 if (
state->rootdescend && !
state->heapkeyspace)
483 errmsg(
"cannot verify that tuples from index \"%s\" can each be found by an independent index search",
485 errhint(
"Only B-Tree version 4 indexes support rootdescend verification.")));
510 errdetail_internal(
"Fast root block %u (level %u) differs from true root block %u (level %u).",
534 errmsg(
"index \"%s\" has no valid pages on level below %u or first level",
543 if (
state->heapallindexed)
584 elog(
DEBUG1,
"verifying that tuples from index \"%s\" are present in \"%s\"",
645 " (true root level)" : level.
level == 0 ?
" (leaf level)" :
"");
648 state->previncompletesplit =
false;
656 state->targetblock = current;
678 errmsg(
"downlink or sibling link points to deleted block in index \"%s\"",
686 errmsg(
"block %u fell off the end of index \"%s\"",
709 errmsg(
"block %u is not leftmost in index \"%s\"",
715 errmsg(
"block %u is not true root in index \"%s\"",
776 errmsg(
"leftmost down link for level points to block in index \"%s\" whose level is not one level down",
778 errdetail_internal(
"Block pointed to=%u expected level=%u level in pointed to block=%u.",
790 errmsg(
"circular link chain found in block %u of index \"%s\"",
835 while (current !=
P_NONE);
859 tid,
state->snapshot, slot);
894 if (
lVis->postingIndex >= 0)
902 errmsg(
"index uniqueness is violated for index \"%s\"",
904 errdetail(
"Index %s%s and%s%s (point to heap %s and %s) page lsn=%X/%08X.",
948 lVis->blkno = targetblock;
949 lVis->offset = offset;
950 lVis->postingIndex =
i;
975 lVis->blkno = targetblock;
976 lVis->offset = offset;
978 lVis->postingIndex = -1;
984 lVis->blkno != targetblock)
988 if (
lVis->postingIndex >= 0)
992 errmsg(
"index uniqueness can not be checked for index tid=(%u,%u) in index \"%s\"",
995 errdetail(
"It doesn't have visible heap tids and key is equal to the tid=(%u,%u)%s (points to heap tid=(%u,%u)).",
999 errhint(
"VACUUM the table and repeat the check.")));
1047 errmsg_internal(
"harmless interrupted page deletion detected in index \"%s\"",
1106 if (!
state->readonly)
1140 state->checkstrategy);
1173 errmsg_internal(
"harmless concurrent page split detected in index \"%s\"",
1177 state->targetblock)));
1193 errmsg(
"left link/right link pair in index \"%s\" not in agreement",
1251 elog(
DEBUG2,
"verifying %u items on %s block %u", max,
1272 errmsg(
"wrong number of high key index tuple attributes in index \"%s\"",
1308 state->target, offset);
1321 errmsg(
"index tuple size does not equal lp_len in index \"%s\"",
1324 state->targetblock, offset,
1327 errhint(
"This could be a torn page problem.")));
1345 errmsg(
"wrong number of index tuple attributes in index \"%s\"",
1395 errmsg(
"could not find tuple using search from root page in index \"%s\"",
1424 errmsg_internal(
"posting list contains misplaced TID in index \"%s\"",
1477 errmsg(
"index row size %zu exceeds maximum for index \"%s\"",
1565 scantid =
skey->scantid;
1584 errmsg(
"high key invariant violated for index \"%s\"",
1593 skey->scantid = scantid;
1630 errmsg(
"item order invariant violated for index \"%s\"",
1632 errdetail_internal(
"Lower index tid=%s (points to %s tid=%s) higher index tid=%s (points to %s tid=%s) page lsn=%X/%08X.",
1648 if (
state->checkunique &&
state->indexinfo->ii_Unique &&
1657 if (
state->checkunique &&
state->indexinfo->ii_Unique &&
1661 scantid =
skey->scantid;
1683 lVis.postingIndex = -1;
1691 skey->scantid = scantid;
1731 if (!
state->readonly)
1747 errmsg(
"cross page item order invariant violated for index \"%s\"",
1750 state->targetblock, offset,
1758 if (
state->checkunique &&
state->indexinfo->ii_Unique &&
1763 elog(
DEBUG2,
"check cross page unique condition");
1800 errmsg(
"right block of leaf block is non-leaf for index \"%s\"",
1922 errmsg_internal(
"level %u sibling page in block %u of index \"%s\" was found deleted or half dead",
1924 errdetail_internal(
"Deleted page found when building scankey from right sibling.")));
2199 state->previncompletesplit =
false;
2207 errmsg(
"can't traverse from downlink %u to downlink %u of index \"%s\"",
2208 state->prevrightlink, downlink,
2224 errmsg(
"the first child of leftmost target page is not leftmost of its level in index \"%s\"",
2227 state->targetblock, blkno,
2235 errmsg(
"block found while following rightlinks from child of index \"%s\" has invalid level",
2237 errdetail_internal(
"Block pointed to=%u expected level=%u level in pointed to block=%u.",
2241 if ((!first && blkno ==
state->prevrightlink) || blkno == opaque->
btpo_prev)
2244 errmsg(
"circular link chain found in block %u of index \"%s\"",
2247 if (blkno != downlink && !
P_IGNORE(opaque))
2291 if (blkno == downlink)
2310 errmsg(
"child high key is greater than rightmost pivot key on target level in index \"%s\"",
2313 state->targetblock, blkno,
2340 errmsg(
"can't find left sibling high key in index \"%s\"",
2343 state->targetblock, blkno,
2345 itup =
state->lowkey;
2352 errmsg(
"mismatch between parent key and child high key in index \"%s\"",
2355 state->targetblock, blkno,
2361 if (blkno == downlink)
2405 state->target, downlinkoffnum);
2492 errmsg(
"downlink to deleted page found in index \"%s\"",
2533 errmsg(
"down-link lower bound invariant violated for index \"%s\"",
2535 errdetail_internal(
"Parent block=%u child index tid=(%u,%u) parent page lsn=%X/%08X.",
2603 errmsg_internal(
"harmless interrupted page split detected in index \"%s\"",
2625 errmsg(
"leaf index block lacks downlink in index \"%s\"",
2632 elog(
DEBUG1,
"checking for interrupted multi-level deletion due to missing downlink in index \"%s\"",
2650 if (
copaque->btpo_level != level - 1)
2653 errmsg_internal(
"downlink points to block in index \"%s\" whose level is not one level down",
2655 errdetail_internal(
"Top parent/under check block=%u block pointed to=%u expected level=%u level in pointed to block=%u.",
2657 level - 1,
copaque->btpo_level)));
2691 errmsg_internal(
"downlink to deleted leaf page found in index \"%s\"",
2693 errdetail_internal(
"Top parent/target block=%u leaf block=%u top parent/under check lsn=%X/%08X.",
2717 errmsg(
"internal index block lacks downlink in index \"%s\"",
2799 errmsg(
"heap tuple (%u,%u) from table \"%s\" lacks matching index tuple within index \"%s\"",
2805 ?
errhint(
"Retrying verification using the function bt_index_parent_check() might provide a more specific error.")
2808 state->heaptuplespresent++;
2876 if (
att->attbyval ||
att->attlen != -1 || isnull[
i])
2887 errmsg(
"external varlena datum in tuple that references heap row (%u,%u) in index \"%s\"",
3017 Assert(key->heapkeyspace && key->scantid !=
NULL);
3114 Assert(!key->nextkey && key->backward);
3120 if (!key->heapkeyspace)
3176 Assert(!key->nextkey && key->backward);
3199 Assert(!key->nextkey && key->backward);
3204 if (!key->heapkeyspace)
3237 Assert(!key->nextkey && key->backward);
3245 if (!key->heapkeyspace)
3304 state->checkstrategy);
3322 errmsg(
"invalid meta page found at block %u in index \"%s\"",
3334 errmsg(
"index \"%s\" meta page is corrupt",
3341 errmsg(
"version mismatch in index \"%s\": file version %d, "
3342 "current version %d, minimum supported version %d",
3363 errmsg_internal(
"invalid leaf page level %u for block %u in index \"%s\"",
3370 errmsg_internal(
"invalid internal page level 0 for block %u in index \"%s\"",
3398 errmsg(
"Number of items on block %u of index \"%s\" exceeds MaxIndexTuplesPerPage (%u)",
3405 errmsg(
"internal block %u in index \"%s\" lacks high key and/or at least one downlink",
3411 errmsg(
"non-rightmost leaf block %u in index \"%s\" lacks high key item",
3424 errmsg(
"internal page block %u in index \"%s\" is half-dead",
3426 errhint(
"This can be caused by an interrupted VACUUM in version 9.3 or older, before upgrade. Please REINDEX it.")));
3435 errmsg_internal(
"internal page block %u in index \"%s\" has garbage items",
3441 errmsg_internal(
"full transaction id page flag appears in non-deleted block %u in index \"%s\"",
3473 skey->backward =
true;
3501 errmsg(
"line pointer points past end of tuple space in index \"%s\"",
3517 errmsg(
"invalid line pointer storage in index \"%s\"",
3546 errmsg_internal(
"block %u or its right sibling block or child block in index \"%s\" has unexpected pivot tuple",
3553 errmsg_internal(
"block %u or its right sibling block or child block in index \"%s\" has unexpected non-pivot tuple",
3561 errmsg(
"block %u or its right sibling block or child block in index \"%s\" contains non-pivot tuple that lacks a heap TID",
3592 return &itup->
t_tid;
#define InvalidBlockNumber
static bool BlockNumberIsValid(BlockNumber blockNumber)
void bloom_free(bloom_filter *filter)
bloom_filter * bloom_create(int64 total_elems, int bloom_work_mem, uint64 seed)
double bloom_prop_bits_set(bloom_filter *filter)
bool bloom_lacks_element(bloom_filter *filter, unsigned char *elem, size_t len)
void bloom_add_element(bloom_filter *filter, unsigned char *elem, size_t len)
static Datum values[MAXATTR]
void UnlockReleaseBuffer(Buffer buffer)
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
#define RelationGetNumberOfBlocks(reln)
static Page BufferGetPage(Buffer buffer)
static void LockBuffer(Buffer buffer, BufferLockMode mode)
static bool BufferIsValid(Buffer bufnum)
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
static void * PageGetItem(PageData *page, const ItemIdData *itemId)
static XLogRecPtr PageGetLSN(const PageData *page)
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
#define Assert(condition)
int errmsg_internal(const char *fmt,...)
int errdetail_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
#define palloc0_object(type)
#define PG_MODULE_MAGIC_EXT(...)
#define PG_FUNCTION_INFO_V1(funcname)
#define PG_DETOAST_DATUM(datum)
#define PG_GETARG_BOOL(n)
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
#define TOAST_INDEX_TARGET
static TransactionId HeapTupleHeaderGetXmin(const HeapTupleHeaderData *tup)
IndexInfo * BuildIndexInfo(Relation index)
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
#define ItemIdGetLength(itemId)
#define ItemIdGetOffset(itemId)
#define ItemIdIsDead(itemId)
#define ItemIdIsUsed(itemId)
#define ItemIdIsRedirected(itemId)
#define ItemIdGetFlags(itemId)
int32 ItemPointerCompare(const ItemPointerData *arg1, const ItemPointerData *arg2)
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
static OffsetNumber ItemPointerGetOffsetNumberNoCheck(const ItemPointerData *pointer)
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
static BlockNumber ItemPointerGetBlockNumberNoCheck(const ItemPointerData *pointer)
static void ItemPointerCopy(const ItemPointerData *fromPointer, ItemPointerData *toPointer)
static bool ItemPointerIsValid(const ItemPointerData *pointer)
static bool IndexTupleHasVarwidths(const IndexTupleData *itup)
IndexTupleData * IndexTuple
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
static Size IndexTupleSize(const IndexTupleData *itup)
#define MaxIndexTuplesPerPage
void * MemoryContextAlloc(MemoryContext context, Size size)
void MemoryContextReset(MemoryContext context)
void pfree(void *pointer)
MemoryContext CurrentMemoryContext
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define CHECK_FOR_INTERRUPTS()
IndexTuple _bt_form_posting(IndexTuple base, const ItemPointerData *htids, int nhtids)
void _bt_relbuf(Relation rel, Buffer buf)
void _bt_checkpage(Relation rel, Buffer buf)
void _bt_metaversion(Relation rel, bool *heapkeyspace, bool *allequalimage)
#define P_HAS_FULLXID(opaque)
#define P_ISHALFDEAD(opaque)
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
static bool BTreeTupleIsPivot(IndexTuple itup)
#define BTREE_MIN_VERSION
#define P_HAS_GARBAGE(opaque)
#define BTPageGetOpaque(page)
#define P_ISDELETED(opaque)
static BlockNumber BTreeTupleGetTopParent(IndexTuple leafhikey)
#define MaxTIDsPerBTreePage
#define P_FIRSTDATAKEY(opaque)
#define P_RIGHTMOST(opaque)
#define P_INCOMPLETE_SPLIT(opaque)
static ItemPointer BTreeTupleGetPostingN(IndexTuple posting, int n)
static BlockNumber BTreeTupleGetDownLink(IndexTuple pivot)
static ItemPointer BTreeTupleGetMaxHeapTID(IndexTuple itup)
static bool BTreeTupleIsPosting(IndexTuple itup)
#define BTMaxItemSizeNoHeapTid
static ItemPointer BTreeTupleGetHeapTID(IndexTuple itup)
#define BTreeTupleGetNAtts(itup, rel)
BTStack _bt_search(Relation rel, Relation heaprel, BTScanInsert key, Buffer *bufP, int access)
OffsetNumber _bt_binsrch_insert(Relation rel, BTInsertState insertstate)
int32 _bt_compare(Relation rel, BTScanInsert key, Page page, OffsetNumber offnum)
void _bt_freestack(BTStack stack)
BTScanInsert _bt_mkscankey(Relation rel, IndexTuple itup)
bool _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
bool _bt_allequalimage(Relation rel, bool debugmessage)
#define InvalidOffsetNumber
#define OffsetNumberIsValid(offsetNumber)
#define OffsetNumberNext(offsetNumber)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
FormData_pg_attribute * Form_pg_attribute
#define ERRCODE_DATA_CORRUPTED
uint64 pg_prng_uint64(pg_prng_state *state)
pg_prng_state pg_global_prng_state
#define ERRCODE_T_R_SERIALIZATION_FAILURE
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
char * psprintf(const char *fmt,...)
static int cmp(const chr *x, const chr *y, size_t len)
static SMgrRelation RelationGetSmgr(Relation rel)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
#define IndexRelationGetNumberOfKeyAttributes(relation)
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Snapshot GetTransactionSnapshot(void)
void UnregisterSnapshot(Snapshot snapshot)
Snapshot RegisterSnapshot(Snapshot snapshot)
BufferAccessStrategy checkstrategy
BlockNumber prevrightlink
MemoryContext targetcontext
uint16 * ii_ExclusionStrats
struct HeapTupleData * rd_indextuple
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
static double table_index_build_scan(Relation table_rel, Relation index_rel, IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
static TableScanDesc table_beginscan_strat(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key, bool allow_strat, bool allow_sync)
static bool table_tuple_fetch_row_version(Relation rel, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot)
#define TransactionIdIsValid(xid)
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
static bool VARATT_CAN_MAKE_SHORT(const void *PTR)
static bool VARATT_IS_EXTERNAL(const void *PTR)
static Size VARSIZE(const void *PTR)
static char * VARDATA(const void *PTR)
static Size VARATT_CONVERTED_SHORT_SIZE(const void *PTR)
static bool VARATT_IS_COMPRESSED(const void *PTR)
static void SET_VARSIZE_SHORT(void *PTR, Size len)
void amcheck_lock_relation_and_check(Oid indrelid, Oid am_id, IndexDoCheckCallback check, LOCKMODE lockmode, void *state)
static bool offset_is_negative_infinity(BTPageOpaque opaque, OffsetNumber offset)
static bool bt_leftmost_ignoring_half_dead(BtreeCheckState *state, BlockNumber start, BTPageOpaque start_opaque)
static bool invariant_l_offset(BtreeCheckState *state, BTScanInsert key, OffsetNumber upperbound)
static ItemPointer BTreeTupleGetPointsToTID(IndexTuple itup)
static IndexTuple bt_posting_plain_tuple(IndexTuple itup, int n)
static void bt_target_page_check(BtreeCheckState *state)
static void bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace, bool readonly, bool heapallindexed, bool rootdescend, bool checkunique)
static void bt_report_duplicate(BtreeCheckState *state, BtreeLastVisibleEntry *lVis, ItemPointer nexttid, BlockNumber nblock, OffsetNumber noffset, int nposting)
static bool bt_pivot_tuple_identical(bool heapkeyspace, IndexTuple itup1, IndexTuple itup2)
static bool invariant_leq_offset(BtreeCheckState *state, BTScanInsert key, OffsetNumber upperbound)
Datum bt_index_parent_check(PG_FUNCTION_ARGS)
static void bt_child_highkey_check(BtreeCheckState *state, OffsetNumber target_downlinkoffnum, Page loaded_child, uint32 target_level)
static bool heap_entry_is_visible(BtreeCheckState *state, ItemPointer tid)
static BTScanInsert bt_mkscankey_pivotsearch(Relation rel, IndexTuple itup)
Datum bt_index_check(PG_FUNCTION_ARGS)
static BtreeLevel bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level)
static void bt_downlink_missing_check(BtreeCheckState *state, bool rightsplit, BlockNumber blkno, Page page)
static ItemId PageGetItemIdCareful(BtreeCheckState *state, BlockNumber block, Page page, OffsetNumber offset)
static void bt_tuple_present_callback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *checkstate)
static void bt_index_check_callback(Relation indrel, Relation heaprel, void *state, bool readonly)
static bool bt_rootdescend(BtreeCheckState *state, IndexTuple itup)
static BTScanInsert bt_right_page_check_scankey(BtreeCheckState *state, OffsetNumber *rightfirstoffset)
static bool invariant_g_offset(BtreeCheckState *state, BTScanInsert key, OffsetNumber lowerbound)
#define InvalidBtreeLevel
static Page palloc_btree_page(BtreeCheckState *state, BlockNumber blocknum)
static IndexTuple bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
static void bt_recheck_sibling_links(BtreeCheckState *state, BlockNumber btpo_prev_from_target, BlockNumber leftcurrent)
static ItemPointer BTreeTupleGetHeapTIDCareful(BtreeCheckState *state, IndexTuple itup, bool nonpivot)
#define BTreeTupleGetNKeyAtts(itup, rel)
static void bt_child_check(BtreeCheckState *state, BTScanInsert targetkey, OffsetNumber downlinkoffnum)
static void bt_entry_unique_check(BtreeCheckState *state, IndexTuple itup, BlockNumber targetblock, OffsetNumber offset, BtreeLastVisibleEntry *lVis)
static bool invariant_l_nontarget_offset(BtreeCheckState *state, BTScanInsert key, BlockNumber nontargetblock, Page nontarget, OffsetNumber upperbound)
#define IsolationUsesXactSnapshot()
#define LSN_FORMAT_ARGS(lsn)