[PATCH] Avoid pallocs in async.c's SignalBackends critical section

From: "Joel Jacobson" <joel(at)compiler(dot)org>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: [PATCH] Avoid pallocs in async.c's SignalBackends critical section
Date: 2025-11-23 14:45:35
Message-ID: 5798d6fa-7fdc-49d4-9b4e-57767cd40198@app.fastmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

This patch addresses this comment in async.c's SignalBackends:

* XXX in principle these pallocs could fail, which would be bad.
* Maybe preallocate the arrays? They're not that large, though.

This is unsafe, since AtCommit_Notify effectively runs in a critical
section, so an OOM there would PANIC ("AbortTransaction while in COMMIT
state"), as we can no longer abort safely.

This patch fixes this by adding two static arrays, notifySignalPids and
notifySignalProcs, allocated lazily in TopMemoryContext by
initSignalArrays. PreCommit_Notify now calls initSignalArrays while it's
still safe to ERROR, ensuring the arrays exist before entering the
commit path.

SignalBackends is updated to use these preallocated arrays instead of
allocating temporary ones.

This removes the risk of palloc in a critical section and eliminates
repeated palloc/pfree cycles.

/Joel

Attachment Content-Type Size
0001-async-avoid-pallocs-in-critical-section-v1.patch application/octet-stream 4.3 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2025-11-23 14:46:24 Re: Exit walsender before confirming remote flush in logical replication
Previous Message Tomas Vondra 2025-11-23 14:39:31 Re: should we have a fast-path planning for OLTP starjoins?