From a8030b0b30142fbbe709b72a00a12f14563b2189 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Tue, 23 Aug 2022 17:21:29 -0400
Subject: [PATCH v1] Remove '#define FRONTEND' hacks.

---
 src/backend/replication/logical/proto.c |  1 +
 src/bin/pg_controldata/pg_controldata.c |  9 +--------
 src/bin/pg_resetwal/pg_resetwal.c       |  9 +--------
 src/bin/pg_waldump/compat.c             |  4 +---
 src/bin/pg_waldump/pg_waldump.c         |  3 +--
 src/bin/pg_waldump/rmgrdesc.c           |  3 +--
 src/include/access/brin_xlog.h          |  1 -
 src/include/access/generic_xlog.h       |  7 +++++++
 src/include/access/gist.h               | 11 +++++++++++
 src/include/access/heapam_xlog.h        |  7 +++++++
 src/include/access/heaptoast.h          |  6 ++++++
 src/include/access/htup_details.h       |  3 ++-
 src/include/access/itup.h               |  3 ++-
 src/include/access/xlog.h               | 16 ++++++++++++----
 src/include/access/xloginsert.h         |  7 +++++++
 src/include/catalog/objectaddress.h     |  9 ++++++++-
 src/include/catalog/pg_publication.h    |  4 ++++
 src/include/commands/sequence.h         | 12 +++++++++---
 src/include/commands/tablespace.h       |  7 +++++++
 src/include/storage/large_object.h      |  4 ++++
 src/include/utils/timestamp.h           |  1 -
 21 files changed, 92 insertions(+), 35 deletions(-)

diff --git a/src/backend/replication/logical/proto.c b/src/backend/replication/logical/proto.c
index ff8513e2d2..48ebb046f7 100644
--- a/src/backend/replication/logical/proto.c
+++ b/src/backend/replication/logical/proto.c
@@ -15,6 +15,7 @@
 #include "access/sysattr.h"
 #include "catalog/pg_namespace.h"
 #include "catalog/pg_type.h"
+#include "fmgr.h"
 #include "libpq/pqformat.h"
 #include "replication/logicalproto.h"
 #include "utils/lsyscache.h"
diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c
index c390ec51ce..9fa53d6a27 100644
--- a/src/bin/pg_controldata/pg_controldata.c
+++ b/src/bin/pg_controldata/pg_controldata.c
@@ -9,14 +9,7 @@
  * src/bin/pg_controldata/pg_controldata.c
  */
 
-/*
- * We have to use postgres.h not postgres_fe.h here, because there's so much
- * backend-only stuff in the XLOG include files we need.  But we need a
- * frontend-ish environment otherwise.  Hence this ugly hack.
- */
-#define FRONTEND 1
-
-#include "postgres.h"
+#include "postgres_fe.h"
 
 #include <time.h>
 
diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c
index d4772a2965..f93d816c21 100644
--- a/src/bin/pg_resetwal/pg_resetwal.c
+++ b/src/bin/pg_resetwal/pg_resetwal.c
@@ -28,14 +28,7 @@
  *-------------------------------------------------------------------------
  */
 
-/*
- * We have to use postgres.h not postgres_fe.h here, because there's so much
- * backend-only stuff in the XLOG include files we need.  But we need a
- * frontend-ish environment otherwise.  Hence this ugly hack.
- */
-#define FRONTEND 1
-
-#include "postgres.h"
+#include "postgres_fe.h"
 
 #include <dirent.h>
 #include <fcntl.h>
diff --git a/src/bin/pg_waldump/compat.c b/src/bin/pg_waldump/compat.c
index 6a3386e970..fe105c2bf9 100644
--- a/src/bin/pg_waldump/compat.c
+++ b/src/bin/pg_waldump/compat.c
@@ -14,9 +14,7 @@
  *-------------------------------------------------------------------------
  */
 
-/* ugly hack, same as in e.g pg_controldata */
-#define FRONTEND 1
-#include "postgres.h"
+#include "postgres_fe.h"
 
 #include <time.h>
 
diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c
index 6528113628..f8bb2b6fef 100644
--- a/src/bin/pg_waldump/pg_waldump.c
+++ b/src/bin/pg_waldump/pg_waldump.c
@@ -9,8 +9,7 @@
  *-------------------------------------------------------------------------
  */
 
-#define FRONTEND 1
-#include "postgres.h"
+#include "postgres_fe.h"
 
 #include <dirent.h>
 #include <signal.h>
