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, 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-01 16:41:09
Message-ID: 179613661287.1822608.15821338901696831941@burd.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Dec 1, 2026 at 2:03 AM Heikki Linnakangas <hlinnaka(at)iki(dot)fi> wrote:
> Thanks, here's a new version, which includes fixes for most of the
> issues you listed, and a whole bunch of other little fixes and cleanups.

Hi Heikki,

Thanks for folding those in, and for the separate spinlock commit; it
makes the SendInterruptWithPid() change much easier to review.

v16 is your v15 rebased onto 026b3b282a4, with one fix in 0008 for a
regression I found while testing it, plus pgindent's two whitespace
changes in interrupt.c and interrupt.h. 0001-0007 are unchanged.

0008: query cancel in single-user mode. On v15, SIGINT during a query
in postgres --single isn't acted on until the query's own wait ends.
SELECT pg_sleep(20) canceled 2 s in comes back after about 18 s;
master cancels it in 3-5 ms. Under the postmaster both take 2-7 ms.

The cause is that InitStandaloneProcess() calls
InitializeWaitEventSupport() before pqinitmask(), so the
sigaddset(&UnBlockSig, SIGURG) for the signalfd is wiped out, and the
standalone backend runs with SIGURG unblocked. That's true on master
too.

The SIGURG that RaiseInterrupt() sends to itself is then handled as a
signal and only makes epoll_wait() return EINTR. Master's
WaitEventSetWait() rechecks latch->is_set at the top of its loop, so it
wakes anyway; the series checks InterruptPending() once before the loop
and on EINTR goes back to sleep.

v16 puts InitStandaloneProcess() in the same order as
InitPostmasterChild(), pqinitmask() first, after which SIGURG is blocked
and the signalfd sees the wakeup. Rechecking InterruptPending() after
EINTR also fixes the symptom, but leaves the standalone backend with
SIGURG unblocked, which nothing intends. The repro is in the attached
notes.txt.

> I fixed this in a slightly different way. report_recovery_conflict() now
> raises INTERRUPT_QUERY_CANCEL to "defer" the cancellations. Because
> query cancel is disabled at that point, the bit stays pending until
> query cancel is re-enabled. ProcessQueryCancelInterrupt() then throws
> the "canceling statement due to conflict with recovery" error.

Nice, that's better than my ResumeQueryCancelInterrupt(). It also
covers the error-recovery path, since PostgresMain() clears
RecoveryConflictCancelPending along with the cancel bit.

> On Windows, your fix depends on the order that the
> shared_preload_libraries are loaded. That seems shaky;
> shared_preload_libraries can be changed and the config file reloaded.

Agreed that it needs more thought, I'm not a Windows expert either. It's
PGC_POSTMASTER, so a reload can't change it in a running cluster, but on
Windows each child loads the libraries itself, and nothing checks that
it hands out the same bits as the postmaster did.

> Oh, I've never thought that a bgworker would look at its own
> bgw_notify_pid field. Why would you do that?

To find the backend that started it. test_shm_mq's worker does
BackendPidGetProc(MyBgworkerEntry->bgw_notify_pid) on master; the series
changes that module to pass the proc number in shared memory instead.
I'd guess some extensions copied it, but keeping the field so they still
compile is the right call.

> I could not reproduce the NOTIFY slowdown. Can you still reproduce it?
> Can you share the benchmark script you used, please?

No, it doesn't reproduce here either. Rerun on a 48-core Xeon with
release builds, the senders are about 6% faster with the series when the
listeners sit in pg_sleep(), and the same when they are idle. The
listener side does reproduce: the series' listeners complete about 7%
more transactions, because a NOTIFY no longer wakes them out of
pg_sleep(). So in retrospect the v14 number was the two groups trading
CPU on a 32-vCPU VM, not a regression. The script and both sets of
numbers are in the attached notes.txt.

v16 attached, rebased onto 026b3b282a4.

-greg

Attachment Content-Type Size
v16-0002-Use-WaitLatch-for-the-timeouts-in-recovery-confl.patch application/octet-stream 16.6 KB
v16-0003-Refactor-datachecksums-launcher-worker-comms-to-.patch application/octet-stream 5.4 KB
v16-0006-Remove-dependency-on-utils-resowner.h-just-for-R.patch application/octet-stream 2.7 KB
notes.txt application/octet-stream 5.3 KB
v16-0005-Rename-postmaster-interrupt.c-to-ipc-signal_hand.patch application/octet-stream 20.7 KB
v16-0007-Hold-spinlock-while-clearing-MyProc-pid-on-backe.patch application/octet-stream 1.3 KB
v16-0001-Refactor-PARALLEL_MESSAGE-procsignal-signaling.patch application/octet-stream 17.7 KB
v16-0004-Move-CHECK_FOR_INTERRUPTS-and-friends-to-new-ipc.patch application/octet-stream 120.0 KB
v16-0008-Replace-Latches-with-Interrupts.patch application/octet-stream 533.1 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ethan Tran 2026-12-01 16:42:40 Re: [WiP] GiST intrapage indexing
Previous Message Narayanan Venkateswaran 2026-12-01 16:18:51 Re: Fix LogStandbySnapshot() race during logical decoding activation