@@ -619,70 +619,28 @@ SimpleLruWritePage(SlruCtl ctl, int slotno)
619
619
620
620
621
621
/*
622
- * NEON: we do not want to include large pg_xact/multixact files in basebackup and prefer
623
- * to download them on demand to reduce startup time.
624
- * If SLRU segment is not found, we try to download it from page server
622
+ * NEON: we do not want to include large pg_xact/multixact files in the
623
+ * basebackup and prefer to download them on demand to reduce startup time.
624
+ *
625
+ * If SLRU segment is not found, we try to download it from the pageserver.
626
+ *
627
+ * Returns:
628
+ * true if the file was successfully downloaded
629
+ * false if the file was not found in pageserver
630
+ * ereports if some other error happened
625
631
*/
626
- static int
627
- SimpleLruDownloadSegment (SlruCtl ctl , int pageno , char const * path )
632
+ static bool
633
+ SimpleLruDownloadSegment (SlruCtl ctl , int pageno , char const * path )
628
634
{
629
- int segno ;
630
- int fd = -1 ;
631
- int n_blocks ;
632
- char * buffer ;
633
-
634
- static SMgrRelationData dummy_smgr_rel = {0 };
635
+ int segno ;
635
636
636
637
/* If page is greater than latest written page, then do not try to download segment from server */
637
638
if (ctl -> PagePrecedes (ctl -> shared -> latest_page_number , pageno ))
638
- return -1 ;
639
+ return false ;
639
640
640
- if (!dummy_smgr_rel .smgr )
641
- {
642
- RelFileNode rnode = {0 };
643
- dummy_smgr_rel .smgr = smgr (InvalidBackendId , rnode );
644
- }
645
641
segno = pageno / SLRU_PAGES_PER_SEGMENT ;
646
642
647
- if (neon_use_communicator_worker ) {
648
- buffer = NULL ;
649
- } else {
650
- buffer = palloc (BLCKSZ * SLRU_PAGES_PER_SEGMENT );
651
- }
652
-
653
- n_blocks = smgr_read_slru_segment (& dummy_smgr_rel , path , segno , buffer );
654
- if (n_blocks > 0 )
655
- {
656
- fd = OpenTransientFile (path , O_RDWR | O_CREAT | PG_BINARY );
657
- if (fd < 0 )
658
- {
659
- slru_errcause = SLRU_OPEN_FAILED ;
660
- slru_errno = errno ;
661
- pfree (buffer );
662
- return -1 ;
663
- }
664
-
665
- if (!neon_use_communicator_worker ) {
666
- errno = 0 ;
667
- pgstat_report_wait_start (WAIT_EVENT_SLRU_WRITE );
668
- if (pg_pwrite (fd , buffer , n_blocks * BLCKSZ , 0 ) != n_blocks * BLCKSZ )
669
- {
670
- pgstat_report_wait_end ();
671
- /* if write didn't set errno, assume problem is no disk space */
672
- if (errno == 0 )
673
- errno = ENOSPC ;
674
- slru_errcause = SLRU_WRITE_FAILED ;
675
- slru_errno = errno ;
676
-
677
- CloseTransientFile (fd );
678
- pfree (buffer );
679
- return -1 ;
680
- }
681
- pgstat_report_wait_end ();
682
- }
683
- }
684
- pfree (buffer );
685
- return fd ;
643
+ return smgr_read_slru_segment (path , segno );
686
644
}
687
645
688
646
/*
@@ -701,29 +659,34 @@ SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
701
659
int fd ;
702
660
bool result ;
703
661
off_t endpos ;
662
+ bool attempted_download = false;
704
663
705
664
/* update the stats counter of checked pages */
706
665
pgstat_count_slru_page_exists (ctl -> shared -> slru_stats_idx );
707
666
708
667
SlruFileName (ctl , path , segno );
709
668
669
+ retry_after_download :
710
670
fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
711
671
if (fd < 0 )
712
672
{
713
- /* expected: file doesn't exist */
714
- if (errno == ENOENT )
715
- {
716
- fd = SimpleLruDownloadSegment (ctl , pageno , path );
717
- if (fd < 0 )
718
- return false;
719
- }
720
- else
673
+ if (errno == ENOENT && !attempted_download )
721
674
{
722
- /* report error normally */
723
- slru_errcause = SLRU_OPEN_FAILED ;
724
- slru_errno = errno ;
725
- SlruReportIOError (ctl , pageno , 0 );
675
+ /* Try to download the file from the pageserver */
676
+ attempted_download = true;
677
+ if (SimpleLruDownloadSegment (ctl , pageno , path ))
678
+ goto retry_after_download ;
679
+ errno = ENOENT ;
726
680
}
681
+
682
+ /* expected: file doesn't exist */
683
+ if (errno == ENOENT )
684
+ return false;
685
+
686
+ /* report error normally */
687
+ slru_errcause = SLRU_OPEN_FAILED ;
688
+ slru_errno = errno ;
689
+ SlruReportIOError (ctl , pageno , 0 );
727
690
}
728
691
729
692
if ((endpos = lseek (fd , 0 , SEEK_END )) < 0 )
@@ -764,6 +727,7 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
764
727
off_t offset = rpageno * BLCKSZ ;
765
728
char path [MAXPGPATH ];
766
729
int fd ;
730
+ bool attempted_download = false;
767
731
768
732
SlruFileName (ctl , path , segno );
769
733
@@ -774,33 +738,31 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
774
738
* SlruPhysicalWritePage). Hence, if we are InRecovery, allow the case
775
739
* where the file doesn't exist, and return zeroes instead.
776
740
*/
741
+ retry_after_download :
777
742
fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
778
743
if (fd < 0 )
779
744
{
780
- if (errno != ENOENT )
745
+ if (errno == ENOENT && !attempted_download )
746
+ {
747
+ /* Try to download the file from the pageserver */
748
+ attempted_download = true;
749
+ if (SimpleLruDownloadSegment (ctl , pageno , path ))
750
+ goto retry_after_download ;
751
+ errno = ENOENT ;
752
+ }
753
+
754
+ if (errno != ENOENT || !InRecovery )
781
755
{
782
756
slru_errcause = SLRU_OPEN_FAILED ;
783
757
slru_errno = errno ;
784
758
return false;
785
759
}
786
- fd = SimpleLruDownloadSegment (ctl , pageno , path );
787
- if (fd < 0 )
788
- {
789
- if (!InRecovery )
790
- {
791
- slru_errcause = SLRU_OPEN_FAILED ;
792
- slru_errno = errno ;
793
- return false;
794
- }
795
- else
796
- {
797
- ereport (LOG ,
798
- (errmsg ("file \"%s\" doesn't exist, reading as zeroes" ,
799
- path )));
800
- MemSet (shared -> page_buffer [slotno ], 0 , BLCKSZ );
801
- return true;
802
- }
803
- }
760
+
761
+ ereport (LOG ,
762
+ (errmsg ("file \"%s\" doesn't exist, reading as zeroes" ,
763
+ path )));
764
+ MemSet (shared -> page_buffer [slotno ], 0 , BLCKSZ );
765
+ return true;
804
766
}
805
767
806
768
errno = 0 ;
0 commit comments