1/*-------------------------------------------------------------------------
4 * support routines for SYSTEM tablesample method
6 * To ensure repeatability of samples, it is necessary that selection of a
7 * given tuple be history-independent; otherwise syncscanning would break
8 * repeatability, to say nothing of logically-irrelevant maintenance such
9 * as physical extension or shortening of the relation.
11 * To achieve that, we proceed by hashing each candidate block number together
12 * with the active seed, and then selecting it if the hash is less than the
13 * cutoff value computed from the selection probability by BeginSampleScan.
16 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
17 * Portions Copyright (c) 1994, Regents of the University of California
20 * src/backend/access/tablesample/system.c
22 *-------------------------------------------------------------------------
33#include "utils/fmgrprotos.h"
64 * Create a TsmRoutine descriptor for the SYSTEM method.
85 * Sample size estimation.
97 /* Try to extract an estimate for the sample percentage */
102 !((
Const *) pctnode)->constisnull)
105 if (samplefract >= 0 && samplefract <= 100 && !isnan(samplefract))
106 samplefract /= 100.0f;
109 /* Default samplefract if the value is bogus */
115 /* Default samplefract if we didn't obtain a non-null Const */
119 /* We'll visit a sample of the pages ... */
122 /* ... and hopefully get a representative number of tuples from them */
127 * Initialize during executor setup.
136 * Examine parameters and prepare for a sample scan.
148 if (percent < 0 || percent > 100 || isnan(percent))
150 (
errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
151 errmsg(
"sample percentage must be between 0 and 100")));
154 * The cutoff is sample probability times (PG_UINT32_MAX + 1); we have to
155 * store that as a uint64, of course. Note that this gives strictly
156 * correct behavior at the limits of zero or one probability.
158 dcutoff = rint(((
double)
PG_UINT32_MAX + 1) * percent / 100);
160 sampler->
seed = seed;
165 * Bulkread buffer access strategy probably makes sense unless we're
166 * scanning a very small fraction of the table. The 1% cutoff here is a
167 * guess. We should use pagemode visibility checking, since we scan all
168 * tuples on each selected page.
175 * Select next block to sample.
185 * We compute the hash by applying hash_any to an array of 2 uint32's
186 * containing the block number and seed. This is efficient to set up, and
187 * with the current implementation of hash_any, it gives
188 * machine-independent results, which is a nice property for regression
191 * These words in the hash input are the same throughout the block:
193 hashinput[1] = sampler->
seed;
196 * Loop over block numbers until finding suitable block or reaching end of
199 for (; nextblock < nblocks; nextblock++)
203 hashinput[0] = nextblock;
206 (
int)
sizeof(hashinput)));
207 if (hash < sampler->cutoff)
211 if (nextblock < nblocks)
213 /* Found a suitable block; remember where we should start next time */
218 /* Done, but let's reset nextblock to 0 for safety. */
224 * Select next sampled tuple in current block.
226 * In block sampling, we just want to sample all the tuples in each selected
229 * It is OK here to return an offset without knowing if the tuple is visible
230 * (or even exists); nodeSamplescan.c will deal with that.
232 * When we reach end of the block, return InvalidOffsetNumber which tells
233 * SampleScan to go to next block.
243 /* Advance to next possible offset on page */
250 if (tupoffset > maxoffset)
253 sampler->
lt = tupoffset;
static OffsetNumber system_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
static void system_initsamplescan(SampleScanState *node, int eflags)
static void system_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)
Datum tsm_system_handler(PG_FUNCTION_ARGS)
static BlockNumber system_nextsampleblock(SampleScanState *node, BlockNumber nblocks)
static void system_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
#define InvalidBlockNumber
Node * estimate_expression_value(PlannerInfo *root, Node *node)
double clamp_row_est(double nrows)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define PG_RETURN_POINTER(x)
static Datum hash_any(const unsigned char *k, int keylen)
if(TABLE==NULL||TABLE_index==NULL)
void * palloc0(Size size)
#define IsA(nodeptr, _type_)
#define InvalidOffsetNumber
#define FirstOffsetNumber
#define list_make1_oid(x1)
static uint32 DatumGetUInt32(Datum X)
static float4 DatumGetFloat4(Datum X)
static unsigned hash(unsigned *uv, int n)
NextSampleTuple_function NextSampleTuple
bool repeatable_across_scans
EndSampleScan_function EndSampleScan
SampleScanGetSampleSize_function SampleScanGetSampleSize
BeginSampleScan_function BeginSampleScan
NextSampleBlock_function NextSampleBlock
InitSampleScan_function InitSampleScan
bool repeatable_across_queries