Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tsm_system_time.c 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*-------------------------------------------------------------------------
  2. *
  3. * tsm_system_time.c
  4. * support routines for SYSTEM_TIME tablesample method
  5. *
  6. * The desire here is to produce a random sample with as many rows as possible
  7. * in no more than the specified amount of time. We use a block-sampling
  8. * approach. To ensure that the whole relation will be visited if necessary,
  9. * we start at a randomly chosen block and then advance with a stride that
  10. * is randomly chosen but is relatively prime to the relation's nblocks.
  11. *
  12. * Because of the time dependence, this method is necessarily unrepeatable.
  13. * However, we do what we can to reduce surprising behavior by selecting
  14. * the sampling pattern just once per query, much as in tsm_system_rows.
  15. *
  16. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  17. * Portions Copyright (c) 1994, Regents of the University of California
  18. *
  19. * IDENTIFICATION
  20. * contrib/tsm_system_time/tsm_system_time.c
  21. *
  22. *-------------------------------------------------------------------------
  23. */
  24. #include "postgres.h"
  25. #include <math.h>
  26. #include "access/relscan.h"
  27. #include "access/tsmapi.h"
  28. #include "catalog/pg_type.h"
  29. #include "miscadmin.h"
  30. #include "optimizer/clauses.h"
  31. #include "optimizer/cost.h"
  32. #include "utils/sampling.h"
  33. #include "utils/spccache.h"
  34. PG_MODULE_MAGIC;
  35. PG_FUNCTION_INFO_V1(tsm_system_time_handler);
  36. /* Private state */
  37. typedef struct
  38. {
  39. uint32 seed; /* random seed */
  40. double millis; /* time limit for sampling */
  41. instr_time start_time; /* scan start time */
  42. OffsetNumber lt; /* last tuple returned from current block */
  43. BlockNumber doneblocks; /* number of already-scanned blocks */
  44. BlockNumber lb; /* last block visited */
  45. /* these three values are not changed during a rescan: */
  46. BlockNumber nblocks; /* number of blocks in relation */
  47. BlockNumber firstblock; /* first block to sample from */
  48. BlockNumber step; /* step size, or 0 if not set yet */
  49. } SystemTimeSamplerData;
  50. static void system_time_samplescangetsamplesize(PlannerInfo *root,
  51. RelOptInfo *baserel,
  52. List *paramexprs,
  53. BlockNumber *pages,
  54. double *tuples);
  55. static void system_time_initsamplescan(SampleScanState *node,
  56. int eflags);
  57. static void system_time_beginsamplescan(SampleScanState *node,
  58. Datum *params,
  59. int nparams,
  60. uint32 seed);
  61. static BlockNumber system_time_nextsampleblock(SampleScanState *node);
  62. static OffsetNumber system_time_nextsampletuple(SampleScanState *node,
  63. BlockNumber blockno,
  64. OffsetNumber maxoffset);
  65. static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate);
  66. /*
  67. * Create a TsmRoutine descriptor for the SYSTEM_TIME method.
  68. */
  69. Datum
  70. tsm_system_time_handler(PG_FUNCTION_ARGS)
  71. {
  72. TsmRoutine *tsm = makeNode(TsmRoutine);
  73. tsm->parameterTypes = list_make1_oid(FLOAT8OID);
  74. /* See notes at head of file */
  75. tsm->repeatable_across_queries = false;
  76. tsm->repeatable_across_scans = false;
  77. tsm->SampleScanGetSampleSize = system_time_samplescangetsamplesize;
  78. tsm->InitSampleScan = system_time_initsamplescan;
  79. tsm->BeginSampleScan = system_time_beginsamplescan;
  80. tsm->NextSampleBlock = system_time_nextsampleblock;
  81. tsm->NextSampleTuple = system_time_nextsampletuple;
  82. tsm->EndSampleScan = NULL;
  83. PG_RETURN_POINTER(tsm);
  84. }
  85. /*
  86. * Sample size estimation.
  87. */
  88. static void
  89. system_time_samplescangetsamplesize(PlannerInfo *root,
  90. RelOptInfo *baserel,
  91. List *paramexprs,
  92. BlockNumber *pages,
  93. double *tuples)
  94. {
  95. Node *limitnode;
  96. double millis;
  97. double spc_random_page_cost;
  98. double npages;
  99. double ntuples;
  100. /* Try to extract an estimate for the limit time spec */
  101. limitnode = (Node *) linitial(paramexprs);
  102. limitnode = estimate_expression_value(root, limitnode);
  103. if (IsA(limitnode, Const) &&
  104. !((Const *) limitnode)->constisnull)
  105. {
  106. millis = DatumGetFloat8(((Const *) limitnode)->constvalue);
  107. if (millis < 0 || isnan(millis))
  108. {
  109. /* Default millis if the value is bogus */
  110. millis = 1000;
  111. }
  112. }
  113. else
  114. {
  115. /* Default millis if we didn't obtain a non-null Const */
  116. millis = 1000;
  117. }
  118. /* Get the planner's idea of cost per page read */
  119. get_tablespace_page_costs(baserel->reltablespace,
  120. &spc_random_page_cost,
  121. NULL);
  122. /*
  123. * Estimate the number of pages we can read by assuming that the cost
  124. * figure is expressed in milliseconds. This is completely, unmistakably
  125. * bogus, but we have to do something to produce an estimate and there's
  126. * no better answer.
  127. */
  128. if (spc_random_page_cost > 0)
  129. npages = millis / spc_random_page_cost;
  130. else
  131. npages = millis; /* even more bogus, but whatcha gonna do? */
  132. /* Clamp to sane value */
  133. npages = clamp_row_est(Min((double) baserel->pages, npages));
  134. if (baserel->tuples > 0 && baserel->pages > 0)
  135. {
  136. /* Estimate number of tuples returned based on tuple density */
  137. double density = baserel->tuples / (double) baserel->pages;
  138. ntuples = npages * density;
  139. }
  140. else
  141. {
  142. /* For lack of data, assume one tuple per page */
  143. ntuples = npages;
  144. }
  145. /* Clamp to the estimated relation size */
  146. ntuples = clamp_row_est(Min(baserel->tuples, ntuples));
  147. *pages = npages;
  148. *tuples = ntuples;
  149. }
  150. /*
  151. * Initialize during executor setup.
  152. */
  153. static void
  154. system_time_initsamplescan(SampleScanState *node, int eflags)
  155. {
  156. node->tsm_state = palloc0(sizeof(SystemTimeSamplerData));
  157. /* Note the above leaves tsm_state->step equal to zero */
  158. }
  159. /*
  160. * Examine parameters and prepare for a sample scan.
  161. */
  162. static void
  163. system_time_beginsamplescan(SampleScanState *node,
  164. Datum *params,
  165. int nparams,
  166. uint32 seed)
  167. {
  168. SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state;
  169. double millis = DatumGetFloat8(params[0]);
  170. if (millis < 0 || isnan(millis))
  171. ereport(ERROR,
  172. (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
  173. errmsg("sample collection time must not be negative")));
  174. sampler->seed = seed;
  175. sampler->millis = millis;
  176. sampler->lt = InvalidOffsetNumber;
  177. sampler->doneblocks = 0;
  178. /* start_time, lb will be initialized during first NextSampleBlock call */
  179. /* we intentionally do not change nblocks/firstblock/step here */
  180. }
  181. /*
  182. * Select next block to sample.
  183. *
  184. * Uses linear probing algorithm for picking next block.
  185. */
  186. static BlockNumber
  187. system_time_nextsampleblock(SampleScanState *node)
  188. {
  189. SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state;
  190. HeapScanDesc scan = node->ss.ss_currentScanDesc;
  191. instr_time cur_time;
  192. /* First call within scan? */
  193. if (sampler->doneblocks == 0)
  194. {
  195. /* First scan within query? */
  196. if (sampler->step == 0)
  197. {
  198. /* Initialize now that we have scan descriptor */
  199. SamplerRandomState randstate;
  200. /* If relation is empty, there's nothing to scan */
  201. if (scan->rs_nblocks == 0)
  202. return InvalidBlockNumber;
  203. /* We only need an RNG during this setup step */
  204. sampler_random_init_state(sampler->seed, randstate);
  205. /* Compute nblocks/firstblock/step only once per query */
  206. sampler->nblocks = scan->rs_nblocks;
  207. /* Choose random starting block within the relation */
  208. /* (Actually this is the predecessor of the first block visited) */
  209. sampler->firstblock = sampler_random_fract(randstate) *
  210. sampler->nblocks;
  211. /* Find relative prime as step size for linear probing */
  212. sampler->step = random_relative_prime(sampler->nblocks, randstate);
  213. }
  214. /* Reinitialize lb and start_time */
  215. sampler->lb = sampler->firstblock;
  216. INSTR_TIME_SET_CURRENT(sampler->start_time);
  217. }
  218. /* If we've read all blocks in relation, we're done */
  219. if (++sampler->doneblocks > sampler->nblocks)
  220. return InvalidBlockNumber;
  221. /* If we've used up all the allotted time, we're done */
  222. INSTR_TIME_SET_CURRENT(cur_time);
  223. INSTR_TIME_SUBTRACT(cur_time, sampler->start_time);
  224. if (INSTR_TIME_GET_MILLISEC(cur_time) >= sampler->millis)
  225. return InvalidBlockNumber;
  226. /*
  227. * It's probably impossible for scan->rs_nblocks to decrease between scans
  228. * within a query; but just in case, loop until we select a block number
  229. * less than scan->rs_nblocks. We don't care if scan->rs_nblocks has
  230. * increased since the first scan.
  231. */
  232. do
  233. {
  234. /* Advance lb, using uint64 arithmetic to forestall overflow */
  235. sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks;
  236. } while (sampler->lb >= scan->rs_nblocks);
  237. return sampler->lb;
  238. }
  239. /*
  240. * Select next sampled tuple in current block.
  241. *
  242. * In block sampling, we just want to sample all the tuples in each selected
  243. * block.
  244. *
  245. * When we reach end of the block, return InvalidOffsetNumber which tells
  246. * SampleScan to go to next block.
  247. */
  248. static OffsetNumber
  249. system_time_nextsampletuple(SampleScanState *node,
  250. BlockNumber blockno,
  251. OffsetNumber maxoffset)
  252. {
  253. SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state;
  254. OffsetNumber tupoffset = sampler->lt;
  255. /* Advance to next possible offset on page */
  256. if (tupoffset == InvalidOffsetNumber)
  257. tupoffset = FirstOffsetNumber;
  258. else
  259. tupoffset++;
  260. /* Done? */
  261. if (tupoffset > maxoffset)
  262. tupoffset = InvalidOffsetNumber;
  263. sampler->lt = tupoffset;
  264. return tupoffset;
  265. }
  266. /*
  267. * Compute greatest common divisor of two uint32's.
  268. */
  269. static uint32
  270. gcd(uint32 a, uint32 b)
  271. {
  272. uint32 c;
  273. while (a != 0)
  274. {
  275. c = a;
  276. a = b % a;
  277. b = c;
  278. }
  279. return b;
  280. }
  281. /*
  282. * Pick a random value less than and relatively prime to n, if possible
  283. * (else return 1).
  284. */
  285. static uint32
  286. random_relative_prime(uint32 n, SamplerRandomState randstate)
  287. {
  288. uint32 r;
  289. /* Safety check to avoid infinite loop or zero result for small n. */
  290. if (n <= 1)
  291. return 1;
  292. /*
  293. * This should only take 2 or 3 iterations as the probability of 2 numbers
  294. * being relatively prime is ~61%; but just in case, we'll include a
  295. * CHECK_FOR_INTERRUPTS in the loop.
  296. */
  297. do
  298. {
  299. CHECK_FOR_INTERRUPTS();
  300. r = (uint32) (sampler_random_fract(randstate) * n);
  301. } while (r == 0 || gcd(r, n) > 1);
  302. return r;
  303. }