PostgreSQL Source Code  git master
tsm_system_rows.c File Reference
#include "postgres.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 dependency graph for tsm_system_rows.c:

Go to the source code of this file.

Data Structures

struct  SystemRowsSamplerData
 

Functions

 PG_FUNCTION_INFO_V1 (tsm_system_rows_handler)
 
static void system_rows_samplescangetsamplesize (PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
 
static void system_rows_initsamplescan (SampleScanState *node, int eflags)
 
static void system_rows_beginsamplescan (SampleScanState *node, Datum *params, int nparams, uint32 seed)
 
static BlockNumber system_rows_nextsampleblock (SampleScanState *node, BlockNumber nblocks)
 
static OffsetNumber system_rows_nextsampletuple (SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
 
static uint32 random_relative_prime (uint32 n, SamplerRandomState randstate)
 
Datum tsm_system_rows_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 301 of file tsm_system_rows.c.

Referenced by random_relative_prime().

302 {
303  uint32 c;
304 
305  while (a != 0)
306  {
307  c = a;
308  a = b % a;
309  b = c;
310  }
311 
312  return b;
313 }
char * c
unsigned int uint32
Definition: c.h:358

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( tsm_system_rows_handler  )

◆ random_relative_prime()

static uint32 random_relative_prime ( uint32  n,
SamplerRandomState  randstate 
)
static

Definition at line 320 of file tsm_system_rows.c.

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

Referenced by system_rows_nextsampleblock().

321 {
322  uint32 r;
323 
324  /* Safety check to avoid infinite loop or zero result for small n. */
325  if (n <= 1)
326  return 1;
327 
328  /*
329  * This should only take 2 or 3 iterations as the probability of 2 numbers
330  * being relatively prime is ~61%; but just in case, we'll include a
331  * CHECK_FOR_INTERRUPTS in the loop.
332  */
333  do
334  {
336  r = (uint32) (sampler_random_fract(randstate) * n);
337  } while (r == 0 || gcd(r, n) > 1);
338 
339  return r;
340 }
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:238
unsigned int uint32
Definition: c.h:358
static uint32 gcd(uint32 a, uint32 b)
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ system_rows_beginsamplescan()

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

Definition at line 172 of file tsm_system_rows.c.

References DatumGetInt64, SystemRowsSamplerData::doneblocks, ereport, errcode(), errmsg(), ERROR, InvalidOffsetNumber, SystemRowsSamplerData::lt, SystemRowsSamplerData::ntuples, SystemRowsSamplerData::seed, SampleScanState::tsm_state, and SampleScanState::use_pagemode.

Referenced by tsm_system_rows_handler().

176 {
178  int64 ntuples = DatumGetInt64(params[0]);
179 
180  if (ntuples < 0)
181  ereport(ERROR,
182  (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
183  errmsg("sample size must not be negative")));
184 
185  sampler->seed = seed;
186  sampler->ntuples = ntuples;
187  sampler->lt = InvalidOffsetNumber;
188  sampler->doneblocks = 0;
189  /* lb will be initialized during first NextSampleBlock call */
190  /* we intentionally do not change nblocks/firstblock/step here */
191 
192  /*
193  * We *must* use pagemode visibility checking in this module, so force
194  * that even though it's currently default.
195  */
196  node->use_pagemode = true;
197 }
void * tsm_state
Definition: execnodes.h:1358
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define DatumGetInt64(X)
Definition: postgres.h:607
#define ereport(elevel, rest)
Definition: elog.h:141
#define InvalidOffsetNumber
Definition: off.h:26
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ system_rows_initsamplescan()

static void system_rows_initsamplescan ( SampleScanState node,
int  eflags 
)
static

Definition at line 162 of file tsm_system_rows.c.

References palloc0(), and SampleScanState::tsm_state.

Referenced by tsm_system_rows_handler().

163 {
164  node->tsm_state = palloc0(sizeof(SystemRowsSamplerData));
165  /* Note the above leaves tsm_state->step equal to zero */
166 }
void * tsm_state
Definition: execnodes.h:1358
void * palloc0(Size size)
Definition: mcxt.c:980

◆ system_rows_nextsampleblock()

static BlockNumber system_rows_nextsampleblock ( SampleScanState node,
BlockNumber  nblocks 
)
static

Definition at line 205 of file tsm_system_rows.c.

References SystemRowsSamplerData::doneblocks, SampleScanState::donetuples, SystemRowsSamplerData::firstblock, InvalidBlockNumber, SystemRowsSamplerData::lb, SystemRowsSamplerData::nblocks, SystemRowsSamplerData::ntuples, random_relative_prime(), sampler_random_fract(), sampler_random_init_state(), SystemRowsSamplerData::seed, SystemRowsSamplerData::step, and SampleScanState::tsm_state.

Referenced by tsm_system_rows_handler().

206 {
208 
209  /* First call within scan? */
210  if (sampler->doneblocks == 0)
211  {
212  /* First scan within query? */
213  if (sampler->step == 0)
214  {
215  /* Initialize now that we have scan descriptor */
216  SamplerRandomState randstate;
217 
218  /* If relation is empty, there's nothing to scan */
219  if (nblocks == 0)
220  return InvalidBlockNumber;
221 
222  /* We only need an RNG during this setup step */
223  sampler_random_init_state(sampler->seed, randstate);
224 
225  /* Compute nblocks/firstblock/step only once per query */
226  sampler->nblocks = nblocks;
227 
228  /* Choose random starting block within the relation */
229  /* (Actually this is the predecessor of the first block visited) */
230  sampler->firstblock = sampler_random_fract(randstate) *
231  sampler->nblocks;
232 
233  /* Find relative prime as step size for linear probing */
234  sampler->step = random_relative_prime(sampler->nblocks, randstate);
235  }
236 
237  /* Reinitialize lb */
238  sampler->lb = sampler->firstblock;
239  }
240 
241  /* If we've read all blocks or returned all needed tuples, we're done */
242  if (++sampler->doneblocks > sampler->nblocks ||
243  node->donetuples >= sampler->ntuples)
244  return InvalidBlockNumber;
245 
246  /*
247  * It's probably impossible for scan->rs_nblocks to decrease between scans
248  * within a query; but just in case, loop until we select a block number
249  * less than scan->rs_nblocks. We don't care if scan->rs_nblocks has
250  * increased since the first scan.
251  */
252  do
253  {
254  /* Advance lb, using uint64 arithmetic to forestall overflow */
255  sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks;
256  } while (sampler->lb >= nblocks);
257 
258  return sampler->lb;
259 }
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:229
void * tsm_state
Definition: execnodes.h:1358
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:238
unsigned short SamplerRandomState[3]
Definition: sampling.h:20
#define InvalidBlockNumber
Definition: block.h:33
static uint32 random_relative_prime(uint32 n, SamplerRandomState randstate)

◆ system_rows_nextsampletuple()

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

Definition at line 271 of file tsm_system_rows.c.

References SampleScanState::donetuples, FirstOffsetNumber, InvalidOffsetNumber, SystemRowsSamplerData::lt, SystemRowsSamplerData::ntuples, and SampleScanState::tsm_state.

Referenced by tsm_system_rows_handler().

274 {
276  OffsetNumber tupoffset = sampler->lt;
277 
278  /* Quit if we've returned all needed tuples */
279  if (node->donetuples >= sampler->ntuples)
280  return InvalidOffsetNumber;
281 
282  /* Advance to next possible offset on page */
283  if (tupoffset == InvalidOffsetNumber)
284  tupoffset = FirstOffsetNumber;
285  else
286  tupoffset++;
287 
288  /* Done? */
289  if (tupoffset > maxoffset)
290  tupoffset = InvalidOffsetNumber;
291 
292  sampler->lt = tupoffset;
293 
294  return tupoffset;
295 }
void * tsm_state
Definition: execnodes.h:1358
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
#define InvalidOffsetNumber
Definition: off.h:26

◆ system_rows_samplescangetsamplesize()

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

Definition at line 103 of file tsm_system_rows.c.

References clamp_row_est(), DatumGetInt64, estimate_expression_value(), IsA, linitial, Min, RelOptInfo::pages, and RelOptInfo::tuples.

Referenced by tsm_system_rows_handler().

108 {
109  Node *limitnode;
110  int64 ntuples;
111  double npages;
112 
113  /* Try to extract an estimate for the limit rowcount */
114  limitnode = (Node *) linitial(paramexprs);
115  limitnode = estimate_expression_value(root, limitnode);
116 
117  if (IsA(limitnode, Const) &&
118  !((Const *) limitnode)->constisnull)
119  {
120  ntuples = DatumGetInt64(((Const *) limitnode)->constvalue);
121  if (ntuples < 0)
122  {
123  /* Default ntuples if the value is bogus */
124  ntuples = 1000;
125  }
126  }
127  else
128  {
129  /* Default ntuples if we didn't obtain a non-null Const */
130  ntuples = 1000;
131  }
132 
133  /* Clamp to the estimated relation size */
134  if (ntuples > baserel->tuples)
135  ntuples = (int64) baserel->tuples;
136  ntuples = clamp_row_est(ntuples);
137 
138  if (baserel->tuples > 0 && baserel->pages > 0)
139  {
140  /* Estimate number of pages visited based on tuple density */
141  double density = baserel->tuples / (double) baserel->pages;
142 
143  npages = ntuples / density;
144  }
145  else
146  {
147  /* For lack of data, assume one tuple per page */
148  npages = ntuples;
149  }
150 
151  /* Clamp to sane value */
152  npages = clamp_row_est(Min((double) baserel->pages, npages));
153 
154  *pages = npages;
155  *tuples = ntuples;
156 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2286
double tuples
Definition: pathnodes.h:681
#define Min(x, y)
Definition: c.h:904
Definition: nodes.h:525
#define linitial(l)
Definition: pg_list.h:195
#define DatumGetInt64(X)
Definition: postgres.h:607
BlockNumber pages
Definition: pathnodes.h:680
double clamp_row_est(double nrows)
Definition: costsize.c:187

◆ tsm_system_rows_handler()

Datum tsm_system_rows_handler ( PG_FUNCTION_ARGS  )

Definition at line 79 of file tsm_system_rows.c.

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_rows_beginsamplescan(), system_rows_initsamplescan(), system_rows_nextsampleblock(), system_rows_nextsampletuple(), and system_rows_samplescangetsamplesize().

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

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 38 of file tsm_system_rows.c.