@@ -455,13 +455,11 @@ visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
455
455
elog (DEBUG1 , "vm_truncate %s %d" , RelationGetRelationName (rel ), nheapblocks );
456
456
#endif
457
457
458
- RelationOpenSmgr (rel );
459
-
460
458
/*
461
459
* If no visibility map has been created yet for this relation, there's
462
460
* nothing to truncate.
463
461
*/
464
- if (!smgrexists (rel -> rd_smgr , VISIBILITYMAP_FORKNUM ))
462
+ if (!smgrexists (RelationGetSmgr ( rel ) , VISIBILITYMAP_FORKNUM ))
465
463
return InvalidBlockNumber ;
466
464
467
465
/*
@@ -528,7 +526,7 @@ visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
528
526
else
529
527
newnblocks = truncBlock ;
530
528
531
- if (smgrnblocks (rel -> rd_smgr , VISIBILITYMAP_FORKNUM ) <= newnblocks )
529
+ if (smgrnblocks (RelationGetSmgr ( rel ) , VISIBILITYMAP_FORKNUM ) <= newnblocks )
532
530
{
533
531
/* nothing to do, the file was already smaller than requested size */
534
532
return InvalidBlockNumber ;
@@ -547,30 +545,29 @@ static Buffer
547
545
vm_readbuf (Relation rel , BlockNumber blkno , bool extend )
548
546
{
549
547
Buffer buf ;
548
+ SMgrRelation reln ;
550
549
551
550
/*
552
- * We might not have opened the relation at the smgr level yet, or we
553
- * might have been forced to close it by a sinval message. The code below
554
- * won't necessarily notice relation extension immediately when extend =
555
- * false, so we rely on sinval messages to ensure that our ideas about the
556
- * size of the map aren't too far out of date.
551
+ * Caution: re-using this smgr pointer could fail if the relcache entry
552
+ * gets closed. It's safe as long as we only do smgr-level operations
553
+ * between here and the last use of the pointer.
557
554
*/
558
- RelationOpenSmgr (rel );
555
+ reln = RelationGetSmgr (rel );
559
556
560
557
/*
561
558
* If we haven't cached the size of the visibility map fork yet, check it
562
559
* first.
563
560
*/
564
- if (rel -> rd_smgr -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] == InvalidBlockNumber )
561
+ if (reln -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] == InvalidBlockNumber )
565
562
{
566
- if (smgrexists (rel -> rd_smgr , VISIBILITYMAP_FORKNUM ))
567
- smgrnblocks (rel -> rd_smgr , VISIBILITYMAP_FORKNUM );
563
+ if (smgrexists (reln , VISIBILITYMAP_FORKNUM ))
564
+ smgrnblocks (reln , VISIBILITYMAP_FORKNUM );
568
565
else
569
- rel -> rd_smgr -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] = 0 ;
566
+ reln -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] = 0 ;
570
567
}
571
568
572
569
/* Handle requests beyond EOF */
573
- if (blkno >= rel -> rd_smgr -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ])
570
+ if (blkno >= reln -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ])
574
571
{
575
572
if (extend )
576
573
vm_extend (rel , blkno + 1 );
@@ -618,6 +615,7 @@ vm_extend(Relation rel, BlockNumber vm_nblocks)
618
615
{
619
616
BlockNumber vm_nblocks_now ;
620
617
PGAlignedBlock pg ;
618
+ SMgrRelation reln ;
621
619
622
620
PageInit ((Page ) pg .data , BLCKSZ , 0 );
623
621
@@ -633,29 +631,32 @@ vm_extend(Relation rel, BlockNumber vm_nblocks)
633
631
*/
634
632
LockRelationForExtension (rel , ExclusiveLock );
635
633
636
- /* Might have to re-open if a cache flush happened */
637
- RelationOpenSmgr (rel );
634
+ /*
635
+ * Caution: re-using this smgr pointer could fail if the relcache entry
636
+ * gets closed. It's safe as long as we only do smgr-level operations
637
+ * between here and the last use of the pointer.
638
+ */
639
+ reln = RelationGetSmgr (rel );
638
640
639
641
/*
640
642
* Create the file first if it doesn't exist. If smgr_vm_nblocks is
641
643
* positive then it must exist, no need for an smgrexists call.
642
644
*/
643
- if ((rel -> rd_smgr -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] == 0 ||
644
- rel -> rd_smgr -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] == InvalidBlockNumber ) &&
645
- !smgrexists (rel -> rd_smgr , VISIBILITYMAP_FORKNUM ))
646
- smgrcreate (rel -> rd_smgr , VISIBILITYMAP_FORKNUM , false);
645
+ if ((reln -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] == 0 ||
646
+ reln -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] == InvalidBlockNumber ) &&
647
+ !smgrexists (reln , VISIBILITYMAP_FORKNUM ))
648
+ smgrcreate (reln , VISIBILITYMAP_FORKNUM , false);
647
649
648
650
/* Invalidate cache so that smgrnblocks() asks the kernel. */
649
- rel -> rd_smgr -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] = InvalidBlockNumber ;
650
- vm_nblocks_now = smgrnblocks (rel -> rd_smgr , VISIBILITYMAP_FORKNUM );
651
+ reln -> smgr_cached_nblocks [VISIBILITYMAP_FORKNUM ] = InvalidBlockNumber ;
652
+ vm_nblocks_now = smgrnblocks (reln , VISIBILITYMAP_FORKNUM );
651
653
652
654
/* Now extend the file */
653
655
while (vm_nblocks_now < vm_nblocks )
654
656
{
655
657
PageSetChecksumInplace ((Page ) pg .data , vm_nblocks_now );
656
658
657
- smgrextend (rel -> rd_smgr , VISIBILITYMAP_FORKNUM , vm_nblocks_now ,
658
- pg .data , false);
659
+ smgrextend (reln , VISIBILITYMAP_FORKNUM , vm_nblocks_now , pg .data , false);
659
660
vm_nblocks_now ++ ;
660
661
}
661
662
@@ -666,7 +667,7 @@ vm_extend(Relation rel, BlockNumber vm_nblocks)
666
667
* to keep checking for creation or extension of the file, which happens
667
668
* infrequently.
668
669
*/
669
- CacheInvalidateSmgr (rel -> rd_smgr -> smgr_rnode );
670
+ CacheInvalidateSmgr (reln -> smgr_rnode );
670
671
671
672
UnlockRelationForExtension (rel , ExclusiveLock );
672
673
}
0 commit comments