Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a016f59

Browse files
committed
Prioritize history files when archiving
At the end of recovery for the post-promotion process, a new history file is created followed by the last partial segment of the previous timeline. Based on the timing, the archiver would first try to archive the last partial segment and then the history file. This can delay the detection of a new timeline taken, particularly depending on the time it takes to transfer the last partial segment as it delays the moment the history file of the new timeline gets archived. This can cause promoted standbys to use the same timeline as one already taken depending on the circumstances if multiple instances look at archives at the same location. This commit changes the order of archiving so as history files are archived in priority over other file types, which reduces the likelihood of the same timeline being taken (still not reducing the window to zero), and it makes the archiver behave more consistently with the startup process doing its post-promotion business. Author: David Steele Reviewed-by: Michael Paquier, Kyotaro Horiguchi Discussion: https://postgr.es/m/929068cf-69e1-bba2-9dc0-e05986aed471@pgmasters.net Backpatch-through: 9.5
1 parent ff9c222 commit a016f59

File tree

1 file changed

+46
-26
lines changed

1 file changed

+46
-26
lines changed

src/backend/postmaster/pgarch.c

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -648,11 +648,12 @@ pgarch_archiveXlog(char *xlog)
648648
* 2) because the oldest ones will sooner become candidates for
649649
* recycling at time of checkpoint
650650
*
651-
* NOTE: the "oldest" comparison will presently consider all segments of
652-
* a timeline with a smaller ID to be older than all segments of a timeline
653-
* with a larger ID; the net result being that past timelines are given
654-
* higher priority for archiving. This seems okay, or at least not
655-
* obviously worth changing.
651+
* NOTE: the "oldest" comparison will consider any .history file to be older
652+
* than any other file except another .history file. Segments on a timeline
653+
* with a smaller ID will be older than all segments on a timeline with a
654+
* larger ID; the net result being that past timelines are given higher
655+
* priority for archiving. This seems okay, or at least not obviously worth
656+
* changing.
656657
*/
657658
static bool
658659
pgarch_readyXlog(char *xlog)
@@ -664,43 +665,62 @@ pgarch_readyXlog(char *xlog)
664665
* of calls, so....
665666
*/
666667
char XLogArchiveStatusDir[MAXPGPATH];
667-
char newxlog[MAX_XFN_CHARS + 6 + 1];
668668
DIR *rldir;
669669
struct dirent *rlde;
670670
bool found = false;
671+
bool historyFound = false;
671672

672673
snprintf(XLogArchiveStatusDir, MAXPGPATH, XLOGDIR "/archive_status");
673674
rldir = AllocateDir(XLogArchiveStatusDir);
674675

675676
while ((rlde = ReadDir(rldir, XLogArchiveStatusDir)) != NULL)
676677
{
677678
int basenamelen = (int) strlen(rlde->d_name) - 6;
679+
char basename[MAX_XFN_CHARS + 1];
680+
bool ishistory;
678681

679-
if (basenamelen >= MIN_XFN_CHARS &&
680-
basenamelen <= MAX_XFN_CHARS &&
681-
strspn(rlde->d_name, VALID_XFN_CHARS) >= basenamelen &&
682-
strcmp(rlde->d_name + basenamelen, ".ready") == 0)
682+
/* Ignore entries with unexpected number of characters */
683+
if (basenamelen < MIN_XFN_CHARS ||
684+
basenamelen > MAX_XFN_CHARS)
685+
continue;
686+
687+
/* Ignore entries with unexpected characters */
688+
if (strspn(rlde->d_name, VALID_XFN_CHARS) < basenamelen)
689+
continue;
690+
691+
/* Ignore anything not suffixed with .ready */
692+
if (strcmp(rlde->d_name + basenamelen, ".ready") != 0)
693+
continue;
694+
695+
/* Truncate off the .ready */
696+
memcpy(basename, rlde->d_name, basenamelen);
697+
basename[basenamelen] = '\0';
698+
699+
/* Is this a history file? */
700+
ishistory = IsTLHistoryFileName(basename);
701+
702+
/*
703+
* Consume the file to archive. History files have the highest
704+
* priority. If this is the first file or the first history file
705+
* ever, copy it. In the presence of a history file already chosen as
706+
* target, ignore all other files except history files which have been
707+
* generated for an older timeline than what is already chosen as
708+
* target to archive.
709+
*/
710+
if (!found || (ishistory && !historyFound))
683711
{
684-
if (!found)
685-
{
686-
strcpy(newxlog, rlde->d_name);
687-
found = true;
688-
}
689-
else
690-
{
691-
if (strcmp(rlde->d_name, newxlog) < 0)
692-
strcpy(newxlog, rlde->d_name);
693-
}
712+
strcpy(xlog, basename);
713+
found = true;
714+
historyFound = ishistory;
715+
}
716+
else if (ishistory || !historyFound)
717+
{
718+
if (strcmp(basename, xlog) < 0)
719+
strcpy(xlog, basename);
694720
}
695721
}
696722
FreeDir(rldir);
697723

698-
if (found)
699-
{
700-
/* truncate off the .ready */
701-
newxlog[strlen(newxlog) - 6] = '\0';
702-
strcpy(xlog, newxlog);
703-
}
704724
return found;
705725
}
706726

0 commit comments

Comments
 (0)