From 0d1f460709c75a0aea5a577cb1cb3d0927fc83fb Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Tue, 20 Oct 2020 09:31:50 -0700
Subject: [PATCH v2 2/3] Refactoring access/xlog_internal.h

Moving commonly used definitions and macros into new headers.
Those pertaining to xlog segment calculations are moved into
access/xlogseg.h; those pertaining to xlog file name conventions
into access/xlogfname.h; and those pertaining to the startup
process into access/xlogstarup.h
---
 src/backend/access/transam/timeline.c         |   3 +-
 src/backend/access/transam/xlogarchive.c      |   3 +-
 src/backend/access/transam/xlogfuncs.c        |   2 +-
 src/backend/access/transam/xlogutils.c        |   2 +-
 src/backend/bootstrap/bootstrap.c             |   2 +-
 src/backend/postmaster/checkpointer.c         |   2 +-
 src/backend/postmaster/pgarch.c               |   2 +-
 src/backend/replication/basebackup.c          |   2 +-
 .../replication/logical/reorderbuffer.c       |   2 +-
 src/backend/replication/slot.c                |   2 +-
 src/backend/replication/walreceiver.c         |   2 +-
 src/backend/replication/walreceiverfuncs.c    |   2 +-
 src/backend/replication/walsender.c           |   2 +-
 src/backend/utils/adt/genfile.c               |   2 +-
 src/backend/utils/misc/pg_controldata.c       |   2 +-
 src/bin/initdb/initdb.c                       |   3 +-
 src/bin/pg_archivecleanup/pg_archivecleanup.c |   2 +-
 src/bin/pg_basebackup/pg_basebackup.c         |   2 +-
 src/bin/pg_basebackup/pg_receivewal.c         |   2 +-
 src/bin/pg_basebackup/receivelog.c            |   2 +-
 src/bin/pg_basebackup/streamutil.c            |   2 +-
 src/bin/pg_checksums/pg_checksums.c           |   2 +-
 src/bin/pg_controldata/pg_controldata.c       |   2 +-
 src/bin/pg_rewind/pg_rewind.c                 |   2 +-
 src/common/controldata_utils.c                |   2 +-
 src/fe_utils/archive.c                        |   3 +-
 src/include/access/generic_xlog.h             |   1 -
 src/include/access/xlog.h                     |   9 +
 src/include/access/xlog_internal.h            | 163 +-----------------
 src/include/access/xlogdefs.h                 |  10 ++
 src/include/access/xlogfname.h                | 107 ++++++++++++
 src/include/access/xlogseg.h                  |  63 +++++++
 src/include/access/xlogstartup.h              |  23 +++
 33 files changed, 244 insertions(+), 188 deletions(-)
 create mode 100644 src/include/access/xlogfname.h
 create mode 100644 src/include/access/xlogseg.h
 create mode 100644 src/include/access/xlogstartup.h

diff --git a/src/backend/access/transam/timeline.c b/src/backend/access/transam/timeline.c
index e6a29d9a9b..6dd49cb6be 100644
--- a/src/backend/access/transam/timeline.c
+++ b/src/backend/access/transam/timeline.c
@@ -36,9 +36,10 @@
 
 #include "access/timeline.h"
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
 #include "access/xlogarchive.h"
 #include "access/xlogdefs.h"
+#include "access/xlogfname.h"
+#include "access/xlogstartup.h"
 #include "pgstat.h"
 #include "storage/fd.h"
 
diff --git a/src/backend/access/transam/xlogarchive.c b/src/backend/access/transam/xlogarchive.c
index cae93ab69d..27dd1414e1 100644
--- a/src/backend/access/transam/xlogarchive.c
+++ b/src/backend/access/transam/xlogarchive.c
@@ -20,8 +20,9 @@
 #include <unistd.h>
 
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
 #include "access/xlogarchive.h"
+#include "access/xlogfname.h"
+#include "access/xlogstartup.h"
 #include "common/archive.h"
 #include "miscadmin.h"
 #include "postmaster/startup.h"
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index 290658b22c..7bddc40f72 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -20,7 +20,7 @@
 
 #include "access/htup_details.h"
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "access/xlogutils.h"
 #include "catalog/pg_type.h"
 #include "funcapi.h"
diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c
index 7e915bcadf..199fca2461 100644
--- a/src/backend/access/transam/xlogutils.c
+++ b/src/backend/access/transam/xlogutils.c
@@ -21,7 +21,7 @@
 
 #include "access/timeline.h"
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "access/xlogutils.h"
 #include "miscadmin.h"
 #include "pgstat.h"
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 76b2f5066f..9dc2ce136e 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -22,7 +22,7 @@
 #include "access/htup_details.h"
 #include "access/tableam.h"
 #include "access/xact.h"
-#include "access/xlog_internal.h"
+#include "access/xlogseg.h"
 #include "bootstrap/bootstrap.h"
 #include "catalog/index.h"
 #include "catalog/pg_collation.h"
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index 429c8010ef..7b0b8f1410 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -40,7 +40,7 @@
 #include <time.h>
 
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
+#include "access/xlogseg.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index ed1b65358d..9b364508a7 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -34,7 +34,7 @@
 #include <unistd.h>
 
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c
index b89df01fa7..94e4e3b245 100644
--- a/src/backend/replication/basebackup.c
+++ b/src/backend/replication/basebackup.c
@@ -16,7 +16,7 @@
 #include <unistd.h>
 #include <time.h>
 
-#include "access/xlog_internal.h"	/* for pg_start/stop_backup */
+#include "access/xlogfname.h"
 #include "catalog/pg_type.h"
 #include "common/file_perm.h"
 #include "commands/progress.h"
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 7a8bf76079..517c443c8a 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -89,7 +89,7 @@
 #include "access/rewriteheap.h"
 #include "access/transam.h"
 #include "access/xact.h"
-#include "access/xlog_internal.h"
+#include "access/xlogseg.h"
 #include "catalog/catalog.h"
 #include "lib/binaryheap.h"
 #include "miscadmin.h"
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index 220b4cd6e9..75585555c6 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -40,7 +40,7 @@
 #include <sys/stat.h>
 
 #include "access/transam.h"
-#include "access/xlog_internal.h"
+#include "access/xlogseg.h"
 #include "common/string.h"
 #include "miscadmin.h"
 #include "pgstat.h"
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index bb1d44ccb7..53ac52e049 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -54,8 +54,8 @@
 #include "access/htup_details.h"
 #include "access/timeline.h"
 #include "access/transam.h"
-#include "access/xlog_internal.h"
 #include "access/xlogarchive.h"
+#include "access/xlogfname.h"
 #include "catalog/pg_authid.h"
 #include "catalog/pg_type.h"
 #include "common/ip.h"
diff --git a/src/backend/replication/walreceiverfuncs.c b/src/backend/replication/walreceiverfuncs.c
index e675757301..72b6b008bc 100644
--- a/src/backend/replication/walreceiverfuncs.c
+++ b/src/backend/replication/walreceiverfuncs.c
@@ -22,7 +22,7 @@
 #include <unistd.h>
 #include <signal.h>
 
-#include "access/xlog_internal.h"
+#include "access/xlogseg.h"
 #include "postmaster/startup.h"
 #include "replication/walreceiver.h"
 #include "storage/pmsignal.h"
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index df27e84761..2a95c5ecc0 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -53,7 +53,7 @@
 #include "access/timeline.h"
 #include "access/transam.h"
 #include "access/xact.h"
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "access/xlogreader.h"
 #include "access/xlogutils.h"
 #include "catalog/pg_authid.h"
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index d34182a7b0..fdd7675d17 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -21,7 +21,7 @@
 #include <dirent.h>
 
 #include "access/htup_details.h"
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "catalog/pg_authid.h"
 #include "catalog/pg_tablespace_d.h"
 #include "catalog/pg_type.h"
diff --git a/src/backend/utils/misc/pg_controldata.c b/src/backend/utils/misc/pg_controldata.c
index 6092312758..f5ba62186a 100644
--- a/src/backend/utils/misc/pg_controldata.c
+++ b/src/backend/utils/misc/pg_controldata.c
@@ -18,7 +18,7 @@
 #include "access/htup_details.h"
 #include "access/transam.h"
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "catalog/pg_control.h"
 #include "catalog/pg_type.h"
 #include "common/controldata_utils.h"
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index ee3bfa82f4..266763599b 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,7 +59,7 @@
 #include "sys/mman.h"
 #endif
 
