PostgreSQL Source Code  git master
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/optimizer.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, BlockNumber nblocks)
 
static OffsetNumber system_time_nextsampletuple (SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
 
static uint32 random_relative_prime (uint32 n, pg_prng_state *randstate)
 
Datum tsm_system_time_handler (PG_FUNCTION_ARGS)
 
static uint32 gcd (uint32 a, uint32 b)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ gcd()

static uint32 gcd ( uint32  a,
uint32  b 
)
static

Definition at line 314 of file tsm_system_time.c.

315 {
316  uint32 c;
317 
318  while (a != 0)
319  {
320  c = a;
321  a = b % a;
322  b = c;
323  }
324 
325  return b;
326 }
unsigned int uint32
Definition: c.h:506
int b
Definition: isn.c:70
int a
Definition: isn.c:69
char * c

References a, and b.

Referenced by random_relative_prime().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( tsm_system_time_handler  )

◆ random_relative_prime()

static uint32 random_relative_prime ( uint32  n,
pg_prng_state randstate 
)
static

Definition at line 333 of file tsm_system_time.c.

334 {
335  uint32 r;
336 
337  /* Safety check to avoid infinite loop or zero result for small n. */
338  if (n <= 1)
339  return 1;
340 
341  /*
342  * This should only take 2 or 3 iterations as the probability of 2 numbers
343  * being relatively prime is ~61%; but just in case, we'll include a
344  * CHECK_FOR_INTERRUPTS in the loop.
345  */
346  do
347  {
349  r = (uint32) (sampler_random_fract(randstate) * n);
350  } while (r == 0 || gcd(r, n) > 1);
351 
352  return r;
353 }
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
double sampler_random_fract(pg_prng_state *randstate)
Definition: sampling.c:241
static uint32 gcd(uint32 a, uint32 b)

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

Referenced by system_time_nextsampleblock().

◆ system_time_beginsamplescan()

static void system_time_beginsamplescan ( SampleScanState node,
Datum params,
int  nparams,
uint32  seed 
)
static

Definition at line 188 of file tsm_system_time.c.

192 {
194  double millis = DatumGetFloat8(params[0]);
195 
196  if (millis < 0 || isnan(millis))
197  ereport(ERROR,
198  (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
199  errmsg("sample collection time must not be negative")));
200 
201  sampler->seed = seed;
202  sampler->millis = millis;
203  sampler->lt = InvalidOffsetNumber;
204  sampler->doneblocks = 0;
205  /* start_time, lb will be initialized during first NextSampleBlock call */
206  /* we intentionally do not change nblocks/firstblock/step here */
207 }
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define InvalidOffsetNumber
Definition: off.h:26
static float8 DatumGetFloat8(Datum X)
Definition: postgres.h:494
void * tsm_state
Definition: execnodes.h:1591

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().

◆ system_time_initsamplescan()

static void system_time_initsamplescan ( SampleScanState node,
int  eflags 
)
static

Definition at line 178 of file tsm_system_time.c.

179 {
180  node->tsm_state = palloc0(sizeof(SystemTimeSamplerData));
181  /* Note the above leaves tsm_state->step equal to zero */
182 }
void * palloc0(Size size)
Definition: mcxt.c:1346

References palloc0(), and SampleScanState::tsm_state.

Referenced by tsm_system_time_handler().

◆ system_time_nextsampleblock()

static BlockNumber system_time_nextsampleblock ( SampleScanState node,
BlockNumber  nblocks 
)
static

Definition at line 215 of file tsm_system_time.c.

216 {
218  instr_time cur_time;
219 
220  /* First call within scan? */
221  if (sampler->doneblocks == 0)
222  {
223  /* First scan within query? */
224  if (sampler->step == 0)
225  {
226  /* Initialize now that we have scan descriptor */
227  pg_prng_state randstate;
228 
229  /* If relation is empty, there's nothing to scan */
230  if (nblocks == 0)
231  return InvalidBlockNumber;
232 
233  /* We only need an RNG during this setup step */
234  sampler_random_init_state(sampler->seed, &randstate);
235 
236  /* Compute nblocks/firstblock/step only once per query */
237  sampler->nblocks = nblocks;
238 
239  /* Choose random starting block within the relation */
240  /* (Actually this is the predecessor of the first block visited) */
241  sampler->firstblock = sampler_random_fract(&randstate) *
242  sampler->nblocks;
243 
244  /* Find relative prime as step size for linear probing */
245  sampler->step = random_relative_prime(sampler->nblocks, &randstate);
246  }
247 
248  /* Reinitialize lb and start_time */
249  sampler->lb = sampler->firstblock;
251  }
252 
253  /* If we've read all blocks in relation, we're done */
254  if (++sampler->doneblocks > sampler->nblocks)
255  return InvalidBlockNumber;
256 
257  /* If we've used up all the allotted time, we're done */
258  INSTR_TIME_SET_CURRENT(cur_time);
259  INSTR_TIME_SUBTRACT(cur_time, sampler->start_time);
260  if (INSTR_TIME_GET_MILLISEC(cur_time) >= sampler->millis)
261  return InvalidBlockNumber;
262 
263  /*
264  * It's probably impossible for scan->rs_nblocks to decrease between scans
265  * within a query; but just in case, loop until we select a block number
266  * less than scan->rs_nblocks. We don't care if scan->rs_nblocks has
267  * increased since the first scan.
268  */
269  do
270  {
271  /* Advance lb, using uint64 arithmetic to forestall overflow */
272  sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks;
273  } while (sampler->lb >= nblocks);
274 
275  return sampler->lb;
276 }
#define InvalidBlockNumber
Definition: block.h:33
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:181
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:191
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
void sampler_random_init_state(uint32 seed, pg_prng_state *randstate)
Definition: sampling.c:234
static uint32 random_relative_prime(uint32 n, pg_prng_state *randstate)

References SystemTimeSamplerData::doneblocks, SystemTimeSamplerData::firstblock, if(), 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, SystemTimeSamplerData::start_time, SystemTimeSamplerData::step, and SampleScanState::tsm_state.

Referenced by tsm_system_time_handler().

◆ system_time_nextsampletuple()

static OffsetNumber system_time_nextsampletuple ( SampleScanState node,
BlockNumber  blockno,
OffsetNumber  maxoffset 
)
static

Definition at line 288 of file tsm_system_time.c.

291 {
293  OffsetNumber tupoffset = sampler->lt;
294 
295  /* Advance to next possible offset on page */
296  if (tupoffset == InvalidOffsetNumber)
297  tupoffset = FirstOffsetNumber;
298  else
299  tupoffset++;
300 
301  /* Done? */
302  if (tupoffset > maxoffset)
303  tupoffset = InvalidOffsetNumber;
304 
305  sampler->lt = tupoffset;
306 
307  return tupoffset;
308 }
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27

References FirstOffsetNumber, if(), InvalidOffsetNumber, SystemTimeSamplerData::lt, and SampleScanState::tsm_state.

Referenced by tsm_system_time_handler().

◆ system_time_samplescangetsamplesize()

static void system_time_samplescangetsamplesize ( PlannerInfo root,
RelOptInfo baserel,
List paramexprs,
BlockNumber pages,
double *  tuples 
)
static

Definition at line 103 of file tsm_system_time.c.

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

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

Referenced by tsm_system_time_handler().

◆ tsm_system_time_handler()

Datum tsm_system_time_handler ( PG_FUNCTION_ARGS  )

Definition at line 79 of file tsm_system_time.c.

80 {
82 
83  tsm->parameterTypes = list_make1_oid(FLOAT8OID);
84 
85  /* See notes at head of file */
86  tsm->repeatable_across_queries = false;
87  tsm->repeatable_across_scans = false;
88 
94  tsm->EndSampleScan = NULL;
95 
96  PG_RETURN_POINTER(tsm);
97 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define makeNode(_type_)
Definition: nodes.h:155
#define list_make1_oid(x1)
Definition: pg_list.h:242
NextSampleTuple_function NextSampleTuple
Definition: tsmapi.h:74
bool repeatable_across_scans
Definition: tsmapi.h:65
EndSampleScan_function EndSampleScan
Definition: tsmapi.h:75
SampleScanGetSampleSize_function SampleScanGetSampleSize
Definition: tsmapi.h:68
BeginSampleScan_function BeginSampleScan
Definition: tsmapi.h:72
NextSampleBlock_function NextSampleBlock
Definition: tsmapi.h:73
InitSampleScan_function InitSampleScan
Definition: tsmapi.h:71
List * parameterTypes
Definition: tsmapi.h:61
bool repeatable_across_queries
Definition: tsmapi.h:64
static void system_time_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
static void system_time_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)
static void system_time_initsamplescan(SampleScanState *node, int eflags)
static OffsetNumber system_time_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
static BlockNumber system_time_nextsampleblock(SampleScanState *node, BlockNumber nblocks)

References TsmRoutine::BeginSampleScan, TsmRoutine::EndSampleScan, TsmRoutine::InitSampleScan, list_make1_oid, makeNode, TsmRoutine::NextSampleBlock, TsmRoutine::NextSampleTuple, 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().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 37 of file tsm_system_time.c.