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

Commit 2d46a57

Browse files
committed
Improve copydir() code for the case that fsync is off.
We should avoid calling sync_file_range or posix_fadvise in this case, since (a) we don't really care if the data gets synced, and might as well save the kernel calls; (b) at least on Linux we know that the kernel might block us until it's scheduled the write. Also, avoid making a useless second traversal of the directory tree if we're not actually going to call fsync(2) after all.
1 parent 2c4f5b4 commit 2d46a57

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/backend/storage/file/copydir.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ copydir(char *fromdir, char *todir, bool recurse)
9898

9999
/*
100100
* Be paranoid here and fsync all files to ensure the copy is really done.
101+
* But if fsync is disabled, we're done.
101102
*/
103+
if (!enableFsync)
104+
return;
105+
102106
xldir = AllocateDir(todir);
103107
if (xldir == NULL)
104108
ereport(ERROR,
@@ -200,9 +204,9 @@ copy_file(char *fromfile, char *tofile)
200204
/*
201205
* We fsync the files later but first flush them to avoid spamming the
202206
* cache and hopefully get the kernel to start writing them out before
203-
* the fsync comes.
207+
* the fsync comes. Ignore any error, since it's only a hint.
204208
*/
205-
pg_flush_data(dstfd, offset, nbytes);
209+
(void) pg_flush_data(dstfd, offset, nbytes);
206210
}
207211

208212
if (close(dstfd))

src/backend/storage/file/fd.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,22 @@ pg_fdatasync(int fd)
337337
* pg_flush_data --- advise OS that the data described won't be needed soon
338338
*
339339
* Not all platforms have sync_file_range or posix_fadvise; treat as no-op
340-
* if not available.
340+
* if not available. Also, treat as no-op if enableFsync is off; this is
341+
* because the call isn't free, and some platforms such as Linux will actually
342+
* block the requestor until the write is scheduled.
341343
*/
342344
int
343345
pg_flush_data(int fd, off_t offset, off_t amount)
344346
{
347+
if (enableFsync)
348+
{
345349
#if defined(HAVE_SYNC_FILE_RANGE)
346-
return sync_file_range(fd, offset, amount, SYNC_FILE_RANGE_WRITE);
350+
return sync_file_range(fd, offset, amount, SYNC_FILE_RANGE_WRITE);
347351
#elif defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
348-
return posix_fadvise(fd, offset, amount, POSIX_FADV_DONTNEED);
349-
#else
350-
return 0;
352+
return posix_fadvise(fd, offset, amount, POSIX_FADV_DONTNEED);
351353
#endif
354+
}
355+
return 0;
352356
}
353357

354358

0 commit comments

Comments
 (0)