@@ -619,23 +619,25 @@ 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
-
635
+ int segno ;
634
636
static SMgrRelationData dummy_smgr_rel = {0 };
635
637
636
638
/* If page is greater than latest written page, then do not try to download segment from server */
637
639
if (ctl -> PagePrecedes (ctl -> shared -> latest_page_number , pageno ))
638
- return -1 ;
640
+ return false ;
639
641
640
642
if (!dummy_smgr_rel .smgr )
641
643
{
@@ -644,45 +646,7 @@ SimpleLruDownloadSegment(SlruCtl ctl, int pageno, char const* path)
644
646
}
645
647
segno = pageno / SLRU_PAGES_PER_SEGMENT ;
646
648
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 ;
649
+ return smgr_read_slru_segment (& dummy_smgr_rel , path , segno );
686
650
}
687
651
688
652
/*
@@ -701,29 +665,34 @@ SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
701
665
int fd ;
702
666
bool result ;
703
667
off_t endpos ;
668
+ bool attempted_download = false;
704
669
705
670
/* update the stats counter of checked pages */
706
671
pgstat_count_slru_page_exists (ctl -> shared -> slru_stats_idx );
707
672
708
673
SlruFileName (ctl , path , segno );
709
674
675
+ retry_after_download :
710
676
fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
711
677
if (fd < 0 )
712
678
{
713
- /* expected: file doesn't exist */
714
- if (errno == ENOENT )
679
+ if (errno == ENOENT && !attempted_download )
715
680
{
716
- fd = SimpleLruDownloadSegment (ctl , pageno , path );
717
- if (fd < 0 )
718
- return false;
719
- }
720
- else
721
- {
722
- /* report error normally */
723
- slru_errcause = SLRU_OPEN_FAILED ;
724
- slru_errno = errno ;
725
- SlruReportIOError (ctl , pageno , 0 );
681
+ /* Try to download the file from the pageserver */
682
+ attempted_download = true;
683
+ if (SimpleLruDownloadSegment (ctl , pageno , path ))
684
+ goto retry_after_download ;
685
+ errno = ENOENT ;
726
686
}
687
+
688
+ /* expected: file doesn't exist */
689
+ if (errno == ENOENT )
690
+ return false;
691
+
692
+ /* report error normally */
693
+ slru_errcause = SLRU_OPEN_FAILED ;
694
+ slru_errno = errno ;
695
+ SlruReportIOError (ctl , pageno , 0 );
727
696
}
728
697
729
698
if ((endpos = lseek (fd , 0 , SEEK_END )) < 0 )
@@ -764,6 +733,7 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
764
733
off_t offset = rpageno * BLCKSZ ;
765
734
char path [MAXPGPATH ];
766
735
int fd ;
736
+ bool attempted_download = false;
767
737
768
738
SlruFileName (ctl , path , segno );
769
739
@@ -774,33 +744,31 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
774
744
* SlruPhysicalWritePage). Hence, if we are InRecovery, allow the case
775
745
* where the file doesn't exist, and return zeroes instead.
776
746
*/
747
+ retry_after_download :
777
748
fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
778
749
if (fd < 0 )
779
750
{
780
- if (errno != ENOENT )
751
+ if (errno == ENOENT && !attempted_download )
752
+ {
753
+ /* Try to download the file from the pageserver */
754
+ attempted_download = true;
755
+ if (SimpleLruDownloadSegment (ctl , pageno , path ))
756
+ goto retry_after_download ;
757
+ errno = ENOENT ;
758
+ }
759
+
760
+ if (errno != ENOENT || !InRecovery )
781
761
{
782
762
slru_errcause = SLRU_OPEN_FAILED ;
783
763
slru_errno = errno ;
784
764
return false;
785
765
}
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
- }
766
+
767
+ ereport (LOG ,
768
+ (errmsg ("file \"%s\" doesn't exist, reading as zeroes" ,
769
+ path )));
770
+ MemSet (shared -> page_buffer [slotno ], 0 , BLCKSZ );
771
+ return true;
804
772
}
805
773
806
774
errno = 0 ;
0 commit comments