Skip to content

Commit 2bdaaf0

Browse files
committed
Add pg_switch_wal(). It is used for page backup
1 parent e9e25d9 commit 2bdaaf0

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

backup.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static void backup_files(void *arg);
5959
static void do_backup_database(parray *backup_list);
6060

6161
static void pg_start_backup(const char *label, bool smooth, pgBackup *backup);
62+
static void pg_switch_wal(void);
6263
static void pg_stop_backup(pgBackup *backup);
6364

6465
static void add_pgdata_files(parray *files, const char *root);
@@ -214,6 +215,16 @@ do_backup_database(parray *backup_list)
214215
*/
215216
if (current.backup_mode == BACKUP_MODE_DIFF_PAGE)
216217
{
218+
/*
219+
* Switch to a new WAL segment. It is necessary to get archived WAL
220+
* segment, which includes start LSN of current backup.
221+
*
222+
* Do not switch for standby node and if backup is stream.
223+
*/
224+
if (!from_replica && !stream_wal)
225+
pg_switch_wal();
226+
if (!stream_wal)
227+
wait_archive_lsn(current.start_lsn, false);
217228
/*
218229
* Build the page map. Obtain information about changed pages
219230
* reading WAL segments present in archives up to the point
@@ -581,14 +592,29 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
581592
/* Calculate LSN */
582593
backup->start_lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
583594

584-
if (!stream_wal)
585-
wait_archive_lsn(backup->start_lsn,
586-
/*
587-
* For backup from master wait for previous segment.
588-
* For backup from replica wait for current segment.
589-
*/
590-
!from_replica);
595+
PQclear(res);
596+
}
597+
598+
/*
599+
* Switch to a new WAL segment. It should be called only for master.
600+
*/
601+
static void
602+
pg_switch_wal(void)
603+
{
604+
PGresult *res;
605+
606+
/* Remove annoying NOTICE messages generated by backend */
607+
res = pgut_execute(backup_conn, "SET client_min_messages = warning;", 0,
608+
NULL);
609+
PQclear(res);
591610

611+
#if PG_VERSION_NUM >= 100000
612+
res = pgut_execute(backup_conn, "SELECT * FROM pg_switch_wal()", 0,
613+
NULL);
614+
#else
615+
res = pgut_execute(backup_conn, "SELECT * FROM pg_switch_xlog()", 0,
616+
NULL);
617+
#endif
592618
PQclear(res);
593619
}
594620

0 commit comments

Comments
 (0)