@@ -56,7 +56,7 @@ static void
5656backup_data_page (pgFile * file , XLogRecPtr prev_backup_start_lsn ,
5757 BlockNumber blknum , BlockNumber nblocks ,
5858 FILE * in , FILE * out ,
59- pg_crc32 * crc )
59+ pg_crc32 * crc , int * n_skipped )
6060{
6161 BackupPageHeader header ;
6262 off_t offset ;
@@ -134,7 +134,10 @@ backup_data_page(pgFile *file, XLogRecPtr prev_backup_start_lsn,
134134 if (!XLogRecPtrIsInvalid (prev_backup_start_lsn )
135135 && !XLogRecPtrIsInvalid (page_lsn )
136136 && page_lsn < prev_backup_start_lsn )
137+ {
138+ * n_skipped += 1 ;
137139 return ;
140+ }
138141
139142 /* Verify checksum */
140143 if (current .checksum_version && !is_zero_page )
@@ -195,6 +198,8 @@ backup_data_file(const char *from_root, const char *to_root,
195198 FILE * out ;
196199 BlockNumber blknum = 0 ;
197200 BlockNumber nblocks = 0 ;
201+ int n_blocks_skipped = 0 ;
202+ int n_blocks_read = 0 ;
198203
199204 /* reset size summary */
200205 file -> read_size = 0 ;
@@ -253,26 +258,24 @@ backup_data_file(const char *from_root, const char *to_root,
253258 if (file -> pagemap .bitmapsize == 0 )
254259 {
255260 for (blknum = 0 ; blknum < nblocks ; blknum ++ )
261+ {
256262 backup_data_page (file , prev_backup_start_lsn , blknum ,
257- nblocks , in , out , & (file -> crc ));
263+ nblocks , in , out , & (file -> crc ), & n_blocks_skipped );
264+ n_blocks_read ++ ;
265+ }
258266 }
259267 else
260268 {
261269 datapagemap_iterator_t * iter ;
262270 iter = datapagemap_iterate (& file -> pagemap );
263271 while (datapagemap_next (iter , & blknum ))
272+ {
264273 backup_data_page (file , prev_backup_start_lsn , blknum ,
265- nblocks , in , out , & (file -> crc ));
274+ nblocks , in , out , & (file -> crc ), & n_blocks_skipped );
275+ n_blocks_read ++ ;
276+ }
266277
267278 pg_free (iter );
268- /*
269- * If we have pagemap then file can't be a zero size.
270- * Otherwise, we will clear the last file.
271- * Increase read_size to delete after.
272- * TODO rewrite this code
273- */
274- if (file -> read_size == 0 )
275- file -> read_size ++ ;
276279 }
277280
278281 /* update file permission */
@@ -294,8 +297,11 @@ backup_data_file(const char *from_root, const char *to_root,
294297 if (file -> read_size == 0 )
295298 file -> is_datafile = false;
296299
297- /* We do not backup if all pages skipped. */
298- if (file -> write_size == 0 && file -> read_size > 0 )
300+ /*
301+ * If we have pagemap then file can't be a zero size.
302+ * Otherwise, we will clear the last file.
303+ */
304+ if (n_blocks_read == n_blocks_skipped )
299305 {
300306 if (remove (to_path ) == -1 )
301307 elog (ERROR , "cannot remove file \"%s\": %s" , to_path ,
0 commit comments