diff options
Diffstat (limited to 'contrib/tsm_system_rows')
-rw-r--r-- | contrib/tsm_system_rows/tsm_system_rows.c | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/contrib/tsm_system_rows/tsm_system_rows.c b/contrib/tsm_system_rows/tsm_system_rows.c index 14efb27f0d..e325eaff49 100644 --- a/contrib/tsm_system_rows/tsm_system_rows.c +++ b/contrib/tsm_system_rows/tsm_system_rows.c @@ -33,14 +33,14 @@ PG_MODULE_MAGIC; typedef struct { SamplerRandomState randstate; - uint32 seed; /* random seed */ - BlockNumber nblocks; /* number of block in relation */ - int32 ntuples; /* number of tuples to return */ - int32 donetuples; /* tuples already returned */ - OffsetNumber lt; /* last tuple returned from current block */ - BlockNumber step; /* step size */ - BlockNumber lb; /* last block visited */ - BlockNumber doneblocks; /* number of already returned blocks */ + uint32 seed; /* random seed */ + BlockNumber nblocks; /* number of block in relation */ + int32 ntuples; /* number of tuples to return */ + int32 donetuples; /* tuples already returned */ + OffsetNumber lt; /* last tuple returned from current block */ + BlockNumber step; /* step size */ + BlockNumber lb; /* last block visited */ + BlockNumber doneblocks; /* number of already returned blocks */ } SystemSamplerData; @@ -60,11 +60,11 @@ static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate); Datum tsm_system_rows_init(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - uint32 seed = PG_GETARG_UINT32(1); - int32 ntuples = PG_ARGISNULL(2) ? -1 : PG_GETARG_INT32(2); - HeapScanDesc scan = tsdesc->heapScan; - SystemSamplerData *sampler; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + uint32 seed = PG_GETARG_UINT32(1); + int32 ntuples = PG_ARGISNULL(2) ? -1 : PG_GETARG_INT32(2); + HeapScanDesc scan = tsdesc->heapScan; + SystemSamplerData *sampler; if (ntuples < 1) ereport(ERROR, @@ -86,6 +86,7 @@ tsm_system_rows_init(PG_FUNCTION_ARGS) /* Find relative prime as step size for linear probing. */ sampler->step = random_relative_prime(sampler->nblocks, sampler->randstate); + /* * Randomize start position so that blocks close to step size don't have * higher probability of being chosen on very short scan. @@ -106,8 +107,8 @@ tsm_system_rows_init(PG_FUNCTION_ARGS) Datum tsm_system_rows_nextblock(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->lb = (sampler->lb + sampler->step) % sampler->nblocks; sampler->doneblocks++; @@ -127,10 +128,10 @@ tsm_system_rows_nextblock(PG_FUNCTION_ARGS) Datum tsm_system_rows_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; @@ -152,9 +153,9 @@ tsm_system_rows_nexttuple(PG_FUNCTION_ARGS) Datum tsm_system_rows_examinetuple(PG_FUNCTION_ARGS) { - TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); - bool visible = PG_GETARG_BOOL(3); - SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); + bool visible = PG_GETARG_BOOL(3); + SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; if (!visible) PG_RETURN_BOOL(false); @@ -183,8 +184,8 @@ tsm_system_rows_end(PG_FUNCTION_ARGS) Datum tsm_system_rows_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; sampler->donetuples = 0; @@ -203,14 +204,14 @@ tsm_system_rows_reset(PG_FUNCTION_ARGS) Datum tsm_system_rows_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 *limitnode; - int32 ntuples; + 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 *limitnode; + int32 ntuples; limitnode = linitial(args); limitnode = estimate_expression_value(root, limitnode); @@ -235,9 +236,9 @@ tsm_system_rows_cost(PG_FUNCTION_ARGS) static uint32 -gcd (uint32 a, uint32 b) +gcd(uint32 a, uint32 b) { - uint32 c; + uint32 c; while (a != 0) { @@ -253,8 +254,8 @@ static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate) { /* Pick random starting number, with some limits on what it can be. */ - uint32 r = (uint32) sampler_random_fract(randstate) * n/2 + n/4, - t; + uint32 r = (uint32) sampler_random_fract(randstate) * n / 2 + n / 4, + t; /* * This should only take 2 or 3 iterations as the probability of 2 numbers |