@@ -619,70 +619,28 @@ SimpleLruWritePage(SlruCtl ctl, int slotno)
619619
620620
621621/*
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
625631 */
626- static int
627- SimpleLruDownloadSegment (SlruCtl ctl , int pageno , char const * path )
632+ static bool
633+ SimpleLruDownloadSegment (SlruCtl ctl , int pageno , char const * path )
628634{
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 ;
635636
636637 /* If page is greater than latest written page, then do not try to download segment from server */
637638 if (ctl -> PagePrecedes (ctl -> shared -> latest_page_number , pageno ))
638- return -1 ;
639+ return false ;
639640
640- if (!dummy_smgr_rel .smgr )
641- {
642- RelFileNode rnode = {0 };
643- dummy_smgr_rel .smgr = smgr (InvalidBackendId , rnode );
644- }
645641 segno = pageno / SLRU_PAGES_PER_SEGMENT ;
646642
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 );
686644}
687645
688646/*
@@ -701,29 +659,34 @@ SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
701659 int fd ;
702660 bool result ;
703661 off_t endpos ;
662+ bool attempted_download = false;
704663
705664 /* update the stats counter of checked pages */
706665 pgstat_count_slru_page_exists (ctl -> shared -> slru_stats_idx );
707666
708667 SlruFileName (ctl , path , segno );
709668
669+ retry_after_download :
710670 fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
711671 if (fd < 0 )
712672 {
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 )
721674 {
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 ;
726680 }
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 );
727690 }
728691
729692 if ((endpos = lseek (fd , 0 , SEEK_END )) < 0 )
@@ -764,6 +727,7 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
764727 off_t offset = rpageno * BLCKSZ ;
765728 char path [MAXPGPATH ];
766729 int fd ;
730+ bool attempted_download = false;
767731
768732 SlruFileName (ctl , path , segno );
769733
@@ -774,33 +738,31 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
774738 * SlruPhysicalWritePage). Hence, if we are InRecovery, allow the case
775739 * where the file doesn't exist, and return zeroes instead.
776740 */
741+ retry_after_download :
777742 fd = OpenTransientFile (path , O_RDONLY | PG_BINARY );
778743 if (fd < 0 )
779744 {
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 )
781755 {
782756 slru_errcause = SLRU_OPEN_FAILED ;
783757 slru_errno = errno ;
784758 return false;
785759 }
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;
804766 }
805767
806768 errno = 0 ;
0 commit comments