PostgreSQL Source Code git master
tsm_system_rows.c File Reference
#include "postgres.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, pg_prng_state *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 300 of file tsm_system_rows.c.

301{
302 uint32 c;
303
304 while (a != 0)
305 {
306 c = a;
307 a = b % a;
308 b = c;
309 }
310
311 return b;
312}
uint32_t uint32
Definition: c.h:502
int b
Definition: isn.c:71
int a
Definition: isn.c:70
char * c

References a, and b.

Referenced by int4lcm(), int8lcm(), and random_relative_prime().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( tsm_system_rows_handler  )

◆ random_relative_prime()

static uint32 random_relative_prime ( uint32  n,
pg_prng_state randstate 
)
static

Definition at line 319 of file tsm_system_rows.c.

320{
321 uint32 r;
322
323 /* Safety check to avoid infinite loop or zero result for small n. */
324 if (n <= 1)
325 return 1;
326
327 /*
328 * This should only take 2 or 3 iterations as the probability of 2 numbers
329 * being relatively prime is ~61%; but just in case, we'll include a
330 * CHECK_FOR_INTERRUPTS in the loop.
331 */
332 do
333 {
335 r = (uint32) (sampler_random_fract(randstate) * n);
336 } while (r == 0 || gcd(r, n) > 1);
337
338 return r;
339}
#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_rows_nextsampleblock().

◆ system_rows_beginsamplescan()

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

Definition at line 171 of file tsm_system_rows.c.

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

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

◆ system_rows_initsamplescan()

static void system_rows_initsamplescan ( SampleScanState node,
int  eflags 
)
static

Definition at line 161 of file tsm_system_rows.c.

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

References palloc0(), and SampleScanState::tsm_state.

Referenced by tsm_system_rows_handler().

◆ system_rows_nextsampleblock()

static BlockNumber system_rows_nextsampleblock ( SampleScanState node,
BlockNumber  nblocks 
)
static

Definition at line 204 of file tsm_system_rows.c.

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

◆ system_rows_nextsampletuple()

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

Definition at line 270 of file tsm_system_rows.c.

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

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

Referenced by tsm_system_rows_handler().

◆ system_rows_samplescangetsamplesize()

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

Definition at line 102 of file tsm_system_rows.c.

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

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

Referenced by tsm_system_rows_handler().

◆ tsm_system_rows_handler()

Datum tsm_system_rows_handler ( PG_FUNCTION_ARGS  )

Definition at line 78 of file tsm_system_rows.c.

79{
81
82 tsm->parameterTypes = list_make1_oid(INT8OID);
83
84 /* See notes at head of file */
85 tsm->repeatable_across_queries = false;
86 tsm->repeatable_across_scans = true;
87
93 tsm->EndSampleScan = NULL;
94
96}
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define makeNode(_type_)
Definition: nodes.h:157
#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_rows_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
static void system_rows_initsamplescan(SampleScanState *node, int eflags)
static BlockNumber system_rows_nextsampleblock(SampleScanState *node, BlockNumber nblocks)
static void system_rows_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)
static OffsetNumber system_rows_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)

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

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 37 of file tsm_system_rows.c.