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

Commit 1012492

Browse files
committed
Make CREATE DATABASE safe against losing whole files by fsyncing the
directory and not just the individual files. Back-patch to 8.1 -- before that we just called "cp -r" and never fsynced anything anyways.
1 parent a05af1d commit 1012492

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/port/copydir.c

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* as a service.
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/port/copydir.c,v 1.24 2010/01/02 16:58:13 momjian Exp $
14+
* $PostgreSQL: pgsql/src/port/copydir.c,v 1.25 2010/02/14 17:50:52 stark Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -50,6 +50,7 @@ copydir(char *fromdir, char *todir, bool recurse)
5050
{
5151
DIR *xldir;
5252
struct dirent *xlde;
53+
int dirfd;
5354
char fromfile[MAXPGPATH];
5455
char tofile[MAXPGPATH];
5556

@@ -91,6 +92,26 @@ copydir(char *fromdir, char *todir, bool recurse)
9192
}
9293

9394
FreeDir(xldir);
95+
96+
/*
97+
* fsync the directory to make sure not just the data but also the
98+
* new directory file entries have reached the disk. While needed
99+
* by most filesystems, the window got bigger with newer ones like
100+
* ext4.
101+
*/
102+
dirfd = BasicOpenFile(todir,
103+
O_RDONLY | PG_BINARY,
104+
S_IRUSR | S_IWUSR);
105+
if(dirfd == -1)
106+
ereport(ERROR,
107+
(errcode_for_file_access(),
108+
errmsg("could not open directory for fsync \"%s\": %m", todir)));
109+
110+
if(pg_fsync(dirfd) == -1)
111+
ereport(ERROR,
112+
(errcode_for_file_access(),
113+
errmsg("could not fsync directory \"%s\": %m", todir)));
114+
close(dirfd);
94115
}
95116

96117
/*

0 commit comments

Comments
 (0)