/* Private state */ typedefstruct
{
uint32 seed; /* random seed */ double millis; /* time limit for sampling */
instr_time start_time; /* scan start time */
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 */
} SystemTimeSamplerData;
staticvoid system_time_samplescangetsamplesize(PlannerInfo *root,
RelOptInfo *baserel,
List *paramexprs,
BlockNumber *pages, double *tuples); staticvoid system_time_initsamplescan(SampleScanState *node, int eflags); staticvoid system_time_beginsamplescan(SampleScanState *node,
Datum *params, int nparams,
uint32 seed); static BlockNumber system_time_nextsampleblock(SampleScanState *node, BlockNumber nblocks); static OffsetNumber system_time_nextsampletuple(SampleScanState *node,
BlockNumber blockno,
OffsetNumber maxoffset); static uint32 random_relative_prime(uint32 n, pg_prng_state *randstate);
/* *CreateaTsmRoutinedescriptorfortheSYSTEM_TIMEmethod.
*/
Datum
tsm_system_time_handler(PG_FUNCTION_ARGS)
{
TsmRoutine *tsm = makeNode(TsmRoutine);
tsm->parameterTypes = list_make1_oid(FLOAT8OID);
/* See notes at head of file */
tsm->repeatable_across_queries = false;
tsm->repeatable_across_scans = false;
/* Try to extract an estimate for the limit time spec */
limitnode = (Node *) linitial(paramexprs);
limitnode = estimate_expression_value(root, limitnode);
if (IsA(limitnode, Const) &&
!((Const *) limitnode)->constisnull)
{
millis = DatumGetFloat8(((Const *) limitnode)->constvalue); if (millis < 0 || isnan(millis))
{ /* Default millis if the value is bogus */
millis = 1000;
}
} else
{ /* Default millis if we didn't obtain a non-null Const */
millis = 1000;
}
/* Get the planner's idea of cost per page read */
get_tablespace_page_costs(baserel->reltablespace,
&spc_random_page_cost,
NULL);
/* *Estimatethenumberofpageswecanreadbyassumingthatthecost *figureisexpressedinmilliseconds.Thisiscompletely,unmistakably *bogus,butwehavetodosomethingtoproduceanestimateandthere's *nobetteranswer.
*/ if (spc_random_page_cost > 0)
npages = millis / spc_random_page_cost; else
npages = millis; /* even more bogus, but whatcha gonna do? */
/* Clamp to sane value */
npages = clamp_row_est(Min((double) baserel->pages, npages));
if (baserel->tuples > 0 && baserel->pages > 0)
{ /* Estimate number of tuples returned based on tuple density */ double density = baserel->tuples / (double) baserel->pages;
ntuples = npages * density;
} else
{ /* For lack of data, assume one tuple per page */
ntuples = npages;
}
/* Clamp to the estimated relation size */
ntuples = clamp_row_est(Min(baserel->tuples, ntuples));
*pages = npages;
*tuples = ntuples;
}
/* *Initializeduringexecutorsetup.
*/ staticvoid
system_time_initsamplescan(SampleScanState *node, int eflags)
{
node->tsm_state = palloc0(sizeof(SystemTimeSamplerData)); /* Note the above leaves tsm_state->step equal to zero */
}
if (millis < 0 || isnan(millis))
ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
errmsg("sample collection time must not be negative")));
sampler->seed = seed;
sampler->millis = millis;
sampler->lt = InvalidOffsetNumber;
sampler->doneblocks = 0; /* start_time, 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 in relation, we're done */ if (++sampler->doneblocks > sampler->nblocks) return InvalidBlockNumber;
/* If we've used up all the allotted time, we're done */
INSTR_TIME_SET_CURRENT(cur_time);
INSTR_TIME_SUBTRACT(cur_time, sampler->start_time); if (INSTR_TIME_GET_MILLISEC(cur_time) >= sampler->millis) 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.