Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Started by vignesh Cabout 1 month ago25 messageshackers
Jump to latest
#1vignesh C
vignesh21@gmail.com

Hi,

While reviewing another thread at [1], I found an issue where an error
relayed from a parallel apply worker can get an unrelated CONTEXT line
from the leader process.
ProcessParallelApplyMessage() in applyparallelworker.c sets
error_context_stack to the leader's apply_error_context_stack before
calling ereport(ERROR). As a result, when errfinish() processes the
error, the leader's error context callback runs again and adds the
leader's current replication context. This can be incorrect if the
leader is processing a different transaction when it receives the
error from the parallel worker. For example, in my test:
tab_a is processed by a parallel worker and fails due to a trigger.

The leader is processing an unrelated insert into tab_b. The parallel
worker's error is received while the leader is processing tab_b. The
final error incorrectly contains context for both tab_a and tab_b. The
resulting error looks like:
ERROR: logical replication parallel apply worker exited due to error
CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE
processing remote data ... relation "public.tab_a" ...
logical replication parallel apply worker
processing remote data ... relation "public.tab_b" ...

The tab_b context is unrelated to the actual failure and comes from
the leader's current activity.
I have attached a test that reproduces the issue. The test verifies
that the relayed error contains the context for tab_a but does not
include the leader's unrelated tab_b context. It currently fails on
the second assertion. I think the error handling in
ProcessParallelApplyMessage() should avoid running the leader's
current error-context callbacks when re-throwing the worker's error.

Regards,
Vignesh

Attachments:

t254101_1
0001-Test-to-reproduce-parallel-apply-error-context-issue.patchapplication/octet-stream; name=0001-Test-to-reproduce-parallel-apply-error-context-issue.patchDownload+172-1
#2Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: vignesh C (#1)
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Dear Vignesh,

While reviewing another thread at [1], I found an issue where an error
relayed from a parallel apply worker can get an unrelated CONTEXT line
from the leader process.
ProcessParallelApplyMessage() in applyparallelworker.c sets
error_context_stack to the leader's apply_error_context_stack before
calling ereport(ERROR). As a result, when errfinish() processes the
error, the leader's error context callback runs again and adds the
leader's current replication context. This can be incorrect if the
leader is processing a different transaction when it receives the
error from the parallel worker.

Good catch and agreed your analysis.

I checked other examples in core, and parallel query seems to handle correctly.
It has an attribute ParallelContext::error_context_stack which preserves an error
context at that time, see CreateParallelContext(). When the parallel worker raises
an ERROR, the leader backend can consume its message and raise it again, and at
that time the error context is restored from the ParallelContext.

I think the easiest fix is to use apply_error_context_stack for preserving the
error context before entering the loop, attached patch implements the idea.
IIUC no contexts can be stacked at the begining of the worker, i.e.,
apply_error_context_stack can be NULL in any case. So the global variable can be
removed if we clean up more aggressively. I also felt the name can be changed,
but it's retained for now because the variable has been exposed.

BTW, I also checked the REPACK CONCURRENTLY, but it seemed to have the same issue
as the parallel apply. I locally found a reproducer and wrote a fix patch,
I can share here or another place.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

t254101_2
0001-Test-to-reproduce-parallel-apply-error-context-issue.patchapplication/octet-stream; name=0001-Test-to-reproduce-parallel-apply-error-context-issue.patchDownload+186-1
0002-Preserve-an-error-context-before-entering-an-apply-l.patchapplication/octet-stream; name=0002-Preserve-an-error-context-before-entering-an-apply-l.patchDownload+3-3
#3vignesh C
vignesh21@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#2)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

On Thu, 8 Oct 2026 at 13:23, Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

I checked other examples in core, and parallel query seems to handle correctly.
It has an attribute ParallelContext::error_context_stack which preserves an error
context at that time, see CreateParallelContext(). When the parallel worker raises
an ERROR, the leader backend can consume its message and raise it again, and at
that time the error context is restored from the ParallelContext.

I think the easiest fix is to use apply_error_context_stack for preserving the
error context before entering the loop, attached patch implements the idea.

