Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * tsm_system_time.c
4 : : * support routines for SYSTEM_TIME tablesample method
5 : : *
6 : : * The desire here is to produce a random sample with as many rows as possible
7 : : * in no more than the specified amount of time. We use a block-sampling
8 : : * approach. To ensure that the whole relation will be visited if necessary,
9 : : * we start at a randomly chosen block and then advance with a stride that
10 : : * is randomly chosen but is relatively prime to the relation's nblocks.
11 : : *
12 : : * Because of the time dependence, this method is necessarily unrepeatable.
13 : : * However, we do what we can to reduce surprising behavior by selecting
14 : : * the sampling pattern just once per query, much as in tsm_system_rows.
15 : : *
16 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
17 : : * Portions Copyright (c) 1994, Regents of the University of California
18 : : *
19 : : * IDENTIFICATION
20 : : * contrib/tsm_system_time/tsm_system_time.c
21 : : *
22 : : *-------------------------------------------------------------------------
23 : : */
24 : :
25 : : #include "postgres.h"
26 : :
27 : : #include <math.h>
28 : :
29 : : #include "access/tsmapi.h"
30 : : #include "catalog/pg_type.h"
31 : : #include "miscadmin.h"
32 : : #include "optimizer/optimizer.h"
33 : : #include "portability/instr_time.h"
34 : : #include "utils/sampling.h"
35 : : #include "utils/spccache.h"
36 : :
405 tgl@sss.pgh.pa.us 37 :CBC 2 : PG_MODULE_MAGIC_EXT(
38 : : .name = "tsm_system_time",
39 : : .version = PG_VERSION
40 : : );
41 : :
3937 42 : 4 : PG_FUNCTION_INFO_V1(tsm_system_time_handler);
43 : :
44 : :
45 : : /* Private state */
46 : : typedef struct
47 : : {
48 : : uint32 seed; /* random seed */
49 : : double millis; /* time limit for sampling */
50 : : instr_time start_time; /* scan start time */
51 : : OffsetNumber lt; /* last tuple returned from current block */
52 : : BlockNumber doneblocks; /* number of already-scanned blocks */
53 : : BlockNumber lb; /* last block visited */
54 : : /* these three values are not changed during a rescan: */
55 : : BlockNumber nblocks; /* number of blocks in relation */
56 : : BlockNumber firstblock; /* first block to sample from */
57 : : BlockNumber step; /* step size, or 0 if not set yet */
58 : : } SystemTimeSamplerData;
59 : :
60 : : static void system_time_samplescangetsamplesize(PlannerInfo *root,
61 : : RelOptInfo *baserel,
62 : : List *paramexprs,
63 : : BlockNumber *pages,
64 : : double *tuples);
65 : : static void system_time_initsamplescan(SampleScanState *node,
66 : : int eflags);
67 : : static void system_time_beginsamplescan(SampleScanState *node,
68 : : Datum *params,
69 : : int nparams,
70 : : uint32 seed);
71 : : static BlockNumber system_time_nextsampleblock(SampleScanState *node, BlockNumber nblocks);
72 : : static OffsetNumber system_time_nextsampletuple(SampleScanState *node,
73 : : BlockNumber blockno,
74 : : OffsetNumber maxoffset);
75 : : static uint32 random_relative_prime(uint32 n, pg_prng_state *randstate);
76 : :
77 : :
78 : : /*
79 : : * Create a TsmRoutine descriptor for the SYSTEM_TIME method.
80 : : */
81 : : Datum
82 : 54 : tsm_system_time_handler(PG_FUNCTION_ARGS)
83 : : {
84 : 54 : TsmRoutine *tsm = makeNode(TsmRoutine);
85 : :
86 : 54 : tsm->parameterTypes = list_make1_oid(FLOAT8OID);
87 : :
88 : : /* See notes at head of file */
89 : 54 : tsm->repeatable_across_queries = false;
90 : 54 : tsm->repeatable_across_scans = false;
91 : :
92 : 54 : tsm->SampleScanGetSampleSize = system_time_samplescangetsamplesize;
93 : 54 : tsm->InitSampleScan = system_time_initsamplescan;
94 : 54 : tsm->BeginSampleScan = system_time_beginsamplescan;
95 : 54 : tsm->NextSampleBlock = system_time_nextsampleblock;
96 : 54 : tsm->NextSampleTuple = system_time_nextsampletuple;
97 : 54 : tsm->EndSampleScan = NULL;
98 : :
99 : 54 : PG_RETURN_POINTER(tsm);
100 : : }
101 : :
102 : : /*
103 : : * Sample size estimation.
104 : : */
105 : : static void
106 : 11 : system_time_samplescangetsamplesize(PlannerInfo *root,
107 : : RelOptInfo *baserel,
108 : : List *paramexprs,
109 : : BlockNumber *pages,
110 : : double *tuples)
111 : : {
112 : : Node *limitnode;
113 : : double millis;
114 : : double spc_random_page_cost;
115 : : double npages;
116 : : double ntuples;
117 : :
118 : : /* Try to extract an estimate for the limit time spec */
119 : 11 : limitnode = (Node *) linitial(paramexprs);
120 : 11 : limitnode = estimate_expression_value(root, limitnode);
121 : :
122 [ + + ]: 11 : if (IsA(limitnode, Const) &&
123 [ + - ]: 9 : !((Const *) limitnode)->constisnull)
124 : : {
125 : 9 : millis = DatumGetFloat8(((Const *) limitnode)->constvalue);
126 [ + + - + ]: 9 : if (millis < 0 || isnan(millis))
127 : : {
128 : : /* Default millis if the value is bogus */
129 : 2 : millis = 1000;
130 : : }
131 : : }
132 : : else
133 : : {
134 : : /* Default millis if we didn't obtain a non-null Const */
135 : 2 : millis = 1000;
136 : : }
137 : :
138 : : /* Get the planner's idea of cost per page read */
139 : 11 : get_tablespace_page_costs(baserel->reltablespace,
140 : : &spc_random_page_cost,
141 : : NULL);
142 : :
143 : : /*
144 : : * Estimate the number of pages we can read by assuming that the cost
145 : : * figure is expressed in milliseconds. This is completely, unmistakably
146 : : * bogus, but we have to do something to produce an estimate and there's
147 : : * no better answer.
148 : : */
149 [ + - ]: 11 : if (spc_random_page_cost > 0)
150 : 11 : npages = millis / spc_random_page_cost;
151 : : else
3937 tgl@sss.pgh.pa.us 152 :UBC 0 : npages = millis; /* even more bogus, but whatcha gonna do? */
153 : :
154 : : /* Clamp to sane value */
3937 tgl@sss.pgh.pa.us 155 [ + + ]:CBC 11 : npages = clamp_row_est(Min((double) baserel->pages, npages));
156 : :
157 [ + - + - ]: 11 : if (baserel->tuples > 0 && baserel->pages > 0)
158 : 11 : {
159 : : /* Estimate number of tuples returned based on tuple density */
160 : 11 : double density = baserel->tuples / (double) baserel->pages;
161 : :
162 : 11 : ntuples = npages * density;
163 : : }
164 : : else
165 : : {
166 : : /* For lack of data, assume one tuple per page */
3937 tgl@sss.pgh.pa.us 167 :UBC 0 : ntuples = npages;
168 : : }
169 : :
170 : : /* Clamp to the estimated relation size */
3937 tgl@sss.pgh.pa.us 171 [ - + ]:CBC 11 : ntuples = clamp_row_est(Min(baserel->tuples, ntuples));
172 : :
173 : 11 : *pages = npages;
174 : 11 : *tuples = ntuples;
4008 simon@2ndQuadrant.co 175 : 11 : }
176 : :
177 : : /*
178 : : * Initialize during executor setup.
179 : : */
180 : : static void
3937 tgl@sss.pgh.pa.us 181 : 11 : system_time_initsamplescan(SampleScanState *node, int eflags)
182 : : {
151 michael@paquier.xyz 183 :GNC 11 : node->tsm_state = palloc0_object(SystemTimeSamplerData);
184 : : /* Note the above leaves tsm_state->step equal to zero */
4008 simon@2ndQuadrant.co 185 :CBC 11 : }
186 : :
187 : : /*
188 : : * Examine parameters and prepare for a sample scan.
189 : : */
190 : : static void
3937 tgl@sss.pgh.pa.us 191 : 6 : system_time_beginsamplescan(SampleScanState *node,
192 : : Datum *params,
193 : : int nparams,
194 : : uint32 seed)
195 : : {
196 : 6 : SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state;
197 : 6 : double millis = DatumGetFloat8(params[0]);
198 : :
199 [ + + - + ]: 6 : if (millis < 0 || isnan(millis))
200 [ + - ]: 1 : ereport(ERROR,
201 : : (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
202 : : errmsg("sample collection time must not be negative")));
203 : :
204 : 5 : sampler->seed = seed;
205 : 5 : sampler->millis = millis;
4008 simon@2ndQuadrant.co 206 : 5 : sampler->lt = InvalidOffsetNumber;
207 : 5 : sampler->doneblocks = 0;
208 : : /* start_time, lb will be initialized during first NextSampleBlock call */
209 : : /* we intentionally do not change nblocks/firstblock/step here */
210 : 5 : }
211 : :
212 : : /*
213 : : * Select next block to sample.
214 : : *
215 : : * Uses linear probing algorithm for picking next block.
216 : : */
217 : : static BlockNumber
2593 andres@anarazel.de 218 : 26 : system_time_nextsampleblock(SampleScanState *node, BlockNumber nblocks)
219 : : {
3937 tgl@sss.pgh.pa.us 220 : 26 : SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state;
221 : : instr_time cur_time;
222 : :
223 : : /* First call within scan? */
224 [ + + ]: 26 : if (sampler->doneblocks == 0)
225 : : {
226 : : /* First scan within query? */
227 [ + + ]: 5 : if (sampler->step == 0)
228 : : {
229 : : /* Initialize now that we have scan descriptor */
230 : : pg_prng_state randstate;
231 : :
232 : : /* If relation is empty, there's nothing to scan */
2593 andres@anarazel.de 233 [ - + ]: 4 : if (nblocks == 0)
3937 tgl@sss.pgh.pa.us 234 :UBC 0 : return InvalidBlockNumber;
235 : :
236 : : /* We only need an RNG during this setup step */
1619 tgl@sss.pgh.pa.us 237 :CBC 4 : sampler_random_init_state(sampler->seed, &randstate);
238 : :
239 : : /* Compute nblocks/firstblock/step only once per query */
2593 andres@anarazel.de 240 : 4 : sampler->nblocks = nblocks;
241 : :
242 : : /* Choose random starting block within the relation */
243 : : /* (Actually this is the predecessor of the first block visited) */
1619 tgl@sss.pgh.pa.us 244 : 4 : sampler->firstblock = sampler_random_fract(&randstate) *
3937 245 : 4 : sampler->nblocks;
246 : :
247 : : /* Find relative prime as step size for linear probing */
1619 248 : 4 : sampler->step = random_relative_prime(sampler->nblocks, &randstate);
249 : : }
250 : :
251 : : /* Reinitialize lb and start_time */
3937 252 : 5 : sampler->lb = sampler->firstblock;
253 : 5 : INSTR_TIME_SET_CURRENT(sampler->start_time);
254 : : }
255 : :
256 : : /* If we've read all blocks in relation, we're done */
257 [ + + ]: 26 : if (++sampler->doneblocks > sampler->nblocks)
258 : 3 : return InvalidBlockNumber;
259 : :
260 : : /* If we've used up all the allotted time, we're done */
261 : 23 : INSTR_TIME_SET_CURRENT(cur_time);
262 : 23 : INSTR_TIME_SUBTRACT(cur_time, sampler->start_time);
263 [ + + ]: 23 : if (INSTR_TIME_GET_MILLISEC(cur_time) >= sampler->millis)
264 : 2 : return InvalidBlockNumber;
265 : :
266 : : /*
267 : : * It's probably impossible for scan->rs_nblocks to decrease between scans
268 : : * within a query; but just in case, loop until we select a block number
269 : : * less than scan->rs_nblocks. We don't care if scan->rs_nblocks has
270 : : * increased since the first scan.
271 : : */
272 : : do
273 : : {
274 : : /* Advance lb, using uint64 arithmetic to forestall overflow */
275 : 21 : sampler->lb = ((uint64) sampler->lb + sampler->step) % sampler->nblocks;
2593 andres@anarazel.de 276 [ - + ]: 21 : } while (sampler->lb >= nblocks);
277 : :
3937 tgl@sss.pgh.pa.us 278 : 21 : return sampler->lb;
279 : : }
280 : :
281 : : /*
282 : : * Select next sampled tuple in current block.
283 : : *
284 : : * In block sampling, we just want to sample all the tuples in each selected
285 : : * block.
286 : : *
287 : : * When we reach end of the block, return InvalidOffsetNumber which tells
288 : : * SampleScan to go to next block.
289 : : */
290 : : static OffsetNumber
291 : 114 : system_time_nextsampletuple(SampleScanState *node,
292 : : BlockNumber blockno,
293 : : OffsetNumber maxoffset)
294 : : {
295 : 114 : SystemTimeSamplerData *sampler = (SystemTimeSamplerData *) node->tsm_state;
296 : 114 : OffsetNumber tupoffset = sampler->lt;
297 : :
298 : : /* Advance to next possible offset on page */
299 [ + + ]: 114 : if (tupoffset == InvalidOffsetNumber)
300 : 21 : tupoffset = FirstOffsetNumber;
301 : : else
302 : 93 : tupoffset++;
303 : :
304 : : /* Done? */
305 [ + + ]: 114 : if (tupoffset > maxoffset)
306 : 21 : tupoffset = InvalidOffsetNumber;
307 : :
308 : 114 : sampler->lt = tupoffset;
309 : :
310 : 114 : return tupoffset;
311 : : }
312 : :
313 : : /*
314 : : * Compute greatest common divisor of two uint32's.
315 : : */
316 : : static uint32
4000 bruce@momjian.us 317 : 4 : gcd(uint32 a, uint32 b)
318 : : {
319 : : uint32 c;
320 : :
4008 simon@2ndQuadrant.co 321 [ + + ]: 12 : while (a != 0)
322 : : {
323 : 8 : c = a;
324 : 8 : a = b % a;
325 : 8 : b = c;
326 : : }
327 : :
328 : 4 : return b;
329 : : }
330 : :
331 : : /*
332 : : * Pick a random value less than and relatively prime to n, if possible
333 : : * (else return 1).
334 : : */
335 : : static uint32
1619 tgl@sss.pgh.pa.us 336 : 4 : random_relative_prime(uint32 n, pg_prng_state *randstate)
337 : : {
338 : : uint32 r;
339 : :
340 : : /* Safety check to avoid infinite loop or zero result for small n. */
3937 341 [ - + ]: 4 : if (n <= 1)
3937 tgl@sss.pgh.pa.us 342 :UBC 0 : return 1;
343 : :
344 : : /*
345 : : * This should only take 2 or 3 iterations as the probability of 2 numbers
346 : : * being relatively prime is ~61%; but just in case, we'll include a
347 : : * CHECK_FOR_INTERRUPTS in the loop.
348 : : */
349 : : do
350 : : {
4008 simon@2ndQuadrant.co 351 [ - + ]:CBC 4 : CHECK_FOR_INTERRUPTS();
3937 tgl@sss.pgh.pa.us 352 : 4 : r = (uint32) (sampler_random_fract(randstate) * n);
353 [ - + - + ]: 4 : } while (r == 0 || gcd(r, n) > 1);
354 : :
4008 simon@2ndQuadrant.co 355 : 4 : return r;
356 : : }
|