@@ -72,7 +72,7 @@ static void pg_switch_xlog(void);
7272static bool pg_is_standby (void );
7373static void add_pgdata_files (parray * files ,const char * root );
7474static void create_file_list (parray * files ,const char * root ,bool is_append );
75- static void wait_archive_lsn (XLogRecPtr lsn ,bool last_segno );
75+ static void wait_archive_lsn (XLogRecPtr lsn ,bool prev_segno );
7676static void make_pagemap_from_ptrack (parray * files );
7777static void StreamLog (void * arg );
7878
@@ -230,17 +230,9 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
230230 * Build page mapping in differential mode. When using this mode, the
231231 * list of blocks to be taken is known by scanning the WAL segments
232232 * present in archives up to the point where start backup has begun.
233- * However, normally this segment is not yet available in the archives,
234- * leading to failures when building the page map. Hence before doing
235- * anything and in order to ensure that all the segments needed for the
236- * scan are here, for a switch of the last segment with pg_switch_xlog.
237233 */
238234if (current .backup_mode == BACKUP_MODE_DIFF_PAGE )
239235{
240- /* Enforce archiving of last segment and wait for it to be here */
241- if (!from_replica )
242- pg_switch_xlog ();
243-
244236/* Now build the page map */
245237parray_qsort (backup_files_list ,pgFileComparePathDesc );
246238elog (LOG ,"extractPageMap" );
@@ -252,7 +244,12 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
252244 (uint32 ) (current .start_lsn >>32 ),
253245 (uint32 ) (current .start_lsn ));
254246extractPageMap (arclog_path ,prev_backup -> start_lsn ,current .tli ,
255- current .start_lsn );
247+ current .start_lsn ,
248+ /*
249+ * For backup from master wait for previous segment.
250+ * For backup from replica wait for current segment.
251+ */
252+ !from_replica );
256253}
257254else if (current .backup_mode == BACKUP_MODE_DIFF_PTRACK )
258255{
@@ -602,7 +599,12 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
602599backup -> start_lsn = (XLogRecPtr ) ((uint64 )xlogid <<32 ) |xrecoff ;
603600
604601if (!stream_wal )
605- wait_archive_lsn (backup -> start_lsn , true);
602+ wait_archive_lsn (backup -> start_lsn ,
603+ /*
604+ * For backup from master wait for previous segment.
605+ * For backup from replica wait for current segment.
606+ */
607+ !from_replica );
606608
607609PQclear (res );
608610}
@@ -729,7 +731,7 @@ pg_ptrack_get_and_clear(Oid tablespace_oid, Oid db_oid, Oid rel_oid,
729731}
730732
731733static void
732- wait_archive_lsn (XLogRecPtr lsn ,bool last_segno )
734+ wait_archive_lsn (XLogRecPtr lsn ,bool prev_segno )
733735{
734736TimeLineID tli ;
735737XLogSegNo targetSegNo ;
@@ -741,7 +743,7 @@ wait_archive_lsn(XLogRecPtr lsn, bool last_segno)
741743
742744/* As well as WAL file name */
743745XLByteToSeg (lsn ,targetSegNo );
744- if (last_segno )
746+ if (prev_segno )
745747targetSegNo -- ;
746748XLogFileName (wal_file ,tli ,targetSegNo );
747749
@@ -844,6 +846,9 @@ pg_stop_backup(pgBackup *backup)
844846
845847PQclear (res );
846848
849+ if (!stream_wal )
850+ wait_archive_lsn (stop_backup_lsn , false);
851+
847852/* Fill in fields if backup exists */
848853if (backup != NULL )
849854{
@@ -868,9 +873,6 @@ pg_stop_backup(pgBackup *backup)
868873
869874PQclear (res );
870875}
871-
872- if (!stream_wal )
873- wait_archive_lsn (stop_backup_lsn , false);
874876}
875877
876878/*