Thanks for the patch, the issue is resolved with your patch. One suggestion:
Can we add a comment saying the saved stack deliberately excludes
apply_error_callback. That would stop someone from "fixing" it back
later:
diff --git a/src/backend/replication/logical/worker.c
b/src/backend/replication/logical/worker.c
index 37b37500213..3d331c356a6 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -4088,7 +4088,7 @@ LogicalRepApplyLoop(XLogRecPtr last_received)
        errcallback.callback = apply_error_callback;
        errcallback.previous = error_context_stack;
        error_context_stack = &errcallback;
-       apply_error_context_stack = error_context_stack;
+       apply_error_context_stack = errcallback.previous;

IIUC no contexts can be stacked at the begining of the worker, i.e.,
apply_error_context_stack can be NULL in any case. So the global variable can be
removed if we clean up more aggressively. I also felt the name can be changed,
but it's retained for now because the variable has been exposed.

BTW, I also checked the REPACK CONCURRENTLY, but it seemed to have the same issue
as the parallel apply. I locally found a reproducer and wrote a fix patch,
I can share here or another place.

I felt we can discuss here itself as the fix should be similar for
both the cases and reviewing also would be easier.

Regards,
Vignesh

#4Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: vignesh C (#3)
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Dear Vignesh,

Thanks for the patch, the issue is resolved with your patch. One suggestion:
Can we add a comment saying the saved stack deliberately excludes
apply_error_callback. That would stop someone from "fixing" it back
later:

Added. Also, I updated the comment in ProcessParallelApplyMessage().

BTW, I also checked the REPACK CONCURRENTLY, but it seemed to have the

same issue

as the parallel apply. I locally found a reproducer and wrote a fix patch,
I can share here or another place.

I felt we can discuss here itself as the fix should be similar for
both the cases and reviewing also would be easier.

OK, then let me attach and describe once. 0003 is the reproducer, and 0004 is the fix
patch.

001_repack_error_context reproduced the issue. A functional index is defined with
the injection point. While the backend waits at the point the repack worker does
the error out (also by the injection point). In this case the backend re-throws
the propagated from the repack worker, so the unrelated context can be printed.
Below is the obtained result, lines after the "SQL statement..." is not propagated
from the repack worker.

```
ERROR: error triggered for injection point repack-worker-error-context
CONTEXT: REPACK decoding worker
SQL statement "SELECT public.injection_points_run('repack-leader-error-context')"
PL/pgSQL function public.repack_error_context(integer) line 3 at PERFORM
```

This means the misleading lines in CONTEXT can be reported if the backend
installed the transient callbacks while processing tuples.

The fix idea is to preserve the context like parallel query. I added an attribute
in the DecodingWorker which controls the repack worker. It's preserved when the
worker is started, and it's restored when the backend reports the log.
It's mostly same as ProcessParallelMessage().

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

t254101_4
v2-0001-Test-to-reproduce-parallel-apply-error-context-is.patchapplication/octet-stream; name=v2-0001-Test-to-reproduce-parallel-apply-error-context-is.patchDownload+186-1
v2-0002-Preserve-an-error-context-before-entering-an-appl.patchapplication/octet-stream; name=v2-0002-Preserve-an-error-context-before-entering-an-appl.patchDownload+8-4
v2-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchapplication/octet-stream; name=v2-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchDownload+89-1
v2-0004-Preserve-an-error-context-before-launching-a-repa.patchapplication/octet-stream; name=v2-0004-Preserve-an-error-context-before-launching-a-repa.patchDownload+16-1
#5Zhijie Hou
houzhijie22@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#2)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Hi,

On Thu, Oct 8, 2026 at 3:54 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Dear Vignesh,

While reviewing another thread at [1], I found an issue where an error
relayed from a parallel apply worker can get an unrelated CONTEXT line
from the leader process.
ProcessParallelApplyMessage() in applyparallelworker.c sets
error_context_stack to the leader's apply_error_context_stack before
calling ereport(ERROR). As a result, when errfinish() processes the
error, the leader's error context callback runs again and adds the
leader's current replication context. This can be incorrect if the
leader is processing a different transaction when it receives the
error from the parallel worker.

