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

Commit 14de825

Browse files
committed
Recursively fsync() the data directory after a crash.
Otherwise, if there's another crash, some writes from after the first crash might make it to disk while writes from before the crash fail to make it to disk. This could lead to data corruption. Back-patch to all supported versions. Abhijit Menon-Sen, reviewed by Andres Freund and slightly revised by me.
1 parent e60581f commit 14de825

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

src/backend/access/transam/xlog.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ 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+
699701
/*
700702
* Insert an XLOG record having the specified RMID and info bytes,
701703
* with the body of the record being the data chunk(s) described by
@@ -5013,6 +5015,18 @@ StartupXLOG(void)
50135015
(errmsg("database system was interrupted; last known up at %s",
50145016
str_time(ControlFile->time))));
50155017

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+
50165030
/* This is just to allow attaching to startup process with a debugger */
50175031
#ifdef XLOG_REPLAY_DELAY
50185032
if (ControlFile->state != DB_SHUTDOWNED)
@@ -10179,3 +10193,31 @@ SetWalWriterSleeping(bool sleeping)
1017910193
xlogctl->WalWriterSleeping = sleeping;
1018010194
SpinLockRelease(&xlogctl->info_lck);
1018110195
}
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+
}

src/backend/storage/file/fd.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,3 +2445,118 @@ looks_like_temp_rel_name(const char *name)
24452445
return false;
24462446
return true;
24472447
}
2448+
2449+
/*
2450+
* Hint to the OS that it should get ready to fsync() this file.
2451+
*
2452+
* Adapted from pre_sync_fname in initdb.c
2453+
*/
2454+
void
2455+
pre_sync_fname(char *fname, bool isdir)
2456+
{
2457+
int fd;
2458+
2459+
fd = open(fname, O_RDONLY | PG_BINARY);
2460+
2461+
/*
2462+
* Some OSs don't allow us to open directories at all (Windows returns
2463+
* EACCES)
2464+
*/
2465+
if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
2466+
return;
2467+
2468+
if (fd < 0)
2469+
ereport(FATAL,
2470+
(errmsg("could not open file \"%s\" before fsync",
2471+
fname)));
2472+
2473+
pg_flush_data(fd, 0, 0);
2474+
2475+
close(fd);
2476+
}
2477+
2478+
/*
2479+
* walkdir: recursively walk a directory, applying the action to each
2480+
* regular file and directory (including the named directory itself)
2481+
* and following symbolic links.
2482+
*
2483+
* NB: There is another version of walkdir in initdb.c, but that version
2484+
* behaves differently with respect to symbolic links. Caveat emptor!
2485+
*/
2486+
void
2487+
walkdir(char *path, void (*action) (char *fname, bool isdir))
2488+
{
2489+
DIR *dir;
2490+
struct dirent *de;
2491+
2492+
dir = AllocateDir(path);
2493+
while ((de = ReadDir(dir, path)) != NULL)
2494+
{
2495+
char subpath[MAXPGPATH];
2496+
struct stat fst;
2497+
2498+
CHECK_FOR_INTERRUPTS();
2499+
2500+
if (strcmp(de->d_name, ".") == 0 ||
2501+
strcmp(de->d_name, "..") == 0)
2502+
continue;
2503+
2504+
snprintf(subpath, MAXPGPATH, "%s/%s", path, de->d_name);
2505+
2506+
if (lstat(subpath, &fst) < 0)
2507+
ereport(ERROR,
2508+
(errcode_for_file_access(),
2509+
errmsg("could not stat file \"%s\": %m", subpath)));
2510+
2511+
if (S_ISREG(fst.st_mode))
2512+
(*action) (subpath, false);
2513+
else if (S_ISDIR(fst.st_mode))
2514+
walkdir(subpath, action);
2515+
#ifndef WIN32
2516+
else if (S_ISLNK(fst.st_mode))
2517+
#else
2518+
else if (pg_win32_is_junction(subpath))
2519+
#endif
2520+
{
2521+
#if defined(HAVE_READLINK) || defined(WIN32)
2522+
char linkpath[MAXPGPATH];
2523+
int len;
2524+
struct stat lst;
2525+
2526+
len = readlink(subpath, linkpath, sizeof(linkpath)-1);
2527+
if (len < 0)
2528+
ereport(ERROR,
2529+
(errcode_for_file_access(),
2530+
errmsg("could not read symbolic link \"%s\": %m",
2531+
subpath)));
2532+
2533+
if (len >= sizeof(linkpath)-1)
2534+
ereport(ERROR,
2535+
(errmsg("symbolic link \"%s\" target is too long",
2536+
subpath)));
2537+
2538+
linkpath[len] = '\0';
2539+
2540+
if (lstat(linkpath, &lst) == 0)
2541+
{
2542+
if (S_ISREG(lst.st_mode))
2543+
(*action) (linkpath, false);
2544+
else if (S_ISDIR(lst.st_mode))
2545+
walkdir(subpath, action);
2546+
}
2547+
else if (errno != ENOENT)
2548+
ereport(ERROR,
2549+
(errcode_for_file_access(),
2550+
errmsg("could not stat file \"%s\": %m", linkpath)));
2551+
#else
2552+
ereport(WARNING,
2553+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2554+
errmsg("this platform does not support symbolic links; ignoring \"%s\"",
2555+
subpath)));
2556+
#endif
2557+
}
2558+
}
2559+
FreeDir(dir);
2560+
2561+
(*action) (path, true);
2562+
}

src/include/storage/fd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ extern int pg_fsync_writethrough(int fd);
114114
extern int pg_fdatasync(int fd);
115115
extern int pg_flush_data(int fd, off_t offset, off_t amount);
116116
extern void fsync_fname(char *fname, bool isdir);
117+
extern void pre_sync_fname(char *fname, bool isdir);
118+
extern void walkdir(char *path, void (*action) (char *fname, bool isdir));
117119

118120
/* Filename components for OpenTemporaryFile */
119121
#define PG_TEMP_FILES_DIR "pgsql_tmp"

0 commit comments

Comments
 (0)