Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit14de825

Browse files
committed
Recursively fsync() the data directory after a crash.
Otherwise, if there's another crash, some writes from after the firstcrash might make it to disk while writes from before the crash failto 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 revisedby me.
1 parente60581f commit14de825

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
staticintget_sync_bit(intmethod);
697697

698698

699+
staticvoidfsync_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
#ifdefXLOG_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+
staticvoid
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,boolisdir)
2456+
{
2457+
intfd;
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,boolisdir))
2488+
{
2489+
DIR*dir;
2490+
structdirent*de;
2491+
2492+
dir=AllocateDir(path);
2493+
while ((de=ReadDir(dir,path))!=NULL)
2494+
{
2495+
charsubpath[MAXPGPATH];
2496+
structstatfst;
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+
elseif (S_ISDIR(fst.st_mode))
2514+
walkdir(subpath,action);
2515+
#ifndefWIN32
2516+
elseif (S_ISLNK(fst.st_mode))
2517+
#else
2518+
elseif (pg_win32_is_junction(subpath))
2519+
#endif
2520+
{
2521+
#if defined(HAVE_READLINK)|| defined(WIN32)
2522+
charlinkpath[MAXPGPATH];
2523+
intlen;
2524+
structstatlst;
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+
elseif (S_ISDIR(lst.st_mode))
2545+
walkdir(subpath,action);
2546+
}
2547+
elseif (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 intpg_fsync_writethrough(int fd);
114114
externintpg_fdatasync(intfd);
115115
externintpg_flush_data(intfd,off_toffset,off_tamount);
116116
externvoidfsync_fname(char*fname,boolisdir);
117+
externvoidpre_sync_fname(char*fname,boolisdir);
118+
externvoidwalkdir(char*path,void (*action) (char*fname,boolisdir));
117119

118120
/* Filename components for OpenTemporaryFile */
119121
#definePG_TEMP_FILES_DIR "pgsql_tmp"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp