Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * verify_nbtree.c
4 : * Verifies the integrity of nbtree indexes based on invariants.
5 : *
6 : * For B-Tree indexes, verification includes checking that each page in the
7 : * target index has items in logical order as reported by an insertion scankey
8 : * (the insertion scankey sort-wise NULL semantics are needed for
9 : * verification).
10 : *
11 : * When index-to-heap verification is requested, a Bloom filter is used to
12 : * fingerprint all tuples in the target index, as the index is traversed to
13 : * verify its structure. A heap scan later uses Bloom filter probes to verify
14 : * that every visible heap tuple has a matching index tuple.
15 : *
16 : *
17 : * Copyright (c) 2017-2025, PostgreSQL Global Development Group
18 : *
19 : * IDENTIFICATION
20 : * contrib/amcheck/verify_nbtree.c
21 : *
22 : *-------------------------------------------------------------------------
23 : */
24 : #include "postgres.h"
25 :
26 : #include "access/heaptoast.h"
27 : #include "access/htup_details.h"
28 : #include "access/nbtree.h"
29 : #include "access/table.h"
30 : #include "access/tableam.h"
31 : #include "access/transam.h"
32 : #include "access/xact.h"
33 : #include "verify_common.h"
34 : #include "catalog/index.h"
35 : #include "catalog/pg_am.h"
36 : #include "catalog/pg_opfamily_d.h"
37 : #include "common/pg_prng.h"
38 : #include "lib/bloomfilter.h"
39 : #include "miscadmin.h"
40 : #include "storage/smgr.h"
41 : #include "utils/guc.h"
42 : #include "utils/memutils.h"
43 : #include "utils/snapmgr.h"
44 :
45 :
46 656 : PG_MODULE_MAGIC_EXT(
47 : .name = "amcheck",
48 : .version = PG_VERSION
49 : );
50 :
51 : /*
52 : * A B-Tree cannot possibly have this many levels, since there must be one
53 : * block per level, which is bound by the range of BlockNumber:
54 : */
55 : #define InvalidBtreeLevel ((uint32) InvalidBlockNumber)
56 : #define BTreeTupleGetNKeyAtts(itup, rel) \
57 : Min(IndexRelationGetNumberOfKeyAttributes(rel), BTreeTupleGetNAtts(itup, rel))
58 :
59 : /*
60 : * State associated with verifying a B-Tree index
61 : *
62 : * target is the point of reference for a verification operation.
63 : *
64 : * Other B-Tree pages may be allocated, but those are always auxiliary (e.g.,
65 : * they are current target's child pages). Conceptually, problems are only
66 : * ever found in the current target page (or for a particular heap tuple during
67 : * heapallindexed verification). Each page found by verification's left/right,
68 : * top/bottom scan becomes the target exactly once.
69 : */
70 : typedef struct BtreeCheckState
71 : {
72 : /*
73 : * Unchanging state, established at start of verification:
74 : */
75 :
76 : /* B-Tree Index Relation and associated heap relation */
77 : Relation rel;
78 : Relation heaprel;
79 : /* rel is heapkeyspace index? */
80 : bool heapkeyspace;
81 : /* ShareLock held on heap/index, rather than AccessShareLock? */
82 : bool readonly;
83 : /* Also verifying heap has no unindexed tuples? */
84 : bool heapallindexed;
85 : /* Also making sure non-pivot tuples can be found by new search? */
86 : bool rootdescend;
87 : /* Also check uniqueness constraint if index is unique */
88 : bool checkunique;
89 : /* Per-page context */
90 : MemoryContext targetcontext;
91 : /* Buffer access strategy */
92 : BufferAccessStrategy checkstrategy;
93 :
94 : /*
95 : * Info for uniqueness checking. Fill these fields once per index check.
96 : */
97 : IndexInfo *indexinfo;
98 : Snapshot snapshot;
99 :
100 : /*
101 : * Mutable state, for verification of particular page:
102 : */
103 :
104 : /* Current target page */
105 : Page target;
106 : /* Target block number */
107 : BlockNumber targetblock;
108 : /* Target page's LSN */
109 : XLogRecPtr targetlsn;
110 :
111 : /*
112 : * Low key: high key of left sibling of target page. Used only for child
113 : * verification. So, 'lowkey' is kept only when 'readonly' is set.
114 : */
115 : IndexTuple lowkey;
116 :
117 : /*
118 : * The rightlink and incomplete split flag of block one level down to the
119 : * target page, which was visited last time via downlink from target page.
120 : * We use it to check for missing downlinks.
121 : */
122 : BlockNumber prevrightlink;
123 : bool previncompletesplit;
124 :
125 : /*
126 : * Mutable state, for optional heapallindexed verification:
127 : */
128 :
129 : /* Bloom filter fingerprints B-Tree index */
130 : bloom_filter *filter;
131 : /* Debug counter */
132 : int64 heaptuplespresent;
133 : } BtreeCheckState;
134 :
135 : /*
136 : * Starting point for verifying an entire B-Tree index level
137 : */
138 : typedef struct BtreeLevel
139 : {
140 : /* Level number (0 is leaf page level). */
141 : uint32 level;
142 :
143 : /* Left most block on level. Scan of level begins here. */
144 : BlockNumber leftmost;
145 :
146 : /* Is this level reported as "true" root level by meta page? */
147 : bool istruerootlevel;
148 : } BtreeLevel;
149 :
150 : /*
151 : * Information about the last visible entry with current B-tree key. Used
152 : * for validation of the unique constraint.
153 : */
154 : typedef struct BtreeLastVisibleEntry
155 : {
156 : BlockNumber blkno; /* Index block */
157 : OffsetNumber offset; /* Offset on index block */
158 : int postingIndex; /* Number in the posting list (-1 for
159 : * non-deduplicated tuples) */
160 : ItemPointer tid; /* Heap tid */
161 : } BtreeLastVisibleEntry;
162 :
163 : /*
164 : * arguments for the bt_index_check_callback callback
165 : */
166 : typedef struct BTCallbackState
167 : {
168 : bool parentcheck;
169 : bool heapallindexed;
170 : bool rootdescend;
171 : bool checkunique;
172 : } BTCallbackState;
173 :
174 186 : PG_FUNCTION_INFO_V1(bt_index_check);
175 128 : PG_FUNCTION_INFO_V1(bt_index_parent_check);
176 :
177 : static void bt_index_check_callback(Relation indrel, Relation heaprel,
178 : void *state, bool readonly);
179 : static void bt_check_every_level(Relation rel, Relation heaprel,
180 : bool heapkeyspace, bool readonly, bool heapallindexed,
181 : bool rootdescend, bool checkunique);
182 : static BtreeLevel bt_check_level_from_leftmost(BtreeCheckState *state,
183 : BtreeLevel level);
184 : static bool bt_leftmost_ignoring_half_dead(BtreeCheckState *state,
185 : BlockNumber start,
186 : BTPageOpaque start_opaque);
187 : static void bt_recheck_sibling_links(BtreeCheckState *state,
188 : BlockNumber btpo_prev_from_target,
189 : BlockNumber leftcurrent);
190 : static bool heap_entry_is_visible(BtreeCheckState *state, ItemPointer tid);
191 : static void bt_report_duplicate(BtreeCheckState *state,
192 : BtreeLastVisibleEntry *lVis,
193 : ItemPointer nexttid,
194 : BlockNumber nblock, OffsetNumber noffset,
195 : int nposting);
196 : static void bt_entry_unique_check(BtreeCheckState *state, IndexTuple itup,
197 : BlockNumber targetblock, OffsetNumber offset,
198 : BtreeLastVisibleEntry *lVis);
199 : static void bt_target_page_check(BtreeCheckState *state);
200 : static BTScanInsert bt_right_page_check_scankey(BtreeCheckState *state,
201 : OffsetNumber *rightfirstoffset);
202 : static void bt_child_check(BtreeCheckState *state, BTScanInsert targetkey,
203 : OffsetNumber downlinkoffnum);
204 : static void bt_child_highkey_check(BtreeCheckState *state,
205 : OffsetNumber target_downlinkoffnum,
206 : Page loaded_child,
207 : uint32 target_level);
208 : static void bt_downlink_missing_check(BtreeCheckState *state, bool rightsplit,
209 : BlockNumber blkno, Page page);
210 : static void bt_tuple_present_callback(Relation index, ItemPointer tid,
211 : Datum *values, bool *isnull,
212 : bool tupleIsAlive, void *checkstate);
213 : static IndexTuple bt_normalize_tuple(BtreeCheckState *state,
214 : IndexTuple itup);
215 : static inline IndexTuple bt_posting_plain_tuple(IndexTuple itup, int n);
216 : static bool bt_rootdescend(BtreeCheckState *state, IndexTuple itup);
217 : static inline bool offset_is_negative_infinity(BTPageOpaque opaque,
218 : OffsetNumber offset);
219 : static inline bool invariant_l_offset(BtreeCheckState *state, BTScanInsert key,
220 : OffsetNumber upperbound);
221 : static inline bool invariant_leq_offset(BtreeCheckState *state,
222 : BTScanInsert key,
223 : OffsetNumber upperbound);
224 : static inline bool invariant_g_offset(BtreeCheckState *state, BTScanInsert key,
225 : OffsetNumber lowerbound);
226 : static inline bool invariant_l_nontarget_offset(BtreeCheckState *state,
227 : BTScanInsert key,
228 : BlockNumber nontargetblock,
229 : Page nontarget,
230 : OffsetNumber upperbound);
231 : static Page palloc_btree_page(BtreeCheckState *state, BlockNumber blocknum);
232 : static inline BTScanInsert bt_mkscankey_pivotsearch(Relation rel,
233 : IndexTuple itup);
234 : static ItemId PageGetItemIdCareful(BtreeCheckState *state, BlockNumber block,
235 : Page page, OffsetNumber offset);
236 : static inline ItemPointer BTreeTupleGetHeapTIDCareful(BtreeCheckState *state,
237 : IndexTuple itup, bool nonpivot);
238 : static inline ItemPointer BTreeTupleGetPointsToTID(IndexTuple itup);
239 :
240 : /*
241 : * bt_index_check(index regclass, heapallindexed boolean, checkunique boolean)
242 : *
243 : * Verify integrity of B-Tree index.
244 : *
245 : * Acquires AccessShareLock on heap & index relations. Does not consider
246 : * invariants that exist between parent/child pages. Optionally verifies
247 : * that heap does not contain any unindexed or incorrectly indexed tuples.
248 : */
249 : Datum
250 7858 : bt_index_check(PG_FUNCTION_ARGS)
251 : {
252 7858 : Oid indrelid = PG_GETARG_OID(0);
253 : BTCallbackState args;
254 :
255 7858 : args.heapallindexed = false;
256 7858 : args.rootdescend = false;
257 7858 : args.parentcheck = false;
258 7858 : args.checkunique = false;
259 :
260 7858 : if (PG_NARGS() >= 2)
261 7846 : args.heapallindexed = PG_GETARG_BOOL(1);
262 7858 : if (PG_NARGS() >= 3)
263 1364 : args.checkunique = PG_GETARG_BOOL(2);
264 :
265 7858 : amcheck_lock_relation_and_check(indrelid, BTREE_AM_OID,
266 : bt_index_check_callback,
267 : AccessShareLock, &args);
268 :
269 7806 : PG_RETURN_VOID();
270 : }
271 :
272 : /*
273 : * bt_index_parent_check(index regclass, heapallindexed boolean, rootdescend boolean, checkunique boolean)
274 : *
275 : * Verify integrity of B-Tree index.
276 : *
277 : * Acquires ShareLock on heap & index relations. Verifies that downlinks in
278 : * parent pages are valid lower bounds on child pages. Optionally verifies
279 : * that heap does not contain any unindexed or incorrectly indexed tuples.
280 : */
281 : Datum
282 122 : bt_index_parent_check(PG_FUNCTION_ARGS)
283 : {
284 122 : Oid indrelid = PG_GETARG_OID(0);
285 : BTCallbackState args;
286 :
287 122 : args.heapallindexed = false;
288 122 : args.rootdescend = false;
289 122 : args.parentcheck = true;
290 122 : args.checkunique = false;
291 :
292 122 : if (PG_NARGS() >= 2)
293 110 : args.heapallindexed = PG_GETARG_BOOL(1);
294 122 : if (PG_NARGS() >= 3)
295 104 : args.rootdescend = PG_GETARG_BOOL(2);
296 122 : if (PG_NARGS() >= 4)
297 52 : args.checkunique = PG_GETARG_BOOL(3);
298 :
299 122 : amcheck_lock_relation_and_check(indrelid, BTREE_AM_OID,
300 : bt_index_check_callback,
301 : ShareLock, &args);
302 :
303 86 : PG_RETURN_VOID();
304 : }
305 :
306 : /*
307 : * Helper for bt_index_[parent_]check, coordinating the bulk of the work.
308 : */
309 : static void
310 7968 : bt_index_check_callback(Relation indrel, Relation heaprel, void *state, bool readonly)
311 : {
312 7968 : BTCallbackState *args = (BTCallbackState *) state;
313 : bool heapkeyspace,
314 : allequalimage;
315 :
316 7968 : if (!smgrexists(RelationGetSmgr(indrel), MAIN_FORKNUM))
317 36 : ereport(ERROR,
318 : (errcode(ERRCODE_INDEX_CORRUPTED),
319 : errmsg("index \"%s\" lacks a main relation fork",
320 : RelationGetRelationName(indrel))));
321 :
322 : /* Extract metadata from metapage, and sanitize it in passing */
323 7932 : _bt_metaversion(indrel, &heapkeyspace, &allequalimage);
324 7932 : if (allequalimage && !heapkeyspace)
325 0 : ereport(ERROR,
326 : (errcode(ERRCODE_INDEX_CORRUPTED),
327 : errmsg("index \"%s\" metapage has equalimage field set on unsupported nbtree version",
328 : RelationGetRelationName(indrel))));
329 7932 : if (allequalimage && !_bt_allequalimage(indrel, false))
330 : {
331 0 : bool has_interval_ops = false;
332 :
333 0 : for (int i = 0; i < IndexRelationGetNumberOfKeyAttributes(indrel); i++)
334 0 : if (indrel->rd_opfamily[i] == INTERVAL_BTREE_FAM_OID)
335 : {
336 0 : has_interval_ops = true;
337 0 : ereport(ERROR,
338 : (errcode(ERRCODE_INDEX_CORRUPTED),
339 : errmsg("index \"%s\" metapage incorrectly indicates that deduplication is safe",
340 : RelationGetRelationName(indrel)),
341 : has_interval_ops
342 : ? errhint("This is known of \"interval\" indexes last built on a version predating 2023-11.")
343 : : 0));
344 : }
345 : }
346 :
347 : /* Check index, possibly against table it is an index on */
348 7932 : bt_check_every_level(indrel, heaprel, heapkeyspace, readonly,
349 7932 : args->heapallindexed, args->rootdescend, args->checkunique);
350 7892 : }
351 :
352 : /*
353 : * Main entry point for B-Tree SQL-callable functions. Walks the B-Tree in
354 : * logical order, verifying invariants as it goes. Optionally, verification
355 : * checks if the heap relation contains any tuples that are not represented in
356 : * the index but should be.
357 : *
358 : * It is the caller's responsibility to acquire appropriate heavyweight lock on
359 : * the index relation, and advise us if extra checks are safe when a ShareLock
360 : * is held. (A lock of the same type must also have been acquired on the heap
361 : * relation.)
362 : *
363 : * A ShareLock is generally assumed to prevent any kind of physical
364 : * modification to the index structure, including modifications that VACUUM may
365 : * make. This does not include setting of the LP_DEAD bit by concurrent index
366 : * scans, although that is just metadata that is not able to directly affect
367 : * any check performed here. Any concurrent process that might act on the
368 : * LP_DEAD bit being set (recycle space) requires a heavyweight lock that
369 : * cannot be held while we hold a ShareLock. (Besides, even if that could
370 : * happen, the ad-hoc recycling when a page might otherwise split is performed
371 : * per-page, and requires an exclusive buffer lock, which wouldn't cause us
372 : * trouble. _bt_delitems_vacuum() may only delete leaf items, and so the extra
373 : * parent/child check cannot be affected.)
374 : */
375 : static void
376 7932 : bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace,
377 : bool readonly, bool heapallindexed, bool rootdescend,
378 : bool checkunique)
379 : {
380 : BtreeCheckState *state;
381 : Page metapage;
382 : BTMetaPageData *metad;
383 : uint32 previouslevel;
384 : BtreeLevel current;
385 7932 : Snapshot snapshot = SnapshotAny;
386 :
387 7932 : if (!readonly)
388 7834 : elog(DEBUG1, "verifying consistency of tree structure for index \"%s\"",
389 : RelationGetRelationName(rel));
390 : else
391 98 : elog(DEBUG1, "verifying consistency of tree structure for index \"%s\" with cross-level checks",
392 : RelationGetRelationName(rel));
393 :
394 : /*
395 : * This assertion matches the one in index_getnext_tid(). See page
396 : * recycling/"visible to everyone" notes in nbtree README.
397 : */
398 : Assert(TransactionIdIsValid(RecentXmin));
399 :
400 : /*
401 : * Initialize state for entire verification operation
402 : */
403 7932 : state = palloc0(sizeof(BtreeCheckState));
404 7932 : state->rel = rel;
405 7932 : state->heaprel = heaprel;
406 7932 : state->heapkeyspace = heapkeyspace;
407 7932 : state->readonly = readonly;
408 7932 : state->heapallindexed = heapallindexed;
409 7932 : state->rootdescend = rootdescend;
410 7932 : state->checkunique = checkunique;
411 7932 : state->snapshot = InvalidSnapshot;
412 :
413 7932 : if (state->heapallindexed)
414 : {
415 : int64 total_pages;
416 : int64 total_elems;
417 : uint64 seed;
418 :
419 : /*
420 : * Size Bloom filter based on estimated number of tuples in index,
421 : * while conservatively assuming that each block must contain at least
422 : * MaxTIDsPerBTreePage / 3 "plain" tuples -- see
423 : * bt_posting_plain_tuple() for definition, and details of how posting
424 : * list tuples are handled.
425 : */
426 130 : total_pages = RelationGetNumberOfBlocks(rel);
427 130 : total_elems = Max(total_pages * (MaxTIDsPerBTreePage / 3),
428 : (int64) state->rel->rd_rel->reltuples);
429 : /* Generate a random seed to avoid repetition */
430 130 : seed = pg_prng_uint64(&pg_global_prng_state);
431 : /* Create Bloom filter to fingerprint index */
432 130 : state->filter = bloom_create(total_elems, maintenance_work_mem, seed);
433 130 : state->heaptuplespresent = 0;
434 :
435 : /*
436 : * Register our own snapshot in !readonly case, rather than asking
437 : * table_index_build_scan() to do this for us later. This needs to
438 : * happen before index fingerprinting begins, so we can later be
439 : * certain that index fingerprinting should have reached all tuples
440 : * returned by table_index_build_scan().
441 : */
442 130 : if (!state->readonly)
443 : {
444 76 : snapshot = RegisterSnapshot(GetTransactionSnapshot());
445 :
446 : /*
447 : * GetTransactionSnapshot() always acquires a new MVCC snapshot in
448 : * READ COMMITTED mode. A new snapshot is guaranteed to have all
449 : * the entries it requires in the index.
450 : *
451 : * We must defend against the possibility that an old xact
452 : * snapshot was returned at higher isolation levels when that
453 : * snapshot is not safe for index scans of the target index. This
454 : * is possible when the snapshot sees tuples that are before the
455 : * index's indcheckxmin horizon. Throwing an error here should be
456 : * very rare. It doesn't seem worth using a secondary snapshot to
457 : * avoid this.
458 : */
459 76 : if (IsolationUsesXactSnapshot() && rel->rd_index->indcheckxmin &&
460 0 : !TransactionIdPrecedes(HeapTupleHeaderGetXmin(rel->rd_indextuple->t_data),
461 : snapshot->xmin))
462 0 : ereport(ERROR,
463 : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
464 : errmsg("index \"%s\" cannot be verified using transaction snapshot",
465 : RelationGetRelationName(rel))));
466 : }
467 : }
468 :
469 : /*
470 : * We need a snapshot to check the uniqueness of the index. For better
471 : * performance take it once per index check. If snapshot already taken
472 : * reuse it.
473 : */
474 7932 : if (state->checkunique)
475 : {
476 1408 : state->indexinfo = BuildIndexInfo(state->rel);
477 1408 : if (state->indexinfo->ii_Unique)
478 : {
479 1268 : if (snapshot != SnapshotAny)
480 14 : state->snapshot = snapshot;
481 : else
482 1254 : state->snapshot = RegisterSnapshot(GetTransactionSnapshot());
483 : }
484 : }
485 :
486 : Assert(!state->rootdescend || state->readonly);
487 7932 : if (state->rootdescend && !state->heapkeyspace)
488 0 : ereport(ERROR,
489 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
490 : errmsg("cannot verify that tuples from index \"%s\" can each be found by an independent index search",
491 : RelationGetRelationName(rel)),
492 : errhint("Only B-Tree version 4 indexes support rootdescend verification.")));
493 :
494 : /* Create context for page */
495 7932 : state->targetcontext = AllocSetContextCreate(CurrentMemoryContext,
496 : "amcheck context",
497 : ALLOCSET_DEFAULT_SIZES);
498 7932 : state->checkstrategy = GetAccessStrategy(BAS_BULKREAD);
499 :
500 : /* Get true root block from meta-page */
501 7932 : metapage = palloc_btree_page(state, BTREE_METAPAGE);
502 7932 : metad = BTPageGetMeta(metapage);
503 :
504 : /*
505 : * Certain deletion patterns can result in "skinny" B-Tree indexes, where
506 : * the fast root and true root differ.
507 : *
508 : * Start from the true root, not the fast root, unlike conventional index
509 : * scans. This approach is more thorough, and removes the risk of
510 : * following a stale fast root from the meta page.
511 : */
512 7932 : if (metad->btm_fastroot != metad->btm_root)
513 26 : ereport(DEBUG1,
514 : (errcode(ERRCODE_NO_DATA),
515 : errmsg_internal("harmless fast root mismatch in index \"%s\"",
516 : RelationGetRelationName(rel)),
517 : errdetail_internal("Fast root block %u (level %u) differs from true root block %u (level %u).",
518 : metad->btm_fastroot, metad->btm_fastlevel,
519 : metad->btm_root, metad->btm_level)));
520 :
521 : /*
522 : * Starting at the root, verify every level. Move left to right, top to
523 : * bottom. Note that there may be no pages other than the meta page (meta
524 : * page can indicate that root is P_NONE when the index is totally empty).
525 : */
526 7932 : previouslevel = InvalidBtreeLevel;
527 7932 : current.level = metad->btm_level;
528 7932 : current.leftmost = metad->btm_root;
529 7932 : current.istruerootlevel = true;
530 12898 : while (current.leftmost != P_NONE)
531 : {
532 : /*
533 : * Verify this level, and get left most page for next level down, if
534 : * not at leaf level
535 : */
536 5002 : current = bt_check_level_from_leftmost(state, current);
537 :
538 4966 : if (current.leftmost == InvalidBlockNumber)
539 0 : ereport(ERROR,
540 : (errcode(ERRCODE_INDEX_CORRUPTED),
541 : errmsg("index \"%s\" has no valid pages on level below %u or first level",
542 : RelationGetRelationName(rel), previouslevel)));
543 :
544 4966 : previouslevel = current.level;
545 : }
546 :
547 : /*
548 : * * Check whether heap contains unindexed/malformed tuples *
549 : */
550 7896 : if (state->heapallindexed)
551 : {
552 116 : IndexInfo *indexinfo = BuildIndexInfo(state->rel);
553 : TableScanDesc scan;
554 :
555 : /*
556 : * Create our own scan for table_index_build_scan(), rather than
557 : * getting it to do so for us. This is required so that we can
558 : * actually use the MVCC snapshot registered earlier in !readonly
559 : * case.
560 : *
561 : * Note that table_index_build_scan() calls heap_endscan() for us.
562 : */
563 116 : scan = table_beginscan_strat(state->heaprel, /* relation */
564 : snapshot, /* snapshot */
565 : 0, /* number of keys */
566 : NULL, /* scan key */
567 : true, /* buffer access strategy OK */
568 : true); /* syncscan OK? */
569 :
570 : /*
571 : * Scan will behave as the first scan of a CREATE INDEX CONCURRENTLY
572 : * behaves in !readonly case.
573 : *
574 : * It's okay that we don't actually use the same lock strength for the
575 : * heap relation as any other ii_Concurrent caller would in !readonly
576 : * case. We have no reason to care about a concurrent VACUUM
577 : * operation, since there isn't going to be a second scan of the heap
578 : * that needs to be sure that there was no concurrent recycling of
579 : * TIDs.
580 : */
581 112 : indexinfo->ii_Concurrent = !state->readonly;
582 :
583 : /*
584 : * Don't wait for uncommitted tuple xact commit/abort when index is a
585 : * unique index on a catalog (or an index used by an exclusion
586 : * constraint). This could otherwise happen in the readonly case.
587 : */
588 112 : indexinfo->ii_Unique = false;
589 112 : indexinfo->ii_ExclusionOps = NULL;
590 112 : indexinfo->ii_ExclusionProcs = NULL;
591 112 : indexinfo->ii_ExclusionStrats = NULL;
592 :
593 112 : elog(DEBUG1, "verifying that tuples from index \"%s\" are present in \"%s\"",
594 : RelationGetRelationName(state->rel),
595 : RelationGetRelationName(state->heaprel));
596 :
597 112 : table_index_build_scan(state->heaprel, state->rel, indexinfo, true, false,
598 : bt_tuple_present_callback, state, scan);
599 :
600 112 : ereport(DEBUG1,
601 : (errmsg_internal("finished verifying presence of " INT64_FORMAT " tuples from table \"%s\" with bitset %.2f%% set",
602 : state->heaptuplespresent, RelationGetRelationName(heaprel),
603 : 100.0 * bloom_prop_bits_set(state->filter))));
604 :
605 112 : if (snapshot != SnapshotAny)
606 66 : UnregisterSnapshot(snapshot);
607 :
608 112 : bloom_free(state->filter);
609 : }
610 :
611 : /* Be tidy: */
612 7892 : if (snapshot == SnapshotAny && state->snapshot != InvalidSnapshot)
613 1254 : UnregisterSnapshot(state->snapshot);
614 7892 : MemoryContextDelete(state->targetcontext);
615 7892 : }
616 :
617 : /*
618 : * Given a left-most block at some level, move right, verifying each page
619 : * individually (with more verification across pages for "readonly"
620 : * callers). Caller should pass the true root page as the leftmost initially,
621 : * working their way down by passing what is returned for the last call here
622 : * until level 0 (leaf page level) was reached.
623 : *
624 : * Returns state for next call, if any. This includes left-most block number
625 : * one level lower that should be passed on next level/call, which is set to
626 : * P_NONE on last call here (when leaf level is verified). Level numbers
627 : * follow the nbtree convention: higher levels have higher numbers, because new
628 : * levels are added only due to a root page split. Note that prior to the
629 : * first root page split, the root is also a leaf page, so there is always a
630 : * level 0 (leaf level), and it's always the last level processed.
631 : *
632 : * Note on memory management: State's per-page context is reset here, between
633 : * each call to bt_target_page_check().
634 : */
635 : static BtreeLevel
636 5002 : bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level)
637 : {
638 : /* State to establish early, concerning entire level */
639 : BTPageOpaque opaque;
640 : MemoryContext oldcontext;
641 : BtreeLevel nextleveldown;
642 :
643 : /* Variables for iterating across level using right links */
644 5002 : BlockNumber leftcurrent = P_NONE;
645 5002 : BlockNumber current = level.leftmost;
646 :
647 : /* Initialize return state */
648 5002 : nextleveldown.leftmost = InvalidBlockNumber;
649 5002 : nextleveldown.level = InvalidBtreeLevel;
650 5002 : nextleveldown.istruerootlevel = false;
651 :
652 : /* Use page-level context for duration of this call */
653 5002 : oldcontext = MemoryContextSwitchTo(state->targetcontext);
654 :
655 5002 : elog(DEBUG1, "verifying level %u%s", level.level,
656 : level.istruerootlevel ?
657 : " (true root level)" : level.level == 0 ? " (leaf level)" : "");
658 :
659 5002 : state->prevrightlink = InvalidBlockNumber;
660 5002 : state->previncompletesplit = false;
661 :
662 : do
663 : {
664 : /* Don't rely on CHECK_FOR_INTERRUPTS() calls at lower level */
665 18230 : CHECK_FOR_INTERRUPTS();
666 :
667 : /* Initialize state for this iteration */
668 18230 : state->targetblock = current;
669 18230 : state->target = palloc_btree_page(state, state->targetblock);
670 18206 : state->targetlsn = PageGetLSN(state->target);
671 :
672 18206 : opaque = BTPageGetOpaque(state->target);
673 :
674 18206 : if (P_IGNORE(opaque))
675 : {
676 : /*
677 : * Since there cannot be a concurrent VACUUM operation in readonly
678 : * mode, and since a page has no links within other pages
679 : * (siblings and parent) once it is marked fully deleted, it
680 : * should be impossible to land on a fully deleted page in
681 : * readonly mode. See bt_child_check() for further details.
682 : *
683 : * The bt_child_check() P_ISDELETED() check is repeated here so
684 : * that pages that are only reachable through sibling links get
685 : * checked.
686 : */
687 0 : if (state->readonly && P_ISDELETED(opaque))
688 0 : ereport(ERROR,
689 : (errcode(ERRCODE_INDEX_CORRUPTED),
690 : errmsg("downlink or sibling link points to deleted block in index \"%s\"",
691 : RelationGetRelationName(state->rel)),
692 : errdetail_internal("Block=%u left block=%u left link from block=%u.",
693 : current, leftcurrent, opaque->btpo_prev)));
694 :
695 0 : if (P_RIGHTMOST(opaque))
696 0 : ereport(ERROR,
697 : (errcode(ERRCODE_INDEX_CORRUPTED),
698 : errmsg("block %u fell off the end of index \"%s\"",
699 : current, RelationGetRelationName(state->rel))));
700 : else
701 0 : ereport(DEBUG1,
702 : (errcode(ERRCODE_NO_DATA),
703 : errmsg_internal("block %u of index \"%s\" concurrently deleted",
704 : current, RelationGetRelationName(state->rel))));
705 0 : goto nextpage;
706 : }
707 18206 : else if (nextleveldown.leftmost == InvalidBlockNumber)
708 : {
709 : /*
710 : * A concurrent page split could make the caller supplied leftmost
711 : * block no longer contain the leftmost page, or no longer be the
712 : * true root, but where that isn't possible due to heavyweight
713 : * locking, check that the first valid page meets caller's
714 : * expectations.
715 : */
716 4978 : if (state->readonly)
717 : {
718 88 : if (!bt_leftmost_ignoring_half_dead(state, current, opaque))
719 0 : ereport(ERROR,
720 : (errcode(ERRCODE_INDEX_CORRUPTED),
721 : errmsg("block %u is not leftmost in index \"%s\"",
722 : current, RelationGetRelationName(state->rel))));
723 :
724 88 : if (level.istruerootlevel && !P_ISROOT(opaque))
725 0 : ereport(ERROR,
726 : (errcode(ERRCODE_INDEX_CORRUPTED),
727 : errmsg("block %u is not true root in index \"%s\"",
728 : current, RelationGetRelationName(state->rel))));
729 : }
730 :
731 : /*
732 : * Before beginning any non-trivial examination of level, prepare
733 : * state for next bt_check_level_from_leftmost() invocation for
734 : * the next level for the next level down (if any).
735 : *
736 : * There should be at least one non-ignorable page per level,
737 : * unless this is the leaf level, which is assumed by caller to be
738 : * final level.
739 : */
740 4978 : if (!P_ISLEAF(opaque))
741 : {
742 : IndexTuple itup;
743 : ItemId itemid;
744 :
745 : /* Internal page -- downlink gets leftmost on next level */
746 1110 : itemid = PageGetItemIdCareful(state, state->targetblock,
747 : state->target,
748 1110 : P_FIRSTDATAKEY(opaque));
749 1110 : itup = (IndexTuple) PageGetItem(state->target, itemid);
750 1110 : nextleveldown.leftmost = BTreeTupleGetDownLink(itup);
751 1110 : nextleveldown.level = opaque->btpo_level - 1;
752 : }
753 : else
754 : {
755 : /*
756 : * Leaf page -- final level caller must process.
757 : *
758 : * Note that this could also be the root page, if there has
759 : * been no root page split yet.
760 : */
761 3868 : nextleveldown.leftmost = P_NONE;
762 3868 : nextleveldown.level = InvalidBtreeLevel;
763 : }
764 :
765 : /*
766 : * Finished setting up state for this call/level. Control will
767 : * never end up back here in any future loop iteration for this
768 : * level.
769 : */
770 : }
771 :
772 : /*
773 : * Sibling links should be in mutual agreement. There arises
774 : * leftcurrent == P_NONE && btpo_prev != P_NONE when the left sibling
775 : * of the parent's low-key downlink is half-dead. (A half-dead page
776 : * has no downlink from its parent.) Under heavyweight locking, the
777 : * last bt_leftmost_ignoring_half_dead() validated this btpo_prev.
778 : * Without heavyweight locking, validation of the P_NONE case remains
779 : * unimplemented.
780 : */
781 18206 : if (opaque->btpo_prev != leftcurrent && leftcurrent != P_NONE)
782 0 : bt_recheck_sibling_links(state, opaque->btpo_prev, leftcurrent);
783 :
784 : /* Check level */
785 18206 : if (level.level != opaque->btpo_level)
786 0 : ereport(ERROR,
787 : (errcode(ERRCODE_INDEX_CORRUPTED),
788 : errmsg("leftmost down link for level points to block in index \"%s\" whose level is not one level down",
789 : RelationGetRelationName(state->rel)),
790 : errdetail_internal("Block pointed to=%u expected level=%u level in pointed to block=%u.",
791 : current, level.level, opaque->btpo_level)));
792 :
793 : /* Verify invariants for page */
794 18206 : bt_target_page_check(state);
795 :
796 18194 : nextpage:
797 :
798 : /* Try to detect circular links */
799 18194 : if (current == leftcurrent || current == opaque->btpo_prev)
800 0 : ereport(ERROR,
801 : (errcode(ERRCODE_INDEX_CORRUPTED),
802 : errmsg("circular link chain found in block %u of index \"%s\"",
803 : current, RelationGetRelationName(state->rel))));
804 :
805 18194 : leftcurrent = current;
806 18194 : current = opaque->btpo_next;
807 :
808 18194 : if (state->lowkey)
809 : {
810 : Assert(state->readonly);
811 3724 : pfree(state->lowkey);
812 3724 : state->lowkey = NULL;
813 : }
814 :
815 : /*
816 : * Copy current target high key as the low key of right sibling.
817 : * Allocate memory in upper level context, so it would be cleared
818 : * after reset of target context.
819 : *
820 : * We only need the low key in corner cases of checking child high
821 : * keys. We use high key only when incomplete split on the child level
822 : * falls to the boundary of pages on the target level. See
823 : * bt_child_highkey_check() for details. So, typically we won't end
824 : * up doing anything with low key, but it's simpler for general case
825 : * high key verification to always have it available.
826 : *
827 : * The correctness of managing low key in the case of concurrent
828 : * splits wasn't investigated yet. Thankfully we only need low key
829 : * for readonly verification and concurrent splits won't happen.
830 : */
831 18194 : if (state->readonly && !P_RIGHTMOST(opaque))
832 : {
833 : IndexTuple itup;
834 : ItemId itemid;
835 :
836 3724 : itemid = PageGetItemIdCareful(state, state->targetblock,
837 : state->target, P_HIKEY);
838 3724 : itup = (IndexTuple) PageGetItem(state->target, itemid);
839 :
840 3724 : state->lowkey = MemoryContextAlloc(oldcontext, IndexTupleSize(itup));
841 3724 : memcpy(state->lowkey, itup, IndexTupleSize(itup));
842 : }
843 :
844 : /* Free page and associated memory for this iteration */
845 18194 : MemoryContextReset(state->targetcontext);
846 : }
847 18194 : while (current != P_NONE);
848 :
849 4966 : if (state->lowkey)
850 : {
851 : Assert(state->readonly);
852 0 : pfree(state->lowkey);
853 0 : state->lowkey = NULL;
854 : }
855 :
856 : /* Don't change context for caller */
857 4966 : MemoryContextSwitchTo(oldcontext);
858 :
859 4966 : return nextleveldown;
860 : }
861 :
862 : /* Check visibility of the table entry referenced by nbtree index */
863 : static bool
864 746 : heap_entry_is_visible(BtreeCheckState *state, ItemPointer tid)
865 : {
866 : bool tid_visible;
867 :
868 746 : TupleTableSlot *slot = table_slot_create(state->heaprel, NULL);
869 :
870 746 : tid_visible = table_tuple_fetch_row_version(state->heaprel,
871 : tid, state->snapshot, slot);
872 746 : if (slot != NULL)
873 746 : ExecDropSingleTupleTableSlot(slot);
874 :
875 746 : return tid_visible;
876 : }
877 :
878 : /*
879 : * Prepare an error message for unique constrain violation in
880 : * a btree index and report ERROR.
881 : */
882 : static void
883 6 : bt_report_duplicate(BtreeCheckState *state,
884 : BtreeLastVisibleEntry *lVis,
885 : ItemPointer nexttid, BlockNumber nblock, OffsetNumber noffset,
886 : int nposting)
887 : {
888 : char *htid,
889 : *nhtid,
890 : *itid,
891 6 : *nitid = "",
892 6 : *pposting = "",
893 6 : *pnposting = "";
894 :
895 6 : htid = psprintf("tid=(%u,%u)",
896 6 : ItemPointerGetBlockNumberNoCheck(lVis->tid),
897 6 : ItemPointerGetOffsetNumberNoCheck(lVis->tid));
898 6 : nhtid = psprintf("tid=(%u,%u)",
899 : ItemPointerGetBlockNumberNoCheck(nexttid),
900 6 : ItemPointerGetOffsetNumberNoCheck(nexttid));
901 6 : itid = psprintf("tid=(%u,%u)", lVis->blkno, lVis->offset);
902 :
903 6 : if (nblock != lVis->blkno || noffset != lVis->offset)
904 6 : nitid = psprintf(" tid=(%u,%u)", nblock, noffset);
905 :
906 6 : if (lVis->postingIndex >= 0)
907 0 : pposting = psprintf(" posting %u", lVis->postingIndex);
908 :
909 6 : if (nposting >= 0)
910 0 : pnposting = psprintf(" posting %u", nposting);
911 :
912 6 : ereport(ERROR,
913 : (errcode(ERRCODE_INDEX_CORRUPTED),
914 : errmsg("index uniqueness is violated for index \"%s\"",
915 : RelationGetRelationName(state->rel)),
916 : errdetail("Index %s%s and%s%s (point to heap %s and %s) page lsn=%X/%08X.",
917 : itid, pposting, nitid, pnposting, htid, nhtid,
918 : LSN_FORMAT_ARGS(state->targetlsn))));
919 : }
920 :
921 : /* Check if current nbtree leaf entry complies with UNIQUE constraint */
922 : static void
923 714 : bt_entry_unique_check(BtreeCheckState *state, IndexTuple itup,
924 : BlockNumber targetblock, OffsetNumber offset,
925 : BtreeLastVisibleEntry *lVis)
926 : {
927 : ItemPointer tid;
928 714 : bool has_visible_entry = false;
929 :
930 : Assert(targetblock != P_NONE);
931 :
932 : /*
933 : * Current tuple has posting list. Report duplicate if TID of any posting
934 : * list entry is visible and lVis->tid is valid.
935 : */
936 714 : if (BTreeTupleIsPosting(itup))
937 : {
938 96 : for (int i = 0; i < BTreeTupleGetNPosting(itup); i++)
939 : {
940 64 : tid = BTreeTupleGetPostingN(itup, i);
941 64 : if (heap_entry_is_visible(state, tid))
942 : {
943 32 : has_visible_entry = true;
944 32 : if (ItemPointerIsValid(lVis->tid))
945 : {
946 0 : bt_report_duplicate(state,
947 : lVis,
948 : tid, targetblock,
949 : offset, i);
950 : }
951 :
952 : /*
953 : * Prevent double reporting unique constraint violation
954 : * between the posting list entries of the first tuple on the
955 : * page after cross-page check.
956 : */
957 32 : if (lVis->blkno != targetblock && ItemPointerIsValid(lVis->tid))
958 0 : return;
959 :
960 32 : lVis->blkno = targetblock;
961 32 : lVis->offset = offset;
962 32 : lVis->postingIndex = i;
963 32 : lVis->tid = tid;
964 : }
965 : }
966 : }
967 :
968 : /*
969 : * Current tuple has no posting list. If TID is visible save info about it
970 : * for the next comparisons in the loop in bt_target_page_check(). Report
971 : * duplicate if lVis->tid is already valid.
972 : */
973 : else
974 : {
975 682 : tid = BTreeTupleGetHeapTID(itup);
976 682 : if (heap_entry_is_visible(state, tid))
977 : {
978 30 : has_visible_entry = true;
979 30 : if (ItemPointerIsValid(lVis->tid))
980 : {
981 6 : bt_report_duplicate(state,
982 : lVis,
983 : tid, targetblock,
984 : offset, -1);
985 : }
986 :
987 24 : lVis->blkno = targetblock;
988 24 : lVis->offset = offset;
989 24 : lVis->tid = tid;
990 24 : lVis->postingIndex = -1;
991 : }
992 : }
993 :
994 708 : if (!has_visible_entry &&
995 652 : lVis->blkno != InvalidBlockNumber &&
996 18 : lVis->blkno != targetblock)
997 : {
998 0 : char *posting = "";
999 :
1000 0 : if (lVis->postingIndex >= 0)
1001 0 : posting = psprintf(" posting %u", lVis->postingIndex);
1002 0 : ereport(DEBUG1,
1003 : (errcode(ERRCODE_NO_DATA),
1004 : errmsg("index uniqueness can not be checked for index tid=(%u,%u) in index \"%s\"",
1005 : targetblock, offset,
1006 : RelationGetRelationName(state->rel)),
1007 : errdetail("It doesn't have visible heap tids and key is equal to the tid=(%u,%u)%s (points to heap tid=(%u,%u)).",
1008 : lVis->blkno, lVis->offset, posting,
1009 : ItemPointerGetBlockNumberNoCheck(lVis->tid),
1010 : ItemPointerGetOffsetNumberNoCheck(lVis->tid)),
1011 : errhint("VACUUM the table and repeat the check.")));
1012 : }
1013 : }
1014 :
1015 : /*
1016 : * Like P_LEFTMOST(start_opaque), but accept an arbitrarily-long chain of
1017 : * half-dead, sibling-linked pages to the left. If a half-dead page appears
1018 : * under state->readonly, the database exited recovery between the first-stage
1019 : * and second-stage WAL records of a deletion.
1020 : */
1021 : static bool
1022 110 : bt_leftmost_ignoring_half_dead(BtreeCheckState *state,
1023 : BlockNumber start,
1024 : BTPageOpaque start_opaque)
1025 : {
1026 110 : BlockNumber reached = start_opaque->btpo_prev,
1027 110 : reached_from = start;
1028 110 : bool all_half_dead = true;
1029 :
1030 : /*
1031 : * To handle the !readonly case, we'd need to accept BTP_DELETED pages and
1032 : * potentially observe nbtree/README "Page deletion and backwards scans".
1033 : */
1034 : Assert(state->readonly);
1035 :
1036 114 : while (reached != P_NONE && all_half_dead)
1037 : {
1038 4 : Page page = palloc_btree_page(state, reached);
1039 4 : BTPageOpaque reached_opaque = BTPageGetOpaque(page);
1040 :
1041 4 : CHECK_FOR_INTERRUPTS();
1042 :
1043 : /*
1044 : * Try to detect btpo_prev circular links. _bt_unlink_halfdead_page()
1045 : * writes that side-links will continue to point to the siblings.
1046 : * Check btpo_next for that property.
1047 : */
1048 4 : all_half_dead = P_ISHALFDEAD(reached_opaque) &&
1049 4 : reached != start &&
1050 8 : reached != reached_from &&
1051 4 : reached_opaque->btpo_next == reached_from;
1052 4 : if (all_half_dead)
1053 : {
1054 4 : XLogRecPtr pagelsn = PageGetLSN(page);
1055 :
1056 : /* pagelsn should point to an XLOG_BTREE_MARK_PAGE_HALFDEAD */
1057 4 : ereport(DEBUG1,
1058 : (errcode(ERRCODE_NO_DATA),
1059 : errmsg_internal("harmless interrupted page deletion detected in index \"%s\"",
1060 : RelationGetRelationName(state->rel)),
1061 : errdetail_internal("Block=%u right block=%u page lsn=%X/%08X.",
1062 : reached, reached_from,
1063 : LSN_FORMAT_ARGS(pagelsn))));
1064 :
1065 4 : reached_from = reached;
1066 4 : reached = reached_opaque->btpo_prev;
1067 : }
1068 :
1069 4 : pfree(page);
1070 : }
1071 :
1072 110 : return all_half_dead;
1073 : }
1074 :
1075 : /*
1076 : * Raise an error when target page's left link does not point back to the
1077 : * previous target page, called leftcurrent here. The leftcurrent page's
1078 : * right link was followed to get to the current target page, and we expect
1079 : * mutual agreement among leftcurrent and the current target page. Make sure
1080 : * that this condition has definitely been violated in the !readonly case,
1081 : * where concurrent page splits are something that we need to deal with.
1082 : *
1083 : * Cross-page inconsistencies involving pages that don't agree about being
1084 : * siblings are known to be a particularly good indicator of corruption
1085 : * involving partial writes/lost updates. The bt_right_page_check_scankey
1086 : * check also provides a way of detecting cross-page inconsistencies for
1087 : * !readonly callers, but it can only detect sibling pages that have an
1088 : * out-of-order keyspace, which can't catch many of the problems that we
1089 : * expect to catch here.
1090 : *
1091 : * The classic example of the kind of inconsistency that we can only catch
1092 : * with this check (when in !readonly mode) involves three sibling pages that
1093 : * were affected by a faulty page split at some point in the past. The
1094 : * effects of the split are reflected in the original page and its new right
1095 : * sibling page, with a lack of any accompanying changes for the _original_
1096 : * right sibling page. The original right sibling page's left link fails to
1097 : * point to the new right sibling page (its left link still points to the
1098 : * original page), even though the first phase of a page split is supposed to
1099 : * work as a single atomic action. This subtle inconsistency will probably
1100 : * only break backwards scans in practice.
1101 : *
1102 : * Note that this is the only place where amcheck will "couple" buffer locks
1103 : * (and only for !readonly callers). In general we prefer to avoid more
1104 : * thorough cross-page checks in !readonly mode, but it seems worth the
1105 : * complexity here. Also, the performance overhead of performing lock
1106 : * coupling here is negligible in practice. Control only reaches here with a
1107 : * non-corrupt index when there is a concurrent page split at the instant
1108 : * caller crossed over to target page from leftcurrent page.
1109 : */
1110 : static void
1111 0 : bt_recheck_sibling_links(BtreeCheckState *state,
1112 : BlockNumber btpo_prev_from_target,
1113 : BlockNumber leftcurrent)
1114 : {
1115 : /* passing metapage to BTPageGetOpaque() would give irrelevant findings */
1116 : Assert(leftcurrent != P_NONE);
1117 :
1118 0 : if (!state->readonly)
1119 : {
1120 : Buffer lbuf;
1121 : Buffer newtargetbuf;
1122 : Page page;
1123 : BTPageOpaque opaque;
1124 : BlockNumber newtargetblock;
1125 :
1126 : /* Couple locks in the usual order for nbtree: Left to right */
1127 0 : lbuf = ReadBufferExtended(state->rel, MAIN_FORKNUM, leftcurrent,
1128 : RBM_NORMAL, state->checkstrategy);
1129 0 : LockBuffer(lbuf, BT_READ);
1130 0 : _bt_checkpage(state->rel, lbuf);
1131 0 : page = BufferGetPage(lbuf);
1132 0 : opaque = BTPageGetOpaque(page);
1133 0 : if (P_ISDELETED(opaque))
1134 : {
1135 : /*
1136 : * Cannot reason about concurrently deleted page -- the left link
1137 : * in the page to the right is expected to point to some other
1138 : * page to the left (not leftcurrent page).
1139 : *
1140 : * Note that we deliberately don't give up with a half-dead page.
1141 : */
1142 0 : UnlockReleaseBuffer(lbuf);
1143 0 : return;
1144 : }
1145 :
1146 0 : newtargetblock = opaque->btpo_next;
1147 : /* Avoid self-deadlock when newtargetblock == leftcurrent */
1148 0 : if (newtargetblock != leftcurrent)
1149 : {
1150 0 : newtargetbuf = ReadBufferExtended(state->rel, MAIN_FORKNUM,
1151 : newtargetblock, RBM_NORMAL,
1152 : state->checkstrategy);
1153 0 : LockBuffer(newtargetbuf, BT_READ);
1154 0 : _bt_checkpage(state->rel, newtargetbuf);
1155 0 : page = BufferGetPage(newtargetbuf);
1156 0 : opaque = BTPageGetOpaque(page);
1157 : /* btpo_prev_from_target may have changed; update it */
1158 0 : btpo_prev_from_target = opaque->btpo_prev;
1159 : }
1160 : else
1161 : {
1162 : /*
1163 : * leftcurrent right sibling points back to leftcurrent block.
1164 : * Index is corrupt. Easiest way to handle this is to pretend
1165 : * that we actually read from a distinct page that has an invalid
1166 : * block number in its btpo_prev.
1167 : */
1168 0 : newtargetbuf = InvalidBuffer;
1169 0 : btpo_prev_from_target = InvalidBlockNumber;
1170 : }
1171 :
1172 : /*
1173 : * No need to check P_ISDELETED here, since new target block cannot be
1174 : * marked deleted as long as we hold a lock on lbuf
1175 : */
1176 0 : if (BufferIsValid(newtargetbuf))
1177 0 : UnlockReleaseBuffer(newtargetbuf);
1178 0 : UnlockReleaseBuffer(lbuf);
1179 :
1180 0 : if (btpo_prev_from_target == leftcurrent)
1181 : {
1182 : /* Report split in left sibling, not target (or new target) */
1183 0 : ereport(DEBUG1,
1184 : (errcode(ERRCODE_INTERNAL_ERROR),
1185 : errmsg_internal("harmless concurrent page split detected in index \"%s\"",
1186 : RelationGetRelationName(state->rel)),
1187 : errdetail_internal("Block=%u new right sibling=%u original right sibling=%u.",
1188 : leftcurrent, newtargetblock,
1189 : state->targetblock)));
1190 0 : return;
1191 : }
1192 :
1193 : /*
1194 : * Index is corrupt. Make sure that we report correct target page.
1195 : *
1196 : * This could have changed in cases where there was a concurrent page
1197 : * split, as well as index corruption (at least in theory). Note that
1198 : * btpo_prev_from_target was already updated above.
1199 : */
1200 0 : state->targetblock = newtargetblock;
1201 : }
1202 :
1203 0 : ereport(ERROR,
1204 : (errcode(ERRCODE_INDEX_CORRUPTED),
1205 : errmsg("left link/right link pair in index \"%s\" not in agreement",
1206 : RelationGetRelationName(state->rel)),
1207 : errdetail_internal("Block=%u left block=%u left link from block=%u.",
1208 : state->targetblock, leftcurrent,
1209 : btpo_prev_from_target)));
1210 : }
1211 :
1212 : /*
1213 : * Function performs the following checks on target page, or pages ancillary to
1214 : * target page:
1215 : *
1216 : * - That every "real" data item is less than or equal to the high key, which
1217 : * is an upper bound on the items on the page. Data items should be
1218 : * strictly less than the high key when the page is an internal page.
1219 : *
1220 : * - That within the page, every data item is strictly less than the item
1221 : * immediately to its right, if any (i.e., that the items are in order
1222 : * within the page, so that the binary searches performed by index scans are
1223 : * sane).
1224 : *
1225 : * - That the last data item stored on the page is strictly less than the
1226 : * first data item on the page to the right (when such a first item is
1227 : * available).
1228 : *
1229 : * - Various checks on the structure of tuples themselves. For example, check
1230 : * that non-pivot tuples have no truncated attributes.
1231 : *
1232 : * - For index with unique constraint make sure that only one of table entries
1233 : * for equal keys is visible.
1234 : *
1235 : * Furthermore, when state passed shows ShareLock held, function also checks:
1236 : *
1237 : * - That all child pages respect strict lower bound from parent's pivot
1238 : * tuple.
1239 : *
1240 : * - That downlink to block was encountered in parent where that's expected.
1241 : *
1242 : * - That high keys of child pages matches corresponding pivot keys in parent.
1243 : *
1244 : * This is also where heapallindexed callers use their Bloom filter to
1245 : * fingerprint IndexTuples for later table_index_build_scan() verification.
1246 : *
1247 : * Note: Memory allocated in this routine is expected to be released by caller
1248 : * resetting state->targetcontext.
1249 : */
1250 : static void
1251 18206 : bt_target_page_check(BtreeCheckState *state)
1252 : {
1253 : OffsetNumber offset;
1254 : OffsetNumber max;
1255 : BTPageOpaque topaque;
1256 :
1257 : /* Last visible entry info for checking indexes with unique constraint */
1258 18206 : BtreeLastVisibleEntry lVis = {InvalidBlockNumber, InvalidOffsetNumber, -1, NULL};
1259 :
1260 18206 : topaque = BTPageGetOpaque(state->target);
1261 18206 : max = PageGetMaxOffsetNumber(state->target);
1262 :
1263 18206 : elog(DEBUG2, "verifying %u items on %s block %u", max,
1264 : P_ISLEAF(topaque) ? "leaf" : "internal", state->targetblock);
1265 :
1266 : /*
1267 : * Check the number of attributes in high key. Note, rightmost page
1268 : * doesn't contain a high key, so nothing to check
1269 : */
1270 18206 : if (!P_RIGHTMOST(topaque))
1271 : {
1272 : ItemId itemid;
1273 : IndexTuple itup;
1274 :
1275 : /* Verify line pointer before checking tuple */
1276 13236 : itemid = PageGetItemIdCareful(state, state->targetblock,
1277 : state->target, P_HIKEY);
1278 13236 : if (!_bt_check_natts(state->rel, state->heapkeyspace, state->target,
1279 : P_HIKEY))
1280 : {
1281 0 : itup = (IndexTuple) PageGetItem(state->target, itemid);
1282 0 : ereport(ERROR,
1283 : (errcode(ERRCODE_INDEX_CORRUPTED),
1284 : errmsg("wrong number of high key index tuple attributes in index \"%s\"",
1285 : RelationGetRelationName(state->rel)),
1286 : errdetail_internal("Index block=%u natts=%u block type=%s page lsn=%X/%08X.",
1287 : state->targetblock,
1288 : BTreeTupleGetNAtts(itup, state->rel),
1289 : P_ISLEAF(topaque) ? "heap" : "index",
1290 : LSN_FORMAT_ARGS(state->targetlsn))));
1291 : }
1292 : }
1293 :
1294 : /*
1295 : * Loop over page items, starting from first non-highkey item, not high
1296 : * key (if any). Most tests are not performed for the "negative infinity"
1297 : * real item (if any).
1298 : */
1299 18206 : for (offset = P_FIRSTDATAKEY(topaque);
1300 4062456 : offset <= max;
1301 4044250 : offset = OffsetNumberNext(offset))
1302 : {
1303 : ItemId itemid;
1304 : IndexTuple itup;
1305 : size_t tupsize;
1306 : BTScanInsert skey;
1307 : bool lowersizelimit;
1308 : ItemPointer scantid;
1309 :
1310 : /*
1311 : * True if we already called bt_entry_unique_check() for the current
1312 : * item. This helps to avoid visiting the heap for keys, which are
1313 : * anyway presented only once and can't comprise a unique violation.
1314 : */
1315 4044262 : bool unique_checked = false;
1316 :
1317 4044262 : CHECK_FOR_INTERRUPTS();
1318 :
1319 4044262 : itemid = PageGetItemIdCareful(state, state->targetblock,
1320 : state->target, offset);
1321 4044262 : itup = (IndexTuple) PageGetItem(state->target, itemid);
1322 4044262 : tupsize = IndexTupleSize(itup);
1323 :
1324 : /*
1325 : * lp_len should match the IndexTuple reported length exactly, since
1326 : * lp_len is completely redundant in indexes, and both sources of
1327 : * tuple length are MAXALIGN()'d. nbtree does not use lp_len all that
1328 : * frequently, and is surprisingly tolerant of corrupt lp_len fields.
1329 : */
1330 4044262 : if (tupsize != ItemIdGetLength(itemid))
1331 0 : ereport(ERROR,
1332 : (errcode(ERRCODE_INDEX_CORRUPTED),
1333 : errmsg("index tuple size does not equal lp_len in index \"%s\"",
1334 : RelationGetRelationName(state->rel)),
1335 : errdetail_internal("Index tid=(%u,%u) tuple size=%zu lp_len=%u page lsn=%X/%08X.",
1336 : state->targetblock, offset,
1337 : tupsize, ItemIdGetLength(itemid),
1338 : LSN_FORMAT_ARGS(state->targetlsn)),
1339 : errhint("This could be a torn page problem.")));
1340 :
1341 : /* Check the number of index tuple attributes */
1342 4044262 : if (!_bt_check_natts(state->rel, state->heapkeyspace, state->target,
1343 : offset))
1344 : {
1345 : ItemPointer tid;
1346 : char *itid,
1347 : *htid;
1348 :
1349 0 : itid = psprintf("(%u,%u)", state->targetblock, offset);
1350 0 : tid = BTreeTupleGetPointsToTID(itup);
1351 0 : htid = psprintf("(%u,%u)",
1352 : ItemPointerGetBlockNumberNoCheck(tid),
1353 0 : ItemPointerGetOffsetNumberNoCheck(tid));
1354 :
1355 0 : ereport(ERROR,
1356 : (errcode(ERRCODE_INDEX_CORRUPTED),
1357 : errmsg("wrong number of index tuple attributes in index \"%s\"",
1358 : RelationGetRelationName(state->rel)),
1359 : errdetail_internal("Index tid=%s natts=%u points to %s tid=%s page lsn=%X/%08X.",
1360 : itid,
1361 : BTreeTupleGetNAtts(itup, state->rel),
1362 : P_ISLEAF(topaque) ? "heap" : "index",
1363 : htid,
1364 : LSN_FORMAT_ARGS(state->targetlsn))));
1365 : }
1366 :
1367 : /*
1368 : * Don't try to generate scankey using "negative infinity" item on
1369 : * internal pages. They are always truncated to zero attributes.
1370 : */
1371 4044262 : if (offset_is_negative_infinity(topaque, offset))
1372 : {
1373 : /*
1374 : * We don't call bt_child_check() for "negative infinity" items.
1375 : * But if we're performing downlink connectivity check, we do it
1376 : * for every item including "negative infinity" one.
1377 : */
1378 1114 : if (!P_ISLEAF(topaque) && state->readonly)
1379 : {
1380 24 : bt_child_highkey_check(state,
1381 : offset,
1382 : NULL,
1383 : topaque->btpo_level);
1384 : }
1385 1114 : continue;
1386 : }
1387 :
1388 : /*
1389 : * Readonly callers may optionally verify that non-pivot tuples can
1390 : * each be found by an independent search that starts from the root.
1391 : * Note that we deliberately don't do individual searches for each
1392 : * TID, since the posting list itself is validated by other checks.
1393 : */
1394 4043148 : if (state->rootdescend && P_ISLEAF(topaque) &&
1395 402196 : !bt_rootdescend(state, itup))
1396 : {
1397 0 : ItemPointer tid = BTreeTupleGetPointsToTID(itup);
1398 : char *itid,
1399 : *htid;
1400 :
1401 0 : itid = psprintf("(%u,%u)", state->targetblock, offset);
1402 0 : htid = psprintf("(%u,%u)", ItemPointerGetBlockNumber(tid),
1403 0 : ItemPointerGetOffsetNumber(tid));
1404 :
1405 0 : ereport(ERROR,
1406 : (errcode(ERRCODE_INDEX_CORRUPTED),
1407 : errmsg("could not find tuple using search from root page in index \"%s\"",
1408 : RelationGetRelationName(state->rel)),
1409 : errdetail_internal("Index tid=%s points to heap tid=%s page lsn=%X/%08X.",
1410 : itid, htid,
1411 : LSN_FORMAT_ARGS(state->targetlsn))));
1412 : }
1413 :
1414 : /*
1415 : * If tuple is a posting list tuple, make sure posting list TIDs are
1416 : * in order
1417 : */
1418 4043148 : if (BTreeTupleIsPosting(itup))
1419 : {
1420 : ItemPointerData last;
1421 : ItemPointer current;
1422 :
1423 21914 : ItemPointerCopy(BTreeTupleGetHeapTID(itup), &last);
1424 :
1425 157566 : for (int i = 1; i < BTreeTupleGetNPosting(itup); i++)
1426 : {
1427 :
1428 135652 : current = BTreeTupleGetPostingN(itup, i);
1429 :
1430 135652 : if (ItemPointerCompare(current, &last) <= 0)
1431 : {
1432 0 : char *itid = psprintf("(%u,%u)", state->targetblock, offset);
1433 :
1434 0 : ereport(ERROR,
1435 : (errcode(ERRCODE_INDEX_CORRUPTED),
1436 : errmsg_internal("posting list contains misplaced TID in index \"%s\"",
1437 : RelationGetRelationName(state->rel)),
1438 : errdetail_internal("Index tid=%s posting list offset=%d page lsn=%X/%08X.",
1439 : itid, i,
1440 : LSN_FORMAT_ARGS(state->targetlsn))));
1441 : }
1442 :
1443 135652 : ItemPointerCopy(current, &last);
1444 : }
1445 : }
1446 :
1447 : /* Build insertion scankey for current page offset */
1448 4043148 : skey = bt_mkscankey_pivotsearch(state->rel, itup);
1449 :
1450 : /*
1451 : * Make sure tuple size does not exceed the relevant BTREE_VERSION
1452 : * specific limit.
1453 : *
1454 : * BTREE_VERSION 4 (which introduced heapkeyspace rules) requisitioned
1455 : * a small amount of space from BTMaxItemSize() in order to ensure
1456 : * that suffix truncation always has enough space to add an explicit
1457 : * heap TID back to a tuple -- we pessimistically assume that every
1458 : * newly inserted tuple will eventually need to have a heap TID
1459 : * appended during a future leaf page split, when the tuple becomes
1460 : * the basis of the new high key (pivot tuple) for the leaf page.
1461 : *
1462 : * Since the reclaimed space is reserved for that purpose, we must not
1463 : * enforce the slightly lower limit when the extra space has been used
1464 : * as intended. In other words, there is only a cross-version
1465 : * difference in the limit on tuple size within leaf pages.
1466 : *
1467 : * Still, we're particular about the details within BTREE_VERSION 4
1468 : * internal pages. Pivot tuples may only use the extra space for its
1469 : * designated purpose. Enforce the lower limit for pivot tuples when
1470 : * an explicit heap TID isn't actually present. (In all other cases
1471 : * suffix truncation is guaranteed to generate a pivot tuple that's no
1472 : * larger than the firstright tuple provided to it by its caller.)
1473 : */
1474 8086296 : lowersizelimit = skey->heapkeyspace &&
1475 4043148 : (P_ISLEAF(topaque) || BTreeTupleGetHeapTID(itup) == NULL);
1476 4043148 : if (tupsize > (lowersizelimit ? BTMaxItemSize : BTMaxItemSizeNoHeapTid))
1477 : {
1478 0 : ItemPointer tid = BTreeTupleGetPointsToTID(itup);
1479 : char *itid,
1480 : *htid;
1481 :
1482 0 : itid = psprintf("(%u,%u)", state->targetblock, offset);
1483 0 : htid = psprintf("(%u,%u)",
1484 : ItemPointerGetBlockNumberNoCheck(tid),
1485 0 : ItemPointerGetOffsetNumberNoCheck(tid));
1486 :
1487 0 : ereport(ERROR,
1488 : (errcode(ERRCODE_INDEX_CORRUPTED),
1489 : errmsg("index row size %zu exceeds maximum for index \"%s\"",
1490 : tupsize, RelationGetRelationName(state->rel)),
1491 : errdetail_internal("Index tid=%s points to %s tid=%s page lsn=%X/%08X.",
1492 : itid,
1493 : P_ISLEAF(topaque) ? "heap" : "index",
1494 : htid,
1495 : LSN_FORMAT_ARGS(state->targetlsn))));
1496 : }
1497 :
1498 : /* Fingerprint leaf page tuples (those that point to the heap) */
1499 4043148 : if (state->heapallindexed && P_ISLEAF(topaque) && !ItemIdIsDead(itemid))
1500 : {
1501 : IndexTuple norm;
1502 :
1503 1013914 : if (BTreeTupleIsPosting(itup))
1504 : {
1505 : /* Fingerprint all elements as distinct "plain" tuples */
1506 52160 : for (int i = 0; i < BTreeTupleGetNPosting(itup); i++)
1507 : {
1508 : IndexTuple logtuple;
1509 :
1510 51800 : logtuple = bt_posting_plain_tuple(itup, i);
1511 51800 : norm = bt_normalize_tuple(state, logtuple);
1512 51800 : bloom_add_element(state->filter, (unsigned char *) norm,
1513 : IndexTupleSize(norm));
1514 : /* Be tidy */
1515 51800 : if (norm != logtuple)
1516 4 : pfree(norm);
1517 51800 : pfree(logtuple);
1518 : }
1519 : }
1520 : else
1521 : {
1522 1013554 : norm = bt_normalize_tuple(state, itup);
1523 1013554 : bloom_add_element(state->filter, (unsigned char *) norm,
1524 : IndexTupleSize(norm));
1525 : /* Be tidy */
1526 1013554 : if (norm != itup)
1527 2 : pfree(norm);
1528 : }
1529 : }
1530 :
1531 : /*
1532 : * * High key check *
1533 : *
1534 : * If there is a high key (if this is not the rightmost page on its
1535 : * entire level), check that high key actually is upper bound on all
1536 : * page items. If this is a posting list tuple, we'll need to set
1537 : * scantid to be highest TID in posting list.
1538 : *
1539 : * We prefer to check all items against high key rather than checking
1540 : * just the last and trusting that the operator class obeys the
1541 : * transitive law (which implies that all previous items also
1542 : * respected the high key invariant if they pass the item order
1543 : * check).
1544 : *
1545 : * Ideally, we'd compare every item in the index against every other
1546 : * item in the index, and not trust opclass obedience of the
1547 : * transitive law to bridge the gap between children and their
1548 : * grandparents (as well as great-grandparents, and so on). We don't
1549 : * go to those lengths because that would be prohibitively expensive,
1550 : * and probably not markedly more effective in practice.
1551 : *
1552 : * On the leaf level, we check that the key is <= the highkey.
1553 : * However, on non-leaf levels we check that the key is < the highkey,
1554 : * because the high key is "just another separator" rather than a copy
1555 : * of some existing key item; we expect it to be unique among all keys
1556 : * on the same level. (Suffix truncation will sometimes produce a
1557 : * leaf highkey that is an untruncated copy of the lastleft item, but
1558 : * never any other item, which necessitates weakening the leaf level
1559 : * check to <=.)
1560 : *
1561 : * Full explanation for why a highkey is never truly a copy of another
1562 : * item from the same level on internal levels:
1563 : *
1564 : * While the new left page's high key is copied from the first offset
1565 : * on the right page during an internal page split, that's not the
1566 : * full story. In effect, internal pages are split in the middle of
1567 : * the firstright tuple, not between the would-be lastleft and
1568 : * firstright tuples: the firstright key ends up on the left side as
1569 : * left's new highkey, and the firstright downlink ends up on the
1570 : * right side as right's new "negative infinity" item. The negative
1571 : * infinity tuple is truncated to zero attributes, so we're only left
1572 : * with the downlink. In other words, the copying is just an
1573 : * implementation detail of splitting in the middle of a (pivot)
1574 : * tuple. (See also: "Notes About Data Representation" in the nbtree
1575 : * README.)
1576 : */
1577 4043148 : scantid = skey->scantid;
1578 4043148 : if (state->heapkeyspace && BTreeTupleIsPosting(itup))
1579 21914 : skey->scantid = BTreeTupleGetMaxHeapTID(itup);
1580 :
1581 7743592 : if (!P_RIGHTMOST(topaque) &&
1582 3700444 : !(P_ISLEAF(topaque) ? invariant_leq_offset(state, skey, P_HIKEY) :
1583 1132 : invariant_l_offset(state, skey, P_HIKEY)))
1584 : {
1585 0 : ItemPointer tid = BTreeTupleGetPointsToTID(itup);
1586 : char *itid,
1587 : *htid;
1588 :
1589 0 : itid = psprintf("(%u,%u)", state->targetblock, offset);
1590 0 : htid = psprintf("(%u,%u)",
1591 : ItemPointerGetBlockNumberNoCheck(tid),
1592 0 : ItemPointerGetOffsetNumberNoCheck(tid));
1593 :
1594 0 : ereport(ERROR,
1595 : (errcode(ERRCODE_INDEX_CORRUPTED),
1596 : errmsg("high key invariant violated for index \"%s\"",
1597 : RelationGetRelationName(state->rel)),
1598 : errdetail_internal("Index tid=%s points to %s tid=%s page lsn=%X/%08X.",
1599 : itid,
1600 : P_ISLEAF(topaque) ? "heap" : "index",
1601 : htid,
1602 : LSN_FORMAT_ARGS(state->targetlsn))));
1603 : }
1604 : /* Reset, in case scantid was set to (itup) posting tuple's max TID */
1605 4043148 : skey->scantid = scantid;
1606 :
1607 : /*
1608 : * * Item order check *
1609 : *
1610 : * Check that items are stored on page in logical order, by checking
1611 : * current item is strictly less than next item (if any).
1612 : */
1613 4043148 : if (OffsetNumberNext(offset) <= max &&
1614 4024958 : !invariant_l_offset(state, skey, OffsetNumberNext(offset)))
1615 : {
1616 : ItemPointer tid;
1617 : char *itid,
1618 : *htid,
1619 : *nitid,
1620 : *nhtid;
1621 :
1622 6 : itid = psprintf("(%u,%u)", state->targetblock, offset);
1623 6 : tid = BTreeTupleGetPointsToTID(itup);
1624 6 : htid = psprintf("(%u,%u)",
1625 : ItemPointerGetBlockNumberNoCheck(tid),
1626 6 : ItemPointerGetOffsetNumberNoCheck(tid));
1627 6 : nitid = psprintf("(%u,%u)", state->targetblock,
1628 6 : OffsetNumberNext(offset));
1629 :
1630 : /* Reuse itup to get pointed-to heap location of second item */
1631 6 : itemid = PageGetItemIdCareful(state, state->targetblock,
1632 : state->target,
1633 6 : OffsetNumberNext(offset));
1634 6 : itup = (IndexTuple) PageGetItem(state->target, itemid);
1635 6 : tid = BTreeTupleGetPointsToTID(itup);
1636 6 : nhtid = psprintf("(%u,%u)",
1637 : ItemPointerGetBlockNumberNoCheck(tid),
1638 6 : ItemPointerGetOffsetNumberNoCheck(tid));
1639 :
1640 6 : ereport(ERROR,
1641 : (errcode(ERRCODE_INDEX_CORRUPTED),
1642 : errmsg("item order invariant violated for index \"%s\"",
1643 : RelationGetRelationName(state->rel)),
1644 : errdetail_internal("Lower index tid=%s (points to %s tid=%s) higher index tid=%s (points to %s tid=%s) page lsn=%X/%08X.",
1645 : itid,
1646 : P_ISLEAF(topaque) ? "heap" : "index",
1647 : htid,
1648 : nitid,
1649 : P_ISLEAF(topaque) ? "heap" : "index",
1650 : nhtid,
1651 : LSN_FORMAT_ARGS(state->targetlsn))));
1652 : }
1653 :
1654 : /*
1655 : * If the index is unique verify entries uniqueness by checking the
1656 : * heap tuples visibility. Immediately check posting tuples and
1657 : * tuples with repeated keys. Postpone check for keys, which have the
1658 : * first appearance.
1659 : */
1660 4043142 : if (state->checkunique && state->indexinfo->ii_Unique &&
1661 577754 : P_ISLEAF(topaque) && !skey->anynullkeys &&
1662 576532 : (BTreeTupleIsPosting(itup) || ItemPointerIsValid(lVis.tid)))
1663 : {
1664 56 : bt_entry_unique_check(state, itup, state->targetblock, offset,
1665 : &lVis);
1666 50 : unique_checked = true;
1667 : }
1668 :
1669 4043136 : if (state->checkunique && state->indexinfo->ii_Unique &&
1670 289466 : P_ISLEAF(topaque) && OffsetNumberNext(offset) <= max)
1671 : {
1672 : /* Save current scankey tid */
1673 286834 : scantid = skey->scantid;
1674 :
1675 : /*
1676 : * Invalidate scankey tid to make _bt_compare compare only keys in
1677 : * the item to report equality even if heap TIDs are different
1678 : */
1679 286834 : skey->scantid = NULL;
1680 :
1681 : /*
1682 : * If next key tuple is different, invalidate last visible entry
1683 : * data (whole index tuple or last posting in index tuple). Key
1684 : * containing null value does not violate unique constraint and
1685 : * treated as different to any other key.
1686 : *
1687 : * If the next key is the same as the previous one, do the
1688 : * bt_entry_unique_check() call if it was postponed.
1689 : */
1690 286834 : if (_bt_compare(state->rel, skey, state->target,
1691 287632 : OffsetNumberNext(offset)) != 0 || skey->anynullkeys)
1692 : {
1693 286176 : lVis.blkno = InvalidBlockNumber;
1694 286176 : lVis.offset = InvalidOffsetNumber;
1695 286176 : lVis.postingIndex = -1;
1696 286176 : lVis.tid = NULL;
1697 : }
1698 658 : else if (!unique_checked)
1699 : {
1700 658 : bt_entry_unique_check(state, itup, state->targetblock, offset,
1701 : &lVis);
1702 : }
1703 286834 : skey->scantid = scantid; /* Restore saved scan key state */
1704 : }
1705 :
1706 : /*
1707 : * * Last item check *
1708 : *
1709 : * Check last item against next/right page's first data item's when
1710 : * last item on page is reached. This additional check will detect
1711 : * transposed pages iff the supposed right sibling page happens to
1712 : * belong before target in the key space. (Otherwise, a subsequent
1713 : * heap verification will probably detect the problem.)
1714 : *
1715 : * This check is similar to the item order check that will have
1716 : * already been performed for every other "real" item on target page
1717 : * when last item is checked. The difference is that the next item
1718 : * (the item that is compared to target's last item) needs to come
1719 : * from the next/sibling page. There may not be such an item
1720 : * available from sibling for various reasons, though (e.g., target is
1721 : * the rightmost page on level).
1722 : */
1723 4043136 : if (offset == max)
1724 : {
1725 : BTScanInsert rightkey;
1726 :
1727 : /* first offset on a right index page (log only) */
1728 18190 : OffsetNumber rightfirstoffset = InvalidOffsetNumber;
1729 :
1730 : /* Get item in next/right page */
1731 18190 : rightkey = bt_right_page_check_scankey(state, &rightfirstoffset);
1732 :
1733 18190 : if (rightkey &&
1734 13228 : !invariant_g_offset(state, rightkey, max))
1735 : {
1736 : /*
1737 : * As explained at length in bt_right_page_check_scankey(),
1738 : * there is a known !readonly race that could account for
1739 : * apparent violation of invariant, which we must check for
1740 : * before actually proceeding with raising error. Our canary
1741 : * condition is that target page was deleted.
1742 : */
1743 0 : if (!state->readonly)
1744 : {
1745 : /* Get fresh copy of target page */
1746 0 : state->target = palloc_btree_page(state, state->targetblock);
1747 : /* Note that we deliberately do not update target LSN */
1748 0 : topaque = BTPageGetOpaque(state->target);
1749 :
1750 : /*
1751 : * All !readonly checks now performed; just return
1752 : */
1753 0 : if (P_IGNORE(topaque))
1754 0 : return;
1755 : }
1756 :
1757 0 : ereport(ERROR,
1758 : (errcode(ERRCODE_INDEX_CORRUPTED),
1759 : errmsg("cross page item order invariant violated for index \"%s\"",
1760 : RelationGetRelationName(state->rel)),
1761 : errdetail_internal("Last item on page tid=(%u,%u) page lsn=%X/%08X.",
1762 : state->targetblock, offset,
1763 : LSN_FORMAT_ARGS(state->targetlsn))));
1764 : }
1765 :
1766 : /*
1767 : * If index has unique constraint make sure that no more than one
1768 : * found equal items is visible.
1769 : */
1770 18190 : if (state->checkunique && state->indexinfo->ii_Unique &&
1771 1026 : rightkey && P_ISLEAF(topaque) && !P_RIGHTMOST(topaque))
1772 : {
1773 1026 : BlockNumber rightblock_number = topaque->btpo_next;
1774 :
1775 1026 : elog(DEBUG2, "check cross page unique condition");
1776 :
1777 : /*
1778 : * Make _bt_compare compare only index keys without heap TIDs.
1779 : * rightkey->scantid is modified destructively but it is ok
1780 : * for it is not used later.
1781 : */
1782 1026 : rightkey->scantid = NULL;
1783 :
1784 : /* The first key on the next page is the same */
1785 1026 : if (_bt_compare(state->rel, rightkey, state->target, max) == 0 &&
1786 14 : !rightkey->anynullkeys)
1787 : {
1788 : Page rightpage;
1789 :
1790 : /*
1791 : * Do the bt_entry_unique_check() call if it was
1792 : * postponed.
1793 : */
1794 0 : if (!unique_checked)
1795 0 : bt_entry_unique_check(state, itup, state->targetblock,
1796 : offset, &lVis);
1797 :
1798 0 : elog(DEBUG2, "cross page equal keys");
1799 0 : rightpage = palloc_btree_page(state,
1800 : rightblock_number);
1801 0 : topaque = BTPageGetOpaque(rightpage);
1802 :
1803 0 : if (P_IGNORE(topaque))
1804 : {
1805 0 : pfree(rightpage);
1806 0 : break;
1807 : }
1808 :
1809 0 : if (unlikely(!P_ISLEAF(topaque)))
1810 0 : ereport(ERROR,
1811 : (errcode(ERRCODE_INDEX_CORRUPTED),
1812 : errmsg("right block of leaf block is non-leaf for index \"%s\"",
1813 : RelationGetRelationName(state->rel)),
1814 : errdetail_internal("Block=%u page lsn=%X/%08X.",
1815 : state->targetblock,
1816 : LSN_FORMAT_ARGS(state->targetlsn))));
1817 :
1818 0 : itemid = PageGetItemIdCareful(state, rightblock_number,
1819 : rightpage,
1820 : rightfirstoffset);
1821 0 : itup = (IndexTuple) PageGetItem(rightpage, itemid);
1822 :
1823 0 : bt_entry_unique_check(state, itup, rightblock_number, rightfirstoffset, &lVis);
1824 :
1825 0 : pfree(rightpage);
1826 : }
1827 : }
1828 : }
1829 :
1830 : /*
1831 : * * Downlink check *
1832 : *
1833 : * Additional check of child items iff this is an internal page and
1834 : * caller holds a ShareLock. This happens for every downlink (item)
1835 : * in target excluding the negative-infinity downlink (again, this is
1836 : * because it has no useful value to compare).
1837 : */
1838 4043136 : if (!P_ISLEAF(topaque) && state->readonly)
1839 3722 : bt_child_check(state, skey, offset);
1840 : }
1841 :
1842 : /*
1843 : * Special case bt_child_highkey_check() call
1844 : *
1845 : * We don't pass a real downlink, but we've to finish the level
1846 : * processing. If condition is satisfied, we've already processed all the
1847 : * downlinks from the target level. But there still might be pages to the
1848 : * right of the child page pointer to by our rightmost downlink. And they
1849 : * might have missing downlinks. This final call checks for them.
1850 : */
1851 18194 : if (!P_ISLEAF(topaque) && P_RIGHTMOST(topaque) && state->readonly)
1852 : {
1853 22 : bt_child_highkey_check(state, InvalidOffsetNumber,
1854 : NULL, topaque->btpo_level);
1855 : }
1856 : }
1857 :
1858 : /*
1859 : * Return a scankey for an item on page to right of current target (or the
1860 : * first non-ignorable page), sufficient to check ordering invariant on last
1861 : * item in current target page. Returned scankey relies on local memory
1862 : * allocated for the child page, which caller cannot pfree(). Caller's memory
1863 : * context should be reset between calls here.
1864 : *
1865 : * This is the first data item, and so all adjacent items are checked against
1866 : * their immediate sibling item (which may be on a sibling page, or even a
1867 : * "cousin" page at parent boundaries where target's rightlink points to page
1868 : * with different parent page). If no such valid item is available, return
1869 : * NULL instead.
1870 : *
1871 : * Note that !readonly callers must reverify that target page has not
1872 : * been concurrently deleted.
1873 : *
1874 : * Save rightfirstoffset for detailed error message.
1875 : */
1876 : static BTScanInsert
1877 18190 : bt_right_page_check_scankey(BtreeCheckState *state, OffsetNumber *rightfirstoffset)
1878 : {
1879 : BTPageOpaque opaque;
1880 : ItemId rightitem;
1881 : IndexTuple firstitup;
1882 : BlockNumber targetnext;
1883 : Page rightpage;
1884 : OffsetNumber nline;
1885 :
1886 : /* Determine target's next block number */
1887 18190 : opaque = BTPageGetOpaque(state->target);
1888 :
1889 : /* If target is already rightmost, no right sibling; nothing to do here */
1890 18190 : if (P_RIGHTMOST(opaque))
1891 4962 : return NULL;
1892 :
1893 : /*
1894 : * General notes on concurrent page splits and page deletion:
1895 : *
1896 : * Routines like _bt_search() don't require *any* page split interlock
1897 : * when descending the tree, including something very light like a buffer
1898 : * pin. That's why it's okay that we don't either. This avoidance of any
1899 : * need to "couple" buffer locks is the raison d' etre of the Lehman & Yao
1900 : * algorithm, in fact.
1901 : *
1902 : * That leaves deletion. A deleted page won't actually be recycled by
1903 : * VACUUM early enough for us to fail to at least follow its right link
1904 : * (or left link, or downlink) and find its sibling, because recycling
1905 : * does not occur until no possible index scan could land on the page.
1906 : * Index scans can follow links with nothing more than their snapshot as
1907 : * an interlock and be sure of at least that much. (See page
1908 : * recycling/"visible to everyone" notes in nbtree README.)
1909 : *
1910 : * Furthermore, it's okay if we follow a rightlink and find a half-dead or
1911 : * dead (ignorable) page one or more times. There will either be a
1912 : * further right link to follow that leads to a live page before too long
1913 : * (before passing by parent's rightmost child), or we will find the end
1914 : * of the entire level instead (possible when parent page is itself the
1915 : * rightmost on its level).
1916 : */
1917 13228 : targetnext = opaque->btpo_next;
1918 : for (;;)
1919 : {
1920 13228 : CHECK_FOR_INTERRUPTS();
1921 :
1922 13228 : rightpage = palloc_btree_page(state, targetnext);
1923 13228 : opaque = BTPageGetOpaque(rightpage);
1924 :
1925 13228 : if (!P_IGNORE(opaque) || P_RIGHTMOST(opaque))
1926 : break;
1927 :
1928 : /*
1929 : * We landed on a deleted or half-dead sibling page. Step right until
1930 : * we locate a live sibling page.
1931 : */
1932 0 : ereport(DEBUG2,
1933 : (errcode(ERRCODE_NO_DATA),
1934 : errmsg_internal("level %u sibling page in block %u of index \"%s\" was found deleted or half dead",
1935 : opaque->btpo_level, targetnext, RelationGetRelationName(state->rel)),
1936 : errdetail_internal("Deleted page found when building scankey from right sibling.")));
1937 :
1938 0 : targetnext = opaque->btpo_next;
1939 :
1940 : /* Be slightly more pro-active in freeing this memory, just in case */
1941 0 : pfree(rightpage);
1942 : }
1943 :
1944 : /*
1945 : * No ShareLock held case -- why it's safe to proceed.
1946 : *
1947 : * Problem:
1948 : *
1949 : * We must avoid false positive reports of corruption when caller treats
1950 : * item returned here as an upper bound on target's last item. In
1951 : * general, false positives are disallowed. Avoiding them here when
1952 : * caller is !readonly is subtle.
1953 : *
1954 : * A concurrent page deletion by VACUUM of the target page can result in
1955 : * the insertion of items on to this right sibling page that would
1956 : * previously have been inserted on our target page. There might have
1957 : * been insertions that followed the target's downlink after it was made
1958 : * to point to right sibling instead of target by page deletion's first
1959 : * phase. The inserters insert items that would belong on target page.
1960 : * This race is very tight, but it's possible. This is our only problem.
1961 : *
1962 : * Non-problems:
1963 : *
1964 : * We are not hindered by a concurrent page split of the target; we'll
1965 : * never land on the second half of the page anyway. A concurrent split
1966 : * of the right page will also not matter, because the first data item
1967 : * remains the same within the left half, which we'll reliably land on. If
1968 : * we had to skip over ignorable/deleted pages, it cannot matter because
1969 : * their key space has already been atomically merged with the first
1970 : * non-ignorable page we eventually find (doesn't matter whether the page
1971 : * we eventually find is a true sibling or a cousin of target, which we go
1972 : * into below).
1973 : *
1974 : * Solution:
1975 : *
1976 : * Caller knows that it should reverify that target is not ignorable
1977 : * (half-dead or deleted) when cross-page sibling item comparison appears
1978 : * to indicate corruption (invariant fails). This detects the single race
1979 : * condition that exists for caller. This is correct because the
1980 : * continued existence of target block as non-ignorable (not half-dead or
1981 : * deleted) implies that target page was not merged into from the right by
1982 : * deletion; the key space at or after target never moved left. Target's
1983 : * parent either has the same downlink to target as before, or a <
1984 : * downlink due to deletion at the left of target. Target either has the
1985 : * same highkey as before, or a highkey < before when there is a page
1986 : * split. (The rightmost concurrently-split-from-target-page page will
1987 : * still have the same highkey as target was originally found to have,
1988 : * which for our purposes is equivalent to target's highkey itself never
1989 : * changing, since we reliably skip over
1990 : * concurrently-split-from-target-page pages.)
1991 : *
1992 : * In simpler terms, we allow that the key space of the target may expand
1993 : * left (the key space can move left on the left side of target only), but
1994 : * the target key space cannot expand right and get ahead of us without
1995 : * our detecting it. The key space of the target cannot shrink, unless it
1996 : * shrinks to zero due to the deletion of the original page, our canary
1997 : * condition. (To be very precise, we're a bit stricter than that because
1998 : * it might just have been that the target page split and only the
1999 : * original target page was deleted. We can be more strict, just not more
2000 : * lax.)
2001 : *
2002 : * Top level tree walk caller moves on to next page (makes it the new
2003 : * target) following recovery from this race. (cf. The rationale for
2004 : * child/downlink verification needing a ShareLock within
2005 : * bt_child_check(), where page deletion is also the main source of
2006 : * trouble.)
2007 : *
2008 : * Note that it doesn't matter if right sibling page here is actually a
2009 : * cousin page, because in order for the key space to be readjusted in a
2010 : * way that causes us issues in next level up (guiding problematic
2011 : * concurrent insertions to the cousin from the grandparent rather than to
2012 : * the sibling from the parent), there'd have to be page deletion of
2013 : * target's parent page (affecting target's parent's downlink in target's
2014 : * grandparent page). Internal page deletion only occurs when there are
2015 : * no child pages (they were all fully deleted), and caller is checking
2016 : * that the target's parent has at least one non-deleted (so
2017 : * non-ignorable) child: the target page. (Note that the first phase of
2018 : * deletion atomically marks the page to be deleted half-dead/ignorable at
2019 : * the same time downlink in its parent is removed, so caller will
2020 : * definitely not fail to detect that this happened.)
2021 : *
2022 : * This trick is inspired by the method backward scans use for dealing
2023 : * with concurrent page splits; concurrent page deletion is a problem that
2024 : * similarly receives special consideration sometimes (it's possible that
2025 : * the backwards scan will re-read its "original" block after failing to
2026 : * find a right-link to it, having already moved in the opposite direction
2027 : * (right/"forwards") a few times to try to locate one). Just like us,
2028 : * that happens only to determine if there was a concurrent page deletion
2029 : * of a reference page, and just like us if there was a page deletion of
2030 : * that reference page it means we can move on from caring about the
2031 : * reference page. See the nbtree README for a full description of how
2032 : * that works.
2033 : */
2034 13228 : nline = PageGetMaxOffsetNumber(rightpage);
2035 :
2036 : /*
2037 : * Get first data item, if any
2038 : */
2039 13228 : if (P_ISLEAF(opaque) && nline >= P_FIRSTDATAKEY(opaque))
2040 : {
2041 : /* Return first data item (if any) */
2042 13224 : rightitem = PageGetItemIdCareful(state, targetnext, rightpage,
2043 13224 : P_FIRSTDATAKEY(opaque));
2044 13224 : *rightfirstoffset = P_FIRSTDATAKEY(opaque);
2045 : }
2046 8 : else if (!P_ISLEAF(opaque) &&
2047 4 : nline >= OffsetNumberNext(P_FIRSTDATAKEY(opaque)))
2048 : {
2049 : /*
2050 : * Return first item after the internal page's "negative infinity"
2051 : * item
2052 : */
2053 4 : rightitem = PageGetItemIdCareful(state, targetnext, rightpage,
2054 4 : OffsetNumberNext(P_FIRSTDATAKEY(opaque)));
2055 : }
2056 : else
2057 : {
2058 : /*
2059 : * No first item. Page is probably empty leaf page, but it's also
2060 : * possible that it's an internal page with only a negative infinity
2061 : * item.
2062 : */
2063 0 : ereport(DEBUG2,
2064 : (errcode(ERRCODE_NO_DATA),
2065 : errmsg_internal("%s block %u of index \"%s\" has no first data item",
2066 : P_ISLEAF(opaque) ? "leaf" : "internal", targetnext,
2067 : RelationGetRelationName(state->rel))));
2068 0 : return NULL;
2069 : }
2070 :
2071 : /*
2072 : * Return first real item scankey. Note that this relies on right page
2073 : * memory remaining allocated.
2074 : */
2075 13228 : firstitup = (IndexTuple) PageGetItem(rightpage, rightitem);
2076 13228 : return bt_mkscankey_pivotsearch(state->rel, firstitup);
2077 : }
2078 :
2079 : /*
2080 : * Check if two tuples are binary identical except the block number. So,
2081 : * this function is capable to compare pivot keys on different levels.
2082 : */
2083 : static bool
2084 3724 : bt_pivot_tuple_identical(bool heapkeyspace, IndexTuple itup1, IndexTuple itup2)
2085 : {
2086 3724 : if (IndexTupleSize(itup1) != IndexTupleSize(itup2))
2087 0 : return false;
2088 :
2089 3724 : if (heapkeyspace)
2090 : {
2091 : /*
2092 : * Offset number will contain important information in heapkeyspace
2093 : * indexes: the number of attributes left in the pivot tuple following
2094 : * suffix truncation. Don't skip over it (compare it too).
2095 : */
2096 3724 : if (memcmp(&itup1->t_tid.ip_posid, &itup2->t_tid.ip_posid,
2097 3724 : IndexTupleSize(itup1) -
2098 : offsetof(ItemPointerData, ip_posid)) != 0)
2099 0 : return false;
2100 : }
2101 : else
2102 : {
2103 : /*
2104 : * Cannot rely on offset number field having consistent value across
2105 : * levels on pg_upgrade'd !heapkeyspace indexes. Compare contents of
2106 : * tuple starting from just after item pointer (i.e. after block
2107 : * number and offset number).
2108 : */
2109 0 : if (memcmp(&itup1->t_info, &itup2->t_info,
2110 0 : IndexTupleSize(itup1) -
2111 : offsetof(IndexTupleData, t_info)) != 0)
2112 0 : return false;
2113 : }
2114 :
2115 3724 : return true;
2116 : }
2117 :
2118 : /*---
2119 : * Check high keys on the child level. Traverse rightlinks from previous
2120 : * downlink to the current one. Check that there are no intermediate pages
2121 : * with missing downlinks.
2122 : *
2123 : * If 'loaded_child' is given, it's assumed to be the page pointed to by the
2124 : * downlink referenced by 'downlinkoffnum' of the target page.
2125 : *
2126 : * Basically this function is called for each target downlink and checks two
2127 : * invariants:
2128 : *
2129 : * 1) You can reach the next child from previous one via rightlinks;
2130 : * 2) Each child high key have matching pivot key on target level.
2131 : *
2132 : * Consider the sample tree picture.
2133 : *
2134 : * 1
2135 : * / \
2136 : * 2 <-> 3
2137 : * / \ / \
2138 : * 4 <> 5 <> 6 <> 7 <> 8
2139 : *
2140 : * This function will be called for blocks 4, 5, 6 and 8. Consider what is
2141 : * happening for each function call.
2142 : *
2143 : * - The function call for block 4 initializes data structure and matches high
2144 : * key of block 4 to downlink's pivot key of block 2.
2145 : * - The high key of block 5 is matched to the high key of block 2.
2146 : * - The block 6 has an incomplete split flag set, so its high key isn't
2147 : * matched to anything.
2148 : * - The function call for block 8 checks that block 8 can be found while
2149 : * following rightlinks from block 6. The high key of block 7 will be
2150 : * matched to downlink's pivot key in block 3.
2151 : *
2152 : * There is also final call of this function, which checks that there is no
2153 : * missing downlinks for children to the right of the child referenced by
2154 : * rightmost downlink in target level.
2155 : */
2156 : static void
2157 3768 : bt_child_highkey_check(BtreeCheckState *state,
2158 : OffsetNumber target_downlinkoffnum,
2159 : Page loaded_child,
2160 : uint32 target_level)
2161 : {
2162 3768 : BlockNumber blkno = state->prevrightlink;
2163 : Page page;
2164 : BTPageOpaque opaque;
2165 3768 : bool rightsplit = state->previncompletesplit;
2166 3768 : bool first = true;
2167 : ItemId itemid;
2168 : IndexTuple itup;
2169 : BlockNumber downlink;
2170 :
2171 3768 : if (OffsetNumberIsValid(target_downlinkoffnum))
2172 : {
2173 3746 : itemid = PageGetItemIdCareful(state, state->targetblock,
2174 : state->target, target_downlinkoffnum);
2175 3746 : itup = (IndexTuple) PageGetItem(state->target, itemid);
2176 3746 : downlink = BTreeTupleGetDownLink(itup);
2177 : }
2178 : else
2179 : {
2180 22 : downlink = P_NONE;
2181 : }
2182 :
2183 : /*
2184 : * If no previous rightlink is memorized for current level just below
2185 : * target page's level, we are about to start from the leftmost page. We
2186 : * can't follow rightlinks from previous page, because there is no
2187 : * previous page. But we still can match high key.
2188 : *
2189 : * So we initialize variables for the loop above like there is previous
2190 : * page referencing current child. Also we imply previous page to not
2191 : * have incomplete split flag, that would make us require downlink for
2192 : * current child. That's correct, because leftmost page on the level
2193 : * should always have parent downlink.
2194 : */
2195 3768 : if (!BlockNumberIsValid(blkno))
2196 : {
2197 22 : blkno = downlink;
2198 22 : rightsplit = false;
2199 : }
2200 :
2201 : /* Move to the right on the child level */
2202 : while (true)
2203 : {
2204 : /*
2205 : * Did we traverse the whole tree level and this is check for pages to
2206 : * the right of rightmost downlink?
2207 : */
2208 3768 : if (blkno == P_NONE && downlink == P_NONE)
2209 : {
2210 22 : state->prevrightlink = InvalidBlockNumber;
2211 22 : state->previncompletesplit = false;
2212 22 : return;
2213 : }
2214 :
2215 : /* Did we traverse the whole tree level and don't find next downlink? */
2216 3746 : if (blkno == P_NONE)
2217 0 : ereport(ERROR,
2218 : (errcode(ERRCODE_INDEX_CORRUPTED),
2219 : errmsg("can't traverse from downlink %u to downlink %u of index \"%s\"",
2220 : state->prevrightlink, downlink,
2221 : RelationGetRelationName(state->rel))));
2222 :
2223 : /* Load page contents */
2224 3746 : if (blkno == downlink && loaded_child)
2225 3722 : page = loaded_child;
2226 : else
2227 24 : page = palloc_btree_page(state, blkno);
2228 :
2229 3746 : opaque = BTPageGetOpaque(page);
2230 :
2231 : /* The first page we visit at the level should be leftmost */
2232 3746 : if (first && !BlockNumberIsValid(state->prevrightlink) &&
2233 22 : !bt_leftmost_ignoring_half_dead(state, blkno, opaque))
2234 0 : ereport(ERROR,
2235 : (errcode(ERRCODE_INDEX_CORRUPTED),
2236 : errmsg("the first child of leftmost target page is not leftmost of its level in index \"%s\"",
2237 : RelationGetRelationName(state->rel)),
2238 : errdetail_internal("Target block=%u child block=%u target page lsn=%X/%08X.",
2239 : state->targetblock, blkno,
2240 : LSN_FORMAT_ARGS(state->targetlsn))));
2241 :
2242 : /* Do level sanity check */
2243 3746 : if ((!P_ISDELETED(opaque) || P_HAS_FULLXID(opaque)) &&
2244 3746 : opaque->btpo_level != target_level - 1)
2245 0 : ereport(ERROR,
2246 : (errcode(ERRCODE_INDEX_CORRUPTED),
2247 : errmsg("block found while following rightlinks from child of index \"%s\" has invalid level",
2248 : RelationGetRelationName(state->rel)),
2249 : errdetail_internal("Block pointed to=%u expected level=%u level in pointed to block=%u.",
2250 : blkno, target_level - 1, opaque->btpo_level)));
2251 :
2252 : /* Try to detect circular links */
2253 3746 : if ((!first && blkno == state->prevrightlink) || blkno == opaque->btpo_prev)
2254 0 : ereport(ERROR,
2255 : (errcode(ERRCODE_INDEX_CORRUPTED),
2256 : errmsg("circular link chain found in block %u of index \"%s\"",
2257 : blkno, RelationGetRelationName(state->rel))));
2258 :
2259 3746 : if (blkno != downlink && !P_IGNORE(opaque))
2260 : {
2261 : /* blkno probably has missing parent downlink */
2262 0 : bt_downlink_missing_check(state, rightsplit, blkno, page);
2263 : }
2264 :
2265 3746 : rightsplit = P_INCOMPLETE_SPLIT(opaque);
2266 :
2267 : /*
2268 : * If we visit page with high key, check that it is equal to the
2269 : * target key next to corresponding downlink.
2270 : */
2271 3746 : if (!rightsplit && !P_RIGHTMOST(opaque))
2272 : {
2273 : BTPageOpaque topaque;
2274 : IndexTuple highkey;
2275 : OffsetNumber pivotkey_offset;
2276 :
2277 : /* Get high key */
2278 3724 : itemid = PageGetItemIdCareful(state, blkno, page, P_HIKEY);
2279 3724 : highkey = (IndexTuple) PageGetItem(page, itemid);
2280 :
2281 : /*
2282 : * There might be two situations when we examine high key. If
2283 : * current child page is referenced by given target downlink, we
2284 : * should look to the next offset number for matching key from
2285 : * target page.
2286 : *
2287 : * Alternatively, we're following rightlinks somewhere in the
2288 : * middle between page referenced by previous target's downlink
2289 : * and the page referenced by current target's downlink. If
2290 : * current child page hasn't incomplete split flag set, then its
2291 : * high key should match to the target's key of current offset
2292 : * number. This happens when a previous call here (to
2293 : * bt_child_highkey_check()) found an incomplete split, and we
2294 : * reach a right sibling page without a downlink -- the right
2295 : * sibling page's high key still needs to be matched to a
2296 : * separator key on the parent/target level.
2297 : *
2298 : * Don't apply OffsetNumberNext() to target_downlinkoffnum when we
2299 : * already had to step right on the child level. Our traversal of
2300 : * the child level must try to move in perfect lockstep behind (to
2301 : * the left of) the target/parent level traversal.
2302 : */
2303 3724 : if (blkno == downlink)
2304 3724 : pivotkey_offset = OffsetNumberNext(target_downlinkoffnum);
2305 : else
2306 0 : pivotkey_offset = target_downlinkoffnum;
2307 :
2308 3724 : topaque = BTPageGetOpaque(state->target);
2309 :
2310 3724 : if (!offset_is_negative_infinity(topaque, pivotkey_offset))
2311 : {
2312 : /*
2313 : * If we're looking for the next pivot tuple in target page,
2314 : * but there is no more pivot tuples, then we should match to
2315 : * high key instead.
2316 : */
2317 3724 : if (pivotkey_offset > PageGetMaxOffsetNumber(state->target))
2318 : {
2319 2 : if (P_RIGHTMOST(topaque))
2320 0 : ereport(ERROR,
2321 : (errcode(ERRCODE_INDEX_CORRUPTED),
2322 : errmsg("child high key is greater than rightmost pivot key on target level in index \"%s\"",
2323 : RelationGetRelationName(state->rel)),
2324 : errdetail_internal("Target block=%u child block=%u target page lsn=%X/%08X.",
2325 : state->targetblock, blkno,
2326 : LSN_FORMAT_ARGS(state->targetlsn))));
2327 2 : pivotkey_offset = P_HIKEY;
2328 : }
2329 3724 : itemid = PageGetItemIdCareful(state, state->targetblock,
2330 : state->target, pivotkey_offset);
2331 3724 : itup = (IndexTuple) PageGetItem(state->target, itemid);
2332 : }
2333 : else
2334 : {
2335 : /*
2336 : * We cannot try to match child's high key to a negative
2337 : * infinity key in target, since there is nothing to compare.
2338 : * However, it's still possible to match child's high key
2339 : * outside of target page. The reason why we're are is that
2340 : * bt_child_highkey_check() was previously called for the
2341 : * cousin page of 'loaded_child', which is incomplete split.
2342 : * So, now we traverse to the right of that cousin page and
2343 : * current child level page under consideration still belongs
2344 : * to the subtree of target's left sibling. Thus, we need to
2345 : * match child's high key to its left uncle page high key.
2346 : * Thankfully we saved it, it's called a "low key" of target
2347 : * page.
2348 : */
2349 0 : if (!state->lowkey)
2350 0 : ereport(ERROR,
2351 : (errcode(ERRCODE_INDEX_CORRUPTED),
2352 : errmsg("can't find left sibling high key in index \"%s\"",
2353 : RelationGetRelationName(state->rel)),
2354 : errdetail_internal("Target block=%u child block=%u target page lsn=%X/%08X.",
2355 : state->targetblock, blkno,
2356 : LSN_FORMAT_ARGS(state->targetlsn))));
2357 0 : itup = state->lowkey;
2358 : }
2359 :
2360 3724 : if (!bt_pivot_tuple_identical(state->heapkeyspace, highkey, itup))
2361 : {
2362 0 : ereport(ERROR,
2363 : (errcode(ERRCODE_INDEX_CORRUPTED),
2364 : errmsg("mismatch between parent key and child high key in index \"%s\"",
2365 : RelationGetRelationName(state->rel)),
2366 : errdetail_internal("Target block=%u child block=%u target page lsn=%X/%08X.",
2367 : state->targetblock, blkno,
2368 : LSN_FORMAT_ARGS(state->targetlsn))));
2369 : }
2370 : }
2371 :
2372 : /* Exit if we already found next downlink */
2373 3746 : if (blkno == downlink)
2374 : {
2375 3746 : state->prevrightlink = opaque->btpo_next;
2376 3746 : state->previncompletesplit = rightsplit;
2377 3746 : return;
2378 : }
2379 :
2380 : /* Traverse to the next page using rightlink */
2381 0 : blkno = opaque->btpo_next;
2382 :
2383 : /* Free page contents if it's allocated by us */
2384 0 : if (page != loaded_child)
2385 0 : pfree(page);
2386 0 : first = false;
2387 : }
2388 : }
2389 :
2390 : /*
2391 : * Checks one of target's downlink against its child page.
2392 : *
2393 : * Conceptually, the target page continues to be what is checked here. The
2394 : * target block is still blamed in the event of finding an invariant violation.
2395 : * The downlink insertion into the target is probably where any problem raised
2396 : * here arises, and there is no such thing as a parent link, so doing the
2397 : * verification this way around is much more practical.
2398 : *
2399 : * This function visits child page and it's sequentially called for each
2400 : * downlink of target page. Assuming this we also check downlink connectivity
2401 : * here in order to save child page visits.
2402 : */
2403 : static void
2404 3722 : bt_child_check(BtreeCheckState *state, BTScanInsert targetkey,
2405 : OffsetNumber downlinkoffnum)
2406 : {
2407 : ItemId itemid;
2408 : IndexTuple itup;
2409 : BlockNumber childblock;
2410 : OffsetNumber offset;
2411 : OffsetNumber maxoffset;
2412 : Page child;
2413 : BTPageOpaque copaque;
2414 : BTPageOpaque topaque;
2415 :
2416 3722 : itemid = PageGetItemIdCareful(state, state->targetblock,
2417 : state->target, downlinkoffnum);
2418 3722 : itup = (IndexTuple) PageGetItem(state->target, itemid);
2419 3722 : childblock = BTreeTupleGetDownLink(itup);
2420 :
2421 : /*
2422 : * Caller must have ShareLock on target relation, because of
2423 : * considerations around page deletion by VACUUM.
2424 : *
2425 : * NB: In general, page deletion deletes the right sibling's downlink, not
2426 : * the downlink of the page being deleted; the deleted page's downlink is
2427 : * reused for its sibling. The key space is thereby consolidated between
2428 : * the deleted page and its right sibling. (We cannot delete a parent
2429 : * page's rightmost child unless it is the last child page, and we intend
2430 : * to also delete the parent itself.)
2431 : *
2432 : * If this verification happened without a ShareLock, the following race
2433 : * condition could cause false positives:
2434 : *
2435 : * In general, concurrent page deletion might occur, including deletion of
2436 : * the left sibling of the child page that is examined here. If such a
2437 : * page deletion were to occur, closely followed by an insertion into the
2438 : * newly expanded key space of the child, a window for the false positive
2439 : * opens up: the stale parent/target downlink originally followed to get
2440 : * to the child legitimately ceases to be a lower bound on all items in
2441 : * the page, since the key space was concurrently expanded "left".
2442 : * (Insertion followed the "new" downlink for the child, not our now-stale
2443 : * downlink, which was concurrently physically removed in target/parent as
2444 : * part of deletion's first phase.)
2445 : *
2446 : * While we use various techniques elsewhere to perform cross-page
2447 : * verification for !readonly callers, a similar trick seems difficult
2448 : * here. The tricks used by bt_recheck_sibling_links and by
2449 : * bt_right_page_check_scankey both involve verification of a same-level,
2450 : * cross-sibling invariant. Cross-level invariants are far more squishy,
2451 : * though. The nbtree REDO routines do not actually couple buffer locks
2452 : * across levels during page splits, so making any cross-level check work
2453 : * reliably in !readonly mode may be impossible.
2454 : */
2455 : Assert(state->readonly);
2456 :
2457 : /*
2458 : * Verify child page has the downlink key from target page (its parent) as
2459 : * a lower bound; downlink must be strictly less than all keys on the
2460 : * page.
2461 : *
2462 : * Check all items, rather than checking just the first and trusting that
2463 : * the operator class obeys the transitive law.
2464 : */
2465 3722 : topaque = BTPageGetOpaque(state->target);
2466 3722 : child = palloc_btree_page(state, childblock);
2467 3722 : copaque = BTPageGetOpaque(child);
2468 3722 : maxoffset = PageGetMaxOffsetNumber(child);
2469 :
2470 : /*
2471 : * Since we've already loaded the child block, combine this check with
2472 : * check for downlink connectivity.
2473 : */
2474 3722 : bt_child_highkey_check(state, downlinkoffnum,
2475 : child, topaque->btpo_level);
2476 :
2477 : /*
2478 : * Since there cannot be a concurrent VACUUM operation in readonly mode,
2479 : * and since a page has no links within other pages (siblings and parent)
2480 : * once it is marked fully deleted, it should be impossible to land on a
2481 : * fully deleted page.
2482 : *
2483 : * It does not quite make sense to enforce that the page cannot even be
2484 : * half-dead, despite the fact the downlink is modified at the same stage
2485 : * that the child leaf page is marked half-dead. That's incorrect because
2486 : * there may occasionally be multiple downlinks from a chain of pages
2487 : * undergoing deletion, where multiple successive calls are made to
2488 : * _bt_unlink_halfdead_page() by VACUUM before it can finally safely mark
2489 : * the leaf page as fully dead. While _bt_mark_page_halfdead() usually
2490 : * removes the downlink to the leaf page that is marked half-dead, that's
2491 : * not guaranteed, so it's possible we'll land on a half-dead page with a
2492 : * downlink due to an interrupted multi-level page deletion.
2493 : *
2494 : * We go ahead with our checks if the child page is half-dead. It's safe
2495 : * to do so because we do not test the child's high key, so it does not
2496 : * matter that the original high key will have been replaced by a dummy
2497 : * truncated high key within _bt_mark_page_halfdead(). All other page
2498 : * items are left intact on a half-dead page, so there is still something
2499 : * to test.
2500 : */
2501 3722 : if (P_ISDELETED(copaque))
2502 0 : ereport(ERROR,
2503 : (errcode(ERRCODE_INDEX_CORRUPTED),
2504 : errmsg("downlink to deleted page found in index \"%s\"",
2505 : RelationGetRelationName(state->rel)),
2506 : errdetail_internal("Parent block=%u child block=%u parent page lsn=%X/%08X.",
2507 : state->targetblock, childblock,
2508 : LSN_FORMAT_ARGS(state->targetlsn))));
2509 :
2510 3722 : for (offset = P_FIRSTDATAKEY(copaque);
2511 1200700 : offset <= maxoffset;
2512 1196978 : offset = OffsetNumberNext(offset))
2513 : {
2514 : /*
2515 : * Skip comparison of target page key against "negative infinity"
2516 : * item, if any. Checking it would indicate that it's not a strict
2517 : * lower bound, but that's only because of the hard-coding for
2518 : * negative infinity items within _bt_compare().
2519 : *
2520 : * If nbtree didn't truncate negative infinity tuples during internal
2521 : * page splits then we'd expect child's negative infinity key to be
2522 : * equal to the scankey/downlink from target/parent (it would be a
2523 : * "low key" in this hypothetical scenario, and so it would still need
2524 : * to be treated as a special case here).
2525 : *
2526 : * Negative infinity items can be thought of as a strict lower bound
2527 : * that works transitively, with the last non-negative-infinity pivot
2528 : * followed during a descent from the root as its "true" strict lower
2529 : * bound. Only a small number of negative infinity items are truly
2530 : * negative infinity; those that are the first items of leftmost
2531 : * internal pages. In more general terms, a negative infinity item is
2532 : * only negative infinity with respect to the subtree that the page is
2533 : * at the root of.
2534 : *
2535 : * See also: bt_rootdescend(), which can even detect transitive
2536 : * inconsistencies on cousin leaf pages.
2537 : */
2538 1196978 : if (offset_is_negative_infinity(copaque, offset))
2539 2 : continue;
2540 :
2541 1196976 : if (!invariant_l_nontarget_offset(state, targetkey, childblock, child,
2542 : offset))
2543 0 : ereport(ERROR,
2544 : (errcode(ERRCODE_INDEX_CORRUPTED),
2545 : errmsg("down-link lower bound invariant violated for index \"%s\"",
2546 : RelationGetRelationName(state->rel)),
2547 : errdetail_internal("Parent block=%u child index tid=(%u,%u) parent page lsn=%X/%08X.",
2548 : state->targetblock, childblock, offset,
2549 : LSN_FORMAT_ARGS(state->targetlsn))));
2550 : }
2551 :
2552 3722 : pfree(child);
2553 3722 : }
2554 :
2555 : /*
2556 : * Checks if page is missing a downlink that it should have.
2557 : *
2558 : * A page that lacks a downlink/parent may indicate corruption. However, we
2559 : * must account for the fact that a missing downlink can occasionally be
2560 : * encountered in a non-corrupt index. This can be due to an interrupted page
2561 : * split, or an interrupted multi-level page deletion (i.e. there was a hard
2562 : * crash or an error during a page split, or while VACUUM was deleting a
2563 : * multi-level chain of pages).
2564 : *
2565 : * Note that this can only be called in readonly mode, so there is no need to
2566 : * be concerned about concurrent page splits or page deletions.
2567 : */
2568 : static void
2569 0 : bt_downlink_missing_check(BtreeCheckState *state, bool rightsplit,
2570 : BlockNumber blkno, Page page)
2571 : {
2572 0 : BTPageOpaque opaque = BTPageGetOpaque(page);
2573 : ItemId itemid;
2574 : IndexTuple itup;
2575 : Page child;
2576 : BTPageOpaque copaque;
2577 : uint32 level;
2578 : BlockNumber childblk;
2579 : XLogRecPtr pagelsn;
2580 :
2581 : Assert(state->readonly);
2582 : Assert(!P_IGNORE(opaque));
2583 :
2584 : /* No next level up with downlinks to fingerprint from the true root */
2585 0 : if (P_ISROOT(opaque))
2586 0 : return;
2587 :
2588 0 : pagelsn = PageGetLSN(page);
2589 :
2590 : /*
2591 : * Incomplete (interrupted) page splits can account for the lack of a
2592 : * downlink. Some inserting transaction should eventually complete the
2593 : * page split in passing, when it notices that the left sibling page is
2594 : * P_INCOMPLETE_SPLIT().
2595 : *
2596 : * In general, VACUUM is not prepared for there to be no downlink to a
2597 : * page that it deletes. This is the main reason why the lack of a
2598 : * downlink can be reported as corruption here. It's not obvious that an
2599 : * invalid missing downlink can result in wrong answers to queries,
2600 : * though, since index scans that land on the child may end up
2601 : * consistently moving right. The handling of concurrent page splits (and
2602 : * page deletions) within _bt_moveright() cannot distinguish
2603 : * inconsistencies that last for a moment from inconsistencies that are
2604 : * permanent and irrecoverable.
2605 : *
2606 : * VACUUM isn't even prepared to delete pages that have no downlink due to
2607 : * an incomplete page split, but it can detect and reason about that case
2608 : * by design, so it shouldn't be taken to indicate corruption. See
2609 : * _bt_pagedel() for full details.
2610 : */
2611 0 : if (rightsplit)
2612 : {
2613 0 : ereport(DEBUG1,
2614 : (errcode(ERRCODE_NO_DATA),
2615 : errmsg_internal("harmless interrupted page split detected in index \"%s\"",
2616 : RelationGetRelationName(state->rel)),
2617 : errdetail_internal("Block=%u level=%u left sibling=%u page lsn=%X/%08X.",
2618 : blkno, opaque->btpo_level,
2619 : opaque->btpo_prev,
2620 : LSN_FORMAT_ARGS(pagelsn))));
2621 0 : return;
2622 : }
2623 :
2624 : /*
2625 : * Page under check is probably the "top parent" of a multi-level page
2626 : * deletion. We'll need to descend the subtree to make sure that
2627 : * descendant pages are consistent with that, though.
2628 : *
2629 : * If the page (which must be non-ignorable) is a leaf page, then clearly
2630 : * it can't be the top parent. The lack of a downlink is probably a
2631 : * symptom of a broad problem that could just as easily cause
2632 : * inconsistencies anywhere else.
2633 : */
2634 0 : if (P_ISLEAF(opaque))
2635 0 : ereport(ERROR,
2636 : (errcode(ERRCODE_INDEX_CORRUPTED),
2637 : errmsg("leaf index block lacks downlink in index \"%s\"",
2638 : RelationGetRelationName(state->rel)),
2639 : errdetail_internal("Block=%u page lsn=%X/%08X.",
2640 : blkno,
2641 : LSN_FORMAT_ARGS(pagelsn))));
2642 :
2643 : /* Descend from the given page, which is an internal page */
2644 0 : elog(DEBUG1, "checking for interrupted multi-level deletion due to missing downlink in index \"%s\"",
2645 : RelationGetRelationName(state->rel));
2646 :
2647 0 : level = opaque->btpo_level;
2648 0 : itemid = PageGetItemIdCareful(state, blkno, page, P_FIRSTDATAKEY(opaque));
2649 0 : itup = (IndexTuple) PageGetItem(page, itemid);
2650 0 : childblk = BTreeTupleGetDownLink(itup);
2651 : for (;;)
2652 : {
2653 0 : CHECK_FOR_INTERRUPTS();
2654 :
2655 0 : child = palloc_btree_page(state, childblk);
2656 0 : copaque = BTPageGetOpaque(child);
2657 :
2658 0 : if (P_ISLEAF(copaque))
2659 0 : break;
2660 :
2661 : /* Do an extra sanity check in passing on internal pages */
2662 0 : if (copaque->btpo_level != level - 1)
2663 0 : ereport(ERROR,
2664 : (errcode(ERRCODE_INDEX_CORRUPTED),
2665 : errmsg_internal("downlink points to block in index \"%s\" whose level is not one level down",
2666 : RelationGetRelationName(state->rel)),
2667 : errdetail_internal("Top parent/under check block=%u block pointed to=%u expected level=%u level in pointed to block=%u.",
2668 : blkno, childblk,
2669 : level - 1, copaque->btpo_level)));
2670 :
2671 0 : level = copaque->btpo_level;
2672 0 : itemid = PageGetItemIdCareful(state, childblk, child,
2673 0 : P_FIRSTDATAKEY(copaque));
2674 0 : itup = (IndexTuple) PageGetItem(child, itemid);
2675 0 : childblk = BTreeTupleGetDownLink(itup);
2676 : /* Be slightly more pro-active in freeing this memory, just in case */
2677 0 : pfree(child);
2678 : }
2679 :
2680 : /*
2681 : * Since there cannot be a concurrent VACUUM operation in readonly mode,
2682 : * and since a page has no links within other pages (siblings and parent)
2683 : * once it is marked fully deleted, it should be impossible to land on a
2684 : * fully deleted page. See bt_child_check() for further details.
2685 : *
2686 : * The bt_child_check() P_ISDELETED() check is repeated here because
2687 : * bt_child_check() does not visit pages reachable through negative
2688 : * infinity items. Besides, bt_child_check() is unwilling to descend
2689 : * multiple levels. (The similar bt_child_check() P_ISDELETED() check
2690 : * within bt_check_level_from_leftmost() won't reach the page either,
2691 : * since the leaf's live siblings should have their sibling links updated
2692 : * to bypass the deletion target page when it is marked fully dead.)
2693 : *
2694 : * If this error is raised, it might be due to a previous multi-level page
2695 : * deletion that failed to realize that it wasn't yet safe to mark the
2696 : * leaf page as fully dead. A "dangling downlink" will still remain when
2697 : * this happens. The fact that the dangling downlink's page (the leaf's
2698 : * parent/ancestor page) lacked a downlink is incidental.
2699 : */
2700 0 : if (P_ISDELETED(copaque))
2701 0 : ereport(ERROR,
2702 : (errcode(ERRCODE_INDEX_CORRUPTED),
2703 : errmsg_internal("downlink to deleted leaf page found in index \"%s\"",
2704 : RelationGetRelationName(state->rel)),
2705 : errdetail_internal("Top parent/target block=%u leaf block=%u top parent/under check lsn=%X/%08X.",
2706 : blkno, childblk,
2707 : LSN_FORMAT_ARGS(pagelsn))));
2708 :
2709 : /*
2710 : * Iff leaf page is half-dead, its high key top parent link should point
2711 : * to what VACUUM considered to be the top parent page at the instant it
2712 : * was interrupted. Provided the high key link actually points to the
2713 : * page under check, the missing downlink we detected is consistent with
2714 : * there having been an interrupted multi-level page deletion. This means
2715 : * that the subtree with the page under check at its root (a page deletion
2716 : * chain) is in a consistent state, enabling VACUUM to resume deleting the
2717 : * entire chain the next time it encounters the half-dead leaf page.
2718 : */
2719 0 : if (P_ISHALFDEAD(copaque) && !P_RIGHTMOST(copaque))
2720 : {
2721 0 : itemid = PageGetItemIdCareful(state, childblk, child, P_HIKEY);
2722 0 : itup = (IndexTuple) PageGetItem(child, itemid);
2723 0 : if (BTreeTupleGetTopParent(itup) == blkno)
2724 0 : return;
2725 : }
2726 :
2727 0 : ereport(ERROR,
2728 : (errcode(ERRCODE_INDEX_CORRUPTED),
2729 : errmsg("internal index block lacks downlink in index \"%s\"",
2730 : RelationGetRelationName(state->rel)),
2731 : errdetail_internal("Block=%u level=%u page lsn=%X/%08X.",
2732 : blkno, opaque->btpo_level,
2733 : LSN_FORMAT_ARGS(pagelsn))));
2734 : }
2735 :
2736 : /*
2737 : * Per-tuple callback from table_index_build_scan, used to determine if index has
2738 : * all the entries that definitely should have been observed in leaf pages of
2739 : * the target index (that is, all IndexTuples that were fingerprinted by our
2740 : * Bloom filter). All heapallindexed checks occur here.
2741 : *
2742 : * The redundancy between an index and the table it indexes provides a good
2743 : * opportunity to detect corruption, especially corruption within the table.
2744 : * The high level principle behind the verification performed here is that any
2745 : * IndexTuple that should be in an index following a fresh CREATE INDEX (based
2746 : * on the same index definition) should also have been in the original,
2747 : * existing index, which should have used exactly the same representation
2748 : *
2749 : * Since the overall structure of the index has already been verified, the most
2750 : * likely explanation for error here is a corrupt heap page (could be logical
2751 : * or physical corruption). Index corruption may still be detected here,
2752 : * though. Only readonly callers will have verified that left links and right
2753 : * links are in agreement, and so it's possible that a leaf page transposition
2754 : * within index is actually the source of corruption detected here (for
2755 : * !readonly callers). The checks performed only for readonly callers might
2756 : * more accurately frame the problem as a cross-page invariant issue (this
2757 : * could even be due to recovery not replaying all WAL records). The !readonly
2758 : * ERROR message raised here includes a HINT about retrying with readonly
2759 : * verification, just in case it's a cross-page invariant issue, though that
2760 : * isn't particularly likely.
2761 : *
2762 : * table_index_build_scan() expects to be able to find the root tuple when a
2763 : * heap-only tuple (the live tuple at the end of some HOT chain) needs to be
2764 : * indexed, in order to replace the actual tuple's TID with the root tuple's
2765 : * TID (which is what we're actually passed back here). The index build heap
2766 : * scan code will raise an error when a tuple that claims to be the root of the
2767 : * heap-only tuple's HOT chain cannot be located. This catches cases where the
2768 : * original root item offset/root tuple for a HOT chain indicates (for whatever
2769 : * reason) that the entire HOT chain is dead, despite the fact that the latest
2770 : * heap-only tuple should be indexed. When this happens, sequential scans may
2771 : * always give correct answers, and all indexes may be considered structurally
2772 : * consistent (i.e. the nbtree structural checks would not detect corruption).
2773 : * It may be the case that only index scans give wrong answers, and yet heap or
2774 : * SLRU corruption is the real culprit. (While it's true that LP_DEAD bit
2775 : * setting will probably also leave the index in a corrupt state before too
2776 : * long, the problem is nonetheless that there is heap corruption.)
2777 : *
2778 : * Heap-only tuple handling within table_index_build_scan() works in a way that
2779 : * helps us to detect index tuples that contain the wrong values (values that
2780 : * don't match the latest tuple in the HOT chain). This can happen when there
2781 : * is no superseding index tuple due to a faulty assessment of HOT safety,
2782 : * perhaps during the original CREATE INDEX. Because the latest tuple's
2783 : * contents are used with the root TID, an error will be raised when a tuple
2784 : * with the same TID but non-matching attribute values is passed back to us.
2785 : * Faulty assessment of HOT-safety was behind at least two distinct CREATE
2786 : * INDEX CONCURRENTLY bugs that made it into stable releases, one of which was
2787 : * undetected for many years. In short, the same principle that allows a
2788 : * REINDEX to repair corruption when there was an (undetected) broken HOT chain
2789 : * also allows us to detect the corruption in many cases.
2790 : */
2791 : static void
2792 1060568 : bt_tuple_present_callback(Relation index, ItemPointer tid, Datum *values,
2793 : bool *isnull, bool tupleIsAlive, void *checkstate)
2794 : {
2795 1060568 : BtreeCheckState *state = (BtreeCheckState *) checkstate;
2796 : IndexTuple itup,
2797 : norm;
2798 :
2799 : Assert(state->heapallindexed);
2800 :
2801 : /* Generate a normalized index tuple for fingerprinting */
2802 1060568 : itup = index_form_tuple(RelationGetDescr(index), values, isnull);
2803 1060568 : itup->t_tid = *tid;
2804 1060568 : norm = bt_normalize_tuple(state, itup);
2805 :
2806 : /* Probe Bloom filter -- tuple should be present */
2807 1060568 : if (bloom_lacks_element(state->filter, (unsigned char *) norm,
2808 : IndexTupleSize(norm)))
2809 0 : ereport(ERROR,
2810 : (errcode(ERRCODE_DATA_CORRUPTED),
2811 : errmsg("heap tuple (%u,%u) from table \"%s\" lacks matching index tuple within index \"%s\"",
2812 : ItemPointerGetBlockNumber(&(itup->t_tid)),
2813 : ItemPointerGetOffsetNumber(&(itup->t_tid)),
2814 : RelationGetRelationName(state->heaprel),
2815 : RelationGetRelationName(state->rel)),
2816 : !state->readonly
2817 : ? errhint("Retrying verification using the function bt_index_parent_check() might provide a more specific error.")
2818 : : 0));
2819 :
2820 1060568 : state->heaptuplespresent++;
2821 1060568 : pfree(itup);
2822 : /* Cannot leak memory here */
2823 1060568 : if (norm != itup)
2824 10 : pfree(norm);
2825 1060568 : }
2826 :
2827 : /*
2828 : * Normalize an index tuple for fingerprinting.
2829 : *
2830 : * In general, index tuple formation is assumed to be deterministic by
2831 : * heapallindexed verification, and IndexTuples are assumed immutable. While
2832 : * the LP_DEAD bit is mutable in leaf pages, that's ItemId metadata, which is
2833 : * not fingerprinted. Normalization is required to compensate for corner
2834 : * cases where the determinism assumption doesn't quite work.
2835 : *
2836 : * There is currently one such case: index_form_tuple() does not try to hide
2837 : * the source TOAST state of input datums. The executor applies TOAST
2838 : * compression for heap tuples based on different criteria to the compression
2839 : * applied within btinsert()'s call to index_form_tuple(): it sometimes
2840 : * compresses more aggressively, resulting in compressed heap tuple datums but
2841 : * uncompressed corresponding index tuple datums. A subsequent heapallindexed
2842 : * verification will get a logically equivalent though bitwise unequal tuple
2843 : * from index_form_tuple(). False positive heapallindexed corruption reports
2844 : * could occur without normalizing away the inconsistency.
2845 : *
2846 : * Returned tuple is often caller's own original tuple. Otherwise, it is a
2847 : * new representation of caller's original index tuple, palloc()'d in caller's
2848 : * memory context.
2849 : *
2850 : * Note: This routine is not concerned with distinctions about the
2851 : * representation of tuples beyond those that might break heapallindexed
2852 : * verification. In particular, it won't try to normalize opclass-equal
2853 : * datums with potentially distinct representations (e.g., btree/numeric_ops
2854 : * index datums will not get their display scale normalized-away here).
2855 : * Caller does normalization for non-pivot tuples that have a posting list,
2856 : * since dummy CREATE INDEX callback code generates new tuples with the same
2857 : * normalized representation.
2858 : */
2859 : static IndexTuple
2860 2125922 : bt_normalize_tuple(BtreeCheckState *state, IndexTuple itup)
2861 : {
2862 2125922 : TupleDesc tupleDescriptor = RelationGetDescr(state->rel);
2863 : Datum normalized[INDEX_MAX_KEYS];
2864 : bool isnull[INDEX_MAX_KEYS];
2865 : bool need_free[INDEX_MAX_KEYS];
2866 2125922 : bool formnewtup = false;
2867 : IndexTuple reformed;
2868 : int i;
2869 :
2870 : /* Caller should only pass "logical" non-pivot tuples here */
2871 : Assert(!BTreeTupleIsPosting(itup) && !BTreeTupleIsPivot(itup));
2872 :
2873 : /* Easy case: It's immediately clear that tuple has no varlena datums */
2874 2125922 : if (!IndexTupleHasVarwidths(itup))
2875 2125874 : return itup;
2876 :
2877 96 : for (i = 0; i < tupleDescriptor->natts; i++)
2878 : {
2879 : Form_pg_attribute att;
2880 :
2881 48 : att = TupleDescAttr(tupleDescriptor, i);
2882 :
2883 : /* Assume untoasted/already normalized datum initially */
2884 48 : need_free[i] = false;
2885 48 : normalized[i] = index_getattr(itup, att->attnum,
2886 : tupleDescriptor,
2887 : &isnull[i]);
2888 48 : if (att->attbyval || att->attlen != -1 || isnull[i])
2889 0 : continue;
2890 :
2891 : /*
2892 : * Callers always pass a tuple that could safely be inserted into the
2893 : * index without further processing, so an external varlena header
2894 : * should never be encountered here
2895 : */
2896 48 : if (VARATT_IS_EXTERNAL(DatumGetPointer(normalized[i])))
2897 0 : ereport(ERROR,
2898 : (errcode(ERRCODE_INDEX_CORRUPTED),
2899 : errmsg("external varlena datum in tuple that references heap row (%u,%u) in index \"%s\"",
2900 : ItemPointerGetBlockNumber(&(itup->t_tid)),
2901 : ItemPointerGetOffsetNumber(&(itup->t_tid)),
2902 : RelationGetRelationName(state->rel))));
2903 48 : else if (!VARATT_IS_COMPRESSED(DatumGetPointer(normalized[i])) &&
2904 44 : VARSIZE(DatumGetPointer(normalized[i])) > TOAST_INDEX_TARGET &&
2905 42 : (att->attstorage == TYPSTORAGE_EXTENDED ||
2906 32 : att->attstorage == TYPSTORAGE_MAIN))
2907 : {
2908 : /*
2909 : * This value will be compressed by index_form_tuple() with the
2910 : * current storage settings. We may be here because this tuple
2911 : * was formed with different storage settings. So, force forming.
2912 : */
2913 10 : formnewtup = true;
2914 : }
2915 38 : else if (VARATT_IS_COMPRESSED(DatumGetPointer(normalized[i])))
2916 : {
2917 4 : formnewtup = true;
2918 4 : normalized[i] = PointerGetDatum(PG_DETOAST_DATUM(normalized[i]));
2919 4 : need_free[i] = true;
2920 : }
2921 :
2922 : /*
2923 : * Short tuples may have 1B or 4B header. Convert 4B header of short
2924 : * tuples to 1B
2925 : */
2926 34 : else if (VARATT_CAN_MAKE_SHORT(DatumGetPointer(normalized[i])))
2927 : {
2928 : /* convert to short varlena */
2929 2 : Size len = VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(normalized[i]));
2930 2 : char *data = palloc(len);
2931 :
2932 2 : SET_VARSIZE_SHORT(data, len);
2933 2 : memcpy(data + 1, VARDATA(DatumGetPointer(normalized[i])), len - 1);
2934 :
2935 2 : formnewtup = true;
2936 2 : normalized[i] = PointerGetDatum(data);
2937 2 : need_free[i] = true;
2938 : }
2939 : }
2940 :
2941 : /*
2942 : * Easier case: Tuple has varlena datums, none of which are compressed or
2943 : * short with 4B header
2944 : */
2945 48 : if (!formnewtup)
2946 32 : return itup;
2947 :
2948 : /*
2949 : * Hard case: Tuple had compressed varlena datums that necessitate
2950 : * creating normalized version of the tuple from uncompressed input datums
2951 : * (normalized input datums). This is rather naive, but shouldn't be
2952 : * necessary too often.
2953 : *
2954 : * In the heap, tuples may contain short varlena datums with both 1B
2955 : * header and 4B headers. But the corresponding index tuple should always
2956 : * have such varlena's with 1B headers. So, if there is a short varlena
2957 : * with 4B header, we need to convert it for fingerprinting.
2958 : *
2959 : * Note that we rely on deterministic index_form_tuple() TOAST compression
2960 : * of normalized input.
2961 : */
2962 16 : reformed = index_form_tuple(tupleDescriptor, normalized, isnull);
2963 16 : reformed->t_tid = itup->t_tid;
2964 :
2965 : /* Cannot leak memory here */
2966 32 : for (i = 0; i < tupleDescriptor->natts; i++)
2967 16 : if (need_free[i])
2968 6 : pfree(DatumGetPointer(normalized[i]));
2969 :
2970 16 : return reformed;
2971 : }
2972 :
2973 : /*
2974 : * Produce palloc()'d "plain" tuple for nth posting list entry/TID.
2975 : *
2976 : * In general, deduplication is not supposed to change the logical contents of
2977 : * an index. Multiple index tuples are merged together into one equivalent
2978 : * posting list index tuple when convenient.
2979 : *
2980 : * heapallindexed verification must normalize-away this variation in
2981 : * representation by converting posting list tuples into two or more "plain"
2982 : * tuples. Each tuple must be fingerprinted separately -- there must be one
2983 : * tuple for each corresponding Bloom filter probe during the heap scan.
2984 : *
2985 : * Note: Caller still needs to call bt_normalize_tuple() with returned tuple.
2986 : */
2987 : static inline IndexTuple
2988 51800 : bt_posting_plain_tuple(IndexTuple itup, int n)
2989 : {
2990 : Assert(BTreeTupleIsPosting(itup));
2991 :
2992 : /* Returns non-posting-list tuple */
2993 51800 : return _bt_form_posting(itup, BTreeTupleGetPostingN(itup, n), 1);
2994 : }
2995 :
2996 : /*
2997 : * Search for itup in index, starting from fast root page. itup must be a
2998 : * non-pivot tuple. This is only supported with heapkeyspace indexes, since
2999 : * we rely on having fully unique keys to find a match with only a single
3000 : * visit to a leaf page, barring an interrupted page split, where we may have
3001 : * to move right. (A concurrent page split is impossible because caller must
3002 : * be readonly caller.)
3003 : *
3004 : * This routine can detect very subtle transitive consistency issues across
3005 : * more than one level of the tree. Leaf pages all have a high key (even the
3006 : * rightmost page has a conceptual positive infinity high key), but not a low
3007 : * key. Their downlink in parent is a lower bound, which along with the high
3008 : * key is almost enough to detect every possible inconsistency. A downlink
3009 : * separator key value won't always be available from parent, though, because
3010 : * the first items of internal pages are negative infinity items, truncated
3011 : * down to zero attributes during internal page splits. While it's true that
3012 : * bt_child_check() and the high key check can detect most imaginable key
3013 : * space problems, there are remaining problems it won't detect with non-pivot
3014 : * tuples in cousin leaf pages. Starting a search from the root for every
3015 : * existing leaf tuple detects small inconsistencies in upper levels of the
3016 : * tree that cannot be detected any other way. (Besides all this, this is
3017 : * probably also useful as a direct test of the code used by index scans
3018 : * themselves.)
3019 : */
3020 : static bool
3021 402196 : bt_rootdescend(BtreeCheckState *state, IndexTuple itup)
3022 : {
3023 : BTScanInsert key;
3024 : BTStack stack;
3025 : Buffer lbuf;
3026 : bool exists;
3027 :
3028 402196 : key = _bt_mkscankey(state->rel, itup);
3029 : Assert(key->heapkeyspace && key->scantid != NULL);
3030 :
3031 : /*
3032 : * Search from root.
3033 : *
3034 : * Ideally, we would arrange to only move right within _bt_search() when
3035 : * an interrupted page split is detected (i.e. when the incomplete split
3036 : * bit is found to be set), but for now we accept the possibility that
3037 : * that could conceal an inconsistency.
3038 : */
3039 : Assert(state->readonly && state->rootdescend);
3040 402196 : exists = false;
3041 402196 : stack = _bt_search(state->rel, NULL, key, &lbuf, BT_READ);
3042 :
3043 402196 : if (BufferIsValid(lbuf))
3044 : {
3045 : BTInsertStateData insertstate;
3046 : OffsetNumber offnum;
3047 : Page page;
3048 :
3049 402196 : insertstate.itup = itup;
3050 402196 : insertstate.itemsz = MAXALIGN(IndexTupleSize(itup));
3051 402196 : insertstate.itup_key = key;
3052 402196 : insertstate.postingoff = 0;
3053 402196 : insertstate.bounds_valid = false;
3054 402196 : insertstate.buf = lbuf;
3055 :
3056 : /* Get matching tuple on leaf page */
3057 402196 : offnum = _bt_binsrch_insert(state->rel, &insertstate);
3058 : /* Compare first >= matching item on leaf page, if any */
3059 402196 : page = BufferGetPage(lbuf);
3060 : /* Should match on first heap TID when tuple has a posting list */
3061 402196 : if (offnum <= PageGetMaxOffsetNumber(page) &&
3062 804392 : insertstate.postingoff <= 0 &&
3063 402196 : _bt_compare(state->rel, key, page, offnum) == 0)
3064 402196 : exists = true;
3065 402196 : _bt_relbuf(state->rel, lbuf);
3066 : }
3067 :
3068 402196 : _bt_freestack(stack);
3069 402196 : pfree(key);
3070 :
3071 402196 : return exists;
3072 : }
3073 :
3074 : /*
3075 : * Is particular offset within page (whose special state is passed by caller)
3076 : * the page negative-infinity item?
3077 : *
3078 : * As noted in comments above _bt_compare(), there is special handling of the
3079 : * first data item as a "negative infinity" item. The hard-coding within
3080 : * _bt_compare() makes comparing this item for the purposes of verification
3081 : * pointless at best, since the IndexTuple only contains a valid TID (a
3082 : * reference TID to child page).
3083 : */
3084 : static inline bool
3085 5244964 : offset_is_negative_infinity(BTPageOpaque opaque, OffsetNumber offset)
3086 : {
3087 : /*
3088 : * For internal pages only, the first item after high key, if any, is
3089 : * negative infinity item. Internal pages always have a negative infinity
3090 : * item, whereas leaf pages never have one. This implies that negative
3091 : * infinity item is either first or second line item, or there is none
3092 : * within page.
3093 : *
3094 : * Negative infinity items are a special case among pivot tuples. They
3095 : * always have zero attributes, while all other pivot tuples always have
3096 : * nkeyatts attributes.
3097 : *
3098 : * Right-most pages don't have a high key, but could be said to
3099 : * conceptually have a "positive infinity" high key. Thus, there is a
3100 : * symmetry between down link items in parent pages, and high keys in
3101 : * children. Together, they represent the part of the key space that
3102 : * belongs to each page in the index. For example, all children of the
3103 : * root page will have negative infinity as a lower bound from root
3104 : * negative infinity downlink, and positive infinity as an upper bound
3105 : * (implicitly, from "imaginary" positive infinity high key in root).
3106 : */
3107 5244964 : return !P_ISLEAF(opaque) && offset == P_FIRSTDATAKEY(opaque);
3108 : }
3109 :
3110 : /*
3111 : * Does the invariant hold that the key is strictly less than a given upper
3112 : * bound offset item?
3113 : *
3114 : * Verifies line pointer on behalf of caller.
3115 : *
3116 : * If this function returns false, convention is that caller throws error due
3117 : * to corruption.
3118 : */
3119 : static inline bool
3120 4026090 : invariant_l_offset(BtreeCheckState *state, BTScanInsert key,
3121 : OffsetNumber upperbound)
3122 : {
3123 : ItemId itemid;
3124 : int32 cmp;
3125 :
3126 : Assert(!key->nextkey && key->backward);
3127 :
3128 : /* Verify line pointer before checking tuple */
3129 4026090 : itemid = PageGetItemIdCareful(state, state->targetblock, state->target,
3130 : upperbound);
3131 : /* pg_upgrade'd indexes may legally have equal sibling tuples */
3132 4026090 : if (!key->heapkeyspace)
3133 0 : return invariant_leq_offset(state, key, upperbound);
3134 :
3135 4026090 : cmp = _bt_compare(state->rel, key, state->target, upperbound);
3136 :
3137 : /*
3138 : * _bt_compare() is capable of determining that a scankey with a
3139 : * filled-out attribute is greater than pivot tuples where the comparison
3140 : * is resolved at a truncated attribute (value of attribute in pivot is
3141 : * minus infinity). However, it is not capable of determining that a
3142 : * scankey is _less than_ a tuple on the basis of a comparison resolved at
3143 : * _scankey_ minus infinity attribute. Complete an extra step to simulate
3144 : * having minus infinity values for omitted scankey attribute(s).
3145 : */
3146 4026090 : if (cmp == 0)
3147 : {
3148 : BTPageOpaque topaque;
3149 : IndexTuple ritup;
3150 : int uppnkeyatts;
3151 : ItemPointer rheaptid;
3152 : bool nonpivot;
3153 :
3154 0 : ritup = (IndexTuple) PageGetItem(state->target, itemid);
3155 0 : topaque = BTPageGetOpaque(state->target);
3156 0 : nonpivot = P_ISLEAF(topaque) && upperbound >= P_FIRSTDATAKEY(topaque);
3157 :
3158 : /* Get number of keys + heap TID for item to the right */
3159 0 : uppnkeyatts = BTreeTupleGetNKeyAtts(ritup, state->rel);
3160 0 : rheaptid = BTreeTupleGetHeapTIDCareful(state, ritup, nonpivot);
3161 :
3162 : /* Heap TID is tiebreaker key attribute */
3163 0 : if (key->keysz == uppnkeyatts)
3164 0 : return key->scantid == NULL && rheaptid != NULL;
3165 :
3166 0 : return key->keysz < uppnkeyatts;
3167 : }
3168 :
3169 4026090 : return cmp < 0;
3170 : }
3171 :
3172 : /*
3173 : * Does the invariant hold that the key is less than or equal to a given upper
3174 : * bound offset item?
3175 : *
3176 : * Caller should have verified that upperbound's line pointer is consistent
3177 : * using PageGetItemIdCareful() call.
3178 : *
3179 : * If this function returns false, convention is that caller throws error due
3180 : * to corruption.
3181 : */
3182 : static inline bool
3183 3699312 : invariant_leq_offset(BtreeCheckState *state, BTScanInsert key,
3184 : OffsetNumber upperbound)
3185 : {
3186 : int32 cmp;
3187 :
3188 : Assert(!key->nextkey && key->backward);
3189 :
3190 3699312 : cmp = _bt_compare(state->rel, key, state->target, upperbound);
3191 :
3192 3699312 : return cmp <= 0;
3193 : }
3194 :
3195 : /*
3196 : * Does the invariant hold that the key is strictly greater than a given lower
3197 : * bound offset item?
3198 : *
3199 : * Caller should have verified that lowerbound's line pointer is consistent
3200 : * using PageGetItemIdCareful() call.
3201 : *
3202 : * If this function returns false, convention is that caller throws error due
3203 : * to corruption.
3204 : */
3205 : static inline bool
3206 13228 : invariant_g_offset(BtreeCheckState *state, BTScanInsert key,
3207 : OffsetNumber lowerbound)
3208 : {
3209 : int32 cmp;
3210 :
3211 : Assert(!key->nextkey && key->backward);
3212 :
3213 13228 : cmp = _bt_compare(state->rel, key, state->target, lowerbound);
3214 :
3215 : /* pg_upgrade'd indexes may legally have equal sibling tuples */
3216 13228 : if (!key->heapkeyspace)
3217 0 : return cmp >= 0;
3218 :
3219 : /*
3220 : * No need to consider the possibility that scankey has attributes that we
3221 : * need to force to be interpreted as negative infinity. _bt_compare() is
3222 : * able to determine that scankey is greater than negative infinity. The
3223 : * distinction between "==" and "<" isn't interesting here, since
3224 : * corruption is indicated either way.
3225 : */
3226 13228 : return cmp > 0;
3227 : }
3228 :
3229 : /*
3230 : * Does the invariant hold that the key is strictly less than a given upper
3231 : * bound offset item, with the offset relating to a caller-supplied page that
3232 : * is not the current target page?
3233 : *
3234 : * Caller's non-target page is a child page of the target, checked as part of
3235 : * checking a property of the target page (i.e. the key comes from the
3236 : * target). Verifies line pointer on behalf of caller.
3237 : *
3238 : * If this function returns false, convention is that caller throws error due
3239 : * to corruption.
3240 : */
3241 : static inline bool
3242 1196976 : invariant_l_nontarget_offset(BtreeCheckState *state, BTScanInsert key,
3243 : BlockNumber nontargetblock, Page nontarget,
3244 : OffsetNumber upperbound)
3245 : {
3246 : ItemId itemid;
3247 : int32 cmp;
3248 :
3249 : Assert(!key->nextkey && key->backward);
3250 :
3251 : /* Verify line pointer before checking tuple */
3252 1196976 : itemid = PageGetItemIdCareful(state, nontargetblock, nontarget,
3253 : upperbound);
3254 1196976 : cmp = _bt_compare(state->rel, key, nontarget, upperbound);
3255 :
3256 : /* pg_upgrade'd indexes may legally have equal sibling tuples */
3257 1196976 : if (!key->heapkeyspace)
3258 0 : return cmp <= 0;
3259 :
3260 : /* See invariant_l_offset() for an explanation of this extra step */
3261 1196976 : if (cmp == 0)
3262 : {
3263 : IndexTuple child;
3264 : int uppnkeyatts;
3265 : ItemPointer childheaptid;
3266 : BTPageOpaque copaque;
3267 : bool nonpivot;
3268 :
3269 3720 : child = (IndexTuple) PageGetItem(nontarget, itemid);
3270 3720 : copaque = BTPageGetOpaque(nontarget);
3271 3720 : nonpivot = P_ISLEAF(copaque) && upperbound >= P_FIRSTDATAKEY(copaque);
3272 :
3273 : /* Get number of keys + heap TID for child/non-target item */
3274 3720 : uppnkeyatts = BTreeTupleGetNKeyAtts(child, state->rel);
3275 3720 : childheaptid = BTreeTupleGetHeapTIDCareful(state, child, nonpivot);
3276 :
3277 : /* Heap TID is tiebreaker key attribute */
3278 3720 : if (key->keysz == uppnkeyatts)
3279 3720 : return key->scantid == NULL && childheaptid != NULL;
3280 :
3281 0 : return key->keysz < uppnkeyatts;
3282 : }
3283 :
3284 1193256 : return cmp < 0;
3285 : }
3286 :
3287 : /*
3288 : * Given a block number of a B-Tree page, return page in palloc()'d memory.
3289 : * While at it, perform some basic checks of the page.
3290 : *
3291 : * There is never an attempt to get a consistent view of multiple pages using
3292 : * multiple concurrent buffer locks; in general, we only acquire a single pin
3293 : * and buffer lock at a time, which is often all that the nbtree code requires.
3294 : * (Actually, bt_recheck_sibling_links couples buffer locks, which is the only
3295 : * exception to this general rule.)
3296 : *
3297 : * Operating on a copy of the page is useful because it prevents control
3298 : * getting stuck in an uninterruptible state when an underlying operator class
3299 : * misbehaves.
3300 : */
3301 : static Page
3302 43140 : palloc_btree_page(BtreeCheckState *state, BlockNumber blocknum)
3303 : {
3304 : Buffer buffer;
3305 : Page page;
3306 : BTPageOpaque opaque;
3307 : OffsetNumber maxoffset;
3308 :
3309 43140 : page = palloc(BLCKSZ);
3310 :
3311 : /*
3312 : * We copy the page into local storage to avoid holding pin on the buffer
3313 : * longer than we must.
3314 : */
3315 43140 : buffer = ReadBufferExtended(state->rel, MAIN_FORKNUM, blocknum, RBM_NORMAL,
3316 : state->checkstrategy);
3317 43116 : LockBuffer(buffer, BT_READ);
3318 :
3319 : /*
3320 : * Perform the same basic sanity checking that nbtree itself performs for
3321 : * every page:
3322 : */
3323 43116 : _bt_checkpage(state->rel, buffer);
3324 :
3325 : /* Only use copy of page in palloc()'d memory */
3326 43116 : memcpy(page, BufferGetPage(buffer), BLCKSZ);
3327 43116 : UnlockReleaseBuffer(buffer);
3328 :
3329 43116 : opaque = BTPageGetOpaque(page);
3330 :
3331 43116 : if (P_ISMETA(opaque) && blocknum != BTREE_METAPAGE)
3332 0 : ereport(ERROR,
3333 : (errcode(ERRCODE_INDEX_CORRUPTED),
3334 : errmsg("invalid meta page found at block %u in index \"%s\"",
3335 : blocknum, RelationGetRelationName(state->rel))));
3336 :
3337 : /* Check page from block that ought to be meta page */
3338 43116 : if (blocknum == BTREE_METAPAGE)
3339 : {
3340 7932 : BTMetaPageData *metad = BTPageGetMeta(page);
3341 :
3342 7932 : if (!P_ISMETA(opaque) ||
3343 7932 : metad->btm_magic != BTREE_MAGIC)
3344 0 : ereport(ERROR,
3345 : (errcode(ERRCODE_INDEX_CORRUPTED),
3346 : errmsg("index \"%s\" meta page is corrupt",
3347 : RelationGetRelationName(state->rel))));
3348 :
3349 7932 : if (metad->btm_version < BTREE_MIN_VERSION ||
3350 7932 : metad->btm_version > BTREE_VERSION)
3351 0 : ereport(ERROR,
3352 : (errcode(ERRCODE_INDEX_CORRUPTED),
3353 : errmsg("version mismatch in index \"%s\": file version %d, "
3354 : "current version %d, minimum supported version %d",
3355 : RelationGetRelationName(state->rel),
3356 : metad->btm_version, BTREE_VERSION,
3357 : BTREE_MIN_VERSION)));
3358 :
3359 : /* Finished with metapage checks */
3360 7932 : return page;
3361 : }
3362 :
3363 : /*
3364 : * Deleted pages that still use the old 32-bit XID representation have no
3365 : * sane "level" field because they type pun the field, but all other pages
3366 : * (including pages deleted on Postgres 14+) have a valid value.
3367 : */
3368 35184 : if (!P_ISDELETED(opaque) || P_HAS_FULLXID(opaque))
3369 : {
3370 : /* Okay, no reason not to trust btpo_level field from page */
3371 :
3372 35184 : if (P_ISLEAF(opaque) && opaque->btpo_level != 0)
3373 0 : ereport(ERROR,
3374 : (errcode(ERRCODE_INDEX_CORRUPTED),
3375 : errmsg_internal("invalid leaf page level %u for block %u in index \"%s\"",
3376 : opaque->btpo_level, blocknum,
3377 : RelationGetRelationName(state->rel))));
3378 :
3379 35184 : if (!P_ISLEAF(opaque) && opaque->btpo_level == 0)
3380 0 : ereport(ERROR,
3381 : (errcode(ERRCODE_INDEX_CORRUPTED),
3382 : errmsg_internal("invalid internal page level 0 for block %u in index \"%s\"",
3383 : blocknum,
3384 : RelationGetRelationName(state->rel))));
3385 : }
3386 :
3387 : /*
3388 : * Sanity checks for number of items on page.
3389 : *
3390 : * As noted at the beginning of _bt_binsrch(), an internal page must have
3391 : * children, since there must always be a negative infinity downlink
3392 : * (there may also be a highkey). In the case of non-rightmost leaf
3393 : * pages, there must be at least a highkey. The exceptions are deleted
3394 : * pages, which contain no items.
3395 : *
3396 : * This is correct when pages are half-dead, since internal pages are
3397 : * never half-dead, and leaf pages must have a high key when half-dead
3398 : * (the rightmost page can never be deleted). It's also correct with
3399 : * fully deleted pages: _bt_unlink_halfdead_page() doesn't change anything
3400 : * about the target page other than setting the page as fully dead, and
3401 : * setting its xact field. In particular, it doesn't change the sibling
3402 : * links in the deletion target itself, since they're required when index
3403 : * scans land on the deletion target, and then need to move right (or need
3404 : * to move left, in the case of backward index scans).
3405 : */
3406 35184 : maxoffset = PageGetMaxOffsetNumber(page);
3407 35184 : if (maxoffset > MaxIndexTuplesPerPage)
3408 0 : ereport(ERROR,
3409 : (errcode(ERRCODE_INDEX_CORRUPTED),
3410 : errmsg("Number of items on block %u of index \"%s\" exceeds MaxIndexTuplesPerPage (%u)",
3411 : blocknum, RelationGetRelationName(state->rel),
3412 : MaxIndexTuplesPerPage)));
3413 :
3414 35184 : if (!P_ISLEAF(opaque) && !P_ISDELETED(opaque) && maxoffset < P_FIRSTDATAKEY(opaque))
3415 0 : ereport(ERROR,
3416 : (errcode(ERRCODE_INDEX_CORRUPTED),
3417 : errmsg("internal block %u in index \"%s\" lacks high key and/or at least one downlink",
3418 : blocknum, RelationGetRelationName(state->rel))));
3419 :
3420 35184 : if (P_ISLEAF(opaque) && !P_ISDELETED(opaque) && !P_RIGHTMOST(opaque) && maxoffset < P_HIKEY)
3421 0 : ereport(ERROR,
3422 : (errcode(ERRCODE_INDEX_CORRUPTED),
3423 : errmsg("non-rightmost leaf block %u in index \"%s\" lacks high key item",
3424 : blocknum, RelationGetRelationName(state->rel))));
3425 :
3426 : /*
3427 : * In general, internal pages are never marked half-dead, except on
3428 : * versions of Postgres prior to 9.4, where it can be valid transient
3429 : * state. This state is nonetheless treated as corruption by VACUUM on
3430 : * from version 9.4 on, so do the same here. See _bt_pagedel() for full
3431 : * details.
3432 : */
3433 35184 : if (!P_ISLEAF(opaque) && P_ISHALFDEAD(opaque))
3434 0 : ereport(ERROR,
3435 : (errcode(ERRCODE_INDEX_CORRUPTED),
3436 : errmsg("internal page block %u in index \"%s\" is half-dead",
3437 : blocknum, RelationGetRelationName(state->rel)),
3438 : errhint("This can be caused by an interrupted VACUUM in version 9.3 or older, before upgrade. Please REINDEX it.")));
3439 :
3440 : /*
3441 : * Check that internal pages have no garbage items, and that no page has
3442 : * an invalid combination of deletion-related page level flags
3443 : */
3444 35184 : if (!P_ISLEAF(opaque) && P_HAS_GARBAGE(opaque))
3445 0 : ereport(ERROR,
3446 : (errcode(ERRCODE_INDEX_CORRUPTED),
3447 : errmsg_internal("internal page block %u in index \"%s\" has garbage items",
3448 : blocknum, RelationGetRelationName(state->rel))));
3449 :
3450 35184 : if (P_HAS_FULLXID(opaque) && !P_ISDELETED(opaque))
3451 0 : ereport(ERROR,
3452 : (errcode(ERRCODE_INDEX_CORRUPTED),
3453 : errmsg_internal("full transaction id page flag appears in non-deleted block %u in index \"%s\"",
3454 : blocknum, RelationGetRelationName(state->rel))));
3455 :
3456 35184 : if (P_ISDELETED(opaque) && P_ISHALFDEAD(opaque))
3457 0 : ereport(ERROR,
3458 : (errcode(ERRCODE_INDEX_CORRUPTED),
3459 : errmsg_internal("deleted page block %u in index \"%s\" is half-dead",
3460 : blocknum, RelationGetRelationName(state->rel))));
3461 :
3462 35184 : return page;
3463 : }
3464 :
3465 : /*
3466 : * _bt_mkscankey() wrapper that automatically prevents insertion scankey from
3467 : * being considered greater than the pivot tuple that its values originated
3468 : * from (or some other identical pivot tuple) in the common case where there
3469 : * are truncated/minus infinity attributes. Without this extra step, there
3470 : * are forms of corruption that amcheck could theoretically fail to report.
3471 : *
3472 : * For example, invariant_g_offset() might miss a cross-page invariant failure
3473 : * on an internal level if the scankey built from the first item on the
3474 : * target's right sibling page happened to be equal to (not greater than) the
3475 : * last item on target page. The !backward tiebreaker in _bt_compare() might
3476 : * otherwise cause amcheck to assume (rather than actually verify) that the
3477 : * scankey is greater.
3478 : */
3479 : static inline BTScanInsert
3480 4056376 : bt_mkscankey_pivotsearch(Relation rel, IndexTuple itup)
3481 : {
3482 : BTScanInsert skey;
3483 :
3484 4056376 : skey = _bt_mkscankey(rel, itup);
3485 4056376 : skey->backward = true;
3486 :
3487 4056376 : return skey;
3488 : }
3489 :
3490 : /*
3491 : * PageGetItemId() wrapper that validates returned line pointer.
3492 : *
3493 : * Buffer page/page item access macros generally trust that line pointers are
3494 : * not corrupt, which might cause problems for verification itself. For
3495 : * example, there is no bounds checking in PageGetItem(). Passing it a
3496 : * corrupt line pointer can cause it to return a tuple/pointer that is unsafe
3497 : * to dereference.
3498 : *
3499 : * Validating line pointers before tuples avoids undefined behavior and
3500 : * assertion failures with corrupt indexes, making the verification process
3501 : * more robust and predictable.
3502 : */
3503 : static ItemId
3504 9313548 : PageGetItemIdCareful(BtreeCheckState *state, BlockNumber block, Page page,
3505 : OffsetNumber offset)
3506 : {
3507 9313548 : ItemId itemid = PageGetItemId(page, offset);
3508 :
3509 9313548 : if (ItemIdGetOffset(itemid) + ItemIdGetLength(itemid) >
3510 : BLCKSZ - MAXALIGN(sizeof(BTPageOpaqueData)))
3511 0 : ereport(ERROR,
3512 : (errcode(ERRCODE_INDEX_CORRUPTED),
3513 : errmsg("line pointer points past end of tuple space in index \"%s\"",
3514 : RelationGetRelationName(state->rel)),
3515 : errdetail_internal("Index tid=(%u,%u) lp_off=%u, lp_len=%u lp_flags=%u.",
3516 : block, offset, ItemIdGetOffset(itemid),
3517 : ItemIdGetLength(itemid),
3518 : ItemIdGetFlags(itemid))));
3519 :
3520 : /*
3521 : * Verify that line pointer isn't LP_REDIRECT or LP_UNUSED, since nbtree
3522 : * never uses either. Verify that line pointer has storage, too, since
3523 : * even LP_DEAD items should within nbtree.
3524 : */
3525 9313548 : if (ItemIdIsRedirected(itemid) || !ItemIdIsUsed(itemid) ||
3526 9313548 : ItemIdGetLength(itemid) == 0)
3527 0 : ereport(ERROR,
3528 : (errcode(ERRCODE_INDEX_CORRUPTED),
3529 : errmsg("invalid line pointer storage in index \"%s\"",
3530 : RelationGetRelationName(state->rel)),
3531 : errdetail_internal("Index tid=(%u,%u) lp_off=%u, lp_len=%u lp_flags=%u.",
3532 : block, offset, ItemIdGetOffset(itemid),
3533 : ItemIdGetLength(itemid),
3534 : ItemIdGetFlags(itemid))));
3535 :
3536 9313548 : return itemid;
3537 : }
3538 :
3539 : /*
3540 : * BTreeTupleGetHeapTID() wrapper that enforces that a heap TID is present in
3541 : * cases where that is mandatory (i.e. for non-pivot tuples)
3542 : */
3543 : static inline ItemPointer
3544 3720 : BTreeTupleGetHeapTIDCareful(BtreeCheckState *state, IndexTuple itup,
3545 : bool nonpivot)
3546 : {
3547 : ItemPointer htid;
3548 :
3549 : /*
3550 : * Caller determines whether this is supposed to be a pivot or non-pivot
3551 : * tuple using page type and item offset number. Verify that tuple
3552 : * metadata agrees with this.
3553 : */
3554 : Assert(state->heapkeyspace);
3555 3720 : if (BTreeTupleIsPivot(itup) && nonpivot)
3556 0 : ereport(ERROR,
3557 : (errcode(ERRCODE_INDEX_CORRUPTED),
3558 : errmsg_internal("block %u or its right sibling block or child block in index \"%s\" has unexpected pivot tuple",
3559 : state->targetblock,
3560 : RelationGetRelationName(state->rel))));
3561 :
3562 3720 : if (!BTreeTupleIsPivot(itup) && !nonpivot)
3563 0 : ereport(ERROR,
3564 : (errcode(ERRCODE_INDEX_CORRUPTED),
3565 : errmsg_internal("block %u or its right sibling block or child block in index \"%s\" has unexpected non-pivot tuple",
3566 : state->targetblock,
3567 : RelationGetRelationName(state->rel))));
3568 :
3569 3720 : htid = BTreeTupleGetHeapTID(itup);
3570 3720 : if (!ItemPointerIsValid(htid) && nonpivot)
3571 0 : ereport(ERROR,
3572 : (errcode(ERRCODE_INDEX_CORRUPTED),
3573 : errmsg("block %u or its right sibling block or child block in index \"%s\" contains non-pivot tuple that lacks a heap TID",
3574 : state->targetblock,
3575 : RelationGetRelationName(state->rel))));
3576 :
3577 3720 : return htid;
3578 : }
3579 :
3580 : /*
3581 : * Return the "pointed to" TID for itup, which is used to generate a
3582 : * descriptive error message. itup must be a "data item" tuple (it wouldn't
3583 : * make much sense to call here with a high key tuple, since there won't be a
3584 : * valid downlink/block number to display).
3585 : *
3586 : * Returns either a heap TID (which will be the first heap TID in posting list
3587 : * if itup is posting list tuple), or a TID that contains downlink block
3588 : * number, plus some encoded metadata (e.g., the number of attributes present
3589 : * in itup).
3590 : */
3591 : static inline ItemPointer
3592 12 : BTreeTupleGetPointsToTID(IndexTuple itup)
3593 : {
3594 : /*
3595 : * Rely on the assumption that !heapkeyspace internal page data items will
3596 : * correctly return TID with downlink here -- BTreeTupleGetHeapTID() won't
3597 : * recognize it as a pivot tuple, but everything still works out because
3598 : * the t_tid field is still returned
3599 : */
3600 12 : if (!BTreeTupleIsPivot(itup))
3601 8 : return BTreeTupleGetHeapTID(itup);
3602 :
3603 : /* Pivot tuple returns TID with downlink block (heapkeyspace variant) */
3604 4 : return &itup->t_tid;
3605 : }
|