-#include "access/xlog_internal.h"
+#include "access/xlogseg.h"
 #include "catalog/pg_authid_d.h"
 #include "catalog/pg_class_d.h" /* pgrminclude ignore */
 #include "catalog/pg_collation_d.h"
@@ -72,6 +72,7 @@
 #include "fe_utils/string_utils.h"
 #include "getaddrinfo.h"
 #include "getopt_long.h"
+#include "lib/stringinfo.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
 
diff --git a/src/bin/pg_archivecleanup/pg_archivecleanup.c b/src/bin/pg_archivecleanup/pg_archivecleanup.c
index 12338e3bb2..61a6950559 100644
--- a/src/bin/pg_archivecleanup/pg_archivecleanup.c
+++ b/src/bin/pg_archivecleanup/pg_archivecleanup.c
@@ -15,7 +15,7 @@
 #include <signal.h>
 #include <sys/time.h>
 
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "common/logging.h"
 #include "pg_getopt.h"
 
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index 7a5d4562f9..df9332e57d 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -26,7 +26,7 @@
 #include <zlib.h>
 #endif
 
-#include "access/xlog_internal.h"
+#include "access/xlogseg.h"
 #include "common/file_perm.h"
 #include "common/file_utils.h"
 #include "common/logging.h"
diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c
index cddc896390..9b72a140d2 100644
--- a/src/bin/pg_basebackup/pg_receivewal.c
+++ b/src/bin/pg_basebackup/pg_receivewal.c
@@ -19,7 +19,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "common/file_perm.h"
 #include "common/logging.h"
 #include "getopt_long.h"
diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c
index dc97c7e89c..4ed66df49c 100644
--- a/src/bin/pg_basebackup/receivelog.c
+++ b/src/bin/pg_basebackup/receivelog.c
@@ -20,7 +20,7 @@
 #include <sys/select.h>
 #endif
 
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "common/file_utils.h"
 #include "common/logging.h"
 #include "libpq-fe.h"
diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c
index be653ebb2d..e680af318d 100644
--- a/src/bin/pg_basebackup/streamutil.c
+++ b/src/bin/pg_basebackup/streamutil.c
@@ -17,7 +17,7 @@
 #include <sys/time.h>
 #include <unistd.h>
 
-#include "access/xlog_internal.h"
+#include "access/xlogseg.h"
 #include "common/connect.h"
 #include "common/fe_memutils.h"
 #include "common/file_perm.h"
diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index ffdc23945c..846062e265 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -19,11 +19,11 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "access/xlog_internal.h"
 #include "common/controldata_utils.h"
 #include "common/file_perm.h"
 #include "common/file_utils.h"
 #include "common/logging.h"
+#include "common/relpath.h"
 #include "getopt_long.h"
 #include "pg_getopt.h"
 #include "storage/bufpage.h"
diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c
index 3e00ac0f70..124fdd49b2 100644
--- a/src/bin/pg_controldata/pg_controldata.c
+++ b/src/bin/pg_controldata/pg_controldata.c
@@ -22,7 +22,7 @@
 
 #include "access/transam.h"
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "catalog/pg_control.h"
 #include "common/controldata_utils.h"
 #include "common/logging.h"
diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c
index 0ec52cb032..570eec78e6 100644
--- a/src/bin/pg_rewind/pg_rewind.c
+++ b/src/bin/pg_rewind/pg_rewind.c
@@ -15,7 +15,7 @@
 #include <unistd.h>
 
 #include "access/timeline.h"
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "catalog/catversion.h"
 #include "catalog/pg_control.h"
 #include "common/controldata_utils.h"
diff --git a/src/common/controldata_utils.c b/src/common/controldata_utils.c
index 3d53b6ac07..01dae42ab7 100644
--- a/src/common/controldata_utils.c
+++ b/src/common/controldata_utils.c
@@ -24,7 +24,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "catalog/pg_control.h"
 #include "common/controldata_utils.h"
 #include "common/file_perm.h"
diff --git a/src/fe_utils/archive.c b/src/fe_utils/archive.c
index 252dc0fb6a..22a9d7017d 100644
--- a/src/fe_utils/archive.c
+++ b/src/fe_utils/archive.c
@@ -15,10 +15,11 @@
 
 #include "postgres_fe.h"
 
+#include <fcntl.h>
 #include <unistd.h>
 #include <sys/stat.h>
 
-#include "access/xlog_internal.h"
+#include "access/xlogfname.h"
 #include "common/archive.h"
 #include "common/logging.h"
 #include "fe_utils/archive.h"
diff --git a/src/include/access/generic_xlog.h b/src/include/access/generic_xlog.h
index e0d460555f..49df99bcc1 100644
--- a/src/include/access/generic_xlog.h
+++ b/src/include/access/generic_xlog.h
@@ -15,7 +15,6 @@
 #define GENERIC_XLOG_H
 
 #include "access/xlog.h"
-#include "access/xlog_internal.h"
 #include "access/xloginsert.h"
 #include "storage/bufpage.h"
 #include "utils/rel.h"
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index 221af87e71..136ea4a16b 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -18,6 +18,7 @@
 #include "datatype/timestamp.h"
 #include "lib/stringinfo.h"
 #include "nodes/pg_list.h"
+#include "pgtime.h"
 #include "storage/fd.h"
 
 
@@ -383,6 +384,14 @@ extern void do_pg_abort_backup(int code, Datum arg);
 extern void register_persistent_abort_backup_handler(void);
 extern SessionBackupState get_backup_status(void);
 
+/*
+ * Exported to support xlog switching from checkpointer
+ */
+extern pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN);
+extern XLogRecPtr RequestXLogSwitch(bool mark_unimportant);
+
+extern void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli);
+
 /* File path names (all relative to $PGDATA) */
 #define RECOVERY_SIGNAL_FILE	"recovery.signal"
 #define STANDBY_SIGNAL_FILE		"standby.signal"
diff --git a/src/include/access/xlog_internal.h b/src/include/access/xlog_internal.h
index 4146753d47..0263166080 100644
--- a/src/include/access/xlog_internal.h
+++ b/src/include/access/xlog_internal.h
@@ -20,10 +20,11 @@
 #define XLOG_INTERNAL_H
 
 #include "access/xlogdefs.h"
+#include "access/xlogfname.h"
 #include "access/xlogreader.h"
+#include "access/xlogstartup.h"
 #include "datatype/timestamp.h"
 #include "lib/stringinfo.h"
-#include "pgtime.h"
 #include "storage/block.h"
 #include "storage/relfilenode.h"
 
@@ -82,42 +83,6 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
 #define XLogPageHeaderSize(hdr)		\
 	(((hdr)->xlp_info & XLP_LONG_HEADER) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD)
 
-/* wal_segment_size can range from 1MB to 1GB */
-#define WalSegMinSize 1024 * 1024
-#define WalSegMaxSize 1024 * 1024 * 1024
-/* default number of min and max wal segments */
-#define DEFAULT_MIN_WAL_SEGS 5
-#define DEFAULT_MAX_WAL_SEGS 64
-
-/* check that the given size is a valid wal_segment_size */
-#define IsPowerOf2(x) (x > 0 && ((x) & ((x)-1)) == 0)
-#define IsValidWalSegSize(size) \
-	 (IsPowerOf2(size) && \
-	 ((size) >= WalSegMinSize && (size) <= WalSegMaxSize))
-
-#define XLogSegmentsPerXLogId(wal_segsz_bytes)	\
-	(UINT64CONST(0x100000000) / (wal_segsz_bytes))
-
-#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest) \
-		(dest) = (segno) * (wal_segsz_bytes) + (offset)
-
-#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)	\
-	((xlogptr) & ((wal_segsz_bytes) - 1))
-
-/*
- * Compute a segment number from an XLogRecPtr.
- *
- * For XLByteToSeg, do the computation at face value.  For XLByteToPrevSeg,
- * a boundary byte is taken to be in the previous segment.  This is suitable
- * for deciding which segment to write given a pointer to a record end,
- * for example.
- */
-#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes) \
-	logSegNo = (xlrp) / (wal_segsz_bytes)
-
-#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
-	logSegNo = ((xlrp) - 1) / (wal_segsz_bytes)
-
 /*
  * Convert values of GUCs measured in megabytes to equiv. segment count.
  * Rounds down.
@@ -125,107 +90,10 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
 #define XLogMBVarToSegs(mbvar, wal_segsz_bytes) \
 	((mbvar) / ((wal_segsz_bytes) / (1024 * 1024)))
 
-/*
- * Is an XLogRecPtr within a particular XLOG segment?
- *
- * For XLByteInSeg, do the computation at face value.  For XLByteInPrevSeg,
- * a boundary byte is taken to be in the previous segment.
- */
-#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes) \
-	(((xlrp) / (wal_segsz_bytes)) == (logSegNo))
-
-#define XLByteInPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
-	((((xlrp) - 1) / (wal_segsz_bytes)) == (logSegNo))
-
 /* Check if an XLogRecPtr value is in a plausible range */
 #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.
- */
-#define MAXFNAMELEN		64
-
-/* Length of XLog file name */
-#define XLOG_FNAME_LEN	   24
-
-/*
- * Generate a WAL segment file name.  Do not use this macro in a helper
- * function allocating the result generated.
- */
-#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)	\
-	snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli,		\
-			 (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
-			 (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)))
-
-#define XLogFileNameById(fname, tli, log, seg)	\
-	snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, log, seg)
-
-#define IsXLogFileName(fname) \
-	(strlen(fname) == XLOG_FNAME_LEN && \
-	 strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN)
-
-/*
- * XLOG segment with .partial suffix.  Used by pg_receivewal and at end of
- * archive recovery, when we want to archive a WAL segment but it might not
- * be complete yet.
- */
-#define IsPartialXLogFileName(fname)	\
-	(strlen(fname) == XLOG_FNAME_LEN + strlen(".partial") &&	\
-	 strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN &&		\
-	 strcmp((fname) + XLOG_FNAME_LEN, ".partial") == 0)
-
-#define XLogFromFileName(fname, tli, logSegNo, wal_segsz_bytes)	\
-	do {												\
-		uint32 log;										\
-		uint32 seg;										\
-		sscanf(fname, "%08X%08X%08X", tli, &log, &seg); \
-		*logSegNo = (uint64) log * XLogSegmentsPerXLogId(wal_segsz_bytes) + seg; \
-	} while (0)
-
-#define XLogFilePath(path, tli, logSegNo, wal_segsz_bytes)	\
-	snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X", tli,	\
-			 (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
-			 (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)))
-
-#define TLHistoryFileName(fname, tli)	\
-	snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
-
-#define IsTLHistoryFileName(fname)	\
-	(strlen(fname) == 8 + strlen(".history") &&		\
-	 strspn(fname, "0123456789ABCDEF") == 8 &&		\
-	 strcmp((fname) + 8, ".history") == 0)
-
-#define TLHistoryFilePath(path, tli)	\
-	snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
-
-#define StatusFilePath(path, xlog, suffix)	\
-	snprintf(path, MAXPGPATH, XLOGDIR "/archive_status/%s%s", xlog, suffix)
-
-#define BackupHistoryFileName(fname, tli, logSegNo, startpoint, wal_segsz_bytes) \
-	snprintf(fname, MAXFNAMELEN, "%08X%08X%08X.%08X.backup", tli, \
-			 (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
-			 (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)), \
-			 (uint32) (XLogSegmentOffset(startpoint, wal_segsz_bytes)))
-
-#define IsBackupHistoryFileName(fname) \
-	(strlen(fname) > XLOG_FNAME_LEN && \
-	 strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
-	 strcmp((fname) + strlen(fname) - strlen(".backup"), ".backup") == 0)
-
-#define BackupHistoryFilePath(path, tli, logSegNo, startpoint, wal_segsz_bytes)	\
-	snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
-			 (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
-			 (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)), \
-			 (uint32) (XLogSegmentOffset((startpoint), wal_segsz_bytes)))
-
 /*
  * Information logged when we detect a change in one of the parameters
  * important for Hot Standby.
@@ -268,16 +136,6 @@ typedef struct XLogRecData
 	uint32		len;			/* length of rmgr data to include */
 } XLogRecData;
 
-/*
- * Recovery target action.
- */
-typedef enum
-{
-	RECOVERY_TARGET_ACTION_PAUSE,
-	RECOVERY_TARGET_ACTION_PROMOTE,
-	RECOVERY_TARGET_ACTION_SHUTDOWN
-}			RecoveryTargetAction;
-
 /*
  * Method table for resource managers.
  *
@@ -307,21 +165,4 @@ typedef struct RmgrData
 
 extern const RmgrData RmgrTable[];
 
-/*
- * Exported to support xlog switching from checkpointer
- */
-extern pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN);
-extern XLogRecPtr RequestXLogSwitch(bool mark_unimportant);
-
-extern void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli);
-
-/*
- * Exported for the functions in timeline.c and xlogarchive.c.  Only valid
- * in the startup process.
- */
-extern bool ArchiveRecoveryRequested;
-extern bool InArchiveRecovery;
-extern bool StandbyMode;
-extern char *recoveryRestoreCommand;
-
 #endif							/* XLOG_INTERNAL_H */
