From 2065ddf5e34ce098f549c4279ee3ab33c188a764 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Fri, 30 Aug 2024 10:42:21 +0900 Subject: [PATCH] Define PG_REPLSLOT_DIR for path pg_replslot/ in data folder 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 | 3 +- .../replication/logical/reorderbuffer.c | 17 +++---- src/backend/replication/slot.c | 46 +++++++++---------- src/backend/utils/adt/genfile.c | 5 +- src/bin/pg_rewind/filemap.c | 2 +- src/include/replication/slot.h | 3 ++ 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c index 01b35e26bd..de16afac74 100644 --- a/src/backend/backup/basebackup.c +++ b/src/backend/backup/basebackup.c @@ -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, diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index d1c4258844..45b622396e 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -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) diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 668b28ead7..0a03776156 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -20,11 +20,11 @@ * 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; } diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c index 0d82557417..f240745cc4 100644 --- a/src/backend/utils/adt/genfile.c +++ b/src/backend/utils/adt/genfile.c @@ -708,7 +708,7 @@ pg_ls_logicalmapdir(PG_FUNCTION_ARGS) } /* - * Function to return the list of files in the pg_replslot/ + * Function to return the list of files in the PG_REPLSLOT_DIR/ * 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); } diff --git a/src/bin/pg_rewind/filemap.c b/src/bin/pg_rewind/filemap.c index 4458324c9d..00e644d988 100644 --- a/src/bin/pg_rewind/filemap.c +++ b/src/bin/pg_rewind/filemap.c @@ -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 */ diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h index c2ee149fd6..45582cf9d8 100644 --- a/src/include/replication/slot.h +++ b/src/include/replication/slot.h @@ -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. * -- 2.39.5