Define PG_LOGICAL_DIR for path pg_logical/ in data folder
authorMichael Paquier <[email protected]>
Fri, 30 Aug 2024 06:25:12 +0000 (15:25 +0900)
committerMichael Paquier <[email protected]>
Fri, 30 Aug 2024 06:25:12 +0000 (15:25 +0900)
This is similar to 2065ddf5e34c, but this time for pg_logical/ itself
and its contents, like the paths for snapshots, mappings or origin
checkpoints.

Author: Bertrand Drouvot
Reviewed-by: Ashutosh Bapat, Yugo Nagata, Michael Paquier
Discussion: https://postgr.es/m/[email protected]

src/backend/access/heap/rewriteheap.c
src/backend/replication/logical/origin.c
src/backend/replication/logical/reorderbuffer.c
src/backend/replication/logical/snapbuild.c
src/backend/utils/adt/genfile.c
src/include/replication/reorderbuffer.h

index 473f3aa9bef9e0c5abbca64efbce84de33cf08e2..09ef2204497200e8d781a25ae596b9a665a0f846 100644 (file)
@@ -961,8 +961,8 @@ logical_rewrite_log_mapping(RewriteState state, TransactionId xid,
            dboid = MyDatabaseId;
 
        snprintf(path, MAXPGPATH,
-                "pg_logical/mappings/" LOGICAL_REWRITE_FORMAT,
-                dboid, relid,
+                "%s/" LOGICAL_REWRITE_FORMAT,
+                PG_LOGICAL_MAPPINGS_DIR, dboid, relid,
                 LSN_FORMAT_ARGS(state->rs_begin_lsn),
                 xid, GetCurrentTransactionId());
 
@@ -1081,8 +1081,8 @@ heap_xlog_logical_rewrite(XLogReaderState *r)
    xlrec = (xl_heap_rewrite_mapping *) XLogRecGetData(r);
 
    snprintf(path, MAXPGPATH,
-            "pg_logical/mappings/" LOGICAL_REWRITE_FORMAT,
-            xlrec->mapped_db, xlrec->mapped_rel,
+            "%s/" LOGICAL_REWRITE_FORMAT,
+            PG_LOGICAL_MAPPINGS_DIR, xlrec->mapped_db, xlrec->mapped_rel,
             LSN_FORMAT_ARGS(xlrec->start_lsn),
             xlrec->mapped_xid, XLogRecGetXid(r));
 
@@ -1158,7 +1158,7 @@ CheckPointLogicalRewriteHeap(void)
    XLogRecPtr  redo;
    DIR        *mappings_dir;
    struct dirent *mapping_de;
-   char        path[MAXPGPATH + 20];
+   char        path[MAXPGPATH + sizeof(PG_LOGICAL_MAPPINGS_DIR)];
 
    /*
     * We start of with a minimum of the last redo pointer. No new decoding
@@ -1173,8 +1173,8 @@ CheckPointLogicalRewriteHeap(void)
    if (cutoff != InvalidXLogRecPtr && redo < cutoff)
        cutoff = redo;
 
-   mappings_dir = AllocateDir("pg_logical/mappings");
-   while ((mapping_de = ReadDir(mappings_dir, "pg_logical/mappings")) != NULL)
+   mappings_dir = AllocateDir(PG_LOGICAL_MAPPINGS_DIR);
+   while ((mapping_de = ReadDir(mappings_dir, PG_LOGICAL_MAPPINGS_DIR)) != NULL)
    {
        Oid         dboid;
        Oid         relid;
@@ -1189,7 +1189,7 @@ CheckPointLogicalRewriteHeap(void)
            strcmp(mapping_de->d_name, "..") == 0)
            continue;
 
-       snprintf(path, sizeof(path), "pg_logical/mappings/%s", mapping_de->d_name);
+       snprintf(path, sizeof(path), "%s/%s", PG_LOGICAL_MAPPINGS_DIR, mapping_de->d_name);
        de_type = get_dirent_type(path, mapping_de, false, DEBUG1);
 
        if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_REG)
@@ -1249,5 +1249,5 @@ CheckPointLogicalRewriteHeap(void)
    FreeDir(mappings_dir);
 
    /* persist directory entries to disk */
-   fsync_fname("pg_logical/mappings", true);
+   fsync_fname(PG_LOGICAL_MAPPINGS_DIR, true);
 }
index 419e4814f057749e67ff046e768c5e8cbd30e15c..baf696d8e68c3934026a9c59ac1f03508861db5d 100644 (file)
 #include "utils/snapmgr.h"
 #include "utils/syscache.h"
 
