Re: Interrupts vs signals

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Greg Burd <greg(at)burd(dot)me>, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, 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 00:03:19
Message-ID: 486a2aa1-e549-4d05-8624-710ce57fca5a@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 26/11/2026 18:24, Greg Burd wrote:
> Thanks for catching the single-user regression from 2e2751e6976. That
> sent me back into the series, and the review turned up a few real bugs
> in the big patch too.

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.

> Design changes in 0009:
>
> - Checkpointer and full_page_writes.
>
> v13 ran the checkpointer's config reload from CHECK_FOR_INTERRUPTS(),
> and added one at the top of CheckpointWriteDelay(), ahead of the
> shutdown checks. So a SIGHUP that turns full_page_writes off during
> the shutdown checkpoint wrote XLOG_FPW_CHANGE after the redo pointer,
> and the checkpoint failed with "PANIC: concurrent write-ahead log
> activity while database system is shutting down".
>
> The checkpointer now disables INTERRUPT_CONFIG_RELOAD once it sees
> INTERRUPT_SHUTDOWN_XLOG, which also covers the CHECK_FOR_INTERRUPTS()
> in errfinish(). The alternative, moving the CHECK_FOR_INTERRUPTS()
> back under the shutdown checks, would leave every other one reachable
> from CreateCheckPoint() open.
>
> Master has a milder form of the same bug after the checkpoint
> completes; Aleksandr Korchak posted a fix for it today [3]. With 0009
> applied, this change covers his case too.

Included that fix. The checkpointer disables INTERRUPT_CONFIG_RELOAD
once it has received INTERRUPT_SHUTDOWN_XLOG.

> - volatile holdoff counters.
>
> v13 made HOLD_INTERRUPTS() and START_CRIT_SECTION() also store to
> MyPendingInterruptsFlags, and dropped volatile from
> InterruptHoldoffCount and CritSectionCount. handle_sig_alarm() still
> does HOLD/RESUME_INTERRUPTS(), and gcc -O2 does reorder the stores: in
> _hash_doinsert() the pointer store comes before the "addl $1" on
> CritSectionCount. A SIGALRM between the two would restore the real
> flags pointer inside the critical section.
>
> The two counters and the pointer are volatile again. That adds 239
> bytes of text, and a CHECK_FOR_INTERRUPTS()-heavy seqscan and pgbench
> -S at 1 and 64 clients are within noise of master either way. Dropping
> the HOLD/RESUME pair from handle_sig_alarm() would also work, but then
> nothing would enforce what its comment promises.

Included that fix. I used a local variable in RESUME_INTERRUPTS() and
END_CRIT_SECTION() to avoid re-reading the volatile variables; these are
hot codepaths so every instruction counts.

> - Deferred recovery conflicts.
>
> When report_recovery_conflict() can't throw because query cancel is
> disabled, v13 re-raised INTERRUPT_RECOVERY_CONFLICT. The bit is still
> enabled, so every CHECK_FOR_INTERRUPTS() and every wait returned at
> once. The slotsync worker never enabled INTERRUPT_QUERY_CANCEL, so a
> logical-slot conflict made it spin forever: about 96% CPU in a probe
> that injects the conflict with gdb.
>
> Now the conflict is only recorded, and a new
> ResumeQueryCancelInterrupt() raises it again where SocketBackend(),
> CopyGetData() and the walsender's manifest reader re-enable cancel.
> The slotsync worker gets the query-cancel handler, which it had on
> master through StatementCancelHandler. With that, the worker in the
> probe throws "canceling statement due to conflict with recovery" and
> restarts, which is what master's code does.

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.

This way, you don't need to remember to use the special function to
re-enable the query cancel interrupt.

> - SendInterruptWithPid() held ProcArrayLock to keep the PID check and
> the send together, but neither ProcKill() nor InitProcess() holds it
> when changing pid.
>
> It now checks the PID and sets the bits under freeProcsLock. A new
> occupant must pop the slot from the freelist under that lock and then
> clears the slot's pending interrupts, so the bits either reach the
> intended process or are discarded. The wakeup happens after the lock
> is released.

I took that fix. To make that subtle change more clear, I added a
separate preparatory commit ("Hold spinlock while clearing MyProc->pid
on backend exit") to make ProcKill() clear the pid under the spinlock.

> - The postmaster passed notify_proc_number from shared memory straight
> to SendInterrupt() in four of the five places it reads it. All five
> go through one range-checking helper now.

I added a range check to SendInterrupt() instead.

> - RequestAddinInterrupt() handed out bits from a per-process counter, as
> Andres pointed out in February, so an extension loaded on demand in
> one backend could get the same bit as a different extension in
> another.
>
> It now errors unless called from shared_preload_libraries, as
> RegisterCustomRmgr() does. There's still no in-tree user; a test
> module that uses it would be a good follow-up.

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.

Yeah, this needs to be fixed, but it needs a bit more thinking.

> Smaller fixes, all in 0009:
> - die() in single-user mode respects INTERRUPTS_CAN_BE_PROCESSED(), as
> ProcessInterrupts() did on master.
> - The checkpointer's write-delay nap wakes for the shutdown interrupts.
> - Four leftover bgw_notify_pid assignments are gone.
> - A stale ProcessRecoveryConflictInterrupts() prototype is removed.
> - pg_interrupt_handler_t is added to typedefs.list.

Ok

> Open questions:
>
> 1. Heikki: do you want registrants of dynamic background workers to get
> INTERRUPT_WAIT_WAKEUP by default, as 0009 does now?

Yeah, that's intentional. You can use the BGWORKER_NO_NOTIFY flag if you
don't want it. But receiving one interrupt like that is very cheap, I'm
actually not sure if BGWORKER_NO_NOTIFY is worth having.

Another idea would be to add a separate interrupt bit for the bgworker
notifications. That would make it even cheaper to ignore if you don't
want them. (At the cost of eating up one interrupt bit).

> It also widens ForgetUnstartedBackgroundWorkers() to every unstarted
> dynamic worker, not only those whose registrant asked to wait. I
> think yes, and the docs already say so, but it changes behavior for
> extensions.

Yeah, I think that's fine. ForgetUnstartedBackgroundWorkers() runs at
PM_STOP_BACKENDS, after which no new workers are started anyway. Any
worker that's still unstarted at that point would never have run. The
only visible difference for an extension is that
GetBackgroundWorkerPid() reports BGWH_STOPPED rather than
BGWH_NOT_YET_STARTED, and that the registrant gets a wakeup.

> 2. Should bgw_notify_pid be removed rather than kept "not used"?

I kept it intentionally, so that extensions that currently set it will
continue to compile.

> An extension that reads MyBgworkerEntry->bgw_notify_pid to find its
> registrant now gets 0 without a compile error. Removing it would make
> that a build failure, which seems better than a silent hang.

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

> Benchmarks and the NOTIFY analysis are in the attached notes.txt. In
> short, NOTIFY senders look 8% slower when the listeners loop on
> pg_sleep(), but only because those listeners wake 42% less often and
> spend the CPU they save on their own loop. With idle or rate-capped
> listeners, sender throughput is the same as master.

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

- Heikki

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Laurenz Albe 2026-12-01 00:09:33 Re: passwordcheck: Make the user name check case-insensitive
Previous Message Aleksandra Pervushina 2026-11-30 23:44:21 pg_dump restore fails for partitions of extension-owned tables