Skip to content

Commit 6dc7760

Browse files
committed
Add support for wal_fsync_writethrough for Darwin, and restructure the
code to better handle writethrough. Chris Campbell
1 parent e9b33ed commit 6dc7760

File tree

7 files changed

+79
-29
lines changed

7 files changed

+79
-29
lines changed

doc/src/sgml/runtime.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.319 2005/05/15 00:26:18 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.320 2005/05/20 14:53:25 momjian Exp $
33
-->
44

55
<chapter Id="runtime">
@@ -1595,7 +1595,7 @@ SET ENABLE_SEQSCAN TO OFF;
15951595
values are
15961596
<literal>fsync</> (call <function>fsync()</> at each commit),
15971597
<literal>fdatasync</> (call <function>fdatasync()</> at each commit),
1598-
<literal>fsync_writethrough</> (call <function>_commit()</> at each commit on Windows),
1598+
<literal>fsync_writethrough</> (force write-through of any disk write cache),
15991599
<literal>open_sync</> (write WAL files with <function>open()</> option <symbol>O_SYNC</>), and
16001600
<literal>open_datasync</> (write WAL files with <function>open()</> option <symbol>O_DSYNC</>).
16011601
Not all of these choices are available on all platforms.

src/backend/access/transam/xlog.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.192 2005/05/19 21:35:45 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.193 2005/05/20 14:53:25 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -51,11 +51,6 @@
5151
* default method. We assume that fsync() is always available, and that
5252
* configure determined whether fdatasync() is.
5353
*/
54-
#define SYNC_METHOD_FSYNC 0
55-
#define SYNC_METHOD_FDATASYNC 1
56-
#define SYNC_METHOD_OPEN 2 /* used for both O_SYNC and
57-
* O_DSYNC */
58-
5954
#if defined(O_SYNC)
6055
#define OPEN_SYNC_FLAG O_SYNC
6156
#else
@@ -79,21 +74,19 @@
7974
#define DEFAULT_SYNC_METHOD_STR "open_datasync"
8075
#define DEFAULT_SYNC_METHOD SYNC_METHOD_OPEN
8176
#define DEFAULT_SYNC_FLAGBIT OPEN_DATASYNC_FLAG
82-
#else
83-
#if defined(HAVE_FDATASYNC)
77+
#elif defined(HAVE_FDATASYNC)
8478
#define DEFAULT_SYNC_METHOD_STR "fdatasync"
8579
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FDATASYNC
8680
#define DEFAULT_SYNC_FLAGBIT 0
87-
#else
88-
#ifndef FSYNC_IS_WRITE_THROUGH
81+
#elif !defined(HAVE_FSYNC_WRITETHROUGH_ONLY)
8982
#define DEFAULT_SYNC_METHOD_STR "fsync"
83+
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC
84+
#define DEFAULT_SYNC_FLAGBIT 0
9085
#else
9186
#define DEFAULT_SYNC_METHOD_STR "fsync_writethrough"
92-
#endif
93-
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC
87+
#define DEFAULT_SYNC_METHOD SYNC_METHOD_FSYNC_WRITETHROUGH
9488
#define DEFAULT_SYNC_FLAGBIT 0
9589
#endif
96-
#endif
9790

9891

9992
/* User-settable parameters */
@@ -122,7 +115,7 @@ bool XLOG_DEBUG = false;
122115

123116

124117
/* these are derived from XLOG_sync_method by assign_xlog_sync_method */
125-
static int sync_method = DEFAULT_SYNC_METHOD;
118+
int sync_method = DEFAULT_SYNC_METHOD;
126119
static int open_sync_bit = DEFAULT_SYNC_FLAGBIT;
127120

128121
#define XLOG_SYNC_BIT (enableFsync ? open_sync_bit : 0)
@@ -5249,16 +5242,18 @@ assign_xlog_sync_method(const char *method, bool doit, GucSource source)
52495242
int new_sync_method;
52505243
int new_sync_bit;
52515244

