diff --git a/contrib/pg_prewarm/autoprewarm.c b/contrib/pg_prewarm/autoprewarm.c index c01b9c7e6a4..b5ac0e9ceb8 100644 --- a/contrib/pg_prewarm/autoprewarm.c +++ b/contrib/pg_prewarm/autoprewarm.c @@ -573,16 +573,23 @@ autoprewarm_database_main(Datum main_arg) * move on. */ while (i < apw_state->prewarm_stop_idx && - blk.tablespace == tablespace && - blk.filenumber == filenumber && have_free_buffer()) { - ForkNumber forknum = blk.forknum; + 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; + + forknum = blk.forknum; + /* * smgrexists is not safe for illegal forknum, hence check whether * the passed forknum is valid before using it in smgrexists. @@ -644,9 +651,12 @@ autoprewarm_database_main(Datum main_arg) read_stream_end(stream); - /* Advance i past all the blocks just prewarmed. */ + /* + * Advance i past all the blocks just prewarmed. Note that the + * callback might have advanced the index beyond the last valid + * block, so don't access block_info[i] yet. + */ i = p.pos; - blk = block_info[i]; } relation_close(rel, AccessShareLock);