77 * Portions Copyright (c) 1996-2008, 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.308 2008/05/13 20:53:52 mha Exp $
10+ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.309 2008/05/14 14:02:57 mha Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -85,16 +85,11 @@ boolXLOG_DEBUG = false;
8585 */
8686#define XLOGfileslop (2*CheckPointSegments + 1)
8787
88-
89- /* these are derived from XLOG_sync_method by assign_xlog_sync_method */
90- int sync_method = DEFAULT_SYNC_METHOD ;
91- static int open_sync_bit = DEFAULT_SYNC_FLAGBIT ;
92-
93- #define XLOG_SYNC_BIT (enableFsync ? open_sync_bit : 0)
94-
9588/*
9689 * GUC support
9790 */
91+ int sync_method = DEFAULT_SYNC_METHOD ;
92+
9893const struct config_enum_entry sync_method_options []= {
9994{"fsync" ,SYNC_METHOD_FSYNC },
10095#ifdef HAVE_FSYNC_WRITETHROUGH
@@ -444,6 +439,7 @@ static void pg_start_backup_callback(int code, Datum arg);
444439static bool read_backup_label (XLogRecPtr * checkPointLoc ,
445440XLogRecPtr * minRecoveryLoc );
446441static void rm_redo_error_callback (void * arg );
442+ static int get_sync_bit (int method );
447443
448444
449445/*
@@ -1960,7 +1956,7 @@ XLogFileInit(uint32 log, uint32 seg,
19601956 */
19611957if (* use_existent )
19621958{
1963- fd = BasicOpenFile (path ,O_RDWR |PG_BINARY |XLOG_SYNC_BIT ,
1959+ fd = BasicOpenFile (path ,O_RDWR |PG_BINARY |get_sync_bit ( sync_method ) ,
19641960S_IRUSR |S_IWUSR );
19651961if (fd < 0 )
19661962{
@@ -1986,7 +1982,7 @@ XLogFileInit(uint32 log, uint32 seg,
19861982
19871983unlink (tmppath );
19881984
1989- /* do not useXLOG_SYNC_BIT here --- want to fsync only at end of fill */
1985+ /* do not useget_sync_bit() here --- want to fsync only at end of fill */
19901986fd = BasicOpenFile (tmppath ,O_RDWR |O_CREAT |O_EXCL |PG_BINARY ,
19911987S_IRUSR |S_IWUSR );
19921988if (fd < 0 )
@@ -2064,7 +2060,7 @@ XLogFileInit(uint32 log, uint32 seg,
20642060* use_existent = false;
20652061
20662062/* Now open original target segment (might not be file I just made) */
2067- fd = BasicOpenFile (path ,O_RDWR |PG_BINARY |XLOG_SYNC_BIT ,
2063+ fd = BasicOpenFile (path ,O_RDWR |PG_BINARY |get_sync_bit ( sync_method ) ,
20682064S_IRUSR |S_IWUSR );
20692065if (fd < 0 )
20702066ereport (ERROR ,
@@ -2115,7 +2111,7 @@ XLogFileCopy(uint32 log, uint32 seg,
21152111
21162112unlink (tmppath );
21172113
2118- /* do not useXLOG_SYNC_BIT here --- want to fsync only at end of fill */
2114+ /* do not useget_sync_bit() here --- want to fsync only at end of fill */
21192115fd = BasicOpenFile (tmppath ,O_RDWR |O_CREAT |O_EXCL |PG_BINARY ,
21202116S_IRUSR |S_IWUSR );
21212117if (fd < 0 )
@@ -2297,7 +2293,7 @@ XLogFileOpen(uint32 log, uint32 seg)
22972293
22982294XLogFilePath (path ,ThisTimeLineID ,log ,seg );
22992295
2300- fd = BasicOpenFile (path ,O_RDWR |PG_BINARY |XLOG_SYNC_BIT ,
2296+ fd = BasicOpenFile (path ,O_RDWR |PG_BINARY |get_sync_bit ( sync_method ) ,
23012297S_IRUSR |S_IWUSR );
23022298if (fd < 0 )
23032299ereport (PANIC ,
@@ -3653,7 +3649,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
36533649
36543650unlink (tmppath );
36553651
3656- /* do not useXLOG_SYNC_BIT here --- want to fsync only at end of fill */
3652+ /* do not useget_sync_bit() here --- want to fsync only at end of fill */
36573653fd = BasicOpenFile (tmppath ,O_RDWR |O_CREAT |O_EXCL ,
36583654S_IRUSR |S_IWUSR );
36593655if (fd < 0 )
@@ -6331,14 +6327,17 @@ xlog_outrec(StringInfo buf, XLogRecord *record)
63316327
63326328
63336329/*
6334- * GUC support
6330+ * Return the (possible) sync flag used for opening a file, depending on the
6331+ * value of the GUC wal_sync_method.
63356332 */
6336- bool
6337- assign_xlog_sync_method (int new_sync_method , bool doit , GucSource source )
6333+ static int
6334+ get_sync_bit (int method )
63386335{
6339- int new_sync_bit = 0 ;
6336+ /* If fsync is disabled, never open in sync mode */
6337+ if (!enableFsync )
6338+ return 0 ;
63406339
6341- switch (new_sync_method )
6340+ switch (method )
63426341{
63436342/*
63446343 * Values for these sync options are defined even if they are not
@@ -6349,32 +6348,36 @@ assign_xlog_sync_method(int new_sync_method, bool doit, GucSource source)
63496348case SYNC_METHOD_FSYNC :
63506349case SYNC_METHOD_FSYNC_WRITETHROUGH :
63516350case SYNC_METHOD_FDATASYNC :
6352- new_sync_bit = 0 ;
6353- break ;
6351+ return 0 ;
63546352#ifdef OPEN_SYNC_FLAG
63556353case SYNC_METHOD_OPEN :
6356- new_sync_bit = OPEN_SYNC_FLAG ;
6357- break ;
6354+ return OPEN_SYNC_FLAG ;
63586355#endif
63596356#ifdef OPEN_DATASYNC_FLAG
63606357case SYNC_METHOD_OPEN_DSYNC :
6361- new_sync_bit = OPEN_DATASYNC_FLAG ;
6362- break ;
6358+ return OPEN_DATASYNC_FLAG ;
63636359#endif
63646360default :
63656361/*
63666362 * This "can never happen", since the available values in
63676363 * new_sync_method are controlled by the available enum
63686364 * options.
63696365 */
6370- elog (PANIC ,"unrecognized wal_sync_method: %d" ,new_sync_method );
6371- break ;
6366+ elog (PANIC ,"unrecognized wal_sync_method: %d" ,method );
6367+ return 0 ; /* silence warning */
63726368}
6369+ }
63736370
6371+ /*
6372+ * GUC support
6373+ */
6374+ bool
6375+ assign_xlog_sync_method (int new_sync_method ,bool doit ,GucSource source )
6376+ {
63746377if (!doit )
63756378return true;
63766379
6377- if (sync_method != new_sync_method || open_sync_bit != new_sync_bit )
6380+ if (sync_method != new_sync_method )
63786381{
63796382/*
63806383 * To ensure that no blocks escape unsynced, force an fsync on the
@@ -6389,11 +6392,9 @@ assign_xlog_sync_method(int new_sync_method, bool doit, GucSource source)
63896392(errcode_for_file_access (),
63906393errmsg ("could not fsync log file %u, segment %u: %m" ,
63916394openLogId ,openLogSeg )));
6392- if (open_sync_bit != new_sync_bit )
6395+ if (get_sync_bit ( sync_method ) != get_sync_bit ( new_sync_method ) )
63936396XLogFileClose ();
63946397}
6395- sync_method = new_sync_method ;
6396- open_sync_bit = new_sync_bit ;
63976398}
63986399
63996400return true;