Skip to content

Commit a8b5e54

Browse files
committed
Bug fix: Get restore_lsn from pg_create_restore_point result
1 parent b4e0eae commit a8b5e54

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/backup.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,8 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
10711071
{
10721072
PGresult *res;
10731073
const char *params[2];
1074-
uint32 xlogid;
1075-
uint32 xrecoff;
1074+
uint32 lsn_hi;
1075+
uint32 lsn_lo;
10761076
PGconn *conn;
10771077

10781078
params[0] = label;
@@ -1100,9 +1100,9 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
11001100
backup_in_progress = true;
11011101

11021102
/* Extract timeline and LSN from results of pg_start_backup() */
1103-
XLogDataFromLSN(PQgetvalue(res, 0, 0), &xlogid, &xrecoff);
1103+
XLogDataFromLSN(PQgetvalue(res, 0, 0), &lsn_hi, &lsn_lo);
11041104
/* Calculate LSN */
1105-
backup->start_lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
1105+
backup->start_lsn = ((uint64) lsn_hi )<< 32 | lsn_lo;
11061106

11071107
PQclear(res);
11081108

@@ -1585,8 +1585,8 @@ wait_replica_wal_lsn(XLogRecPtr lsn, bool is_start_backup)
15851585
while (true)
15861586
{
15871587
PGresult *res;
1588-
uint32 xlogid;
1589-
uint32 xrecoff;
1588+
uint32 lsn_hi;
1589+
uint32 lsn_lo;
15901590
XLogRecPtr replica_lsn;
15911591

15921592
/*
@@ -1617,9 +1617,9 @@ wait_replica_wal_lsn(XLogRecPtr lsn, bool is_start_backup)
16171617
}
16181618

16191619
/* Extract timeline and LSN from result */
1620-
XLogDataFromLSN(PQgetvalue(res, 0, 0), &xlogid, &xrecoff);
1620+
XLogDataFromLSN(PQgetvalue(res, 0, 0), &lsn_hi, &lsn_lo);
16211621
/* Calculate LSN */
1622-
replica_lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
1622+
replica_lsn = ((uint64) lsn_hi) << 32 | lsn_lo;
16231623
PQclear(res);
16241624

16251625
/* target lsn was replicated */
@@ -1653,10 +1653,10 @@ pg_stop_backup(pgBackup *backup)
16531653
PGconn *conn;
16541654
PGresult *res;
16551655
PGresult *tablespace_map_content = NULL;
1656-
uint32 xlogid;
1657-
uint32 xrecoff;
1656+
uint32 lsn_hi;
1657+
uint32 lsn_lo;
16581658
XLogRecPtr restore_lsn = InvalidXLogRecPtr;
1659-
int pg_stop_backup_timeout = 0;
1659+
int pg_stop_backup_timeout = 0;
16601660
char path[MAXPGPATH];
16611661
char backup_label[MAXPGPATH];
16621662
FILE *fp;
@@ -1699,6 +1699,10 @@ pg_stop_backup(pgBackup *backup)
16991699

17001700
res = pgut_execute(conn, "SELECT pg_catalog.pg_create_restore_point($1)",
17011701
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;
17021706
PQclear(res);
17031707
}
17041708

@@ -1731,7 +1735,6 @@ pg_stop_backup(pgBackup *backup)
17311735
}
17321736
else
17331737
{
1734-
17351738
stop_backup_query = "SELECT"
17361739
" pg_catalog.txid_snapshot_xmax(pg_catalog.txid_current_snapshot()),"
17371740
" current_timestamp(0)::timestamptz,"
@@ -1750,6 +1753,8 @@ pg_stop_backup(pgBackup *backup)
17501753
*/
17511754
if (pg_stop_backup_is_sent && !in_cleanup)
17521755
{
1756+
res = NULL;
1757+
17531758
while (1)
17541759
{
17551760
if (!PQconsumeInput(conn) || PQisBusy(conn))
@@ -1791,8 +1796,11 @@ pg_stop_backup(pgBackup *backup)
17911796
{
17921797
switch (PQresultStatus(res))
17931798
{
1799+
/*
1800+
* We should expect only PGRES_TUPLES_OK since pg_stop_backup
1801+
* returns tuples.
1802+
*/
17941803
case PGRES_TUPLES_OK:
1795-
case PGRES_COMMAND_OK:
17961804
break;
17971805
default:
17981806
elog(ERROR, "query failed: %s query was: %s",
@@ -1804,9 +1812,9 @@ pg_stop_backup(pgBackup *backup)
18041812
backup_in_progress = false;
18051813

18061814
/* Extract timeline and LSN from results of pg_stop_backup() */
1807-
XLogDataFromLSN(PQgetvalue(res, 0, 2), &xlogid, &xrecoff);
1815+
XLogDataFromLSN(PQgetvalue(res, 0, 2), &lsn_hi, &lsn_lo);
18081816
/* Calculate LSN */
1809-
stop_backup_lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
1817+
stop_backup_lsn = ((uint64) lsn_hi) << 32 | lsn_lo;
18101818

18111819
if (!XRecOffIsValid(stop_backup_lsn))
18121820
{
@@ -2617,16 +2625,16 @@ get_last_ptrack_lsn(void)
26172625

26182626
{
26192627
PGresult *res;
2620-
uint32 xlogid;
2621-
uint32 xrecoff;
2628+
uint32 lsn_hi;
2629+
uint32 lsn_lo;
26222630
XLogRecPtr lsn;
26232631

26242632
res = pgut_execute(backup_conn, "select pg_catalog.pg_ptrack_control_lsn()", 0, NULL);
26252633

26262634
/* Extract timeline and LSN from results of pg_start_backup() */
2627-
XLogDataFromLSN(PQgetvalue(res, 0, 0), &xlogid, &xrecoff);
2635+
XLogDataFromLSN(PQgetvalue(res, 0, 0), &lsn_hi, &lsn_lo);
26282636
/* Calculate LSN */
2629-
lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
2637+
lsn = ((uint64) lsn_hi) << 32 | lsn_lo;
26302638

26312639
PQclear(res);
26322640
return lsn;

0 commit comments

Comments
 (0)