Skip to content

Commit 576477e

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 e620ee3 commit 576477e

File tree

7 files changed

+77
-62
lines changed

7 files changed

+77
-62
lines changed

doc/src/sgml/config.sgml

+19-20
Original file line numberDiff line numberDiff line change
@@ -1460,18 +1460,19 @@ SET ENABLE_SEQSCAN TO OFF;
14601460
<para>
14611461
While turning off <varname>fsync</varname> is often a performance
14621462
benefit, this can result in unrecoverable data corruption in
1463-
the event of an unexpected system shutdown or crash. Thus it
1464-
is only advisable to turn off <varname>fsync</varname> if
1463+
the event of a power failure or system crash. Thus it
1464+
is only advisable to turn off <varname>fsync</varname> if
14651465
you can easily recreate your entire database from external
14661466
data.
14671467
</para>
14681468

14691469
<para>
14701470
Examples of safe circumstances for turning off
1471-
<varname>fsync</varname> include the initial loading a new
1471+
<varname>fsync</varname> include the initial loading of a new
14721472
database cluster from a backup file, using a database cluster
1473-
for processing statistics on an hourly basis which is then
1474-
recreated, or for a reporting read-only database clone which
1473+
for processing a batch of data after which the database
1474+
will be thrown away and recreated,
1475+
or for a read-only database clone which
14751476
gets recreated frequently and is not used for failover. High
14761477
quality hardware alone is not a sufficient justification for
14771478
turning off <varname>fsync</varname>.
@@ -1554,12 +1555,12 @@ SET ENABLE_SEQSCAN TO OFF;
15541555
</listitem>
15551556
<listitem>
15561557
<para>
1557-
<literal>fsync_writethrough</> (call <function>fsync()</> at each commit, forcing write-through of any disk write cache)
1558+
<literal>fsync</> (call <function>fsync()</> at each commit)
15581559
</para>
15591560
</listitem>
15601561
<listitem>
15611562
<para>
1562-
<literal>fsync</> (call <function>fsync()</> at each commit)
1563+
<literal>fsync_writethrough</> (call <function>fsync()</> at each commit, forcing write-through of any disk write cache)
15631564
</para>
15641565
</listitem>
15651566
<listitem>
@@ -1569,16 +1570,15 @@ SET ENABLE_SEQSCAN TO OFF;
15691570
</listitem>
15701571
</itemizedlist>
15711572
<para>
1572-
Not all of these choices are available on all platforms.
15731573
The <literal>open_</>* options also use <literal>O_DIRECT</> if available.
1574+
Not all of these choices are available on all platforms.
15741575
The default is the first method in the above list that is supported
1575-
by the platform. The default is not necessarily ideal; it might be
1576+
by the platform, except that <literal>fdatasync</> is the default on
1577+
Linux. The default is not necessarily ideal; it might be
15761578
necessary to change this setting or other aspects of your system
15771579
configuration in order to create a crash-safe configuration or
15781580
achieve optimal performance.
15791581
These aspects are discussed in <xref linkend="wal-reliability">.
1580-
The utility <filename>src/tools/fsync</> in the PostgreSQL source tree
1581-
can do performance testing of various fsync methods.
15821582
This parameter can only be set in the <filename>postgresql.conf</>
15831583
file or on the server command line.
15841584
</para>
@@ -1686,21 +1686,20 @@ SET ENABLE_SEQSCAN TO OFF;
16861686
When the commit data for a transaction is flushed to disk, any
16871687
additional commits ready at that time are also flushed out.
16881688
<varname>commit_delay</varname> adds a time delay, set in
1689-
microseconds, before writing some commit records to the WAL
1690-
buffer and flushing the buffer out to disks. A nonzero delay
1691-
can allow more transactions to be committed with only one call
1692-
to the active <varname>wal_sync_method</varname>, if
1689+
microseconds, before a transaction attempts to
1690+
flush the WAL buffer out to disk. A nonzero delay can allow more
1691+
transactions to be committed with only one flush operation, if
16931692
system load is high enough that additional transactions become
16941693
ready to commit within the given interval. But the delay is
16951694
just wasted if no other transactions become ready to
16961695
commit. Therefore, the delay is only performed if at least
16971696
<varname>commit_siblings</varname> other transactions are
16981697
active at the instant that a server process has written its
1699-
commit record. The default is zero (no delay). Since
1700-
all pending commit data flushes are written at every flush
1701-
regardless of this setting, it is rare that adding delay to
1702-
that by increasing this parameter will actually improve commit
1703-
performance.
1698+
commit record.
1699+
The default <varname>commit_delay</> is zero (no delay).
1700+
Since all pending commit data will be written at every flush
1701+
regardless of this setting, it is rare that adding delay
1702+
by increasing this parameter will actually improve performance.
17041703
</para>
17051704
</listitem>
17061705
</varlistentry>

