[PATCH] Fix vacuum_delay_point happening inside lock

Started by Kevin Rocker4 months ago20 messageshackers
Jump to latest
#1Kevin Rocker
me@kevinrocker.com

ginInsertCleanup() calls vacuum_delay_point while still holding a lock on the current pending-list page, so the delay runs with interrupts held off.

This is the same problem 2d7f6947293 fixed in btbulkdelete() back in 2006, and it's related to 8a045f760f6 which went in last week.

The patch keeps the other vacuum_delay_point() calls that exist after the buffer is released, in both the flush branch and the main branch at the end of the loop.

I haven't seen this hang directly and this is my first contribution, so I'd appreciate a second opinion on anything. The issue exists in multiple supported branches, but should be easy to backpatch (happy to help with that).

The issue was surfaced by Opus, I've verified the logic against those two commits myself.

- Kevin Rocker

Attachments:

t253278_1
v1-0001-Don-t-call-vacuum_delay_point-while-holding-a-buf.patchtext/x-patch; name="=?UTF-8?Q?v1-0001-Don-t-call-vacuum=5Fdelay=5Fpoint-while-holding-a-buf.?= =?UTF-8?Q?patch?="Download+3-3
#2Neil Chen
carpenter.nail.cz@gmail.com
In reply to: Kevin Rocker (#1)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi Kevin,

On Sun, Aug 2, 2026 at 12:58 AM Kevin Rocker <me@kevinrocker.com> wrote:

ginInsertCleanup() calls vacuum_delay_point while still holding a lock on
the current pending-list page, so the delay runs with interrupts held off.

This is the same problem 2d7f6947293 fixed in btbulkdelete() back in 2006,
and it's related to 8a045f760f6 which went in last week.

The patch keeps the other vacuum_delay_point() calls that exist after the
buffer is released, in both the flush branch and the main branch at the end
of the loop.

I haven't seen this hang directly and this is my first contribution, so
I'd appreciate a second opinion on anything. The issue exists in multiple
supported branches, but should be easy to backpatch (happy to help with
that).

The issue was surfaced by Opus, I've verified the logic against those two
commits myself.

- Kevin Rocker

Thanks for the patch. The analysis looks correct to me. But one thought:
Would it be better to move the call just after

LockBuffer(buffer, GIN_UNLOCK);

rather than remove it? The following comment explicitly notes that moving
the collected data can take significant time. Placing the call after the
unlock would allow a pending cancellation to be handled before that work,
while preserving the existing cost-delay point.

Perhaps I’m missing something, but it may be worth considering.

Best regards,
--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/

#3Kevin Rocker
me@kevinrocker.com
In reply to: Neil Chen (#2)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi Neil,

The `LockBuffer(buffer, GIN_UNLOCK)` you mentioned does call the vacuum delay as part of inserting each entry to disk, so the gain would be one additional delay_point before the scan and the first insertion. That unlock is also only in the flush-to-disk path, so it's not a direct replacement for the removed one.

The unconditional part of the loop is 'processPendingPage' then release the buffer and call vacuum_delay right after the if/else. Given all that, it's probably fine as is? Let me know what you think. I've attached a patch with your suggestion as well.

- Kevin Rocker

Attachments:

t253278_3
v1-0002-Additional-vacuum_delay_point-before-flushing-ent.patchtext/x-patch; name="=?UTF-8?Q?v1-0002-Additional-vacuum=5Fdelay=5Fpoint-before-flushing-ent.?= =?UTF-8?Q?patch?="Download+2-1
#4Neil Chen
carpenter.nail.cz@gmail.com
In reply to: Kevin Rocker (#3)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi Kevin,

On Mon, Aug 3, 2026 at 11:21 PM Kevin Rocker <me@kevinrocker.com> wrote:

Hi Neil,

The `LockBuffer(buffer, GIN_UNLOCK)` you mentioned does call the vacuum
delay as part of inserting each entry to disk, so the gain would be one
additional delay_point before the scan and the first insertion. That unlock
is also only in the flush-to-disk path, so it's not a direct replacement
for the removed one.

The unconditional part of the loop is 'processPendingPage' then release
the buffer and call vacuum_delay right after the if/else. Given all that,
it's probably fine as is? Let me know what you think. I've attached a patch
with your suggestion as well.

- Kevin Rocker

Thanks, that makes sense. I agree that the original patch is sufficient.

The additional call could improve cancellation responsiveness in the narrow
case where an interrupt becomes pending before the flush, but that window is
probably small, and the existing per-entry delay points already cover the
long-running part. So I have no objection to keeping the patch as is.

Best regards,
--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/

#5Andrey Borodin
amborodin@acm.org
In reply to: Kevin Rocker (#3)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi Kevin and Neil!

On 3 Aug 2026, at 20:20, Kevin Rocker <me@kevinrocker.com> wrote:

Looks like an interesting and useful improvement.

I've instrumenetd vacuum_delay_point() and CHECK_FOR_INTERRUPTS() with __FILE__ and
__LINE__ and found several other cases when we always check for interrupts where
we cannot actually process them. Let's make a combined patch?

There are couple of cases where I do not know what to do.

Best regards, Andrey Borodin.

Attachments:

t253278_5
v3-0001-Move-interrupt-checks-out-of-locked-regions.patchapplication/octet-stream; name=v3-0001-Move-interrupt-checks-out-of-locked-regions.patch; x-unix-mode=0644Download+15-4
#6Neil Chen
carpenter.nail.cz@gmail.com
In reply to: Andrey Borodin (#5)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi Andrey,

On Tue, Aug 4, 2026 at 1:45 PM Andrey Borodin <x4mmm@yandex-team.ru> wrote:

Looks like an interesting and useful improvement.

I've instrumenetd vacuum_delay_point() and CHECK_FOR_INTERRUPTS() with
__FILE__ and
__LINE__ and found several other cases when we always check for interrupts
where
we cannot actually process them. Let's make a combined patch?

There are couple of cases where I do not know what to do.

Thanks for the broader audit and the patch.

The GIN and ANALYZE changes look reasonable to me. In particular, the
ANALYZE change restores the ordering that existed before 041b96802ef moved
next_block() into the while condition.

I think the hash case deserves separate discussion. With the current lock
chaining, there is no lock-free per-page boundary. Moving the delay point to
the bucket boundary avoids sleeping while holding a buffer content lock and
should make little difference for short overflow chains. For a long chain,
however, the accumulated delay can be capped at four times
vacuum_cost_delay and then reset, so this may reduce overall throttling
rather than merely making it coarser.

For discussion, I have attached a second, experimental patch on top of v3.
It moves the hash delay point to the bucket boundary and adds these
assertions to vacuum_delay_point():

Assert(InterruptHoldoffCount == 0);
Assert(CritSectionCount == 0);

With assistance from OpenAI Codex (Sol), I tried this locally; it builds and
passes the core regression tests. I have not benchmarked the performance
tradeoff described above.

I am not deeply familiar with the hash AM code, so review from someone more
familiar with this area would be very welcome.

Best regards,

--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t253278_6
v3-0002-Experiment-with-hash-vacuum-delay-point.patchapplication/octet-stream; name=v3-0002-Experiment-with-hash-vacuum-delay-point.patchDownload+6-2
#7Andrey Borodin
amborodin@acm.org
In reply to: Neil Chen (#6)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

On 4 Aug 2026, at 13:13, Neil Chen <carpenter.nail.cz@gmail.com> wrote:

Assert(InterruptHoldoffCount == 0);
Assert(CritSectionCount == 0);

With assistance from OpenAI Codex (Sol), I tried this locally; it builds and
passes the core regression tests. I have not benchmarked the performance
tradeoff described above.

I think that asserts are a bit too much. I observed several other
non-interruptible cases of check for interrupts. But all other cases
that I found were called sometimes without LWlocks too.

Also, make check-world is completely different beast than make check.
I will take a lot more time, but also cover a lot more cases. I used
regression tests, isolation and some subset of other tests. With Sol,
perhaps, you can scan through logs of whole make check-world.

Best regards, Andrey Borodin.

#8Kevin Rocker
me@kevinrocker.com
In reply to: Andrey Borodin (#7)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Andrey's extension makes sense to me, happy to make a combined patch.

If there are other places where this happens, we can either go one patch at a time or try to do it all together. I'm leaning towards one patch at a time. My only remaining question is if we need to backpatch this.

/messages/by-id/178447127453.110.12276981925360691905@mail.gmail.com also adjusted vacuum_delay_point calls and was backpatched back to 14. That might not be necessary in our case, but I want to ask.

- Kevin Rocker

#9Neil Chen
carpenter.nail.cz@gmail.com
In reply to: Kevin Rocker (#8)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi Kevin and Andery,

My second patch was intended as an experiment, with the assertions also
serving as instrumentation while testing the hash relocation. Following
your suggestion, I ran the Meson equivalent of check-world and checked the
logs with Sol; there were no assertion failures. Still, I agree that the
assertions are too strong to keep permanently.

I am happy to leave hash aside for now, and the current patch looks good to
me. I also agree that a backpatch does not seem necessary here.

Best regards,

--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/

#10Kevin Rocker
me@kevinrocker.com
In reply to: Neil Chen (#9)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Just bumping the patch to v4 for CFBot and pointed 'Discussion' to the thread root. Also confirmed check-world passes.

Attachments:

t253278_10
v4-0001-Move-interrupt-checks-out-of-locked-regions.patchtext/x-patch; name="=?UTF-8?Q?v4-0001-Move-interrupt-checks-out-of-locked-regions.patch?="Download+15-4
#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kevin Rocker (#10)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

"Kevin Rocker" <me@kevinrocker.com> writes:

Just bumping the patch to v4 for CFBot and pointed 'Discussion' to the thread root. Also confirmed check-world passes.

I wonder if we could enforce (via an Assert) that vacuum_delay_point
is only called from places where an interrupt can be accepted?
We found that that wasn't practical for CHECK_FOR_INTERRUPTS itself,
but maybe we could be stricter about vacuum_delay_point placement?

regards, tom lane

#12Andrey Borodin
amborodin@acm.org
In reply to: Tom Lane (#11)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

On 10 Aug 2026, at 04:53, Tom Lane <tgl@sss.pgh.pa.us> wrote:

I wonder if we could enforce (via an Assert) that vacuum_delay_point
is only called from places where an interrupt can be accepted?

I'd say even sleeping with LWLock is not a very good idea.

Best regards, Andrey Borodin.

#13Kevin Rocker
me@kevinrocker.com
In reply to: Andrey Borodin (#12)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Attached is v5, which takes Tom's suggestion for a spin.

0002 adds asserts at the top of vacuum_delay_point(). I picked separate asserts instead of INTERRUPTS_CAN_BE_PROCESSED(), just like Neil.

Running check-world with that assert tripped on hashbucketcleanup().

Since hashbucketcleanup is called assuming a lock is in place, no place in it is safe. Instead, I moved the delay to the per-bucket loop. It's a switch from a per-page delay to a per-bucket delay, but the per-page delay we're removing wasn't valid anyway.

Andrey, let me know if you have concerns about lock chaining/cleanup with this approach. There shouldn't be any issues since we're outside the locked function entirely.

With both patches applied, check-world passes with assertions enabled, and each patch passes on its own.

- Kevin Rocker

Attachments:

t253278_13
v5-0001-Move-interrupt-checks-out-of-locked-regions.patchtext/x-patch; name="=?UTF-8?Q?v5-0001-Move-interrupt-checks-out-of-locked-regions.patch?="Download+14-6
v5-0002-Assert-that-vacuum_delay_point-is-called-only-whe.patchtext/x-patch; name="=?UTF-8?Q?v5-0002-Assert-that-vacuum=5Fdelay=5Fpoint-is-called-only-whe.?= =?UTF-8?Q?patch?="Download+7-1
#14Andrey Borodin
amborodin@acm.org
In reply to: Kevin Rocker (#13)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi Kevin,

Andrey, let me know if you have concerns about lock chaining/cleanup
with this approach.

I don't see a problem with the move. It is before the cleanup lock on
the primary bucket is acquired, while the lock chaining within
hashbucketcleanup() is unchanged. A bucket with a long overflow chain
can accumulate more vacuum cost before delaying, but there is no safe
boundary inside that chain. The per-bucket placement seems like the
best available option.

There is now a production report of exactly this hash problem [0]/messages/by-id/19628-c2b17d358181a1ea@postgresql.org. A
shutdown waited about six minutes for an autovacuum in
hashbucketcleanup(). Mostafa independently posted the same per-bucket
move and measured cancellation improving from about 2.08 seconds to
0.014 seconds. That confirms the placement and makes the hash fix a
backpatch candidate.

One detail in 0002: the two assertions do not check
QueryCancelHoldoffCount. Since vacuum_delay_point() is also expected to
service query cancellation, I think this should simply be:

Assert(INTERRUPTS_CAN_BE_PROCESSED());

This also expresses Tom's proposed invariant directly. Did you run
the full CI after this change? check-world on one system might be not
enough.

Best regards, Andrey Borodin.

[0]: /messages/by-id/19628-c2b17d358181a1ea@postgresql.org

#15Kevin Rocker
me@kevinrocker.com
In reply to: Andrey Borodin (#14)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Glad to hear the per-bucket logic makes sense. I reached the same conclusion: if nothing in per-page was safe, per-bucket is as close as we can get.

v6 updates the assert and rebases on 8aa49d34e977af, and full CI is clean on my fork [0]https://github.com/kjrocker/postgres/tree/delay-point-v6.

I'll flag this in CF to be backpatched to 14, let me know if there's anything else I need to do for that or if we want to try to carve out the hash fix.

- Kevin Rocker

[0]: https://github.com/kjrocker/postgres/tree/delay-point-v6

Attachments:

t253278_15
v6-0001-Move-interrupt-checks-out-of-locked-regions.patchtext/x-patch; name="=?UTF-8?Q?v6-0001-Move-interrupt-checks-out-of-locked-regions.patch?="Download+14-6
v6-0002-Assert-that-vacuum_delay_point-is-called-only-whe.patchtext/x-patch; name="=?UTF-8?Q?v6-0002-Assert-that-vacuum=5Fdelay=5Fpoint-is-called-only-whe.?= =?UTF-8?Q?patch?="Download+6-1
#16Andrey Borodin
amborodin@acm.org
In reply to: Kevin Rocker (#15)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi Kevin,

v6 addresses my concerns. Thank you for running the full CI.

I would keep the fixes in 0001 together. They all restore the same
invariant, and the production report for hash strengthens the backpatch
case without making that particular move a separate kind of change. A
committer can choose a narrower set for a particular branch if
needed (especially wrt just comments change).

I would consider 0002 separately, though. Adding the Assert on HEAD is
useful, but backpatching the fixes does not necessarily mean that we
should risk extra buildfarm noise in all stable branches.

Two tiny commit message typos in 0001: "it's duration" should be "its
duration", and "vaccuum" should be "vacuum".

With these minor points, the patch set looks Ready for Committer to me.

Thank you!

Best regards, Andrey Borodin.

#17Kevin Rocker
me@kevinrocker.com
In reply to: Andrey Borodin (#16)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

That sounds like a good plan to me, backpatch 0001 with the actual fixes and keep 0002 with the assert on master .

v7 is identical to v6 except with typos fixed.

- Kevin Rocker

Attachments:

t253278_17
v7-0001-Move-interrupt-checks-out-of-locked-regions.patchtext/x-patch; name="=?UTF-8?Q?v7-0001-Move-interrupt-checks-out-of-locked-regions.patch?="Download+14-6
v7-0002-Assert-that-vacuum_delay_point-is-called-only-whe.patchtext/x-patch; name="=?UTF-8?Q?v7-0002-Assert-that-vacuum=5Fdelay=5Fpoint-is-called-only-whe.?= =?UTF-8?Q?patch?="Download+6-1
#18Kevin Rocker
me@kevinrocker.com
In reply to: Kevin Rocker (#17)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Greg, I think this was meant for the list since it opens by greeting
everyone, so I've added pgsql-hackers back and quoted your message in
full.

Hi Kevin, Andrey, Neil, Tom,

I tested this on my aarch64 Windows/MSVC buildfarm animal (unicorn,
--enable-cassert, injection_points) which has been crashing repeatedly
in tests and it fixed the issue. I'd just yesterday started a thread
[1] on this, but I'll shut that one down and point here instead as I
think you've got it nailed.

I think this raises the priority of the ANALYZE hunk in v7-0001, and
argues for landing it ASAP.

In my logs on that animal I find:

TRAP: failed Assert("entry->data.lockmode == BUFFER_LOCK_UNLOCK"),
bufmgr.c, in BufferLockAcquire (Windows exception 0xC0000409)

And when I reviewed the crash dumps all 10 had the identical stack.
Reading it outermost to innermost (nearest-symbol mislabels from
optimized codegen noted):

AutoVacWorkerMain
-> vacuum -> analyze_rel -> acquire_sample_rows
-> [heapam_scan_analyze_next_block holds BUFFER_LOCK_SHARE on a
pg_class page and returns with it held]
-> vacuum_delay_point(true) <-- analyze.c, delay point
-> ProcessConfigFile(PGC_SIGHUP) (a config reload landed)
-> ... -> check_default_text_search_config (GUC check hook)
-> get_ts_config_oid -> LookupExplicitNamespace
-> SearchSysCache -> table_open -> relation_open
-> LockRelationOid -> AcceptInvalidationMessages
-> RelationCacheInvalidate (rebuild a nailed catalog entry)
-> systable_getnext -> heapgettup_pagemode
-> heap_prepare_pagescan -> LockBuffer(BUFFER_LOCK_SHARE)
-> Assert (second SHARE lock on the pg_class buffer that
acquire_sample_rows already holds SHARE on)

So this looks to be the same "vacuum_delay_point() reached with a buffer
content lock held" bug. The ANALYZE call site that v7-0001 moves before
scan_analyze_next_block(). The extra wrinkle on the ANALYZE path is that
the delay point can run a SIGHUP config reload, whose GUC check hook
does a catalog lookup that triggers a relcache rebuild, and that
rebuild's pagemode pg_class scan re-locks the very buffer the sampling
scan is still holding SHARE on.

The reason this is more than a cancellation-latency issue on v19 is the
buffer content-lock rewrite in fcb9c977aa5 tracks only one lock per
buffer per backend (the single data.lockmode field). A second SHARE
acquire on an already-share-locked buffer is now:

- a hard Assert/crash in cassert builds (what unicorn shows), and
- in a non-assert build, an asymmetric leak: BufferLockAttempt() adds
a second BM_LOCK_VAL_SHARED to the shared state, data.lockmode
records only one, and release subtracts one -- so that pg_class
buffer is left permanently one shared-locker too high and can never
again be locked exclusive. Any later exclusive waiter (VACUUM) on
that buffer blocks for the life of the cluster.

Pre-v19 the double SHARE was harmless (the held-lwlocks array could
represent it), which is presumably why the call site survived so long.

The steps to reproduce this are ordinary, an autovacuum ANALYZE of a
catalog (pg_class here, reached via the text-search-config GUC hook's
syscache lookup) that overlaps a config reload. My animal happens to
build with injection_points, but nothing in this stack is an injection
point, it is the stock ANALYZE -> vacuum_delay_point ->
ProcessConfigFile -> relcache-rebuild path, so I don't believe
injection_points is required to hit it.

I have not seen the crash on non-cassert animals because there it silently
leaks the lock rather than asserting, which is arguably worse.

Given that v7-0001 already contains the fix (thank you), my only ask is
that the ANALYZE hunk be treated as a v19 crash-regression fix rather than
a latency improvement, and backpatched to REL_19 before GA. Happy to test
again on the aarch64/MSVC animal.

best.

-greg

[1] /messages/by-id/arKVu9wp5A7EdKkx@floki

I've attached v8 of the patch. The code is the same, but I extracted the
ANALYZE fix to 0001 so it can be backpatched separately.

This is a v19 regression from fcb9c977aa5, so I think it needs an open
item. I don't have wiki edit access yet, so could someone from the RMT
(Cc'd) add it, with Andres as owner?

- Kevin Rocker

Attachments:

t253278_18
v8-0001-Don-t-call-vacuum_delay_point-with-a-buffer-lock-.patchtext/x-patch; name="=?UTF-8?Q?v8-0001-Don-t-call-vacuum=5Fdelay=5Fpoint-with-a-buffer-lock-.?= =?UTF-8?Q?patch?="Download+4-2
v8-0002-Move-remaining-interrupt-checks-out-of-locked-reg.patchtext/x-patch; name="=?UTF-8?Q?v8-0002-Move-remaining-interrupt-checks-out-of-locked-reg.patc?= =?UTF-8?Q?h?="Download+10-5
v8-0003-Assert-that-vacuum_delay_point-is-called-only-whe.patchtext/x-patch; name="=?UTF-8?Q?v8-0003-Assert-that-vacuum=5Fdelay=5Fpoint-is-called-only-whe.?= =?UTF-8?Q?patch?="Download+6-1
#19Andres Freund
andres@anarazel.de
In reply to: Kevin Rocker (#18)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

Hi,

On 2026-09-28 17:07:52 +0200, Kevin Rocker wrote:

The reason this is more than a cancellation-latency issue on v19 is the
buffer content-lock rewrite in fcb9c977aa5 tracks only one lock per
buffer per backend (the single data.lockmode field). A second SHARE
acquire on an already-share-locked buffer is now:

- a hard Assert/crash in cassert builds (what unicorn shows), and
- in a non-assert build, an asymmetric leak: BufferLockAttempt() adds
a second BM_LOCK_VAL_SHARED to the shared state, data.lockmode
records only one, and release subtracts one -- so that pg_class
buffer is left permanently one shared-locker too high and can never
again be locked exclusive. Any later exclusive waiter (VACUUM) on
that buffer blocks for the life of the cluster.

Pre-v19 the double SHARE was harmless (the held-lwlocks array could
represent it), which is presumably why the call site survived so long.

I think this was completely broken before 19 too. Acquiring a lock while
holding the same lock just happened to be undiagnosed. Note that if you ever
did this with an exclusive lock being involved, you'd just have ended up with
an uninterruptible endless wait.

It surely was never safe to call ProcessConfigFile(), or sane to sleep, while
holding an lwlock. And calling vacuum_delay_point() with interrupts held, made
it not actually properly work, due to not doing the CFI().

This is a v19 regression from fcb9c977aa5, so I think it needs an open
item. I don't have wiki edit access yet, so could someone from the RMT
(Cc'd) add it, with Andres as owner?

I do not believe that fcb9c977aa5 is the culprit here.

Greetings,

Andres Freund

#20Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Andres Freund (#19)
Re: [PATCH] Fix vacuum_delay_point happening inside lock

On 28/09/2026 18:24, Andres Freund wrote:

Hi,

On 2026-09-28 17:07:52 +0200, Kevin Rocker wrote:

The reason this is more than a cancellation-latency issue on v19 is the
buffer content-lock rewrite in fcb9c977aa5 tracks only one lock per
buffer per backend (the single data.lockmode field). A second SHARE
acquire on an already-share-locked buffer is now:

- a hard Assert/crash in cassert builds (what unicorn shows), and
- in a non-assert build, an asymmetric leak: BufferLockAttempt() adds
a second BM_LOCK_VAL_SHARED to the shared state, data.lockmode
records only one, and release subtracts one -- so that pg_class
buffer is left permanently one shared-locker too high and can never
again be locked exclusive. Any later exclusive waiter (VACUUM) on
that buffer blocks for the life of the cluster.

Pre-v19 the double SHARE was harmless (the held-lwlocks array could
represent it), which is presumably why the call site survived so long.

I think this was completely broken before 19 too. Acquiring a lock while
holding the same lock just happened to be undiagnosed. Note that if you ever
did this with an exclusive lock being involved, you'd just have ended up with
an uninterruptible endless wait.

It surely was never safe to call ProcessConfigFile(), or sane to sleep, while
holding an lwlock. And calling vacuum_delay_point() with interrupts held, made
it not actually properly work, due to not doing the CFI().

+1

I just arrived at this thread from this other thread discussing the same
issue:
/messages/by-id/19628-c2b17d358181a1ea@postgresql.org.

I committed and backported the fix to all stable branches. I included a
quick exit in vacuum_delay_point(), if !INTERRUPTS_CAN_BE_PROCESSED().

- Heikki