diff --git a/src/bin/pg_waldump/rmgrdesc.c b/src/bin/pg_waldump/rmgrdesc.c
index 6b8c17bb4c..1c20f29da1 100644
--- a/src/bin/pg_waldump/rmgrdesc.c
+++ b/src/bin/pg_waldump/rmgrdesc.c
@@ -5,8 +5,7 @@
  *
  * src/bin/pg_waldump/rmgrdesc.c
  */
-#define FRONTEND 1
-#include "postgres.h"
+#include "postgres_fe.h"
 
 #include "access/brin_xlog.h"
 #include "access/clog.h"
diff --git a/src/include/access/brin_xlog.h b/src/include/access/brin_xlog.h
index 012a9afdf4..e27dc0eab0 100644
--- a/src/include/access/brin_xlog.h
+++ b/src/include/access/brin_xlog.h
@@ -19,7 +19,6 @@
 #include "storage/bufpage.h"
 #include "storage/itemptr.h"
 #include "storage/relfilelocator.h"
-#include "utils/relcache.h"
 
 
 /*
diff --git a/src/include/access/generic_xlog.h b/src/include/access/generic_xlog.h
index c8363a4766..21bdd3b38d 100644
--- a/src/include/access/generic_xlog.h
+++ b/src/include/access/generic_xlog.h
@@ -18,7 +18,10 @@
 #include "access/xlog_internal.h"
 #include "access/xloginsert.h"
 #include "storage/bufpage.h"
+
+#ifndef FRONTEND
 #include "utils/rel.h"
+#endif
 
 #define MAX_GENERIC_XLOG_PAGES	XLR_NORMAL_MAX_BLOCK_ID
 
@@ -29,6 +32,8 @@
 struct GenericXLogState;
 typedef struct GenericXLogState GenericXLogState;
 
+#ifndef FRONTEND
+
 /* API for construction of generic xlog records */
 extern GenericXLogState *GenericXLogStart(Relation relation);
 extern Page GenericXLogRegisterBuffer(GenericXLogState *state, Buffer buffer,
@@ -36,6 +41,8 @@ extern Page GenericXLogRegisterBuffer(GenericXLogState *state, Buffer buffer,
 extern XLogRecPtr GenericXLogFinish(GenericXLogState *state);
 extern void GenericXLogAbort(GenericXLogState *state);
 
+#endif
+
 /* functions defined for rmgr */
 extern void generic_redo(XLogReaderState *record);
 extern const char *generic_identify(uint8 info);
diff --git a/src/include/access/gist.h b/src/include/access/gist.h
index a3337627b8..3950b5d7d8 100644
--- a/src/include/access/gist.h
+++ b/src/include/access/gist.h
@@ -22,7 +22,10 @@
 #include "access/xlogdefs.h"
 #include "storage/block.h"
 #include "storage/bufpage.h"
+
+#ifndef FRONTEND
 #include "utils/relcache.h"
+#endif
 
 /*
  * amproc indexes for GiST indexes.
@@ -110,6 +113,8 @@ typedef GISTPageOpaqueData *GISTPageOpaque;
  */
 #define GIST_PAGE_ID		0xFF81
 
+#ifndef FRONTEND
+
 /*
  * This is the Split Vector to be returned by the PickSplit method.
  * PickSplit should fill the indexes of tuples to go to the left side into
@@ -164,6 +169,8 @@ typedef struct GISTENTRY
 	bool		leafkey;
 } GISTENTRY;
 
+#endif
+
 #define GistPageGetOpaque(page) ( (GISTPageOpaque) PageGetSpecialPointer(page) )
 
 #define GistPageIsLeaf(page)	( GistPageGetOpaque(page)->flags & F_LEAF)
@@ -226,6 +233,8 @@ GistPageGetDeleteXid(Page page)
 		return FullTransactionIdFromEpochAndXid(0, FirstNormalTransactionId);
 }
 
+#ifndef FRONTEND
+
 /*
  * Vector of GISTENTRY structs; user-defined methods union and picksplit
  * take it as one of their arguments
@@ -245,4 +254,6 @@ typedef struct
 	do { (e).key = (k); (e).rel = (r); (e).page = (pg); \
 		 (e).offset = (o); (e).leafkey = (l); } while (0)
 
+#endif
+
 #endif							/* GIST_H */
diff --git a/src/include/access/heapam_xlog.h b/src/include/access/heapam_xlog.h
index 1705e736be..7f89ec7edb 100644
--- a/src/include/access/heapam_xlog.h
+++ b/src/include/access/heapam_xlog.h
@@ -20,7 +20,10 @@
 #include "storage/buf.h"
 #include "storage/bufpage.h"
 #include "storage/relfilelocator.h"
+
+#ifndef FRONTEND
 #include "utils/relcache.h"
+#endif
 
 
 /*
@@ -401,6 +404,8 @@ extern void heap2_desc(StringInfo buf, XLogReaderState *record);
 extern const char *heap2_identify(uint8 info);
 extern void heap_xlog_logical_rewrite(XLogReaderState *r);
 
+#ifndef FRONTEND
+
 extern XLogRecPtr log_heap_freeze(Relation reln, Buffer buffer,
 								  TransactionId cutoff_xid, xl_heap_freeze_tuple *tuples,
 								  int ntuples);
@@ -418,4 +423,6 @@ extern void heap_execute_freeze_tuple(HeapTupleHeader tuple,
 extern XLogRecPtr log_heap_visible(RelFileLocator rlocator, Buffer heap_buffer,
 								   Buffer vm_buffer, TransactionId cutoff_xid, uint8 flags);
 
+#endif
+
 #endif							/* HEAPAM_XLOG_H */
diff --git a/src/include/access/heaptoast.h b/src/include/access/heaptoast.h
index a75699054a..a5f8a8358b 100644
--- a/src/include/access/heaptoast.h
+++ b/src/include/access/heaptoast.h
@@ -15,7 +15,10 @@
 
 #include "access/htup_details.h"
 #include "storage/lockdefs.h"
+
+#ifndef FRONTEND
 #include "utils/relcache.h"
+#endif
 
 /*
  * Find the maximum size of a tuple if there are to be N tuples per page.
@@ -88,6 +91,8 @@
 	 sizeof(int32) -									\
 	 VARHDRSZ)
 
+#ifndef FRONTEND
+
 /* ----------
  * heap_toast_insert_or_update -
  *
@@ -145,5 +150,6 @@ extern HeapTuple toast_build_flattened_tuple(TupleDesc tupleDesc,
 extern void heap_fetch_toast_slice(Relation toastrel, Oid valueid,
 								   int32 attrsize, int32 sliceoffset,
 								   int32 slicelength, struct varlena *result);
+#endif
 
 #endif							/* HEAPTOAST_H */
diff --git a/src/include/access/htup_details.h b/src/include/access/htup_details.h
index 51a60eda08..1ac148a37c 100644
--- a/src/include/access/htup_details.h
+++ b/src/include/access/htup_details.h
@@ -690,6 +690,8 @@ struct MinimalTupleData
 #define HeapTupleClearHeapOnly(tuple) \
 		HeapTupleHeaderClearHeapOnly((tuple)->t_data)
 
+#ifndef FRONTEND
+
 /* prototypes for functions in common/heaptuple.c */
 extern Size heap_compute_data_size(TupleDesc tupleDesc,
 								   Datum *values, bool *isnull);
@@ -733,7 +735,6 @@ extern size_t varsize_any(void *p);
 extern HeapTuple heap_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc);
 extern MinimalTuple minimal_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc);
 
