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

Commit2bc3397

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 parent7c2ccb4 commit2bc3397

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
@@ -701,6 +701,7 @@ static bool read_backup_label(XLogRecPtr *checkPointLoc,
701701
staticvoidrm_redo_error_callback(void*arg);
702702
staticintget_sync_bit(intmethod);
703703

704+
staticvoidfsync_pgdata(char*datadir);
704705

705706
/*
706707
* Insert an XLOG record having the specified RMID and info bytes,
@@ -6417,6 +6418,18 @@ StartupXLOG(void)
64176418
(errmsg("database system was interrupted; last known up at %s",
64186419
str_time(ControlFile->time))));
64196420

6421+
/*
6422+
* If we previously crashed, there might be data which we had written,
6423+
* intending to fsync it, but which we had not actually fsync'd yet.
6424+
* Therefore, a power failure in the near future might cause earlier
6425+
* unflushed writes to be lost, even though more recent data written to
6426+
* disk from here on would be persisted. To avoid that, fsync the entire
6427+
* data directory.
6428+
*/
6429+
if (ControlFile->state!=DB_SHUTDOWNED&&
6430+
ControlFile->state!=DB_SHUTDOWNED_IN_RECOVERY)
6431+
fsync_pgdata(data_directory);
6432+
64206433
/* This is just to allow attaching to startup process with a debugger */
64216434
#ifdefXLOG_REPLAY_DELAY
64226435
if (ControlFile->state!=DB_SHUTDOWNED)
@@ -11014,3 +11027,31 @@ SetWalWriterSleeping(bool sleeping)
1101411027
xlogctl->WalWriterSleeping=sleeping;
1101511028
SpinLockRelease(&xlogctl->info_lck);
1101611029
}
11030+
11031+
/*
11032+
* Issue fsync recursively on PGDATA and all its contents.
11033+
*/
11034+
staticvoid
11035+
fsync_pgdata(char*datadir)
11036+
{
11037+
if (!enableFsync)
11038+
return;
11039+
11040+
/*
11041+
* If possible, hint to the kernel that we're soon going to fsync
11042+
* the data directory and its contents.
11043+
*/
11044+
#if defined(HAVE_SYNC_FILE_RANGE)|| \
11045+
(defined(USE_POSIX_FADVISE)&& defined(POSIX_FADV_DONTNEED))
11046+
walkdir(datadir,pre_sync_fname);
11047+
#endif
11048+
11049+
/*
11050+
* Now we do the fsync()s in the same order.
11051+
*
11052+
* It's important to fsync the destination directory itself as individual
11053+
* file fsyncs don't guarantee that the directory entry for the file is
11054+
* synced.
11055+
*/
11056+
walkdir(datadir,fsync_fname);
11057+
}

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,3 +2265,118 @@ looks_like_temp_rel_name(const char *name)
22652265
return false;
22662266
return true;
22672267
}
2268+
2269+
/*
2270+
* Hint to the OS that it should get ready to fsync() this file.
2271+
*
2272+
* Adapted from pre_sync_fname in initdb.c
2273+
*/
2274+
void
2275+
pre_sync_fname(char*fname,boolisdir)
2276+
{
2277+
intfd;
2278+
2279+
fd=open(fname,O_RDONLY |PG_BINARY);
2280+
2281+
/*
2282+
* Some OSs don't allow us to open directories at all (Windows returns
2283+
* EACCES)
2284+
*/
2285+
if (fd<0&&isdir&& (errno==EISDIR||errno==EACCES))
2286+
return;
2287+
2288+
if (fd<0)
2289+
ereport(FATAL,
2290+
(errmsg("could not open file \"%s\" before fsync",
2291+
fname)));
2292+
2293+
pg_flush_data(fd,0,0);
2294+
2295+
close(fd);
2296+
}
2297+
2298+
/*
2299+
* walkdir: recursively walk a directory, applying the action to each
2300+
* regular file and directory (including the named directory itself)
2301+
* and following symbolic links.
2302+
*
2303+
* NB: There is another version of walkdir in initdb.c, but that version
2304+
* behaves differently with respect to symbolic links. Caveat emptor!
2305+
*/
2306+
void
2307+
walkdir(char*path,void (*action) (char*fname,boolisdir))
2308+
{
2309+
DIR*dir;
2310+
structdirent*de;
2311+
2312+
dir=AllocateDir(path);
2313+
while ((de=ReadDir(dir,path))!=NULL)
2314+
{
2315+
charsubpath[MAXPGPATH];
2316+
structstatfst;
2317+
2318+
CHECK_FOR_INTERRUPTS();
2319+
2320+
if (strcmp(de->d_name,".")==0||
2321+
strcmp(de->d_name,"..")==0)
2322+
continue;
2323+
2324+
snprintf(subpath,MAXPGPATH,"%s/%s",path,de->d_name);
2325+
2326+
if (lstat(subpath,&fst)<0)
2327+
ereport(ERROR,
2328+
(errcode_for_file_access(),
2329+
errmsg("could not stat file \"%s\": %m",subpath)));
2330+
2331+
if (S_ISREG(fst.st_mode))
2332+
(*action) (subpath, false);
2333+
elseif (S_ISDIR(fst.st_mode))
2334+
walkdir(subpath,action);
2335+
#ifndefWIN32
2336+
elseif (S_ISLNK(fst.st_mode))
2337+
#else
2338+
elseif (pg_win32_is_junction(subpath))
2339+
#endif
2340+
{
2341+
#if defined(HAVE_READLINK)|| defined(WIN32)
2342+
charlinkpath[MAXPGPATH];
2343+
intlen;
2344+
structstatlst;
2345+
2346+
len=readlink(subpath,linkpath,sizeof(linkpath)-1);
2347+
if (len<0)
2348+
ereport(ERROR,
2349+
(errcode_for_file_access(),
2350+
errmsg("could not read symbolic link \"%s\": %m",
2351+
subpath)));
2352+
2353+
if (len >=sizeof(linkpath)-1)
2354+
ereport(ERROR,
2355+
(errmsg("symbolic link \"%s\" target is too long",
2356+
subpath)));
2357+
2358+
linkpath[len]='\0';
2359+
2360+
if (lstat(linkpath,&lst)==0)
2361+
{
2362+
if (S_ISREG(lst.st_mode))
2363+
(*action) (linkpath, false);
2364+
elseif (S_ISDIR(lst.st_mode))
2365+
walkdir(subpath,action);
2366+
}
2367+
elseif (errno!=ENOENT)
2368+
ereport(ERROR,
2369+
(errcode_for_file_access(),
2370+
errmsg("could not stat file \"%s\": %m",linkpath)));
2371+
#else
2372+
ereport(WARNING,
2373+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2374+
errmsg("this platform does not support symbolic links; ignoring \"%s\"",
2375+
subpath)));
2376+
#endif
2377+
}
2378+
}
2379+
FreeDir(dir);
2380+
2381+
(*action) (path, true);
2382+
}

‎src/include/storage/fd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ extern intpg_fsync_writethrough(int fd);
105105
externintpg_fdatasync(intfd);
106106
externintpg_flush_data(intfd,off_toffset,off_tamount);
107107
externvoidfsync_fname(char*fname,boolisdir);
108+
externvoidpre_sync_fname(char*fname,boolisdir);
109+
externvoidwalkdir(char*path,void (*action) (char*fname,boolisdir));
108110

109111
/* Filename components for OpenTemporaryFile */
110112
#definePG_TEMP_FILES_DIR "pgsql_tmp"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp