Skip to content

Commit 517939d

Browse files
committed
PGPRO-2040: Use checkpoint LSN to retreive start_lsn for a replica backup
1 parent ad5cda6 commit 517939d

File tree

3 files changed

+62
-28
lines changed

3 files changed

+62
-28
lines changed

src/backup.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,10 +1142,11 @@ pg_switch_wal(PGconn *conn)
11421142
res = pgut_execute(conn, "SET client_min_messages = warning;", 0, NULL);
11431143
PQclear(res);
11441144

1145-
if (server_version >= 100000)
1146-
res = pgut_execute(conn, "SELECT * FROM pg_catalog.pg_switch_wal()", 0, NULL);
1147-
else
1148-
res = pgut_execute(conn, "SELECT * FROM pg_catalog.pg_switch_xlog()", 0, NULL);
1145+
#if PG_VERSION_NUM >= 100000
1146+
res = pgut_execute(conn, "SELECT * FROM pg_catalog.pg_switch_wal()", 0, NULL);
1147+
#else
1148+
res = pgut_execute(conn, "SELECT * FROM pg_catalog.pg_switch_xlog()", 0, NULL);
1149+
#endif
11491150

11501151
PQclear(res);
11511152
}
@@ -1584,9 +1585,6 @@ wait_replica_wal_lsn(XLogRecPtr lsn, bool is_start_backup)
15841585

15851586
while (true)
15861587
{
1587-
PGresult *res;
1588-
uint32 lsn_hi;
1589-
uint32 lsn_lo;
15901588
XLogRecPtr replica_lsn;
15911589

15921590
/*
@@ -1595,32 +1593,32 @@ wait_replica_wal_lsn(XLogRecPtr lsn, bool is_start_backup)
15951593
*/
15961594
if (is_start_backup)
15971595
{
1598-
if (server_version >= 100000)
1599-
res = pgut_execute(backup_conn, "SELECT pg_catalog.pg_last_wal_replay_lsn()",
1600-
0, NULL);
1601-
else
1602-
res = pgut_execute(backup_conn, "SELECT pg_catalog.pg_last_xlog_replay_location()",
1603-
0, NULL);
1596+
replica_lsn = get_checkpoint_location(backup_conn);
16041597
}
16051598
/*
16061599
* For lsn from pg_stop_backup() we need it only to be received by
16071600
* replica and fsync()'ed on WAL segment.
16081601
*/
16091602
else
16101603
{
1611-
if (server_version >= 100000)
1612-
res = pgut_execute(backup_conn, "SELECT pg_catalog.pg_last_wal_receive_lsn()",
1613-
0, NULL);
1614-
else
1615-
res = pgut_execute(backup_conn, "SELECT pg_catalog.pg_last_xlog_receive_location()",
1616-
0, NULL);
1617-
}
1604+
PGresult *res;
1605+
uint32 lsn_hi;
1606+
uint32 lsn_lo;
16181607

1619-
/* Extract timeline and LSN from result */
1620-
XLogDataFromLSN(PQgetvalue(res, 0, 0), &lsn_hi, &lsn_lo);
1621-
/* Calculate LSN */
1622-
replica_lsn = ((uint64) lsn_hi) << 32 | lsn_lo;
1623-
PQclear(res);
1608+
#if PG_VERSION_NUM >= 100000
1609+
res = pgut_execute(backup_conn, "SELECT pg_catalog.pg_last_wal_receive_lsn()",
1610+
0, NULL);
1611+
#else
1612+
res = pgut_execute(backup_conn, "SELECT pg_catalog.pg_last_xlog_receive_location()",
1613+
0, NULL);
1614+
#endif
1615+
1616+
/* Extract LSN from result */
1617+
XLogDataFromLSN(PQgetvalue(res, 0, 0), &lsn_hi, &lsn_lo);
1618+
/* Calculate LSN */
1619+
replica_lsn = ((uint64) lsn_hi) << 32 | lsn_lo;
1620+
PQclear(res);
1621+
}
16241622

16251623
/* target lsn was replicated */
16261624
if (replica_lsn >= lsn)

src/pg_probackup.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,17 +560,19 @@ extern bool wal_contains_lsn(const char *archivedir, XLogRecPtr target_lsn,
560560

561561
/* in util.c */
562562
extern TimeLineID get_current_timeline(bool safe);
563+
extern XLogRecPtr get_checkpoint_location(PGconn *conn);
564+
extern uint64 get_system_identifier(char *pgdata);
565+
extern uint64 get_remote_system_identifier(PGconn *conn);
566+
extern uint32 get_data_checksum_version(bool safe);
567+
563568
extern void sanityChecks(void);
564569
extern void time2iso(char *buf, size_t len, time_t time);
565570
extern const char *status2str(BackupStatus status);
566571
extern void remove_trailing_space(char *buf, int comment_mark);
567572
extern void remove_not_digit(char *buf, size_t len, const char *str);
568-
extern uint32 get_data_checksum_version(bool safe);
569573
extern const char *base36enc(long unsigned int value);
570574
extern char *base36enc_dup(long unsigned int value);
571575
extern long unsigned int base36dec(const char *text);
572-
extern uint64 get_system_identifier(char *pgdata);
573-
extern uint64 get_remote_system_identifier(PGconn *conn);
574576
extern pg_time_t timestamptz_to_time_t(TimestampTz t);
575577
extern int parse_server_version(char *server_version_str);
576578

src/util.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,40 @@ get_current_timeline(bool safe)
122122
return ControlFile.checkPointCopy.ThisTimeLineID;
123123
}
124124

125+
/*
126+
* Get last check point record ptr from pg_tonrol.
127+
*/
128+
XLogRecPtr
129+
get_checkpoint_location(PGconn *conn)
130+
{
131+
#if PG_VERSION_NUM >= 90600
132+
PGresult *res;
133+
uint32 lsn_hi;
134+
uint32 lsn_lo;
135+
XLogRecPtr lsn;
136+
137+
res = pgut_execute(conn,
138+
"SELECT checkpoint_location FROM pg_catalog.pg_control_checkpoint()",
139+
0, NULL);
140+
XLogDataFromLSN(PQgetvalue(res, 0, 0), &lsn_hi, &lsn_lo);
141+
PQclear(res);
142+
/* Calculate LSN */
143+
lsn = ((uint64) lsn_hi) << 32 | lsn_lo;
144+
145+
return lsn;
146+
#else
147+
char *buffer;
148+
size_t size;
149+
ControlFileData ControlFile;
150+
151+
buffer = fetchFile(conn, "global/pg_control", &size);
152+
digestControlFile(&ControlFile, buffer, size);
153+
pg_free(buffer);
154+
155+
return ControlFile.checkPoint;
156+
#endif
157+
}
158+
125159
uint64
126160
get_system_identifier(char *pgdata_path)
127161
{

0 commit comments

Comments
 (0)