-#ifndef FRONTEND
 /*
  *	fastgetattr
  *		Fetch a user attribute's value as a Datum (might be either a
diff --git a/src/include/access/itup.h b/src/include/access/itup.h
index 773cef8b3a..014f1f5c46 100644
--- a/src/include/access/itup.h
+++ b/src/include/access/itup.h
@@ -74,6 +74,7 @@ typedef IndexAttributeBitMapData * IndexAttributeBitMap;
 
 
 /* routines in indextuple.c */
+#ifndef FRONTEND
 extern IndexTuple index_form_tuple(TupleDesc tupleDescriptor,
 								   Datum *values, bool *isnull);
 extern IndexTuple index_form_tuple_context(TupleDesc tupleDescriptor,
@@ -89,7 +90,7 @@ extern void index_deform_tuple_internal(TupleDesc tupleDescriptor,
 extern IndexTuple CopyIndexTuple(IndexTuple source);
 extern IndexTuple index_truncate_tuple(TupleDesc sourceDescriptor,
 									   IndexTuple source, int leavenatts);
-
+#endif
 
 /*
  * Takes an infomask as argument (primarily because this needs to be usable
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index cd674c3c23..7e8d79eee0 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -192,6 +192,12 @@ typedef enum WALAvailability
 
 struct XLogRecData;
 
+extern void xlog_desc(StringInfo buf, XLogReaderState *record);
+extern const char *xlog_identify(uint8 info);
+extern void xlog_redo(XLogReaderState *record);
+
+#ifndef FRONTEND
+
 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata,
 								   XLogRecPtr fpw_lsn,
 								   uint8 flags,
@@ -208,10 +214,6 @@ extern XLogSegNo XLogGetLastRemovedSegno(void);
 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
 extern void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn);
 
-extern void xlog_redo(XLogReaderState *record);
-extern void xlog_desc(StringInfo buf, XLogReaderState *record);
-extern const char *xlog_identify(uint8 info);
-
 extern void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli);
 
 extern bool RecoveryInProgress(void);
@@ -259,6 +261,8 @@ extern void SetInstallXLogFileSegmentActive(void);
 extern bool IsInstallXLogFileSegmentActive(void);
 extern void XLogShutdownWalRcv(void);
 
+#endif
+
 /*
  * Routines to start, stop, and get status of a base backup.
  */
@@ -279,6 +283,8 @@ typedef enum SessionBackupState
 	SESSION_BACKUP_RUNNING,
 } SessionBackupState;
 
+#ifndef FRONTEND
+
 extern XLogRecPtr do_pg_backup_start(const char *backupidstr, bool fast,
 									 TimeLineID *starttli_p, StringInfo labelfile,
 									 List **tablespaces, StringInfo tblspcmapfile);
@@ -288,6 +294,8 @@ extern void do_pg_abort_backup(int code, Datum arg);
 extern void register_persistent_abort_backup_handler(void);
 extern SessionBackupState get_backup_status(void);
 
+#endif
+
 /* 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/xloginsert.h b/src/include/access/xloginsert.h
index aed4643d1c..42bb275dd2 100644
--- a/src/include/access/xloginsert.h
+++ b/src/include/access/xloginsert.h
@@ -16,7 +16,10 @@
 #include "storage/block.h"
 #include "storage/buf.h"
 #include "storage/relfilelocator.h"
+
+#ifndef FRONTEND
 #include "utils/relcache.h"
+#endif
 
 /*
  * The minimum size of the WAL construction working area. If you need to
@@ -38,6 +41,8 @@
 #define REGBUF_KEEP_DATA	0x10	/* include data even if a full-page image
 									 * is taken */
 
+#ifndef FRONTEND
+
 /* prototypes for public functions in xloginsert.c: */
 extern void XLogBeginInsert(void);
 extern void XLogSetRecordFlags(uint8 flags);
@@ -63,4 +68,6 @@ extern XLogRecPtr XLogSaveBufferForHint(Buffer buffer, bool buffer_std);
 
 extern void InitXLogInsert(void);
 
+#endif
+
 #endif							/* XLOGINSERT_H */
diff --git a/src/include/catalog/objectaddress.h b/src/include/catalog/objectaddress.h
index cf4d8b3107..cc20a5f399 100644
--- a/src/include/catalog/objectaddress.h
+++ b/src/include/catalog/objectaddress.h
@@ -14,9 +14,12 @@
 #define OBJECTADDRESS_H
 
 #include "access/htup.h"
-#include "nodes/parsenodes.h"
 #include "storage/lockdefs.h"
+
+#ifndef FRONTEND
+#include "nodes/parsenodes.h"
 #include "utils/relcache.h"
+#endif
 
 /*
  * An ObjectAddress represents a database object of any type.
@@ -40,6 +43,8 @@ extern PGDLLIMPORT const ObjectAddress InvalidObjectAddress;
 #define ObjectAddressSet(addr, class_id, object_id) \
 	ObjectAddressSubSet(addr, class_id, object_id, 0)
 
+#ifndef FRONTEND
+
 extern ObjectAddress get_object_address(ObjectType objtype, Node *object,
 										Relation *relp,
 										LOCKMODE lockmode, bool missing_ok);
@@ -86,4 +91,6 @@ extern struct ArrayType *strlist_to_textarray(List *list);
 
 extern ObjectType get_relkind_objtype(char relkind);
 
+#endif
+
 #endif							/* OBJECTADDRESS_H */
diff --git a/src/include/catalog/pg_publication.h b/src/include/catalog/pg_publication.h
index c298327f5e..6d29adb734 100644
--- a/src/include/catalog/pg_publication.h
+++ b/src/include/catalog/pg_publication.h
@@ -103,6 +103,8 @@ typedef struct Publication
 	PublicationActions pubactions;
 } Publication;
 