doc/src/sgml/wal.sgml

+32-29
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</para>
2828

2929
<para>
30-
While forcing data periodically to the disk platters might seem like
30+
While forcing data to the disk platters periodically might seem like
3131
a simple operation, it is not. Because disk drives are dramatically
3232
slower than main memory and CPUs, several layers of caching exist
3333
between the computer's main memory and the disk platters.
@@ -48,7 +48,7 @@
4848
some later time. Such caches can be a reliability hazard because the
4949
memory in the disk controller cache is volatile, and will lose its
5050
contents in a power failure. Better controller cards have
51-
<firstterm>battery-backed unit</> (<acronym>BBU</>) caches, meaning
51+
<firstterm>battery-backup units</> (<acronym>BBU</>s), meaning
5252
the card has a battery that
5353
maintains power to the cache in case of system power loss. After power
5454
is restored the data will be written to the disk drives.
@@ -57,15 +57,10 @@
5757
<para>
5858
And finally, most disk drives have caches. Some are write-through
5959
while some are write-back, and the same concerns about data loss
60-
exist for write-back drive caches as exist for disk controller
60+
exist for write-back drive caches as for disk controller
6161
caches. Consumer-grade IDE and SATA drives are particularly likely
62-
to have write-back caches that will not survive a power failure,
63-
though <acronym>ATAPI-6</> introduced a drive cache flush command
64-
(<command>FLUSH CACHE EXT</>) that some file systems use, e.g.
65-
<acronym>ZFS</>, <acronym>ext4</>. (The SCSI command
66-
<command>SYNCHRONIZE CACHE</> has long been available.) Many
67-
solid-state drives (SSD) also have volatile write-back caches, and
68-
many do not honor cache flush commands by default.
62+
to have write-back caches that will not survive a power failure. Many
63+
solid-state drives (SSD) also have volatile write-back caches.
6964
</para>
7065

7166
<para>
@@ -81,7 +76,7 @@
8176
a <literal>*</> next to <literal>Write cache</>. <command>hdparm -W</>
8277
can be used to turn off write caching. SCSI drives can be queried
8378
using <ulink url="http://sg.danny.cz/sg/sdparm.html"><application>sdparm</></ulink>.
84-
for SCSI drives. Use <command>sdparm --get=WCE</command> to check
79+
Use <command>sdparm --get=WCE</command> to check
8580
whether the write cache is enabled and <command>sdparm --clear=WCE</>
8681
to disable it.
8782
</para>
@@ -107,35 +102,40 @@
107102
<listitem>
108103
<para>
109104
On <productname>Windows</>, if <varname>wal_sync_method</> is
110-
<literal>open_datasync</> (the default), write caching is disabled
111-
by unchecking <literal>My Computer\Open\{select disk drive}\Properties\Hardware\Properties\Policies\Enable write caching on the disk</>.
112-
Alternatively, set <varname>wal_sync_method</varname> to <literal>fsync</> or <literal>fsync_writethrough</>, which never do write caching.
105+
<literal>open_datasync</> (the default), write caching can be disabled
106+
by unchecking <literal>My Computer\Open\<replaceable>disk drive</>\Properties\Hardware\Properties\Policies\Enable write caching on the disk</>.
107+
Alternatively, set <varname>wal_sync_method</varname> to
108+
<literal>fsync</> or <literal>fsync_writethrough</>, which prevent
109+
write caching.
113110
</para>
114111
</listitem>
115112

116113
<listitem>
117114
<para>
118-
On <productname>MacOS X</productname>, write caching can be disabled by
115+
On <productname>Mac OS X</productname>, write caching can be prevented by
119116
setting <varname>wal_sync_method</> to <literal>fsync_writethrough</>.
120117
</para>
121118
</listitem>
122119
</itemizedlist>
123120