Good catch and agreed your analysis.

I checked other examples in core, and parallel query seems to handle correctly.
It has an attribute ParallelContext::error_context_stack which preserves an error
context at that time, see CreateParallelContext(). When the parallel worker raises
an ERROR, the leader backend can consume its message and raise it again, and at
that time the error context is restored from the ParallelContext.

I think the easiest fix is to use apply_error_context_stack for preserving the
error context before entering the loop, attached patch implements the idea.
IIUC no contexts can be stacked at the begining of the worker, i.e.,
apply_error_context_stack can be NULL in any case. So the global variable can be
removed if we clean up more aggressively. I also felt the name can be changed,
but it's retained for now because the variable has been exposed.

I think it makes sense to preserve the error context before the loop, to be
consistent with what parallel query does.

But I think we'd better simply save the error context to
apply_error_context_stack before adding the error callback as that
looks better than
indirectly getting the previous one after adding a new one, and it can save
readers a few cycles. E.g.,

+apply_error_context_stack = error_context_stack;
errcallback.callback = apply_error_callback;
...

Best Regards,
Zhijie Hou

#6Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Zhijie Hou (#5)
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Dear Hou,

But I think we'd better simply save the error context to
apply_error_context_stack before adding the error callback as that
looks better than
indirectly getting the previous one after adding a new one, and it can save
readers a few cycles. E.g.,

Indeed, PSA the new version.
Also, I adjusted the order of the initialization of DecodingWorker to align with
the location of attributes.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

t254101_6
v3-0001-Test-to-reproduce-parallel-apply-error-context-is.patchapplication/octet-stream; name=v3-0001-Test-to-reproduce-parallel-apply-error-context-is.patchDownload+186-1
v3-0002-Preserve-an-error-context-before-entering-an-appl.patchapplication/octet-stream; name=v3-0002-Preserve-an-error-context-before-entering-an-appl.patchDownload+8-4
v3-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchapplication/octet-stream; name=v3-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchDownload+89-1
v3-0004-Preserve-an-error-context-before-launching-a-repa.patchapplication/octet-stream; name=v3-0004-Preserve-an-error-context-before-launching-a-repa.patchDownload+17-1
#7Takeshi Ideriha
iderihatakeshi@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#6)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Hi all

On 2026/10/09 11:34, Hayato Kuroda (Fujitsu) wrote:

Indeed, PSA the new version.
Also, I adjusted the order of the initialization of DecodingWorker to align with
the location of attributes.

I don't have any objections to the fixes and am learning from the fixes.

But let me add two minor comments.
Do we plan to commit the test codes as well?
While I was running test codes, I noticed
src/test/subscription/meson.build lacks
't/039_streaming_parallel_apply_error_context.pl line. Without this
line, meson test don't execute this test.

+ 'enable_injection_points': 'yes',

The other minor thing is, I believe we can remove this line from
injection_points/meson.build since
the repack test code does not check this environment value and without
this I was able to execute this test. Moreover, the tests under
injection_points directory just finish if injection_points is not
configured according to meson.build.

Regards,
Takeshi Ideriha

#8Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Takeshi Ideriha (#7)
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Hi Ideriha-san,

Thanks for the review. PSA v4 patch set.

I don't have any objections to the fixes and am learning from the fixes.

But let me add two minor comments.
Do we plan to commit the test codes as well?
While I was running test codes, I noticed
src/test/subscription/meson.build lacks
't/039_streaming_parallel_apply_error_context.pl line. Without this
line, meson test don't execute this test.

I think the current code won't be included to core, thus we do not take care
the shape of meson.build (I have tested with fixing it locally).
But it might be better to add for the cfbot. Done.
Also, I found the test sometimes could not launch the parallel apply worker
because of the race condition with the table synchronization. I set to
copy_data = false instead.

+ 'enable_injection_points': 'yes',

The other minor thing is, I believe we can remove this line from
injection_points/meson.build since
the repack test code does not check this environment value and without
this I was able to execute this test. Moreover, the tests under
injection_points directory just finish if injection_points is not
configured according to meson.build.

