@@ -47,10 +47,22 @@ const char *progname = "pg_probackup";
4747static parray * backup_files_list = NULL ;
4848
4949static pthread_mutex_t start_stream_mut = PTHREAD_MUTEX_INITIALIZER ;
50+
5051/*
5152 * We need to wait end of WAL streaming before execute pg_stop_backup().
5253 */
54+ typedef struct
55+ {
56+ const char * basedir ;
57+ /*
58+ * Return value from the thread.
59+ * 0 means there is no error, 1 - there is an error.
60+ */
61+ int ret ;
62+ } StreamThreadArg ;
63+
5364static pthread_t stream_thread ;
65+ static StreamThreadArg stream_thread_arg = {"" , 1 };
5466
5567static int is_ptrack_enable = false;
5668bool is_ptrack_support = false;
@@ -424,6 +436,9 @@ remote_backup_files(void *arg)
424436 file -> path , (unsigned long ) file -> write_size );
425437 PQfinish (file_backup_conn );
426438 }
439+
440+ /* Data files transferring is successful */
441+ arguments -> ret = 0 ;
427442}
428443
429444/*
@@ -441,6 +456,7 @@ do_backup_instance(void)
441456
442457 pthread_t backup_threads [num_threads ];
443458 backup_files_args * backup_threads_args [num_threads ];
459+ bool backup_isok = true;
444460
445461 pgBackup * prev_backup = NULL ;
446462 char prev_backup_filelist_path [MAXPGPATH ];
@@ -541,8 +557,13 @@ do_backup_instance(void)
541557 join_path_components (dst_backup_path , database_path , PG_XLOG_DIR );
542558 dir_create_dir (dst_backup_path , DIR_PERMISSION );
543559
560+ stream_thread_arg .basedir = dst_backup_path ;
561+ /* By default there are some error */
562+ stream_thread_arg .ret = 1 ;
563+
544564 pthread_mutex_lock (& start_stream_mut );
545- pthread_create (& stream_thread , NULL , (void * (* )(void * )) StreamLog , dst_backup_path );
565+ pthread_create (& stream_thread , NULL , (void * (* )(void * )) StreamLog ,
566+ & stream_thread_arg );
546567 pthread_mutex_lock (& start_stream_mut );
547568 if (conn == NULL )
548569 elog (ERROR , "Cannot continue backup because stream connect has failed." );
@@ -653,6 +674,8 @@ do_backup_instance(void)
653674 arg -> prev_backup_start_lsn = prev_backup_start_lsn ;
654675 arg -> thread_backup_conn = NULL ;
655676 arg -> thread_cancel_conn = NULL ;
677+ /* By default there are some error */
678+ arg -> ret = 1 ;
656679 backup_threads_args [i ] = arg ;
657680 }
658681
@@ -676,9 +699,15 @@ do_backup_instance(void)
676699 for (i = 0 ; i < num_threads ; i ++ )
677700 {
678701 pthread_join (backup_threads [i ], NULL );
702+ if (backup_threads_args [i ]-> ret == 1 )
703+ backup_isok = false;
704+
679705 pg_free (backup_threads_args [i ]);
680706 }
681- elog (LOG , "Data files are transfered" );
707+ if (backup_isok )
708+ elog (LOG , "Data files are transfered" );
709+ else
710+ elog (ERROR , "Data files transferring failed" );
682711
683712 /* clean previous backup file list */
684713 if (prev_backup_filelist )
@@ -1784,8 +1813,12 @@ pg_stop_backup(pgBackup *backup)
17841813 PQclear (res );
17851814
17861815 if (stream_wal )
1816+ {
17871817 /* Wait for the completion of stream */
17881818 pthread_join (stream_thread , NULL );
1819+ if (stream_thread_arg .ret == 1 )
1820+ elog (ERROR , "WAL streaming failed" );
1821+ }
17891822 }
17901823
17911824 /* Fill in fields if that is the correct end of backup. */
@@ -2025,6 +2058,8 @@ backup_files(void *arg)
20252058 if (arguments -> thread_backup_conn )
20262059 pgut_disconnect (arguments -> thread_backup_conn );
20272060
2061+ /* Data files transferring is successful */
2062+ arguments -> ret = 0 ;
20282063}
20292064
20302065/*
@@ -2616,7 +2651,7 @@ StreamLog(void *arg)
26162651{
26172652 XLogRecPtr startpos ;
26182653 TimeLineID starttli ;
2619- char * basedir = (char * )arg ;
2654+ StreamThreadArg * stream_arg = (StreamThreadArg * ) arg ;
26202655
26212656 /*
26222657 * Connect in replication mode to the server
@@ -2682,7 +2717,7 @@ StreamLog(void *arg)
26822717 ctl .sysidentifier = NULL ;
26832718
26842719#if PG_VERSION_NUM >= 100000
2685- ctl .walmethod = CreateWalDirectoryMethod (basedir , 0 , true);
2720+ ctl .walmethod = CreateWalDirectoryMethod (stream_arg -> basedir , 0 , true);
26862721 ctl .replication_slot = replication_slot ;
26872722 ctl .stop_socket = PGINVALID_SOCKET ;
26882723#else
@@ -2713,6 +2748,7 @@ StreamLog(void *arg)
27132748
27142749 elog (LOG , _ ("finished streaming WAL at %X/%X (timeline %u)" ),
27152750 (uint32 ) (stop_stream_lsn >> 32 ), (uint32 ) stop_stream_lsn , starttli );
2751+ stream_arg -> ret = 0 ;
27162752
27172753 PQfinish (conn );
27182754 conn = NULL ;
0 commit comments