Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
470 changes: 462 additions & 8 deletions src/backend/access/aocs/aocsam.c

Large diffs are not rendered by default.

94 changes: 74 additions & 20 deletions src/backend/access/aocs/aocsam_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "utils/lsyscache.h"
#include "utils/pg_rusage.h"
#include "utils/guc.h"
#include "utils/sampling.h"

#define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)

Expand Down Expand Up @@ -1623,39 +1624,92 @@ static bool
aoco_scan_analyze_next_block(TableScanDesc scan, BlockNumber blockno,
BufferAccessStrategy bstrategy)
{
AOCSScanDesc aoscan = (AOCSScanDesc) scan;
aoscan->targetTupleId = blockno;

return true;
/*
* For append-optimized relations, we use a separate sampling
* method. See table_relation_acquire_sample_rows().
*/
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("API not supported for appendoptimized relations")));
}

static bool
aoco_scan_analyze_next_tuple(TableScanDesc scan, TransactionId OldestXmin,
double *liverows, double *deadrows,
TupleTableSlot *slot)
{
AOCSScanDesc aoscan = (AOCSScanDesc) scan;
bool ret = false;
/*
* For append-optimized relations, we use a separate sampling
* method. See table_relation_acquire_sample_rows().
*/
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("API not supported for appendoptimized relations")));
}

/* skip several tuples if they are not sampling target */
while (aoscan->targetTupleId > aoscan->nextTupleId)
{
aoco_getnextslot(scan, ForwardScanDirection, slot);
aoscan->nextTupleId++;
}
static int
aoco_acquire_sample_rows(Relation onerel, int elevel, HeapTuple *rows,
int targrows, double *totalrows, double *totaldeadrows)
{
int numrows = 0; /* # rows now in reservoir */
double liverows = 0; /* # live rows seen */
double deadrows = 0; /* # dead rows seen */

if (aoscan->targetTupleId == aoscan->nextTupleId)
Assert(targrows > 0);

TableScanDesc scan = table_beginscan_analyze(onerel);
TupleTableSlot *slot = table_slot_create(onerel, NULL);
AOCSScanDesc aocoscan = (AOCSScanDesc) scan;

int64 totaltupcount = AOCSScanDesc_TotalTupCount(aocoscan);
int64 totaldeadtupcount = 0;
if (aocoscan->total_seg > 0 )
totaldeadtupcount = AppendOnlyVisimap_GetRelationHiddenTupleCount(&aocoscan->visibilityMap);
/*
* The conversion from int64 to double (53 significant bits) is safe as the
* AOTupleId is 48bits, the max value of totalrows is never greater than
* AOTupleId_MaxSegmentFileNum * AOTupleId_MaxRowNum (< 48 significant bits).
*/
*totalrows = (double) (totaltupcount - totaldeadtupcount);
*totaldeadrows = (double) totaldeadtupcount;

/* Prepare for sampling tuple numbers */
RowSamplerData rs;
RowSampler_Init(&rs, *totalrows, targrows, random());

while (RowSampler_HasMore(&rs))
{
ret = aoco_getnextslot(scan, ForwardScanDirection, slot);
aoscan->nextTupleId++;
aocoscan->targrow = RowSampler_Next(&rs);

vacuum_delay_point();

if (ret)
*liverows += 1;
if (aocs_get_target_tuple(aocoscan, aocoscan->targrow, slot))
{
rows[numrows++] = ExecCopySlotHeapTuple(slot);
liverows++;
}
else
*deadrows += 1; /* if return an invisible tuple */
deadrows++;

ExecClearTuple(slot);
}

return ret;
ExecDropSingleTupleTableSlot(slot);
table_endscan(scan);

/*
* Emit some interesting relation info
*/
ereport(elevel,
(errmsg("\"%s\": scanned " INT64_FORMAT " rows, "
"containing %.0f live rows and %.0f dead rows; "
"%d rows in sample, %.0f accurate total live rows, "
"%.f accurate total dead rows",
RelationGetRelationName(onerel),
rs.m, liverows, deadrows, numrows,
*totalrows, *totaldeadrows)));

return numrows;
}

static double
Expand Down Expand Up @@ -2588,6 +2642,7 @@ static TableAmRoutine ao_column_methods = {
.relation_vacuum = aoco_vacuum_rel,
.scan_analyze_next_block = aoco_scan_analyze_next_block,
.scan_analyze_next_tuple = aoco_scan_analyze_next_tuple,
.relation_acquire_sample_rows = aoco_acquire_sample_rows,
.index_build_range_scan = aoco_index_build_range_scan,
.index_validate_scan = aoco_index_validate_scan,

Expand All @@ -2602,7 +2657,6 @@ static TableAmRoutine ao_column_methods = {
.scan_bitmap_next_tuple = aoco_scan_bitmap_next_tuple,
.scan_sample_next_block = aoco_scan_sample_next_block,
.scan_sample_next_tuple = aoco_scan_sample_next_tuple,
.acquire_sample_rows = acquire_sample_rows,

.amoptions = ao_amoptions,
.swap_relation_files = aoco_swap_relation_files,
Expand Down
Loading
Loading