@@ -382,7 +382,6 @@ bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace,
382382 BTMetaPageData * metad ;
383383 uint32 previouslevel ;
384384 BtreeLevel current ;
385- Snapshot snapshot = SnapshotAny ;
386385
387386 if (!readonly )
388387 elog (DEBUG1 , "verifying consistency of tree structure for index \"%s\"" ,
@@ -433,54 +432,46 @@ bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace,
433432 state -> heaptuplespresent = 0 ;
434433
435434 /*
436- * Register our own snapshot in !readonly case , rather than asking
435+ * Register our own snapshot for heapallindexed , rather than asking
437436 * table_index_build_scan() to do this for us later. This needs to
438437 * happen before index fingerprinting begins, so we can later be
439438 * certain that index fingerprinting should have reached all tuples
440439 * returned by table_index_build_scan().
441440 */
442- if (!state -> readonly )
443- {
444- snapshot = RegisterSnapshot (GetTransactionSnapshot ());
441+ state -> snapshot = RegisterSnapshot (GetTransactionSnapshot ());
445442
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- if (IsolationUsesXactSnapshot () && rel -> rd_index -> indcheckxmin &&
460- !TransactionIdPrecedes (HeapTupleHeaderGetXmin (rel -> rd_indextuple -> t_data ),
461- snapshot -> xmin ))
462- ereport (ERROR ,
463- (errcode (ERRCODE_T_R_SERIALIZATION_FAILURE ),
464- errmsg ("index \"%s\" cannot be verified using transaction snapshot" ,
465- RelationGetRelationName (rel ))));
466- }
443+ /*
444+ * GetTransactionSnapshot() always acquires a new MVCC snapshot in
445+ * READ COMMITTED mode. A new snapshot is guaranteed to have all the
446+ * entries it requires in the index.
447+ *
448+ * We must defend against the possibility that an old xact snapshot
449+ * was returned at higher isolation levels when that snapshot is not
450+ * safe for index scans of the target index. This is possible when
451+ * the snapshot sees tuples that are before the index's indcheckxmin
452+ * horizon. Throwing an error here should be very rare. It doesn't
453+ * seem worth using a secondary snapshot to avoid this.
454+ */
455+ if (IsolationUsesXactSnapshot () && rel -> rd_index -> indcheckxmin &&
456+ !TransactionIdPrecedes (HeapTupleHeaderGetXmin (rel -> rd_indextuple -> t_data ),
457+ state -> snapshot -> xmin ))
458+ ereport (ERROR ,
459+ errcode (ERRCODE_T_R_SERIALIZATION_FAILURE ),
460+ errmsg ("index \"%s\" cannot be verified using transaction snapshot" ,
461+ RelationGetRelationName (rel )));
467462 }
468463
469464 /*
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 .
465+ * We need a snapshot to check the uniqueness of the index. For better
466+ * performance, take it once per index check. If one was already taken
467+ * above, use that .
473468 */
474469 if (state -> checkunique )
475470 {
476471 state -> indexinfo = BuildIndexInfo (state -> rel );
477- if (state -> indexinfo -> ii_Unique )
478- {
479- if (snapshot != SnapshotAny )
480- state -> snapshot = snapshot ;
481- else
482- state -> snapshot = RegisterSnapshot (GetTransactionSnapshot ());
483- }
472+
473+ if (state -> indexinfo -> ii_Unique && state -> snapshot == InvalidSnapshot )
474+ state -> snapshot = RegisterSnapshot (GetTransactionSnapshot ());
484475 }
485476
486477 Assert (!state -> rootdescend || state -> readonly );
@@ -555,30 +546,28 @@ bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace,
555546 /*
556547 * Create our own scan for table_index_build_scan(), rather than
557548 * 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.
549+ * actually use the MVCC snapshot registered earlier.
560550 *
561551 * Note that table_index_build_scan() calls heap_endscan() for us.
562552 */
563553 scan = table_beginscan_strat (state -> heaprel , /* relation */
564- snapshot , /* snapshot */
554+ state -> snapshot , /* snapshot */
565555 0 , /* number of keys */
566556 NULL , /* scan key */
567557 true, /* buffer access strategy OK */
568558 true); /* syncscan OK? */
569559
570560 /*
571561 * Scan will behave as the first scan of a CREATE INDEX CONCURRENTLY
572- * behaves in !readonly case .
562+ * behaves.
573563 *
574564 * 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.
565+ * heap relation as any other ii_Concurrent caller would. We have no
566+ * reason to care about a concurrent VACUUM operation, since there
567+ * isn't going to be a second scan of the heap that needs to be sure
568+ * that there was no concurrent recycling of TIDs.
580569 */
581- indexinfo -> ii_Concurrent = ! state -> readonly ;
570+ indexinfo -> ii_Concurrent = true ;
582571
583572 /*
584573 * Don't wait for uncommitted tuple xact commit/abort when index is a
@@ -602,14 +591,11 @@ bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace,
602591 state -> heaptuplespresent , RelationGetRelationName (heaprel ),
603592 100.0 * bloom_prop_bits_set (state -> filter ))));
604593
605- if (snapshot != SnapshotAny )
606- UnregisterSnapshot (snapshot );
607-
608594 bloom_free (state -> filter );
609595 }
610596
611597 /* Be tidy: */
612- if (snapshot == SnapshotAny && state -> snapshot != InvalidSnapshot )
598+ if (state -> snapshot != InvalidSnapshot )
613599 UnregisterSnapshot (state -> snapshot );
614600 MemoryContextDelete (state -> targetcontext );
615601}
0 commit comments