mirror of
https://github.com/postgres/postgres.git
synced 2025-05-02 11:44:50 +03:00
Tolerate timeline switches while "pg_basebackup -X fetch" is running.
If you take a base backup from a standby server with "pg_basebackup -X fetch", and the timeline switches while the backup is being taken, the backup used to fail with an error "requested WAL segment %s has already been removed". This is because the server-side code that sends over the required WAL files would not construct the WAL filename with the correct timeline after a switch. Fix that by using readdir() to scan pg_xlog for all the WAL segments in the range, regardless of timeline. Also, include all timeline history files in the backup, if taken with "-X fetch". That fixes another related bug: If a timeline switch happened just before the backup was initiated in a standby, the WAL segment containing the initial checkpoint record contains WAL from the older timeline too. Recovery will not accept that without a timeline history file that lists the older timeline. Backpatch to 9.2. Versions prior to that were not affected as you could not take a base backup from a standby before 9.2.
This commit is contained in:
parent
faf1b1bd71
commit
b4c99c9af3
@ -3456,19 +3456,36 @@ PreallocXlogFiles(XLogRecPtr endptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get the log/seg of the latest removed or recycled WAL segment.
|
* Throws an error if the given log segment has already been removed or
|
||||||
* Returns 0/0 if no WAL segments have been removed since startup.
|
* recycled. The caller should only pass a segment that it knows to have
|
||||||
|
* existed while the server has been running, as this function always
|
||||||
|
* succeeds if no WAL segments have been removed since startup.
|
||||||
|
* 'tli' is only used in the error message.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
XLogGetLastRemoved(uint32 *log, uint32 *seg)
|
CheckXLogRemoved(uint32 log, uint32 seg, TimeLineID tli)
|
||||||
{
|
{
|
||||||
/* use volatile pointer to prevent code rearrangement */
|
/* use volatile pointer to prevent code rearrangement */
|
||||||
volatile XLogCtlData *xlogctl = XLogCtl;
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
||||||
|
uint32 lastRemovedLog,
|
||||||
|
lastRemovedSeg;
|
||||||
|
|
||||||
SpinLockAcquire(&xlogctl->info_lck);
|
SpinLockAcquire(&xlogctl->info_lck);
|
||||||
*log = xlogctl->lastRemovedLog;
|
lastRemovedLog = xlogctl->lastRemovedLog;
|
||||||
*seg = xlogctl->lastRemovedSeg;
|
lastRemovedSeg = xlogctl->lastRemovedSeg;
|
||||||
SpinLockRelease(&xlogctl->info_lck);
|
SpinLockRelease(&xlogctl->info_lck);
|
||||||
|
|
||||||
|
if (log < lastRemovedLog ||
|
||||||
|
(log == lastRemovedLog && seg <= lastRemovedSeg))
|
||||||
|
{
|
||||||
|
char filename[MAXFNAMELEN];
|
||||||
|
|
||||||
|
XLogFileName(filename, tli, log, seg);
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode_for_file_access(),
|
||||||
|
errmsg("requested WAL segment %s has already been removed",
|
||||||
|
filename)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -55,11 +55,10 @@ static void base_backup_cleanup(int code, Datum arg);
|
|||||||
static void perform_base_backup(basebackup_options *opt, DIR *tblspcdir);
|
static void perform_base_backup(basebackup_options *opt, DIR *tblspcdir);
|
||||||
static void parse_basebackup_options(List *options, basebackup_options *opt);
|
static void parse_basebackup_options(List *options, basebackup_options *opt);
|
||||||
static void SendXlogRecPtrResult(XLogRecPtr ptr);
|
static void SendXlogRecPtrResult(XLogRecPtr ptr);
|
||||||
|
static int compareWalFileNames(const void *a, const void *b);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Size of each block sent into the tar stream for larger files.
|
* Size of each block sent into the tar stream for larger files.
|
||||||
*
|
|
||||||
* XLogSegSize *MUST* be evenly dividable by this
|
|
||||||
*/
|
*/
|
||||||
#define TAR_SEND_SIZE 32768
|
#define TAR_SEND_SIZE 32768
|
||||||
|
|
||||||
@ -221,68 +220,208 @@ perform_base_backup(basebackup_options *opt, DIR *tblspcdir)
|
|||||||
* We've left the last tar file "open", so we can now append the
|
* We've left the last tar file "open", so we can now append the
|
||||||
* required WAL files to it.
|
* required WAL files to it.
|
||||||
*/
|
*/
|
||||||
|
char pathbuf[MAXPGPATH];
|
||||||
uint32 logid,
|
uint32 logid,
|
||||||
logseg;
|
logseg;
|
||||||
|
uint32 startlogid,
|
||||||
|
startlogseg;
|
||||||
uint32 endlogid,
|
uint32 endlogid,
|
||||||
endlogseg;
|
endlogseg;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
|
List *historyFileList = NIL;
|
||||||
|
List *walFileList = NIL;
|
||||||
|
char **walFiles;
|
||||||
|
int nWalFiles;
|
||||||
|
char firstoff[MAXFNAMELEN];
|
||||||
|
char lastoff[MAXFNAMELEN];
|
||||||
|
DIR *dir;
|
||||||
|
struct dirent *de;
|
||||||
|
int i;
|
||||||
|
ListCell *lc;
|
||||||
|
TimeLineID tli;
|
||||||
|
|
||||||
MemSet(&statbuf, 0, sizeof(statbuf));
|
/*
|
||||||
statbuf.st_mode = S_IRUSR | S_IWUSR;
|
* I'd rather not worry about timelines here, so scan pg_xlog and
|
||||||
#ifndef WIN32
|
* include all WAL files in the range between 'startptr' and 'endptr',
|
||||||
statbuf.st_uid = geteuid();
|
* regardless of the timeline the file is stamped with. If there are
|
||||||
statbuf.st_gid = getegid();
|
* some spurious WAL files belonging to timelines that don't belong
|
||||||
#endif
|
* in this server's history, they will be included too. Normally there
|
||||||
statbuf.st_size = XLogSegSize;
|
* shouldn't be such files, but if there are, there's little harm in
|
||||||
statbuf.st_mtime = time(NULL);
|
* including them.
|
||||||
|
*/
|
||||||
XLByteToSeg(startptr, logid, logseg);
|
XLByteToSeg(startptr, startlogid, startlogseg);
|
||||||
|
XLogFileName(firstoff, ThisTimeLineID, startlogid, startlogseg);
|
||||||
XLByteToPrevSeg(endptr, endlogid, endlogseg);
|
XLByteToPrevSeg(endptr, endlogid, endlogseg);
|
||||||
|
XLogFileName(lastoff, ThisTimeLineID, endlogid, endlogseg);
|
||||||
|
|
||||||
while (true)
|
dir = AllocateDir("pg_xlog");
|
||||||
|
if (!dir)
|
||||||
|
ereport(ERROR,
|
||||||
|
(errmsg("could not open directory \"%s\": %m", "pg_xlog")));
|
||||||
|
while ((de = ReadDir(dir, "pg_xlog")) != NULL)
|
||||||
{
|
{
|
||||||
/* Send another xlog segment */
|
/* Does it look like a WAL segment, and is it in the range? */
|
||||||
char fn[MAXPGPATH];
|
if (strlen(de->d_name) == 24 &&
|
||||||
int i;
|
strspn(de->d_name, "0123456789ABCDEF") == 24 &&
|
||||||
|
strcmp(de->d_name + 8, firstoff + 8) >= 0 &&
|
||||||
XLogFilePath(fn, ThisTimeLineID, logid, logseg);
|
strcmp(de->d_name + 8, lastoff + 8) <= 0)
|
||||||
_tarWriteHeader(fn, NULL, &statbuf);
|
|
||||||
|
|
||||||
/* Send the actual WAL file contents, block-by-block */
|
|
||||||
for (i = 0; i < XLogSegSize / TAR_SEND_SIZE; i++)
|
|
||||||
{
|
{
|
||||||
char buf[TAR_SEND_SIZE];
|
walFileList = lappend(walFileList, pstrdup(de->d_name));
|
||||||
XLogRecPtr ptr;
|
}
|
||||||
|
/* Does it look like a timeline history file? */
|
||||||
|
else if (strlen(de->d_name) == 8 + strlen(".history") &&
|
||||||
|
strspn(de->d_name, "0123456789ABCDEF") == 8 &&
|
||||||
|
strcmp(de->d_name + 8, ".history") == 0)
|
||||||
|
{
|
||||||
|
historyFileList = lappend(historyFileList, pstrdup(de->d_name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FreeDir(dir);
|
||||||
|
|
||||||
ptr.xlogid = logid;
|
/*
|
||||||
ptr.xrecoff = logseg * XLogSegSize + TAR_SEND_SIZE * i;
|
* Before we go any further, check that none of the WAL segments we
|
||||||
|
* need were removed.
|
||||||
|
*/
|
||||||
|
CheckXLogRemoved(startlogid, startlogseg, ThisTimeLineID);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Put the WAL filenames into an array, and sort. We send the files
|
||||||
|
* in order from oldest to newest, to reduce the chance that a file
|
||||||
|
* is recycled before we get a chance to send it over.
|
||||||
|
*/
|
||||||
|
nWalFiles = list_length(walFileList);
|
||||||
|
walFiles = palloc(nWalFiles * sizeof(char *));
|
||||||
|
i = 0;
|
||||||
|
foreach(lc, walFileList)
|
||||||
|
{
|
||||||
|
walFiles[i++] = lfirst(lc);
|
||||||
|
}
|
||||||
|
qsort(walFiles, nWalFiles, sizeof(char *), compareWalFileNames);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sanity check: the first and last segment should cover startptr and
|
||||||
|
* endptr, with no gaps in between.
|
||||||
|
*/
|
||||||
|
XLogFromFileName(walFiles[0], &tli, &logid, &logseg);
|
||||||
|
if (logid != startlogid || logseg != startlogseg)
|
||||||
|
{
|
||||||
|
char startfname[MAXFNAMELEN];
|
||||||
|
XLogFileName(startfname, ThisTimeLineID, startlogid, startlogseg);
|
||||||
|
ereport(ERROR,
|
||||||
|
(errmsg("could not find WAL file %s", startfname)));
|
||||||
|
}
|
||||||
|
for (i = 0; i < nWalFiles; i++)
|
||||||
|
{
|
||||||
|
int currlogid = logid,
|
||||||
|
currlogseg = logseg;
|
||||||
|
int nextlogid = logid,
|
||||||
|
nextlogseg = logseg;
|
||||||
|
NextLogSeg(nextlogid, nextlogseg);
|
||||||
|
|
||||||
|
XLogFromFileName(walFiles[i], &tli, &logid, &logseg);
|
||||||
|
if (!((nextlogid == logid && nextlogseg == logseg) ||
|
||||||
|
(currlogid == logid && currlogseg == logseg)))
|
||||||
|
{
|
||||||
|
char nextfname[MAXFNAMELEN];
|
||||||
|
XLogFileName(nextfname, ThisTimeLineID, nextlogid, nextlogseg);
|
||||||
|
ereport(ERROR,
|
||||||
|
(errmsg("could not find WAL file %s", nextfname)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (logid != endlogid || logseg != endlogseg)
|
||||||
|
{
|
||||||
|
char endfname[MAXFNAMELEN];
|
||||||
|
XLogFileName(endfname, ThisTimeLineID, endlogid, endlogseg);
|
||||||
|
ereport(ERROR,
|
||||||
|
(errmsg("could not find WAL file %s", endfname)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Ok, we have everything we need. Send the WAL files. */
|
||||||
|
for (i = 0; i < nWalFiles; i++)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
char buf[TAR_SEND_SIZE];
|
||||||
|
size_t cnt;
|
||||||
|
pgoff_t len = 0;
|
||||||
|
|
||||||
|
snprintf(pathbuf, MAXPGPATH, XLOGDIR "/%s", walFiles[i]);
|
||||||
|
XLogFromFileName(walFiles[i], &tli, &logid, &logseg);
|
||||||
|
|
||||||
|
fp = AllocateFile(pathbuf, "rb");
|
||||||
|
if (fp == NULL)
|
||||||
|
{
|
||||||
/*
|
/*
|
||||||
* Some old compilers, e.g. gcc 2.95.3/x86, think that passing
|
* Most likely reason for this is that the file was already
|
||||||
* a struct in the same function as a longjump might clobber a
|
* removed by a checkpoint, so check for that to get a better
|
||||||
* variable. bjm 2011-02-04
|
* error message.
|
||||||
* http://lists.apple.com/archives/xcode-users/2003/Dec//msg000
|
|
||||||
* 51.html
|
|
||||||
*/
|
*/
|
||||||
XLogRead(buf, ptr, TAR_SEND_SIZE);
|
CheckXLogRemoved(logid, logseg, tli);
|
||||||
if (pq_putmessage('d', buf, TAR_SEND_SIZE))
|
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errmsg("base backup could not send data, aborting backup")));
|
(errcode_for_file_access(),
|
||||||
|
errmsg("could not open file \"%s\": %m", pathbuf)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (fstat(fileno(fp), &statbuf) != 0)
|
||||||
* Files are always fixed size, and always end on a 512 byte
|
ereport(ERROR,
|
||||||
* boundary, so padding is never necessary.
|
(errcode_for_file_access(),
|
||||||
*/
|
errmsg("could not stat file \"%s\": %m",
|
||||||
|
pathbuf)));
|
||||||
|
if (statbuf.st_size != XLogSegSize)
|
||||||
|
{
|
||||||
|
CheckXLogRemoved(logid, logseg, tli);
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode_for_file_access(),
|
||||||
|
errmsg("unexpected WAL file size \"%s\"", walFiles[i])));
|
||||||
|
}
|
||||||
|
|
||||||
|
_tarWriteHeader(pathbuf, NULL, &statbuf);
|
||||||
|
|
||||||
/* Advance to the next WAL file */
|
while ((cnt = fread(buf, 1, Min(sizeof(buf), XLogSegSize - len), fp)) > 0)
|
||||||
NextLogSeg(logid, logseg);
|
{
|
||||||
|
CheckXLogRemoved(logid, logseg, tli);
|
||||||
|
/* Send the chunk as a CopyData message */
|
||||||
|
if (pq_putmessage('d', buf, cnt))
|
||||||
|
ereport(ERROR,
|
||||||
|
(errmsg("base backup could not send data, aborting backup")));
|
||||||
|
|
||||||
/* Have we reached our stop position yet? */
|
len += cnt;
|
||||||
if (logid > endlogid ||
|
if (len == XLogSegSize)
|
||||||
(logid == endlogid && logseg > endlogseg))
|
break;
|
||||||
break;
|
}
|
||||||
|
|
||||||
|
if (len != XLogSegSize)
|
||||||
|
{
|
||||||
|
CheckXLogRemoved(logid, logseg, tli);
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode_for_file_access(),
|
||||||
|
errmsg("unexpected WAL file size \"%s\"", walFiles[i])));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* XLogSegSize is a multiple of 512, so no need for padding */
|
||||||
|
FreeFile(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Send timeline history files too. Only the latest timeline history
|
||||||
|
* file is required for recovery, and even that only if there happens
|
||||||
|
* to be a timeline switch in the first WAL segment that contains the
|
||||||
|
* checkpoint record, or if we're taking a base backup from a standby
|
||||||
|
* server and the target timeline changes while the backup is taken.
|
||||||
|
* But they are small and highly useful for debugging purposes, so
|
||||||
|
* better include them all, always.
|
||||||
|
*/
|
||||||
|
foreach(lc, historyFileList)
|
||||||
|
{
|
||||||
|
char *fname = lfirst(lc);
|
||||||
|
snprintf(pathbuf, MAXPGPATH, XLOGDIR "/%s", fname);
|
||||||
|
|
||||||
|
if (lstat(pathbuf, &statbuf) != 0)
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode_for_file_access(),
|
||||||
|
errmsg("could not stat file \"%s\": %m", pathbuf)));
|
||||||
|
|
||||||
|
sendFile(pathbuf, pathbuf, &statbuf, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send CopyDone message for the last tar file */
|
/* Send CopyDone message for the last tar file */
|
||||||
@ -291,6 +430,19 @@ perform_base_backup(basebackup_options *opt, DIR *tblspcdir)
|
|||||||
SendXlogRecPtrResult(endptr);
|
SendXlogRecPtrResult(endptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* qsort comparison function, to compare log/seg portion of WAL segment
|
||||||
|
* filenames, ignoring the timeline portion.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
compareWalFileNames(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
char *fna = *((char **) a);
|
||||||
|
char *fnb = *((char **) b);
|
||||||
|
|
||||||
|
return strcmp(fna + 8, fnb + 8);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse the base backup options passed down by the parser
|
* Parse the base backup options passed down by the parser
|
||||||
*/
|
*/
|
||||||
|
@ -977,8 +977,6 @@ XLogRead(char *buf, XLogRecPtr startptr, Size count)
|
|||||||
char *p;
|
char *p;
|
||||||
XLogRecPtr recptr;
|
XLogRecPtr recptr;
|
||||||
Size nbytes;
|
Size nbytes;
|
||||||
uint32 lastRemovedLog;
|
|
||||||
uint32 lastRemovedSeg;
|
|
||||||
uint32 log;
|
uint32 log;
|
||||||
uint32 seg;
|
uint32 seg;
|
||||||
|
|
||||||
@ -1073,19 +1071,8 @@ retry:
|
|||||||
* read() succeeds in that case, but the data we tried to read might
|
* read() succeeds in that case, but the data we tried to read might
|
||||||
* already have been overwritten with new WAL records.
|
* already have been overwritten with new WAL records.
|
||||||
*/
|
*/
|
||||||
XLogGetLastRemoved(&lastRemovedLog, &lastRemovedSeg);
|
|
||||||
XLByteToSeg(startptr, log, seg);
|
XLByteToSeg(startptr, log, seg);
|
||||||
if (log < lastRemovedLog ||
|
CheckXLogRemoved(log, seg, ThisTimeLineID);
|
||||||
(log == lastRemovedLog && seg <= lastRemovedSeg))
|
|
||||||
{
|
|
||||||
char filename[MAXFNAMELEN];
|
|
||||||
|
|
||||||
XLogFileName(filename, ThisTimeLineID, log, seg);
|
|
||||||
ereport(ERROR,
|
|
||||||
(errcode_for_file_access(),
|
|
||||||
errmsg("requested WAL segment %s has already been removed",
|
|
||||||
filename)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* During recovery, the currently-open WAL file might be replaced with the
|
* During recovery, the currently-open WAL file might be replaced with the
|
||||||
|
@ -275,7 +275,7 @@ extern int XLogFileInit(uint32 log, uint32 seg,
|
|||||||
extern int XLogFileOpen(uint32 log, uint32 seg);
|
extern int XLogFileOpen(uint32 log, uint32 seg);
|
||||||
|
|
||||||
|
|
||||||
extern void XLogGetLastRemoved(uint32 *log, uint32 *seg);
|
extern void CheckXLogRemoved(uint32 log, uint32 seg, TimeLineID tli);
|
||||||
extern void XLogSetAsyncXactLSN(XLogRecPtr record);
|
extern void XLogSetAsyncXactLSN(XLogRecPtr record);
|
||||||
|
|
||||||
extern Buffer RestoreBackupBlock(XLogRecPtr lsn, XLogRecord *record,
|
extern Buffer RestoreBackupBlock(XLogRecPtr lsn, XLogRecord *record,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user