From e3e8335fede955bda99fc896f1f44a8249113e39 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Mon, 28 Apr 2025 17:12:47 +0900 Subject: [PATCH v2 2/2] Optimize slot reuse after garbage collection in logicalrep_worker_launch(). Previously, when logicalrep_worker_launch() ran garbage collection and cleaned up at least one worker slot, it would rescan all worker slots to find a free one. However, since it is guaranteed that at least one slot was freed in this case, this additional scan was unnecessary. This commit removes the redundant scan and makes logicalrep_worker_launch() immediately reuse the freed slot. --- src/backend/replication/logical/launcher.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c index ac95afe4bae..400f06de5af 100644 --- a/src/backend/replication/logical/launcher.c +++ b/src/backend/replication/logical/launcher.c @@ -336,7 +336,6 @@ logicalrep_worker_launch(LogicalRepWorkerType wtype, */ LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE); -retry: /* Find unused worker slot. */ for (i = 0; i < max_logical_replication_workers; i++) { @@ -386,11 +385,21 @@ retry: logicalrep_worker_cleanup(w); did_cleanup = true; + + if (worker == NULL) + { + worker = w; + slot = i; + } } } + /* + * Count the current number of sync and parallel apply workers again, + * since garbage collection may have changed it. + */ if (did_cleanup) - goto retry; + logicalrep_worker_count(subid, &nsyncworkers, &nparallelapplyworkers); } /* -- 2.49.0