Re: Interrupts vs signals

From: Greg Burd <greg(at)burd(dot)me>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, Robert Haas <robertmhaas(at)gmail(dot)com>, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
Subject: Re: Interrupts vs signals
Date: 2026-12-03 15:36:09
Message-ID: 179624030610.1088661.9628909319697334809@burd.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Dec 2, 2026 at 5:38 PM Heikki Linnakangas <hlinnaka(at)iki(dot)fi> wrote:

Hi Heikki,

Thanks for pushing back on this. I thought I'd find out just how shaky
it was on master for fun. Both behaviors you describe have a hole I can
reproduce. I don't want to put patches onto your thread that are only
tangentially related, so I've attached patches with .nocfbot ignores
them. Maybe these should be discussed in a separate thread?

0001: a parallel worker's error that is caught in the leader can leave
the leader waiting forever for a worker that already exited. It sets
ParallelMessagePending again when an error escapes the reader, which is
v17's PG_TRY() applied to master. The other way would be to read every
queue before rethrowing the first error, but that reorders what the
leader reports; with the flag set again, the next CHECK_FOR_INTERRUPTS()
finds the detached queue and reports it.

0002: the "parallel apply worker exited due to error" report becomes
FATAL when the leader is in a subtransaction, and stays an ERROR
otherwise. Unconditional FATAL loses disable_on_error, details below.

If you agree with 0001, then your v17 patches' 0001 and 0008 can keep
their PG_TRY() as is.

> If ProccessParallelMessages() throws an ERROR, the rest of the
> messages won't be processed, but that's OK, because all the workers
> are part of the same parallel context, and they will be shut down on
> error cleanup.

Not when the error is caught. Since 0075d78947e (PG17), a PARALLEL SAFE
PL/pgSQL function can open an exception block while it runs in the
leader. If the worker's ErrorResponse is thrown in there, only that
inner subtransaction aborts. The ParallelContext belongs to an outer
one, so it survives, and nothing shuts the workers down. If the worker
has already exited by the time the leader reads the ErrorResponse, its
exit signal was folded into the same ParallelMessagePending, so nothing
ever reads its queue again, and WaitForParallelWorkersToFinish() waits
for a worker that is gone.

To force that timing I SIGSTOP the leader for 1.5s while the worker
raises and exits:

master hangs in ParallelFinish until
statement_timeout (60s), 3 of 3 runs
REL_17_STABLE same, 1 of 1
master + 0001 "lost connection to parallel worker", 2s
v17 same as master + 0001
v17 minus its PG_TRY hangs

Without the SIGSTOP the exit signal arrives on its own, and every build
reports "lost connection" after 1s. So on master this is a narrow race,
but it's the bug Robert's review found, and it doesn't need the shared
flag.

> However, if that happens in the PL/pgSQL block, the exception handler
> will catch that ERROR, abort just the subtransaction, and continue
> running. I wonder if that should be FATAL? Or if we should do some other
> more explicit cleanup, instead of relying on process exit?

I reproduced that with a trigger on a replicated table that sleeps
inside an exception block while the parallel apply worker fails. On
master the trigger logs "trigger caught: logical replication parallel
apply worker exited due to error", and the leader commits the trigger's
transaction. It only exits when the next change for the dead worker's
transaction arrives, with "could not send data to shared-memory queue".

FATAL, but not unconditionally. Plain FATAL skips start_apply()'s
PG_CATCH(), so disable_on_error never disables the subscription; the
leader restarts and fails every half second instead. It also breaks
023_twophase_stream, which waits for this ERROR in the log.

So 0002 uses IsSubTransaction() ? FATAL : ERROR. As far as I can find,
the leader never starts a subtransaction of its own (pa_start_subtrans()
runs only in the parallel apply worker), so being in one means user code
is running. report_recovery_conflict() makes the same
IsSubTransaction() test for a similar reason.

With 0002 the trigger catches nothing and the leader exits at once, and
disable_on_error still works.

Both look back-patchable, 0001 to 17 and 0002 to 16. 0001 applies to 18
and 19 as is but not to 17, which has the older function names. 0002
needs rewording there, since the back branches pass the worker's context
instead of master's errdetail. I haven't built back-branch versions
yet. Would it make sense to you to start a separate thread for these
and finish the back porting?

best.

-greg

Attachment Content-Type Size
0002-Make-a-parallel-apply-worker-s-error-FATAL-inside-a-.patch.nocfbot application/octet-stream 2.6 KB
0001-Don-t-lose-parallel-worker-messages-behind-a-caught-.patch.nocfbot application/octet-stream 4.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jan Nidzwetzki 2026-12-03 15:36:56 Re: Standby writes pages past minRecoveryPoint after crash recovery
Previous Message Matthias van de Meent 2026-12-03 15:23:47 Re: [WIP] Adding a { LOCK ROWS | LOCK KEY INDEX } clause to FK definitions