/* Private state */ typedefstruct
{
uint32 seed; /* random seed */
int64 ntuples; /* number of tuples to return */
OffsetNumber lt; /* last tuple returned from current block */
BlockNumber doneblocks; /* number of already-scanned blocks */
BlockNumber lb; /* last block visited */ /* these three values are not changed during a rescan: */
BlockNumber nblocks; /* number of blocks in relation */
BlockNumber firstblock; /* first block to sample from */
BlockNumber step; /* step size, or 0 if not set yet */
} SystemRowsSamplerData;
staticvoid system_rows_samplescangetsamplesize(PlannerInfo *root,
RelOptInfo *baserel,
List *paramexprs,
BlockNumber *pages, double *tuples); staticvoid system_rows_initsamplescan(SampleScanState *node, int eflags); staticvoid system_rows_beginsamplescan(SampleScanState *node,
Datum *params, int nparams,
uint32 seed); static BlockNumber system_rows_nextsampleblock(SampleScanState *node, BlockNumber nblocks); static OffsetNumber system_rows_nextsampletuple(SampleScanState *node,
BlockNumber blockno,
OffsetNumber maxoffset); static uint32 random_relative_prime(uint32 n, pg_prng_state *randstate);
/* *CreateaTsmRoutinedescriptorfortheSYSTEM_ROWSmethod.
*/
Datum
tsm_system_rows_handler(PG_FUNCTION_ARGS)
{
TsmRoutine *tsm = makeNode(TsmRoutine);
tsm->parameterTypes = list_make1_oid(INT8OID);
/* See notes at head of file */
tsm->repeatable_across_queries = false;
tsm->repeatable_across_scans = true;
/* Try to extract an estimate for the limit rowcount */
limitnode = (Node *) linitial(paramexprs);
limitnode = estimate_expression_value(root, limitnode);
if (IsA(limitnode, Const) &&
!((Const *) limitnode)->constisnull)
{
ntuples = DatumGetInt64(((Const *) limitnode)->constvalue); if (ntuples < 0)
{ /* Default ntuples if the value is bogus */
ntuples = 1000;
}
} else
{ /* Default ntuples if we didn't obtain a non-null Const */
ntuples = 1000;
}
/* Clamp to the estimated relation size */ if (ntuples > baserel->tuples)
ntuples = (int64) baserel->tuples;
ntuples = clamp_row_est(ntuples);
if (baserel->tuples > 0 && baserel->pages > 0)
{ /* Estimate number of pages visited based on tuple density */ double density = baserel->tuples / (double) baserel->pages;
npages = ntuples / density;
} else
{ /* For lack of data, assume one tuple per page */
npages = ntuples;
}
/* Clamp to sane value */
npages = clamp_row_est(Min((double) baserel->pages, npages));
*pages = npages;
*tuples = ntuples;
}
/* *Initializeduringexecutorsetup.
*/ staticvoid
system_rows_initsamplescan(SampleScanState *node, int eflags)
{
node->tsm_state = palloc0(sizeof(SystemRowsSamplerData)); /* Note the above leaves tsm_state->step equal to zero */
}
if (ntuples < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
errmsg("sample size must not be negative")));
sampler->seed = seed;
sampler->ntuples = ntuples;
sampler->lt = InvalidOffsetNumber;
sampler->doneblocks = 0; /* lb will be initialized during first NextSampleBlock call */ /* we intentionally do not change nblocks/firstblock/step here */
/* First call within scan? */ if (sampler->doneblocks == 0)
{ /* First scan within query? */ if (sampler->step == 0)
{ /* Initialize now that we have scan descriptor */
pg_prng_state randstate;
/* If relation is empty, there's nothing to scan */ if (nblocks == 0) return InvalidBlockNumber;
/* We only need an RNG during this setup step */
sampler_random_init_state(sampler->seed, &randstate);
/* Compute nblocks/firstblock/step only once per query */
sampler->nblocks = nblocks;
/* Choose random starting block within the relation */ /* (Actually this is the predecessor of the first block visited) */
sampler->firstblock = sampler_random_fract(&randstate) *
sampler->nblocks;
/* Find relative prime as step size for linear probing */
sampler->step = random_relative_prime(sampler->nblocks, &randstate);
}
/* If we've read all blocks or returned all needed tuples, we're done */ if (++sampler->doneblocks > sampler->nblocks ||
node->donetuples >= sampler->ntuples) return InvalidBlockNumber;
/* *It'sprobablyimpossibleforscan->rs_nblockstodecreasebetweenscans *withinaquery;butjustincase,loopuntilweselectablocknumber *lessthanscan->rs_nblocks.Wedon'tcareifscan->rs_nblockshas *increasedsincethefirstscan.
*/ do
{ /* Advance lb, using uint64 arithmetic to forestall overflow */
sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks;
} while (sampler->lb >= nblocks);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.