+/* paths for replication origin checkpoint files */
+#define PG_REPLORIGIN_CHECKPOINT_FILENAME PG_LOGICAL_DIR "/replorigin_checkpoint"
+#define PG_REPLORIGIN_CHECKPOINT_TMPFILE PG_REPLORIGIN_CHECKPOINT_FILENAME ".tmp"
+
 /*
  * Replay progress of a single remote node.
  */
@@ -572,8 +576,8 @@ ReplicationOriginShmemInit(void)
 void
 CheckPointReplicationOrigin(void)
 {
-   const char *tmppath = "pg_logical/replorigin_checkpoint.tmp";
-   const char *path = "pg_logical/replorigin_checkpoint";
+   const char *tmppath = PG_REPLORIGIN_CHECKPOINT_TMPFILE;
+   const char *path = PG_REPLORIGIN_CHECKPOINT_FILENAME;
    int         tmpfd;
    int         i;
    uint32      magic = REPLICATION_STATE_MAGIC;
@@ -698,7 +702,7 @@ CheckPointReplicationOrigin(void)
 void
 StartupReplicationOrigin(void)
 {
-   const char *path = "pg_logical/replorigin_checkpoint";
+   const char *path = PG_REPLORIGIN_CHECKPOINT_FILENAME;
    int         fd;
    int         readBytes;
    uint32      magic = REPLICATION_STATE_MAGIC;
index 45b622396e62ad3e86042c5a41a5e2c2b5bd8da1..22bcf171ff0219ae08d29937def8142513489357 100644 (file)
@@ -5081,7 +5081,7 @@ ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
    int         readBytes;
    LogicalRewriteMappingData map;
 
-   sprintf(path, "pg_logical/mappings/%s", fname);
+   sprintf(path, "%s/%s", PG_LOGICAL_MAPPINGS_DIR, fname);
    fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
    if (fd < 0)
        ereport(ERROR,
@@ -5197,8 +5197,8 @@ UpdateLogicalMappings(HTAB *tuplecid_data, Oid relid, Snapshot snapshot)
    ListCell   *file;
    Oid         dboid = IsSharedRelation(relid) ? InvalidOid : MyDatabaseId;
 
-   mapping_dir = AllocateDir("pg_logical/mappings");
-   while ((mapping_de = ReadDir(mapping_dir, "pg_logical/mappings")) != NULL)
+   mapping_dir = AllocateDir(PG_LOGICAL_MAPPINGS_DIR);
+   while ((mapping_de = ReadDir(mapping_dir, PG_LOGICAL_MAPPINGS_DIR)) != NULL)
    {
        Oid         f_dboid;
        Oid         f_relid;
index ae676145e602e16b0d0a998900b13b0b4ff1d7f8..0450f94ba8a9384b0868e01802c434c9c9eda446 100644 (file)
@@ -1654,7 +1654,8 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
     * unless the user used pg_resetwal or similar. If a user did so, there's
     * no hope continuing to decode anyway.
     */
-   sprintf(path, "pg_logical/snapshots/%X-%X.snap",
+   sprintf(path, "%s/%X-%X.snap",
+           PG_LOGICAL_SNAPSHOTS_DIR,
            LSN_FORMAT_ARGS(lsn));
 
    /*
@@ -1681,7 +1682,7 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
         * be safely on disk.
         */
        fsync_fname(path, false);
-       fsync_fname("pg_logical/snapshots", true);
+       fsync_fname(PG_LOGICAL_SNAPSHOTS_DIR, true);
 
        builder->last_serialized_snapshot = lsn;
        goto out;
@@ -1697,7 +1698,8 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
    elog(DEBUG1, "serializing snapshot to %s", path);
 
    /* to make sure only we will write to this tempfile, include pid */
-   sprintf(tmppath, "pg_logical/snapshots/%X-%X.snap.%d.tmp",
+   sprintf(tmppath, "%s/%X-%X.snap.%d.tmp",
+           PG_LOGICAL_SNAPSHOTS_DIR,
            LSN_FORMAT_ARGS(lsn), MyProcPid);
 
    /*
@@ -1818,7 +1820,7 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
                (errcode_for_file_access(),
                 errmsg("could not close file \"%s\": %m", tmppath)));
 
-   fsync_fname("pg_logical/snapshots", true);
+   fsync_fname(PG_LOGICAL_SNAPSHOTS_DIR, true);
 
    /*
     * We may overwrite the work from some other backend, but that's ok, our
@@ -1834,7 +1836,7 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
 
    /* make sure we persist */
    fsync_fname(path, false);
-   fsync_fname("pg_logical/snapshots", true);
+   fsync_fname(PG_LOGICAL_SNAPSHOTS_DIR, true);
 
    /*
     * Now there's no way we can lose the dumped state anymore, remember this
@@ -1871,7 +1873,8 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
    if (builder->state == SNAPBUILD_CONSISTENT)
        return false;
 
-   sprintf(path, "pg_logical/snapshots/%X-%X.snap",
+   sprintf(path, "%s/%X-%X.snap",
+           PG_LOGICAL_SNAPSHOTS_DIR,
            LSN_FORMAT_ARGS(lsn));
 
    fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
@@ -1892,7 +1895,7 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
     * ----
     */
    fsync_fname(path, false);
-   fsync_fname("pg_logical/snapshots", true);
+   fsync_fname(PG_LOGICAL_SNAPSHOTS_DIR, true);
 
 
    /* read statically sized portion of snapshot */
@@ -2074,7 +2077,7 @@ CheckPointSnapBuild(void)
    XLogRecPtr  redo;
    DIR        *snap_dir;
    struct dirent *snap_de;
-   char        path[MAXPGPATH + 21];
+   char        path[MAXPGPATH + sizeof(PG_LOGICAL_SNAPSHOTS_DIR)];
 
    /*
     * We start off with a minimum of the last redo pointer. No new
@@ -2090,8 +2093,8 @@ CheckPointSnapBuild(void)
    if (redo < cutoff)
        cutoff = redo;
 
-   snap_dir = AllocateDir("pg_logical/snapshots");
-   while ((snap_de = ReadDir(snap_dir, "pg_logical/snapshots")) != NULL)
+   snap_dir = AllocateDir(PG_LOGICAL_SNAPSHOTS_DIR);
+   while ((snap_de = ReadDir(snap_dir, PG_LOGICAL_SNAPSHOTS_DIR)) != NULL)
    {
        uint32      hi;
        uint32      lo;
@@ -2102,7 +2105,7 @@ CheckPointSnapBuild(void)
            strcmp(snap_de->d_name, "..") == 0)
            continue;
 
-       snprintf(path, sizeof(path), "pg_logical/snapshots/%s", snap_de->d_name);
+       snprintf(path, sizeof(path), "%s/%s", PG_LOGICAL_SNAPSHOTS_DIR, snap_de->d_name);
        de_type = get_dirent_type(path, snap_de, false, DEBUG1);
 
        if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_REG)
@@ -2162,7 +2165,8 @@ SnapBuildSnapshotExists(XLogRecPtr lsn)
    int         ret;
    struct stat stat_buf;
 
-   sprintf(path, "pg_logical/snapshots/%X-%X.snap",
+   sprintf(path, "%s/%X-%X.snap",
+           PG_LOGICAL_SNAPSHOTS_DIR,
            LSN_FORMAT_ARGS(lsn));
 
    ret = stat(path, &stat_buf);
index f240745cc4181863e1424b123af1f1bc58ef412a..24b95c32b787e5608bd6c746e630144f21025701 100644 (file)
@@ -690,21 +690,23 @@ pg_ls_archive_statusdir(PG_FUNCTION_ARGS)
 }
 
 /*
- * Function to return the list of files in the pg_logical/snapshots directory.
+ * Function to return the list of files in the PG_LOGICAL_SNAPSHOTS_DIR
+ * directory.
  */
 Datum
 pg_ls_logicalsnapdir(PG_FUNCTION_ARGS)
 {
-   return pg_ls_dir_files(fcinfo, "pg_logical/snapshots", false);
+   return pg_ls_dir_files(fcinfo, PG_LOGICAL_SNAPSHOTS_DIR, false);
 }
 
 /*
- * Function to return the list of files in the pg_logical/mappings directory.
+ * Function to return the list of files in the PG_LOGICAL_MAPPINGS_DIR
+ * directory.
  */
 Datum
 pg_ls_logicalmapdir(PG_FUNCTION_ARGS)
 {
-   return pg_ls_dir_files(fcinfo, "pg_logical/mappings", false);
+   return pg_ls_dir_files(fcinfo, PG_LOGICAL_MAPPINGS_DIR, false);
 }
 
 /*
index 851a001c8bb2b1c0fb3bbedc0caf5685d44bf87a..e332635f70b5a83174558353b225f0926cb81299 100644 (file)
 #include "utils/snapshot.h"
 #include "utils/timestamp.h"
 
+/* paths for logical decoding data (relative to installation's $PGDATA) */
+#define PG_LOGICAL_DIR             "pg_logical"
+#define PG_LOGICAL_MAPPINGS_DIR        PG_LOGICAL_DIR "/mappings"
+#define PG_LOGICAL_SNAPSHOTS_DIR   PG_LOGICAL_DIR "/snapshots"
+
 /* GUC variables */
 extern PGDLLIMPORT int logical_decoding_work_mem;
 extern PGDLLIMPORT int debug_logical_replication_streaming;