Skip to content

Commit 82ca5a0

Browse files
committed
Wait for stop_lsn.
In stream mode we wait it in backuped pg_xlog directory now.
1 parent 635a4a5 commit 82ca5a0

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

backup.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void pg_stop_backup(pgBackup *backup);
6464

6565
static void add_pgdata_files(parray *files, const char *root);
6666
static void write_backup_file_list(parray *files, const char *root);
67-
static void wait_archive_lsn(XLogRecPtr lsn, bool prev_segno);
67+
static void wait_wal_lsn(XLogRecPtr lsn);
6868
static void make_pagemap_from_ptrack(parray *files);
6969
static void StreamLog(void *arg);
7070

@@ -598,7 +598,12 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
598598
if (!from_replica && !stream_wal)
599599
pg_switch_wal();
600600
if (!stream_wal)
601-
wait_archive_lsn(backup->start_lsn, false);
601+
/*
602+
* Do not wait start_lsn for stream backup.
603+
* Because WAL streaming will start after pg_start_backup() in stream
604+
* mode.
605+
*/
606+
wait_wal_lsn(backup->start_lsn);
602607
}
603608

604609
/*
@@ -784,28 +789,32 @@ pg_ptrack_get_and_clear(Oid tablespace_oid, Oid db_oid, Oid rel_oid,
784789
* WAL segment file.
785790
*/
786791
static void
787-
wait_archive_lsn(XLogRecPtr lsn, bool prev_segno)
792+
wait_wal_lsn(XLogRecPtr lsn)
788793
{
789794
TimeLineID tli;
790795
XLogSegNo targetSegNo;
791-
char wal_path[MAXPGPATH];
792-
char wal_file[MAXFNAMELEN];
796+
char wal_dir[MAXPGPATH],
797+
wal_segment_full_path[MAXPGPATH];
798+
char wal_segment[MAXFNAMELEN];
793799
uint32 try_count = 0;
794800

795-
Assert(!stream_wal);
796-
797801
tli = get_current_timeline(false);
798802

799803
/* Compute the name of the WAL file containig requested LSN */
800804
XLByteToSeg(lsn, targetSegNo);
801-
if (prev_segno)
802-
targetSegNo--;
803-
XLogFileName(wal_file, tli, targetSegNo);
805+
XLogFileName(wal_segment, tli, targetSegNo);
804806

805-
join_path_components(wal_path, arclog_path, wal_file);
807+
if (stream_wal)
808+
{
809+
pgBackupGetPath2(&current, wal_dir, lengthof(wal_dir),
810+
DATABASE_DIR, PG_XLOG_DIR);
811+
join_path_components(wal_segment_full_path, wal_dir, wal_segment);
812+
}
813+
else
814+
join_path_components(wal_segment_full_path, arclog_path, wal_segment);
806815

807816
/* Wait until switched WAL is archived */
808-
while (!fileExists(wal_path))
817+
while (!fileExists(wal_segment_full_path))
809818
{
810819
sleep(1);
811820
if (interrupted)
@@ -815,21 +824,21 @@ wait_archive_lsn(XLogRecPtr lsn, bool prev_segno)
815824
/* Inform user if WAL segment is absent in first attempt */
816825
if (try_count == 1)
817826
elog(INFO, "wait for LSN %X/%X in archived WAL segment %s",
818-
(uint32) (lsn >> 32), (uint32) lsn, wal_path);
827+
(uint32) (lsn >> 32), (uint32) lsn, wal_segment_full_path);
819828

820829
if (archive_timeout > 0 && try_count > archive_timeout)
821830
elog(ERROR,
822831
"switched WAL segment %s could not be archived in %d seconds",
823-
wal_file, archive_timeout);
832+
wal_segment, archive_timeout);
824833
}
825834

826835
/*
827-
* WAL segment was archived. Check LSN on it if we waited current WAL
828-
* segment, not previous.
836+
* WAL segment was archived. Check LSN on it.
829837
*/
830-
if (!prev_segno && !wal_contains_lsn(arclog_path, lsn, tli))
838+
if ((stream_wal && !wal_contains_lsn(wal_dir, lsn, tli)) ||
839+
(!stream_wal && !wal_contains_lsn(arclog_path, lsn, tli)))
831840
elog(ERROR, "WAL segment %s doesn't contain target LSN %X/%X",
832-
wal_file, (uint32) (lsn >> 32), (uint32) lsn);
841+
wal_segment, (uint32) (lsn >> 32), (uint32) lsn);
833842
}
834843

835844
/*
@@ -950,8 +959,7 @@ pg_stop_backup(pgBackup *backup)
950959

951960
PQclear(res);
952961

953-
if (!stream_wal)
954-
wait_archive_lsn(stop_backup_lsn, false);
962+
wait_wal_lsn(stop_backup_lsn);
955963

956964
/* Fill in fields if that is the correct end of backup. */
957965
if (backup != NULL)
@@ -961,7 +969,9 @@ pg_stop_backup(pgBackup *backup)
961969

962970
if (stream_wal)
963971
{
964-
join_path_components(stream_xlog_path, pgdata, PG_XLOG_DIR);
972+
pgBackupGetPath2(backup, stream_xlog_path,
973+
lengthof(stream_xlog_path),
974+
DATABASE_DIR, PG_XLOG_DIR);
965975
xlog_path = stream_xlog_path;
966976
}
967977
else

parsexlog.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ read_recovery_info(const char *archivedir, TimeLineID tli,
488488
}
489489

490490
/*
491-
* Check if WAL segment file 'wal_path' contains 'target_lsn'.
491+
* Check if there is a WAL segment file in 'archivedir' which contains
492+
* 'target_lsn'.
492493
*/
493494
bool
494495
wal_contains_lsn(const char *archivedir, XLogRecPtr target_lsn,

0 commit comments

Comments
 (0)