Define PG_REPLSLOT_DIR for path pg_replslot/ in data folder
authorMichael Paquier <michael@paquier.xyz>
Fri, 30 Aug 2024 01:42:21 +0000 (10:42 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 30 Aug 2024 01:42:21 +0000 (10:42 +0900)
This commit replaces most of the hardcoded values of "pg_replslot" by a
new PG_REPLSLOT_DIR #define.  This makes the style more consistent with
the existing PG_STAT_TMP_DIR, for example.  More places will follow a
similar change.

Author: Bertrand Drouvot
Reviewed-by: Ashutosh Bapat, Yugo Nagata, Michael Paquier
Discussion: https://postgr.es/m/ZryVvjqS9SnV1GPP@ip-10-97-1-34.eu-west-3.compute.internal

src/backend/backup/basebackup.c
src/backend/replication/logical/reorderbuffer.c
src/backend/replication/slot.c
src/backend/utils/adt/genfile.c
src/bin/pg_rewind/filemap.c
src/include/replication/slot.h

index 01b35e26bda08532c55c53665bb356d9938c3492..de16afac749e082272525cdb4c462f22154fea8a 100644 (file)
@@ -36,6 +36,7 @@
 #include "port.h"
 #include "postmaster/syslogger.h"
 #include "postmaster/walsummarizer.h"
+#include "replication/slot.h"
 #include "replication/walsender.h"
 #include "replication/walsender_private.h"
 #include "storage/bufpage.h"
@@ -161,7 +162,7 @@ static const char *const excludeDirContents[] =
         * even if the intention is to restore to another primary. See backup.sgml
         * for a more detailed description.
         */
-       "pg_replslot",
+       PG_REPLSLOT_DIR,
 
        /* Contents removed on startup, see dsm_cleanup_for_mmap(). */
        PG_DYNSHMEM_DIR,
index d1c4258844f78d688e9faa1b096612d790be90b7..45b622396e62ad3e86042c5a41a5e2c2b5bd8da1 100644 (file)
@@ -4595,9 +4595,9 @@ ReorderBufferCleanupSerializedTXNs(const char *slotname)
        DIR                *spill_dir;
        struct dirent *spill_de;
        struct stat statbuf;
-       char            path[MAXPGPATH * 2 + 12];
+       char            path[MAXPGPATH * 2 + sizeof(PG_REPLSLOT_DIR)];
 
-       sprintf(path, "pg_replslot/%s", slotname);
+       sprintf(path, "%s/%s", PG_REPLSLOT_DIR, slotname);
 
        /* we're only handling directories here, skip if it's not ours */
        if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
@@ -4610,14 +4610,14 @@ ReorderBufferCleanupSerializedTXNs(const char *slotname)
                if (strncmp(spill_de->d_name, "xid", 3) == 0)
                {
                        snprintf(path, sizeof(path),
-                                        "pg_replslot/%s/%s", slotname,
+                                        "%s/%s/%s", PG_REPLSLOT_DIR, slotname,
                                         spill_de->d_name);
 
                        if (unlink(path) != 0)
                                ereport(ERROR,
                                                (errcode_for_file_access(),
-                                                errmsg("could not remove file \"%s\" during removal of pg_replslot/%s/xid*: %m",
-                                                               path, slotname)));
+                                                errmsg("could not remove file \"%s\" during removal of %s/%s/xid*: %m",
+                                                               path, PG_REPLSLOT_DIR, slotname)));
                }
        }
        FreeDir(spill_dir);
