From 92d8e460c4f65f8fca735d90f02fb128f53abeef Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 13 Feb 2026 17:05:15 -0500
Subject: [PATCH v1] WIP: Reduce includes in pgstat.h

---
 src/include/pgstat.h                                | 13 +++++++------
 src/include/replication/conflict.h                  |  5 +++--
 src/backend/commands/functioncmds.c                 |  1 +
 src/backend/executor/execReplication.c              |  1 +
 src/backend/replication/logical/conflict.c          |  1 +
 src/backend/storage/ipc/procsignal.c                |  1 +
 src/backend/utils/activity/pgstat_subscription.c    |  4 ++--
 .../test_custom_stats/test_custom_fixed_stats.c     |  2 +-
 .../test_custom_stats/test_custom_var_stats.c       |  1 +
 9 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index fff7ecc2533..a2021a0e66f 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -11,18 +11,20 @@
 #ifndef PGSTAT_H
 #define PGSTAT_H
 
-#include "access/transam.h"		/* for FullTransactionId */
 #include "datatype/timestamp.h"
 #include "portability/instr_time.h"
 #include "postmaster/pgarch.h"	/* for MAX_XFN_CHARS */
-#include "replication/conflict.h"
-#include "replication/worker_internal.h"
+#include "replication/conflict.h"	/* for CONFLICT_NUM_TYPES */
 #include "utils/backend_progress.h" /* for backward compatibility */	/* IWYU pragma: export */
 #include "utils/backend_status.h"	/* for backward compatibility */	/* IWYU pragma: export */
 #include "utils/pgstat_kind.h"
-#include "utils/relcache.h"
 #include "utils/wait_event.h"	/* for backward compatibility */	/* IWYU pragma: export */
 
+/* to avoid including access/transam.h, for FullTransactionId */
+typedef struct FullTransactionId FullTransactionId;
+
+/* to avoid including utils/relcache.h */
+typedef struct RelationData *Relation;
 
 /* ----------
  * Paths for the statistics files (relative to installation's $PGDATA).
@@ -775,8 +777,7 @@ extern PgStat_SLRUStats *pgstat_fetch_slru(void);
  * Functions in pgstat_subscription.c
  */
 
-extern void pgstat_report_subscription_error(Oid subid,
-											 LogicalRepWorkerType wtype);
+extern void pgstat_report_subscription_error(Oid subid, int wtype);
 extern void pgstat_report_subscription_conflict(Oid subid, ConflictType type);
 extern void pgstat_create_subscription(Oid subid);
 extern void pgstat_drop_subscription(Oid subid);
diff --git a/src/include/replication/conflict.h b/src/include/replication/conflict.h
index 1cade336c91..4e1a8a2303f 100644
--- a/src/include/replication/conflict.h
+++ b/src/include/replication/conflict.h
@@ -10,14 +10,15 @@
 #define CONFLICT_H
 
 #include "access/xlogdefs.h"
-#include "nodes/pg_list.h"
-#include "utils/timestamp.h"
+#include "datatype/timestamp.h"
 
 /* Avoid including execnodes.h here */
 typedef struct EState EState;
 typedef struct ResultRelInfo ResultRelInfo;
 typedef struct TupleTableSlot TupleTableSlot;
 
