From 6698d430ffb6bd2e33aba0b21dc2a9358cf6328c Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Mon, 5 Feb 2018 13:03:38 +0900
Subject: [PATCH 1/5] Refactor path definitions into a single header file,
 pg_paths.h

The definition of all internal paths of PostgreSQL are spread across the
code base, making tracing of those definitions difficult to begin with,
and resulting in a lot of code paths to be hardcoded.  While this has no
real practical consequences in practice, this makes the creation of
lists manipulating those paths less maintainable as as things stand the
already-hardcoded paths need to be copied around more.  In order to
improve things for the long-term, move all those definitions into a
single header file.

This commit does a first step by switching basebackup.c and initdb.c to
use them.  An upcoming commit will make all the definitions across the
code base use this new header more consistently.
---
 src/backend/postmaster/postmaster.c  |   1 +
 src/backend/postmaster/syslogger.c   |   1 +
 src/backend/replication/basebackup.c |  16 ++--
 src/backend/storage/ipc/dsm.c        |   1 +
 src/backend/storage/ipc/dsm_impl.c   |   1 +
 src/backend/storage/lmgr/predicate.c |   3 +-
 src/backend/utils/adt/misc.c         |   1 +
 src/bin/initdb/initdb.c              |  45 ++++++------
 src/include/access/xlog_internal.h   |   7 +-
 src/include/pg_paths.h               | 137 +++++++++++++++++++++++++++++++++++
 src/include/pgstat.h                 |   3 -
 src/include/postmaster/syslogger.h   |   7 --
 src/include/storage/dsm_impl.h       |   9 ---
 src/include/utils/guc.h              |   7 --
 14 files changed, 176 insertions(+), 63 deletions(-)
 create mode 100644 src/include/pg_paths.h

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index f3ddf828bb..66d80914a0 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -105,6 +105,7 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pg_getopt.h"
+#include "pg_paths.h"
 #include "pgstat.h"
 #include "port/pg_bswap.h"
 #include "postmaster/autovacuum.h"
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index f70eea37df..c8770f6c61 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -35,6 +35,7 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "nodes/pg_list.h"
+#include "pg_paths.h"
 #include "pgstat.h"
 #include "pgtime.h"
 #include "postmaster/fork_process.h"
diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c
index dd7ad64862..63ab81f837 100644
--- a/src/backend/replication/basebackup.c
+++ b/src/backend/replication/basebackup.c
@@ -117,25 +117,25 @@ static const char *excludeDirContents[] =
 	 * even if the intention is to restore to another master. 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,
 
 	/* Contents removed on startup, see AsyncShmemInit(). */
-	"pg_notify",
+	PG_NOTIFY_DIR,
 
 	/*
 	 * Old contents are loaded for possible debugging but are not required for
 	 * normal operation, see OldSerXidInit().
 	 */
-	"pg_serial",
+	PG_SERIAL_DIR,
 
 	/* Contents removed on startup, see DeleteAllExportedSnapshotFiles(). */
-	"pg_snapshots",
+	PG_SNAPSHOTS_DIR,
 
 	/* Contents zeroed on startup, see StartupSUBTRANS(). */
-	"pg_subtrans",
+	PG_SUBTRANS_DIR,
 
 	/* end of list */
 	NULL
