From 92299c4e7e6a53f53527498c9eff76c2595f82c2 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Tue, 20 Aug 2024 15:21:30 +0000
Subject: [PATCH v3 2/6] Define REPLSLOT_DIR_ARGS

Adding a new macro REPLSLOT_DIR_ARGS used when a slot name needs to be concatenated
to PG_REPLSLOT_DIR.
---
 src/backend/replication/logical/reorderbuffer.c |  9 ++++-----
 src/backend/replication/slot.c                  | 16 ++++++++--------
 src/backend/utils/adt/genfile.c                 |  2 +-
 src/include/replication/slot.h                  |  1 +
 4 files changed, 14 insertions(+), 14 deletions(-)
  23.5% src/backend/replication/logical/
  64.9% src/backend/replication/
   7.7% src/backend/utils/adt/
   3.7% src/include/replication/

diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 78166b6ab7..e4c2851c57 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -4573,7 +4573,7 @@ ReorderBufferCleanupSerializedTXNs(const char *slotname)
 	struct stat statbuf;
 	char		path[MAXPGPATH * 2 + sizeof(PG_REPLSLOT_DIR)];
 
-	sprintf(path, "%s/%s", PG_REPLSLOT_DIR, slotname);
+	sprintf(path, "%s/%s", REPLSLOT_DIR_ARGS(slotname));
 
 	/* we're only handling directories here, skip if it's not ours */
 	if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
@@ -4586,14 +4586,14 @@ ReorderBufferCleanupSerializedTXNs(const char *slotname)
 		if (strncmp(spill_de->d_name, "xid", 3) == 0)
 		{
 			snprintf(path, sizeof(path),
-					 "%s/%s/%s", PG_REPLSLOT_DIR, slotname,
+					 "%s/%s/%s", REPLSLOT_DIR_ARGS(slotname),
 					 spill_de->d_name);
 
 			if (unlink(path) != 0)
 				ereport(ERROR,
 						(errcode_for_file_access(),
 						 errmsg("could not remove file \"%s\" during removal of %s/%s/xid*: %m",
-								path, PG_REPLSLOT_DIR, slotname)));
+								path, REPLSLOT_DIR_ARGS(slotname))));
 		}
 	}
 	FreeDir(spill_dir);
@@ -4613,8 +4613,7 @@ ReorderBufferSerializedPath(char *path, ReplicationSlot *slot, TransactionId xid
 	XLogSegNoOffsetToRecPtr(segno, 0, wal_segment_size, recptr);
 
 	snprintf(path, MAXPGPATH, "%s/%s/xid-%u-lsn-%X-%X.spill",
-			 PG_REPLSLOT_DIR,
-			 NameStr(MyReplicationSlot->data.name),
+			 REPLSLOT_DIR_ARGS(NameStr(MyReplicationSlot->data.name)),
 			 xid, LSN_FORMAT_ARGS(recptr));
 }
 
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index 1299d92696..45225ca314 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -916,8 +916,8 @@ ReplicationSlotDropPtr(ReplicationSlot *slot)
 	LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
 
 	/* Generate pathnames. */
-	sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(slot->data.name));
-	sprintf(tmppath, "%s/%s.tmp", PG_REPLSLOT_DIR, NameStr(slot->data.name));
+	sprintf(path, "%s/%s", REPLSLOT_DIR_ARGS(NameStr(slot->data.name)));
+	sprintf(tmppath, "%s/%s.tmp", REPLSLOT_DIR_ARGS(NameStr(slot->data.name)));
 
 	/*
 	 * Rename the slot directory on disk, so that we'll no longer recognize
@@ -1016,7 +1016,7 @@ ReplicationSlotSave(void)
 
 	Assert(MyReplicationSlot != NULL);
 
-	sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(MyReplicationSlot->data.name));
+	sprintf(path, "%s/%s", REPLSLOT_DIR_ARGS(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, "%s/%s", PG_REPLSLOT_DIR, NameStr(s->data.name));
+		sprintf(path, "%s/%s", REPLSLOT_DIR_ARGS(NameStr(s->data.name)));
 
 		/*
 		 * Slot's data is not flushed each time the confirmed_flush LSN is
@@ -1932,7 +1932,7 @@ StartupReplicationSlots(void)
 			strcmp(replication_de->d_name, "..") == 0)
 			continue;
 
-		snprintf(path, sizeof(path), "%s/%s", PG_REPLSLOT_DIR, replication_de->d_name);
+		snprintf(path, sizeof(path), "%s/%s", REPLSLOT_DIR_ARGS(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 */
@@ -1987,8 +1987,8 @@ CreateSlotOnDisk(ReplicationSlot *slot)
 	 * takes out the lock, if we'd take the lock here, we'd deadlock.
 	 */
 
-	sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(slot->data.name));
-	sprintf(tmppath, "%s/%s.tmp", PG_REPLSLOT_DIR, NameStr(slot->data.name));
+	sprintf(path, "%s/%s", REPLSLOT_DIR_ARGS(NameStr(slot->data.name)));
+	sprintf(tmppath, "%s/%s.tmp", REPLSLOT_DIR_ARGS(NameStr(slot->data.name)));
 
 	/*
 	 * It's just barely possible that some previous effort to create or drop a
@@ -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, "%s/%s", PG_REPLSLOT_DIR, name);
+	sprintf(slotdir, "%s/%s", REPLSLOT_DIR_ARGS(name));
 	sprintf(path, "%s/state.tmp", slotdir);
 	if (unlink(path) < 0 && errno != ENOENT)
 		ereport(PANIC,
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index d95683d041..82d68b0caa 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -728,7 +728,7 @@ pg_ls_replslotdir(PG_FUNCTION_ARGS)
 				 errmsg("replication slot \"%s\" does not exist",
 						slotname)));
 
-	snprintf(path, sizeof(path), "%s/%s", PG_REPLSLOT_DIR, slotname);
+	snprintf(path, sizeof(path), "%s/%s", REPLSLOT_DIR_ARGS(slotname));
 
 	return pg_ls_dir_files(fcinfo, path, false);
 }
diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h
index 9665faac1c..150e3a228c 100644
--- a/src/include/replication/slot.h
+++ b/src/include/replication/slot.h
@@ -18,6 +18,7 @@
 #include "replication/walreceiver.h"
 
 #define PG_REPLSLOT_DIR     "pg_replslot"
+#define REPLSLOT_DIR_ARGS(slotname) (PG_REPLSLOT_DIR), (slotname)
 
 /*
  * Behaviour of replication slots, upon release or crash.
-- 
2.34.1

