@@ -736,71 +736,29 @@ SimpleLruWritePage(SlruCtl ctl, int slotno)
736
736
737
737
738
738
/*
739
- * NEON: we do not want to include large pg_xact/multixact files in basebackup and prefer
740
- * to download them on demand to reduce startup time.
741
- * If SLRU segment is not found, we try to download it from page server
739
+ * NEON: we do not want to include large pg_xact/multixact files in the
740
+ * basebackup and prefer to download them on demand to reduce startup time.
741
+ *
742
+ * If SLRU segment is not found, we try to download it from the pageserver.
743
+ *
744
+ * Returns:
745
+ * true if the file was successfully downloaded
746
+ * false if the file was not found in pageserver
747
+ * ereports if some other error happened
742
748
*/
743
- static int
744
- SimpleLruDownloadSegment (SlruCtl ctl , int pageno , char const * path )
749
+ static bool
750
+ SimpleLruDownloadSegment (SlruCtl ctl , int pageno , char const * path )
745
751
{
746
752
SlruShared shared = ctl -> shared ;
747
- int segno ;
748
- int fd = -1 ;
749
- int n_blocks ;
750
- char * buffer ;
751
-
752
- static SMgrRelationData dummy_smgr_rel = {0 };
753
+ int segno ;
753
754
754
755
/* If page is greater than latest written page, then do not try to download segment from server */
755
756
if (ctl -> PagePrecedes (pg_atomic_read_u64 (& shared -> latest_page_number ), pageno ))
756
- return -1 ;
757
+ return false ;
757
758
758
- if (!dummy_smgr_rel .smgr )
759
- {
760
- RelFileLocator rlocator = {0 };
761
- dummy_smgr_rel .smgr = smgr (INVALID_PROC_NUMBER , rlocator );
762
- }
763
759
segno = pageno / SLRU_PAGES_PER_SEGMENT ;
764
760
765
- if (neon_use_communicator_worker ) {
766
- buffer = NULL ;
767
- } else {
768
- buffer = palloc (BLCKSZ * SLRU_PAGES_PER_SEGMENT );
769
- }
770
-
771
- n_blocks = smgr_read_slru_segment (& dummy_smgr_rel , path , segno , buffer );
772
- if (n_blocks > 0 )
773
- {
774
- fd = OpenTransientFile (path , O_RDWR | O_CREAT | PG_BINARY );
775
- if (fd < 0 )
776
- {
777
- slru_errcause = SLRU_OPEN_FAILED ;
778
- slru_errno = errno ;
779
- pfree (buffer );
780
- return -1 ;
781
- }
782
-
783
- if (!neon_use_communicator_worker ) {
784
- errno = 0 ;
785
- pgstat_report_wait_start (WAIT_EVENT_SLRU_WRITE );
786
- if (pg_pwrite (fd , buffer , n_blocks * BLCKSZ , 0 ) != n_blocks * BLCKSZ )
787
- {
788
- pgstat_report_wait_end ();
789
- /* if write didn't set errno, assume problem is no disk space */
790
- if (errno == 0 )
791
- errno = ENOSPC ;
792
- slru_errcause = SLRU_WRITE_FAILED ;
793
- slru_errno = errno ;
794
-
795
- CloseTransientFile (fd );
796
- pfree (buffer );
797
- return -1 ;
798
- }
799
- pgstat_report_wait_end ();
800
- }
801
- }
802
- pfree (buffer );
803
- return fd ;
761
+ return smgr_read_slru_segment (path , segno );
804
762
}
805
763
806
764
/*
@@ -819,29 +777,34 @@ SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
819
777
int fd ;
820
778
bool result ;
821
779
off_t endpos ;
780
+ bool attempted_download = false;
822
781
823
782
/* update the stats counter of checked pages */
824
783
pgstat_count_slru_page_exists (ctl -> shared -> slru_stats_idx );
825
784
826
785
SlruFileName (ctl , path , segno );
827
786
787
+ retry_after_download :
828
788
fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
829
789
if (fd < 0 )
830
790
{
831
- /* expected: file doesn't exist */
832
- if (errno == ENOENT )
791
+ if (errno == ENOENT && !attempted_download )
833
792
{
834
- fd = SimpleLruDownloadSegment (ctl , pageno , path );
835
- if (fd < 0 )
836
- return false;
837
- }
838
- else
839
- {
840
- /* report error normally */
841
- slru_errcause = SLRU_OPEN_FAILED ;
842
- slru_errno = errno ;
843
- SlruReportIOError (ctl , pageno , 0 );
793
+ /* Try to download the file from the pageserver */
794
+ attempted_download = true;
795
+ if (SimpleLruDownloadSegment (ctl , pageno , path ))
796
+ goto retry_after_download ;
797
+ errno = ENOENT ;
844
798
}
799
+
800
+ /* expected: file doesn't exist */
801
+ if (errno == ENOENT )
802
+ return false;
803
+
804
+ /* report error normally */
805
+ slru_errcause = SLRU_OPEN_FAILED ;
806
+ slru_errno = errno ;
807
+ SlruReportIOError (ctl , pageno , 0 );
845
808
}
846
809
847
810
if ((endpos = lseek (fd , 0 , SEEK_END )) < 0 )
@@ -882,6 +845,7 @@ SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
882
845
off_t offset = rpageno * BLCKSZ ;
883
846
char path [MAXPGPATH ];
884
847
int fd ;
848
+ bool attempted_download = false;
885
849
886
850
SlruFileName (ctl , path , segno );
887
851
@@ -892,33 +856,31 @@ SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
892
856
* SlruPhysicalWritePage). Hence, if we are InRecovery, allow the case
893
857
* where the file doesn't exist, and return zeroes instead.
894
858
*/
859
+ retry_after_download :
895
860
fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
896
861
if (fd < 0 )
897
862
{
898
- if (errno != ENOENT )
863
+ if (errno == ENOENT && !attempted_download )
864
+ {
865
+ /* Try to download the file from the pageserver */
866
+ attempted_download = true;
867
+ if (SimpleLruDownloadSegment (ctl , pageno , path ))
868
+ goto retry_after_download ;
869
+ errno = ENOENT ;
870
+ }
871
+
872
+ if (errno != ENOENT || !InRecovery )
899
873
{
900
874
slru_errcause = SLRU_OPEN_FAILED ;
901
875
slru_errno = errno ;
902
876
return false;
903
877
}
904
- fd = SimpleLruDownloadSegment (ctl , pageno , path );
905
- if (fd < 0 )
906
- {
907
- if (!InRecovery )
908
- {
909
- slru_errcause = SLRU_OPEN_FAILED ;
910
- slru_errno = errno ;
911
- return false;
912
- }
913
- else
914
- {
915
- ereport (LOG ,
916
- (errmsg ("file \"%s\" doesn't exist, reading as zeroes" ,
917
- path )));
918
- MemSet (shared -> page_buffer [slotno ], 0 , BLCKSZ );
919
- return true;
920
- }
921
- }
878
+
879
+ ereport (LOG ,
880
+ (errmsg ("file \"%s\" doesn't exist, reading as zeroes" ,
881
+ path )));
882
+ MemSet (shared -> page_buffer [slotno ], 0 , BLCKSZ );
883
+ return true;
922
884
}
923
885
924
886
errno = 0 ;
0 commit comments