Patch
Drain the parallel apply worker's error queue in the leader
- Branch
- master
- Risk
- 15
- Base commit
- 7fc2f0423320e02beb8c44b863f1827604c47c75
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1e719-c52f-703f-9e09-60fa135e7f99 | jq -r .body | git am
From 9de72ea00abf2f118776ada508a060a38259fa4b Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Mon, 16 Nov 2026 17:14:25 -0800
Subject: [PATCH] Drain the parallel apply worker's error queue in the leader
ProcessParallelApplyMessages() read a single message from each parallel
apply worker's error queue per invocation. A parallel apply worker
signals the leader once per message it sends, but signals arriving while
the leader is still processing an earlier one merge into a single
invocation, so the leader could never catch up with a burst of messages
and the queue eventually filled. Once that happened, both processes
waited forever: the worker waited in mq_putmessage() for the leader to
free space, but shm_mq_receive() reports consumed bytes to the sender,
and wakes it, only after more than a quarter of the ring has been
consumed or when it is about to return SHM_MQ_WOULD_BLOCK, neither of
which a single small read triggers. The leader in turn waited in
pa_wait_for_xact_finish() for the worker to finish its transaction.
The subscription stalled until the apply worker was terminated, and
the same transaction stalled again once it was replayed.
Any ENABLE ALWAYS or ENABLE REPLICA trigger that raises more NOTICEs or
WARNINGs than fit into the 16kB error queue while a streamed transaction
is applied by a parallel apply worker is enough to trigger this.
Fix by reading messages until the queue would block, as
HandleParallelMessages() has always done for parallel query workers.
Add a test to 015_stream.pl that applies a streamed transaction whose
rows fire a trigger raising many NOTICEs.
Back-patch to 16, where parallel apply was introduced.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
---
.../replication/logical/applyparallelworker.c | 45 +++++++++++++------
src/test/subscription/t/015_stream.pl | 35 +++++++++++++++
2 files changed, 66 insertions(+), 14 deletions(-)
diff --git a/src/backend/replication/logical/applyparallelworker.c b/src/backend/replication/logical/applyparallelworker.c
index a6d58ad0161..c060f5c21ad 100644
--- a/src/backend/replication/logical/applyparallelworker.c
+++ b/src/backend/replication/logical/applyparallelworker.c
@@ -1112,23 +1112,40 @@ ProcessParallelApplyMessages(void)
if (!winfo->error_mq_handle)
continue;
- res = shm_mq_receive(winfo->error_mq_handle, &nbytes, &data, true);
-
- if (res == SHM_MQ_WOULD_BLOCK)
- continue;
- else if (res == SHM_MQ_SUCCESS)
+ /*
+ * Read as many messages as we can from this worker, stopping when no
+ * more can be read without blocking, as HandleParallelMessages() does.
+ * The worker signals us once per message, and signals that arrive
+ * while we are still processing an earlier one merge into a single
+ * call of this function, so reading a single message per call would
+ * leave messages behind. Worse, shm_mq_receive() reports the bytes
+ * it consumed to the sender, and wakes it, only once more than a
+ * quarter of the ring has been consumed or when it is about to return
+ * SHM_MQ_WOULD_BLOCK. A worker blocked on a full queue would thus
+ * never be woken, and the leader, waiting for that worker to finish
+ * its transaction, would wait forever as well.
+ */
+ for (;;)
{
- StringInfoData msg;
+ res = shm_mq_receive(winfo->error_mq_handle, &nbytes, &data,
+ true);
- initStringInfo(&msg);
- appendBinaryStringInfo(&msg, data, nbytes);
- ProcessParallelApplyMessage(&msg);
- pfree(msg.data);
+ if (res == SHM_MQ_WOULD_BLOCK)
+ break;
+ else if (res == SHM_MQ_SUCCESS)
+ {
+ StringInfoData msg;
+
+ initStringInfo(&msg);
+ appendBinaryStringInfo(&msg, data, nbytes);
+ ProcessParallelApplyMessage(&msg);
+ pfree(msg.data);
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("lost connection to the logical replication parallel apply worker")));
}
- else
- ereport(ERROR,
- (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
- errmsg("lost connection to the logical replication parallel apply worker")));
}
MemoryContextSwitchTo(oldcontext);
diff --git a/src/test/subscription/t/015_stream.pl b/src/test/subscription/t/015_stream.pl
index ac96bc3f009..7ef54183cd5 100644
--- a/src/test/subscription/t/015_stream.pl
+++ b/src/test/subscription/t/015_stream.pl
@@ -322,6 +322,41 @@ $result =
is($result, qq(15000),
'parallel apply worker replayed all changes from file');
+# Test that a streamed transaction is applied even when applying it emits
+# many more NOTICEs than fit into the error queue through which the parallel
+# apply worker sends such messages to the leader apply worker. The trigger
+# is ENABLE ALWAYS so that it fires in the apply worker.
+$node_subscriber->append_conf('postgresql.conf',
+ 'debug_logical_replication_streaming = buffered');
+$node_subscriber->reload;
+
+# Run a query to make sure that the reload has taken effect.
+$node_subscriber->safe_psql('postgres', q{SELECT 1});
+
+$node_subscriber->safe_psql(
+ 'postgres', q{
+CREATE FUNCTION notice_trigger_func() RETURNS trigger LANGUAGE plpgsql AS $$
+BEGIN
+ FOR i IN 1..5 LOOP
+ RAISE NOTICE '%', repeat('x', 150);
+ END LOOP;
+ RETURN NEW;
+END $$;
+CREATE TRIGGER notice_trigger BEFORE INSERT ON test_tab_2
+ FOR EACH ROW EXECUTE FUNCTION notice_trigger_func();
+ALTER TABLE test_tab_2 ENABLE ALWAYS TRIGGER notice_trigger;
+});
+
+$node_publisher->safe_psql('postgres',
+ "INSERT INTO test_tab_2 SELECT i FROM generate_series(1, 5000) s(i)");
+
+$node_publisher->wait_for_catchup($appname);
+
+$result =
+ $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_2");
+is($result, qq(20000),
+ 'streamed transaction raising many NOTICEs was applied');
+
$node_subscriber->stop;
$node_publisher->stop;
base-commit: 7fc2f0423320e02beb8c44b863f1827604c47c75
--
2.53.0