Improve the naming in wal_sync_method code.
authorNathan Bossart <nathan@postgresql.org>
Fri, 13 Oct 2023 20:16:45 +0000 (15:16 -0500)
committerNathan Bossart <nathan@postgresql.org>
Fri, 13 Oct 2023 20:16:45 +0000 (15:16 -0500)
* sync_method is renamed to wal_sync_method.

* sync_method_options[] is renamed to wal_sync_method_options[].

* assign_xlog_sync_method() is renamed to assign_wal_sync_method().

* The names of the available synchronization methods are now
  prefixed with "WAL_SYNC_METHOD_" and have been moved into a
  WalSyncMethod enum.

* PLATFORM_DEFAULT_SYNC_METHOD is renamed to
  PLATFORM_DEFAULT_WAL_SYNC_METHOD, and DEFAULT_SYNC_METHOD is
  renamed to DEFAULT_WAL_SYNC_METHOD.

These more descriptive names help distinguish the code for
wal_sync_method from the code for DataDirSyncMethod (e.g., the
recovery_init_sync_method configuration parameter and the
--sync-method option provided by several frontend utilities).  This
change also prevents name collisions between the aforementioned
sets of code.  Since this only improves the naming of internal
identifiers, there should be no behavior change.

Author: Maxim Orlov
Discussion: https://postgr.es/m/CACG%3DezbL1gwE7_K7sr9uqaCGkWhmvRTcTEnm3%2BX1xsRNwbXULQ%40mail.gmail.com

src/backend/access/transam/xlog.c
src/backend/storage/file/fd.c
src/backend/utils/misc/guc_tables.c
src/include/access/xlog.h
src/include/access/xlogdefs.h
src/include/port/freebsd.h
src/include/port/linux.h
src/include/utils/guc_hooks.h
src/tools/pgindent/typedefs.list

index 07cf7662ff862c163fe67c45c4e2e03267d86448..c0e4ca50899917285dda12f96587bb0520bf9256 100644 (file)
@@ -130,7 +130,7 @@ bool           *wal_consistency_checking = NULL;
 bool           wal_init_zero = true;
 bool           wal_recycle = true;
 bool           log_checkpoints = true;
-int                    sync_method = DEFAULT_SYNC_METHOD;
+int                    wal_sync_method = DEFAULT_WAL_SYNC_METHOD;
 int                    wal_level = WAL_LEVEL_REPLICA;
 int                    CommitDelay = 0;        /* precommit delay in microseconds */
 int                    CommitSiblings = 5; /* # concurrent xacts needed to sleep */
@@ -171,17 +171,17 @@ static bool check_wal_consistency_checking_deferred = false;
 /*
  * GUC support
  */
