@@ -104,7 +104,8 @@ static int checkpoint_timeout(void);
104104
105105//static void backup_list_file(parray *files, const char *root, )
106106static void parse_backup_filelist_filenames (parray * files , const char * root );
107- static void wait_wal_lsn (XLogRecPtr lsn , bool wait_prev_segment );
107+ static void wait_wal_lsn (XLogRecPtr lsn , bool is_start_lsn ,
108+ bool wait_prev_segment );
108109static void wait_replica_wal_lsn (XLogRecPtr lsn , bool is_start_backup );
109110static void make_pagemap_from_ptrack (parray * files );
110111static void * StreamLog (void * arg );
@@ -717,7 +718,7 @@ do_backup_instance(void)
717718 }
718719
719720 /* Run threads */
720- elog (LOG , "Start transfering data files" );
721+ elog (INFO , "Start transfering data files" );
721722 for (i = 0 ; i < num_threads ; i ++ )
722723 {
723724 backup_files_arg * arg = & (threads_args [i ]);
@@ -738,7 +739,7 @@ do_backup_instance(void)
738739 backup_isok = false;
739740 }
740741 if (backup_isok )
741- elog (LOG , "Data files are transfered" );
742+ elog (INFO , "Data files are transfered" );
742743 else
743744 elog (ERROR , "Data files transferring failed" );
744745
@@ -1070,8 +1071,8 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
10701071{
10711072 PGresult * res ;
10721073 const char * params [2 ];
1073- uint32 xlogid ;
1074- uint32 xrecoff ;
1074+ uint32 lsn_hi ;
1075+ uint32 lsn_lo ;
10751076 PGconn * conn ;
10761077
10771078 params [0 ] = label ;
@@ -1099,9 +1100,9 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
10991100 backup_in_progress = true;
11001101
11011102 /* Extract timeline and LSN from results of pg_start_backup() */
1102- XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & xlogid , & xrecoff );
1103+ XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & lsn_hi , & lsn_lo );
11031104 /* Calculate LSN */
1104- backup -> start_lsn = (XLogRecPtr ) (( uint64 ) xlogid << 32 ) | xrecoff ;
1105+ backup -> start_lsn = (( uint64 ) lsn_hi ) << 32 | lsn_lo ;
11051106
11061107 PQclear (res );
11071108
@@ -1112,20 +1113,17 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
11121113 */
11131114 pg_switch_wal (conn );
11141115
1115- if (!stream_wal )
1116- {
1117- /*
1118- * Do not wait start_lsn for stream backup.
1119- * Because WAL streaming will start after pg_start_backup() in stream
1120- * mode.
1121- */
1116+ if (current .backup_mode == BACKUP_MODE_DIFF_PAGE )
11221117 /* In PAGE mode wait for current segment... */
1123- if (current .backup_mode == BACKUP_MODE_DIFF_PAGE )
1124- wait_wal_lsn (backup -> start_lsn , false);
1118+ wait_wal_lsn (backup -> start_lsn , true, false);
1119+ /*
1120+ * Do not wait start_lsn for stream backup.
1121+ * Because WAL streaming will start after pg_start_backup() in stream
1122+ * mode.
1123+ */
1124+ else if (!stream_wal )
11251125 /* ...for others wait for previous segment */
1126- else
1127- wait_wal_lsn (backup -> start_lsn , true);
1128- }
1126+ wait_wal_lsn (backup -> start_lsn , true, true);
11291127
11301128 /* Wait for start_lsn to be replayed by replica */
11311129 if (backup -> from_replica )
@@ -1443,16 +1441,20 @@ pg_ptrack_get_and_clear(Oid tablespace_oid, Oid db_oid, Oid rel_filenode,
14431441 * If current backup started in stream mode wait for 'lsn' to be streamed in
14441442 * 'pg_wal' directory.
14451443 *
1444+ * If 'is_start_lsn' is true and backup mode is PAGE then we wait for 'lsn' to
1445+ * be archived in archive 'wal' directory regardless stream mode.
1446+ *
14461447 * If 'wait_prev_segment' wait for previous segment.
14471448 */
14481449static void
1449- wait_wal_lsn (XLogRecPtr lsn , bool wait_prev_segment )
1450+ wait_wal_lsn (XLogRecPtr lsn , bool is_start_lsn , bool wait_prev_segment )
14501451{
14511452 TimeLineID tli ;
14521453 XLogSegNo targetSegNo ;
1453- char wal_dir [MAXPGPATH ],
1454- wal_segment_path [MAXPGPATH ];
1455- char wal_segment [MAXFNAMELEN ];
1454+ char pg_wal_dir [MAXPGPATH ];
1455+ char wal_segment_path [MAXPGPATH ],
1456+ * wal_segment_dir ,
1457+ wal_segment [MAXFNAMELEN ];
14561458 bool file_exists = false;
14571459 uint32 try_count = 0 ,
14581460 timeout ;
@@ -1469,18 +1471,28 @@ wait_wal_lsn(XLogRecPtr lsn, bool wait_prev_segment)
14691471 targetSegNo -- ;
14701472 XLogFileName (wal_segment , tli , targetSegNo );
14711473
1472- if (stream_wal )
1474+ /*
1475+ * In pg_start_backup we wait for 'lsn' in 'pg_wal' directory iff it is
1476+ * stream and non-page backup. Page backup needs archived WAL files, so we
1477+ * wait for 'lsn' in archive 'wal' directory for page backups.
1478+ *
1479+ * In pg_stop_backup it depends only on stream_wal.
1480+ */
1481+ if (stream_wal &&
1482+ (current .backup_mode != BACKUP_MODE_DIFF_PAGE || !is_start_lsn ))
14731483 {
1474- pgBackupGetPath2 (& current , wal_dir , lengthof (wal_dir ),
1484+ pgBackupGetPath2 (& current , pg_wal_dir , lengthof (pg_wal_dir ),
14751485 DATABASE_DIR , PG_XLOG_DIR );
1476- join_path_components (wal_segment_path , wal_dir , wal_segment );
1486+ join_path_components (wal_segment_path , pg_wal_dir , wal_segment );
1487+ wal_segment_dir = pg_wal_dir ;
14771488
14781489 timeout = (uint32 ) checkpoint_timeout ();
14791490 timeout = timeout + timeout * 0.1 ;
14801491 }
14811492 else
14821493 {
14831494 join_path_components (wal_segment_path , arclog_path , wal_segment );
1495+ wal_segment_dir = arclog_path ;
14841496 timeout = archive_timeout ;
14851497 }
14861498
@@ -1523,8 +1535,7 @@ wait_wal_lsn(XLogRecPtr lsn, bool wait_prev_segment)
15231535 /*
15241536 * A WAL segment found. Check LSN on it.
15251537 */
1526- if ((stream_wal && wal_contains_lsn (wal_dir , lsn , tli )) ||
1527- (!stream_wal && wal_contains_lsn (arclog_path , lsn , tli )))
1538+ if (wal_contains_lsn (wal_segment_dir , lsn , tli ))
15281539 /* Target LSN was found */
15291540 {
15301541 elog (LOG , "Found LSN: %X/%X" , (uint32 ) (lsn >> 32 ), (uint32 ) lsn );
@@ -1574,8 +1585,8 @@ wait_replica_wal_lsn(XLogRecPtr lsn, bool is_start_backup)
15741585 while (true)
15751586 {
15761587 PGresult * res ;
1577- uint32 xlogid ;
1578- uint32 xrecoff ;
1588+ uint32 lsn_hi ;
1589+ uint32 lsn_lo ;
15791590 XLogRecPtr replica_lsn ;
15801591
15811592 /*
@@ -1606,9 +1617,9 @@ wait_replica_wal_lsn(XLogRecPtr lsn, bool is_start_backup)
16061617 }
16071618
16081619 /* Extract timeline and LSN from result */
1609- XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & xlogid , & xrecoff );
1620+ XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & lsn_hi , & lsn_lo );
16101621 /* Calculate LSN */
1611- replica_lsn = (XLogRecPtr ) (( uint64 ) xlogid << 32 ) | xrecoff ;
1622+ replica_lsn = (( uint64 ) lsn_hi ) << 32 | lsn_lo ;
16121623 PQclear (res );
16131624
16141625 /* target lsn was replicated */
@@ -1642,10 +1653,10 @@ pg_stop_backup(pgBackup *backup)
16421653 PGconn * conn ;
16431654 PGresult * res ;
16441655 PGresult * tablespace_map_content = NULL ;
1645- uint32 xlogid ;
1646- uint32 xrecoff ;
1656+ uint32 lsn_hi ;
1657+ uint32 lsn_lo ;
16471658 XLogRecPtr restore_lsn = InvalidXLogRecPtr ;
1648- int pg_stop_backup_timeout = 0 ;
1659+ int pg_stop_backup_timeout = 0 ;
16491660 char path [MAXPGPATH ];
16501661 char backup_label [MAXPGPATH ];
16511662 FILE * fp ;
@@ -1688,6 +1699,10 @@ pg_stop_backup(pgBackup *backup)
16881699
16891700 res = pgut_execute (conn , "SELECT pg_catalog.pg_create_restore_point($1)" ,
16901701 1 , params );
1702+ /* Extract timeline and LSN from the result */
1703+ XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & lsn_hi , & lsn_lo );
1704+ /* Calculate LSN */
1705+ restore_lsn = ((uint64 ) lsn_hi ) << 32 | lsn_lo ;
16911706 PQclear (res );
16921707 }
16931708
@@ -1720,7 +1735,6 @@ pg_stop_backup(pgBackup *backup)
17201735 }
17211736 else
17221737 {
1723-
17241738 stop_backup_query = "SELECT"
17251739 " pg_catalog.txid_snapshot_xmax(pg_catalog.txid_current_snapshot()),"
17261740 " current_timestamp(0)::timestamptz,"
@@ -1739,6 +1753,8 @@ pg_stop_backup(pgBackup *backup)
17391753 */
17401754 if (pg_stop_backup_is_sent && !in_cleanup )
17411755 {
1756+ res = NULL ;
1757+
17421758 while (1 )
17431759 {
17441760 if (!PQconsumeInput (conn ) || PQisBusy (conn ))
@@ -1780,8 +1796,11 @@ pg_stop_backup(pgBackup *backup)
17801796 {
17811797 switch (PQresultStatus (res ))
17821798 {
1799+ /*
1800+ * We should expect only PGRES_TUPLES_OK since pg_stop_backup
1801+ * returns tuples.
1802+ */
17831803 case PGRES_TUPLES_OK :
1784- case PGRES_COMMAND_OK :
17851804 break ;
17861805 default :
17871806 elog (ERROR , "query failed: %s query was: %s" ,
@@ -1793,9 +1812,9 @@ pg_stop_backup(pgBackup *backup)
17931812 backup_in_progress = false;
17941813
17951814 /* Extract timeline and LSN from results of pg_stop_backup() */
1796- XLogDataFromLSN (PQgetvalue (res , 0 , 2 ), & xlogid , & xrecoff );
1815+ XLogDataFromLSN (PQgetvalue (res , 0 , 2 ), & lsn_hi , & lsn_lo );
17971816 /* Calculate LSN */
1798- stop_backup_lsn = (XLogRecPtr ) (( uint64 ) xlogid << 32 ) | xrecoff ;
1817+ stop_backup_lsn = (( uint64 ) lsn_hi ) << 32 | lsn_lo ;
17991818
18001819 if (!XRecOffIsValid (stop_backup_lsn ))
18011820 {
@@ -1912,7 +1931,7 @@ pg_stop_backup(pgBackup *backup)
19121931 * Wait for stop_lsn to be archived or streamed.
19131932 * We wait for stop_lsn in stream mode just in case.
19141933 */
1915- wait_wal_lsn (stop_backup_lsn , false);
1934+ wait_wal_lsn (stop_backup_lsn , false, false );
19161935
19171936 if (stream_wal )
19181937 {
@@ -2606,16 +2625,16 @@ get_last_ptrack_lsn(void)
26062625
26072626{
26082627 PGresult * res ;
2609- uint32 xlogid ;
2610- uint32 xrecoff ;
2628+ uint32 lsn_hi ;
2629+ uint32 lsn_lo ;
26112630 XLogRecPtr lsn ;
26122631
26132632 res = pgut_execute (backup_conn , "select pg_catalog.pg_ptrack_control_lsn()" , 0 , NULL );
26142633
26152634 /* Extract timeline and LSN from results of pg_start_backup() */
2616- XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & xlogid , & xrecoff );
2635+ XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & lsn_hi , & lsn_lo );
26172636 /* Calculate LSN */
2618- lsn = (XLogRecPtr ) (( uint64 ) xlogid << 32 ) | xrecoff ;
2637+ lsn = (( uint64 ) lsn_hi ) << 32 | lsn_lo ;
26192638
26202639 PQclear (res );
26212640 return lsn ;
0 commit comments