@@ -1102,6 +1102,20 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
1102
1102
return stats ;
1103
1103
}
1104
1104
1105
+ /*
1106
+ * Read stream callback returning the next BlockNumber as chosen by the
1107
+ * BlockSampling algorithm.
1108
+ */
1109
+ static BlockNumber
1110
+ block_sampling_read_stream_next (ReadStream * stream ,
1111
+ void * callback_private_data ,
1112
+ void * per_buffer_data )
1113
+ {
1114
+ BlockSamplerData * bs = callback_private_data ;
1115
+
1116
+ return BlockSampler_HasMore (bs ) ? BlockSampler_Next (bs ) : InvalidBlockNumber ;
1117
+ }
1118
+
1105
1119
/*
1106
1120
* acquire_sample_rows -- acquire a random sample of rows from the heap
1107
1121
*
@@ -1154,10 +1168,7 @@ acquire_sample_rows(Relation onerel, int elevel,
1154
1168
TableScanDesc scan ;
1155
1169
BlockNumber nblocks ;
1156
1170
BlockNumber blksdone = 0 ;
1157
- #ifdef USE_PREFETCH
1158
- int prefetch_maximum = 0 ; /* blocks to prefetch if enabled */
1159
- BlockSamplerData prefetch_bs ;
1160
- #endif
1171
+ ReadStream * stream ;
1161
1172
1162
1173
Assert (targrows > 0 );
1163
1174
@@ -1170,13 +1181,6 @@ acquire_sample_rows(Relation onerel, int elevel,
1170
1181
randseed = pg_prng_uint32 (& pg_global_prng_state );
1171
1182
nblocks = BlockSampler_Init (& bs , totalblocks , targrows , randseed );
1172
1183
1173
- #ifdef USE_PREFETCH
1174
- prefetch_maximum = get_tablespace_maintenance_io_concurrency (onerel -> rd_rel -> reltablespace );
1175
- /* Create another BlockSampler, using the same seed, for prefetching */
1176
- if (prefetch_maximum )
1177
- (void ) BlockSampler_Init (& prefetch_bs , totalblocks , targrows , randseed );
1178
- #endif
1179
-
1180
1184
/* Report sampling block numbers */
1181
1185
pgstat_progress_update_param (PROGRESS_ANALYZE_BLOCKS_TOTAL ,
1182
1186
nblocks );
@@ -1187,60 +1191,19 @@ acquire_sample_rows(Relation onerel, int elevel,
1187
1191
scan = heap_beginscan (onerel , NULL , 0 , NULL , NULL , SO_TYPE_ANALYZE );
1188
1192
slot = table_slot_create (onerel , NULL );
1189
1193
1190
- #ifdef USE_PREFETCH
1191
-
1192
- /*
1193
- * If we are doing prefetching, then go ahead and tell the kernel about
1194
- * the first set of pages we are going to want. This also moves our
1195
- * iterator out ahead of the main one being used, where we will keep it so
1196
- * that we're always pre-fetching out prefetch_maximum number of blocks
1197
- * ahead.
1198
- */
1199
- if (prefetch_maximum )
1200
- {
1201
- for (int i = 0 ; i < prefetch_maximum ; i ++ )
1202
- {
1203
- BlockNumber prefetch_block ;
1204
-
1205
- if (!BlockSampler_HasMore (& prefetch_bs ))
1206
- break ;
1207
-
1208
- prefetch_block = BlockSampler_Next (& prefetch_bs );
1209
- PrefetchBuffer (scan -> rs_rd , MAIN_FORKNUM , prefetch_block );
1210
- }
1211
- }
1212
- #endif
1194
+ stream = read_stream_begin_relation (READ_STREAM_MAINTENANCE ,
1195
+ vac_strategy ,
1196
+ scan -> rs_rd ,
1197
+ MAIN_FORKNUM ,
1198
+ block_sampling_read_stream_next ,
1199
+ & bs ,
1200
+ 0 );
1213
1201
1214
1202
/* Outer loop over blocks to sample */
1215
- while (BlockSampler_HasMore ( & bs ))
1203
+ while (heapam_scan_analyze_next_block ( scan , stream ))
1216
1204
{
1217
- BlockNumber targblock = BlockSampler_Next (& bs );
1218
- #ifdef USE_PREFETCH
1219
- BlockNumber prefetch_targblock = InvalidBlockNumber ;
1220
-
1221
- /*
1222
- * Make sure that every time the main BlockSampler is moved forward
1223
- * that our prefetch BlockSampler also gets moved forward, so that we
1224
- * always stay out ahead.
1225
- */
1226
- if (prefetch_maximum && BlockSampler_HasMore (& prefetch_bs ))
1227
- prefetch_targblock = BlockSampler_Next (& prefetch_bs );
1228
- #endif
1229
-
1230
1205
vacuum_delay_point ();
1231
1206
1232
- heapam_scan_analyze_next_block (scan , targblock , vac_strategy );
1233
-
1234
- #ifdef USE_PREFETCH
1235
-
1236
- /*
1237
- * When pre-fetching, after we get a block, tell the kernel about the
1238
- * next one we will want, if there's any left.
1239
- */
1240
- if (prefetch_maximum && prefetch_targblock != InvalidBlockNumber )
1241
- PrefetchBuffer (scan -> rs_rd , MAIN_FORKNUM , prefetch_targblock );
1242
- #endif
1243
-
1244
1207
while (heapam_scan_analyze_next_tuple (scan , OldestXmin , & liverows , & deadrows , slot ))
1245
1208
{
1246
1209
/*
@@ -1290,6 +1253,8 @@ acquire_sample_rows(Relation onerel, int elevel,
1290
1253
++ blksdone );
1291
1254
}
1292
1255
1256
+ read_stream_end (stream );
1257
+
1293
1258
ExecDropSingleTupleTableSlot (slot );
1294
1259
heap_endscan (scan );
1295
1260
0 commit comments