Reduce the dependence on access/xlog_internal.h
Hackers,
Please find access/xlog_internal.h refactored in the attached patch series. This header is included from many places, including external tools. It is aesthetically displeasing to have something called "internal" used from so many places, especially when many of those places do not deal directly with the internal workings of xlog. But it is even worse that multiple files include this header for no reason.
A small portion of access/xlog_internal.h defines the RmgrData struct, and in support of this struct the header includes a number of other headers. Files that include access/xlog_internal.h indirectly include these other headers, which most do not need. (Only 3 out of 41 files involved actually need that portion of the header.) For third-party tools which deal with backup, restore, or replication matters, including xlog_internal.h is necessary to get macros for calculating xlog file names, but doing so also indirectly pulls in other headers, increasing the risk of unwanted symbol collisions. Some colleagues and I ran into this exact problem in a C++ program that uses both xlog_internal.h and the Boost C++ library.
0001 - Removes gratuitous inclusion of access/xlog_internal.h from *.c files in core that are currently including it without need. The following files are so modified:
src/backend/access/transam/rmgr.c
src/backend/postmaster/bgwriter.c
src/backend/replication/logical/decode.c
src/backend/replication/logical/logical.c
src/backend/replication/logical/logicalfuncs.c
src/backend/replication/logical/worker.c
src/bin/pg_basebackup/pg_recvlogical.c
src/bin/pg_rewind/timeline.c
src/bin/pg_waldump/rmgrdesc.c
0002 - Moves RmgrData from access/xlog_internal.h into a new file access/rmgr_internal.h. I clearly did not waste time thinking of a clever file name. Bikeshedding welcome. Most files which currently include xlog_internal.h do not need the definition of RmgrData. As it stands now, inclusion of xlog_internal.h indirectly includes the following headers:
<fcntl.h>
"access/rmgr.h"
"access/rmgrlist.h"
"access/transam.h"
"access/xlogdefs.h"
"access/xlogreader.h"
"access/xlogrecord.h"
"catalog/catversion.h"
"common/relpath.h"
"datatype/timestamp.h"
"lib/stringinfo.h"
"pgtime.h"
"port/pg_bswap.h"
"port/pg_crc32c.h"
"storage/backendid.h"
"storage/block.h"
"storage/relfilenode.h"
After refactoring, the inclusion of xlog_internal.h includes indirectly only these headers:
<fcntl.h>
"access/xlogdefs.h"
"datatype/timestamp.h"
"pgtime.h"
and only these files need to be altered to include the new rmgr_internal.h header:
src/backend/access/transam/rmgr.c
src/backend/access/transam/xlog.c
src/backend/utils/misc/guc.c
Thoughts?
Attachments:
v1-0001-Removing-gratuitous-inclusion-of-xlog_internal.h.patchapplication/octet-stream; name=v1-0001-Removing-gratuitous-inclusion-of-xlog_internal.h.patch; x-unix-mode=0644Download
From 77a2acad8377dac03c7be3c5c4d7740ffb97ce22 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Mon, 19 Oct 2020 17:08:34 -0700
Subject: [PATCH v1 1/2] Removing gratuitous inclusion of xlog_internal.h
Multiple files that were including xlog_internal.h did not need to
do so.
---
src/backend/postmaster/bgwriter.c | 1 -
src/backend/replication/logical/decode.c | 1 -
src/backend/replication/logical/logical.c | 1 -
src/backend/replication/logical/logicalfuncs.c | 1 -
src/backend/replication/logical/worker.c | 1 -
src/bin/pg_basebackup/pg_recvlogical.c | 1 -
src/bin/pg_rewind/timeline.c | 1 -
src/bin/pg_waldump/rmgrdesc.c | 1 -
8 files changed, 8 deletions(-)
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index a7afa758b6..aed72a75a9 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -35,7 +35,6 @@
#include "postgres.h"
#include "access/xlog.h"
-#include "access/xlog_internal.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index 3f84ee99b8..19549862f2 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -30,7 +30,6 @@
#include "access/heapam_xlog.h"
#include "access/transam.h"
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "access/xlogreader.h"
#include "access/xlogrecord.h"
#include "access/xlogutils.h"
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 8675832f4d..6471cb0d5e 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -29,7 +29,6 @@
#include "postgres.h"
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "pgstat.h"
diff --git a/src/backend/replication/logical/logicalfuncs.c b/src/backend/replication/logical/logicalfuncs.c
index b99c94e848..df2b110251 100644
--- a/src/backend/replication/logical/logicalfuncs.c
+++ b/src/backend/replication/logical/logicalfuncs.c
@@ -18,7 +18,6 @@
#include <unistd.h>
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "access/xlogutils.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index 3a5b733ee3..84578711c1 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -60,7 +60,6 @@
#include "access/table.h"
#include "access/tableam.h"
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "catalog/catalog.h"
#include "catalog/namespace.h"
#include "catalog/partition.h"
diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c
index a4e0d6aeb2..3971bc7dd3 100644
--- a/src/bin/pg_basebackup/pg_recvlogical.c
+++ b/src/bin/pg_basebackup/pg_recvlogical.c
@@ -19,7 +19,6 @@
#include <sys/select.h>
#endif
-#include "access/xlog_internal.h"
#include "common/fe_memutils.h"
#include "common/file_perm.h"
#include "common/logging.h"
diff --git a/src/bin/pg_rewind/timeline.c b/src/bin/pg_rewind/timeline.c
index 1ea6607189..5ba890a3c6 100644
--- a/src/bin/pg_rewind/timeline.c
+++ b/src/bin/pg_rewind/timeline.c
@@ -10,7 +10,6 @@
#include "postgres_fe.h"
#include "access/timeline.h"
-#include "access/xlog_internal.h"
#include "pg_rewind.h"
/*
diff --git a/src/bin/pg_waldump/rmgrdesc.c b/src/bin/pg_waldump/rmgrdesc.c
index 852d8ca4b1..5facf3da70 100644
--- a/src/bin/pg_waldump/rmgrdesc.c
+++ b/src/bin/pg_waldump/rmgrdesc.c
@@ -21,7 +21,6 @@
#include "access/rmgr.h"
#include "access/spgxlog.h"
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "catalog/storage_xlog.h"
#include "commands/dbcommands_xlog.h"
#include "commands/sequence.h"
--
2.21.1 (Apple Git-122.3)
v1-0002-Moving-RmgrData-from-xlog_internal.h-to-its-own-h.patchapplication/octet-stream; name=v1-0002-Moving-RmgrData-from-xlog_internal.h-to-its-own-h.patch; x-unix-mode=0644Download
From 43afb55c007a6ea5af146342289c5e1683364ba0 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Mon, 19 Oct 2020 18:00:34 -0700
Subject: [PATCH v1 2/2] Moving RmgrData from xlog_internal.h to its own
header.
---
src/backend/access/transam/rmgr.c | 1 +
src/backend/access/transam/xlog.c | 1 +
src/backend/utils/misc/guc.c | 1 +
src/bin/initdb/initdb.c | 1 +
src/bin/pg_checksums/pg_checksums.c | 1 +
src/include/access/rmgr_internal.h | 45 +++++++++++++++++++++++++++++
src/include/access/xlog_internal.h | 33 ---------------------
7 files changed, 50 insertions(+), 33 deletions(-)
create mode 100644 src/include/access/rmgr_internal.h
diff --git a/src/backend/access/transam/rmgr.c b/src/backend/access/transam/rmgr.c
index 58091f6b52..e30f18a4b2 100644
--- a/src/backend/access/transam/rmgr.c
+++ b/src/backend/access/transam/rmgr.c
@@ -18,6 +18,7 @@
#include "access/multixact.h"
#include "access/nbtxlog.h"
#include "access/spgxlog.h"
+#include "access/rmgr_internal.h"
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/storage_xlog.h"
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 52a67b1170..a7ffe05e54 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -27,6 +27,7 @@
#include "access/heaptoast.h"
#include "access/multixact.h"
#include "access/rewriteheap.h"
+#include "access/rmgr_internal.h"
#include "access/subtrans.h"
#include "access/timeline.h"
#include "access/transam.h"
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index a62d64eaa4..085441c836 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -32,6 +32,7 @@
#include "access/commit_ts.h"
#include "access/gin.h"
#include "access/rmgr.h"
+#include "access/rmgr_internal.h"
#include "access/tableam.h"
#include "access/transam.h"
#include "access/twophase.h"
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index ee3bfa82f4..d86b488a2f 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -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_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index ffdc23945c..db5d8a0ee5 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -24,6 +24,7 @@
#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/include/access/rmgr_internal.h b/src/include/access/rmgr_internal.h
new file mode 100644
index 0000000000..7ed8aca847
--- /dev/null
+++ b/src/include/access/rmgr_internal.h
@@ -0,0 +1,45 @@
+/*
+ * rmgr_internal.h
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/access/rmgr_internal.h
+ */
+#ifndef RMGR_INTERNAL_H
+#define RMGR_INTERNAL_H
+
+#include "lib/stringinfo.h"
+#include "storage/block.h"
+#include "storage/relfilenode.h"
+
+/*
+ * Method table for resource managers.
+ *
+ * This struct must be kept in sync with the PG_RMGR definition in
+ * rmgr.c.
+ *
+ * rm_identify must return a name for the record based on xl_info (without
+ * reference to the rmid). For example, XLOG_BTREE_VACUUM would be named
+ * "VACUUM". rm_desc can then be called to obtain additional detail for the
+ * record, if available (e.g. the last block).
+ *
+ * rm_mask takes as input a page modified by the resource manager and masks
+ * out bits that shouldn't be flagged by wal_consistency_checking.
+ *
+ * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h).
+ */
+typedef struct RmgrData
+{
+ const char *rm_name;
+ void (*rm_redo) (XLogReaderState *record);
+ void (*rm_desc) (StringInfo buf, XLogReaderState *record);
+ const char *(*rm_identify) (uint8 info);
+ void (*rm_startup) (void);
+ void (*rm_cleanup) (void);
+ void (*rm_mask) (char *pagedata, BlockNumber blkno);
+} RmgrData;
+
+extern const RmgrData RmgrTable[];
+
+#endif /* RMGR_INTERNAL_H */
diff --git a/src/include/access/xlog_internal.h b/src/include/access/xlog_internal.h
index 4146753d47..2ad7e443cb 100644
--- a/src/include/access/xlog_internal.h
+++ b/src/include/access/xlog_internal.h
@@ -20,12 +20,8 @@
#define XLOG_INTERNAL_H
#include "access/xlogdefs.h"
-#include "access/xlogreader.h"
#include "datatype/timestamp.h"
-#include "lib/stringinfo.h"
#include "pgtime.h"
-#include "storage/block.h"
-#include "storage/relfilenode.h"
/*
@@ -278,35 +274,6 @@ typedef enum
RECOVERY_TARGET_ACTION_SHUTDOWN
} RecoveryTargetAction;
-/*
- * Method table for resource managers.
- *
- * This struct must be kept in sync with the PG_RMGR definition in
- * rmgr.c.
- *
- * rm_identify must return a name for the record based on xl_info (without
- * reference to the rmid). For example, XLOG_BTREE_VACUUM would be named
- * "VACUUM". rm_desc can then be called to obtain additional detail for the
- * record, if available (e.g. the last block).
- *
- * rm_mask takes as input a page modified by the resource manager and masks
- * out bits that shouldn't be flagged by wal_consistency_checking.
- *
- * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h).
- */
-typedef struct RmgrData
-{
- const char *rm_name;
- void (*rm_redo) (XLogReaderState *record);
- void (*rm_desc) (StringInfo buf, XLogReaderState *record);
- const char *(*rm_identify) (uint8 info);
- void (*rm_startup) (void);
- void (*rm_cleanup) (void);
- void (*rm_mask) (char *pagedata, BlockNumber blkno);
-} RmgrData;
-
-extern const RmgrData RmgrTable[];
-
/*
* Exported to support xlog switching from checkpointer
*/
--
2.21.1 (Apple Git-122.3)
Hi,
On 2020-10-19 18:29:27 -0700, Mark Dilger wrote:
Please find access/xlog_internal.h refactored in the attached patch
series. This header is included from many places, including external
tools. It is aesthetically displeasing to have something called
"internal" used from so many places, especially when many of those
places do not deal directly with the internal workings of xlog. But
it is even worse that multiple files include this header for no
reason.
0002 - Moves RmgrData from access/xlog_internal.h into a new file access/rmgr_internal.h. I clearly did not waste time thinking of a clever file name. Bikeshedding welcome. Most files which currently include xlog_internal.h do not need the definition of RmgrData. As it stands now, inclusion of xlog_internal.h indirectly includes the following headers:
After refactoring, the inclusion of xlog_internal.h includes indirectly only these headers:
and only these files need to be altered to include the new rmgr_internal.h header:
src/backend/access/transam/rmgr.c
src/backend/access/transam/xlog.c
src/backend/utils/misc/guc.cThoughts?
It's not clear why the correct direction here is to make
xlog_internals.h less "low level" by moving things into headers like
rmgr_internal.h, rather than moving the widely used parts of
xlog_internal.h elsewhere.
A small portion of access/xlog_internal.h defines the RmgrData struct,
and in support of this struct the header includes a number of other
headers. Files that include access/xlog_internal.h indirectly include
these other headers, which most do not need. (Only 3 out of 41 files
involved actually need that portion of the header.) For third-party
tools which deal with backup, restore, or replication matters,
including xlog_internal.h is necessary to get macros for calculating
xlog file names, but doing so also indirectly pulls in other headers,
increasing the risk of unwanted symbol collisions. Some colleagues
and I ran into this exact problem in a C++ program that uses both
xlog_internal.h and the Boost C++ library.
It seems better to me to just use forward declarations for StringInfo
and XLogReaderState (and just generally use them mroe aggressively). We
don't need the functions for dealing with those datatypes here.
Greetings,
Andres Freund
On Oct 19, 2020, at 7:05 PM, Andres Freund <andres@anarazel.de> wrote:
Hi,
On 2020-10-19 18:29:27 -0700, Mark Dilger wrote:
Please find access/xlog_internal.h refactored in the attached patch
series. This header is included from many places, including external
tools. It is aesthetically displeasing to have something called
"internal" used from so many places, especially when many of those
places do not deal directly with the internal workings of xlog. But
it is even worse that multiple files include this header for no
reason.0002 - Moves RmgrData from access/xlog_internal.h into a new file access/rmgr_internal.h. I clearly did not waste time thinking of a clever file name. Bikeshedding welcome. Most files which currently include xlog_internal.h do not need the definition of RmgrData. As it stands now, inclusion of xlog_internal.h indirectly includes the following headers:
After refactoring, the inclusion of xlog_internal.h includes indirectly only these headers:
and only these files need to be altered to include the new rmgr_internal.h header:
src/backend/access/transam/rmgr.c
src/backend/access/transam/xlog.c
src/backend/utils/misc/guc.cThoughts?
It's not clear why the correct direction here is to make
xlog_internals.h less "low level" by moving things into headers like
rmgr_internal.h, rather than moving the widely used parts of
xlog_internal.h elsewhere.
Thanks for reviewing!
A small portion of access/xlog_internal.h defines the RmgrData struct,
and in support of this struct the header includes a number of other
headers. Files that include access/xlog_internal.h indirectly include
these other headers, which most do not need. (Only 3 out of 41 files
involved actually need that portion of the header.) For third-party
tools which deal with backup, restore, or replication matters,
including xlog_internal.h is necessary to get macros for calculating
xlog file names, but doing so also indirectly pulls in other headers,
increasing the risk of unwanted symbol collisions. Some colleagues
and I ran into this exact problem in a C++ program that uses both
xlog_internal.h and the Boost C++ library.It seems better to me to just use forward declarations for StringInfo
and XLogReaderState (and just generally use them mroe aggressively). We
don't need the functions for dealing with those datatypes here.
Yeah, those are good points. Please find attached version 2 of the patch set which preserves the cleanup of the first version's 0001 patch, and introduces two new patches, 0002 and 0003:
0002 - Moves commonly used stuff from xlog_internal.h into other headers
0003 - Uses forward declarations for StringInfo and XLogReaderState so as to not need to include lib/stringinfo.h nor access/xlogreader.h from access/xlog_internal.h
Attachments:
v2-0001-Removing-gratuitous-inclusion-of-xlog_internal.h.patchapplication/octet-stream; name=v2-0001-Removing-gratuitous-inclusion-of-xlog_internal.h.patch; x-unix-mode=0644Download
From d24fb1a06e54abfb69bc0f8412dd0fd5191071f0 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Mon, 19 Oct 2020 17:08:34 -0700
Subject: [PATCH v2 1/3] Removing gratuitous inclusion of xlog_internal.h
Multiple files that were including xlog_internal.h did not need to
do so.
---
src/backend/postmaster/bgwriter.c | 1 -
src/backend/replication/logical/decode.c | 1 -
src/backend/replication/logical/logical.c | 1 -
src/backend/replication/logical/logicalfuncs.c | 1 -
src/backend/replication/logical/worker.c | 1 -
src/bin/pg_basebackup/pg_recvlogical.c | 1 -
src/bin/pg_rewind/timeline.c | 1 -
src/bin/pg_waldump/rmgrdesc.c | 1 -
8 files changed, 8 deletions(-)
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index a7afa758b6..aed72a75a9 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -35,7 +35,6 @@
#include "postgres.h"
#include "access/xlog.h"
-#include "access/xlog_internal.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index 3f84ee99b8..19549862f2 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -30,7 +30,6 @@
#include "access/heapam_xlog.h"
#include "access/transam.h"
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "access/xlogreader.h"
#include "access/xlogrecord.h"
#include "access/xlogutils.h"
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 8675832f4d..6471cb0d5e 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -29,7 +29,6 @@
#include "postgres.h"
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "pgstat.h"
diff --git a/src/backend/replication/logical/logicalfuncs.c b/src/backend/replication/logical/logicalfuncs.c
index b99c94e848..df2b110251 100644
--- a/src/backend/replication/logical/logicalfuncs.c
+++ b/src/backend/replication/logical/logicalfuncs.c
@@ -18,7 +18,6 @@
#include <unistd.h>
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "access/xlogutils.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index 3a5b733ee3..84578711c1 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -60,7 +60,6 @@
#include "access/table.h"
#include "access/tableam.h"
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "catalog/catalog.h"
#include "catalog/namespace.h"
#include "catalog/partition.h"
diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c
index a4e0d6aeb2..3971bc7dd3 100644
--- a/src/bin/pg_basebackup/pg_recvlogical.c
+++ b/src/bin/pg_basebackup/pg_recvlogical.c
@@ -19,7 +19,6 @@
#include <sys/select.h>
#endif
-#include "access/xlog_internal.h"
#include "common/fe_memutils.h"
#include "common/file_perm.h"
#include "common/logging.h"
diff --git a/src/bin/pg_rewind/timeline.c b/src/bin/pg_rewind/timeline.c
index 1ea6607189..5ba890a3c6 100644
--- a/src/bin/pg_rewind/timeline.c
+++ b/src/bin/pg_rewind/timeline.c
@@ -10,7 +10,6 @@
#include "postgres_fe.h"
#include "access/timeline.h"
-#include "access/xlog_internal.h"
#include "pg_rewind.h"
/*
diff --git a/src/bin/pg_waldump/rmgrdesc.c b/src/bin/pg_waldump/rmgrdesc.c
index 852d8ca4b1..5facf3da70 100644
--- a/src/bin/pg_waldump/rmgrdesc.c
+++ b/src/bin/pg_waldump/rmgrdesc.c
@@ -21,7 +21,6 @@
#include "access/rmgr.h"
#include "access/spgxlog.h"
#include "access/xact.h"
-#include "access/xlog_internal.h"
#include "catalog/storage_xlog.h"
#include "commands/dbcommands_xlog.h"
#include "commands/sequence.h"
--
2.21.1 (Apple Git-122.3)
v2-0002-Refactoring-access-xlog_internal.h.patchapplication/octet-stream; name=v2-0002-Refactoring-access-xlog_internal.h.patch; x-unix-mode=0644Download
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)
v2-0003-Using-forward-declarations-to-reduce-including-he.patchapplication/octet-stream; name=v2-0003-Using-forward-declarations-to-reduce-including-he.patch; x-unix-mode=0644Download
From ab130eba20dd0e3891ed20a866e97e22b8f55e84 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Tue, 20 Oct 2020 16:42:23 -0700
Subject: [PATCH v2 3/3] Using forward declarations to reduce including headers
Using forward declarations of StringInfoData and XLogReaderState
to avoid pulling in so many symbols from access/xlog_internal.h
---
src/include/access/xlog_internal.h | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/src/include/access/xlog_internal.h b/src/include/access/xlog_internal.h
index 0263166080..7e177c2320 100644
--- a/src/include/access/xlog_internal.h
+++ b/src/include/access/xlog_internal.h
@@ -21,13 +21,12 @@
#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 "storage/block.h"
-#include "storage/relfilenode.h"
+typedef struct XLogReaderState XLogReaderState;
+typedef struct StringInfoData StringInfoData;
/*
* Each page of XLOG file has a header like this:
@@ -156,7 +155,7 @@ typedef struct RmgrData
{
const char *rm_name;
void (*rm_redo) (XLogReaderState *record);
- void (*rm_desc) (StringInfo buf, XLogReaderState *record);
+ void (*rm_desc) (struct StringInfoData *buf, XLogReaderState *record);
const char *(*rm_identify) (uint8 info);
void (*rm_startup) (void);
void (*rm_cleanup) (void);
--
2.21.1 (Apple Git-122.3)