@@ -756,28 +756,25 @@ do_backup_instance(void)
756
756
parray_free (prev_backup_filelist );
757
757
}
758
758
759
- /* Copy pg_control in case of backup from replica >= 9.6 */
759
+ /* In case of backup from replica >= 9.6 we must fix minRecPoint,
760
+ * First we must find pg_control in backup_files_list.
761
+ */
760
762
if (current .from_replica && !exclusive_backup )
761
763
{
764
+ char pg_control_path [MAXPGPATH ];
765
+
766
+ snprintf (pg_control_path , sizeof (pg_control_path ), "%s/%s" , pgdata , "global/pg_control" );
767
+
762
768
for (i = 0 ; i < parray_num (backup_files_list ); i ++ )
763
769
{
764
770
pgFile * tmp_file = (pgFile * ) parray_get (backup_files_list , i );
765
771
766
- if (strcmp (tmp_file -> name , "pg_control" ) == 0 )
772
+ if (strcmp (tmp_file -> path , pg_control_path ) == 0 )
767
773
{
768
774
pg_control = tmp_file ;
769
775
break ;
770
776
}
771
777
}
772
-
773
- if (!pg_control )
774
- elog (ERROR , "Failed to locate pg_control in copied files" );
775
-
776
- if (is_remote_backup )
777
- remote_copy_file (NULL , pg_control );
778
- else
779
- if (!copy_file (pgdata , database_path , pg_control ))
780
- elog (ERROR , "Failed to copy pg_control" );
781
778
}
782
779
783
780
@@ -1160,9 +1157,6 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
1160
1157
*/
1161
1158
pg_switch_wal (conn );
1162
1159
1163
- //elog(INFO, "START LSN: %X/%X",
1164
- // (uint32) (backup->start_lsn >> 32), (uint32) (backup->start_lsn));
1165
-
1166
1160
if (current .backup_mode == BACKUP_MODE_DIFF_PAGE )
1167
1161
/* In PAGE mode wait for current segment... */
1168
1162
wait_wal_lsn (backup -> start_lsn , true, false);
@@ -1175,8 +1169,10 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
1175
1169
/* ...for others wait for previous segment */
1176
1170
wait_wal_lsn (backup -> start_lsn , true, true);
1177
1171
1178
- /* Wait for start_lsn to be replayed by replica */
1179
- if (backup -> from_replica )
1172
+ /* In case of backup from replica for PostgreSQL 9.5
1173
+ * wait for start_lsn to be replayed by replica
1174
+ */
1175
+ if (backup -> from_replica && exclusive_backup )
1180
1176
wait_replica_wal_lsn (backup -> start_lsn , true);
1181
1177
}
1182
1178
@@ -1526,7 +1522,7 @@ wait_wal_lsn(XLogRecPtr lsn, bool is_start_lsn, bool wait_prev_segment)
1526
1522
GetXLogFileName (wal_segment , tli , targetSegNo , xlog_seg_size );
1527
1523
1528
1524
/*
1529
- * In pg_start_backup we wait for 'lsn' in 'pg_wal' directory iff it is
1525
+ * In pg_start_backup we wait for 'lsn' in 'pg_wal' directory if it is
1530
1526
* stream and non-page backup. Page backup needs archived WAL files, so we
1531
1527
* wait for 'lsn' in archive 'wal' directory for page backups.
1532
1528
*
@@ -1547,7 +1543,12 @@ wait_wal_lsn(XLogRecPtr lsn, bool is_start_lsn, bool wait_prev_segment)
1547
1543
{
1548
1544
join_path_components (wal_segment_path , arclog_path , wal_segment );
1549
1545
wal_segment_dir = arclog_path ;
1550
- timeout = archive_timeout ;
1546
+
1547
+ if (archive_timeout > 0 )
1548
+ timeout = archive_timeout ;
1549
+ else
1550
+ timeout = ARCHIVE_TIMEOUT_DEFAULT ;
1551
+
1551
1552
}
1552
1553
1553
1554
if (wait_prev_segment )
@@ -1780,14 +1781,29 @@ pg_stop_backup(pgBackup *backup)
1780
1781
* Stop the non-exclusive backup. Besides stop_lsn it returns from
1781
1782
* pg_stop_backup(false) copy of the backup label and tablespace map
1782
1783
* so they can be written to disk by the caller.
1784
+ * In case of backup from replica >= 9.6 we do not trust minRecPoint
1785
+ * and stop_backup LSN, so we use latest replayed LSN as STOP LSN.
1783
1786
*/
1784
- stop_backup_query = "SELECT"
1785
- " pg_catalog.txid_snapshot_xmax(pg_catalog.txid_current_snapshot()),"
1786
- " current_timestamp(0)::timestamptz,"
1787
- " lsn,"
1788
- " labelfile,"
1789
- " spcmapfile"
1790
- " FROM pg_catalog.pg_stop_backup(false)" ;
1787
+ if (current .from_replica )
1788
+ stop_backup_query = "SELECT"
1789
+ " pg_catalog.txid_snapshot_xmax(pg_catalog.txid_current_snapshot()),"
1790
+ " current_timestamp(0)::timestamptz,"
1791
+ #if PG_VERSION_NUM >= 100000
1792
+ " pg_catalog.pg_last_wal_replay_lsn(),"
1793
+ #else
1794
+ " pg_catalog.pg_last_xlog_replay_location(),"
1795
+ #endif
1796
+ " labelfile,"
1797
+ " spcmapfile"
1798
+ " FROM pg_catalog.pg_stop_backup(false)" ;
1799
+ else
1800
+ stop_backup_query = "SELECT"
1801
+ " pg_catalog.txid_snapshot_xmax(pg_catalog.txid_current_snapshot()),"
1802
+ " current_timestamp(0)::timestamptz,"
1803
+ " lsn,"
1804
+ " labelfile,"
1805
+ " spcmapfile"
1806
+ " FROM pg_catalog.pg_stop_backup(false)" ;
1791
1807
1792
1808
}
1793
1809
else
@@ -1873,14 +1889,14 @@ pg_stop_backup(pgBackup *backup)
1873
1889
/* Calculate LSN */
1874
1890
stop_backup_lsn = ((uint64 ) lsn_hi ) << 32 | lsn_lo ;
1875
1891
1876
- //if (!XRecOffIsValid(stop_backup_lsn))
1877
- //{
1878
- // stop_backup_lsn = restore_lsn;
1879
- //}
1880
-
1881
1892
if (!XRecOffIsValid (stop_backup_lsn ))
1882
- elog (ERROR , "Invalid stop_backup_lsn value %X/%X" ,
1883
- (uint32 ) (stop_backup_lsn >> 32 ), (uint32 ) (stop_backup_lsn ));
1893
+ {
1894
+ if (XRecOffIsNull (stop_backup_lsn ))
1895
+ stop_backup_lsn = stop_backup_lsn + SizeOfXLogLongPHD ;
1896
+ else
1897
+ elog (ERROR , "Invalid stop_backup_lsn value %X/%X" ,
1898
+ (uint32 ) (stop_backup_lsn >> 32 ), (uint32 ) (stop_backup_lsn ));
1899
+ }
1884
1900
1885
1901
/* Write backup_label and tablespace_map */
1886
1902
if (!exclusive_backup )
0 commit comments