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

Commit 81f3d3b

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 27bae8d commit 81f3d3b

File tree

3 files changed

+254
-111
lines changed

3 files changed

+254
-111
lines changed

src/backend/access/transam/xlog.c

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,6 @@ static void rm_redo_error_callback(void *arg);
696696
static int get_sync_bit(int method);
697697

698698

699-
static void fsync_pgdata(char *datadir);
700-
701699
/*
702700
* Insert an XLOG record having the specified RMID and info bytes,
703701
* with the body of the record being the data chunk(s) described by
@@ -5015,18 +5013,6 @@ StartupXLOG(void)
50155013
(errmsg("database system was interrupted; last known up at %s",
50165014
str_time(ControlFile->time))));
50175015

5018-
/*
5019-
* If we previously crashed, there might be data which we had written,
5020-
* intending to fsync it, but which we had not actually fsync'd yet.
5021-
* Therefore, a power failure in the near future might cause earlier
5022-
* unflushed writes to be lost, even though more recent data written to
5023-
* disk from here on would be persisted. To avoid that, fsync the entire
5024-
* data directory.
5025-
*/
5026-
if (ControlFile->state != DB_SHUTDOWNED &&
5027-
ControlFile->state != DB_SHUTDOWNED_IN_RECOVERY)
5028-
fsync_pgdata(data_directory);
5029-
50305016
/* This is just to allow attaching to startup process with a debugger */
50315017
#ifdef XLOG_REPLAY_DELAY
50325018
if (ControlFile->state != DB_SHUTDOWNED)
@@ -5050,6 +5036,18 @@ StartupXLOG(void)
50505036
*/
50515037
RelationCacheInitFileRemove();
50525038

5039+
/*
5040+
* If we previously crashed, there might be data which we had written,
5041+
* intending to fsync it, but which we had not actually fsync'd yet.
5042+
* Therefore, a power failure in the near future might cause earlier
5043+
* unflushed writes to be lost, even though more recent data written to
5044+
* disk from here on would be persisted. To avoid that, fsync the entire
5045+
* data directory.
5046+
*/
5047+
if (ControlFile->state != DB_SHUTDOWNED &&
5048+
ControlFile->state != DB_SHUTDOWNED_IN_RECOVERY)
5049+
SyncDataDirectory();
5050+
50535051
/*
50545052
* Initialize on the assumption we want to recover to the latest timeline
50555053
* that's active according to pg_control.
@@ -10193,31 +10191,3 @@ SetWalWriterSleeping(bool sleeping)
1019310191
xlogctl->WalWriterSleeping = sleeping;
1019410192
SpinLockRelease(&xlogctl->info_lck);
1019510193
}
10196-
10197-
/*
10198-
* Issue fsync recursively on PGDATA and all its contents.
10199-
*/
10200-
static void
10201-
fsync_pgdata(char *datadir)
10202-
{
10203-
if (!enableFsync)
10204-
return;
10205-
10206-
/*
10207-
* If possible, hint to the kernel that we're soon going to fsync
10208-
* the data directory and its contents.
10209-
*/
10210-
#if defined(HAVE_SYNC_FILE_RANGE) || \
10211-
(defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED))
10212-
walkdir(datadir, pre_sync_fname);
10213-
#endif
10214-
10215-
/*
10216-
* Now we do the fsync()s in the same order.
10217-
*
10218-
* It's important to fsync the destination directory itself as individual
10219-
* file fsyncs don't guarantee that the directory entry for the file is
10220-
* synced.
10221-
*/
10222-
walkdir(datadir, fsync_fname);
10223-
}

0 commit comments

Comments
 (0)