@@ -4636,7 +4636,8 @@ ReorderBufferSerializedPath(char *path, ReplicationSlot *slot, TransactionId xid
 
        XLogSegNoOffsetToRecPtr(segno, 0, wal_segment_size, recptr);
 
-       snprintf(path, MAXPGPATH, "pg_replslot/%s/xid-%u-lsn-%X-%X.spill",
+       snprintf(path, MAXPGPATH, "%s/%s/xid-%u-lsn-%X-%X.spill",
+                        PG_REPLSLOT_DIR,
                         NameStr(MyReplicationSlot->data.name),
                         xid, LSN_FORMAT_ARGS(recptr));
 }
@@ -4651,8 +4652,8 @@ StartupReorderBuffer(void)
        DIR                *logical_dir;
        struct dirent *logical_de;
 
-       logical_dir = AllocateDir("pg_replslot");
-       while ((logical_de = ReadDir(logical_dir, "pg_replslot")) != NULL)
+       logical_dir = AllocateDir(PG_REPLSLOT_DIR);
+       while ((logical_de = ReadDir(logical_dir, PG_REPLSLOT_DIR)) != NULL)
        {
                if (strcmp(logical_de->d_name, ".") == 0 ||
                        strcmp(logical_de->d_name, "..") == 0)
index 668b28ead79c82fd2db469d6d999360e1d091cea..0a037761569254451ba49788e2e0d6feba2362a5 100644 (file)
  * on standbys (to support cascading setups).  The requirement that slots be
  * usable on standbys precludes storing them in the system catalogs.
  *
- * Each replication slot gets its own directory inside the $PGDATA/pg_replslot
- * directory. Inside that directory the state file will contain the slot's
- * own data. Additional data can be stored alongside that file if required.
- * While the server is running, the state data is also cached in memory for
- * efficiency.
+ * Each replication slot gets its own directory inside the directory
+ * $PGDATA / PG_REPLSLOT_DIR.  Inside that directory the state file will
+ * contain the slot's own data.  Additional data can be stored alongside that
+ * file if required.  While the server is running, the state data is also
+ * cached in memory for efficiency.
  *
  * ReplicationSlotAllocationLock must be taken in exclusive mode to allocate
  * or free a slot. ReplicationSlotControlLock must be taken in shared mode
@@ -916,8 +916,8 @@ ReplicationSlotDropPtr(ReplicationSlot *slot)
        LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
 
        /* Generate pathnames. */
-       sprintf(path, "pg_replslot/%s", NameStr(slot->data.name));
-       sprintf(tmppath, "pg_replslot/%s.tmp", NameStr(slot->data.name));
+       sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(slot->data.name));
+       sprintf(tmppath, "%s/%s.tmp", PG_REPLSLOT_DIR, NameStr(slot->data.name));
 
        /*
         * Rename the slot directory on disk, so that we'll no longer recognize
@@ -938,7 +938,7 @@ ReplicationSlotDropPtr(ReplicationSlot *slot)
                 */
                START_CRIT_SECTION();
                fsync_fname(tmppath, true);
-               fsync_fname("pg_replslot", true);
+               fsync_fname(PG_REPLSLOT_DIR, true);
                END_CRIT_SECTION();
        }
        else
@@ -1016,7 +1016,7 @@ ReplicationSlotSave(void)
 
        Assert(MyReplicationSlot != NULL);
 
-       sprintf(path, "pg_replslot/%s", NameStr(MyReplicationSlot->data.name));
+       sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(MyReplicationSlot->data.name));
        SaveSlotToPath(MyReplicationSlot, path, ERROR);
 }
 
@@ -1881,7 +1881,7 @@ CheckPointReplicationSlots(bool is_shutdown)
                        continue;
 
                /* save the slot to disk, locking is handled in SaveSlotToPath() */
-               sprintf(path, "pg_replslot/%s", NameStr(s->data.name));
+               sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(s->data.name));
 
                /*
                 * Slot's data is not flushed each time the confirmed_flush LSN is
@@ -1922,17 +1922,17 @@ StartupReplicationSlots(void)
        elog(DEBUG1, "starting up replication slots");
 
        /* restore all slots by iterating over all on-disk entries */
-       replication_dir = AllocateDir("pg_replslot");
-       while ((replication_de = ReadDir(replication_dir, "pg_replslot")) != NULL)
+       replication_dir = AllocateDir(PG_REPLSLOT_DIR);
+       while ((replication_de = ReadDir(replication_dir, PG_REPLSLOT_DIR)) != NULL)
        {
-               char            path[MAXPGPATH + 12];
+               char            path[MAXPGPATH + sizeof(PG_REPLSLOT_DIR)];
                PGFileType      de_type;
 
                if (strcmp(replication_de->d_name, ".") == 0 ||
                        strcmp(replication_de->d_name, "..") == 0)
                        continue;
 
-               snprintf(path, sizeof(path), "pg_replslot/%s", replication_de->d_name);
+               snprintf(path, sizeof(path), "%s/%s", PG_REPLSLOT_DIR, replication_de->d_name);
                de_type = get_dirent_type(path, replication_de, false, DEBUG1);
 
                /* we're only creating directories here, skip if it's not our's */
@@ -1949,7 +1949,7 @@ StartupReplicationSlots(void)
                                                                path)));
                                continue;
                        }
-                       fsync_fname("pg_replslot", true);
+                       fsync_fname(PG_REPLSLOT_DIR, true);
                        continue;
                }
 
