@@ -67,7 +67,6 @@ static parray *do_backup_database(parray *backup_list, bool smooth_checkpoint);
67
67
68
68
static void pg_start_backup (const char * label , bool smooth , pgBackup * backup );
69
69
static void pg_stop_backup (pgBackup * backup );
70
- static void pg_switch_xlog (void );
71
70
72
71
static bool pg_is_standby (void );
73
72
static void add_pgdata_files (parray * files , const char * root );
@@ -169,7 +168,7 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
169
168
/* start stream replication */
170
169
if (stream_wal )
171
170
{
172
- join_path_components (dst_backup_path , database_path , "pg_xlog" );
171
+ join_path_components (dst_backup_path , database_path , PG_XLOG_DIR );
173
172
dir_create_dir (dst_backup_path , DIR_PERMISSION );
174
173
175
174
pthread_mutex_lock (& check_stream_mut );
@@ -186,14 +185,14 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
186
185
char label_path [MAXPGPATH ];
187
186
188
187
/* If backup_label does not exist in $PGDATA, stop taking backup */
189
- join_path_components (label_path , pgdata , "backup_label" );
188
+ join_path_components (label_path , pgdata , PG_BACKUP_LABEL_FILE );
190
189
191
190
/* Leave if no backup file */
192
191
if (!fileExists (label_path ))
193
192
{
194
- elog (LOG , "backup_label does not exist, stopping backup" );
193
+ elog (LOG , "%s does not exist, stopping backup" , PG_BACKUP_LABEL_FILE );
195
194
pg_stop_backup (NULL );
196
- elog (ERROR , "backup_label does not exist in PGDATA." );
195
+ elog (ERROR , "%s does not exist in PGDATA" , PG_BACKUP_LABEL_FILE );
197
196
}
198
197
}
199
198
@@ -353,7 +352,7 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
353
352
354
353
/* Scan backup pg_xlog dir */
355
354
list_file = parray_new ();
356
- join_path_components (pg_xlog_path , database_path , "pg_xlog" );
355
+ join_path_components (pg_xlog_path , database_path , PG_XLOG_DIR );
357
356
dir_list_file (list_file , pg_xlog_path , false, true, false);
358
357
359
358
/* Remove file path root prefix and calc meta */
@@ -739,6 +738,8 @@ wait_archive_lsn(XLogRecPtr lsn, bool prev_segno)
739
738
char wal_file [MAXFNAMELEN ];
740
739
uint32 try_count = 0 ;
741
740
741
+ Assert (!stream_wal );
742
+
742
743
tli = get_current_timeline (false);
743
744
744
745
/* As well as WAL file name */
@@ -764,8 +765,8 @@ wait_archive_lsn(XLogRecPtr lsn, bool prev_segno)
764
765
765
766
if (archive_timeout > 0 && try_count > archive_timeout )
766
767
elog (ERROR ,
767
- "switched WAL could not be archived in %d seconds" ,
768
- archive_timeout );
768
+ "switched WAL segment %s could not be archived in %d seconds" ,
769
+ wal_file , archive_timeout );
769
770
}
770
771
}
771
772
@@ -778,6 +779,8 @@ pg_stop_backup(pgBackup *backup)
778
779
PGresult * res ;
779
780
uint32 xlogid ;
780
781
uint32 xrecoff ;
782
+ time_t recovery_time ;
783
+ TransactionId recovery_xid ;
781
784
782
785
/* Remove annoying NOTICE messages generated by backend */
783
786
res = pgut_execute (backup_conn , "SET client_min_messages = warning;" ,
@@ -786,10 +789,18 @@ pg_stop_backup(pgBackup *backup)
786
789
787
790
if (from_replica )
788
791
res = pgut_execute (backup_conn ,
789
- "SELECT * FROM pg_stop_backup(false)" , 0 , NULL );
792
+ "SELECT *, txid_snapshot_xmax(txid_current_snapshot()) FROM pg_stop_backup(false)" ,
793
+ 0 , NULL );
790
794
else
791
795
res = pgut_execute (backup_conn ,
792
- "SELECT * FROM pg_stop_backup()" , 0 , NULL );
796
+ "SELECT *, txid_snapshot_xmax(txid_current_snapshot()) FROM pg_stop_backup()" ,
797
+ 0 , NULL );
798
+
799
+ /*
800
+ * We will use this value if there are no transactions between start_lsn
801
+ * and stop_lsn.
802
+ */
803
+ recovery_time = time (NULL );
793
804
794
805
/*
795
806
* Extract timeline and LSN from results of pg_stop_backup()
@@ -809,7 +820,7 @@ pg_stop_backup(pgBackup *backup)
809
820
Assert (PQnfields (res ) >= 3 );
810
821
811
822
pgBackupGetPath (& current , path , lengthof (path ), DATABASE_DIR );
812
- join_path_components (backup_label , path , "backup_label" );
823
+ join_path_components (backup_label , path , PG_BACKUP_LABEL_FILE );
813
824
814
825
/* Write backup_label */
815
826
fp = fopen (backup_label , "w" );
@@ -823,7 +834,7 @@ pg_stop_backup(pgBackup *backup)
823
834
file = pgFileNew (backup_label , true);
824
835
calc_file (file );
825
836
free (file -> path );
826
- file -> path = strdup ("backup_label" );
837
+ file -> path = strdup (PG_BACKUP_LABEL_FILE );
827
838
parray_append (backup_files_list , file );
828
839
829
840
/* Write tablespace_map */
@@ -847,7 +858,17 @@ pg_stop_backup(pgBackup *backup)
847
858
file -> path = strdup ("tablespace_map" );
848
859
parray_append (backup_files_list , file );
849
860
}
861
+
862
+ if (sscanf (PQgetvalue (res , 0 , 3 ), XID_FMT , & recovery_xid ) != 1 )
863
+ elog (ERROR ,
864
+ "result of txid_snapshot_xmax() is invalid: %s" ,
865
+ PQerrorMessage (backup_conn ));
850
866
}
867
+ else
868
+ if (sscanf (PQgetvalue (res , 0 , 1 ), XID_FMT , & recovery_xid ) != 1 )
869
+ elog (ERROR ,
870
+ "result of txid_snapshot_xmax() is invalid: %s" ,
871
+ PQerrorMessage (backup_conn ));
851
872
852
873
PQclear (res );
853
874
@@ -857,63 +878,30 @@ pg_stop_backup(pgBackup *backup)
857
878
/* Fill in fields if backup exists */
858
879
if (backup != NULL )
859
880
{
860
- backup -> tli = get_current_timeline (false);
861
- backup -> stop_lsn = stop_backup_lsn ;
881
+ char * xlog_path ,
882
+ stream_xlog_path [ MAXPGPATH ] ;
862
883
863
- if (from_replica )
864
- res = pgut_execute (backup_conn , TXID_CURRENT_IF_SQL , 0 , NULL );
884
+ if (stream_wal )
885
+ {
886
+ join_path_components (stream_xlog_path , pgdata , PG_XLOG_DIR );
887
+ xlog_path = stream_xlog_path ;
888
+ }
865
889
else
866
- res = pgut_execute (backup_conn , TXID_CURRENT_SQL , 0 , NULL );
867
-
868
- if (sscanf (PQgetvalue (res , 0 , 0 ), XID_FMT , & backup -> recovery_xid ) != 1 )
869
- elog (ERROR ,
870
- "result of txid_current() is invalid: %s" ,
871
- PQerrorMessage (backup_conn ));
872
- backup -> recovery_time = time (NULL );
890
+ xlog_path = arclog_path ;
873
891
874
- elog (LOG , "finish backup: tli=%X lsn=%X/%08X xid=%s" ,
875
- backup -> tli ,
876
- (uint32 ) (backup -> stop_lsn >> 32 ), (uint32 ) backup -> stop_lsn ,
877
- PQgetvalue (res , 0 , 0 ));
892
+ backup -> tli = get_current_timeline (false);
893
+ backup -> stop_lsn = stop_backup_lsn ;
878
894
879
- PQclear (res );
895
+ if (!read_recovery_info (xlog_path , backup -> tli ,
896
+ backup -> start_lsn , backup -> stop_lsn ,
897
+ & backup -> recovery_time , & backup -> recovery_xid ))
898
+ {
899
+ backup -> recovery_time = recovery_time ;
900
+ backup -> recovery_xid = recovery_xid ;
901
+ }
880
902
}
881
903
}
882
904
883
- /*
884
- * Switch to a new WAL segment for master.
885
- */
886
- static void
887
- pg_switch_xlog (void )
888
- {
889
- PGresult * res ;
890
- XLogRecPtr lsn ;
891
- uint32 xlogid ;
892
- uint32 xrecoff ;
893
-
894
- /* Remove annoying NOTICE messages generated by backend */
895
- res = pgut_execute (backup_conn , "SET client_min_messages = warning;" , 0 ,
896
- NULL );
897
- PQclear (res );
898
-
899
- res = pgut_execute (backup_conn , "SELECT * FROM pg_switch_xlog()" ,
900
- 0 , NULL );
901
-
902
- /*
903
- * Extract timeline and LSN from results of pg_stop_backup()
904
- * and friends.
905
- */
906
- XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & xlogid , & xrecoff );
907
- /* Calculate LSN */
908
- lsn = (XLogRecPtr ) ((uint64 ) xlogid << 32 ) | xrecoff ;
909
-
910
- PQclear (res );
911
-
912
- /* Wait for returned lsn - 1 in archive folder */
913
- wait_archive_lsn (lsn , false);
914
- }
915
-
916
-
917
905
/*
918
906
* Check if node is a standby by looking at the presence of
919
907
* recovery.conf.
@@ -957,11 +945,10 @@ backup_cleanup(bool fatal, void *userdata)
957
945
return ;
958
946
959
947
/* If backup_label exist in $PGDATA, notify stop of backup to PostgreSQL */
960
- snprintf (path , lengthof (path ), "%s/backup_label" , pgdata );
961
- make_native_path (path );
948
+ join_path_components (path , pgdata , PG_BACKUP_LABEL_FILE );
962
949
if (fileExists (path ))
963
950
{
964
- elog (LOG , "backup_label exists, stop backup" );
951
+ elog (LOG , "%s exists, stop backup" , PG_BACKUP_LABEL_FILE );
965
952
pg_stop_backup (NULL ); /* don't care stop_lsn on error case */
966
953
}
967
954
@@ -1240,7 +1227,7 @@ add_pgdata_files(parray *files, const char *root)
1240
1227
relative = file -> path + strlen (root ) + 1 ;
1241
1228
if (!path_is_prefix_of_path ("base" , relative ) &&
1242
1229
/*!path_is_prefix_of_path("global", relative) &&*/ //TODO What's wrong with this line?
1243
- !path_is_prefix_of_path ("pg_tblspc" , relative ))
1230
+ !path_is_prefix_of_path (PG_TBLSPC_DIR , relative ))
1244
1231
continue ;
1245
1232
1246
1233
/* Get file name from path */
@@ -1508,7 +1495,7 @@ make_pagemap_from_ptrack(parray *files)
1508
1495
}
1509
1496
/* For unix only now */
1510
1497
sscanf (tmp_path , "%u/%u_ptrack" , & db_oid , & rel_oid );
1511
- tablespace = strstr (p -> ptrack_path , "pg_tblspc" );
1498
+ tablespace = strstr (p -> ptrack_path , PG_TBLSPC_DIR );
1512
1499
if (tablespace != NULL )
1513
1500
sscanf (tablespace + 10 , "%i/" , & tablespace_oid );
1514
1501
0 commit comments