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

Skip to content

Commit 6b2c0ea

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 0ae63a4 commit 6b2c0ea

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

doc/src/sgml/config.sgml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,12 +1347,12 @@ SET ENABLE_SEQSCAN TO OFF;
13471347
</listitem>
13481348
<listitem>
13491349
<para>
1350-
<literal>fsync_writethrough</> (call <function>fsync()</> at each commit, forcing write-through of any disk write cache)
1350+
<literal>fsync</> (call <function>fsync()</> at each commit)
13511351
</para>
13521352
</listitem>
13531353
<listitem>
13541354
<para>
1355-
<literal>fsync</> (call <function>fsync()</> at each commit)
1355+
<literal>fsync_writethrough</> (call <function>fsync()</> at each commit, forcing write-through of any disk write cache)
13561356
</para>
13571357
</listitem>
13581358
<listitem>
@@ -1363,7 +1363,9 @@ SET ENABLE_SEQSCAN TO OFF;
13631363
</itemizedlist>
13641364
<para>
13651365
Not all of these choices are available on all platforms.
1366-
The default is the first method in the above list that is supported.
1366+
The default is the first method in the above list that is supported
1367+
by the platform, except that <literal>fdatasync</> is the default on
1368+
Linux.
13671369
This option can be set at server start or in the
13681370
<filename>postgresql.conf</filename> file.
13691371
</para>

src/backend/access/transam/xlog.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,18 @@
9292
#endif
9393
#endif
9494

95-
#if defined(OPEN_DATASYNC_FLAG)
95+
#if defined(PLATFORM_DEFAULT_SYNC_METHOD)
96+
#define DEFAULT_SYNC_METHOD_STR PLATFORM_DEFAULT_SYNC_METHOD_STR
97+
#define DEFAULT_SYNC_METHOD PLATFORM_DEFAULT_SYNC_METHOD
98+
#define DEFAULT_SYNC_FLAGBIT PLATFORM_DEFAULT_SYNC_FLAGBIT
99+
#elif defined(OPEN_DATASYNC_FLAG)
96100
#define DEFAULT_SYNC_METHOD_STR "open_datasync"
97101
#define DEFAULT_SYNC_METHOD SYNC_METHOD_OPEN
98102
#define DEFAULT_SYNC_FLAGBIT OPEN_DATASYNC_FLAG
99103
#elif defined(HAVE_FDATASYNC)
100104
#define DEFAULT_SYNC_METHOD_STR "fdatasync"
101105
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FDATASYNC
102106
#define DEFAULT_SYNC_FLAGBIT 0
103-
#elif defined(HAVE_FSYNC_WRITETHROUGH_ONLY)
104-
#define DEFAULT_SYNC_METHOD_STR "fsync_writethrough"
105-
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC_WRITETHROUGH
106-
#define DEFAULT_SYNC_FLAGBIT 0
107107
#else
108108
#define DEFAULT_SYNC_METHOD_STR "fsync"
109109
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC

src/backend/storage/file/fd.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,13 @@ static void RemovePgTempFilesInDir(const char *tmpdirname);
237237
int
238238
pg_fsync(int fd)
239239
{
240-
#ifndef HAVE_FSYNC_WRITETHROUGH_ONLY
241-
if (sync_method != SYNC_METHOD_FSYNC_WRITETHROUGH)
242-
return pg_fsync_no_writethrough(fd);
240+
/* #if is to skip the sync_method test if there's no need for it */
241+
#if defined(HAVE_FSYNC_WRITETHROUGH) && !defined(FSYNC_WRITETHROUGH_IS_FSYNC)
242+
if (sync_method == SYNC_METHOD_FSYNC_WRITETHROUGH)
243+
return pg_fsync_writethrough(fd);
243244
else
244245
#endif
245-
return pg_fsync_writethrough(fd);
246+
return pg_fsync_no_writethrough(fd);
246247
}
247248

248249

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
#wal_sync_method = fsync # the default is the first option
139139
# supported by the operating system:
140140
# open_datasync
141-
# fdatasync
141+
# fdatasync (default on Linux)
142142
# fsync
143143
# fsync_writethrough
144144
# open_sync

src/include/port/linux.h

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

src/include/port/win32.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616
#define mkdir(a,b) mkdir(a)
1717

1818

19-
#define HAVE_FSYNC_WRITETHROUGH
20-
#define HAVE_FSYNC_WRITETHROUGH_ONLY
2119
#define ftruncate(a,b) chsize(a,b)
20+
21+
/* Windows doesn't have fsync() as such, use _commit() */
22+
#define fsync(fd) _commit(fd)
23+
2224
/*
23-
* Even though we don't support 'fsync' as a wal_sync_method,
24-
* we do fsync() a few other places where _commit() is just fine.
25+
* For historical reasons, we allow setting wal_sync_method to
26+
* fsync_writethrough on Windows, even though it's really identical to fsync
27+
* (both code paths wind up at _commit()).
2528
*/
26-
#define fsync(fd) _commit(fd)
29+
#define HAVE_FSYNC_WRITETHROUGH
30+
#define FSYNC_WRITETHROUGH_IS_FSYNC
2731

2832
#define USES_WINSOCK
2933

0 commit comments

Comments
 (0)
0