@@ -725,64 +725,29 @@ SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
725725}
726726
727727/*
728- * NEON: we do not want to include large pg_xact/multixact files in basebackup and prefer
729- * to download them on demand to reduce startup time.
730- * If SLRU segment is not found, we try to download it from page server
728+ * NEON: we do not want to include large pg_xact/multixact files in the
729+ * basebackup and prefer to download them on demand to reduce startup time.
730+ *
731+ * If SLRU segment is not found, we try to download it from the pageserver.
732+ *
733+ * Returns:
734+ * true if the file was successfully downloaded
735+ * false if the file was not found in pageserver
736+ * ereports if some other error happened
731737 */
732- static int
733- SimpleLruDownloadSegment (SlruCtl ctl , int pageno , char const * path )
738+ static bool
739+ SimpleLruDownloadSegment (SlruCtl ctl , int pageno , char const * path )
734740{
735741 SlruShared shared = ctl -> shared ;
736- int segno ;
737- int fd = -1 ;
738- int n_blocks ;
739- char * buffer ;
742+ int segno ;
740743
741744 /* If page is greater than latest written page, then do not try to download segment from server */
742745 if (ctl -> PagePrecedes (pg_atomic_read_u64 (& shared -> latest_page_number ), pageno ))
743- return -1 ;
746+ return false ;
744747
745748 segno = pageno / SLRU_PAGES_PER_SEGMENT ;
746749
747- if (neon_use_communicator_worker ) {
748- buffer = NULL ;
749- } else {
750- buffer = palloc (BLCKSZ * SLRU_PAGES_PER_SEGMENT );
751- }
752-
753- n_blocks = read_slru_segment (path , segno , buffer );
754- if (n_blocks > 0 )
755- {
756- fd = OpenTransientFile (path , O_RDWR | O_CREAT | PG_BINARY );
757- if (fd < 0 )
758- {
759- slru_errcause = SLRU_OPEN_FAILED ;
760- slru_errno = errno ;
761- pfree (buffer );
762- return -1 ;
763- }
764-
765- if (!neon_use_communicator_worker ) {
766- errno = 0 ;
767- pgstat_report_wait_start (WAIT_EVENT_SLRU_WRITE );
768- if (pg_pwrite (fd , buffer , n_blocks * BLCKSZ , 0 ) != n_blocks * BLCKSZ )
769- {
770- pgstat_report_wait_end ();
771- /* if write didn't set errno, assume problem is no disk space */
772- if (errno == 0 )
773- errno = ENOSPC ;
774- slru_errcause = SLRU_WRITE_FAILED ;
775- slru_errno = errno ;
776-
777- CloseTransientFile (fd );
778- pfree (buffer );
779- return -1 ;
780- }
781- pgstat_report_wait_end ();
782- }
783- }
784- pfree (buffer );
785- return fd ;
750+ return smgr_read_slru_segment (path , segno );
786751}
787752
788753/*
@@ -813,29 +778,34 @@ SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
813778 int fd ;
814779 bool result ;
815780 off_t endpos ;
781+ bool attempted_download = false;
816782
817783 /* update the stats counter of checked pages */
818784 pgstat_count_slru_page_exists (ctl -> shared -> slru_stats_idx );
819785
820786 SlruFileName (ctl , path , segno );
821787
788+ retry_after_download :
822789 fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
823790 if (fd < 0 )
824791 {
825- /* expected: file doesn't exist */
826- if (errno == ENOENT )
792+ if (errno == ENOENT && !attempted_download )
827793 {
828- fd = SimpleLruDownloadSegment (ctl , pageno , path );
829- if (fd < 0 )
830- return false;
831- }
832- else
833- {
834- /* report error normally */
835- slru_errcause = SLRU_OPEN_FAILED ;
836- slru_errno = errno ;
837- SlruReportIOError (ctl , pageno , 0 );
794+ /* Try to download the file from the pageserver */
795+ attempted_download = true;
796+ if (SimpleLruDownloadSegment (ctl , pageno , path ))
797+ goto retry_after_download ;
798+ errno = ENOENT ;
838799 }
800+
801+ /* expected: file doesn't exist */
802+ if (errno == ENOENT )
803+ return false;
804+
805+ /* report error normally */
806+ slru_errcause = SLRU_OPEN_FAILED ;
807+ slru_errno = errno ;
808+ SlruReportIOError (ctl , pageno , 0 );
839809 }
840810
841811 if ((endpos = lseek (fd , 0 , SEEK_END )) < 0 )
@@ -876,6 +846,7 @@ SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
876846 off_t offset = rpageno * BLCKSZ ;
877847 char path [MAXPGPATH ];
878848 int fd ;
849+ bool attempted_download = false;
879850
880851 SlruFileName (ctl , path , segno );
881852
@@ -886,34 +857,31 @@ SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
886857 * SlruPhysicalWritePage). Hence, if we are InRecovery, allow the case
887858 * where the file doesn't exist, and return zeroes instead.
888859 */
860+ retry_after_download :
889861 fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
890862 if (fd < 0 )
891863 {
892- if (errno != ENOENT )
864+ if (errno == ENOENT && !attempted_download )
865+ {
866+ /* Try to download the file from the pageserver */
867+ attempted_download = true;
868+ if (SimpleLruDownloadSegment (ctl , pageno , path ))
869+ goto retry_after_download ;
870+ errno = ENOENT ;
871+ }
872+
873+ if (errno != ENOENT || !InRecovery )
893874 {
894875 slru_errcause = SLRU_OPEN_FAILED ;
895876 slru_errno = errno ;
896877 return false;
897878 }
898879
899- fd = SimpleLruDownloadSegment (ctl , pageno , path );
900- if (fd < 0 )
901- {
902- if (!InRecovery )
903- {
904- slru_errcause = SLRU_OPEN_FAILED ;
905- slru_errno = errno ;
906- return false;
907- }
908- else
909- {
910- ereport (LOG ,
911- (errmsg ("file \"%s\" doesn't exist, reading as zeroes" ,
912- path )));
913- MemSet (shared -> page_buffer [slotno ], 0 , BLCKSZ );
914- return true;
915- }
916- }
880+ ereport (LOG ,
881+ (errmsg ("file \"%s\" doesn't exist, reading as zeroes" ,
882+ path )));
883+ MemSet (shared -> page_buffer [slotno ], 0 , BLCKSZ );
884+ return true;
917885 }
918886
919887 errno = 0 ;
0 commit comments