@@ -56,7 +56,7 @@ static void pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn, PGNode
5656
5757static XLogRecPtr wait_wal_lsn (XLogRecPtr lsn , bool is_start_lsn , TimeLineID tli ,
5858 bool in_prev_segment , bool segment_only ,
59- int timeout_elevel , bool in_stream_dir );
59+ int timeout_elevel , bool in_stream_dir , pgBackup * backup );
6060
6161static void check_external_for_tablespaces (parray * external_list ,
6262 PGconn * backup_conn );
@@ -165,8 +165,10 @@ do_backup_instance(PGconn *backup_conn, PGNodeInfo *nodeInfo, bool no_sync, bool
165165 "trying to look up on previous timelines" ,
166166 current .tli );
167167
168- /* TODO: use read_timeline_history */
169- tli_list = catalog_get_timelines (& instance_config );
168+ tli_list = get_history_streaming (& instance_config .conn_opt , current .tli , backup_list );
169+ if (!tli_list )
170+ /* fallback to using archive */
171+ tli_list = catalog_get_timelines (& instance_config );
170172
171173 if (parray_num (tli_list ) == 0 )
172174 elog (WARNING , "Cannot find valid backup on previous timelines, "
@@ -268,7 +270,7 @@ do_backup_instance(PGconn *backup_conn, PGNodeInfo *nodeInfo, bool no_sync, bool
268270 * Because WAL streaming will start after pg_start_backup() in stream
269271 * mode.
270272 */
271- wait_wal_lsn (current .start_lsn , true, current .tli , false, true, ERROR , false);
273+ wait_wal_lsn (current .start_lsn , true, current .tli , false, true, ERROR , false, & current );
272274 }
273275
274276 /* start stream replication */
@@ -279,6 +281,12 @@ do_backup_instance(PGconn *backup_conn, PGNodeInfo *nodeInfo, bool no_sync, bool
279281
280282 start_WAL_streaming (backup_conn , dst_backup_path , & instance_config .conn_opt ,
281283 current .start_lsn , current .tli );
284+
285+ /* Make sure that WAL streaming is working
286+ * PAGE backup in stream mode is waited twice, first for
287+ * segment in WAL archive and then for streamed segment
288+ */
289+ wait_wal_lsn (current .start_lsn , true, current .tli , false, true, ERROR , true, & current );
282290 }
283291
284292 /* initialize backup's file list */
@@ -1262,7 +1270,7 @@ pg_is_superuser(PGconn *conn)
12621270static XLogRecPtr
12631271wait_wal_lsn (XLogRecPtr target_lsn , bool is_start_lsn , TimeLineID tli ,
12641272 bool in_prev_segment , bool segment_only ,
1265- int timeout_elevel , bool in_stream_dir )
1273+ int timeout_elevel , bool in_stream_dir , pgBackup * backup )
12661274{
12671275 XLogSegNo targetSegNo ;
12681276 char pg_wal_dir [MAXPGPATH ];
@@ -1294,15 +1302,14 @@ wait_wal_lsn(XLogRecPtr target_lsn, bool is_start_lsn, TimeLineID tli,
12941302 */
12951303 if (in_stream_dir )
12961304 {
1297- pgBackupGetPath2 (& current , pg_wal_dir , lengthof (pg_wal_dir ),
1298- DATABASE_DIR , PG_XLOG_DIR );
1305+ join_path_components (pg_wal_dir , backup -> database_dir , PG_XLOG_DIR );
12991306 join_path_components (wal_segment_path , pg_wal_dir , wal_segment );
13001307 wal_segment_dir = pg_wal_dir ;
13011308 }
13021309 else
13031310 {
13041311 join_path_components (wal_segment_path , arclog_path , wal_segment );
1305- wal_segment_dir = arclog_path ;
1312+ wal_segment_dir = arclog_path ; /* global var */
13061313 }
13071314
13081315 /* TODO: remove this in 3.0 (it is a cludge against some old bug with archive_timeout) */
@@ -1394,7 +1401,7 @@ wait_wal_lsn(XLogRecPtr target_lsn, bool is_start_lsn, TimeLineID tli,
13941401
13951402 sleep (1 );
13961403 if (interrupted )
1397- elog (ERROR , "Interrupted during waiting for WAL archiving" );
1404+ elog (ERROR , "Interrupted during waiting for WAL %s" , in_stream_dir ? "streaming" : " archiving" );
13981405 try_count ++ ;
13991406
14001407 /* Inform user if WAL segment is absent in first attempt */
@@ -1418,9 +1425,10 @@ wait_wal_lsn(XLogRecPtr target_lsn, bool is_start_lsn, TimeLineID tli,
14181425 {
14191426 if (file_exists )
14201427 elog (timeout_elevel , "WAL segment %s was %s, "
1421- "but target LSN %X/%X could not be archived in %d seconds" ,
1428+ "but target LSN %X/%X could not be %s in %d seconds" ,
14221429 wal_segment , wal_delivery_str ,
1423- (uint32 ) (target_lsn >> 32 ), (uint32 ) target_lsn , timeout );
1430+ (uint32 ) (target_lsn >> 32 ), (uint32 ) target_lsn ,
1431+ wal_delivery_str , timeout );
14241432 /* If WAL segment doesn't exist or we wait for previous segment */
14251433 else
14261434 elog (timeout_elevel ,
@@ -1572,8 +1580,13 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
15721580 */
15731581 if (pg_stop_backup_is_sent && !in_cleanup )
15741582 {
1583+ int timeout = ARCHIVE_TIMEOUT_DEFAULT ;
15751584 res = NULL ;
15761585
1586+ /* kludge against some old bug in archive_timeout. TODO: remove in 3.0.0 */
1587+ if (instance_config .archive_timeout > 0 )
1588+ timeout = instance_config .archive_timeout ;
1589+
15771590 while (1 )
15781591 {
15791592 if (!PQconsumeInput (conn ))
@@ -1598,11 +1611,10 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
15981611 * If postgres haven't answered in archive_timeout seconds,
15991612 * send an interrupt.
16001613 */
1601- if (pg_stop_backup_timeout > instance_config . archive_timeout )
1614+ if (pg_stop_backup_timeout > timeout )
16021615 {
16031616 pgut_cancel (conn );
1604- elog (ERROR , "pg_stop_backup doesn't answer in %d seconds, cancel it" ,
1605- instance_config .archive_timeout );
1617+ elog (ERROR , "pg_stop_backup doesn't answer in %d seconds, cancel it" , timeout );
16061618 }
16071619 }
16081620 else
@@ -1701,7 +1713,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
17011713 {
17021714 /* Wait for segment with current stop_lsn, it is ok for it to never arrive */
17031715 wait_wal_lsn (stop_backup_lsn_tmp , false, backup -> tli ,
1704- false, true, WARNING , stream_wal );
1716+ false, true, WARNING , stream_wal , backup );
17051717
17061718 /* Get the first record in segment with current stop_lsn */
17071719 lsn_tmp = get_first_record_lsn (xlog_path , segno , backup -> tli ,
@@ -1729,7 +1741,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
17291741 * because previous record can be the contrecord.
17301742 */
17311743 lsn_tmp = wait_wal_lsn (stop_backup_lsn_tmp , false, backup -> tli ,
1732- true, false, ERROR , stream_wal );
1744+ true, false, ERROR , stream_wal , backup );
17331745
17341746 /* sanity */
17351747 if (!XRecOffIsValid (lsn_tmp ) || XLogRecPtrIsInvalid (lsn_tmp ))
@@ -1743,7 +1755,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
17431755 {
17441756 /* Wait for segment with current stop_lsn */
17451757 wait_wal_lsn (stop_backup_lsn_tmp , false, backup -> tli ,
1746- false, true, ERROR , stream_wal );
1758+ false, true, ERROR , stream_wal , backup );
17471759
17481760 /* Get the next closest record in segment with current stop_lsn */
17491761 lsn_tmp = get_next_record_lsn (xlog_path , segno , backup -> tli ,
@@ -1872,7 +1884,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
18721884 */
18731885 if (!stop_lsn_exists )
18741886 stop_backup_lsn = wait_wal_lsn (stop_backup_lsn_tmp , false, backup -> tli ,
1875- false, false, ERROR , stream_wal );
1887+ false, false, ERROR , stream_wal , backup );
18761888
18771889 if (stream_wal )
18781890 {
0 commit comments