Skip to content

Commit 62fb12d

Browse files
committed
When performing a base backup, check for read errors.
The old code didn't differentiate between a read error and a concurrent truncation. fread reports both of these by returning 0; you have to use feof() or ferror() to distinguish between them, which this code did not do. It might be a better idea to use read() rather than fread() here, so that we can display a less-generic error message, but I'm not sure that would qualify as a back-patchable bug fix, so just do this much for now. Jeevan Chalke, reviewed by Jeevan Ladhe and by me. Discussion: http://postgr.es/m/CA+TgmobG4ywMzL5oQq2a8YKp8x2p3p1LOMMcGqpS7aekT9+ETA@mail.gmail.com
1 parent 4950f09 commit 62fb12d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/backend/replication/basebackup.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ static char *statrelpath = NULL;
8585
*/
8686
#define THROTTLING_FREQUENCY 8
8787

88+
/*
89+
* Checks whether we encountered any error in fread(). fread() doesn't give
90+
* any clue what has happened, so we check with ferror(). Also, neither
91+
* fread() nor ferror() set errno, so we just throw a generic error.
92+
*/
93+
#define CHECK_FREAD_ERROR(fp, filename) \
94+
do { \
95+
if (ferror(fp)) \
96+
ereport(ERROR, \
97+
(errmsg("could not read from file \"%s\"", filename))); \
98+
} while (0)
99+
88100
/* The actual number of bytes, transfer of which may cause sleep. */
89101
static uint64 throttling_sample;
90102

@@ -509,6 +521,8 @@ perform_base_backup(basebackup_options *opt, DIR *tblspcdir)
509521
break;
510522
}
511523

524+
CHECK_FREAD_ERROR(fp, pathbuf);
525+
512526
if (len != XLogSegSize)
513527
{
514528
CheckXLogRemoved(segno, tli);
@@ -1245,6 +1259,8 @@ sendFile(char *readfilename, char *tarfilename, struct stat *statbuf,
12451259
}
12461260
}
12471261

1262+
CHECK_FREAD_ERROR(fp, readfilename);
1263+
12481264
/* If the file was truncated while we were sending it, pad it with zeros */
12491265
if (len < statbuf->st_size)
12501266
{

0 commit comments

Comments
 (0)