1 /*-------------------------------------------------------------------------
4 * Support routines for sample scans of relations (table sampling).
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/executor/nodeSamplescan.c
13 *-------------------------------------------------------------------------
17 #include "access/hash.h"
18 #include "access/heapam.h"
19 #include "access/relscan.h"
20 #include "access/tsmapi.h"
21 #include "executor/executor.h"
22 #include "executor/nodeSamplescan.h"
23 #include "miscadmin.h"
25 #include "storage/predicate.h"
26 #include "utils/builtins.h"
27 #include "utils/rel.h"
28 #include "utils/tqual.h"
30 static TupleTableSlot *SampleNext(SampleScanState *node);
31 static void tablesample_init(SampleScanState *scanstate);
32 static HeapTuple tablesample_getnext(SampleScanState *scanstate);
33 static bool SampleTupleVisible(HeapTuple tuple, OffsetNumber tupoffset,
36 /* ----------------------------------------------------------------
38 * ----------------------------------------------------------------
41 /* ----------------------------------------------------------------
44 * This is a workhorse for ExecSampleScan
45 * ----------------------------------------------------------------
47 static TupleTableSlot *
48 SampleNext(SampleScanState *node)
54 * if this is first call within a scan, initialize
57 tablesample_init(node);
60 * get the next tuple, and store it in our result slot
62 tuple = tablesample_getnext(node);
64 slot = node->ss.ss_ScanTupleSlot;
67 ExecStoreBufferHeapTuple(tuple, /* tuple to store */
68 slot, /* slot to store in */
69 node->ss.ss_currentScanDesc->rs_cbuf); /* tuple's buffer */
77 * SampleRecheck -- access method routine to recheck a tuple in EvalPlanQual
80 SampleRecheck(SampleScanState *node, TupleTableSlot *slot)
83 * No need to recheck for SampleScan, since like SeqScan we don't pass any
84 * checkable keys to heap_beginscan.
89 /* ----------------------------------------------------------------
90 * ExecSampleScan(node)
92 * Scans the relation using the sampling method and returns
93 * the next qualifying tuple.
94 * We call the ExecScan() routine and pass it the appropriate
95 * access method functions.
96 * ----------------------------------------------------------------
98 static TupleTableSlot *
99 ExecSampleScan(PlanState *pstate)
101 SampleScanState *node = castNode(SampleScanState, pstate);
103 return ExecScan(&node->ss,
104 (ExecScanAccessMtd) SampleNext,
105 (ExecScanRecheckMtd) SampleRecheck);
108 /* ----------------------------------------------------------------
110 * ----------------------------------------------------------------
113 ExecInitSampleScan(SampleScan *node, EState *estate, int eflags)
115 SampleScanState *scanstate;
116 TableSampleClause *tsc = node->tablesample;
119 Assert(outerPlan(node) == NULL);
120 Assert(innerPlan(node) == NULL);
123 * create state structure
125 scanstate = makeNode(SampleScanState);
126 scanstate->ss.ps.plan = (Plan *) node;
127 scanstate->ss.ps.state = estate;
128 scanstate->ss.ps.ExecProcNode = ExecSampleScan;
131 * Miscellaneous initialization
133 * create expression context for node
135 ExecAssignExprContext(estate, &scanstate->ss.ps);
138 * open the scan relation
140 scanstate->ss.ss_currentRelation =
141 ExecOpenScanRelation(estate,
142 node->scan.scanrelid,
145 /* we won't set up the HeapScanDesc till later */
146 scanstate->ss.ss_currentScanDesc = NULL;
148 /* and create slot with appropriate rowtype */
149 ExecInitScanTupleSlot(estate, &scanstate->ss,
150 RelationGetDescr(scanstate->ss.ss_currentRelation),
151 &TTSOpsBufferHeapTuple);
154 * Initialize result type and projection.
156 ExecInitResultTypeTL(&scanstate->ss.ps);
157 ExecAssignScanProjectionInfo(&scanstate->ss);
160 * initialize child expressions
162 scanstate->ss.ps.qual =
163 ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
165 scanstate->args = ExecInitExprList(tsc->args, (PlanState *) scanstate);
166 scanstate->repeatable =
167 ExecInitExpr(tsc->repeatable, (PlanState *) scanstate);
170 * If we don't have a REPEATABLE clause, select a random seed. We want to
171 * do this just once, since the seed shouldn't change over rescans.
173 if (tsc->repeatable == NULL)
174 scanstate->seed = random();
177 * Finally, initialize the TABLESAMPLE method handler.
179 tsm = GetTsmRoutine(tsc->tsmhandler);
180 scanstate->tsmroutine = tsm;
181 scanstate->tsm_state = NULL;
183 if (tsm->InitSampleScan)
184 tsm->InitSampleScan(scanstate, eflags);
186 /* We'll do BeginSampleScan later; we can't evaluate params yet */
187 scanstate->begun = false;
192 /* ----------------------------------------------------------------
195 * frees any storage allocated through C routines.
196 * ----------------------------------------------------------------
199 ExecEndSampleScan(SampleScanState *node)
202 * Tell sampling function that we finished the scan.
204 if (node->tsmroutine->EndSampleScan)
205 node->tsmroutine->EndSampleScan(node);
208 * Free the exprcontext
210 ExecFreeExprContext(&node->ss.ps);
213 * clean out the tuple table
215 if (node->ss.ps.ps_ResultTupleSlot)
216 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
217 ExecClearTuple(node->ss.ss_ScanTupleSlot);
222 if (node->ss.ss_currentScanDesc)
223 heap_endscan(node->ss.ss_currentScanDesc);
226 /* ----------------------------------------------------------------
227 * ExecReScanSampleScan
229 * Rescans the relation.
231 * ----------------------------------------------------------------
234 ExecReScanSampleScan(SampleScanState *node)
236 /* Remember we need to do BeginSampleScan again (if we did it at all) */
239 ExecScanReScan(&node->ss);
244 * Initialize the TABLESAMPLE method: evaluate params and call BeginSampleScan.
247 tablesample_init(SampleScanState *scanstate)
249 TsmRoutine *tsm = scanstate->tsmroutine;
250 ExprContext *econtext = scanstate->ss.ps.ps_ExprContext;
259 params = (Datum *) palloc(list_length(scanstate->args) * sizeof(Datum));
262 foreach(arg, scanstate->args)
264 ExprState *argstate = (ExprState *) lfirst(arg);
266 params[i] = ExecEvalExprSwitchContext(argstate,
271 (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
272 errmsg("TABLESAMPLE parameter cannot be null")));
276 if (scanstate->repeatable)
278 datum = ExecEvalExprSwitchContext(scanstate->repeatable,
283 (errcode(ERRCODE_INVALID_TABLESAMPLE_REPEAT),
284 errmsg("TABLESAMPLE REPEATABLE parameter cannot be null")));
287 * The REPEATABLE parameter has been coerced to float8 by the parser.
288 * The reason for using float8 at the SQL level is that it will
289 * produce unsurprising results both for users used to databases that
290 * accept only integers in the REPEATABLE clause and for those who
291 * might expect that REPEATABLE works like setseed() (a float in the
292 * range from -1 to 1).
294 * We use hashfloat8() to convert the supplied value into a suitable
295 * seed. For regression-testing purposes, that has the convenient
296 * property that REPEATABLE(0) gives a machine-independent result.
298 seed = DatumGetUInt32(DirectFunctionCall1(hashfloat8, datum));
302 /* Use the seed selected by ExecInitSampleScan */
303 seed = scanstate->seed;
306 /* Set default values for params that BeginSampleScan can adjust */
307 scanstate->use_bulkread = true;
308 scanstate->use_pagemode = true;
310 /* Let tablesample method do its thing */
311 tsm->BeginSampleScan(scanstate,
313 list_length(scanstate->args),
316 /* We'll use syncscan if there's no NextSampleBlock function */
317 allow_sync = (tsm->NextSampleBlock == NULL);
319 /* Now we can create or reset the HeapScanDesc */
320 if (scanstate->ss.ss_currentScanDesc == NULL)
322 scanstate->ss.ss_currentScanDesc =
323 heap_beginscan_sampling(scanstate->ss.ss_currentRelation,
324 scanstate->ss.ps.state->es_snapshot,
326 scanstate->use_bulkread,
328 scanstate->use_pagemode);
332 heap_rescan_set_params(scanstate->ss.ss_currentScanDesc, NULL,
333 scanstate->use_bulkread,
335 scanstate->use_pagemode);
340 /* And we're initialized. */
341 scanstate->begun = true;
345 * Get next tuple from TABLESAMPLE method.
347 * Note: an awful lot of this is copied-and-pasted from heapam.c. It would
348 * perhaps be better to refactor to share more code.
351 tablesample_getnext(SampleScanState *scanstate)
353 TsmRoutine *tsm = scanstate->tsmroutine;
354 HeapScanDesc scan = scanstate->ss.ss_currentScanDesc;
355 HeapTuple tuple = &(scan->rs_ctup);
356 Snapshot snapshot = scan->rs_snapshot;
357 bool pagemode = scan->rs_pageatatime;
361 OffsetNumber maxoffset;
363 if (!scan->rs_inited)
366 * return null immediately if relation is empty
368 if (scan->rs_nblocks == 0)
370 Assert(!BufferIsValid(scan->rs_cbuf));
371 tuple->t_data = NULL;
374 if (tsm->NextSampleBlock)
376 blockno = tsm->NextSampleBlock(scanstate);
377 if (!BlockNumberIsValid(blockno))
379 tuple->t_data = NULL;
384 blockno = scan->rs_startblock;
385 Assert(blockno < scan->rs_nblocks);
386 heapgetpage(scan, blockno);
387 scan->rs_inited = true;
391 /* continue from previously returned page/tuple */
392 blockno = scan->rs_cblock; /* current page */
396 * When not using pagemode, we must lock the buffer during tuple
400 LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
402 page = (Page) BufferGetPage(scan->rs_cbuf);
403 all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery;
404 maxoffset = PageGetMaxOffsetNumber(page);
408 OffsetNumber tupoffset;
411 CHECK_FOR_INTERRUPTS();
413 /* Ask the tablesample method which tuples to check on this page. */
414 tupoffset = tsm->NextSampleTuple(scanstate,
418 if (OffsetNumberIsValid(tupoffset))
423 /* Skip invalid tuple pointers. */
424 itemid = PageGetItemId(page, tupoffset);
425 if (!ItemIdIsNormal(itemid))
428 tuple->t_data = (HeapTupleHeader) PageGetItem(page, itemid);
429 tuple->t_len = ItemIdGetLength(itemid);
430 ItemPointerSet(&(tuple->t_self), blockno, tupoffset);
435 visible = SampleTupleVisible(tuple, tupoffset, scan);
437 /* in pagemode, heapgetpage did this for us */
439 CheckForSerializableConflictOut(visible, scan->rs_rd, tuple,
440 scan->rs_cbuf, snapshot);
444 /* Found visible tuple, return it. */
446 LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
451 /* Try next tuple from same page. */
457 * if we get here, it means we've exhausted the items on this page and
458 * it's time to move to the next.
461 LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
463 if (tsm->NextSampleBlock)
465 blockno = tsm->NextSampleBlock(scanstate);
466 Assert(!scan->rs_syncscan);
467 finished = !BlockNumberIsValid(blockno);
471 /* Without NextSampleBlock, just do a plain forward seqscan. */
473 if (blockno >= scan->rs_nblocks)
477 * Report our new scan position for synchronization purposes.
479 * Note: we do this before checking for end of scan so that the
480 * final state of the position hint is back at the start of the
481 * rel. That's not strictly necessary, but otherwise when you run
482 * the same query multiple times the starting position would shift
483 * a little bit backwards on every invocation, which is confusing.
484 * We don't guarantee any specific ordering in general, though.
486 if (scan->rs_syncscan)
487 ss_report_location(scan->rs_rd, blockno);
489 finished = (blockno == scan->rs_startblock);
493 * Reached end of scan?
497 if (BufferIsValid(scan->rs_cbuf))
498 ReleaseBuffer(scan->rs_cbuf);
499 scan->rs_cbuf = InvalidBuffer;
500 scan->rs_cblock = InvalidBlockNumber;
501 tuple->t_data = NULL;
502 scan->rs_inited = false;
506 Assert(blockno < scan->rs_nblocks);
507 heapgetpage(scan, blockno);
509 /* Re-establish state for new page */
511 LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
513 page = (Page) BufferGetPage(scan->rs_cbuf);
514 all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery;
515 maxoffset = PageGetMaxOffsetNumber(page);
518 /* Count successfully-fetched tuples as heap fetches */
519 pgstat_count_heap_getnext(scan->rs_rd);
521 return &(scan->rs_ctup);
525 * Check visibility of the tuple.
528 SampleTupleVisible(HeapTuple tuple, OffsetNumber tupoffset, HeapScanDesc scan)
530 if (scan->rs_pageatatime)
533 * In pageatatime mode, heapgetpage() already did visibility checks,
534 * so just look at the info it left in rs_vistuples[].
536 * We use a binary search over the known-sorted array. Note: we could
537 * save some effort if we insisted that NextSampleTuple select tuples
538 * in increasing order, but it's not clear that there would be enough
539 * gain to justify the restriction.
542 end = scan->rs_ntuples - 1;
546 int mid = (start + end) / 2;
547 OffsetNumber curoffset = scan->rs_vistuples[mid];
549 if (tupoffset == curoffset)
551 else if (tupoffset < curoffset)
561 /* Otherwise, we have to check the tuple individually. */
562 return HeapTupleSatisfiesVisibility(tuple,