From 46ea93e86c0c4125735a3a115704ad2b954c0af1 Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Mon, 8 Feb 2021 11:30:14 +1100
Subject: [PATCH v1] ReplicationSlotDropAtPubNode param.

Apply this patch on top of V28.

This patch only changes the name of the ReplicationSlotDropAtPubNode bool param which says whether to give WARNING in place of ERROR. The previous name "missing_ok" was misleading because we also want WARNING for situations where slot cannot be dropped for reasons *other* than just missing (e.g. unable to drop because connection down).
---
 src/backend/commands/subscriptioncmds.c     | 20 ++++++++++----------
 src/backend/replication/logical/tablesync.c |  4 ++--
 src/include/replication/slot.h              |  2 +-
 3 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index 1d3ca43..691140f 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -731,7 +731,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data)
 				 * does not exist yet. Also, if we fail after removing some of
 				 * the slots, next time, it will again try to drop already
 				 * dropped slots and fail. For these reasons, we allow
-				 * missing_ok = true for the drop.
+				 * error_ok = true for the drop.
 				 *
 				 * XXX If there is a network breakdown while dropping the
 				 * slots then we will give a WARNING to the user and they need
@@ -740,7 +740,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data)
 				 * this.
 				 */
 				ReplicationSlotNameForTablesync(sub->oid, sub_remove_rels[off].relid, syncslotname);
-				ReplicationSlotDropAtPubNode(wrconn, syncslotname, true /* missing_ok */);
+				ReplicationSlotDropAtPubNode(wrconn, syncslotname, true /* error_ok */);
 			}
 		}
 	}
@@ -1234,14 +1234,14 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
 			 * does not exist yet. Also, if we fail after removing some of
 			 * the slots, next time, it will again try to drop already
 			 * dropped slots and fail. For these reasons, we allow
-			 * missing_ok = true for the drop.
+			 * error_ok = true for the drop.
 			 */
 			if (rstate->state != SUBREL_STATE_SYNCDONE)
 			{
 				char		syncslotname[NAMEDATALEN] = {0};
 
 				ReplicationSlotNameForTablesync(subid, relid, syncslotname);
-				ReplicationSlotDropAtPubNode(wrconn, syncslotname, true /* missing_ok */ );
+				ReplicationSlotDropAtPubNode(wrconn, syncslotname, true /* error_ok */ );
 			}
 		}
 
@@ -1252,7 +1252,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
 		 * replication slot at the publisher.
 		 */
 		if (slotname)
-			ReplicationSlotDropAtPubNode(wrconn, slotname, false /* missing_ok */ );
+			ReplicationSlotDropAtPubNode(wrconn, slotname, false /* error_ok */ );
 
 	}
 	PG_FINALLY();
@@ -1268,11 +1268,11 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
  * Drop the replication slot at the publisher node using the replication
  * connection.
  *
- * missing_ok - if true then only issue WARNING message if the slot cannot be
- * deleted.
+ * error_ok - if true then only issue WARNING message instead of ERROR when
+ * the slot cannot dropped, because does not exist, connection broken, etc.
  */
 void
-ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
+ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool error_ok)
 {
 	StringInfoData cmd;
 
@@ -1296,9 +1296,9 @@ ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missi
 					(errmsg("dropped replication slot \"%s\" on publisher",
 							slotname)));
 		}
-		else if (res->status == WALRCV_ERROR && missing_ok)
+		else if (res->status == WALRCV_ERROR && error_ok)
 		{
-			/* WARNING. Error, but missing_ok = true. */
+			/* WARNING. Error, but error_ok = true. */
 			ereport(WARNING,
 					(errmsg("could not drop the replication slot \"%s\" on publisher",
 							slotname),
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index b636976..4b8ee33 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -357,7 +357,7 @@ process_syncing_tables_for_sync(XLogRecPtr current_lsn)
 		 * otherwise, it won't be dropped till the corresponding subscription
 		 * is dropped.
 		 */
-		ReplicationSlotDropAtPubNode(wrconn, syncslotname, false /* missing_ok */);
+		ReplicationSlotDropAtPubNode(wrconn, syncslotname, false /* error_ok */);
 
 		finish_sync_worker();
 	}
@@ -994,7 +994,7 @@ LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
 		 * breakdown then it wouldn't have succeeded so trying it next time
 		 * seems like a better bet.
 		 */
-		ReplicationSlotDropAtPubNode(wrconn, slotname, true /* missing_ok */);
+		ReplicationSlotDropAtPubNode(wrconn, slotname, true /* error_ok */);
 	}
 	else if (MyLogicalRepWorker->relstate == SUBREL_STATE_FINISHEDCOPY)
 	{
diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h
index 5f52335..e67942d 100644
--- a/src/include/replication/slot.h
+++ b/src/include/replication/slot.h
@@ -213,7 +213,7 @@ extern void ReplicationSlotsDropDBSlots(Oid dboid);
 extern void InvalidateObsoleteReplicationSlots(XLogSegNo oldestSegno);
 extern ReplicationSlot *SearchNamedReplicationSlot(const char *name);
 extern char *ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname);
-extern void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok);
+extern void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool error_ok);
 
 extern void StartupReplicationSlots(void);
 extern void CheckPointReplicationSlots(void);
-- 
1.8.3.1

