From ff8ef7eefb443ad44f2e5e78c6e8b88d6174c4f9 Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Mon, 22 Mar 2021 18:14:54 +1100
Subject: [PATCH v65] Feedback - AllTablesyncsReady function simplified

---
 src/backend/replication/logical/tablesync.c | 37 ++---------------------------
 1 file changed, 2 insertions(+), 35 deletions(-)

diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 233649e..307d858 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1185,46 +1185,13 @@ FetchTableStates(bool *started_tx)
 bool
 AllTablesyncsReady(void)
 {
-	bool		found_busy = false;
+	bool		found_busy;
 	bool		started_tx = false;
-	int			count = 0;
-	ListCell   *lc;
 
 	/* We need up-to-date sync state info for subscription tables here. */
 	FetchTableStates(&started_tx);
 
-	/*
-	 * Process all not-READY tables.
-	 */
-	foreach(lc, table_states_not_ready)
-	{
-		SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc);
-
-		count++;
-		/*
-		 * When the process_syncing_tables_for_apply changes the state from
-		 * SYNCDONE to READY, that change is actually written directly into
-		 * the list element of table_states_not_ready.
-		 *
-		 * The table_states_valid flag is not immediately updated, so
-		 * FetchTableStates does not rebuild the "table_states_not_ready" list
-		 * because it is unaware that it needs to.
-		 *
-		 * It means the "table_states_not_ready" list might end up having
-		 * a READY state in it even though there was none when it was
-		 * initially created.
-		 *
-		 * This is why the code is testing for READY. And because a READY in the
-		 * table_states_not_ready list is the exception rather than the rule it
-		 * means we will nearly always break from this loop at the first
-		 * iteration.
-		 */
-		if (rstate->state != SUBREL_STATE_READY)
-		{
-			found_busy = true;
-			break;
-		}
-	}
+	found_busy = list_length(table_states_not_ready) > 0;
 
 	if (started_tx)
 	{
-- 
1.8.3.1