diff --git a/src/include/access/xlogdefs.h b/src/include/access/xlogdefs.h
index e1f5812213..74fea05e83 100644
--- a/src/include/access/xlogdefs.h
+++ b/src/include/access/xlogdefs.h
@@ -106,4 +106,14 @@ typedef uint16 RepOriginId;
 #define DEFAULT_SYNC_METHOD		SYNC_METHOD_FSYNC
 #endif
 
+/*
+ * Recovery target action.
+ */
+typedef enum
+{
+	RECOVERY_TARGET_ACTION_PAUSE,
+	RECOVERY_TARGET_ACTION_PROMOTE,
+	RECOVERY_TARGET_ACTION_SHUTDOWN
+}			RecoveryTargetAction;
+
 #endif							/* XLOG_DEFS_H */
diff --git a/src/include/access/xlogfname.h b/src/include/access/xlogfname.h
new file mode 100644
index 0000000000..e44ffb8f1d
--- /dev/null
+++ b/src/include/access/xlogfname.h
@@ -0,0 +1,107 @@
+/*
+ * xlogfname.h
+ *
+ * PostgreSQL write-ahead log file naming macros and definitions.
+ *
+ * NOTE: this file is intended to contain declarations useful for
+ * manipulating the names of XLOG files but not their internal contents.
+ *
+ * Note: This file must be includable in both frontend and backend contexts,
+ * to allow stand-alone tools to deal with WAL files.
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/access/xlogfname.h
+ */
+#ifndef XLOG_FNAME_H
+#define XLOG_FNAME_H
+
+#include "access/xlogseg.h"
+
+/*
+ * 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.
+ */
+#define MAXFNAMELEN		64
+
+/* Length of XLog file name */
+#define XLOG_FNAME_LEN	   24
+
+/*
+ * Generate a WAL segment file name.  Do not use this macro in a helper
+ * function allocating the result generated.
+ */
+#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)	\
+	snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli,		\
+			 (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
+			 (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)))
+
+#define XLogFileNameById(fname, tli, log, seg)	\
+	snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, log, seg)
+
+#define IsXLogFileName(fname) \
+	(strlen(fname) == XLOG_FNAME_LEN && \
+	 strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN)
+
+/*
+ * XLOG segment with .partial suffix.  Used by pg_receivewal and at end of
+ * archive recovery, when we want to archive a WAL segment but it might not
+ * be complete yet.
+ */
+#define IsPartialXLogFileName(fname)	\
+	(strlen(fname) == XLOG_FNAME_LEN + strlen(".partial") &&	\
+	 strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN &&		\
+	 strcmp((fname) + XLOG_FNAME_LEN, ".partial") == 0)
+
+#define XLogFromFileName(fname, tli, logSegNo, wal_segsz_bytes)	\
+	do {												\
+		uint32 log;										\
+		uint32 seg;										\
+		sscanf(fname, "%08X%08X%08X", tli, &log, &seg); \
+		*logSegNo = (uint64) log * XLogSegmentsPerXLogId(wal_segsz_bytes) + seg; \
+	} while (0)
+
+#define XLogFilePath(path, tli, logSegNo, wal_segsz_bytes)	\
+	snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X", tli,	\
+			 (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
+			 (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)))
+
+#define TLHistoryFileName(fname, tli)	\
+	snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
+
+#define IsTLHistoryFileName(fname)	\
+	(strlen(fname) == 8 + strlen(".history") &&		\
+	 strspn(fname, "0123456789ABCDEF") == 8 &&		\
+	 strcmp((fname) + 8, ".history") == 0)
+
+#define TLHistoryFilePath(path, tli)	\
+	snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
+
+#define StatusFilePath(path, xlog, suffix)	\
+	snprintf(path, MAXPGPATH, XLOGDIR "/archive_status/%s%s", xlog, suffix)
+
+#define BackupHistoryFileName(fname, tli, logSegNo, startpoint, wal_segsz_bytes) \
+	snprintf(fname, MAXFNAMELEN, "%08X%08X%08X.%08X.backup", tli, \
+			 (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
+			 (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)), \
+			 (uint32) (XLogSegmentOffset(startpoint, wal_segsz_bytes)))
+
+#define IsBackupHistoryFileName(fname) \
+	(strlen(fname) > XLOG_FNAME_LEN && \
+	 strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
+	 strcmp((fname) + strlen(fname) - strlen(".backup"), ".backup") == 0)
+
+#define BackupHistoryFilePath(path, tli, logSegNo, startpoint, wal_segsz_bytes)	\
+	snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
+			 (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
+			 (uint32) ((logSegNo) % XLogSegmentsPerXLogId(wal_segsz_bytes)), \
+			 (uint32) (XLogSegmentOffset((startpoint), wal_segsz_bytes)))
+
+#endif							/* XLOG_FNAME_H */
diff --git a/src/include/access/xlogseg.h b/src/include/access/xlogseg.h
new file mode 100644
index 0000000000..5a8f16b4e6
--- /dev/null
+++ b/src/include/access/xlogseg.h
@@ -0,0 +1,63 @@
+/*
+ * xlogseg.h
+ *
+ * PostgreSQL write-ahead log segment computation macros and definitions.
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/access/xlogseg.h
+ */
+#ifndef XLOG_SEG_H
+#define XLOG_SEG_H
+
+/* wal_segment_size can range from 1MB to 1GB */
+#define WalSegMinSize 1024 * 1024
+#define WalSegMaxSize 1024 * 1024 * 1024
+/* default number of min and max wal segments */
+#define DEFAULT_MIN_WAL_SEGS 5
+#define DEFAULT_MAX_WAL_SEGS 64
+
+/* check that the given size is a valid wal_segment_size */
+#define IsPowerOf2(x) (x > 0 && ((x) & ((x)-1)) == 0)
+#define IsValidWalSegSize(size) \
+	 (IsPowerOf2(size) && \
+	 ((size) >= WalSegMinSize && (size) <= WalSegMaxSize))
+
+#define XLogSegmentsPerXLogId(wal_segsz_bytes)	\
+	(UINT64CONST(0x100000000) / (wal_segsz_bytes))
+
+#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest) \
+		(dest) = (segno) * (wal_segsz_bytes) + (offset)
+
+#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)	\
+	((xlogptr) & ((wal_segsz_bytes) - 1))
+
+/*
+ * Compute a segment number from an XLogRecPtr.
+ *
+ * For XLByteToSeg, do the computation at face value.  For XLByteToPrevSeg,
+ * a boundary byte is taken to be in the previous segment.  This is suitable
+ * for deciding which segment to write given a pointer to a record end,
+ * for example.
+ */
+#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes) \
+	logSegNo = (xlrp) / (wal_segsz_bytes)
+
+#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
+	logSegNo = ((xlrp) - 1) / (wal_segsz_bytes)
+
+/*
+ * Is an XLogRecPtr within a particular XLOG segment?
+ *
+ * For XLByteInSeg, do the computation at face value.  For XLByteInPrevSeg,
+ * a boundary byte is taken to be in the previous segment.
+ */
+#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes) \
+	(((xlrp) / (wal_segsz_bytes)) == (logSegNo))
+
+#define XLByteInPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \
+	((((xlrp) - 1) / (wal_segsz_bytes)) == (logSegNo))
+
+
+#endif							/* XLOG_SEG_H */
diff --git a/src/include/access/xlogstartup.h b/src/include/access/xlogstartup.h
new file mode 100644
index 0000000000..a64f7cf92c
--- /dev/null
+++ b/src/include/access/xlogstartup.h
@@ -0,0 +1,23 @@
+/*
+ * xlogstartup.h
+ *
+ * PostgreSQL write-ahead log global state variable declarations.
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/access/xlogstartup.h
+ */
+#ifndef XLOG_STARTUP_H
+#define XLOG_STARTUP_H
+
+/*
+ * Exported for the functions in timeline.c and xlogarchive.c.  Only valid
+ * in the startup process.
+ */
+extern bool ArchiveRecoveryRequested;
+extern bool InArchiveRecovery;
+extern bool StandbyMode;
+extern char *recoveryRestoreCommand;
+
+#endif							/* XLOG_STARTUP_H */
-- 
2.21.1 (Apple Git-122.3)

