Skip to content

Commit c582b75

Browse files
committed
Add block_range_read_stream_cb(), to deduplicate code.
This replaces two functions for iterating over all blocks in a range. A pending patch will use this instead of adding a third. Nazir Bilal Yavuz Discussion: https://postgr.es/m/20240820184742.f2.nmisch@google.com
1 parent ba7625a commit c582b75

File tree

5 files changed

+36
-54
lines changed

5 files changed

+36
-54
lines changed

contrib/pg_prewarm/pg_prewarm.c

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,6 @@ typedef enum
3939

4040
static PGIOAlignedBlock blockbuffer;
4141

42-
struct pg_prewarm_read_stream_private
43-
{
44-
BlockNumber blocknum;
45-
int64 last_block;
46-
};
47-
48-
static BlockNumber
49-
pg_prewarm_read_stream_next_block(ReadStream *stream,
50-
void *callback_private_data,
51-
void *per_buffer_data)
52-
{
53-
struct pg_prewarm_read_stream_private *p = callback_private_data;
54-
55-
if (p->blocknum <= p->last_block)
56-
return p->blocknum++;
57-
58-
return InvalidBlockNumber;
59-
}
60-
6142
/*
6243
* pg_prewarm(regclass, mode text, fork text,
6344
* first_block int8, last_block int8)
@@ -203,22 +184,22 @@ pg_prewarm(PG_FUNCTION_ARGS)
203184
}
204185
else if (ptype == PREWARM_BUFFER)
205186
{
206-
struct pg_prewarm_read_stream_private p;
187+
BlockRangeReadStreamPrivate p;
207188
ReadStream *stream;
208189

209190
/*
210191
* In buffer mode, we actually pull the data into shared_buffers.
211192
*/
212193

213194
/* Set up the private state for our streaming buffer read callback. */
214-
p.blocknum = first_block;
215-
p.last_block = last_block;
195+
p.current_blocknum = first_block;
196+
p.last_exclusive = last_block + 1;
216197

217198
stream = read_stream_begin_relation(READ_STREAM_FULL,
218199
NULL,
219200
rel,
220201
forkNumber,
221-
pg_prewarm_read_stream_next_block,
202+
block_range_read_stream_cb,
222203
&p,
223204
0);
224205

src/backend/storage/aio/read_stream.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,23 @@ get_per_buffer_data(ReadStream *stream, int16 buffer_index)
163163
stream->per_buffer_data_size * buffer_index;
164164
}
165165

166+
/*
167+
* General-use ReadStreamBlockNumberCB for block range scans. Loops over the
168+
* blocks [current_blocknum, last_exclusive).
169+
*/
170+
BlockNumber
171+
block_range_read_stream_cb(ReadStream *stream,
172+
void *callback_private_data,
173+
void *per_buffer_data)
174+
{
175+
BlockRangeReadStreamPrivate *p = callback_private_data;
176+
177+
if (p->current_blocknum < p->last_exclusive)
178+
return p->current_blocknum++;
179+
180+
return InvalidBlockNumber;
181+
}
182+
166183
/*
167184
* Ask the callback which block it would like us to read next, with a one block
168185
* buffer in front to allow read_stream_unget_block() to work.

src/backend/storage/buffer/bufmgr.c

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,6 @@ typedef struct SMgrSortArray
136136
SMgrRelation srel;
137137
} SMgrSortArray;
138138

139-
/*
140-
* Helper struct for read stream object used in
141-
* RelationCopyStorageUsingBuffer() function.
142-
*/
143-
struct copy_storage_using_buffer_read_stream_private
144-
{
145-
BlockNumber blocknum;
146-
BlockNumber nblocks;
147-
};
148-
149-
/*
150-
* Callback function to get next block for read stream object used in
151-
* RelationCopyStorageUsingBuffer() function.
152-
*/
153-
static BlockNumber
154-
copy_storage_using_buffer_read_stream_next_block(ReadStream *stream,
155-
void *callback_private_data,
156-
void *per_buffer_data)
157-
{
158-
struct copy_storage_using_buffer_read_stream_private *p = callback_private_data;
159-
160-
if (p->blocknum < p->nblocks)
161-
return p->blocknum++;
162-
163-
return InvalidBlockNumber;
164-
}
165-
166139
/* GUC variables */
167140
bool zero_damaged_pages = false;
168141
int bgwriter_lru_maxpages = 100;
@@ -4710,7 +4683,7 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
47104683
PGIOAlignedBlock buf;
47114684
BufferAccessStrategy bstrategy_src;
47124685
BufferAccessStrategy bstrategy_dst;
4713-
struct copy_storage_using_buffer_read_stream_private p;
4686+
BlockRangeReadStreamPrivate p;
47144687
ReadStream *src_stream;
47154688
SMgrRelation src_smgr;
47164689

@@ -4742,15 +4715,15 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
47424715
bstrategy_dst = GetAccessStrategy(BAS_BULKWRITE);
47434716

47444717
/* Initialize streaming read */
4745-
p.blocknum = 0;
4746-
p.nblocks = nblocks;
4718+
p.current_blocknum = 0;
4719+
p.last_exclusive = nblocks;
47474720
src_smgr = smgropen(srclocator, INVALID_PROC_NUMBER);
47484721
src_stream = read_stream_begin_smgr_relation(READ_STREAM_FULL,
47494722
bstrategy_src,
47504723
src_smgr,
47514724
permanent ? RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED,
47524725
forkNum,
4753-
copy_storage_using_buffer_read_stream_next_block,
4726+
block_range_read_stream_cb,
47544727
&p,
47554728
0);
47564729

src/include/storage/read_stream.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,21 @@
4545
struct ReadStream;
4646
typedef struct ReadStream ReadStream;
4747

48+
/* for block_range_read_stream_cb */
49+
typedef struct BlockRangeReadStreamPrivate
50+
{
51+
BlockNumber current_blocknum;
52+
BlockNumber last_exclusive;
53+
} BlockRangeReadStreamPrivate;
54+
4855
/* Callback that returns the next block number to read. */
4956
typedef BlockNumber (*ReadStreamBlockNumberCB) (ReadStream *stream,
5057
void *callback_private_data,
5158
void *per_buffer_data);
5259

60+
extern BlockNumber block_range_read_stream_cb(ReadStream *stream,
61+
void *callback_private_data,
62+
void *per_buffer_data);
5363
extern ReadStream *read_stream_begin_relation(int flags,
5464
BufferAccessStrategy strategy,
5565
Relation rel,

src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ BlockId
275275
BlockIdData
276276
BlockInfoRecord
277277
BlockNumber
278+
BlockRangeReadStreamPrivate
278279
BlockRefTable
279280
BlockRefTableBuffer
280281
BlockRefTableChunk

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy