PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tsm_system_time.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/relscan.h"
#include "access/tsmapi.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "utils/sampling.h"
#include "utils/spccache.h"
Include dependency graph for tsm_system_time.c:

Go to the source code of this file.

Data Structures

struct  SystemTimeSamplerData
 

Functions

 PG_FUNCTION_INFO_V1 (tsm_system_time_handler)
 
static void system_time_samplescangetsamplesize (PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
 
static void system_time_initsamplescan (SampleScanState *node, int eflags)
 
static void system_time_beginsamplescan (SampleScanState *node, Datum *params, int nparams, uint32 seed)
 
static BlockNumber system_time_nextsampleblock (SampleScanState *node)
 
static OffsetNumber system_time_nextsampletuple (SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
 
static uint32 random_relative_prime (uint32 n, SamplerRandomState randstate)
 
Datum tsm_system_time_handler (PG_FUNCTION_ARGS)
 
static uint32 gcd (uint32 a, uint32 b)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

static uint32 gcd ( uint32  a,
uint32  b 
)
static

Definition at line 319 of file tsm_system_time.c.

Referenced by random_relative_prime().

320 {
321  uint32 c;
322 
323  while (a != 0)
324  {
325  c = a;
326  a = b % a;
327  b = c;
328  }
329 
330  return b;
331 }
char * c
unsigned int uint32
Definition: c.h:268
PG_FUNCTION_INFO_V1 ( tsm_system_time_handler  )
static uint32 random_relative_prime ( uint32  n,
SamplerRandomState  randstate 
)
static

Definition at line 338 of file tsm_system_time.c.

References CHECK_FOR_INTERRUPTS, gcd(), and sampler_random_fract().

Referenced by system_time_nextsampleblock().

339 {
340  uint32 r;
341 
342  /* Safety check to avoid infinite loop or zero result for small n. */
343  if (n <= 1)
344  return 1;
345 
346  /*
347  * This should only take 2 or 3 iterations as the probability of 2 numbers
348  * being relatively prime is ~61%; but just in case, we'll include a
349  * CHECK_FOR_INTERRUPTS in the loop.
350  */
351  do
352  {
354  r = (uint32) (sampler_random_fract(randstate) * n);
355  } while (r == 0 || gcd(r, n) > 1);
356 
357  return r;
358 }
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:238
static uint32 gcd(uint32 a, uint32 b)
unsigned int uint32
Definition: c.h:268
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
static void system_time_beginsamplescan ( SampleScanState node,
Datum params,
int  nparams,
uint32  seed 
)
static

Definition at line 192 of file tsm_system_time.c.

References DatumGetFloat8, SystemTimeSamplerData::doneblocks, ereport, errcode(), errmsg(), ERROR, InvalidOffsetNumber, SystemTimeSamplerData::lt, SystemTimeSamplerData::millis, SystemTimeSamplerData::seed, and SampleScanState::tsm_state.

Referenced by tsm_system_time_handler().

196 {
198  double millis = DatumGetFloat8(params[0]);
199 
200  if (millis < 0 || isnan(millis))
201  ereport(ERROR,
202  (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
203  errmsg("sample collection time must not be negative")));
204 
205  sampler->seed = seed;
206  sampler->millis = millis;
207  sampler->lt = InvalidOffsetNumber;
208  sampler->doneblocks = 0;
209  /* start_time, lb will be initialized during first NextSampleBlock call */
210  /* we intentionally do not change nblocks/firstblock/step here */
211 }
void * tsm_state
Definition: execnodes.h:1317
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define DatumGetFloat8(X)
Definition: postgres.h:734
#define InvalidOffsetNumber
Definition: off.h:26
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void system_time_initsamplescan ( SampleScanState node,
int  eflags 
)
static

Definition at line 182 of file tsm_system_time.c.

References palloc0(), and SampleScanState::tsm_state.

Referenced by tsm_system_time_handler().

183 {
184  node->tsm_state = palloc0(sizeof(SystemTimeSamplerData));
185  /* Note the above leaves tsm_state->step equal to zero */
186 }
void * tsm_state
Definition: execnodes.h:1317
void * palloc0(Size size)
Definition: mcxt.c:878
static BlockNumber system_time_nextsampleblock ( SampleScanState node)
static

Definition at line 219 of file tsm_system_time.c.

References SystemTimeSamplerData::doneblocks, SystemTimeSamplerData::firstblock, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, InvalidBlockNumber, SystemTimeSamplerData::lb, SystemTimeSamplerData::millis, SystemTimeSamplerData::nblocks, random_relative_prime(), sampler_random_fract(), sampler_random_init_state(), SystemTimeSamplerData::seed, SampleScanState::ss, ScanState::ss_currentScanDesc, SystemTimeSamplerData::start_time, SystemTimeSamplerData::step, and SampleScanState::tsm_state.

Referenced by tsm_system_time_handler().

220 {
222  HeapScanDesc scan = node->ss.ss_currentScanDesc;
223  instr_time cur_time;
224 
225  /* First call within scan? */
226  if (sampler->doneblocks == 0)
227  {
228  /* First scan within query? */
229  if (sampler->step == 0)
230  {
231  /* Initialize now that we have scan descriptor */
232  SamplerRandomState randstate;
233 
234  /* If relation is empty, there's nothing to scan */
235  if (scan->rs_nblocks == 0)
236  return InvalidBlockNumber;
237 
238  /* We only need an RNG during this setup step */
239  sampler_random_init_state(sampler->seed, randstate);
240 
241  /* Compute nblocks/firstblock/step only once per query */
242  sampler->nblocks = scan->rs_nblocks;
243 
244  /* Choose random starting block within the relation */
245  /* (Actually this is the predecessor of the first block visited) */
246  sampler->firstblock = sampler_random_fract(randstate) *
247  sampler->nblocks;
248 
249  /* Find relative prime as step size for linear probing */
250  sampler->step = random_relative_prime(sampler->nblocks, randstate);
251  }
252 
253  /* Reinitialize lb and start_time */
254  sampler->lb = sampler->firstblock;
256  }
257 
258  /* If we've read all blocks in relation, we're done */
259  if (++sampler->doneblocks > sampler->nblocks)
260  return InvalidBlockNumber;
261 
262  /* If we've used up all the allotted time, we're done */
263  INSTR_TIME_SET_CURRENT(cur_time);
264  INSTR_TIME_SUBTRACT(cur_time, sampler->start_time);
265  if (INSTR_TIME_GET_MILLISEC(cur_time) >= sampler->millis)
266  return InvalidBlockNumber;
267 
268  /*
269  * It's probably impossible for scan->rs_nblocks to decrease between scans
270  * within a query; but just in case, loop until we select a block number
271  * less than scan->rs_nblocks. We don't care if scan->rs_nblocks has
272  * increased since the first scan.
273  */
274  do
275  {
276  /* Advance lb, using uint64 arithmetic to forestall overflow */
277  sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks;
278  } while (sampler->lb >= scan->rs_nblocks);
279 
280  return sampler->lb;
281 }
static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate)
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:229
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:199
struct timeval instr_time
Definition: instr_time.h:147
void * tsm_state
Definition: execnodes.h:1317
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:238
unsigned short SamplerRandomState[3]
Definition: sampling.h:20
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:167
#define InvalidBlockNumber
Definition: block.h:33
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
HeapScanDesc ss_currentScanDesc
Definition: execnodes.h:1292
ScanState ss
Definition: execnodes.h:1312
static OffsetNumber system_time_nextsampletuple ( SampleScanState node,
BlockNumber  blockno,
OffsetNumber  maxoffset 
)
static

Definition at line 293 of file tsm_system_time.c.

References FirstOffsetNumber, InvalidOffsetNumber, SystemTimeSamplerData::lt, and SampleScanState::tsm_state.

Referenced by tsm_system_time_handler().

296 {
298  OffsetNumber tupoffset = sampler->lt;
299 
300  /* Advance to next possible offset on page */
301  if (tupoffset == InvalidOffsetNumber)
302  tupoffset = FirstOffsetNumber;
303  else
304  tupoffset++;
305 
306  /* Done? */
307  if (tupoffset > maxoffset)
308  tupoffset = InvalidOffsetNumber;
309 
310  sampler->lt = tupoffset;
311 
312  return tupoffset;
313 }
void * tsm_state
Definition: execnodes.h:1317
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
#define InvalidOffsetNumber
Definition: off.h:26
static void system_time_samplescangetsamplesize ( PlannerInfo root,
RelOptInfo baserel,
List paramexprs,
BlockNumber pages,
double *  tuples 
)
static

Definition at line 107 of file tsm_system_time.c.

References clamp_row_est(), DatumGetFloat8, estimate_expression_value(), get_tablespace_page_costs(), IsA, linitial, Min, NULL, RelOptInfo::pages, RelOptInfo::reltablespace, and RelOptInfo::tuples.

Referenced by tsm_system_time_handler().

112 {
113  Node *limitnode;
114  double millis;
115  double spc_random_page_cost;
116  double npages;
117  double ntuples;
118 
119  /* Try to extract an estimate for the limit time spec */
120  limitnode = (Node *) linitial(paramexprs);
121  limitnode = estimate_expression_value(root, limitnode);
122 
123  if (IsA(limitnode, Const) &&
124  !((Const *) limitnode)->constisnull)
125  {
126  millis = DatumGetFloat8(((Const *) limitnode)->constvalue);
127  if (millis < 0 || isnan(millis))
128  {
129  /* Default millis if the value is bogus */
130  millis = 1000;
131  }
132  }
133  else
134  {
135  /* Default millis if we didn't obtain a non-null Const */
136  millis = 1000;
137  }
138 
139  /* Get the planner's idea of cost per page read */
141  &spc_random_page_cost,
142  NULL);
143 
144  /*
145  * Estimate the number of pages we can read by assuming that the cost
146  * figure is expressed in milliseconds. This is completely, unmistakably
147  * bogus, but we have to do something to produce an estimate and there's
148  * no better answer.
149  */
150  if (spc_random_page_cost > 0)
151  npages = millis / spc_random_page_cost;
152  else
153  npages = millis; /* even more bogus, but whatcha gonna do? */
154 
155  /* Clamp to sane value */
156  npages = clamp_row_est(Min((double) baserel->pages, npages));
157 
158  if (baserel->tuples > 0 && baserel->pages > 0)
159  {
160  /* Estimate number of tuples returned based on tuple density */
161  double density = baserel->tuples / (double) baserel->pages;
162 
163  ntuples = npages * density;
164  }
165  else
166  {
167  /* For lack of data, assume one tuple per page */
168  ntuples = npages;
169  }
170 
171  /* Clamp to the estimated relation size */
172  ntuples = clamp_row_est(Min(baserel->tuples, ntuples));
173 
174  *pages = npages;
175  *tuples = ntuples;
176 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2399
double tuples
Definition: relation.h:534
Oid reltablespace
Definition: relation.h:523
#define Min(x, y)
Definition: c.h:806
Definition: nodes.h:522
#define linitial(l)
Definition: pg_list.h:110
void get_tablespace_page_costs(Oid spcid, double *spc_random_page_cost, double *spc_seq_page_cost)
Definition: spccache.c:182
#define DatumGetFloat8(X)
Definition: postgres.h:734
BlockNumber pages
Definition: relation.h:533
#define NULL
Definition: c.h:229
double clamp_row_est(double nrows)
Definition: costsize.c:173
Datum tsm_system_time_handler ( PG_FUNCTION_ARGS  )

Definition at line 83 of file tsm_system_time.c.

References TsmRoutine::BeginSampleScan, TsmRoutine::EndSampleScan, FLOAT8OID, TsmRoutine::InitSampleScan, list_make1_oid, makeNode, TsmRoutine::NextSampleBlock, TsmRoutine::NextSampleTuple, NULL, TsmRoutine::parameterTypes, PG_RETURN_POINTER, TsmRoutine::repeatable_across_queries, TsmRoutine::repeatable_across_scans, TsmRoutine::SampleScanGetSampleSize, system_time_beginsamplescan(), system_time_initsamplescan(), system_time_nextsampleblock(), system_time_nextsampletuple(), and system_time_samplescangetsamplesize().

84 {
86 
88 
89  /* See notes at head of file */
90  tsm->repeatable_across_queries = false;
91  tsm->repeatable_across_scans = false;
92 
98  tsm->EndSampleScan = NULL;
99 
100  PG_RETURN_POINTER(tsm);
101 }
InitSampleScan_function InitSampleScan
Definition: tsmapi.h:70
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:313
static OffsetNumber system_time_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
bool repeatable_across_queries
Definition: tsmapi.h:63
NextSampleTuple_function NextSampleTuple
Definition: tsmapi.h:73
static BlockNumber system_time_nextsampleblock(SampleScanState *node)
NextSampleBlock_function NextSampleBlock
Definition: tsmapi.h:72
SampleScanGetSampleSize_function SampleScanGetSampleSize
Definition: tsmapi.h:67
#define list_make1_oid(x1)
Definition: pg_list.h:145
#define makeNode(_type_)
Definition: nodes.h:570
#define NULL
Definition: c.h:229
#define FLOAT8OID
Definition: pg_type.h:415
BeginSampleScan_function BeginSampleScan
Definition: tsmapi.h:71
bool repeatable_across_scans
Definition: tsmapi.h:64
static void system_time_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
List * parameterTypes
Definition: tsmapi.h:60
EndSampleScan_function EndSampleScan
Definition: tsmapi.h:74
static void system_time_initsamplescan(SampleScanState *node, int eflags)
static void system_time_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)

Variable Documentation

PG_MODULE_MAGIC

Definition at line 41 of file tsm_system_time.c.