PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeSamplescan.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeSamplescan.c
4 * Support routines for sample scans of relations (table sampling).
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/executor/nodeSamplescan.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "access/relscan.h"
18#include "access/tableam.h"
19#include "access/tsmapi.h"
20#include "common/pg_prng.h"
21#include "executor/executor.h"
23#include "utils/fmgrprotos.h"
24#include "utils/rel.h"
25
29
30/* ----------------------------------------------------------------
31 * Scan Support
32 * ----------------------------------------------------------------
33 */
34
35/* ----------------------------------------------------------------
36 * SampleNext
37 *
38 * This is a workhorse for ExecSampleScan
39 * ----------------------------------------------------------------
40 */
41static TupleTableSlot *
43{
44 /*
45 * if this is first call within a scan, initialize
46 */
47 if (!node->begun)
48 tablesample_init(node);
49
50 /*
51 * get the next tuple, and store it in our result slot
52 */
53 return tablesample_getnext(node);
54}
55
56/*
57 * SampleRecheck -- access method routine to recheck a tuple in EvalPlanQual
58 */
59static bool
61{
62 /*
63 * No need to recheck for SampleScan, since like SeqScan we don't pass any
64 * checkable keys to heap_beginscan.
65 */
66 return true;
67}
68
69/* ----------------------------------------------------------------
70 * ExecSampleScan(node)
71 *
72 * Scans the relation using the sampling method and returns
73 * the next qualifying tuple.
74 * We call the ExecScan() routine and pass it the appropriate
75 * access method functions.
76 * ----------------------------------------------------------------
77 */
78static TupleTableSlot *
87
88/* ----------------------------------------------------------------
89 * ExecInitSampleScan
90 * ----------------------------------------------------------------
91 */
93ExecInitSampleScan(SampleScan *node, EState *estate, int eflags)
94{
98
99 Assert(outerPlan(node) == NULL);
100 Assert(innerPlan(node) == NULL);
101
102 /*
103 * create state structure
104 */
106 scanstate->ss.ps.plan = (Plan *) node;
107 scanstate->ss.ps.state = estate;
108 scanstate->ss.ps.ExecProcNode = ExecSampleScan;
109
110 /*
111 * Miscellaneous initialization
112 *
113 * create expression context for node
114 */
115 ExecAssignExprContext(estate, &scanstate->ss.ps);
116
117 /*
118 * open the scan relation
119 */
120 scanstate->ss.ss_currentRelation =
122 node->scan.scanrelid,
123 eflags);
124
125 /* we won't set up the HeapScanDesc till later */
126 scanstate->ss.ss_currentScanDesc = NULL;
127
128 /* and create slot with appropriate rowtype */
129 ExecInitScanTupleSlot(estate, &scanstate->ss,
130 RelationGetDescr(scanstate->ss.ss_currentRelation),
131 table_slot_callbacks(scanstate->ss.ss_currentRelation),
133
134 /*
135 * Initialize result type and projection.
136 */
139
140 /*
141 * initialize child expressions
142 */
143 scanstate->ss.ps.qual =
144 ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
145
147 scanstate->repeatable =
148 ExecInitExpr(tsc->repeatable, (PlanState *) scanstate);
149
150 /*
151 * If we don't have a REPEATABLE clause, select a random seed. We want to
152 * do this just once, since the seed shouldn't change over rescans.
153 */
154 if (tsc->repeatable == NULL)
156
157 /*
158 * Finally, initialize the TABLESAMPLE method handler.
159 */
160 tsm = GetTsmRoutine(tsc->tsmhandler);
161 scanstate->tsmroutine = tsm;
162 scanstate->tsm_state = NULL;
163
164 if (tsm->InitSampleScan)
165 tsm->InitSampleScan(scanstate, eflags);
166
167 /* We'll do BeginSampleScan later; we can't evaluate params yet */
168 scanstate->begun = false;
169
170 return scanstate;
171}
172
173/* ----------------------------------------------------------------
174 * ExecEndSampleScan
175 *
176 * frees any storage allocated through C routines.
177 * ----------------------------------------------------------------
178 */
179void
181{
182 /*
183 * Tell sampling function that we finished the scan.
184 */
185 if (node->tsmroutine->EndSampleScan)
186 node->tsmroutine->EndSampleScan(node);
187
188 /*
189 * close heap scan
190 */
191 if (node->ss.ss_currentScanDesc)
193}
194
195/* ----------------------------------------------------------------
196 * ExecReScanSampleScan
197 *
198 * Rescans the relation.
199 *
200 * ----------------------------------------------------------------
201 */
202void
204{
205 /* Remember we need to do BeginSampleScan again (if we did it at all) */
206 node->begun = false;
207 node->done = false;
208 node->haveblock = false;
209 node->donetuples = 0;
210
211 ExecScanReScan(&node->ss);
212}
213
214
215/*
216 * Initialize the TABLESAMPLE method: evaluate params and call BeginSampleScan.
217 */
218static void
220{
221 TsmRoutine *tsm = scanstate->tsmroutine;
222 ExprContext *econtext = scanstate->ss.ps.ps_ExprContext;
223 Datum *params;
224 Datum datum;
225 bool isnull;
226 uint32 seed;
227 bool allow_sync;
228 int i;
229 ListCell *arg;
230
231 scanstate->donetuples = 0;
232 params = palloc_array(Datum, list_length(scanstate->args));
233
234 i = 0;
235 foreach(arg, scanstate->args)
236 {
238
240 econtext,
241 &isnull);
242 if (isnull)
245 errmsg("TABLESAMPLE parameter cannot be null")));
246 i++;
247 }
248
249 if (scanstate->repeatable)
250 {
251 datum = ExecEvalExprSwitchContext(scanstate->repeatable,
252 econtext,
253 &isnull);
254 if (isnull)
257 errmsg("TABLESAMPLE REPEATABLE parameter cannot be null")));
258
259 /*
260 * The REPEATABLE parameter has been coerced to float8 by the parser.
261 * The reason for using float8 at the SQL level is that it will
262 * produce unsurprising results both for users used to databases that
263 * accept only integers in the REPEATABLE clause and for those who
264 * might expect that REPEATABLE works like setseed() (a float in the
265 * range from -1 to 1).
266 *
267 * We use hashfloat8() to convert the supplied value into a suitable
268 * seed. For regression-testing purposes, that has the convenient
269 * property that REPEATABLE(0) gives a machine-independent result.
270 */
272 }
273 else
274 {
275 /* Use the seed selected by ExecInitSampleScan */
276 seed = scanstate->seed;
277 }
278
279 /* Set default values for params that BeginSampleScan can adjust */
280 scanstate->use_bulkread = true;
281 scanstate->use_pagemode = true;
282
283 /* Let tablesample method do its thing */
284 tsm->BeginSampleScan(scanstate,
285 params,
286 list_length(scanstate->args),
287 seed);
288
289 /* We'll use syncscan if there's no NextSampleBlock function */
290 allow_sync = (tsm->NextSampleBlock == NULL);
291
292 /* Now we can create or reset the HeapScanDesc */
293 if (scanstate->ss.ss_currentScanDesc == NULL)
294 {
295 scanstate->ss.ss_currentScanDesc =
296 table_beginscan_sampling(scanstate->ss.ss_currentRelation,
297 scanstate->ss.ps.state->es_snapshot,
298 0, NULL,
299 scanstate->use_bulkread,
301 scanstate->use_pagemode);
302 }
303 else
304 {
305 table_rescan_set_params(scanstate->ss.ss_currentScanDesc, NULL,
306 scanstate->use_bulkread,
308 scanstate->use_pagemode);
309 }
310
311 pfree(params);
312
313 /* And we're initialized. */
314 scanstate->begun = true;
315}
316
317/*
318 * Get next tuple from TABLESAMPLE method.
319 */
320static TupleTableSlot *
322{
323 TableScanDesc scan = scanstate->ss.ss_currentScanDesc;
324 TupleTableSlot *slot = scanstate->ss.ss_ScanTupleSlot;
325
326 ExecClearTuple(slot);
327
328 if (scanstate->done)
329 return NULL;
330
331 for (;;)
332 {
333 if (!scanstate->haveblock)
334 {
336 {
337 scanstate->haveblock = false;
338 scanstate->done = true;
339
340 /* exhausted relation */
341 return NULL;
342 }
343
344 scanstate->haveblock = true;
345 }
346
347 if (!table_scan_sample_next_tuple(scan, scanstate, slot))
348 {
349 /*
350 * If we get here, it means we've exhausted the items on this page
351 * and it's time to move to the next.
352 */
353 scanstate->haveblock = false;
354 continue;
355 }
356
357 /* Found visible tuple, return it. */
358 break;
359 }
360
361 scanstate->donetuples++;
362
363 return slot;
364}
#define Assert(condition)
Definition c.h:945
uint32_t uint32
Definition c.h:618
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition execExpr.c:143
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition execExpr.c:250
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition execExpr.c:356
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition execScan.c:47
void ExecAssignScanProjectionInfo(ScanState *node)
Definition execScan.c:81
void ExecScanReScan(ScanState *node)
Definition execScan.c:108
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops, uint16 flags)
void ExecInitResultTypeTL(PlanState *planstate)
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition execUtils.c:490
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition execUtils.c:747
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition executor.h:583
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition executor.h:582
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition executor.h:439
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
Datum hashfloat8(PG_FUNCTION_ARGS)
Definition hashfunc.c:194
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1616
static void tablesample_init(SampleScanState *scanstate)
SampleScanState * ExecInitSampleScan(SampleScan *node, EState *estate, int eflags)
void ExecReScanSampleScan(SampleScanState *node)
void ExecEndSampleScan(SampleScanState *node)
static TupleTableSlot * ExecSampleScan(PlanState *pstate)
static TupleTableSlot * SampleNext(SampleScanState *node)
static bool SampleRecheck(SampleScanState *node, TupleTableSlot *slot)
static TupleTableSlot * tablesample_getnext(SampleScanState *scanstate)
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
static char * errmsg
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
uint32 pg_prng_uint32(pg_prng_state *state)
Definition pg_prng.c:227
pg_prng_state pg_global_prng_state
Definition pg_prng.c:34
#define innerPlan(node)
Definition plannodes.h:264
#define outerPlan(node)
Definition plannodes.h:265
static uint32 DatumGetUInt32(Datum X)
Definition postgres.h:222
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
#define RelationGetDescr(relation)
Definition rel.h:540
struct TsmRoutine * tsmroutine
Definition execnodes.h:1659
struct TableSampleClause * tablesample
Definition plannodes.h:560
struct TableScanDescData * ss_currentScanDesc
Definition execnodes.h:1635
Index scanrelid
Definition plannodes.h:540
EndSampleScan_function EndSampleScan
Definition tsmapi.h:75
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition tableam.c:59
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1004
static bool table_scan_sample_next_block(TableScanDesc scan, SampleScanState *scanstate)
Definition tableam.h:1978
static void table_rescan_set_params(TableScanDesc scan, ScanKeyData *key, bool allow_strat, bool allow_sync, bool allow_pagemode)
Definition tableam.h:1027
static TableScanDesc table_beginscan_sampling(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key, bool allow_strat, bool allow_sync, bool allow_pagemode)
Definition tableam.h:957
static bool table_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, TupleTableSlot *slot)
Definition tableam.h:1993
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition tablesample.c:27
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:476
#define TTS_FLAG_OBEYS_NOT_NULL_CONSTRAINTS
Definition tuptable.h:102