1/*-------------------------------------------------------------------------
4 * support routines for BERNOULLI 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 TID together with
12 * 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/bernoulli.c
22 *-------------------------------------------------------------------------
33#include "utils/fmgrprotos.h"
62 * Create a TsmRoutine descriptor for the BERNOULLI method.
83 * Sample size estimation.
95 /* Try to extract an estimate for the sample percentage */
100 !((
Const *) pctnode)->constisnull)
103 if (samplefract >= 0 && samplefract <= 100 && !isnan(samplefract))
104 samplefract /= 100.0f;
107 /* Default samplefract if the value is bogus */
113 /* Default samplefract if we didn't obtain a non-null Const */
117 /* We'll visit all pages of the baserel */
118 *pages = baserel->
pages;
124 * Initialize during executor setup.
133 * Examine parameters and prepare for a sample scan.
145 if (percent < 0 || percent > 100 || isnan(percent))
147 (
errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
148 errmsg(
"sample percentage must be between 0 and 100")));
151 * The cutoff is sample probability times (PG_UINT32_MAX + 1); we have to
152 * store that as a uint64, of course. Note that this gives strictly
153 * correct behavior at the limits of zero or one probability.
155 dcutoff = rint(((
double)
PG_UINT32_MAX + 1) * percent / 100);
157 sampler->
seed = seed;
161 * Use bulkread, since we're scanning all pages. But pagemode visibility
162 * checking is a win only at larger sampling fractions. The 25% cutoff
163 * here is based on very limited experimentation.
170 * Select next sampled tuple in current block.
172 * It is OK here to return an offset without knowing if the tuple is visible
173 * (or even exists). The reason is that we do the coinflip for every tuple
174 * offset in the table. Since all tuples have the same probability of being
175 * returned, it doesn't matter if we do extra coinflips for invisible tuples.
177 * When we reach end of the block, return InvalidOffsetNumber which tells
178 * SampleScan to go to next block.
189 /* Advance to first/next tuple in block */
196 * We compute the hash by applying hash_any to an array of 3 uint32's
197 * containing the block, offset, and seed. This is efficient to set up,
198 * and with the current implementation of hash_any, it gives
199 * machine-independent results, which is a nice property for regression
202 * These words in the hash input are the same throughout the block:
204 hashinput[0] = blockno;
205 hashinput[2] = sampler->
seed;
208 * Loop over tuple offsets until finding suitable TID or reaching end of
211 for (; tupoffset <= maxoffset; tupoffset++)
215 hashinput[1] = tupoffset;
218 (
int)
sizeof(hashinput)));
219 if (hash < sampler->cutoff)
223 if (tupoffset > maxoffset)
226 sampler->
lt = tupoffset;
static void bernoulli_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
static void bernoulli_initsamplescan(SampleScanState *node, int eflags)
Datum tsm_bernoulli_handler(PG_FUNCTION_ARGS)
static void bernoulli_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)
static OffsetNumber bernoulli_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
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