Indeed, removed. I have not checked well due to the above reason.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

t254101_8
v4-0001-Test-to-reproduce-parallel-apply-error-context-is.patchapplication/octet-stream; name=v4-0001-Test-to-reproduce-parallel-apply-error-context-is.patchDownload+192-1
v4-0002-Preserve-an-error-context-before-entering-an-appl.patchapplication/octet-stream; name=v4-0002-Preserve-an-error-context-before-entering-an-appl.patchDownload+8-4
v4-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchapplication/octet-stream; name=v4-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchDownload+86-1
v4-0004-Preserve-an-error-context-before-launching-a-repa.patchapplication/octet-stream; name=v4-0004-Preserve-an-error-context-before-launching-a-repa.patchDownload+17-1
#9Takeshi Ideriha
iderihatakeshi@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#8)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Hi Kuroda-san

Thank you for the explanation and updated patches.

I think the current code won't be included to core, thus we do not take care
the shape of meson.build (I have tested with fixing it locally).
But it might be better to add for the cfbot. Done.

Sure, It make sense to me.
This issue is not so general one and I'm +1 for not including test codes
to mainline.
I haven't checked hackers for several years and wanted to hear your
opinion about the test code commit. Thank you.

Regards,
Takeshi Ideriha

#10Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Takeshi Ideriha (#9)
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Hi,

One of my colleagues requested me to update the commit message. I did for 0002 and
0004. I did not do for 0001 and 0003 because it's not intended to be included.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

t254101_10
v5-0001-Test-to-reproduce-parallel-apply-error-context-is.patchapplication/octet-stream; name=v5-0001-Test-to-reproduce-parallel-apply-error-context-is.patchDownload+192-1
v5-0002-Avoid-adding-leader-context-to-parallel-apply-wor.patchapplication/octet-stream; name=v5-0002-Avoid-adding-leader-context-to-parallel-apply-wor.patchDownload+8-4
v5-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchapplication/octet-stream; name=v5-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchDownload+86-1
v5-0004-Avoid-adding-backend-context-to-repack-worker-mes.patchapplication/octet-stream; name=v5-0004-Avoid-adding-backend-context-to-repack-worker-mes.patchDownload+17-1
#11Ajin Cherian
itsajin@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#10)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

On Mon, Oct 26, 2026 at 7:26 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Hi,

One of my colleagues requested me to update the commit message. I did for 0002 and
0004. I did not do for 0001 and 0003 because it's not intended to be included.

+1
I've reviewed the patches and they LGTM.

regards,
Ajin Cherian
Fujitsu Australia

#12Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Ajin Cherian (#11)
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Hi,

Thanks for reviewing. Since reviewers said OK and cfbot is green,
I marked the thread as "Ready for Committer".

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#13vignesh C
vignesh21@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#10)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

On Mon, 26 Oct 2026 at 13:56, Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Hi,

One of my colleagues requested me to update the commit message. I did for 0002 and
0004. I did not do for 0001 and 0003 because it's not intended to be included.

Thanks for the updated patch.
Few minor suggestions:
1) apply_error_context_stack used to mean "the stack including
apply_error_callback", and with this patch it deliberately means the
opposite. The name now says the reverse of what the variable holds.
How about renaming it to apply_loop_outer_error_context_stack or a
better one if you have, and adding a short comment at the definition
saying that it is the stack before LogicalRepApplyLoop() pushed
apply_error_callback, and that parallel apply worker errors are
reported with it?

2) The patch reverses the choice made in 216a784829c, which states:
"Context beyond that should use the error context callbacks that were
in effect in LogicalRepApplyLoop()". The reasoning for this change is
sound. Could the commit message explicitly mention that it changes the
earlier choice and explain why?

How about something like(feel free to change it):
An error reported by a parallel apply worker is relayed by the leader
apply worker. Before rethrowing it, ProcessParallelApplyMessage()
replaces error_context_stack with a stack saved in
LogicalRepApplyLoop(), so that the context callbacks of whatever the
leader is doing at that moment (for example, a callback pushed while
applying another transaction) are not run for this error.

