8000 Force default wal_sync_method to be fdatasync on Linux. · justtesting112233/postgres@f3224e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f3224e0

Browse files
committed
Force default wal_sync_method to be fdatasync on Linux.
Recent versions of the Linux system header files cause xlogdefs.h to believe that open_datasync should be the default sync method, whereas formerly fdatasync was the default on Linux. open_datasync is a bad choice, first because it doesn't actually outperform fdatasync (in fact the reverse), and second because we try to use O_DIRECT with it, causing failures on certain filesystems (e.g., ext4 with data=journal option). This part of the patch is largely per a proposal from Marti Raudsepp. More extensive changes are likely to follow in HEAD, but this is as much change as we want to back-patch. Also clean up confusing code and incorrect documentation surrounding the fsync_writethrough option. Those changes shouldn't result in any actual behavioral change, but I chose to back-patch them anyway to keep the branches looking similar in this area. In 9.0 and HEAD, also do some copy-editing on the WAL Reliability documentation section. Back-patch to all supported branches, since any of them might get used on modern Linux versions.
1 parent 234ad01 commit f3224e0

File tree

6 files changed

+31
-19
lines changed

6 files changed

+31
-19
lines changed

doc/src/sgml/config.sgml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,12 +1442,12 @@ SET ENABLE_SEQSCAN TO OFF;
14421442
</listitem>
14431443
<listitem>
14441444
<para>
1445-
<literal>fsync_writethrough</> (call <function>fsync()</> at each commit, forcing write-through of any disk write cache)
1445+
<literal>fsync</> (call <function>fsync()</> at each commit)
14461446
</para>
14471447
</listitem>
14481448
<listitem>
14491449
<para>
1450-
<literal>fsync</> (call <function>fsync()</> at each commit)
1450+
<literal>fsync_writethrough</> (call <function>fsync()</> at each commit, forcing write-through of any disk write cache)
14511451
</para>
14521452
</listitem>
14531453
<listitem>
@@ -1457,12 +1457,11 @@ SET ENABLE_SEQSCAN TO OFF;
14571457
</listitem>
14581458
</itemizedlist>
14591459
<para>
1460-
Not all of these choices are available on all platforms.
14611460
The <literal>open_</>* options also use <literal>O_DIRECT</> if available.
1461+
Not all of these choices are available on all platforms.
14621462
The default is the first method in the above list that is supported
1463-
by the platform.
1464-
The default is the first method in the above list that is supported
1465-
by the platform. The default is not necessarily ideal; it might be
1463+
by the platform, except that <literal>fdatasync</> is the default on
1464+
Linux. The default is not necessarily ideal; it might be
14661465
necessary to change this setting or other aspects of your system
14671466
configuration in order to create a crash-safe configuration or
14681467
achieve optimal performance.

src/backend/storage/file/fd.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,13 @@ static void RemovePgTempFilesInDir(const char *tmpdirname);
256256
int
257257
pg_fsync(int fd)
258258
{
259-
#ifndef HAVE_FSYNC_WRITETHROUGH_ONLY
260-
if (sync_method != SYNC_METHOD_FSYNC_WRITETHROUGH)
261-
return pg_fsync_no_writethrough(fd);
259+
/* #if is to skip the sync_method test if there's no need for it */
260+
#if defined(HAVE_FSYNC_WRITETHROUGH) && !defined(FSYNC_WRITETHROUGH_IS_FSYNC)
261+
if (sync_method == SYNC_METHOD_FSYNC_WRITETHROUGH)
262+
return pg_fsync_writethrough(fd);
262263
else
263264
#endif
264-
return pg_fsync_writethrough(fd);
265+
return pg_fsync_no_writethrough(fd);
265266
}
266267

267268

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
#wal_sync_method = fsync # the default is the first option
154154
# supported by the operating system:
155155
# open_datasync
156-
# fdatasync
156+
# fdatasync (default on Linux)
157157
# fsync
158158
# fsync_writethrough
159159
# open_sync

src/include/access/xlogdefs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ typedef uint32 TimeLineID;
110110
#endif
111111
#endif
112112

113-
#if defined(OPEN_DATASYNC_FLAG)
113+
#if defined(PLATFORM_DEFAULT_SYNC_METHOD)
114+
#define DEFAULT_SYNC_METHOD PLATFORM_DEFAULT_SYNC_METHOD
115+
#elif defined(OPEN_DATASYNC_FLAG)
114116
#define DEFAULT_SYNC_METHOD SYNC_METHOD_OPEN_DSYNC
115117
#elif defined(HAVE_FDATASYNC)
116118
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FDATASYNC
117-
#elif defined(HAVE_FSYNC_WRITETHROUGH_ONLY)
118-
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC_WRITETHROUGH
119119
#else
120120
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC
121121
#endif

src/include/port/linux.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@
1212
* to have a kernel version test here.
1313
*/
1414
#define HAVE_LINUX_EIDRM_BUG
15+
16+
/*
17+
* Set the default wal_sync_method to fdatasync. With recent Linux versions,
18+
* xlogdefs.h's normal rules will prefer open_datasync, which (a) doesn't
19+
* perform better and (b) causes outright failures on ext4 data=journal
20+
* filesystems, because those don't support O_DIRECT.
21+
*/
22+
#define PLATFORM_DEFAULT_SYNC_METHOD SYNC_METHOD_FDATASYNC

src/include/port/win32.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@
3434
/* Must be here to avoid conflicting with prototype in windows.h */
3535
#define mkdir(a,b) mkdir(a)
3636

37-
#define HAVE_FSYNC_WRITETHROUGH
38-
#define HAVE_FSYNC_WRITETHROUGH_ONLY
3937
#define ftruncate(a,b) chsize(a,b)
38+
39+
/* Windows doesn't have fsync() as such, use _commit() */
40+
#define fsync(fd) _commit(fd)
41+
4042
/*
41-
* Even though we don't support 'fsync' as a wal_sync_method,
42-
* we do fsync() a few other places where _commit() is just fine.
43+
* For historical reasons, we allow setting wal_sync_method to
44+
* fsync_writethrough on Windows, even though it's really identical to fsync
45+
* (both code paths wind up at _commit()).
4346
*/
44-
#define fsync(fd) _commit(fd)
47+
#define HAVE_FSYNC_WRITETHROUGH
48+
#define FSYNC_WRITETHROUGH_IS_FSYNC
4549

4650
#define USES_WINSOCK
4751

0 commit comments

Comments
 (0)
0