Skip to content

Commit 8aa6b84

Browse files
author
Michael Paquier
committed
Fix history file parsing when fetched from archive
History file format has changed from 9.2 to 9.3 to indicate the WAL record when timeline branched off. In 9.2, the complete WAL file name was used while in 9.3 the WAL record is used (like 1/4000090). pg_rman contains a copy of a function of postgres core code to parse the history file that was not anymore compatible, leading to errors related to timelines.
1 parent 71d019c commit 8aa6b84

File tree

3 files changed

+23
-40
lines changed

3 files changed

+23
-40
lines changed

pg_rman.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ extern int pgFileCompareMtimeDesc(const void *f1, const void *f2);
306306

307307
/* in xlog.c */
308308
extern bool xlog_is_complete_wal(const pgFile *file, int server_version);
309-
extern bool xlog_logfname2lsn(const char *logfname, XLogRecPtr *lsn);
310309
extern void xlog_fname(char *fname, size_t len, TimeLineID tli, XLogRecPtr *lsn);
311310

312311
/* in data.c */

restore.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -768,28 +768,41 @@ readTimeLineHistory(TimeLineID targetTLI)
768768
*/
769769
while (fd && fgets(fline, sizeof(fline), fd) != NULL)
770770
{
771-
/* skip leading whitespaces and check for # comment */
772771
char *ptr;
773-
char *endptr;
772+
TimeLineID tli;
773+
uint32 switchpoint_hi;
774+
uint32 switchpoint_lo;
775+
int nfields;
774776

775777
for (ptr = fline; *ptr; ptr++)
776778
{
777-
if (!IsSpace(*ptr))
779+
if (!isspace((unsigned char) *ptr))
778780
break;
779781
}
780782
if (*ptr == '\0' || *ptr == '#')
781783
continue;
782784

785+
/* Parse one entry... */
786+
nfields = sscanf(fline, "%u\t%X/%X", &tli, &switchpoint_hi, &switchpoint_lo);
787+
783788
timeline = pgut_new(pgTimeLine);
784789
timeline->tli = 0;
785790
timeline->end = 0;
786791

787792
/* expect a numeric timeline ID as first field of line */
788-
timeline->tli = (TimeLineID) strtoul(ptr, &endptr, 0);
789-
if (endptr == ptr)
793+
timeline->tli = tli;
794+
795+
if (nfields < 1)
796+
{
797+
/* expect a numeric timeline ID as first field of line */
790798
elog(ERROR_CORRUPTED,
791-
_("syntax error(timeline ID) in history file: %s"),
792-
fline);
799+
_("syntax error in history file: %s. Expected a numeric timeline ID."),
800+
fline);
801+
}
802+
if (nfields != 3)
803+
elog(ERROR_CORRUPTED,
804+
_("syntax error in history file: %s. Expected a transaction log switchpoint location."),
805+
fline);
793806

794807
if (last_timeline && timeline->tli <= last_timeline->tli)
795808
elog(ERROR_CORRUPTED,
@@ -799,20 +812,9 @@ readTimeLineHistory(TimeLineID targetTLI)
799812
parray_insert(result, 0, timeline);
800813
last_timeline = timeline;
801814

802-
/* parse end point(logfname, xid) in the timeline */
803-
for (ptr = endptr; *ptr; ptr++)
804-
{
805-
if (!IsSpace(*ptr))
806-
break;
807-
}
808-
if (*ptr == '\0' || *ptr == '#')
809-
elog(ERROR_CORRUPTED,
810-
_("End logfile must follow Timeline ID."));
811-
812-
if (!xlog_logfname2lsn(ptr, &timeline->end))
813-
elog(ERROR_CORRUPTED,
814-
_("syntax error(endfname) in history file: %s"), fline);
815-
/* we ignore the remainder of each line */
815+
/* Calculate the end lsn finally */
816+
timeline->end = (XLogRecPtr)
817+
((uint64) switchpoint_hi << 32) | switchpoint_lo;
816818
}
817819

818820
if (fd)

xlog.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,6 @@ xlog_is_complete_wal(const pgFile *file, int server_version)
109109
return true;
110110
}
111111

112-
bool
113-
xlog_logfname2lsn(const char *logfname, XLogRecPtr *lsn)
114-
{
115-
uint32 tli;
116-
uint32 xlogid;
117-
uint32 xrecoff;
118-
119-
if (sscanf(logfname, "%08X%08X%08X",
120-
&tli, &xlogid, &xrecoff) != 3)
121-
return false;
122-
123-
xrecoff *= XLogSegSize;
124-
125-
/* Finish calculation of LSN */
126-
*lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
127-
return true;
128-
}
129-
130112
/*
131113
* based on XLogFileName() in xlog_internal.h
132114
*/

0 commit comments

Comments
 (0)