The saved stack was taken after apply_error_callback was pushed, so
that callback was still run. It formats the leader's own apply state
(the relation, the remote transaction and the message being applied),
which has nothing to do with the transaction that failed in the
worker. If the leader was applying another transaction at that
moment, the error got a misleading CONTEXT line naming the relation
the leader was working on, instead of just the worker's own context.

This changes what commit 216a784829c chose, namely to keep the
callbacks in effect in LogicalRepApplyLoop(), apply_error_callback
included. That is not useful here, because the worker's own context,
relayed in the error message, already says where the error happened.
Save the stack before pushing apply_error_callback.

Regards,
Vignesh

#14Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: vignesh C (#13)
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Hi Vignesh,

Thanks for the review!

Thanks for the updated patch.
Few minor suggestions:
1) apply_error_context_stack used to mean "the stack including
apply_error_callback", and with this patch it deliberately means the
opposite. The name now says the reverse of what the variable holds.
How about renaming it to apply_loop_outer_error_context_stack or a
better one if you have, and adding a short comment at the definition
saying that it is the stack before LogicalRepApplyLoop() pushed
apply_error_callback, and that parallel apply worker errors are
reported with it?

Indeed, replaced to apply_loop_outer_error_context_stack. Code comments were
added atop the declaration, so a comment in LogicalRepApplyLoop is adjusted (reduced).

2) The patch reverses the choice made in 216a784829c, which states:
"Context beyond that should use the error context callbacks that were
in effect in LogicalRepApplyLoop()". The reasoning for this change is
sound. Could the commit message explicitly mention that it changes the
earlier choice and explain why?

+1, but adjusted on my side. The commit id should be clarified at second paragraph,
and changes are clarified at the end.
I checked sentences via Grammarly.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

t254101_14
v6-0001-Test-to-reproduce-parallel-apply-error-context-is.patchapplication/octet-stream; name=v6-0001-Test-to-reproduce-parallel-apply-error-context-is.patchDownload+192-1
v6-0002-Avoid-adding-leader-apply-context-to-parallel-wor.patchapplication/octet-stream; name=v6-0002-Avoid-adding-leader-apply-context-to-parallel-wor.patchDownload+14-8
v6-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchapplication/octet-stream; name=v6-0003-Reproduce-the-mixture-of-the-error-context-by-REP.patchDownload+86-1
v6-0004-Avoid-adding-backend-context-to-repack-worker-mes.patchapplication/octet-stream; name=v6-0004-Avoid-adding-backend-context-to-repack-worker-mes.patchDownload+17-1
#15vignesh C
vignesh21@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#14)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

On Mon, 2 Nov 2026 at 11:03, Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

+1, but adjusted on my side. The commit id should be clarified at second paragraph,
and changes are clarified at the end.
I checked sentences via Grammarly.

Thanks for the updated version, I have no more comments.

Regards,
Vignesh

#16Amit Kapila
amit.kapila16@gmail.com
In reply to: vignesh C (#1)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

On Thu, Oct 8, 2026 at 11:31 AM vignesh C <vignesh21@gmail.com> wrote:

While reviewing another thread at [1], I found an issue where an error
relayed from a parallel apply worker can get an unrelated CONTEXT line
from the leader process.
ProcessParallelApplyMessage() in applyparallelworker.c sets
error_context_stack to the leader's apply_error_context_stack before
calling ereport(ERROR). As a result, when errfinish() processes the
error, the leader's error context callback runs again and adds the
leader's current replication context. This can be incorrect if the
leader is processing a different transaction when it receives the
error from the parallel worker. For example, in my test:
tab_a is processed by a parallel worker and fails due to a trigger.

The leader is processing an unrelated insert into tab_b. The parallel
worker's error is received while the leader is processing tab_b. The
final error incorrectly contains context for both tab_a and tab_b. The
resulting error looks like:
ERROR: logical replication parallel apply worker exited due to error
CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE
processing remote data ... relation "public.tab_a" ...
logical replication parallel apply worker
processing remote data ... relation "public.tab_b" ...

The tab_b context is unrelated to the actual failure and comes from
the leader's current activity.

OTOH, the leader apply worker also exited due to this ERROR, so
showing what the leader worker was doing via CONTEXT information is
also not a bad idea. However, I think it is difficult to make out with
the current display of this message. One thing we can try to see what
if ProcessParallelApplyMessage() instead of throwing an explicit ERROR
just rethrows what it gets from the parallel apply worker by using
ThrowErrorData(). The other idea to make this information meaningful
is to display it in something like:
CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE
processing remote data ... relation "public.tab_a" ...
logical replication parallel apply worker
leader apply worker was processing:
processing remote data ... relation "public.tab_b" ...

This will make it clear what the leader worker was processing during
the ERROR. I think for this we may need to register a leader specific
error-callback.

--
With Regards,
Amit Kapila.

#17Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Amit Kapila (#16)
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

Hi Amit,

Thanks for giving comment!

One thing we can try to see what
if ProcessParallelApplyMessage() instead of throwing an explicit ERROR
just rethrows what it gets from the parallel apply worker by using
ThrowErrorData().

To confirm, apply_loop_outer_error_context_stack is anyway needed even if we use
ThrowErrorData(), right? Otherwise the output can be like [1]ERROR: tab_a trigger boom CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE processing remote data for replication origin "pg_16409" during message type "INSERT" for replication target relation "public.tab_a" in transaction 667 logical replication parallel apply worker processing remote data for replication origin "pg_16409" during message type "INSERT" for replication target relation "public.tab_b" in transaction 668, finished at 0/0182EEF, context from leader
worker can exist without any marks.

IIUC ThrowErrorData() has benefits that 1) SQLSTATE can be preserved and 2) we
can make the shape of codes similar. Parallel workers and REPACK workers has the same.
But I do not have preference here. 0002 implements this.

The other idea to make this information meaningful
is to display it in something like:
CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE
processing remote data ... relation "public.tab_a" ...
logical replication parallel apply worker
leader apply worker was processing:
processing remote data ... relation "public.tab_b" ...

This will make it clear what the leader worker was processing during
the ERROR. I think for this we may need to register a leader specific
error-callback.

0003 tried the idea. Per my understanding we can introduce an ephemeral error
context to append the leader's work.

How do you feel?

[1]: ERROR: tab_a trigger boom CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE processing remote data for replication origin "pg_16409" during message type "INSERT" for replication target relation "public.tab_a" in transaction 667 logical replication parallel apply worker processing remote data for replication origin "pg_16409" during message type "INSERT" for replication target relation "public.tab_b" in transaction 668, finished at 0/0182EEF
ERROR: tab_a trigger boom
CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE
processing remote data for replication origin "pg_16409" during message type "INSERT" for replication target relation "public.tab_a" in transaction 667
logical replication parallel apply worker
processing remote data for replication origin "pg_16409" during message type "INSERT" for replication target relation "public.tab_b" in transaction 668, finished at 0/0182EEF

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

t254101_17
v7-0001-Test-to-reproduce-parallel-apply-error-context-is.patchapplication/octet-stream; name=v7-0001-Test-to-reproduce-parallel-apply-error-context-is.patchDownload+192-1
v7-0002-Avoid-adding-leader-apply-context-to-parallel-wor.patchapplication/octet-stream; name=v7-0002-Avoid-adding-leader-apply-context-to-parallel-wor.patchDownload+24-18
v7-0003-Distinguish-leader-context-in-parallel-apply-work.patchapplication/octet-stream; name=v7-0003-Distinguish-leader-context-in-parallel-apply-work.patchDownload+34-16
#18vignesh C
vignesh21@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#17)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

On Wed, 4 Nov 2026 at 19:27, Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Hi Amit,

Thanks for giving comment!

One thing we can try to see what
if ProcessParallelApplyMessage() instead of throwing an explicit ERROR
just rethrows what it gets from the parallel apply worker by using
ThrowErrorData().

To confirm, apply_loop_outer_error_context_stack is anyway needed even if we use
ThrowErrorData(), right? Otherwise the output can be like [1], context from leader
worker can exist without any marks.

IIUC ThrowErrorData() has benefits that 1) SQLSTATE can be preserved and 2) we
can make the shape of codes similar. Parallel workers and REPACK workers has the same.
But I do not have preference here. 0002 implements this.

The other idea to make this information meaningful
is to display it in something like:
CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE
processing remote data ... relation "public.tab_a" ...
logical replication parallel apply worker
leader apply worker was processing:
processing remote data ... relation "public.tab_b" ...

This will make it clear what the leader worker was processing during
the ERROR. I think for this we may need to register a leader specific
error-callback.

0003 tried the idea. Per my understanding we can introduce an ephemeral error
context to append the leader's work.

How do you feel?

Thanks for the patches.

With 0002 and 0003, the leader's entry still contains the error of the
parallel apply worker, or its context, followed by the context of the
leader's own transaction. The error of a different process then sits
next to the leader's context, and the context of the failed
transaction is logged a second time. This can be misleading.

Kuroda-san and me had an offline discussion and came up with the
following approach: The parallel apply worker will reports its error
with the context of the failed transaction, and the leader only says
that the worker failed, with its own context like:
logical replication parallel worker[17710] ERROR: tab_a trigger boom
logical replication parallel worker[17710] CONTEXT: PL/pgSQL function
public.tab_a_boom_fn() line 3 at RAISE
processing remote data for replication origin "pg_16411"
during message type "INSERT" for replication target relation
"public.tab_a" in transaction 667
logical replication apply worker[17698] ERROR: logical replication
parallel apply worker exited due to error, exiting leader as well
logical replication apply worker[17698] CONTEXT: processing remote
data for replication origin "pg_16411" during message type "INSERT"
for
replication target relation "public.tab_b" in transaction 668,
finished at 0/0177DBA0
postmaster[17642] LOG: background worker "logical replication
parallel worker" (PID 17710) exited with exit code 1
postmaster[17642] LOG: background worker "logical replication apply
worker" (PID 17698) exited with exit code 1

That way the transaction is not logged a second time, and the error of
another process is no longer attached next to the leader's context.
Thanks Kuroda-san for the offline discussion.

The attached v8 version patch has the changes to drop the copying of
the worker's context, keeps the pinned context stack, and changes the
message.
Thoughts?

Regards,
Vignesh

Attachments:

t254101_18
v8-0001-Fix-the-error-context-of-the-leader-apply-worker-.patchapplication/octet-stream; name=v8-0001-Fix-the-error-context-of-the-leader-apply-worker-.patchDownload+3-22
#19Dilip Kumar
dilipbalaut@gmail.com
In reply to: vignesh C (#18)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

On Thu, Nov 5, 2026 at 2:38 PM vignesh C <vignesh21@gmail.com> wrote:

On Wed, 4 Nov 2026 at 19:27, Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Hi Amit,

Thanks for giving comment!

One thing we can try to see what
if ProcessParallelApplyMessage() instead of throwing an explicit ERROR
just rethrows what it gets from the parallel apply worker by using
ThrowErrorData().

To confirm, apply_loop_outer_error_context_stack is anyway needed even if we use
ThrowErrorData(), right? Otherwise the output can be like [1], context from leader
worker can exist without any marks.

IIUC ThrowErrorData() has benefits that 1) SQLSTATE can be preserved and 2) we
can make the shape of codes similar. Parallel workers and REPACK workers has the same.
But I do not have preference here. 0002 implements this.

The other idea to make this information meaningful
is to display it in something like:
CONTEXT: PL/pgSQL function public.tab_a_boom_fn() line 3 at RAISE
processing remote data ... relation "public.tab_a" ...
logical replication parallel apply worker
leader apply worker was processing:
processing remote data ... relation "public.tab_b" ...

This will make it clear what the leader worker was processing during
the ERROR. I think for this we may need to register a leader specific
error-callback.

0003 tried the idea. Per my understanding we can introduce an ephemeral error
context to append the leader's work.

How do you feel?

Thanks for the patches.

With 0002 and 0003, the leader's entry still contains the error of the
parallel apply worker, or its context, followed by the context of the
leader's own transaction. The error of a different process then sits
next to the leader's context, and the context of the failed
transaction is logged a second time. This can be misleading.

Kuroda-san and me had an offline discussion and came up with the
following approach: The parallel apply worker will reports its error
with the context of the failed transaction, and the leader only says
that the worker failed, with its own context like:
logical replication parallel worker[17710] ERROR: tab_a trigger boom
logical replication parallel worker[17710] CONTEXT: PL/pgSQL function
public.tab_a_boom_fn() line 3 at RAISE
processing remote data for replication origin "pg_16411"
during message type "INSERT" for replication target relation
"public.tab_a" in transaction 667
logical replication apply worker[17698] ERROR: logical replication
parallel apply worker exited due to error, exiting leader as well
logical replication apply worker[17698] CONTEXT: processing remote
data for replication origin "pg_16411" during message type "INSERT"
for
replication target relation "public.tab_b" in transaction 668,
finished at 0/0177DBA0
postmaster[17642] LOG: background worker "logical replication
parallel worker" (PID 17710) exited with exit code 1
postmaster[17642] LOG: background worker "logical replication apply
worker" (PID 17698) exited with exit code 1

That way the transaction is not logged a second time, and the error of
another process is no longer attached next to the leader's context.
Thanks Kuroda-san for the offline discussion.

The attached v8 version patch has the changes to drop the copying of
the worker's context, keeps the pinned context stack, and changes the
message.
Thoughts?

Some observation while started reading the patch, I haven't done the
complete review yet, will do in sometime.

1.
/*
* Context beyond that should use the error context callbacks
* that were in effect in LogicalRepApplyLoop().
@@ -1052,13 +1035,12 @@ ProcessParallelApplyMessage(StringInfo msg)
error_context_stack = apply_error_context_stack;

IMHO in this comment "Context beyond that" refers to "edata.context",
which was just deleted above this comment. So, it seems we need to
adjust this comment?

2.
     ereport(ERROR,
  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
- errmsg("logical replication parallel apply worker exited due to error"),
- errcontext("%s", edata.context)));
+ errmsg("logical replication parallel apply worker exited due to
error, exiting leader as well")));

Do we really need ", exiting leader as well"" part of the error
message? I mean if we just say "logical replication parallel apply
worker exited due to error" isn't it obvious that leader will be
exited as well with elevel ERROR. Like other places[1]ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("lost connection to the logical replication parallel apply worker"))); we just say
"lost connection to the logical replication parallel apply worker" and
that means leader will also be exited, I do not like the pattern of
saying "exiting leader as well"

[1]: ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("lost connection to the logical replication parallel apply worker")));
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("lost connection to the logical replication parallel apply worker")));

--
Regards,
Dilip Kumar
Google

#20Amit Kapila
amit.kapila16@gmail.com
In reply to: Dilip Kumar (#19)
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication

On Thu, Nov 5, 2026 at 4:52 PM Dilip Kumar <dilipbalaut@gmail.com> wrote:

2.
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
- errmsg("logical replication parallel apply worker exited due to error"),
- errcontext("%s", edata.context)));
+ errmsg("logical replication parallel apply worker exited due to
error, exiting leader as well")));

Do we really need ", exiting leader as well"" part of the error
message? I mean if we just say "logical replication parallel apply
worker exited due to error" isn't it obvious that leader will be
exited as well with elevel ERROR.

Yes, but also note that on error/stop of leader apply worker, other
parallel apply workers will also be terminated. See
logicalrep_worker_onexit. How about slightly more descriptive message
as follows or something on those lines:
errmsg("logical replication parallel apply worker exited due to error"),
errdetail("The apply worker will now exit, which also stops the other
parallel apply workers.")

--
With Regards,
Amit Kapila.

#21Dilip Kumar
dilipbalaut@gmail.com
In reply to: Amit Kapila (#20)
#22vignesh C
vignesh21@gmail.com
In reply to: Dilip Kumar (#21)
#23Zhijie Hou
houzhijie22@gmail.com
In reply to: vignesh C (#22)
#24Amit Kapila
amit.kapila16@gmail.com
In reply to: Zhijie Hou (#23)
#25Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Amit Kapila (#24)