+#ifndef FRONTEND
+
 typedef struct PublicationRelInfo
 {
 	Relation	relation;
@@ -155,4 +157,6 @@ extern ObjectAddress publication_add_schema(Oid pubid, Oid schemaid,
 extern Bitmapset *pub_collist_to_bitmapset(Bitmapset *columns, Datum pubcols,
 										   MemoryContext mcxt);
 
+#endif
+
 #endif							/* PG_PUBLICATION_H */
diff --git a/src/include/commands/sequence.h b/src/include/commands/sequence.h
index d38c0e2388..a5846b1e74 100644
--- a/src/include/commands/sequence.h
+++ b/src/include/commands/sequence.h
@@ -15,12 +15,14 @@
 
 #include "access/xlogreader.h"
 #include "catalog/objectaddress.h"
-#include "fmgr.h"
 #include "lib/stringinfo.h"
-#include "nodes/parsenodes.h"
-#include "parser/parse_node.h"
 #include "storage/relfilelocator.h"
 
+#ifndef FRONTEND
+#include "fmgr.h"
+#include "nodes/parsenodes.h"
+#include "parser/parse_node.h"
+#endif
 
 typedef struct FormData_pg_sequence_data
 {
@@ -51,6 +53,8 @@ typedef struct xl_seq_rec
 	/* SEQUENCE TUPLE DATA FOLLOWS AT THE END */
 } xl_seq_rec;
 
+#ifndef FRONTEND
+
 extern int64 nextval_internal(Oid relid, bool check_permissions);
 extern Datum nextval(PG_FUNCTION_ARGS);
 extern List *sequence_options(Oid relid);
@@ -62,6 +66,8 @@ extern void DeleteSequenceTuple(Oid relid);
 extern void ResetSequence(Oid seq_relid);
 extern void ResetSequenceCaches(void);
 
+#endif
+
 extern void seq_redo(XLogReaderState *rptr);
 extern void seq_desc(StringInfo buf, XLogReaderState *rptr);
 extern const char *seq_identify(uint8 info);
diff --git a/src/include/commands/tablespace.h b/src/include/commands/tablespace.h
index 1f80907112..9b7b1424e6 100644
--- a/src/include/commands/tablespace.h
+++ b/src/include/commands/tablespace.h
@@ -17,7 +17,10 @@
 #include "access/xlogreader.h"
 #include "catalog/objectaddress.h"
 #include "lib/stringinfo.h"
+
+#ifndef FRONTEND
 #include "nodes/parsenodes.h"
+#endif
 
 extern PGDLLIMPORT bool allow_in_place_tablespaces;
 
@@ -45,6 +48,8 @@ typedef struct TableSpaceOpts
 	int			maintenance_io_concurrency;
 } TableSpaceOpts;
 
+#ifndef FRONTEND
+
 extern Oid	CreateTableSpace(CreateTableSpaceStmt *stmt);
 extern void DropTableSpace(DropTableSpaceStmt *stmt);
 extern ObjectAddress RenameTableSpace(const char *oldname, const char *newname);
@@ -62,6 +67,8 @@ extern char *get_tablespace_name(Oid spc_oid);
 extern bool directory_is_empty(const char *path);
 extern void remove_tablespace_symlink(const char *linkloc);
 
+#endif
+
 extern void tblspc_redo(XLogReaderState *rptr);
 extern void tblspc_desc(StringInfo buf, XLogReaderState *rptr);
 extern const char *tblspc_identify(uint8 info);
diff --git a/src/include/storage/large_object.h b/src/include/storage/large_object.h
index b826a7dcd5..4a14cd3ed2 100644
--- a/src/include/storage/large_object.h
+++ b/src/include/storage/large_object.h
@@ -76,6 +76,8 @@ typedef struct LargeObjectDesc
 #define MAX_LARGE_OBJECT_SIZE	((int64) INT_MAX * LOBLKSIZE)
 
 
+#ifndef FRONTEND
+
 /*
  * GUC: backwards-compatibility flag to suppress LO permission checks
  */
@@ -97,4 +99,6 @@ extern int	inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes);
 extern int	inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes);
 extern void inv_truncate(LargeObjectDesc *obj_desc, int64 len);
 
+#endif
+
 #endif							/* LARGE_OBJECT_H */
diff --git a/src/include/utils/timestamp.h b/src/include/utils/timestamp.h
index edf3a97318..2c353ee1d7 100644
--- a/src/include/utils/timestamp.h
+++ b/src/include/utils/timestamp.h
@@ -14,7 +14,6 @@
 #define TIMESTAMP_H
 
 #include "datatype/timestamp.h"
-#include "fmgr.h"
 #include "pgtime.h"
 
 
-- 
2.24.3 (Apple Git-128)

