1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

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
This commit is contained in:
Michael Paquier
2024-08-30 10:42:21 +09:00
parent a83a944e9f
commit 2065ddf5e3
6 changed files with 41 additions and 35 deletions

View File

@ -36,6 +36,7 @@
#include "port.h" #include "port.h"
#include "postmaster/syslogger.h" #include "postmaster/syslogger.h"
#include "postmaster/walsummarizer.h" #include "postmaster/walsummarizer.h"
#include "replication/slot.h"
#include "replication/walsender.h" #include "replication/walsender.h"
#include "replication/walsender_private.h" #include "replication/walsender_private.h"
#include "storage/bufpage.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 * even if the intention is to restore to another primary. See backup.sgml
* for a more detailed description. * for a more detailed description.
*/ */
"pg_replslot", PG_REPLSLOT_DIR,
/* Contents removed on startup, see dsm_cleanup_for_mmap(). */ /* Contents removed on startup, see dsm_cleanup_for_mmap(). */
PG_DYNSHMEM_DIR, PG_DYNSHMEM_DIR,

View File

@ -4595,9 +4595,9 @@ ReorderBufferCleanupSerializedTXNs(const char *slotname)
DIR *spill_dir; DIR *spill_dir;
struct dirent *spill_de; struct dirent *spill_de;
struct stat statbuf; 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 */ /* we're only handling directories here, skip if it's not ours */
if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode)) 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) if (strncmp(spill_de->d_name, "xid", 3) == 0)
{ {
snprintf(path, sizeof(path), snprintf(path, sizeof(path),
"pg_replslot/%s/%s", slotname, "%s/%s/%s", PG_REPLSLOT_DIR, slotname,
spill_de->d_name); spill_de->d_name);
if (unlink(path) != 0) if (unlink(path) != 0)
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not remove file \"%s\" during removal of pg_replslot/%s/xid*: %m", errmsg("could not remove file \"%s\" during removal of %s/%s/xid*: %m",
path, slotname))); path, PG_REPLSLOT_DIR, slotname)));
} }
} }
FreeDir(spill_dir); FreeDir(spill_dir);
@ -4636,7 +4636,8 @@ ReorderBufferSerializedPath(char *path, ReplicationSlot *slot, TransactionId xid
XLogSegNoOffsetToRecPtr(segno, 0, wal_segment_size, recptr); 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), NameStr(MyReplicationSlot->data.name),
xid, LSN_FORMAT_ARGS(recptr)); xid, LSN_FORMAT_ARGS(recptr));
} }
@ -4651,8 +4652,8 @@ StartupReorderBuffer(void)
DIR *logical_dir; DIR *logical_dir;
struct dirent *logical_de; struct dirent *logical_de;
logical_dir = AllocateDir("pg_replslot"); logical_dir = AllocateDir(PG_REPLSLOT_DIR);
while ((logical_de = ReadDir(logical_dir, "pg_replslot")) != NULL) while ((logical_de = ReadDir(logical_dir, PG_REPLSLOT_DIR)) != NULL)
{ {
if (strcmp(logical_de->d_name, ".") == 0 || if (strcmp(logical_de->d_name, ".") == 0 ||
strcmp(logical_de->d_name, "..") == 0) strcmp(logical_de->d_name, "..") == 0)

View File

@ -20,11 +20,11 @@
* on standbys (to support cascading setups). The requirement that slots be * on standbys (to support cascading setups). The requirement that slots be
* usable on standbys precludes storing them in the system catalogs. * usable on standbys precludes storing them in the system catalogs.
* *
* Each replication slot gets its own directory inside the $PGDATA/pg_replslot * Each replication slot gets its own directory inside the directory
* directory. Inside that directory the state file will contain the slot's * $PGDATA / PG_REPLSLOT_DIR. Inside that directory the state file will
* own data. Additional data can be stored alongside that file if required. * contain the slot's own data. Additional data can be stored alongside that
* While the server is running, the state data is also cached in memory for * file if required. While the server is running, the state data is also
* efficiency. * cached in memory for efficiency.
* *
* ReplicationSlotAllocationLock must be taken in exclusive mode to allocate * ReplicationSlotAllocationLock must be taken in exclusive mode to allocate
* or free a slot. ReplicationSlotControlLock must be taken in shared mode * or free a slot. ReplicationSlotControlLock must be taken in shared mode
@ -916,8 +916,8 @@ ReplicationSlotDropPtr(ReplicationSlot *slot)
LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE); LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
/* Generate pathnames. */ /* Generate pathnames. */
sprintf(path, "pg_replslot/%s", NameStr(slot->data.name)); sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(slot->data.name));
sprintf(tmppath, "pg_replslot/%s.tmp", 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 * Rename the slot directory on disk, so that we'll no longer recognize
@ -938,7 +938,7 @@ ReplicationSlotDropPtr(ReplicationSlot *slot)
*/ */
START_CRIT_SECTION(); START_CRIT_SECTION();
fsync_fname(tmppath, true); fsync_fname(tmppath, true);
fsync_fname("pg_replslot", true); fsync_fname(PG_REPLSLOT_DIR, true);
END_CRIT_SECTION(); END_CRIT_SECTION();
} }
else else
@ -1016,7 +1016,7 @@ ReplicationSlotSave(void)
Assert(MyReplicationSlot != NULL); 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); SaveSlotToPath(MyReplicationSlot, path, ERROR);
} }
@ -1881,7 +1881,7 @@ CheckPointReplicationSlots(bool is_shutdown)
continue; continue;
/* save the slot to disk, locking is handled in SaveSlotToPath() */ /* 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 * 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"); elog(DEBUG1, "starting up replication slots");
/* restore all slots by iterating over all on-disk entries */ /* restore all slots by iterating over all on-disk entries */
replication_dir = AllocateDir("pg_replslot"); replication_dir = AllocateDir(PG_REPLSLOT_DIR);
while ((replication_de = ReadDir(replication_dir, "pg_replslot")) != NULL) while ((replication_de = ReadDir(replication_dir, PG_REPLSLOT_DIR)) != NULL)
{ {
char path[MAXPGPATH + 12]; char path[MAXPGPATH + sizeof(PG_REPLSLOT_DIR)];
PGFileType de_type; PGFileType de_type;
if (strcmp(replication_de->d_name, ".") == 0 || if (strcmp(replication_de->d_name, ".") == 0 ||
strcmp(replication_de->d_name, "..") == 0) strcmp(replication_de->d_name, "..") == 0)
continue; 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); de_type = get_dirent_type(path, replication_de, false, DEBUG1);
/* we're only creating directories here, skip if it's not our's */ /* we're only creating directories here, skip if it's not our's */
@ -1949,7 +1949,7 @@ StartupReplicationSlots(void)
path))); path)));
continue; continue;
} }
fsync_fname("pg_replslot", true); fsync_fname(PG_REPLSLOT_DIR, true);
continue; continue;
} }
@ -1987,8 +1987,8 @@ CreateSlotOnDisk(ReplicationSlot *slot)
* takes out the lock, if we'd take the lock here, we'd deadlock. * takes out the lock, if we'd take the lock here, we'd deadlock.
*/ */
sprintf(path, "pg_replslot/%s", NameStr(slot->data.name)); sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(slot->data.name));
sprintf(tmppath, "pg_replslot/%s.tmp", 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 * It's just barely possible that some previous effort to create or drop a
@ -2027,7 +2027,7 @@ CreateSlotOnDisk(ReplicationSlot *slot)
START_CRIT_SECTION(); START_CRIT_SECTION();
fsync_fname(path, true); fsync_fname(path, true);
fsync_fname("pg_replslot", true); fsync_fname(PG_REPLSLOT_DIR, true);
END_CRIT_SECTION(); END_CRIT_SECTION();
} }
@ -2170,7 +2170,7 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
fsync_fname(path, false); fsync_fname(path, false);
fsync_fname(dir, true); fsync_fname(dir, true);
fsync_fname("pg_replslot", true); fsync_fname(PG_REPLSLOT_DIR, true);
END_CRIT_SECTION(); END_CRIT_SECTION();
@ -2195,8 +2195,8 @@ RestoreSlotFromDisk(const char *name)
{ {
ReplicationSlotOnDisk cp; ReplicationSlotOnDisk cp;
int i; int i;
char slotdir[MAXPGPATH + 12]; char slotdir[MAXPGPATH + sizeof(PG_REPLSLOT_DIR)];
char path[MAXPGPATH + 22]; char path[MAXPGPATH + sizeof(PG_REPLSLOT_DIR) + 10];
int fd; int fd;
bool restored = false; bool restored = false;
int readBytes; int readBytes;
@ -2205,7 +2205,7 @@ RestoreSlotFromDisk(const char *name)
/* no need to lock here, no concurrent access allowed yet */ /* no need to lock here, no concurrent access allowed yet */
/* delete temp file if it exists */ /* 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); sprintf(path, "%s/state.tmp", slotdir);
if (unlink(path) < 0 && errno != ENOENT) if (unlink(path) < 0 && errno != ENOENT)
ereport(PANIC, ereport(PANIC,
@ -2332,7 +2332,7 @@ RestoreSlotFromDisk(const char *name)
(errmsg("could not remove directory \"%s\"", (errmsg("could not remove directory \"%s\"",
slotdir))); slotdir)));
} }
fsync_fname("pg_replslot", true); fsync_fname(PG_REPLSLOT_DIR, true);
return; return;
} }

View 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. * directory.
*/ */
Datum Datum
@ -728,6 +728,7 @@ pg_ls_replslotdir(PG_FUNCTION_ARGS)
errmsg("replication slot \"%s\" does not exist", errmsg("replication slot \"%s\" does not exist",
slotname))); 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); return pg_ls_dir_files(fcinfo, path, false);
} }

View File

@ -97,7 +97,7 @@ static const char *const excludeDirContents[] =
* even if the intention is to restore to another primary. See backup.sgml * even if the intention is to restore to another primary. See backup.sgml
* for a more detailed description. * for a more detailed description.
*/ */
"pg_replslot", "pg_replslot", /* defined as PG_REPLSLOT_DIR */
/* Contents removed on startup, see dsm_cleanup_for_mmap(). */ /* Contents removed on startup, see dsm_cleanup_for_mmap(). */
"pg_dynshmem", /* defined as PG_DYNSHMEM_DIR */ "pg_dynshmem", /* defined as PG_DYNSHMEM_DIR */

View File

@ -17,6 +17,9 @@
#include "storage/spin.h" #include "storage/spin.h"
#include "replication/walreceiver.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. * Behaviour of replication slots, upon release or crash.
* *