From 4e4750713fab4e7f3e1fd43d0bc8945b3797b392 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Mon, 24 Jun 2024 04:21:32 +0000
Subject: [PATCH v2 2/2] pg_createsubscriber: Warn the two-phase is disabled
 for logical replication

For now, pg_createsubscriber sets up with the two-phase commit disabled because
the setting is a default behaivor of logical replication. This commit adds the
description in the doc, and output warning when max_prepared_tranasctions > 0
on the publisher node.
---
 doc/src/sgml/ref/pg_createsubscriber.sgml   |  6 ++++++
 src/bin/pg_basebackup/pg_createsubscriber.c | 23 ++++++++++++++++++---
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/doc/src/sgml/ref/pg_createsubscriber.sgml b/doc/src/sgml/ref/pg_createsubscriber.sgml
index 2ee6eee9e3..4cc4b7c741 100644
--- a/doc/src/sgml/ref/pg_createsubscriber.sgml
+++ b/doc/src/sgml/ref/pg_createsubscriber.sgml
@@ -353,6 +353,12 @@ PostgreSQL documentation
     <application>pg_createsubscriber</application>.
    </para>
 
+   <para>
+    For now, <application>pg_createsubscriber</application> sets up logical
+    replication with the two-phase commit disabled.  This restriction may be
+    removed in future versions.
+   </para>
+
    <para>
     <application>pg_createsubscriber</application> changes the system
     identifier using <application>pg_resetwal</application>.  It would avoid
diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c
index 8ed10f010b..0230960110 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -820,6 +820,7 @@ check_publisher(const struct LogicalRepInfo *dbinfo)
 	int			cur_repslots;
 	int			max_walsenders;
 	int			cur_walsenders;
+	int			max_prepared_transactions;
 
 	pg_log_info("checking settings on publisher");
 
@@ -860,9 +861,12 @@ check_publisher(const struct LogicalRepInfo *dbinfo)
 				 "WHERE name = 'max_wal_senders'), "
 				 "cur_mws AS "
 				 "(SELECT count(*) AS cmws FROM pg_catalog.pg_stat_activity "
-				 "WHERE backend_type = 'walsender') "
-				 "SELECT wallevel, tmrs, cmrs, tmws, cmws "
-				 "FROM wl, total_mrs, cur_mrs, total_mws, cur_mws");
+				 "WHERE backend_type = 'walsender'), "
+				 "cur_mpt AS "
+				 "(SELECT setting AS mpt FROM pg_catalog.pg_settings "
+				 "WHERE name = 'max_prepared_transactions') "
+				 "SELECT wallevel, tmrs, cmrs, tmws, cmws, mpt "
+				 "FROM wl, total_mrs, cur_mrs, total_mws, cur_mws, cur_mpt");
 
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
@@ -876,6 +880,7 @@ check_publisher(const struct LogicalRepInfo *dbinfo)
 	cur_repslots = atoi(PQgetvalue(res, 0, 2));
 	max_walsenders = atoi(PQgetvalue(res, 0, 3));
 	cur_walsenders = atoi(PQgetvalue(res, 0, 4));
+	max_prepared_transactions = atoi(PQgetvalue(res, 0, 5));
 
 	PQclear(res);
 
@@ -884,6 +889,8 @@ check_publisher(const struct LogicalRepInfo *dbinfo)
 	pg_log_debug("publisher: current replication slots: %d", cur_repslots);
 	pg_log_debug("publisher: max_wal_senders: %d", max_walsenders);
 	pg_log_debug("publisher: current wal senders: %d", cur_walsenders);
+	pg_log_debug("publisher: max_prepared_transactions: %d",
+				 max_prepared_transactions);
 
 	disconnect_database(conn, false);
 
@@ -911,6 +918,16 @@ check_publisher(const struct LogicalRepInfo *dbinfo)
 		failed = true;
 	}
 
+	if (max_prepared_transactions != 0)
+	{
+		pg_log_warning("publisher should set max_prepared_transactions to zero, "
+					   "but now it is set to %d",
+					   max_prepared_transactions);
+		pg_log_warning_detail("Subscriptions will be created with the two_phase disabled. "
+							  "Transactions will not be replicated at PREPARE. "
+							  "They will be done at COMMIT PREPARED.");
+	}
+
 	pg_free(wal_level);
 
 	if (failed)
-- 
2.43.0