-const struct config_enum_entry sync_method_options[] = {
-       {"fsync", SYNC_METHOD_FSYNC, false},
+const struct config_enum_entry wal_sync_method_options[] = {
+       {"fsync", WAL_SYNC_METHOD_FSYNC, false},
 #ifdef HAVE_FSYNC_WRITETHROUGH
-       {"fsync_writethrough", SYNC_METHOD_FSYNC_WRITETHROUGH, false},
+       {"fsync_writethrough", WAL_SYNC_METHOD_FSYNC_WRITETHROUGH, false},
 #endif
-       {"fdatasync", SYNC_METHOD_FDATASYNC, false},
+       {"fdatasync", WAL_SYNC_METHOD_FDATASYNC, false},
 #ifdef O_SYNC
-       {"open_sync", SYNC_METHOD_OPEN, false},
+       {"open_sync", WAL_SYNC_METHOD_OPEN, false},
 #endif
 #ifdef O_DSYNC
-       {"open_datasync", SYNC_METHOD_OPEN_DSYNC, false},
+       {"open_datasync", WAL_SYNC_METHOD_OPEN_DSYNC, false},
 #endif
        {NULL, 0, false}
 };
@@ -2343,8 +2343,8 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
                 * have no open file or the wrong one.  However, we do not need to
                 * fsync more than one file.
                 */
-               if (sync_method != SYNC_METHOD_OPEN &&
-                       sync_method != SYNC_METHOD_OPEN_DSYNC)
+               if (wal_sync_method != WAL_SYNC_METHOD_OPEN &&
+                       wal_sync_method != WAL_SYNC_METHOD_OPEN_DSYNC)
                {
                        if (openLogFile >= 0 &&
                                !XLByteInPrevSeg(LogwrtResult.Write, openLogSegNo,
@@ -2974,7 +2974,7 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
         */
        *added = false;
        fd = BasicOpenFile(path, O_RDWR | PG_BINARY | O_CLOEXEC |
-                                          get_sync_bit(sync_method));
+                                          get_sync_bit(wal_sync_method));
        if (fd < 0)
        {
                if (errno != ENOENT)
@@ -3139,7 +3139,7 @@ XLogFileInit(XLogSegNo logsegno, TimeLineID logtli)
 
        /* Now open original target segment (might not be file I just made) */
        fd = BasicOpenFile(path, O_RDWR | PG_BINARY | O_CLOEXEC |
-                                          get_sync_bit(sync_method));
+                                          get_sync_bit(wal_sync_method));
        if (fd < 0)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -3371,7 +3371,7 @@ XLogFileOpen(XLogSegNo segno, TimeLineID tli)
        XLogFilePath(path, tli, segno, wal_segment_size);
 
        fd = BasicOpenFile(path, O_RDWR | PG_BINARY | O_CLOEXEC |
-                                          get_sync_bit(sync_method));
+                                          get_sync_bit(wal_sync_method));
        if (fd < 0)
                ereport(PANIC,
                                (errcode_for_file_access(),
@@ -8137,16 +8137,16 @@ get_sync_bit(int method)
                         * not included in the enum option array, and therefore will never
                         * be seen here.
                         */
-               case SYNC_METHOD_FSYNC:
-               case SYNC_METHOD_FSYNC_WRITETHROUGH:
-               case SYNC_METHOD_FDATASYNC:
+               case WAL_SYNC_METHOD_FSYNC:
+               case WAL_SYNC_METHOD_FSYNC_WRITETHROUGH:
+               case WAL_SYNC_METHOD_FDATASYNC:
                        return o_direct_flag;
 #ifdef O_SYNC
-               case SYNC_METHOD_OPEN:
+               case WAL_SYNC_METHOD_OPEN:
                        return O_SYNC | o_direct_flag;
 #endif
 #ifdef O_DSYNC
-               case SYNC_METHOD_OPEN_DSYNC:
+               case WAL_SYNC_METHOD_OPEN_DSYNC:
                        return O_DSYNC | o_direct_flag;
 #endif
                default:
@@ -8160,9 +8160,9 @@ get_sync_bit(int method)
  * GUC support
  */
 void
-assign_xlog_sync_method(int new_sync_method, void *extra)
+assign_wal_sync_method(int new_wal_sync_method, void *extra)
 {
-       if (sync_method != new_sync_method)
+       if (wal_sync_method != new_wal_sync_method)
        {
                /*
                 * To ensure that no blocks escape unsynced, force an fsync on the
@@ -8188,7 +8188,7 @@ assign_xlog_sync_method(int new_sync_method, void *extra)
                        }
 
                        pgstat_report_wait_end();
-                       if (get_sync_bit(sync_method) != get_sync_bit(new_sync_method))
+                       if (get_sync_bit(wal_sync_method) != get_sync_bit(new_wal_sync_method))
                                XLogFileClose();
                }
        }
@@ -8214,8 +8214,8 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
         * file.
         */
        if (!enableFsync ||
-               sync_method == SYNC_METHOD_OPEN ||
-               sync_method == SYNC_METHOD_OPEN_DSYNC)
+               wal_sync_method == WAL_SYNC_METHOD_OPEN ||
+               wal_sync_method == WAL_SYNC_METHOD_OPEN_DSYNC)
                return;
 
        /* Measure I/O timing to sync the WAL file */
@@ -8225,29 +8225,29 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
                INSTR_TIME_SET_ZERO(start);
 
        pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
-       switch (sync_method)
+       switch (wal_sync_method)
        {
-               case SYNC_METHOD_FSYNC:
+               case WAL_SYNC_METHOD_FSYNC:
                        if (pg_fsync_no_writethrough(fd) != 0)
                                msg = _("could not fsync file \"%s\": %m");
                        break;
 #ifdef HAVE_FSYNC_WRITETHROUGH
-               case SYNC_METHOD_FSYNC_WRITETHROUGH:
+               case WAL_SYNC_METHOD_FSYNC_WRITETHROUGH:
                        if (pg_fsync_writethrough(fd) != 0)
                                msg = _("could not fsync write-through file \"%s\": %m");
                        break;
 #endif
-               case SYNC_METHOD_FDATASYNC:
+               case WAL_SYNC_METHOD_FDATASYNC:
                        if (pg_fdatasync(fd) != 0)
                                msg = _("could not fdatasync file \"%s\": %m");
                        break;
-               case SYNC_METHOD_OPEN:
-               case SYNC_METHOD_OPEN_DSYNC:
+               case WAL_SYNC_METHOD_OPEN:
+               case WAL_SYNC_METHOD_OPEN_DSYNC:
                        /* not reachable */
                        Assert(false);
                        break;
                default:
-                       elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
+                       elog(PANIC, "unrecognized wal_sync_method: %d", wal_sync_method);
                        break;
        }
 
index 3fed475c381894dfc536f0e9231e6f72967de0b3..b7607aa1bec2271a4db752cff38d9d0914c1aae1 100644 (file)
@@ -398,9 +398,9 @@ pg_fsync(int fd)
        errno = 0;
 #endif
 
-       /* #if is to skip the sync_method test if there's no need for it */
+       /* #if is to skip the wal_sync_method test if there's no need for it */
 #if defined(HAVE_FSYNC_WRITETHROUGH)
-       if (sync_method == SYNC_METHOD_FSYNC_WRITETHROUGH)
+       if (wal_sync_method == WAL_SYNC_METHOD_FSYNC_WRITETHROUGH)
                return pg_fsync_writethrough(fd);
        else
 #endif
index 16ec6c5ef02904a8c1a4588d80d1bc0db922315d..4c585741661e7f8194d6b6d39d16b4a9f290c914 100644 (file)
@@ -485,7 +485,7 @@ static const struct config_enum_entry wal_compression_options[] = {
 extern const struct config_enum_entry wal_level_options[];
 extern const struct config_enum_entry archive_mode_options[];
 extern const struct config_enum_entry recovery_target_action_options[];
-extern const struct config_enum_entry sync_method_options[];
+extern const struct config_enum_entry wal_sync_method_options[];
 extern const struct config_enum_entry dynamic_shared_memory_options[];
 
 /*
@@ -4843,9 +4843,9 @@ struct config_enum ConfigureNamesEnum[] =
                        gettext_noop("Selects the method used for forcing WAL updates to disk."),
                        NULL
                },
-               &sync_method,
-               DEFAULT_SYNC_METHOD, sync_method_options,
-               NULL, assign_xlog_sync_method, NULL
+               &wal_sync_method,
+               DEFAULT_WAL_SYNC_METHOD, wal_sync_method_options,
+               NULL, assign_wal_sync_method, NULL
        },
 
        {
index 48ca8523810ccf249215e633ae7dfec4ed7b10ed..4ad572cb8748d65d4e43ed50bd0c73f9253dab19 100644 (file)
 
 
 /* Sync methods */
-#define SYNC_METHOD_FSYNC              0
-#define SYNC_METHOD_FDATASYNC  1
-#define SYNC_METHOD_OPEN               2       /* for O_SYNC */
-#define SYNC_METHOD_FSYNC_WRITETHROUGH 3
-#define SYNC_METHOD_OPEN_DSYNC 4       /* for O_DSYNC */
-extern PGDLLIMPORT int sync_method;
+typedef enum WalSyncMethod
+{
+       WAL_SYNC_METHOD_FSYNC = 0,
+       WAL_SYNC_METHOD_FDATASYNC,
+       WAL_SYNC_METHOD_OPEN,           /* for O_SYNC */
+       WAL_SYNC_METHOD_FSYNC_WRITETHROUGH,
+       WAL_SYNC_METHOD_OPEN_DSYNC      /* for O_DSYNC */
+} WalSyncMethod;
+extern PGDLLIMPORT int wal_sync_method;
 
 extern PGDLLIMPORT XLogRecPtr ProcLastRecPtr;
 extern PGDLLIMPORT XLogRecPtr XactLastRecEnd;
index fe794c77405a50521ce031c4ad765bc68f2803bf..5f54c2816bc80b1045eddc1d42bc01994c114909 100644 (file)
@@ -71,12 +71,12 @@ typedef uint16 RepOriginId;
  *
  * Note that we define our own O_DSYNC on Windows, but not O_SYNC.
  */
-#if defined(PLATFORM_DEFAULT_SYNC_METHOD)
-#define DEFAULT_SYNC_METHOD            PLATFORM_DEFAULT_SYNC_METHOD
+#if defined(PLATFORM_DEFAULT_WAL_SYNC_METHOD)
+#define DEFAULT_WAL_SYNC_METHOD                PLATFORM_DEFAULT_WAL_SYNC_METHOD
 #elif defined(O_DSYNC) && (!defined(O_SYNC) || O_DSYNC != O_SYNC)
-#define DEFAULT_SYNC_METHOD            SYNC_METHOD_OPEN_DSYNC
+#define DEFAULT_WAL_SYNC_METHOD                WAL_SYNC_METHOD_OPEN_DSYNC
 #else
-#define DEFAULT_SYNC_METHOD            SYNC_METHOD_FDATASYNC
+#define DEFAULT_WAL_SYNC_METHOD                WAL_SYNC_METHOD_FDATASYNC
 #endif
 
 #endif                                                 /* XLOG_DEFS_H */
index 0e3fde55d6d9ad99a4a473eabcc79067fafd2fc5..c604187acdd3a529d978f18d0a569b8ef4c135a7 100644 (file)
@@ -5,4 +5,4 @@
  * would prefer open_datasync on FreeBSD 13+, but that is not a good choice on
  * many systems.
  */
-#define PLATFORM_DEFAULT_SYNC_METHOD   SYNC_METHOD_FDATASYNC
+#define PLATFORM_DEFAULT_WAL_SYNC_METHOD       WAL_SYNC_METHOD_FDATASYNC
index 7a6e46cdbb787f8196ed1c28e0d046a1748ed4f0..8101af2b93f55e1c7a7b5bd4845e346cd41d38fa 100644 (file)
@@ -19,4 +19,4 @@
  * perform better and (b) causes outright failures on ext4 data=journal
  * filesystems, because those don't support O_DIRECT.
  */
-#define PLATFORM_DEFAULT_SYNC_METHOD   SYNC_METHOD_FDATASYNC
+#define PLATFORM_DEFAULT_WAL_SYNC_METHOD       WAL_SYNC_METHOD_FDATASYNC
index f04b99e3b955e89a988d487eace222009a6c9888..2a191830a89ffc3f74fdff3509f6f342f309213f 100644 (file)
@@ -159,6 +159,6 @@ extern bool check_wal_consistency_checking(char **newval, void **extra,
                                                                                   GucSource source);
 extern void assign_wal_consistency_checking(const char *newval, void *extra);
 extern bool check_wal_segment_size(int *newval, void **extra, GucSource source);
-extern void assign_xlog_sync_method(int new_sync_method, void *extra);
+extern void assign_wal_sync_method(int new_wal_sync_method, void *extra);
 
 #endif                                                 /* GUC_HOOKS_H */
index 8de90c495853166ac12f447e331ab6e3bc2397ee..e69bb671bf8cd45049ccb1dfa33bf3fd4691fec1 100644 (file)
@@ -3016,6 +3016,7 @@ WalSnd
 WalSndCtlData
 WalSndSendDataCallback
 WalSndState
+WalSyncMethod
 WalTimeSample
 WalUsage
 WalWriteMethod