From 5b571bb8c8d2bea610e01ae1ee7bc05adcfff528 Mon Sep 17 00:00:00 2001 From: Jeff Davis Date: Sat, 6 Jul 2013 13:46:04 -0700 Subject: [PATCH] Handle posix_fallocate() errors. On some platforms, posix_fallocate() is available but may still return EINVAL if the underlying filesystem does not support it. So, in case of an error, fall through to the alternate implementation that just writes zeros. Per buildfarm failure and analysis by Tom Lane. --- src/backend/access/transam/xlog.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index f97ab1ae3a..77e5c3b5d8 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2259,6 +2259,7 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock) XLogSegNo installed_segno; int max_advance; int fd; + bool zero_fill = true; XLogFilePath(path, ThisTimeLineID, logsegno); @@ -2301,24 +2302,18 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock) errmsg("could not create file \"%s\": %m", tmppath))); #ifdef HAVE_POSIX_FALLOCATE - { - errno = posix_fallocate(fd, 0, XLogSegSize); - - if (errno) - { - int errno_saved = errno; - - close(fd); - unlink(tmppath); - errno = errno_saved; + /* + * If posix_fallocate() is available and succeeds, then the file is + * properly allocated and we don't need to zero-fill it (which is less + * efficient). In case of an error, fall back to writing zeros, because on + * some platforms posix_fallocate() is available but will not always + * succeed in cases where zero-filling will. + */ + if (posix_fallocate(fd, 0, XLogSegSize) == 0) + zero_fill = false; +#endif /* HAVE_POSIX_FALLOCATE */ - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not allocate space for file \"%s\" using posix_fallocate: %m", - tmppath))); - } - } -#else /* !HAVE_POSIX_FALLOCATE */ + if (zero_fill) { /* * Allocate a buffer full of zeros. This is done before opening the @@ -2366,7 +2361,6 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock) } pfree(zbuffer); } -#endif /* HAVE_POSIX_FALLOCATE */ if (pg_fsync(fd) != 0) { -- GitLab