@@ -1987,8 +1987,8 @@ CreateSlotOnDisk(ReplicationSlot *slot)
         * takes out the lock, if we'd take the lock here, we'd deadlock.
         */
 
-       sprintf(path, "pg_replslot/%s", NameStr(slot->data.name));
-       sprintf(tmppath, "pg_replslot/%s.tmp", NameStr(slot->data.name));
+       sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(slot->data.name));
+       sprintf(tmppath, "%s/%s.tmp", PG_REPLSLOT_DIR, NameStr(slot->data.name));
 
        /*
         * It's just barely possible that some previous effort to create or drop a
@@ -2027,7 +2027,7 @@ CreateSlotOnDisk(ReplicationSlot *slot)
        START_CRIT_SECTION();
 
        fsync_fname(path, true);
-       fsync_fname("pg_replslot", true);
+       fsync_fname(PG_REPLSLOT_DIR, true);
 
        END_CRIT_SECTION();
 }
@@ -2170,7 +2170,7 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
 
        fsync_fname(path, false);
        fsync_fname(dir, true);
-       fsync_fname("pg_replslot", true);
+       fsync_fname(PG_REPLSLOT_DIR, true);
 
        END_CRIT_SECTION();
 
@@ -2195,8 +2195,8 @@ RestoreSlotFromDisk(const char *name)
 {
        ReplicationSlotOnDisk cp;
        int                     i;
-       char            slotdir[MAXPGPATH + 12];
-       char            path[MAXPGPATH + 22];
+       char            slotdir[MAXPGPATH + sizeof(PG_REPLSLOT_DIR)];
+       char            path[MAXPGPATH + sizeof(PG_REPLSLOT_DIR) + 10];
        int                     fd;
        bool            restored = false;
        int                     readBytes;
@@ -2205,7 +2205,7 @@ RestoreSlotFromDisk(const char *name)
        /* no need to lock here, no concurrent access allowed yet */
 
        /* delete temp file if it exists */
-       sprintf(slotdir, "pg_replslot/%s", name);
+       sprintf(slotdir, "%s/%s", PG_REPLSLOT_DIR, name);
        sprintf(path, "%s/state.tmp", slotdir);
        if (unlink(path) < 0 && errno != ENOENT)
                ereport(PANIC,
@@ -2332,7 +2332,7 @@ RestoreSlotFromDisk(const char *name)
                                        (errmsg("could not remove directory \"%s\"",
                                                        slotdir)));
                }
-               fsync_fname("pg_replslot", true);
+               fsync_fname(PG_REPLSLOT_DIR, true);
                return;
        }
 
index 0d82557417991c9f9c228b880b6a5a96fbf33c37..f240745cc4181863e1424b123af1f1bc58ef412a 100644 (file)
@@ -708,7 +708,7 @@ pg_ls_logicalmapdir(PG_FUNCTION_ARGS)
 }
 
 /*
- * Function to return the list of files in the pg_replslot/<replication_slot>
+ * Function to return the list of files in the PG_REPLSLOT_DIR/<slot_name>
  * directory.
  */
 Datum
@@ -728,6 +728,7 @@ pg_ls_replslotdir(PG_FUNCTION_ARGS)
                                 errmsg("replication slot \"%s\" does not exist",
                                                slotname)));
 
-       snprintf(path, sizeof(path), "pg_replslot/%s", slotname);
+       snprintf(path, sizeof(path), "%s/%s", PG_REPLSLOT_DIR, slotname);
+
        return pg_ls_dir_files(fcinfo, path, false);
 }
index 4458324c9d81bb01f504bf4e0b2b49b676d2c19b..00e644d988695e9278f1b5230182cec975af9bee 100644 (file)
@@ -97,7 +97,7 @@ static const char *const excludeDirContents[] =
         * even if the intention is to restore to another primary. See backup.sgml
         * for a more detailed description.
         */
-       "pg_replslot",
+       "pg_replslot",                          /* defined as PG_REPLSLOT_DIR */
 
        /* Contents removed on startup, see dsm_cleanup_for_mmap(). */
        "pg_dynshmem",                          /* defined as PG_DYNSHMEM_DIR */
index c2ee149fd666819bc9f7366f62495b345dfb5ebc..45582cf9d894c48b5187dda550a1af816f30e9fc 100644 (file)
@@ -17,6 +17,9 @@
 #include "storage/spin.h"
 #include "replication/walreceiver.h"
 
+/* directory to store replication slot data in */
+#define PG_REPLSLOT_DIR     "pg_replslot"
+
 /*
  * Behaviour of replication slots, upon release or crash.
  *