Skip to content

Commit b485ad7

Browse files
committed
Provide multi-block smgrprefetch().
Previously smgrprefetch() could issue POSIX_FADV_WILLNEED advice for a single block at a time. Add an nblocks argument so that we can do the same for a range of blocks. This usually produces a single system call, but might need to loop if it crosses a segment boundary. Initially it is only called with nblocks == 1, but proposed patches will make wider calls. Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> (earlier version) Discussion: https://postgr.es/m/CA+hUKGJkOiOCa+mag4BF+zHo7qo=o9CFheB8=g6uT5TUm2gkvA@mail.gmail.com
1 parent 59bd34c commit b485ad7

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ PrefetchSharedBuffer(SMgrRelation smgr_reln,
569569
* recovery if the relation file doesn't exist.
570570
*/
571571
if ((io_direct_flags & IO_DIRECT_DATA) == 0 &&
572-
smgrprefetch(smgr_reln, forkNum, blockNum))
572+
smgrprefetch(smgr_reln, forkNum, blockNum, 1))
573573
{
574574
result.initiated_io = true;
575575
}

src/backend/storage/buffer/localbuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ PrefetchLocalBuffer(SMgrRelation smgr, ForkNumber forkNum,
9494
#ifdef USE_PREFETCH
9595
/* Not in buffers, so initiate prefetch */
9696
if ((io_direct_flags & IO_DIRECT_DATA) == 0 &&
97-
smgrprefetch(smgr, forkNum, blockNum))
97+
smgrprefetch(smgr, forkNum, blockNum, 1))
9898
{
9999
result.initiated_io = true;
100100
}

src/backend/storage/smgr/md.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -710,27 +710,44 @@ mdclose(SMgrRelation reln, ForkNumber forknum)
710710
}
711711

712712
/*
713-
* mdprefetch() -- Initiate asynchronous read of the specified block of a relation
713+
* mdprefetch() -- Initiate asynchronous read of the specified blocks of a relation
714714
*/
715715
bool
716-
mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
716+
mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
717+
int nblocks)
717718
{
718719
#ifdef USE_PREFETCH
719-
off_t seekpos;
720-
MdfdVec *v;
721720

722721
Assert((io_direct_flags & IO_DIRECT_DATA) == 0);
723722

724-
v = _mdfd_getseg(reln, forknum, blocknum, false,
725-
InRecovery ? EXTENSION_RETURN_NULL : EXTENSION_FAIL);
726-
if (v == NULL)
723+
if ((uint64) blocknum + nblocks > (uint64) MaxBlockNumber + 1)
727724
return false;
728725

729-
seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
726+
while (nblocks > 0)
727+
{
728+
off_t seekpos;
729+
MdfdVec *v;
730+
int nblocks_this_segment;
730731

731-
Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
732+
v = _mdfd_getseg(reln, forknum, blocknum, false,
733+
InRecovery ? EXTENSION_RETURN_NULL : EXTENSION_FAIL);
734+
if (v == NULL)
735+
return false;
736+
737+
seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
732738

733-
(void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ, WAIT_EVENT_DATA_FILE_PREFETCH);
739+
Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
740+
741+
nblocks_this_segment =
742+
Min(nblocks,
743+
RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE)));
744+
745+
(void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ * nblocks_this_segment,
746+
WAIT_EVENT_DATA_FILE_PREFETCH);
747+
748+
blocknum += nblocks_this_segment;
749+
nblocks -= nblocks_this_segment;
750+
}
734751
#endif /* USE_PREFETCH */
735752

736753
return true;

src/backend/storage/smgr/smgr.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ typedef struct f_smgr
5454
void (*smgr_zeroextend) (SMgrRelation reln, ForkNumber forknum,
5555
BlockNumber blocknum, int nblocks, bool skipFsync);
5656
bool (*smgr_prefetch) (SMgrRelation reln, ForkNumber forknum,
57-
BlockNumber blocknum);
57+
BlockNumber blocknum, int nblocks);
5858
void (*smgr_read) (SMgrRelation reln, ForkNumber forknum,
5959
BlockNumber blocknum, void *buffer);
6060
void (*smgr_write) (SMgrRelation reln, ForkNumber forknum,
@@ -547,9 +547,10 @@ smgrzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
547547
* record).
548548
*/
549549
bool
550-
smgrprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
550+
smgrprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
551+
int nblocks)
551552
{
552-
return smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum);
553+
return smgrsw[reln->smgr_which].smgr_prefetch(reln, forknum, blocknum, nblocks);
553554
}
554555

555556
/*

src/include/storage/md.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern void mdextend(SMgrRelation reln, ForkNumber forknum,
3131
extern void mdzeroextend(SMgrRelation reln, ForkNumber forknum,
3232
BlockNumber blocknum, int nblocks, bool skipFsync);
3333
extern bool mdprefetch(SMgrRelation reln, ForkNumber forknum,
34-
BlockNumber blocknum);
34+
BlockNumber blocknum, int nblocks);
3535
extern void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
3636
void *buffer);
3737
extern void mdwrite(SMgrRelation reln, ForkNumber forknum,

src/include/storage/smgr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ extern void smgrextend(SMgrRelation reln, ForkNumber forknum,
9595
extern void smgrzeroextend(SMgrRelation reln, ForkNumber forknum,
9696
BlockNumber blocknum, int nblocks, bool skipFsync);
9797
extern bool smgrprefetch(SMgrRelation reln, ForkNumber forknum,
98-
BlockNumber blocknum);
98+
BlockNumber blocknum, int nblocks);
9999
extern void smgrread(SMgrRelation reln, ForkNumber forknum,
100100
BlockNumber blocknum, void *buffer);
101101
extern void smgrwrite(SMgrRelation reln, ForkNumber forknum,

0 commit comments

Comments
 (0)