+/* To avoid including pg_list.h */
+typedef struct List List;
 
 /*
  * Conflict types that could occur while applying remote changes.
diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c
index a516b037dea..242372b1e68 100644
--- a/src/backend/commands/functioncmds.c
+++ b/src/backend/commands/functioncmds.c
@@ -34,6 +34,7 @@
 
 #include "access/htup_details.h"
 #include "access/table.h"
+#include "access/xact.h"
 #include "catalog/catalog.h"
 #include "catalog/dependency.h"
 #include "catalog/indexing.h"
diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c
index 743b1ee2b28..c1007c1b41b 100644
--- a/src/backend/executor/execReplication.c
+++ b/src/backend/executor/execReplication.c
@@ -27,6 +27,7 @@
 #include "commands/trigger.h"
 #include "executor/executor.h"
 #include "executor/nodeModifyTable.h"
+#include "nodes/pg_list.h"
 #include "replication/conflict.h"
 #include "replication/logicalrelation.h"
 #include "storage/lmgr.h"
diff --git a/src/backend/replication/logical/conflict.c b/src/backend/replication/logical/conflict.c
index ca71a81c7bf..73cca04ffa5 100644
--- a/src/backend/replication/logical/conflict.c
+++ b/src/backend/replication/logical/conflict.c
@@ -17,6 +17,7 @@
 #include "access/commit_ts.h"
 #include "access/tableam.h"
 #include "executor/executor.h"
+#include "nodes/pg_list.h"
 #include "pgstat.h"
 #include "replication/conflict.h"
 #include "replication/worker_internal.h"
diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c
index 5d33559926a..4e32a29766c 100644
--- a/src/backend/storage/ipc/procsignal.c
+++ b/src/backend/storage/ipc/procsignal.c
@@ -23,6 +23,7 @@
 #include "pgstat.h"
 #include "port/pg_bitutils.h"
 #include "replication/logicalworker.h"
+#include "replication/logicalctl.h"
 #include "replication/walsender.h"
 #include "storage/condition_variable.h"
 #include "storage/ipc.h"
diff --git a/src/backend/utils/activity/pgstat_subscription.c b/src/backend/utils/activity/pgstat_subscription.c
index 500b1899188..adeff040075 100644
--- a/src/backend/utils/activity/pgstat_subscription.c
+++ b/src/backend/utils/activity/pgstat_subscription.c
@@ -25,7 +25,7 @@
  * Report a subscription error.
  */
 void
-pgstat_report_subscription_error(Oid subid, LogicalRepWorkerType wtype)
+pgstat_report_subscription_error(Oid subid, int wtype)
 {
 	PgStat_EntryRef *entry_ref;
 	PgStat_BackendSubEntry *pending;
@@ -34,7 +34,7 @@ pgstat_report_subscription_error(Oid subid, LogicalRepWorkerType wtype)
 										  InvalidOid, subid, NULL);
 	pending = entry_ref->pending;
 
-	switch (wtype)
+	switch ((LogicalRepWorkerType) wtype)
 	{
 		case WORKERTYPE_APPLY:
 			pending->apply_error_count++;
diff --git a/src/test/modules/test_custom_stats/test_custom_fixed_stats.c b/src/test/modules/test_custom_stats/test_custom_fixed_stats.c
index 908bd18a7c7..fb74f3dc5ad 100644
--- a/src/test/modules/test_custom_stats/test_custom_fixed_stats.c
+++ b/src/test/modules/test_custom_stats/test_custom_fixed_stats.c
@@ -16,8 +16,8 @@
 #include "access/htup_details.h"
 #include "funcapi.h"
 #include "pgstat.h"
-#include "utils/builtins.h"
 #include "utils/pgstat_internal.h"
+#include "utils/timestamp.h"
 
 PG_MODULE_MAGIC_EXT(
 					.name = "test_custom_fixed_stats",
diff --git a/src/test/modules/test_custom_stats/test_custom_var_stats.c b/src/test/modules/test_custom_stats/test_custom_var_stats.c
index 64a8fe63cce..da28afbd929 100644
--- a/src/test/modules/test_custom_stats/test_custom_var_stats.c
+++ b/src/test/modules/test_custom_stats/test_custom_var_stats.c
@@ -12,6 +12,7 @@
  */
 #include "postgres.h"
 
+#include "access/htup_details.h"
 #include "common/hashfn.h"
 #include "funcapi.h"
 #include "storage/dsm_registry.h"
-- 
2.46.0.519.g2e7b89e038