5252-
#ifndef FSYNC_IS_WRITE_THROUGH
52535245
if (pg_strcasecmp(method, "fsync") == 0)
5254-
#else
5255-
/* Win32 fsync() == _commit(), which writes through a write cache */
5256-
if (pg_strcasecmp(method, "fsync_writethrough") == 0)
5257-
#endif
52585246
{
52595247
new_sync_method = SYNC_METHOD_FSYNC;
52605248
new_sync_bit = 0;
52615249
}
5250+
#ifdef HAVE_FSYNC_WRITETHROUGH
5251+
else if (pg_strcasecmp(method, "fsync_writethrough") == 0)
5252+
{
5253+
new_sync_method = SYNC_METHOD_FSYNC_WRITETHROUGH;
5254+
new_sync_bit = 0;
5255+
}
5256+
#endif
52625257
#ifdef HAVE_FDATASYNC
52635258
else if (pg_strcasecmp(method, "fdatasync") == 0)
52645259
{
@@ -5328,12 +5323,21 @@ issue_xlog_fsync(void)
53285323
switch (sync_method)
53295324
{
53305325
case SYNC_METHOD_FSYNC:
5331-
if (pg_fsync(openLogFile) != 0)
5326+
if (pg_fsync_no_writethrough(openLogFile) != 0)
53325327
ereport(PANIC,
53335328
(errcode_for_file_access(),
53345329
errmsg("could not fsync log file %u, segment %u: %m",
53355330
openLogId, openLogSeg)));
53365331
break;
5332+
#ifdef HAVE_FSYNC_WRITETHROUGH
5333+
case SYNC_METHOD_FSYNC_WRITETHROUGH:
5334+
if (pg_fsync_writethrough(openLogFile) != 0)
5335+
ereport(PANIC,
5336+
(errcode_for_file_access(),
5337+
errmsg("could not fsync write-through log file %u, segment %u: %m",
5338+
openLogId, openLogSeg)));
5339+
break;
5340+
#endif
53375341
#ifdef HAVE_FDATASYNC
53385342
case SYNC_METHOD_FDATASYNC:
53395343
if (pg_fdatasync(openLogFile) != 0)

src/backend/storage/file/fd.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.115 2004/12/31 22:00:51 pgsql Exp $
10+
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.116 2005/05/20 14:53:26 momjian Exp $
1111
*
1212
* NOTES:
1313
*
@@ -232,17 +232,51 @@ static void RemovePgTempFilesInDir(const char *tmpdirname);
232232

233233

234234
/*
235-
* pg_fsync --- same as fsync except does nothing if enableFsync is off
235+
* pg_fsync --- do fsync with or without writethrough
236236
*/
237237
int
238238
pg_fsync(int fd)
239+
{
240+
#ifndef HAVE_FSYNC_WRITETHROUGH_ONLY
241+
if (sync_method != SYNC_METHOD_FSYNC_WRITETHROUGH)
242+
return pg_fsync_no_writethrough(fd);
243+
else
244+
#endif
245+
return pg_fsync_writethrough(fd);
246+
}
247+
248+
249+
/*
250+
* pg_fsync_no_writethrough --- same as fsync except does nothing if
251+
* enableFsync is off
252+
*/
253+
int
254+
pg_fsync_no_writethrough(int fd)
239255
{
240256
if (enableFsync)
241257
return fsync(fd);
242258
else
243259
return 0;
244260
}
245261

262+
/*
263+
* pg_fsync_writethrough
264+
*/
265+
int
266+
pg_fsync_writethrough(int fd)
267+
{
268+
if (enableFsync)
269+
#ifdef WIN32
270+
return _commit(fd);
271+
#elif defined(__darwin__)
272+
return (fcntl(fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
273+
#else
274+
return -1;
275+
#endif
276+
else
277+
return 0;
278+
}
279+
246280
/*
247281
* pg_fdatasync --- same as fdatasync except does nothing if enableFsync is off
248282
*

src/include/access/xlog.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.60 2005/04/28 21:47:17 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.61 2005/05/20 14:53:26 momjian Exp $
1010
*/
1111
#ifndef XLOG_H
1212
#define XLOG_H
@@ -75,6 +75,13 @@ typedef struct XLogRecord
7575
*/
7676
#define XLOG_NO_TRAN XLR_INFO_MASK
7777

78+
/* Sync methods */
79+
#define SYNC_METHOD_FSYNC 0
80+
#define SYNC_METHOD_FDATASYNC 1
81+
#define SYNC_METHOD_OPEN 2 /* for O_SYNC and O_DSYNC */
82+
#define SYNC_METHOD_FSYNC_WRITETHROUGH 3
83+
extern int sync_method;
84+
7885
/*
7986
* List of these structs is used to pass data to XLogInsert().
8087
*

src/include/port/darwin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
#define __darwin__ 1
2+
3+
#define HAVE_FSYNC_WRITETHROUGH
4+

src/include/port/win32.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.44 2005/03/24 04:36:19 momjian Exp $ */
1+
/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.45 2005/05/20 14:53:26 momjian Exp $ */
22

33
/* undefine and redefine after #include */
44
#undef mkdir
@@ -16,8 +16,8 @@
1616
#define mkdir(a,b) mkdir(a)
1717

1818

19-
#define fsync(a) _commit(a)
20-
#define FSYNC_IS_WRITE_THROUGH
19+
#define HAVE_FSYNC_WRITETHROUGH
20+
#define HAVE_FSYNC_WRITETHROUGH_ONLY
2121
#define ftruncate(a,b) chsize(a,b)
2222

2323
#define USES_WINSOCK

src/include/storage/fd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/fd.h,v 1.50 2004/12/31 22:03:42 pgsql Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/fd.h,v 1.51 2005/05/20 14:53:26 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -89,6 +89,8 @@ extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
8989
SubTransactionId parentSubid);
9090
extern void RemovePgTempFiles(void);
9191
extern int pg_fsync(int fd);
92+
extern int pg_fsync_no_writethrough(int fd);
93+
extern int pg_fsync_writethrough(int fd);
9294
extern int pg_fdatasync(int fd);
9395

9496
/* Filename components for OpenTemporaryFile */

0 commit comments

Comments
 (0)