Skip to content

Commit 4bff35c

Browse files
committed
Retry opening new segments in pg_xlogdump --folllow
There is a small window between when the server closes out the existing segment and the new one is created. Put a loop around the open call in this case to make sure we wait for the new file to actually appear.
1 parent 3c97a7c commit 4bff35c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

contrib/pg_xlogdump/pg_xlogdump.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ XLogDumpXLogRead(const char *directory, TimeLineID timeline_id,
231231
if (sendFile < 0 || !XLByteInSeg(recptr, sendSegNo))
232232
{
233233
char fname[MAXFNAMELEN];
234+
int tries;
234235

235236
/* Switch to another logfile segment */
236237
if (sendFile >= 0)
@@ -240,7 +241,30 @@ XLogDumpXLogRead(const char *directory, TimeLineID timeline_id,
240241

241242
XLogFileName(fname, timeline_id, sendSegNo);
242243

243-
sendFile = fuzzy_open_file(directory, fname);
244+
/*
245+
* In follow mode there is a short period of time after the
246+
* server has written the end of the previous file before the
247+
* new file is available. So we loop for 5 seconds looking
248+
* for the file to appear before giving up.
249+
*/
250+
for (tries = 0; tries < 10; tries++)
251+
{
252+
sendFile = fuzzy_open_file(directory, fname);
253+
if (sendFile >= 0)
254+
break;
255+
if (errno == ENOENT)
256+
{
257+
int save_errno = errno;
258+
259+
/* File not there yet, try again */
260+
pg_usleep(500 * 1000);
261+
262+
errno = save_errno;
263+
continue;
264+
}
265+
/* Any other error, fall through and fail */
266+
break;
267+
}
244268

245269
if (sendFile < 0)
246270
fatal_error("could not find file \"%s\": %s",

0 commit comments

Comments
 (0)