124121
<para>
125-
Many file systems that use write barriers (e.g. <acronym>ZFS</>,
126-
<acronym>ext4</>) internally use <command>FLUSH CACHE EXT</> or
127-
<command>SYNCHRONIZE CACHE</> commands to flush data to the platters on
128-
write-back-enabled drives. Unfortunately, such write barrier file
129-
systems behave suboptimally when combined with battery-backed unit
122+
Recent SATA drives (those following <acronym>ATAPI-6</> or later)
123+
offer a drive cache flush command (<command>FLUSH CACHE EXT</>),
124+
while SCSI drives have long supported a similar command
125+
<command>SYNCHRONIZE CACHE</>. These commands are not directly
126+
accessible to <productname>PostgreSQL</>, but some file systems
127+
(e.g., <acronym>ZFS</>, <acronym>ext4</>) can use them to flush
128+
data to the platters on write-back-enabled drives. Unfortunately, such
129+
file systems behave suboptimally when combined with battery-backup unit
130130
(<acronym>BBU</>) disk controllers. In such setups, the synchronize
131-
command forces all data from the BBU to the disks, eliminating much
132-
of the benefit of the BBU. You can run the utility
131+
command forces all data from the controller cache to the disks,
132+
eliminating much of the benefit of the BBU. You can run the utility
133133
<filename>src/tools/fsync</> in the PostgreSQL source tree to see
134134
if you are affected. If you are affected, the performance benefits
135-
of the BBU cache can be regained by turning off write barriers in
135+
of the BBU can be regained by turning off write barriers in
136136
the file system or reconfiguring the disk controller, if that is
137137
an option. If write barriers are turned off, make sure the battery
138-
remains active; a faulty battery can potentially lead to data loss.
138+
remains functional; a faulty battery can potentially lead to data loss.
139139
Hopefully file system and disk controller designers will eventually
140140
address this suboptimal behavior.
141141
</para>
@@ -148,6 +148,8 @@
148148
ensure data integrity. Avoid disk controllers that have non-battery-backed
149149
write caches. At the drive level, disable write-back caching if the
150150
drive cannot guarantee the data will be written before shutdown.
151+
If you use SSDs, be aware that many of these do not honor cache flush
152+
commands by default.
151153
You can test for reliable I/O subsystem behavior using <ulink
152154
url="http://brad.livejournal.com/2116715.html"><filename>diskchecker.pl</filename></ulink>.
153155
</para>
@@ -157,16 +159,17 @@
157159
operations themselves. Disk platters are divided into sectors,
158160
commonly 512 bytes each. Every physical read or write operation
159161
processes a whole sector.
160-
When a write request arrives at the drive, it might be for 512 bytes,
161-
1024 bytes, or 8192 bytes, and the process of writing could fail due
162+
When a write request arrives at the drive, it might be for some multiple
163+
of 512 bytes (<productname>PostgreSQL</> typically writes 8192 bytes, or
164+
16 sectors, at a time), and the process of writing could fail due
162165
to power loss at any time, meaning some of the 512-byte sectors were
163-
written, and others were not. To guard against such failures,
166+
written while others were not. To guard against such failures,
164167
<productname>PostgreSQL</> periodically writes full page images to
165168
permanent WAL storage <emphasis>before</> modifying the actual page on
166169
disk. By doing this, during crash recovery <productname>PostgreSQL</> can
167-
restore partially-written pages. If you have a battery-backed disk
170+
restore partially-written pages from WAL. If you have a battery-backed disk
168171
controller or file-system software that prevents partial page writes
169-
(e.g., ZFS), you can turn off this page imaging by turning off the
172+
(e.g., ZFS), you can safely turn off this page imaging by turning off the
170173
<xref linkend="guc-full-page-writes"> parameter.
171174
</para>
172175
</sect1>

src/backend/storage/file/fd.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,13 @@ static bool looks_like_temp_rel_name(const char *name);
260260
int
261261
pg_fsync(int fd)
262262
{
263-
#ifndef HAVE_FSYNC_WRITETHROUGH_ONLY
264-
if (sync_method != SYNC_METHOD_FSYNC_WRITETHROUGH)
265-
return pg_fsync_no_writethrough(fd);
263+
/* #if is to skip the sync_method test if there's no need for it */
264+
#if defined(HAVE_FSYNC_WRITETHROUGH) && !defined(FSYNC_WRITETHROUGH_IS_FSYNC)
265+
if (sync_method == SYNC_METHOD_FSYNC_WRITETHROUGH)
266+
return pg_fsync_writethrough(fd);
266267
else
267268
#endif
268-
return pg_fsync_writethrough(fd);
269+
return pg_fsync_no_writethrough(fd);
269270
}
270271

271272

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
#wal_sync_method = fsync # the default is the first option
158158
# supported by the operating system:
159159
# open_datasync
160-
# fdatasync
160+
# fdatasync (default on Linux)
161161
# fsync
162162
# fsync_writethrough
163163
# open_sync

src/include/access/xlogdefs.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ typedef uint32 TimeLineID;
123123
#endif
124124
#endif
125125

126-
#if defined(OPEN_DATASYNC_FLAG)
126+
#if defined(PLATFORM_DEFAULT_SYNC_METHOD)
127+
#define DEFAULT_SYNC_METHOD PLATFORM_DEFAULT_SYNC_METHOD
128+
#elif defined(OPEN_DATASYNC_FLAG)
127129
#define DEFAULT_SYNC_METHOD SYNC_METHOD_OPEN_DSYNC
128130
#elif defined(HAVE_FDATASYNC)
129131
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FDATASYNC
130-
#elif defined(HAVE_FSYNC_WRITETHROUGH_ONLY)
131-
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC_WRITETHROUGH
132132
#else
133133
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC
134134
#endif

src/include/port/linux.h

+8
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

+9-5
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)