diff options
Diffstat (limited to 'src/backend/access/tablesample')
-rw-r--r-- | src/backend/access/tablesample/bernoulli.c | 69 | ||||
-rw-r--r-- | src/backend/access/tablesample/system.c | 48 | ||||
-rw-r--r-- | src/backend/access/tablesample/tablesample.c | 94 |
3 files changed, 106 insertions, 105 deletions
diff --git a/src/backend/access/tablesample/bernoulli.c b/src/backend/access/tablesample/bernoulli.c index c91f3f593e5..563a9168f0f 100644 --- a/src/backend/access/tablesample/bernoulli.c +++ b/src/backend/access/tablesample/bernoulli.c @@ -27,13 +27,15 @@ /* tsdesc */ typedef struct { - uint32 seed; /* random seed */ - BlockNumber startblock; /* starting block, we use ths for syncscan support */ + uint32 seed; /* random seed */ + BlockNumber startblock; /* starting block, we use ths for syncscan + * support */ BlockNumber nblocks; /* number of blocks */ BlockNumber blockno; /* current block */ - float4 probability; /* probabilty that tuple will be returned (0.0-1.0) */ + float4 probability; /* probabilty that tuple will be returned + * (0.0-1.0) */ OffsetNumber lt; /* last tuple returned from current block */ - SamplerRandomState randstate; /* random generator tsdesc */ + SamplerRandomState randstate; /* random generator tsdesc */ } BernoulliSamplerData; /* @@ -42,10 +44,10 @@ typedef struct Datum tsm_bernoulli_init(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - uint32 seed = PG_GETARG_UINT32(1); - float4 percent = PG_ARGISNULL(2) ? -1 : PG_GETARG_FLOAT4(2); - HeapScanDesc scan = tsdesc->heapScan; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + uint32 seed = PG_GETARG_UINT32(1); + float4 percent = PG_ARGISNULL(2) ? -1 : PG_GETARG_FLOAT4(2); + HeapScanDesc scan = tsdesc->heapScan; BernoulliSamplerData *sampler; if (percent < 0 || percent > 100) @@ -77,14 +79,13 @@ tsm_bernoulli_init(PG_FUNCTION_ARGS) Datum tsm_bernoulli_nextblock(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - BernoulliSamplerData *sampler = - (BernoulliSamplerData *) tsdesc->tsmdata; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + BernoulliSamplerData *sampler = + (BernoulliSamplerData *) tsdesc->tsmdata; /* - * Bernoulli sampling scans all blocks on the table and supports - * syncscan so loop from startblock to startblock instead of - * from 0 to nblocks. + * Bernoulli sampling scans all blocks on the table and supports syncscan + * so loop from startblock to startblock instead of from 0 to nblocks. */ if (sampler->blockno == InvalidBlockNumber) sampler->blockno = sampler->startblock; @@ -116,7 +117,7 @@ tsm_bernoulli_nextblock(PG_FUNCTION_ARGS) * tuples have same probability of being returned the visible and invisible * tuples will be returned in same ratio as they have in the actual table. * This means that there is no skew towards either visible or invisible tuples - * and the number returned visible tuples to from the executor node is the + * and the number returned visible tuples to from the executor node is the * fraction of visible tuples which was specified in input. * * This is faster than doing the coinflip in the examinetuple because we don't @@ -128,12 +129,12 @@ tsm_bernoulli_nextblock(PG_FUNCTION_ARGS) Datum tsm_bernoulli_nexttuple(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - OffsetNumber maxoffset = PG_GETARG_UINT16(2); - BernoulliSamplerData *sampler = - (BernoulliSamplerData *) tsdesc->tsmdata; - OffsetNumber tupoffset = sampler->lt; - float4 probability = sampler->probability; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + OffsetNumber maxoffset = PG_GETARG_UINT16(2); + BernoulliSamplerData *sampler = + (BernoulliSamplerData *) tsdesc->tsmdata; + OffsetNumber tupoffset = sampler->lt; + float4 probability = sampler->probability; if (tupoffset == InvalidOffsetNumber) tupoffset = FirstOffsetNumber; @@ -142,8 +143,8 @@ tsm_bernoulli_nexttuple(PG_FUNCTION_ARGS) /* * Loop over tuple offsets until the random generator returns value that - * is within the probability of returning the tuple or until we reach - * end of the block. + * is within the probability of returning the tuple or until we reach end + * of the block. * * (This is our implementation of bernoulli trial) */ @@ -183,9 +184,9 @@ tsm_bernoulli_end(PG_FUNCTION_ARGS) Datum tsm_bernoulli_reset(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - BernoulliSamplerData *sampler = - (BernoulliSamplerData *) tsdesc->tsmdata; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + BernoulliSamplerData *sampler = + (BernoulliSamplerData *) tsdesc->tsmdata; sampler->blockno = InvalidBlockNumber; sampler->lt = InvalidOffsetNumber; @@ -200,14 +201,14 @@ tsm_bernoulli_reset(PG_FUNCTION_ARGS) Datum tsm_bernoulli_cost(PG_FUNCTION_ARGS) { - PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0); - Path *path = (Path *) PG_GETARG_POINTER(1); - RelOptInfo *baserel = (RelOptInfo *) PG_GETARG_POINTER(2); - List *args = (List *) PG_GETARG_POINTER(3); - BlockNumber *pages = (BlockNumber *) PG_GETARG_POINTER(4); - double *tuples = (double *) PG_GETARG_POINTER(5); - Node *pctnode; - float4 samplesize; + PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0); + Path *path = (Path *) PG_GETARG_POINTER(1); + RelOptInfo *baserel = (RelOptInfo *) PG_GETARG_POINTER(2); + List *args = (List *) PG_GETARG_POINTER(3); + BlockNumber *pages = (BlockNumber *) PG_GETARG_POINTER(4); + double *tuples = (double *) PG_GETARG_POINTER(5); + Node *pctnode; + float4 samplesize; *pages = baserel->pages; diff --git a/src/backend/access/tablesample/system.c b/src/backend/access/tablesample/system.c index 1412e511faf..1d834369a4b 100644 --- a/src/backend/access/tablesample/system.c +++ b/src/backend/access/tablesample/system.c @@ -31,9 +31,9 @@ typedef struct { BlockSamplerData bs; - uint32 seed; /* random seed */ + uint32 seed; /* random seed */ BlockNumber nblocks; /* number of block in relation */ - int samplesize; /* number of blocks to return */ + int samplesize; /* number of blocks to return */ OffsetNumber lt; /* last tuple returned from current block */ } SystemSamplerData; @@ -44,11 +44,11 @@ typedef struct Datum tsm_system_init(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - uint32 seed = PG_GETARG_UINT32(1); - float4 percent = PG_ARGISNULL(2) ? -1 : PG_GETARG_FLOAT4(2); - HeapScanDesc scan = tsdesc->heapScan; - SystemSamplerData *sampler; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + uint32 seed = PG_GETARG_UINT32(1); + float4 percent = PG_ARGISNULL(2) ? -1 : PG_GETARG_FLOAT4(2); + HeapScanDesc scan = tsdesc->heapScan; + SystemSamplerData *sampler; if (percent < 0 || percent > 100) ereport(ERROR, @@ -80,9 +80,9 @@ tsm_system_init(PG_FUNCTION_ARGS) Datum tsm_system_nextblock(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; - BlockNumber blockno; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; + BlockNumber blockno; if (!BlockSampler_HasMore(&sampler->bs)) PG_RETURN_UINT32(InvalidBlockNumber); @@ -99,10 +99,10 @@ tsm_system_nextblock(PG_FUNCTION_ARGS) Datum tsm_system_nexttuple(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - OffsetNumber maxoffset = PG_GETARG_UINT16(2); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; - OffsetNumber tupoffset = sampler->lt; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + OffsetNumber maxoffset = PG_GETARG_UINT16(2); + SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; + OffsetNumber tupoffset = sampler->lt; if (tupoffset == InvalidOffsetNumber) tupoffset = FirstOffsetNumber; @@ -136,8 +136,8 @@ tsm_system_end(PG_FUNCTION_ARGS) Datum tsm_system_reset(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; sampler->lt = InvalidOffsetNumber; BlockSampler_Init(&sampler->bs, sampler->nblocks, sampler->samplesize, @@ -152,14 +152,14 @@ tsm_system_reset(PG_FUNCTION_ARGS) Datum tsm_system_cost(PG_FUNCTION_ARGS) { - PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0); - Path *path = (Path *) PG_GETARG_POINTER(1); - RelOptInfo *baserel = (RelOptInfo *) PG_GETARG_POINTER(2); - List *args = (List *) PG_GETARG_POINTER(3); - BlockNumber *pages = (BlockNumber *) PG_GETARG_POINTER(4); - double *tuples = (double *) PG_GETARG_POINTER(5); - Node *pctnode; - float4 samplesize; + PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0); + Path *path = (Path *) PG_GETARG_POINTER(1); + RelOptInfo *baserel = (RelOptInfo *) PG_GETARG_POINTER(2); + List *args = (List *) PG_GETARG_POINTER(3); + BlockNumber *pages = (BlockNumber *) PG_GETARG_POINTER(4); + double *tuples = (double *) PG_GETARG_POINTER(5); + Node *pctnode; + float4 samplesize; pctnode = linitial(args); pctnode = estimate_expression_value(root, pctnode); diff --git a/src/backend/access/tablesample/tablesample.c b/src/backend/access/tablesample/tablesample.c index ef55d062e75..3398d02f854 100644 --- a/src/backend/access/tablesample/tablesample.c +++ b/src/backend/access/tablesample/tablesample.c @@ -1,14 +1,14 @@ /*------------------------------------------------------------------------- * * tablesample.c - * TABLESAMPLE internal API + * TABLESAMPLE internal API * * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION - * src/backend/access/tablesample/tablesample.c + * src/backend/access/tablesample/tablesample.c * * TABLESAMPLE is the SQL standard clause for sampling the relations. * @@ -53,7 +53,7 @@ tablesample_init(SampleScanState *scanstate, TableSampleClause *tablesample) List *args = tablesample->args; ListCell *arg; ExprContext *econtext = scanstate->ss.ps.ps_ExprContext; - TableSampleDesc *tsdesc = (TableSampleDesc *) palloc0(sizeof(TableSampleDesc)); + TableSampleDesc *tsdesc = (TableSampleDesc *) palloc0(sizeof(TableSampleDesc)); /* Load functions */ fmgr_info(tablesample->tsminit, &(tsdesc->tsminit)); @@ -78,21 +78,21 @@ tablesample_init(SampleScanState *scanstate, TableSampleClause *tablesample) fcinfo.argnull[0] = false; /* - * Second arg for init function is always REPEATABLE - * When tablesample->repeatable is NULL then REPEATABLE clause was not - * specified. - * When specified, the expression cannot evaluate to NULL. + * Second arg for init function is always REPEATABLE When + * tablesample->repeatable is NULL then REPEATABLE clause was not + * specified. When specified, the expression cannot evaluate to NULL. */ if (tablesample->repeatable) { ExprState *argstate = ExecInitExpr((Expr *) tablesample->repeatable, (PlanState *) scanstate); + fcinfo.arg[1] = ExecEvalExpr(argstate, econtext, &fcinfo.argnull[1], NULL); if (fcinfo.argnull[1]) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), - errmsg("REPEATABLE clause must be NOT NULL numeric value"))); + errmsg("REPEATABLE clause must be NOT NULL numeric value"))); } else { @@ -130,15 +130,15 @@ tablesample_init(SampleScanState *scanstate, TableSampleClause *tablesample) HeapTuple tablesample_getnext(TableSampleDesc *desc) { - HeapScanDesc scan = desc->heapScan; - HeapTuple tuple = &(scan->rs_ctup); - bool pagemode = scan->rs_pageatatime; - BlockNumber blockno; - Page page; - bool page_all_visible; - ItemId itemid; - OffsetNumber tupoffset, - maxoffset; + HeapScanDesc scan = desc->heapScan; + HeapTuple tuple = &(scan->rs_ctup); + bool pagemode = scan->rs_pageatatime; + BlockNumber blockno; + Page page; + bool page_all_visible; + ItemId itemid; + OffsetNumber tupoffset, + maxoffset; if (!scan->rs_inited) { @@ -152,7 +152,7 @@ tablesample_getnext(TableSampleDesc *desc) return NULL; } blockno = DatumGetInt32(FunctionCall1(&desc->tsmnextblock, - PointerGetDatum(desc))); + PointerGetDatum(desc))); if (!BlockNumberIsValid(blockno)) { tuple->t_data = NULL; @@ -184,14 +184,14 @@ tablesample_getnext(TableSampleDesc *desc) CHECK_FOR_INTERRUPTS(); tupoffset = DatumGetUInt16(FunctionCall3(&desc->tsmnexttuple, - PointerGetDatum(desc), - UInt32GetDatum(blockno), - UInt16GetDatum(maxoffset))); + PointerGetDatum(desc), + UInt32GetDatum(blockno), + UInt16GetDatum(maxoffset))); if (OffsetNumberIsValid(tupoffset)) { - bool visible; - bool found; + bool visible; + bool found; /* Skip invalid tuple pointers. */ itemid = PageGetItemId(page, tupoffset); @@ -208,8 +208,8 @@ tablesample_getnext(TableSampleDesc *desc) visible = SampleTupleVisible(tuple, tupoffset, scan); /* - * Let the sampling method examine the actual tuple and decide if we - * should return it. + * Let the sampling method examine the actual tuple and decide if + * we should return it. * * Note that we let it examine even invisible tuples for * statistical purposes, but not return them since user should @@ -218,10 +218,10 @@ tablesample_getnext(TableSampleDesc *desc) if (OidIsValid(desc->tsmexaminetuple.fn_oid)) { found = DatumGetBool(FunctionCall4(&desc->tsmexaminetuple, - PointerGetDatum(desc), - UInt32GetDatum(blockno), - PointerGetDatum(tuple), - BoolGetDatum(visible))); + PointerGetDatum(desc), + UInt32GetDatum(blockno), + PointerGetDatum(tuple), + BoolGetDatum(visible))); /* Should not happen if sampling method is well written. */ if (found && !visible) elog(ERROR, "Sampling method wanted to return invisible tuple"); @@ -248,19 +248,19 @@ tablesample_getnext(TableSampleDesc *desc) LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); blockno = DatumGetInt32(FunctionCall1(&desc->tsmnextblock, - PointerGetDatum(desc))); + PointerGetDatum(desc))); /* - * Report our new scan position for synchronization purposes. We - * don't do that when moving backwards, however. That would just - * mess up any other forward-moving scanners. + * Report our new scan position for synchronization purposes. We don't + * do that when moving backwards, however. That would just mess up any + * other forward-moving scanners. * - * Note: we do this before checking for end of scan so that the - * final state of the position hint is back at the start of the - * rel. That's not strictly necessary, but otherwise when you run - * the same query multiple times the starting position would shift - * a little bit backwards on every invocation, which is confusing. - * We don't guarantee any specific ordering in general, though. + * Note: we do this before checking for end of scan so that the final + * state of the position hint is back at the start of the rel. That's + * not strictly necessary, but otherwise when you run the same query + * multiple times the starting position would shift a little bit + * backwards on every invocation, which is confusing. We don't + * guarantee any specific ordering in general, though. */ if (scan->rs_syncscan) ss_report_location(scan->rs_rd, BlockNumberIsValid(blockno) ? @@ -321,25 +321,25 @@ SampleTupleVisible(HeapTuple tuple, OffsetNumber tupoffset, HeapScanDesc scan) { /* * If this scan is reading whole pages at a time, there is already - * visibility info present in rs_vistuples so we can just search it - * for the tupoffset. + * visibility info present in rs_vistuples so we can just search it for + * the tupoffset. */ if (scan->rs_pageatatime) { - int start = 0, - end = scan->rs_ntuples - 1; + int start = 0, + end = scan->rs_ntuples - 1; /* * Do the binary search over rs_vistuples, it's already sorted by * OffsetNumber so we don't need to do any sorting ourselves here. * - * We could use bsearch() here but it's slower for integers because - * of the function call overhead and because it needs boiler plate code + * We could use bsearch() here but it's slower for integers because of + * the function call overhead and because it needs boiler plate code * it would not save us anything code-wise anyway. */ while (start <= end) { - int mid = start + (end - start) / 2; + int mid = start + (end - start) / 2; OffsetNumber curoffset = scan->rs_vistuples[mid]; if (curoffset == tupoffset) @@ -358,7 +358,7 @@ SampleTupleVisible(HeapTuple tuple, OffsetNumber tupoffset, HeapScanDesc scan) Snapshot snapshot = scan->rs_snapshot; Buffer buffer = scan->rs_cbuf; - bool visible = HeapTupleSatisfiesVisibility(tuple, snapshot, buffer); + bool visible = HeapTupleSatisfiesVisibility(tuple, snapshot, buffer); CheckForSerializableConflictOut(visible, scan->rs_rd, tuple, buffer, snapshot); |