Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd9c7911

Browse files
Use streaming read I/O in autoprewarm
Make a read stream for each valid fork of each valid relationrepresented in the autoprewarm dump file and prewarm those blocksthrough the read stream API instead of by directly invokingReadBuffer().Co-authored-by: Nazir Bilal Yavuz <byavuz81@gmail.com>Co-authored-by: Melanie Plageman <melanieplageman@gmail.com>Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>Reviewed-by: Daniel Gustafsson <daniel@yesql.se>Reviewed-by: Andrey M. Borodin <x4mmm@yandex-team.ru> (earlier versions)Reviewed-by: Kirill Reshke <reshkekirill@gmail.com> (earlier versions)Reviewed-by: Matheus Alcantara <mths.dev@pm.me> (earlier versions)Discussion:https://postgr.es/m/flat/CAN55FZ3n8Gd%2BhajbL%3D5UkGzu_aHGRqnn%2BxktXq2fuds%3D1AOR6Q%40mail.gmail.com
1 parent6acab8b commitd9c7911

File tree

2 files changed

+104
-23
lines changed

2 files changed

+104
-23
lines changed

‎contrib/pg_prewarm/autoprewarm.c

Lines changed: 103 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include"storage/latch.h"
4242
#include"storage/lwlock.h"
4343
#include"storage/procsignal.h"
44+
#include"storage/read_stream.h"
4445
#include"storage/smgr.h"
4546
#include"tcop/tcopprot.h"
4647
#include"utils/guc.h"
@@ -75,6 +76,28 @@ typedef struct AutoPrewarmSharedState
7576
intprewarmed_blocks;
7677
}AutoPrewarmSharedState;
7778

79+
/*
80+
* Private data passed through the read stream API for our use in the
81+
* callback.
82+
*/
83+
typedefstructAutoPrewarmReadStreamData
84+
{
85+
/* The array of records containing the blocks we should prewarm. */
86+
BlockInfoRecord*block_info;
87+
88+
/*
89+
* pos is the read stream callback's index into block_info. Because the
90+
* read stream may read ahead, pos is likely to be ahead of the index in
91+
* the main loop in autoprewarm_database_main().
92+
*/
93+
intpos;
94+
Oidtablespace;
95+
RelFileNumberfilenumber;
96+
ForkNumberforknum;
97+
BlockNumbernblocks;
98+
}AutoPrewarmReadStreamData;
99+
100+
78101
PGDLLEXPORTvoidautoprewarm_main(Datummain_arg);
79102
PGDLLEXPORTvoidautoprewarm_database_main(Datummain_arg);
80103

@@ -422,6 +445,54 @@ apw_load_buffers(void)
422445
apw_state->prewarmed_blocks,num_elements)));
423446
}
424447

448+
/*
449+
* Return the next block number of a specific relation and fork to read
450+
* according to the array of BlockInfoRecord.
451+
*/
452+
staticBlockNumber
453+
apw_read_stream_next_block(ReadStream*stream,
454+
void*callback_private_data,
455+
void*per_buffer_data)
456+
{
457+
AutoPrewarmReadStreamData*p=callback_private_data;
458+
459+
CHECK_FOR_INTERRUPTS();
460+
461+
while (p->pos<apw_state->prewarm_stop_idx)
462+
{
463+
BlockInfoRecordblk=p->block_info[p->pos];
464+
465+
if (!have_free_buffer())
466+
{
467+
p->pos=apw_state->prewarm_stop_idx;
468+
returnInvalidBlockNumber;
469+
}
470+
471+
if (blk.tablespace!=p->tablespace)
472+
returnInvalidBlockNumber;
473+
474+
if (blk.filenumber!=p->filenumber)
475+
returnInvalidBlockNumber;
476+
477+
if (blk.forknum!=p->forknum)
478+
returnInvalidBlockNumber;
479+
480+
p->pos++;
481+
482+
/*
483+
* Check whether blocknum is valid and within fork file size.
484+
* Fast-forward through any invalid blocks. We want p->pos to reflect
485+
* the location of the next relation or fork before ending the stream.
486+
*/
487+
if (blk.blocknum >=p->nblocks)
488+
continue;
489+
490+
returnblk.blocknum;
491+
}
492+
493+
returnInvalidBlockNumber;
494+
}
495+
425496
/*
426497
* Prewarm all blocks for one database (and possibly also global objects, if
427498
* those got grouped with this database).
@@ -462,8 +533,6 @@ autoprewarm_database_main(Datum main_arg)
462533
Oidreloid;
463534
Relationrel;
464535

465-
CHECK_FOR_INTERRUPTS();
466-
467536
/*
468537
* All blocks between prewarm_start_idx and prewarm_stop_idx should
469538
* belong either to global objects or the same database.
@@ -510,6 +579,8 @@ autoprewarm_database_main(Datum main_arg)
510579
{
511580
ForkNumberforknum=blk.forknum;
512581
BlockNumbernblocks;
582+
structAutoPrewarmReadStreamDatap;
583+
ReadStream*stream;
513584
Bufferbuf;
514585

515586
/*
@@ -540,32 +611,41 @@ autoprewarm_database_main(Datum main_arg)
540611

541612
nblocks=RelationGetNumberOfBlocksInFork(rel,blk.forknum);
542613

543-
/* Prewarm buffers. */
544-
while (i<apw_state->prewarm_stop_idx&&
545-
blk.tablespace==tablespace&&
546-
blk.filenumber==filenumber&&
547-
blk.forknum==forknum&&
548-
have_free_buffer())
614+
p= (structAutoPrewarmReadStreamData)
549615
{
550-
CHECK_FOR_INTERRUPTS();
551-
552-
/* Check whether blocknum is valid and within fork file size. */
553-
if (blk.blocknum >=nblocks)
554-
{
555-
blk=block_info[++i];
556-
continue;
557-
}
558-
559-
buf=ReadBufferExtended(rel,blk.forknum,blk.blocknum,RBM_NORMAL,
560-
NULL);
561-
562-
blk=block_info[++i];
563-
if (!BufferIsValid(buf))
564-
break;
616+
.block_info=block_info,
617+
.pos=i,
618+
.tablespace=tablespace,
619+
.filenumber=filenumber,
620+
.forknum=forknum,
621+
.nblocks=nblocks,
622+
};
623+
624+
stream=read_stream_begin_relation(READ_STREAM_DEFAULT |
625+
READ_STREAM_USE_BATCHING,
626+
NULL,
627+
rel,
628+
p.forknum,
629+
apw_read_stream_next_block,
630+
&p,
631+
0);
565632

633+
/*
634+
* Loop until we've prewarmed all the blocks from this fork. The
635+
* read stream callback will check that we still have free buffers
636+
* before requesting each block from the read stream API.
637+
*/
638+
while ((buf=read_stream_next_buffer(stream,NULL))!=InvalidBuffer)
639+
{
566640
apw_state->prewarmed_blocks++;
567641
ReleaseBuffer(buf);
568642
}
643+
644+
read_stream_end(stream);
645+
646+
/* Advance i past all the blocks just prewarmed. */
647+
i=p.pos;
648+
blk=block_info[i];
569649
}
570650

571651
relation_close(rel,AccessShareLock);

‎src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ AttributeOpts
175175
AuthRequest
176176
AuthToken
177177
AutoPrewarmSharedState
178+
AutoPrewarmReadStreamData
178179
AutoVacOpts
179180
AutoVacuumShmemStruct
180181
AutoVacuumWorkItem

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp