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

Commit a3ae3db

Browse files
committed
Fix fsync-at-startup code to not treat errors as fatal.
Commit 2ce439f introduced a rather serious regression, namely that if its scan of the data directory came across any un-fsync-able files, it would fail and thereby prevent database startup. Worse yet, symlinks to such files also caused the problem, which meant that crash restart was guaranteed to fail on certain common installations such as older Debian. After discussion, we agreed that (1) failure to start is worse than any consequence of not fsync'ing is likely to be, therefore treat all errors in this code as nonfatal; (2) we should not chase symlinks other than those that are expected to exist, namely pg_xlog/ and tablespace links under pg_tblspc/. The latter restriction avoids possibly fsync'ing a much larger part of the filesystem than intended, if the user has left random symlinks hanging about in the data directory. This commit takes care of that and also does some code beautification, mainly moving the relevant code into fd.c, which seems a much better place for it than xlog.c, and making sure that the conditional compilation for the pre_sync_fname pass has something to do with whether pg_flush_data works. I also relocated the call site in xlog.c down a few lines; it seems a bit silly to be doing this before ValidateXLOGDirectoryStructure(). The similar logic in initdb.c ought to be made to match this, but that change is noncritical and will be dealt with separately. Back-patch to all active branches, like the prior commit. Abhijit Menon-Sen and Tom Lane
1 parent d4a9f55 commit a3ae3db

File tree

3 files changed

+254
-111
lines changed

3 files changed

+254
-111
lines changed

src/backend/access/transam/xlog.c

+12-42
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,6 @@ static void WALInsertLockAcquireExclusive(void);
826826
static void WALInsertLockRelease(void);
827827
static void WALInsertLockUpdateInsertingAt(XLogRecPtr insertingAt);
828828

829-
static void fsync_pgdata(char *datadir);
830-
831829
/*
832830
* Insert an XLOG record having the specified RMID and info bytes,
833831
* with the body of the record being the data chunk(s) described by
@@ -6118,18 +6116,6 @@ StartupXLOG(void)
61186116
(errmsg("database system was interrupted; last known up at %s",
61196117
str_time(ControlFile->time))));
61206118

6121-
/*
6122-
* If we previously crashed, there might be data which we had written,
6123-
* intending to fsync it, but which we had not actually fsync'd yet.
6124-
* Therefore, a power failure in the near future might cause earlier
6125-
* unflushed writes to be lost, even though more recent data written to
6126-
* disk from here on would be persisted. To avoid that, fsync the entire
6127-
* data directory.
6128-
*/
6129-
if (ControlFile->state != DB_SHUTDOWNED &&
6130-
ControlFile->state != DB_SHUTDOWNED_IN_RECOVERY)
6131-
fsync_pgdata(data_directory);
6132-
61336119
/* This is just to allow attaching to startup process with a debugger */
61346120
#ifdef XLOG_REPLAY_DELAY
61356121
if (ControlFile->state != DB_SHUTDOWNED)
@@ -6153,6 +6139,18 @@ StartupXLOG(void)
61536139
*/
61546140
RelationCacheInitFileRemove();
61556141

6142+
/*
6143+
* If we previously crashed, there might be data which we had written,
6144+
* intending to fsync it, but which we had not actually fsync'd yet.
6145+
* Therefore, a power failure in the near future might cause earlier
6146+
* unflushed writes to be lost, even though more recent data written to
6147+
* disk from here on would be persisted. To avoid that, fsync the entire
6148+
* data directory.
6149+
*/
6150+
if (ControlFile->state != DB_SHUTDOWNED &&
6151+
ControlFile->state != DB_SHUTDOWNED_IN_RECOVERY)
6152+
SyncDataDirectory();
6153+
61566154
/*
61576155
* Initialize on the assumption we want to recover to the latest timeline
61586156
* that's active according to pg_control.
@@ -11352,31 +11350,3 @@ SetWalWriterSleeping(bool sleeping)
1135211350
xlogctl->WalWriterSleeping = sleeping;
1135311351
SpinLockRelease(&xlogctl->info_lck);
1135411352
}
11355-
11356-
/*
11357-
* Issue fsync recursively on PGDATA and all its contents.
11358-
*/
11359-
static void
11360-
fsync_pgdata(char *datadir)
11361-
{
11362-
if (!enableFsync)
11363-
return;
11364-
11365-
/*
11366-
* If possible, hint to the kernel that we're soon going to fsync
11367-
* the data directory and its contents.
11368-
*/
11369-
#if defined(HAVE_SYNC_FILE_RANGE) || \
11370-
(defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED))
11371-
walkdir(datadir, pre_sync_fname);
11372-
#endif
11373-
11374-
/*
11375-
* Now we do the fsync()s in the same order.
11376-
*
11377-
* It's important to fsync the destination directory itself as individual
11378-
* file fsyncs don't guarantee that the directory entry for the file is
11379-
* synced.
11380-
*/
11381-
walkdir(datadir, fsync_fname);
11382-
}

0 commit comments

Comments
 (0)