@@ -147,7 +147,7 @@ static const char *excludeDirContents[] =
 static const char *excludeFiles[] =
 {
 	/* Skip auto conf temporary file. */
-	PG_AUTOCONF_FILENAME ".tmp",
+	PG_AUTOCONF_FILENAME_TMP,
 
 	/* Skip current log file temporary file */
 	LOG_METAINFO_DATAFILE_TMP,
@@ -164,8 +164,8 @@ static const char *excludeFiles[] =
 	BACKUP_LABEL_FILE,
 	TABLESPACE_MAP,
 
-	"postmaster.pid",
-	"postmaster.opts",
+	POSTMASTER_PID_FILE,
+	POSTMASTER_OPTS_FILE,
 
 	/* end of list */
 	NULL
diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c
index f1f75b73f5..a0d69aa0d8 100644
--- a/src/backend/storage/ipc/dsm.c
+++ b/src/backend/storage/ipc/dsm.c
@@ -35,6 +35,7 @@
 
 #include "lib/ilist.h"
 #include "miscadmin.h"
+#include "pg_paths.h"
 #include "storage/dsm.h"
 #include "storage/ipc.h"
 #include "storage/lwlock.h"
diff --git a/src/backend/storage/ipc/dsm_impl.c b/src/backend/storage/ipc/dsm_impl.c
index 67e76b98fe..a2bb1e2288 100644
--- a/src/backend/storage/ipc/dsm_impl.c
+++ b/src/backend/storage/ipc/dsm_impl.c
@@ -62,6 +62,7 @@
 #endif
 #include "pgstat.h"
 
+#include "pg_paths.h"
 #include "portability/mem.h"
 #include "storage/dsm_impl.h"
 #include "storage/fd.h"
diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index d1ff2b1edc..731ed66251 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -194,6 +194,7 @@
 #include "access/xact.h"
 #include "access/xlog.h"
 #include "miscadmin.h"
+#include "pg_paths.h"
 #include "pgstat.h"
 #include "storage/bufmgr.h"
 #include "storage/predicate.h"
@@ -806,7 +807,7 @@ OldSerXidInit(void)
 	 */
 	OldSerXidSlruCtl->PagePrecedes = OldSerXidPagePrecedesLogically;
 	SimpleLruInit(OldSerXidSlruCtl, "oldserxid",
-				  NUM_OLDSERXID_BUFFERS, 0, OldSerXidLock, "pg_serial",
+				  NUM_OLDSERXID_BUFFERS, 0, OldSerXidLock, PG_SERIAL_DIR,
 				  LWTRANCHE_OLDSERXID_BUFFERS);
 	/* Override default assumption that writes should be fsync'd */
 	OldSerXidSlruCtl->do_fsync = false;
diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index 2e1e020c4b..9f98c03173 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -30,6 +30,7 @@
 #include "common/keywords.h"
 #include "funcapi.h"
 #include "miscadmin.h"
+#include "pg_paths.h"
 #include "pgstat.h"
 #include "parser/scansup.h"
 #include "postmaster/syslogger.h"
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 2efd3b75b1..528fdf7f57 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -72,6 +72,7 @@
 #include "getopt_long.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "pg_paths.h"
 
 
 /* Ideally this would be in a .h file, but it hardly seems worth the trouble */
@@ -199,28 +200,28 @@ static const char *boot_options = "-F";
 static const char *backend_options = "--single -F -O -j -c search_path=pg_catalog -c exit_on_error=true";
 
 static const char *const subdirs[] = {
-	"global",
-	"pg_wal/archive_status",
-	"pg_commit_ts",
-	"pg_dynshmem",
-	"pg_notify",
-	"pg_serial",
-	"pg_snapshots",
-	"pg_subtrans",
-	"pg_twophase",
-	"pg_multixact",
-	"pg_multixact/members",
-	"pg_multixact/offsets",
-	"base",
-	"base/1",
-	"pg_replslot",
-	"pg_tblspc",
-	"pg_stat",
-	"pg_stat_tmp",
-	"pg_xact",
-	"pg_logical",
-	"pg_logical/snapshots",
-	"pg_logical/mappings"
+	PG_GLOBAL_DIR,
+	XLOG_ARCHIVE_STATUS_DIR,
+	PG_COMMIT_TS_DIR,
+	PG_DYNSHMEM_DIR,
+	PG_NOTIFY_DIR,
+	PG_SERIAL_DIR,
+	PG_SNAPSHOTS_DIR,
+	PG_SUBTRANS_DIR,
+	PG_TWOPHASE_DIR,
+	PG_MULTIXACT_DIR,
+	PG_MULTIXACT_MEMBERS_DIR,
+	PG_MULTIXACT_OFFSETS_DIR,
+	PG_BASE_DIR,
+	PG_BASE_DIR "/1",
+	PG_REPLSLOT_DIR,
+	PG_TABLESPACE_DIR,
+	PG_STAT_DIR,
+	PG_STAT_TMP_DIR,
+	PG_XACT_DIR,
+	PG_LOGICAL_DIR,
+	PG_LOGICAL_SNAPSHOTS_DIR,
+	PG_LOGICAL_MAPPINGS_DIR
 };
 
 
diff --git a/src/include/access/xlog_internal.h b/src/include/access/xlog_internal.h
index a5c074642f..4ab8be61b5 100644
--- a/src/include/access/xlog_internal.h
+++ b/src/include/access/xlog_internal.h
@@ -23,6 +23,7 @@
 #include "access/xlogreader.h"
 #include "datatype/timestamp.h"
 #include "lib/stringinfo.h"
+#include "pg_paths.h"
 #include "pgtime.h"
 #include "storage/block.h"
 #include "storage/relfilenode.h"
@@ -137,12 +138,6 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
 #define XRecOffIsValid(xlrp) \
 		((xlrp) % XLOG_BLCKSZ >= SizeOfXLogShortPHD)
 
-/*
- * The XLog directory and control file (relative to $PGDATA)
- */
-#define XLOGDIR				"pg_wal"
-#define XLOG_CONTROL_FILE	"global/pg_control"
-
 /*
  * These macros encapsulate knowledge about the exact layout of XLog file
  * names, timeline history file names, and archive-status file names.
diff --git a/src/include/pg_paths.h b/src/include/pg_paths.h
new file mode 100644
index 0000000000..a4746e75e4
--- /dev/null
+++ b/src/include/pg_paths.h
@@ -0,0 +1,137 @@
+/* ----------
+ *  pg_paths.h
+ *
+ *  Definitions for paths internal to PostgreSQL instances.  This can
+ *	be used by both frontend and backend.
+ *
+ *  Copyright (c) 2018, PostgreSQL Global Development Group
+ *
+ *  src/include/pg_paths.h
+ * ----------
+ */
+
+#ifndef PG_PATHS_H
+#define PG_PATHS_H
+
+/*
+ * Name of files saving meta-data information about the log
+ * files currently in use by the syslogger
+ */
+#define LOG_METAINFO_DATAFILE			"current_logfiles"
+#define LOG_METAINFO_DATAFILE_TMP		LOG_METAINFO_DATAFILE ".tmp"
+
+/*
+ * A file recording the command-line options the server was
+ * last started with.
+ */
+#define POSTMASTER_OPTS_FILE			"postmaster.opts"
+
+/*
+ * Lock file recording data about the current postmaster process, like
+ * its PID.
+ */
+#define POSTMASTER_PID_FILE				"postmaster.pid"
+
+/*
+ * Automatic configuration file name for ALTER SYSTEM.
+ * This file will be used to store values of configuration parameters
+ * set by ALTER SYSTEM command.
+ */
+#define PG_AUTOCONF_FILENAME			"postgresql.auto.conf"
+#define PG_AUTOCONF_FILENAME_TMP		PG_AUTOCONF_FILENAME ".tmp"
+
+/*
+ * Directory containing transaction commit timestamp data.
+ */
+#define PG_COMMIT_TS_DIR	"pg_commit_ts"
+
+/*
+ * Directory for on-disk state of dynamic shared memory segments.
+ *
+ * This is used by all implementations for crash recovery and by the mmap
+ * implementation for storage.
+ */
+#define PG_DYNSHMEM_DIR					"pg_dynshmem"
+#define PG_DYNSHMEM_MMAP_FILE_PREFIX	"mmap."
+
+/*
+ * Directory containing cluster-wide tables.
+ */
+#define PG_GLOBAL_DIR		"global"
+
+/*
+ * Directory containing per-database subdirectories
+ */
+#define PG_BASE_DIR			"base"
+
+/*
+ * WAL directory, archive status directory and control file.
+ */
+#define XLOGDIR					"pg_wal"
+#define XLOG_ARCHIVE_STATUS_DIR	XLOGDIR "/archive_status"
+#define XLOG_CONTROL_FILE		PG_GLOBAL_DIR "/pg_control"
+
+/*
+ * Directory containing status data for logical decoding.
+ */
+#define PG_LOGICAL_DIR				"pg_logical"
+#define PG_LOGICAL_MAPPINGS_DIR		PG_LOGICAL_DIR "/mappings"
+#define PG_LOGICAL_SNAPSHOTS_DIR	PG_LOGICAL_DIR "/snapshots"
+
+/*
+ * Directories containing multitransaction status data (used for shared
+ * row locks).
+ */
+#define PG_MULTIXACT_DIR			"pg_multixact"
+#define PG_MULTIXACT_MEMBERS_DIR    PG_MULTIXACT_DIR "/members"
+#define PG_MULTIXACT_OFFSETS_DIR    PG_MULTIXACT_DIR "/offsets"
+
+/*
+ * Directory containing LISTEN/NOTIFY status data.
+ */
+#define PG_NOTIFY_DIR		"pg_notify"
+
+/*
+ * Directory containing replication slot data.
+ */
+#define PG_REPLSLOT_DIR		"pg_replslot"
+
+/*
+ * Default directories to store permanent and temporary statistics
+ * data in.  Used by the statistics collector.
+ */
+#define PG_STAT_DIR			"pg_stat"
+#define PG_STAT_TMP_DIR		"pg_stat_tmp"
+
+/*
+ * Directory containing information about committed serializable
+ * transactions.
+ */
+#define PG_SERIAL_DIR		"pg_serial"
+
+/*
+ * Directory containing exported snapshots dara.
+ */
+#define PG_SNAPSHOTS_DIR	"pg_snapshots"
+
+/*
+ * Directory containing subtransaction status data.
+ */
+#define PG_SUBTRANS_DIR		"pg_subtrans"
+
+/*
+ * Directory containing symbolic links to tablespaces.
+ */
+#define PG_TABLESPACE_DIR	"pg_tblspc"
+
+/*
+ * Directory containing state files for prepared transactions.
+ */
+#define PG_TWOPHASE_DIR		"pg_twophase"
+
+/*
+ * Directory containing transaction commit status data.
+ */
+#define PG_XACT_DIR			"pg_xact"
+
+#endif							/* PG_PATHS_H */
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index be2f59239b..7ae4b9097e 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -30,9 +30,6 @@
 #define PGSTAT_STAT_PERMANENT_FILENAME		"pg_stat/global.stat"
 #define PGSTAT_STAT_PERMANENT_TMPFILE		"pg_stat/global.tmp"
 
-/* Default directory to store temporary statistics data in */
-#define PG_STAT_TMP_DIR		"pg_stat_tmp"
-
 /* Values for track_functions GUC variable --- order is significant! */
 typedef enum TrackFunctionsLevel
 {
diff --git a/src/include/postmaster/syslogger.h b/src/include/postmaster/syslogger.h
index b35fadc1bd..00cc7a3023 100644
--- a/src/include/postmaster/syslogger.h
+++ b/src/include/postmaster/syslogger.h
@@ -87,11 +87,4 @@ extern void write_syslogger_file(const char *buffer, int count, int dest);
 extern void SysLoggerMain(int argc, char *argv[]) pg_attribute_noreturn();
 #endif
 
-/*
- * Name of files saving meta-data information about the log
- * files currently in use by the syslogger
- */
-#define LOG_METAINFO_DATAFILE  "current_logfiles"
-#define LOG_METAINFO_DATAFILE_TMP  LOG_METAINFO_DATAFILE ".tmp"
-
 #endif							/* _SYSLOGGER_H */
diff --git a/src/include/storage/dsm_impl.h b/src/include/storage/dsm_impl.h
index 0e5730f7c5..f13b46b6c3 100644
--- a/src/include/storage/dsm_impl.h
+++ b/src/include/storage/dsm_impl.h
@@ -42,15 +42,6 @@
 /* GUC. */
 extern int	dynamic_shared_memory_type;
 
-/*
- * Directory for on-disk state.
- *
- * This is used by all implementations for crash recovery and by the mmap
- * implementation for storage.
- */
-#define PG_DYNSHMEM_DIR					"pg_dynshmem"
-#define PG_DYNSHMEM_MMAP_FILE_PREFIX	"mmap."
-
 /* A "name" for a dynamic shared memory segment. */
 typedef uint32 dsm_handle;
 
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index 77daa5a539..8bb5ba9198 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -26,13 +26,6 @@
 #define MAX_KILOBYTES	(INT_MAX / 1024)
 #endif
 
-/*
- * Automatic configuration file name for ALTER SYSTEM.
- * This file will be used to store values of configuration parameters
- * set by ALTER SYSTEM command.
- */
-#define PG_AUTOCONF_FILENAME		"postgresql.auto.conf"
-
 /*
  * Certain options can only be set at certain times. The rules are
  * like this:
-- 
2.16.1

