Skip to content

Commit

Permalink
Make XLogFileCopy() look the same as in 9.4.
Browse files Browse the repository at this point in the history
XLogFileCopy() was changed heavily in commit de76884. However it was
partially reverted in commit 7abc685 and most of those changes to
XLogFileCopy() were no longer needed. Then commit 7cbee7c removed
those unnecessary code, but XLogFileCopy() looked different in master
and 9.4 though the contents are almost the same.

This patch makes XLogFileCopy() look the same in master and back-branches,
which makes back-patching easier, per discussion on pgsql-hackers.
Back-patch to 9.5.

Discussion: 55760844.7090703@iki.fi

Michael Paquier
  • Loading branch information
MasaoFujii committed Jul 1, 2015
1 parent 7f32dbc commit 8217370
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions src/backend/access/transam/xlog.c
Expand Up @@ -808,7 +808,7 @@ static bool XLogCheckpointNeeded(XLogSegNo new_segno);
static void XLogWrite(XLogwrtRqst WriteRqst, bool flexible);
static bool InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
bool find_free, XLogSegNo max_segno,
bool use_lock, int elevel);
bool use_lock);
static int XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
int source, bool notexistOk);
static int XLogFileReadAnyTLI(XLogSegNo segno, int emode, int source);
Expand Down Expand Up @@ -3013,7 +3013,7 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
max_segno = logsegno + CheckPointSegments;
if (!InstallXLogFileSegment(&installed_segno, tmppath,
*use_existent, max_segno,
use_lock, LOG))
use_lock))
{
/*
* No need for any more future segments, or InstallXLogFileSegment()
Expand All @@ -3040,20 +3040,25 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
}

/*
* Copy a WAL segment file in pg_xlog directory.
* Create a new XLOG file segment by copying a pre-existing one.
*
* srcfname source filename
* upto how much of the source file to copy? (the rest is filled with
* zeros)
* segno identify segment to install.
* destsegno: identify segment to be created.
*
* The file is first copied with a temporary filename, and then installed as
* a newly-created segment.
* srcTLI, srclog, srcseg: identify segment to be copied (could be from
* a different timeline)
*
* upto: how much of the source file to copy (the rest is filled with
* zeros)
*
* Currently this is only used during recovery, and so there are no locking
* considerations. But we should be just as tense as XLogFileInit to avoid
* emplacing a bogus file.
*/
static void
XLogFileCopy(char *srcfname, int upto, XLogSegNo segno)
XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno,
int upto)
{
char srcpath[MAXPGPATH];
char path[MAXPGPATH];
char tmppath[MAXPGPATH];
char buffer[XLOG_BLCKSZ];
int srcfd;
Expand All @@ -3063,12 +3068,12 @@ XLogFileCopy(char *srcfname, int upto, XLogSegNo segno)
/*
* Open the source file
*/
snprintf(srcpath, MAXPGPATH, XLOGDIR "/%s", srcfname);
srcfd = OpenTransientFile(srcpath, O_RDONLY | PG_BINARY, 0);
XLogFilePath(path, srcTLI, srcsegno);
srcfd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
if (srcfd < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not open file \"%s\": %m", srcpath)));
errmsg("could not open file \"%s\": %m", path)));

/*
* Copy into a temp file name.
Expand Down Expand Up @@ -3112,11 +3117,11 @@ XLogFileCopy(char *srcfname, int upto, XLogSegNo segno)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not read file \"%s\": %m",
srcpath)));
path)));
else
ereport(ERROR,
(errmsg("not enough data in file \"%s\"",
srcpath)));
path)));
}
}
errno = 0;
Expand Down Expand Up @@ -3149,9 +3154,11 @@ XLogFileCopy(char *srcfname, int upto, XLogSegNo segno)

CloseTransientFile(srcfd);

/* install the new file */
(void) InstallXLogFileSegment(&segno, tmppath, false,
0, false, ERROR);
/*
* Now move the segment into place with its final name.
*/
if (!InstallXLogFileSegment(&destsegno, tmppath, false, 0, false))
elog(ERROR, "InstallXLogFileSegment should not have failed");
}

/*
Expand All @@ -3178,16 +3185,14 @@ XLogFileCopy(char *srcfname, int upto, XLogSegNo segno)
* place. This should be TRUE except during bootstrap log creation. The
* caller must *not* hold the lock at call.
*
* elevel: log level used by this routine.
*
* Returns TRUE if the file was installed successfully. FALSE indicates that
* max_segno limit was exceeded, or an error occurred while renaming the
* file into place.
*/
static bool
InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
bool find_free, XLogSegNo max_segno,
bool use_lock, int elevel)
bool use_lock)
{
char path[MAXPGPATH];
struct stat stat_buf;
Expand Down Expand Up @@ -3232,7 +3237,7 @@ InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
{
if (use_lock)
LWLockRelease(ControlFileLock);
ereport(elevel,
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not link file \"%s\" to \"%s\" (initialization of log file): %m",
tmppath, path)));
Expand All @@ -3244,7 +3249,7 @@ InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
{
if (use_lock)
LWLockRelease(ControlFileLock);
ereport(elevel,
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not rename file \"%s\" to \"%s\" (initialization of log file): %m",
tmppath, path)));
Expand Down Expand Up @@ -3733,7 +3738,7 @@ RemoveXlogFile(const char *segname, XLogRecPtr PriorRedoPtr, XLogRecPtr endptr)
if (endlogSegNo <= recycleSegNo &&
lstat(path, &statbuf) == 0 && S_ISREG(statbuf.st_mode) &&
InstallXLogFileSegment(&endlogSegNo, path,
true, recycleSegNo, true, LOG))
true, recycleSegNo, true))
{
ereport(DEBUG2,
(errmsg("recycled transaction log file \"%s\"",
Expand Down Expand Up @@ -5212,16 +5217,15 @@ exitArchiveRecovery(TimeLineID endTLI, XLogRecPtr endOfLog)
*/
if (endLogSegNo == startLogSegNo)
{
XLogFileName(xlogfname, endTLI, endLogSegNo);

/*
* Make a copy of the file on the new timeline.
*
* Writing WAL isn't allowed yet, so there are no locking
* considerations. But we should be just as tense as XLogFileInit to
* avoid emplacing a bogus file.
*/
XLogFileCopy(xlogfname, endOfLog % XLOG_SEG_SIZE, endLogSegNo);
XLogFileCopy(endLogSegNo, endTLI, endLogSegNo,
endOfLog % XLOG_SEG_SIZE);
}
else
{
Expand Down

0 comments on commit 8217370

Please sign in to comment.