|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * tsm_system_rows.c |
| 4 | + * interface routines for system_rows tablesample method |
| 5 | + * |
| 6 | + * |
| 7 | + * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group |
| 8 | + * |
| 9 | + * IDENTIFICATION |
| 10 | + * contrib/tsm_system_rows_rowlimit/tsm_system_rows.c |
| 11 | + * |
| 12 | + *------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | + |
| 15 | +#include "postgres.h" |
| 16 | + |
| 17 | +#include "fmgr.h" |
| 18 | + |
| 19 | +#include "access/tablesample.h" |
| 20 | +#include "access/relscan.h" |
| 21 | +#include "miscadmin.h" |
| 22 | +#include "nodes/execnodes.h" |
| 23 | +#include "nodes/relation.h" |
| 24 | +#include "optimizer/clauses.h" |
| 25 | +#include "storage/bufmgr.h" |
| 26 | +#include "utils/sampling.h" |
| 27 | + |
| 28 | +PG_MODULE_MAGIC; |
| 29 | + |
| 30 | +/* |
| 31 | + * State |
| 32 | + */ |
| 33 | +typedef struct |
| 34 | +{ |
| 35 | + SamplerRandomState randstate; |
| 36 | + uint32 seed; /* random seed */ |
| 37 | + BlockNumber nblocks; /* number of block in relation */ |
| 38 | + int32 ntuples; /* number of tuples to return */ |
| 39 | + int32 donetuples; /* tuples already returned */ |
| 40 | + OffsetNumber lt; /* last tuple returned from current block */ |
| 41 | + BlockNumber step; /* step size */ |
| 42 | + BlockNumber lb; /* last block visited */ |
| 43 | + BlockNumber doneblocks; /* number of already returned blocks */ |
| 44 | +} SystemSamplerData; |
| 45 | + |
| 46 | + |
| 47 | +PG_FUNCTION_INFO_V1(tsm_system_rows_init); |
| 48 | +PG_FUNCTION_INFO_V1(tsm_system_rows_nextblock); |
| 49 | +PG_FUNCTION_INFO_V1(tsm_system_rows_nexttuple); |
| 50 | +PG_FUNCTION_INFO_V1(tsm_system_rows_examinetuple); |
| 51 | +PG_FUNCTION_INFO_V1(tsm_system_rows_end); |
| 52 | +PG_FUNCTION_INFO_V1(tsm_system_rows_reset); |
| 53 | +PG_FUNCTION_INFO_V1(tsm_system_rows_cost); |
| 54 | + |
| 55 | +static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate); |
| 56 | + |
| 57 | +/* |
| 58 | + * Initializes the state. |
| 59 | + */ |
| 60 | +Datum |
| 61 | +tsm_system_rows_init(PG_FUNCTION_ARGS) |
| 62 | +{ |
| 63 | + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); |
| 64 | + uint32 seed = PG_GETARG_UINT32(1); |
| 65 | + int32 ntuples = PG_ARGISNULL(2) ? -1 : PG_GETARG_INT32(2); |
| 66 | + HeapScanDesc scan = tsdesc->heapScan; |
| 67 | + SystemSamplerData *sampler; |
| 68 | + |
| 69 | + if (ntuples < 1) |
| 70 | + ereport(ERROR, |
| 71 | + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), |
| 72 | + errmsg("invalid sample size"), |
| 73 | + errhint("Sample size must be positive integer value."))); |
| 74 | + |
| 75 | + sampler = palloc0(sizeof(SystemSamplerData)); |
| 76 | + |
| 77 | + /* Remember initial values for reinit */ |
| 78 | + sampler->seed = seed; |
| 79 | + sampler->nblocks = scan->rs_nblocks; |
| 80 | + sampler->ntuples = ntuples; |
| 81 | + sampler->donetuples = 0; |
| 82 | + sampler->lt = InvalidOffsetNumber; |
| 83 | + sampler->doneblocks = 0; |
| 84 | + |
| 85 | + sampler_random_init_state(sampler->seed, sampler->randstate); |
| 86 | + |
| 87 | + /* Find relative prime as step size for linear probing. */ |
| 88 | + sampler->step = random_relative_prime(sampler->nblocks, sampler->randstate); |
| 89 | + /* |
| 90 | + * Randomize start position so that blocks close to step size don't have |
| 91 | + * higher probability of being chosen on very short scan. |
| 92 | + */ |
| 93 | + sampler->lb = sampler_random_fract(sampler->randstate) * |
| 94 | + (sampler->nblocks / sampler->step); |
| 95 | + |
| 96 | + tsdesc->tsmdata = (void *) sampler; |
| 97 | + |
| 98 | + PG_RETURN_VOID(); |
| 99 | +} |
| 100 | + |
| 101 | +/* |
| 102 | + * Get next block number or InvalidBlockNumber when we're done. |
| 103 | + * |
| 104 | + * Uses linear probing algorithm for picking next block. |
| 105 | + */ |
| 106 | +Datum |
| 107 | +tsm_system_rows_nextblock(PG_FUNCTION_ARGS) |
| 108 | +{ |
| 109 | + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); |
| 110 | + SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; |
| 111 | + |
| 112 | + sampler->lb = (sampler->lb + sampler->step) % sampler->nblocks; |
| 113 | + sampler->doneblocks++; |
| 114 | + |
| 115 | + /* All blocks have been read, we're done */ |
| 116 | + if (sampler->doneblocks > sampler->nblocks || |
| 117 | + sampler->donetuples >= sampler->ntuples) |
| 118 | + PG_RETURN_UINT32(InvalidBlockNumber); |
| 119 | + |
| 120 | + PG_RETURN_UINT32(sampler->lb); |
| 121 | +} |
| 122 | + |
| 123 | +/* |
| 124 | + * Get next tuple offset in current block or InvalidOffsetNumber if we are done |
| 125 | + * with this block. |
| 126 | + */ |
| 127 | +Datum |
| 128 | +tsm_system_rows_nexttuple(PG_FUNCTION_ARGS) |
| 129 | +{ |
| 130 | + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); |
| 131 | + OffsetNumber maxoffset = PG_GETARG_UINT16(2); |
| 132 | + SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; |
| 133 | + OffsetNumber tupoffset = sampler->lt; |
| 134 | + |
| 135 | + if (tupoffset == InvalidOffsetNumber) |
| 136 | + tupoffset = FirstOffsetNumber; |
| 137 | + else |
| 138 | + tupoffset++; |
| 139 | + |
| 140 | + if (tupoffset > maxoffset || |
| 141 | + sampler->donetuples >= sampler->ntuples) |
| 142 | + tupoffset = InvalidOffsetNumber; |
| 143 | + |
| 144 | + sampler->lt = tupoffset; |
| 145 | + |
| 146 | + PG_RETURN_UINT16(tupoffset); |
| 147 | +} |
| 148 | + |
| 149 | +/* |
| 150 | + * Examine tuple and decide if it should be returned. |
| 151 | + */ |
| 152 | +Datum |
| 153 | +tsm_system_rows_examinetuple(PG_FUNCTION_ARGS) |
| 154 | +{ |
| 155 | + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); |
| 156 | + bool visible = PG_GETARG_BOOL(3); |
| 157 | + SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; |
| 158 | + |
| 159 | + if (!visible) |
| 160 | + PG_RETURN_BOOL(false); |
| 161 | + |
| 162 | + sampler->donetuples++; |
| 163 | + |
| 164 | + PG_RETURN_BOOL(true); |
| 165 | +} |
| 166 | + |
| 167 | +/* |
| 168 | + * Cleanup method. |
| 169 | + */ |
| 170 | +Datum |
| 171 | +tsm_system_rows_end(PG_FUNCTION_ARGS) |
| 172 | +{ |
| 173 | + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); |
| 174 | + |
| 175 | + pfree(tsdesc->tsmdata); |
| 176 | + |
| 177 | + PG_RETURN_VOID(); |
| 178 | +} |
| 179 | + |
| 180 | +/* |
| 181 | + * Reset state (called by ReScan). |
| 182 | + */ |
| 183 | +Datum |
| 184 | +tsm_system_rows_reset(PG_FUNCTION_ARGS) |
| 185 | +{ |
| 186 | + TableSampleDesc *tsdesc = (TableSampleDesc *) PG_GETARG_POINTER(0); |
| 187 | + SystemSamplerData *sampler = (SystemSamplerData *) tsdesc->tsmdata; |
| 188 | + |
| 189 | + sampler->lt = InvalidOffsetNumber; |
| 190 | + sampler->donetuples = 0; |
| 191 | + sampler->doneblocks = 0; |
| 192 | + |
| 193 | + sampler_random_init_state(sampler->seed, sampler->randstate); |
| 194 | + sampler->step = random_relative_prime(sampler->nblocks, sampler->randstate); |
| 195 | + sampler->lb = sampler_random_fract(sampler->randstate) * (sampler->nblocks / sampler->step); |
| 196 | + |
| 197 | + PG_RETURN_VOID(); |
| 198 | +} |
| 199 | + |
| 200 | +/* |
| 201 | + * Costing function. |
| 202 | + */ |
| 203 | +Datum |
| 204 | +tsm_system_rows_cost(PG_FUNCTION_ARGS) |
| 205 | +{ |
| 206 | + PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0); |
| 207 | + Path *path = (Path *) PG_GETARG_POINTER(1); |
| 208 | + RelOptInfo *baserel = (RelOptInfo *) PG_GETARG_POINTER(2); |
| 209 | + List *args = (List *) PG_GETARG_POINTER(3); |
| 210 | + BlockNumber *pages = (BlockNumber *) PG_GETARG_POINTER(4); |
| 211 | + double *tuples = (double *) PG_GETARG_POINTER(5); |
| 212 | + Node *limitnode; |
| 213 | + int32 ntuples; |
| 214 | + |
| 215 | + limitnode = linitial(args); |
| 216 | + limitnode = estimate_expression_value(root, limitnode); |
| 217 | + |
| 218 | + if (IsA(limitnode, RelabelType)) |
| 219 | + limitnode = (Node *) ((RelabelType *) limitnode)->arg; |
| 220 | + |
| 221 | + if (IsA(limitnode, Const)) |
| 222 | + ntuples = DatumGetInt32(((Const *) limitnode)->constvalue); |
| 223 | + else |
| 224 | + { |
| 225 | + /* Default ntuples if the estimation didn't return Const. */ |
| 226 | + ntuples = 1000; |
| 227 | + } |
| 228 | + |
| 229 | + *pages = Min(baserel->pages, ntuples); |
| 230 | + *tuples = ntuples; |
| 231 | + path->rows = *tuples; |
| 232 | + |
| 233 | + PG_RETURN_VOID(); |
| 234 | +} |
| 235 | + |
| 236 | + |
| 237 | +static uint32 |
| 238 | +gcd (uint32 a, uint32 b) |
| 239 | +{ |
| 240 | + uint32 c; |
| 241 | + |
| 242 | + while (a != 0) |
| 243 | + { |
| 244 | + c = a; |
| 245 | + a = b % a; |
| 246 | + b = c; |
| 247 | + } |
| 248 | + |
| 249 | + return b; |
| 250 | +} |
| 251 | + |
| 252 | +static uint32 |
| 253 | +random_relative_prime(uint32 n, SamplerRandomState randstate) |
| 254 | +{ |
| 255 | + /* Pick random starting number, with some limits on what it can be. */ |
| 256 | + uint32 r = (uint32) sampler_random_fract(randstate) * n/2 + n/4, |
| 257 | + t; |
| 258 | + |
| 259 | + /* |
| 260 | + * This should only take 2 or 3 iterations as the probability of 2 numbers |
| 261 | + * being relatively prime is ~61%. |
| 262 | + */ |
| 263 | + while ((t = gcd(r, n)) > 1) |
| 264 | + { |
| 265 | + CHECK_FOR_INTERRUPTS(); |
| 266 | + r /= t; |
| 267 | + } |
| 268 | + |
| 269 | + return r; |
| 270 | +} |
0 commit comments