Re: Autoprewarm workers terminated due to a segmentation fault
Re: Autoprewarm workers terminated due to a segmentation fault
От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 6/9/26 23:06, Matheus Alcantara wrote:
> Hi,
>
> On Tue Jun 9, 2026 at 3:37 PM -03, Glauber Batista wrote:
>> I have an issue with the autoprewarm workers segfaulting during the service
>> restart. Sometimes, it successfully restarts after a few tries, but usually
>> I need to remove the autoprewarm.blocks file. My setup consists of a
>> primary server with two replicas and all of them present the same issue. I
>> have been using this setup for several years with no issues, but since I
>> upgraded to Postgres 18 I'm having it. This is a production database.
>>
>> Details:
>>
>> [ ... ]
>>
>> All that said, it seems there's a missing guard-clause at line 649. I
>> didn't spend much time reading the code, but it's clearly accessing a
>> position in the array that is not allocated.
>>
>
> Thank you for the report!
>
> I've managed to reproduce the issue with the following:
>
> create table test_warm (id int, data text);
> -- insert enough rows to generate too many pages on an index
> insert into test_warm select g, repeat('a', 100) from generate_series(1, 5000000) g;
> create index warm_idx on test_warm(id);
>
> -- force read the index entirely into shared_buffers
> select count(*) from test_warm where id > 0;
>
> Then pg_ctl stop and pg_ctl start will start failing with the following logs:
>
> 2026-06-09 17:47:40.924 -03 [23025] LOG: shutting down
> 2026-06-09 17:47:40.925 -03 [23025] LOG: checkpoint starting: shutdown fast
> 2026-06-09 17:47:41.033 -03 [23022] LOG: database system is shut down
> 2026-06-09 17:47:49.830 -03 [23172] LOG: starting PostgreSQL 19beta1 on aarch64-darwin, compiled by clang-17.0.0, 64-bit
> 2026-06-09 17:47:49.842 -03 [23172] LOG: database system is ready to accept connections
> 2026-06-09 17:47:49.917 -03 [23172] LOG: background worker "autoprewarm worker" (PID 23182) was terminated by signal 11: Segmentation fault: 11
> 2026-06-09 17:47:49.917 -03 [23172] LOG: terminating any other active server processes
> 2026-06-09 17:47:49.918 -03 [23172] LOG: all server processes terminated; reinitializing
>
> Wondering if the following would be enough?
>
> /* Advance i past all the blocks just prewarmed. */
> i = p.pos;
> + if (i >= apw_state->prewarm_stop_idx)
> + break;
> +
> blk = block_info[i];
>
So how does it get advanced past the prewarm_stop_idx? I've been unable
to reproduce it locally, maybe it's platform-specific. The original
report was from ARM, are you on ARM too, Matheus?
But AFAIK the code may not account for read stream callback updating the
pos to prewarm_stop_idx? The callback may end with (p->pos =
apw_state->prewarm_stop_idx), and that seems to be past the end of the
array.
That'd mean the proposed check is generally the correct way to fix this.
TBH it's not clear to me why this needs to set the *next* entry at the
end of the loop. Well, it does that so that the loop condition can use
'blk', but that seems a bit fragile / confusing, and no one noticed the
issue.
Maybe this would be a better way to write the while loop?
while (i < apw_state->prewarm_stop_idx)
{
blk = block_info[i];
if (blk.tablespace != tablespace ||
blk.filenumber != filenumber)
break;
...
}
regards
--
Tomas Vondra
Re: Autoprewarm workers terminated due to a segmentation fault
On Tue Jun 9, 2026 at 6:44 PM -03, Tomas Vondra wrote:
> So how does it get advanced past the prewarm_stop_idx? I've been unable
> to reproduce it locally, maybe it's platform-specific. The original
> report was from ARM, are you on ARM too, Matheus?
>
Yes, I'm also on ARM. I also set pg_prewarm.autoprewarm_interval=10s on
postgresql.conf, not sure if it make more easier to reproduce.
> But AFAIK the code may not account for read stream callback updating the
> pos to prewarm_stop_idx? The callback may end with (p->pos =
> apw_state->prewarm_stop_idx), and that seems to be past the end of the
> array.
>
Yes, this is my understanding.
> That'd mean the proposed check is generally the correct way to fix this.
> TBH it's not clear to me why this needs to set the *next* entry at the
> end of the loop. Well, it does that so that the loop condition can use
> 'blk', but that seems a bit fragile / confusing, and no one noticed the
> issue.
>
> Maybe this would be a better way to write the while loop?
>
> while (i < apw_state->prewarm_stop_idx)
> {
> blk = block_info[i];
>
> if (blk.tablespace != tablespace ||
> blk.filenumber != filenumber)
> break;
>
> ...
> }
>
>
Is attached patch what are you sugesting? If yes, I agree that looks
better, it's more safe and easier to understand.
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
Re: Autoprewarm workers terminated due to a segmentation fault
От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 6/10/26 00:25, Matheus Alcantara wrote:
> On Tue Jun 9, 2026 at 6:44 PM -03, Tomas Vondra wrote:
>> So how does it get advanced past the prewarm_stop_idx? I've been unable
>> to reproduce it locally, maybe it's platform-specific. The original
>> report was from ARM, are you on ARM too, Matheus?
>>
>
> Yes, I'm also on ARM. I also set pg_prewarm.autoprewarm_interval=10s on
> postgresql.conf, not sure if it make more easier to reproduce.
>
>> But AFAIK the code may not account for read stream callback updating the
>> pos to prewarm_stop_idx? The callback may end with (p->pos =
>> apw_state->prewarm_stop_idx), and that seems to be past the end of the
>> array.
>>
>
> Yes, this is my understanding.
>
>> That'd mean the proposed check is generally the correct way to fix this.
>> TBH it's not clear to me why this needs to set the *next* entry at the
>> end of the loop. Well, it does that so that the loop condition can use
>> 'blk', but that seems a bit fragile / confusing, and no one noticed the
>> issue.
>>
>> Maybe this would be a better way to write the while loop?
>>
>> while (i < apw_state->prewarm_stop_idx)
>> {
>> blk = block_info[i];
>>
>> if (blk.tablespace != tablespace ||
>> blk.filenumber != filenumber)
>> break;
>>
>> ...
>> }
>>
>>
>
> Is attached patch what are you sugesting? If yes, I agree that looks
> better, it's more safe and easier to understand.
>
Yeah, that's roughly what I had in mind.
After looking a bit closer, I think this bug was introduced by
commit 6acab8bdbcda735ef47b1bb0ba2284d6c465cd88
Author: Melanie Plageman
Date: Fri Apr 4 15:25:27 2025 -0400
Refactor autoprewarm_database_main() in preparation for read stream
which happens to advance to the next block in a couple places
blk = block_info[++i];
before we know it the incremented "i" is a valid element. The following
commit (d9c7911e1a5f adding the read stream) ends up doing the same
thing, except the index is incremented in a callback.
Melanie, do you agree with the proposed fix?
regards
--
Tomas Vondra
Re: Autoprewarm workers terminated due to a segmentation fault
От:
Melanie Plageman <melanieplageman@gmail.com>
Дата:
On Thu, Jun 11, 2026 at 10:40 AM Tomas Vondra wrote:
>
> After looking a bit closer, I think this bug was introduced by
>
> commit 6acab8bdbcda735ef47b1bb0ba2284d6c465cd88
> Author: Melanie Plageman
> Date: Fri Apr 4 15:25:27 2025 -0400
>
> Refactor autoprewarm_database_main() in preparation for read stream
>
> which happens to advance to the next block in a couple places
>
> blk = block_info[++i];
>
> before we know it the incremented "i" is a valid element. The following
> commit (d9c7911e1a5f adding the read stream) ends up doing the same
> thing, except the index is incremented in a callback.
>
> Melanie, do you agree with the proposed fix?
Thanks all for investigating this! I agree that the proposed fix is
correct (both v1 and v2). I am a bit torn between the two versions.
v1:
i = p.pos;
if (i >= apw_state->prewarm_stop_idx)
break;
blk = block_info[i];
preserves what I had intended in that it sets blk as close to where i
was advanced as possible. I had wanted this behavior because we have
the "fast-forwarding" that happens in a few places in that loop, and I
was afraid it would get quite confusing. The other thing I wanted was
the invariant that blk is always the i'th block when exiting the loop.
This was in case future code restructured the outer loop and added
other break conditions and then used blk.
However, I do hate duplicating the loop bounds checking in two places.
And future code that restructures the control flow of this function is
going to have to be extremely careful anyway. The combination of
multiple nested loops, "fast-forwarding" behavior, and skipping
forward X number of blocks with the read stream made the requirements
of this function's control flow quite complex.
v2:
while (i < apw_state->prewarm_stop_idx)
{
ForkNumber forknum;
BlockNumber nblocks;
struct AutoPrewarmReadStreamData p;
ReadStream *stream;
Buffer buf;
blk = block_info[i];
/* Stop when we reach a different relation. */
if (blk.tablespace != tablespace ||
blk.filenumber != filenumber)
break;
has the advantage that we set blk directly before using it instead of
at the end of the previous iteration, which people may find less
confusing.
So, overall, I am fine with either approach. Whatever other hackers
find more clear is the right approach. In my attempt to make it clear,
I obviously made it incorrect -- which is far worse :)
- Melanie
Re: Autoprewarm workers terminated due to a segmentation fault
От:
Matheus Alcantara <matheusssilv97@gmail.com>
Дата:
On 26/06/26 14:55, Tomas Vondra wrote: > FWIW I've committed the v2 fix (I liked it more), and I've backpatched > it to 18. > Thank you! -- Matheus Alcantara EDB: https://www.enterprisedb.com
Re: Autoprewarm workers terminated due to a segmentation fault
От:
Glauber Batista <glauberrbatista@gmail.com>
Дата:
Hello,
I finally got the chance and time to test the patch. I'm using it on a replica for now and it's working as expected.
2026-07-01 19:09:35 UTC::@:[169848]:LOG: database system is ready to accept read-only connections
2026-07-01 19:09:35 UTC::@:[174803]:LOG: started streaming WAL from primary at B7E/24000000 on timeline 1
2026-07-01 19:09:35 UTC::@:[174804]:LOG: autoprewarm successfully prewarmed 0 of 131072 previously-loaded blocks
2026-07-01 19:09:35 UTC::@:[174803]:LOG: started streaming WAL from primary at B7E/24000000 on timeline 1
2026-07-01 19:09:35 UTC::@:[174804]:LOG: autoprewarm successfully prewarmed 0 of 131072 previously-loaded blocks
Thank you all for your hard work!
Best,
Glauber Cassiano Batista
Em seg., 29 de jun. de 2026 às 12:13, Melanie Plageman <melanieplageman@gmail.com> escreveu:
On Fri, Jun 26, 2026 at 1:55 PM Tomas Vondra <tomas@vondra.me> wrote:
>
> FWIW I've committed the v2 fix (I liked it more), and I've backpatched
> it to 18.
Thank you so much for doing this. I had it on my list for next week,
but a few other things got ahead of it. I really, really appreciate
you taking on committing it!
- Melanie
pgsql: Use EVP_MAC for HMAC (OpenSSL >= 3.0)
Use EVP_MAC for HMAC (OpenSSL >= 3.0) The legacy HMAC_CTX interface (HMAC_CTX_new(), HMAC_Init_ex(), etc.) has been deprecated since OpenSSL 3.0. The legacy routines do not dispatch through the provider framework, so a loaded provider's HMAC implementation could be bypassed. This commit updates hmac_openssl.c to use EVP_MAC when building with OpenSSL 3.0 or newer versions. OpenSSL 1.1.1 is the oldest version supported on HEAD, so the legacy routines need to be kept. Note that LibreSSL uses the legacy routines. Like b91f79cd08ab, no backpatch is done. Author: Mark Atwood Co-authored-by: Michael Paquier Discussion: https://postgr.es/m/178596055149.1584250.13974609482797470185@reviewcommit.com Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/1f3b9bb109b8b1a7bb60d2d7343c1759679e501b Modified Files -------------- src/common/hmac_openssl.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-)