From b966dd6c4228d696b291c1cdcb5ab8c8475fefa8 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 13 Jul 2012 17:16:58 -0400 Subject: Add fsync capability to initdb, and use sync_file_range() if available. Historically we have not worried about fsync'ing anything during initdb (in fact, initdb intentionally passes -F to each backend launch to prevent it from fsync'ing). But with filesystems getting more aggressive about caching data, that's not such a good plan anymore. Make initdb do a pass over the finished data directory tree to fsync everything. For testing purposes, the -N/--nosync flag can be used to restore the old behavior. Also, testing shows that on Linux, sync_file_range() is much faster than posix_fadvise() for hinting to the kernel that an fsync is coming, apparently because the latter blocks on a rather small request queue while the former doesn't. So use this function if available in initdb, and also in the backend's pg_flush_data() (where it currently will affect only the speed of CREATE DATABASE's cloning step). We will later make pg_regress invoke initdb with the --nosync flag to avoid slowing down cases such as "make check" in contrib. But let's not do so until we've shaken out any portability issues in this patch. Jeff Davis, reviewed by Andres Freund --- src/backend/storage/file/fd.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/backend/storage/file/fd.c') diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index f79f4c6a36e..9724f481dc0 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -336,12 +336,15 @@ pg_fdatasync(int fd) /* * pg_flush_data --- advise OS that the data described won't be needed soon * - * Not all platforms have posix_fadvise; treat as noop if not available. + * Not all platforms have sync_file_range or posix_fadvise; treat as no-op + * if not available. */ int pg_flush_data(int fd, off_t offset, off_t amount) { -#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED) +#if defined(HAVE_SYNC_FILE_RANGE) + return sync_file_range(fd, offset, amount, SYNC_FILE_RANGE_WRITE); +#elif defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED) return posix_fadvise(fd, offset, amount, POSIX_FADV_DONTNEED); #else return 0; -- cgit v1.2.3