PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_MODULE_MAGIC_EXT (.name="tsm_system_rows",.version=PG_VERSION)
 
 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)
 

Function Documentation

◆ gcd()

static uint32 gcd ( uint32  a,
uint32  b 
)
static

Definition at line 303 of file tsm_system_rows.c.

304{
305 uint32 c;
306
307 while (a != 0)
308 {
309 c = a;
310 a = b % a;
311 b = c;
312 }
313
314 return b;
315}
uint32_t uint32
Definition: c.h:502
int b
Definition: isn.c:74
int a
Definition: isn.c:73
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  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "tsm_system_rows",
version = PG_VERSION 
)

◆ random_relative_prime()

static uint32 random_relative_prime ( uint32  n,
pg_prng_state randstate 
)
static

Definition at line 322 of file tsm_system_rows.c.

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

178{
180 int64 ntuples = DatumGetInt64(params[0]);
181
182 if (ntuples < 0)
184 (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
185 errmsg("sample size must not be negative")));
186
187 sampler->seed = seed;
188 sampler->ntuples = ntuples;
189 sampler->lt = InvalidOffsetNumber;
190 sampler->doneblocks = 0;
191 /* lb will be initialized during first NextSampleBlock call */
192 /* we intentionally do not change nblocks/firstblock/step here */
193
194 /*
195 * We *must* use pagemode visibility checking in this module, so force
196 * that even though it's currently default.
197 */
198 node->use_pagemode = true;
199}
int64_t int64
Definition: c.h:499
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#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:1642

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 164 of file tsm_system_rows.c.

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

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 207 of file tsm_system_rows.c.

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

276{
278 OffsetNumber tupoffset = sampler->lt;
279
280 /* Quit if we've returned all needed tuples */
281 if (node->donetuples >= sampler->ntuples)
282 return InvalidOffsetNumber;
283
284 /* Advance to next possible offset on page */
285 if (tupoffset == InvalidOffsetNumber)
286 tupoffset = FirstOffsetNumber;
287 else
288 tupoffset++;
289
290 /* Done? */
291 if (tupoffset > maxoffset)
292 tupoffset = InvalidOffsetNumber;
293
294 sampler->lt = tupoffset;
295
296 return tupoffset;
297}
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 105 of file tsm_system_rows.c.

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

82{
84
85 tsm->parameterTypes = list_make1_oid(INT8OID);
86
87 /* See notes at head of file */
88 tsm->repeatable_across_queries = false;
89 tsm->repeatable_across_scans = true;
90
96 tsm->EndSampleScan = NULL;
97
99}
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define makeNode(_type_)
Definition: nodes.h:161
#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().