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

Commit4b71d28

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 parent1dadd36 commit4b71d28

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

‎src/backend/access/transam/xlog.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ static bool read_backup_label(XLogRecPtr *checkPointLoc,
678678
staticvoidrm_redo_error_callback(void*arg);
679679
staticintget_sync_bit(intmethod);
680680

681+
staticvoidfsync_pgdata(char*datadir);
681682

682683
/*
683684
* Insert an XLOG record having the specified RMID and info bytes,
@@ -6229,6 +6230,18 @@ StartupXLOG(void)
62296230
(errmsg("database system was interrupted; last known up at %s",
62306231
str_time(ControlFile->time))));
62316232

6233+
/*
6234+
* If we previously crashed, there might be data which we had written,
6235+
* intending to fsync it, but which we had not actually fsync'd yet.
6236+
* Therefore, a power failure in the near future might cause earlier
6237+
* unflushed writes to be lost, even though more recent data written to
6238+
* disk from here on would be persisted. To avoid that, fsync the entire
6239+
* data directory.
6240+
*/
6241+
if (ControlFile->state!=DB_SHUTDOWNED&&
6242+
ControlFile->state!=DB_SHUTDOWNED_IN_RECOVERY)
6243+
fsync_pgdata(data_directory);
6244+
62326245
/* This is just to allow attaching to startup process with a debugger */
62336246
#ifdefXLOG_REPLAY_DELAY
62346247
if (ControlFile->state!=DB_SHUTDOWNED)
@@ -10825,3 +10838,31 @@ WakeupRecovery(void)
1082510838
{
1082610839
SetLatch(&XLogCtl->recoveryWakeupLatch);
1082710840
}
10841+
10842+
/*
10843+
* Issue fsync recursively on PGDATA and all its contents.
10844+
*/
10845+
staticvoid
10846+
fsync_pgdata(char*datadir)
10847+
{
10848+
if (!enableFsync)
10849+
return;
10850+
10851+
/*
10852+
* If possible, hint to the kernel that we're soon going to fsync
10853+
* the data directory and its contents.
10854+
*/
10855+
#if defined(HAVE_SYNC_FILE_RANGE)|| \
10856+
(defined(USE_POSIX_FADVISE)&& defined(POSIX_FADV_DONTNEED))
10857+
walkdir(datadir,pre_sync_fname);
10858+
#endif
10859+
10860+
/*
10861+
* Now we do the fsync()s in the same order.
10862+
*
10863+
* It's important to fsync the destination directory itself as individual
10864+
* file fsyncs don't guarantee that the directory entry for the file is
10865+
* synced.
10866+
*/
10867+
walkdir(datadir,fsync_fname);
10868+
}

‎src/backend/storage/file/fd.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,3 +2206,118 @@ looks_like_temp_rel_name(const char *name)
22062206
return false;
22072207
return true;
22082208
}
2209+
2210+
/*
2211+
* Hint to the OS that it should get ready to fsync() this file.
2212+
*
2213+
* Adapted from pre_sync_fname in initdb.c
2214+
*/
2215+
void
2216+
pre_sync_fname(char*fname,boolisdir)
2217+
{
2218+
intfd;
2219+
2220+
fd=open(fname,O_RDONLY |PG_BINARY);
2221+
2222+
/*
2223+
* Some OSs don't allow us to open directories at all (Windows returns
2224+
* EACCES)
2225+
*/
2226+
if (fd<0&&isdir&& (errno==EISDIR||errno==EACCES))
2227+
return;
2228+
2229+
if (fd<0)
2230+
ereport(FATAL,
2231+
(errmsg("could not open file \"%s\" before fsync",
2232+
fname)));
2233+
2234+
pg_flush_data(fd,0,0);
2235+
2236+
close(fd);
2237+
}
2238+
2239+
/*
2240+
* walkdir: recursively walk a directory, applying the action to each
2241+
* regular file and directory (including the named directory itself)
2242+
* and following symbolic links.
2243+
*
2244+
* NB: There is another version of walkdir in initdb.c, but that version
2245+
* behaves differently with respect to symbolic links. Caveat emptor!
2246+
*/
2247+
void
2248+
walkdir(char*path,void (*action) (char*fname,boolisdir))
2249+
{
2250+
DIR*dir;
2251+
structdirent*de;
2252+
2253+
dir=AllocateDir(path);
2254+
while ((de=ReadDir(dir,path))!=NULL)
2255+
{
2256+
charsubpath[MAXPGPATH];
2257+
structstatfst;
2258+
2259+
CHECK_FOR_INTERRUPTS();
2260+
2261+
if (strcmp(de->d_name,".")==0||
2262+
strcmp(de->d_name,"..")==0)
2263+
continue;
2264+
2265+
snprintf(subpath,MAXPGPATH,"%s/%s",path,de->d_name);
2266+
2267+
if (lstat(subpath,&fst)<0)
2268+
ereport(ERROR,
2269+
(errcode_for_file_access(),
2270+
errmsg("could not stat file \"%s\": %m",subpath)));
2271+
2272+
if (S_ISREG(fst.st_mode))
2273+
(*action) (subpath, false);
2274+
elseif (S_ISDIR(fst.st_mode))
2275+
walkdir(subpath,action);
2276+
#ifndefWIN32
2277+
elseif (S_ISLNK(fst.st_mode))
2278+
#else
2279+
elseif (pg_win32_is_junction(subpath))
2280+
#endif
2281+
{
2282+
#if defined(HAVE_READLINK)|| defined(WIN32)
2283+
charlinkpath[MAXPGPATH];
2284+
intlen;
2285+
structstatlst;
2286+
2287+
len=readlink(subpath,linkpath,sizeof(linkpath)-1);
2288+
if (len<0)
2289+
ereport(ERROR,
2290+
(errcode_for_file_access(),
2291+
errmsg("could not read symbolic link \"%s\": %m",
2292+
subpath)));
2293+
2294+
if (len >=sizeof(linkpath)-1)
2295+
ereport(ERROR,
2296+
(errmsg("symbolic link \"%s\" target is too long",
2297+
subpath)));
2298+
2299+
linkpath[len]='\0';
2300+
2301+
if (lstat(linkpath,&lst)==0)
2302+
{
2303+
if (S_ISREG(lst.st_mode))
2304+
(*action) (linkpath, false);
2305+
elseif (S_ISDIR(lst.st_mode))
2306+
walkdir(subpath,action);
2307+
}
2308+
elseif (errno!=ENOENT)
2309+
ereport(ERROR,
2310+
(errcode_for_file_access(),
2311+
errmsg("could not stat file \"%s\": %m",linkpath)));
2312+
#else
2313+
ereport(WARNING,
2314+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2315+
errmsg("this platform does not support symbolic links; ignoring \"%s\"",
2316+
subpath)));
2317+
#endif
2318+
}
2319+
}
2320+
FreeDir(dir);
2321+
2322+
(*action) (path, true);
2323+
}

‎src/include/storage/fd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ extern intpg_fsync_writethrough(int fd);
100100
externintpg_fdatasync(intfd);
101101
externintpg_flush_data(intfd,off_toffset,off_tamount);
102102
externvoidfsync_fname(char*fname,boolisdir);
103+
externvoidpre_sync_fname(char*fname,boolisdir);
104+
externvoidwalkdir(char*path,void (*action) (char*fname,boolisdir));
103105

104106
/* Filename components for OpenTemporaryFile */
105107
#definePG_TEMP_FILES_DIR "pgsql_tmp"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp