Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
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
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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
vignesh C <vignesh21@gmail.com>
Дата:
On Thu, 8 Oct 2026 at 13:23, Hayato Kuroda (Fujitsu)
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
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Вложения:
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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Hi, On Thu, Oct 8, 2026 at 3:54 PM Hayato Kuroda (Fujitsu) 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
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Вложения:
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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Takeshi Ideriha <iderihatakeshi@gmail.com>
Дата:
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
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Вложения:
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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Takeshi Ideriha <iderihatakeshi@gmail.com>
Дата:
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
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Вложения:
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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Mon, Oct 26, 2026 at 7:26 PM Hayato Kuroda (Fujitsu) 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
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
vignesh C <vignesh21@gmail.com>
Дата:
On Mon, 26 Oct 2026 at 13:56, Hayato Kuroda (Fujitsu) 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
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Вложения:
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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
vignesh C <vignesh21@gmail.com>
Дата:
On Mon, 2 Nov 2026 at 11:03, Hayato Kuroda (Fujitsu) 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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Thu, Oct 8, 2026 at 11:31 AM vignesh C 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.
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], 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
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
On Wed, 4 Nov 2026 at 19:27, Hayato Kuroda (Fujitsu)
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
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Dilip Kumar <dilipbalaut@gmail.com>
Дата:
On Thu, Nov 5, 2026 at 2:38 PM vignesh C wrote:
>
> On Wed, 4 Nov 2026 at 19:27, Hayato Kuroda (Fujitsu)
> 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] 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")));
--
Regards,
Dilip Kumar
Google
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Thu, Nov 5, 2026 at 4:52 PM Dilip Kumar 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.
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Dilip Kumar <dilipbalaut@gmail.com>
Дата:
On Fri, Nov 6, 2026 at 10:59 AM Amit Kapila wrote:
>
> On Thu, Nov 5, 2026 at 4:52 PM Dilip Kumar 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.")
>
Yeah this is better. I just have a suggestion that might sound better,
but feel free to ignore it.
errmsg("logical replication parallel apply worker exited due to error"),
errdetail("The leader apply worker will now exit and terminate any
other parallel apply workers.")
--
Regards,
Dilip Kumar
Google
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
On Fri, 6 Nov 2026 at 11:34, Dilip Kumar wrote:
>
> Yeah this is better. I just have a suggestion that might sound better,
> but feel free to ignore it.
>
> errmsg("logical replication parallel apply worker exited due to error"),
> errdetail("The leader apply worker will now exit and terminate any
> other parallel apply workers.")
Thanks for the comments, the updated v9 version patch has the changes
to fix the same. The other comment regarding stale comments from [1]
also is addressed in this version.
[1] - https://www.postgresql.org/message-id/CAFiTN-sKca5sRc1xVL9ghdYT%3DhKtzoNoKMVwbrE1G55J0pdC9A%40mail.gmail.com
Regards,
Vignesh
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Zhijie Hou <houzhijie22@gmail.com>
Дата:
Hi,
On Tue, Nov10, 2026 at 01:35 AM vignesh C wrote:
> Thanks for the comments, the updated v9 version patch has the changes
> to fix the same. The other comment regarding stale comments from [1]
> also is addressed in this version. [1] -
> https://www.postgresql.org/message-id/CAFiTN-sKca5sRc1xVL9ghdYT%3DhKtzoNoKMVwbrE1G55J0pdC9A%40mail.gmail.com
Thanks for updating the patch.
I have two minor comments:
1. It looks unnecessary to change this comment:
/*
- * The actual error must have been reported by the parallel
- * apply worker.
+ * The parallel apply worker did error-out, the leader apply
+ * worker must exit as well.
*/
2. Since we removed this local variable, the '{' also becomes removable,
as we normally avoid adding it under case condition when no local
variable is declared.
{
- ErrorData edata;
-
- /* Parse ErrorResponse. */
- pq_parse_errornotice(msg, &edata);
The other part looks good to me.
--
Best Regards,
Zhijie Hou
Re: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
Amit Kapila <amit.kapila16@gmail.com>
Дата:
On Tue, Nov 10, 2026 at 10:43 AM Zhijie Hou wrote: > > On Tue, Nov10, 2026 at 01:35 AM vignesh C wrote: > > Thanks for the comments, the updated v9 version patch has the changes > > to fix the same. The other comment regarding stale comments from [1] > > > The other part looks good to me. > Changed as per your comments and pushed. I only pushed it to HEAD as this is an error message improvement patch and we didn't see any user report yet. -- With Regards, Amit Kapila.
RE: Incorrect CONTEXT reported for errors from parallel apply worker in logical replication
От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
Hi, > Changed as per your comments and pushed. I only pushed it to HEAD as > this is an error message improvement patch and we didn't see any user > report yet. Thanks for the push. I found that some codes for parallel apply workers can be refactored based on the change, see [1]. This thread also had a proposal for REPACK worker, but I'm planning to fork new thread. Let's close this thread. [1]: https://www.postgresql.org/message-id/OS7PR01MB18317CA08E7D389B8A8C5F63AF57C2%40OS7PR01MB18317.jpnprd01.prod.outlook.com Best regards, Hayato Kuroda FUJITSU LIMITED