From 5230dd361e0509880572cc1fac23176f48f0d4cd Mon Sep 17 00:00:00 2001
From: Baji Shaik <baji.pgdev@gmail.com>
Date: Fri, 23 Oct 2026 12:37:02 -0500
Subject: [PATCH] Fall back to serializing when parallel apply runs out of DSM
 segments

pa_setup_dsm() checked for a NULL segment and the caller falls back to
having the leader serialize the streamed transaction, but dsm_create()
was called without DSM_CREATE_NULL_IF_MAXSEGMENTS, so running out of DSM
segments raised an ERROR instead.  The leader apply worker then exited
and restarted, failing again at the same STREAM START until segments
were freed, and replication made no progress meanwhile.

Pass DSM_CREATE_NULL_IF_MAXSEGMENTS, as parallel query does, so the
existing fallback is used.

Author: Baji Shaik <baji.pgdev@gmail.com>
Backpatch-through: 16
---
 src/backend/replication/logical/applyparallelworker.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/backend/replication/logical/applyparallelworker.c b/src/backend/replication/logical/applyparallelworker.c
index 96bf499e49c..e6887c32c6c 100644
--- a/src/backend/replication/logical/applyparallelworker.c
+++ b/src/backend/replication/logical/applyparallelworker.c
@@ -357,8 +357,12 @@ pa_setup_dsm(ParallelApplyWorkerInfo *winfo)
 	shm_toc_estimate_keys(&e, 3);
 	segsize = shm_toc_estimate(&e);
 
-	/* Create the shared memory segment and establish a table of contents. */
-	seg = dsm_create(shm_toc_estimate(&e), 0);
+	/*
+	 * Create the shared memory segment and establish a table of contents.
+	 * If no segment is available, return false so that the caller applies
+	 * the transaction itself.
+	 */
+	seg = dsm_create(segsize, DSM_CREATE_NULL_IF_MAXSEGMENTS);
 	if (!seg)
 		return false;
 
-- 
2.54.0 (Apple Git-157)

