v4-0002-Apply-worker-will-get-started-after-180-seconds-b.patch

text/x-patch

Filename: v4-0002-Apply-worker-will-get-started-after-180-seconds-b.patch
Type: text/x-patch
Part: 1
Message: Re: Race condition in FetchTableStates() breaks synchronization of subscription tables

Patch

Format: format-patch
Series: patch v4-0002
Subject: Apply worker will get started after 180 seconds by the launcher in case the apply worker exits immediately after startup.
File+
src/backend/replication/logical/launcher.c 13 0
From 817dd4089d7c9c3e4481d0718a0a269f7f0f30c5 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignesh21@gmail.com>
Date: Mon, 5 Feb 2024 14:55:48 +0530
Subject: [PATCH v4 2/2] Apply worker will get started after 180 seconds by the
 launcher in case the apply worker exits immediately after startup.

Apply worker was getting started after 180 seconds tiemout of the
launcher in case the apply worker exits immediately after startup. This
was happening because the launcher process's latch was getting reset
after the apply worker was started, which resulted in the launcher to
wait for the next 180 seconds timeout before starting the apply worker.
Fixed this issue by not setting the latch in case the apply worker has
exited.
---
 src/backend/replication/logical/launcher.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c
index 122db0bb13..2c750e2cd6 100644
--- a/src/backend/replication/logical/launcher.c
+++ b/src/backend/replication/logical/launcher.c
@@ -233,6 +233,19 @@ WaitForReplicationWorkerAttach(LogicalRepWorker *worker,
 
 		if (rc & WL_LATCH_SET)
 		{
+			bool worker_in_use;
+
+			LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
+			worker_in_use = worker->in_use;
+			LWLockRelease(LogicalRepWorkerLock);
+
+			/*
+			 * Latch must not be reset in case of the apply worker exited
+			 * immediately after start.
+			 */
+			if (!worker_in_use)
+				return worker_in_use;
+
 			ResetLatch(MyLatch);
 			CHECK_FOR_INTERRUPTS();
 		}
-- 
2.34.1