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

Attachment: 0001-Test-to-reproduce-parallel-apply-error-context-issue.patch
Description: Binary data

Reply via email to