17
17
#include <math.h>
18
18
19
19
#include "access/detoast.h"
20
+ #include "access/heapam.h"
20
21
#include "access/genam.h"
21
22
#include "access/multixact.h"
22
23
#include "access/relation.h"
@@ -190,10 +191,9 @@ analyze_rel(Oid relid, RangeVar *relation,
190
191
if (onerel -> rd_rel -> relkind == RELKIND_RELATION ||
191
192
onerel -> rd_rel -> relkind == RELKIND_MATVIEW )
192
193
{
193
- /* Regular table, so we'll use the regular row acquisition function */
194
- acquirefunc = acquire_sample_rows ;
195
- /* Also get regular table's size */
196
- relpages = RelationGetNumberOfBlocks (onerel );
194
+ /* Use row acquisition function provided by table AM */
195
+ table_relation_analyze (onerel , & acquirefunc ,
196
+ & relpages , vac_strategy );
197
197
}
198
198
else if (onerel -> rd_rel -> relkind == RELKIND_FOREIGN_TABLE )
199
199
{
@@ -1103,15 +1103,15 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
1103
1103
}
1104
1104
1105
1105
/*
1106
- * acquire_sample_rows -- acquire a random sample of rows from the table
1106
+ * acquire_sample_rows -- acquire a random sample of rows from the heap
1107
1107
*
1108
1108
* Selected rows are returned in the caller-allocated array rows[], which
1109
1109
* must have at least targrows entries.
1110
1110
* The actual number of rows selected is returned as the function result.
1111
- * We also estimate the total numbers of live and dead rows in the table ,
1111
+ * We also estimate the total numbers of live and dead rows in the heap ,
1112
1112
* and return them into *totalrows and *totaldeadrows, respectively.
1113
1113
*
1114
- * The returned list of tuples is in order by physical position in the table .
1114
+ * The returned list of tuples is in order by physical position in the heap .
1115
1115
* (We will rely on this later to derive correlation estimates.)
1116
1116
*
1117
1117
* As of May 2004 we use a new two-stage method: Stage one selects up
@@ -1133,7 +1133,7 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
1133
1133
* look at a statistically unbiased set of blocks, we should get
1134
1134
* unbiased estimates of the average numbers of live and dead rows per
1135
1135
* block. The previous sampling method put too much credence in the row
1136
- * density near the start of the table .
1136
+ * density near the start of the heap .
1137
1137
*/
1138
1138
static int
1139
1139
acquire_sample_rows (Relation onerel , int elevel ,
@@ -1184,7 +1184,7 @@ acquire_sample_rows(Relation onerel, int elevel,
1184
1184
/* Prepare for sampling rows */
1185
1185
reservoir_init_selection_state (& rstate , targrows );
1186
1186
1187
- scan = table_beginscan_analyze (onerel );
1187
+ scan = heap_beginscan (onerel , NULL , 0 , NULL , NULL , SO_TYPE_ANALYZE );
1188
1188
slot = table_slot_create (onerel , NULL );
1189
1189
1190
1190
#ifdef USE_PREFETCH
@@ -1214,7 +1214,6 @@ acquire_sample_rows(Relation onerel, int elevel,
1214
1214
/* Outer loop over blocks to sample */
1215
1215
while (BlockSampler_HasMore (& bs ))
1216
1216
{
1217
- bool block_accepted ;
1218
1217
BlockNumber targblock = BlockSampler_Next (& bs );
1219
1218
#ifdef USE_PREFETCH
1220
1219
BlockNumber prefetch_targblock = InvalidBlockNumber ;
@@ -1230,29 +1229,19 @@ acquire_sample_rows(Relation onerel, int elevel,
1230
1229
1231
1230
vacuum_delay_point ();
1232
1231
1233
- block_accepted = table_scan_analyze_next_block (scan , targblock , vac_strategy );
1232
+ heapam_scan_analyze_next_block (scan , targblock , vac_strategy );
1234
1233
1235
1234
#ifdef USE_PREFETCH
1236
1235
1237
1236
/*
1238
1237
* When pre-fetching, after we get a block, tell the kernel about the
1239
1238
* next one we will want, if there's any left.
1240
- *
1241
- * We want to do this even if the table_scan_analyze_next_block() call
1242
- * above decides against analyzing the block it picked.
1243
1239
*/
1244
1240
if (prefetch_maximum && prefetch_targblock != InvalidBlockNumber )
1245
1241
PrefetchBuffer (scan -> rs_rd , MAIN_FORKNUM , prefetch_targblock );
1246
1242
#endif
1247
1243
1248
- /*
1249
- * Don't analyze if table_scan_analyze_next_block() indicated this
1250
- * block is unsuitable for analyzing.
1251
- */
1252
- if (!block_accepted )
1253
- continue ;
1254
-
1255
- while (table_scan_analyze_next_tuple (scan , OldestXmin , & liverows , & deadrows , slot ))
1244
+ while (heapam_scan_analyze_next_tuple (scan , OldestXmin , & liverows , & deadrows , slot ))
1256
1245
{
1257
1246
/*
1258
1247
* The first targrows sample rows are simply copied into the
@@ -1302,7 +1291,7 @@ acquire_sample_rows(Relation onerel, int elevel,
1302
1291
}
1303
1292
1304
1293
ExecDropSingleTupleTableSlot (slot );
1305
- table_endscan (scan );
1294
+ heap_endscan (scan );
1306
1295
1307
1296
/*
1308
1297
* If we didn't find as many tuples as we wanted then we're done. No sort
@@ -1373,6 +1362,19 @@ compare_rows(const void *a, const void *b, void *arg)
1373
1362
return 0 ;
1374
1363
}
1375
1364
1365
+ /*
1366
+ * heapam_analyze -- implementation of relation_analyze() table access method
1367
+ * callback for heap
1368
+ */
1369
+ void
1370
+ heapam_analyze (Relation relation , AcquireSampleRowsFunc * func ,
1371
+ BlockNumber * totalpages , BufferAccessStrategy bstrategy )
1372
+ {
1373
+ * func = acquire_sample_rows ;
1374
+ * totalpages = RelationGetNumberOfBlocks (relation );
1375
+ vac_strategy = bstrategy ;
1376
+ }
1377
+
1376
1378
1377
1379
/*
1378
1380
* acquire_inherited_sample_rows -- acquire sample rows from inheritance tree
@@ -1462,9 +1464,9 @@ acquire_inherited_sample_rows(Relation onerel, int elevel,
1462
1464
if (childrel -> rd_rel -> relkind == RELKIND_RELATION ||
1463
1465
childrel -> rd_rel -> relkind == RELKIND_MATVIEW )
1464
1466
{
1465
- /* Regular table, so use the regular row acquisition function */
1466
- acquirefunc = acquire_sample_rows ;
1467
- relpages = RelationGetNumberOfBlocks ( childrel );
1467
+ /* Use row acquisition function provided by table AM */
1468
+ table_relation_analyze ( childrel , & acquirefunc ,
1469
+ & relpages , vac_strategy );
1468
1470
}
1469
1471
else if (childrel -> rd_rel -> relkind == RELKIND_FOREIGN_TABLE )
1470
1472
{
0 commit comments