@@ -46,14 +46,14 @@ const char *progname = "pg_probackup";
46
46
/* list of files contained in backup */
47
47
static parray * backup_files_list = NULL ;
48
48
49
- static pthread_mutex_t start_stream_mut = PTHREAD_MUTEX_INITIALIZER ;
50
-
51
49
/*
52
50
* We need to wait end of WAL streaming before execute pg_stop_backup().
53
51
*/
54
52
typedef struct
55
53
{
56
54
const char * basedir ;
55
+ PGconn * conn ;
56
+
57
57
/*
58
58
* Return value from the thread.
59
59
* 0 means there is no error, 1 - there is an error.
@@ -62,7 +62,7 @@ typedef struct
62
62
} StreamThreadArg ;
63
63
64
64
static pthread_t stream_thread ;
65
- static StreamThreadArg stream_thread_arg = {"" , 1 };
65
+ static StreamThreadArg stream_thread_arg = {"" , NULL , 1 };
66
66
67
67
static int is_ptrack_enable = false;
68
68
bool is_ptrack_support = false;
@@ -558,17 +558,39 @@ do_backup_instance(void)
558
558
dir_create_dir (dst_backup_path , DIR_PERMISSION );
559
559
560
560
stream_thread_arg .basedir = dst_backup_path ;
561
+
562
+ /*
563
+ * Connect in replication mode to the server.
564
+ */
565
+ stream_thread_arg .conn = pgut_connect_replication (pgut_dbname );
566
+
567
+ if (!CheckServerVersionForStreaming (stream_thread_arg .conn ))
568
+ {
569
+ PQfinish (stream_thread_arg .conn );
570
+ /*
571
+ * Error message already written in CheckServerVersionForStreaming().
572
+ * There's no hope of recovering from a version mismatch, so don't
573
+ * retry.
574
+ */
575
+ elog (ERROR , "Cannot continue backup because stream connect has failed." );
576
+ }
577
+
578
+ /*
579
+ * Identify server, obtaining start LSN position and current timeline ID
580
+ * at the same time, necessary if not valid data can be found in the
581
+ * existing output directory.
582
+ */
583
+ if (!RunIdentifySystem (stream_thread_arg .conn , NULL , NULL , NULL , NULL ))
584
+ {
585
+ PQfinish (stream_thread_arg .conn );
586
+ elog (ERROR , "Cannot continue backup because stream connect has failed." );
587
+ }
588
+
561
589
/* By default there are some error */
562
590
stream_thread_arg .ret = 1 ;
563
591
564
- pthread_mutex_lock (& start_stream_mut );
565
592
pthread_create (& stream_thread , NULL , (void * (* )(void * )) StreamLog ,
566
593
& stream_thread_arg );
567
- pthread_mutex_lock (& start_stream_mut );
568
- if (conn == NULL )
569
- elog (ERROR , "Cannot continue backup because stream connect has failed." );
570
-
571
- pthread_mutex_unlock (& start_stream_mut );
572
594
}
573
595
574
596
/* initialize backup list */
@@ -2653,43 +2675,11 @@ StreamLog(void *arg)
2653
2675
TimeLineID starttli ;
2654
2676
StreamThreadArg * stream_arg = (StreamThreadArg * ) arg ;
2655
2677
2656
- /*
2657
- * Connect in replication mode to the server
2658
- */
2659
- if (conn == NULL )
2660
- conn = pgut_connect_replication (pgut_dbname );
2661
- if (!conn )
2662
- {
2663
- pthread_mutex_unlock (& start_stream_mut );
2664
- /* Error message already written in GetConnection() */
2665
- return ;
2666
- }
2667
-
2668
- if (!CheckServerVersionForStreaming (conn ))
2669
- {
2670
- /*
2671
- * Error message already written in CheckServerVersionForStreaming().
2672
- * There's no hope of recovering from a version mismatch, so don't
2673
- * retry.
2674
- */
2675
- disconnect_and_exit (1 );
2676
- }
2677
-
2678
- /*
2679
- * Identify server, obtaining start LSN position and current timeline ID
2680
- * at the same time, necessary if not valid data can be found in the
2681
- * existing output directory.
2682
- */
2683
- if (!RunIdentifySystem (conn , NULL , & starttli , & startpos , NULL ))
2684
- disconnect_and_exit (1 );
2685
-
2686
- /* Ok we have normal stream connect and main process can work again */
2687
- pthread_mutex_unlock (& start_stream_mut );
2688
-
2689
2678
/*
2690
2679
* We must use startpos as start_lsn from start_backup
2691
2680
*/
2692
2681
startpos = current .start_lsn ;
2682
+ starttli = current .tli ;
2693
2683
2694
2684
/*
2695
2685
* Always start streaming at the beginning of a segment
@@ -2730,7 +2720,7 @@ StreamLog(void *arg)
2730
2720
ctl .synchronous = false;
2731
2721
ctl .mark_done = false;
2732
2722
2733
- if (ReceiveXlogStream (conn , & ctl ) == false)
2723
+ if (ReceiveXlogStream (stream_arg -> conn , & ctl ) == false)
2734
2724
elog (ERROR , "Problem in receivexlog" );
2735
2725
2736
2726
#if PG_VERSION_NUM >= 100000
@@ -2740,18 +2730,18 @@ StreamLog(void *arg)
2740
2730
#endif
2741
2731
}
2742
2732
#else
2743
- if (ReceiveXlogStream (conn , startpos , starttli , NULL , basedir ,
2744
- stop_streaming , standby_message_timeout , NULL ,
2745
- false, false) == false)
2733
+ if (ReceiveXlogStream (stream_arg -> conn , startpos , starttli , NULL , basedir ,
2734
+ stop_streaming , standby_message_timeout , NULL ,
2735
+ false, false) == false)
2746
2736
elog (ERROR , "Problem in receivexlog" );
2747
2737
#endif
2748
2738
2749
2739
elog (LOG , _ ("finished streaming WAL at %X/%X (timeline %u)" ),
2750
2740
(uint32 ) (stop_stream_lsn >> 32 ), (uint32 ) stop_stream_lsn , starttli );
2751
2741
stream_arg -> ret = 0 ;
2752
2742
2753
- PQfinish (conn );
2754
- conn = NULL ;
2743
+ PQfinish (stream_arg -> conn );
2744
+ stream_arg -> conn = NULL ;
2755
2745
}
2756
2746
2757
2747
/*
0 commit comments