PostgreSQL Source Code  git master
bernoulli.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/tsmapi.h"
#include "catalog/pg_type.h"
#include "optimizer/optimizer.h"
#include "utils/builtins.h"
#include "utils/hashutils.h"
Include dependency graph for bernoulli.c:

Go to the source code of this file.

Data Structures

struct  BernoulliSamplerData
 

Functions

static void bernoulli_samplescangetsamplesize (PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
 
static void bernoulli_initsamplescan (SampleScanState *node, int eflags)
 
static void bernoulli_beginsamplescan (SampleScanState *node, Datum *params, int nparams, uint32 seed)
 
static OffsetNumber bernoulli_nextsampletuple (SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
 
Datum tsm_bernoulli_handler (PG_FUNCTION_ARGS)
 

Function Documentation

◆ bernoulli_beginsamplescan()

static void bernoulli_beginsamplescan ( SampleScanState node,
Datum params,
int  nparams,
uint32  seed 
)
static

Definition at line 136 of file bernoulli.c.

References BernoulliSamplerData::cutoff, DatumGetFloat4, ereport, errcode(), errmsg(), ERROR, InvalidOffsetNumber, BernoulliSamplerData::lt, PG_UINT32_MAX, rint(), BernoulliSamplerData::seed, SampleScanState::tsm_state, SampleScanState::use_bulkread, and SampleScanState::use_pagemode.

Referenced by tsm_bernoulli_handler().

140 {
142  double percent = DatumGetFloat4(params[0]);
143  double dcutoff;
144 
145  if (percent < 0 || percent > 100 || isnan(percent))
146  ereport(ERROR,
147  (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
148  errmsg("sample percentage must be between 0 and 100")));
149 
150  /*
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.
154  */
155  dcutoff = rint(((double) PG_UINT32_MAX + 1) * percent / 100);
156  sampler->cutoff = (uint64) dcutoff;
157  sampler->seed = seed;
158  sampler->lt = InvalidOffsetNumber;
159 
160  /*
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.
164  */
165  node->use_bulkread = true;
166  node->use_pagemode = (percent >= 25);
167 }
void * tsm_state
Definition: execnodes.h:1358
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_UINT32_MAX
Definition: c.h:442
#define ERROR
Definition: elog.h:43
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
OffsetNumber lt
Definition: bernoulli.c:41
#define InvalidOffsetNumber
Definition: off.h:26
#define DatumGetFloat4(X)
Definition: postgres.h:680
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ bernoulli_initsamplescan()

static void bernoulli_initsamplescan ( SampleScanState node,
int  eflags 
)
static

Definition at line 127 of file bernoulli.c.

References palloc0(), and SampleScanState::tsm_state.

Referenced by tsm_bernoulli_handler().

128 {
129  node->tsm_state = palloc0(sizeof(BernoulliSamplerData));
130 }
void * tsm_state
Definition: execnodes.h:1358
void * palloc0(Size size)
Definition: mcxt.c:980

◆ bernoulli_nextsampletuple()

static OffsetNumber bernoulli_nextsampletuple ( SampleScanState node,
BlockNumber  blockno,
OffsetNumber  maxoffset 
)
static

Definition at line 181 of file bernoulli.c.

References DatumGetUInt32, FirstOffsetNumber, hash(), hash_any(), InvalidOffsetNumber, BernoulliSamplerData::lt, BernoulliSamplerData::seed, and SampleScanState::tsm_state.

Referenced by tsm_bernoulli_handler().

184 {
186  OffsetNumber tupoffset = sampler->lt;
187  uint32 hashinput[3];
188 
189  /* Advance to first/next tuple in block */
190  if (tupoffset == InvalidOffsetNumber)
191  tupoffset = FirstOffsetNumber;
192  else
193  tupoffset++;
194 
195  /*
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
200  * testing.
201  *
202  * These words in the hash input are the same throughout the block:
203  */
204  hashinput[0] = blockno;
205  hashinput[2] = sampler->seed;
206 
207  /*
208  * Loop over tuple offsets until finding suitable TID or reaching end of
209  * block.
210  */
211  for (; tupoffset <= maxoffset; tupoffset++)
212  {
213  uint32 hash;
214 
215  hashinput[1] = tupoffset;
216 
217  hash = DatumGetUInt32(hash_any((const unsigned char *) hashinput,
218  (int) sizeof(hashinput)));
219  if (hash < sampler->cutoff)
220  break;
221  }
222 
223  if (tupoffset > maxoffset)
224  tupoffset = InvalidOffsetNumber;
225 
226  sampler->lt = tupoffset;
227 
228  return tupoffset;
229 }
#define DatumGetUInt32(X)
Definition: postgres.h:486
Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.c:148
void * tsm_state
Definition: execnodes.h:1358
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
unsigned int uint32
Definition: c.h:358
OffsetNumber lt
Definition: bernoulli.c:41
#define InvalidOffsetNumber
Definition: off.h:26
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541

◆ bernoulli_samplescangetsamplesize()

static void bernoulli_samplescangetsamplesize ( PlannerInfo root,
RelOptInfo baserel,
List paramexprs,
BlockNumber pages,
double *  tuples 
)
static

Definition at line 86 of file bernoulli.c.

References clamp_row_est(), DatumGetFloat4, estimate_expression_value(), IsA, linitial, RelOptInfo::pages, and RelOptInfo::tuples.

Referenced by tsm_bernoulli_handler().

91 {
92  Node *pctnode;
93  float4 samplefract;
94 
95  /* Try to extract an estimate for the sample percentage */
96  pctnode = (Node *) linitial(paramexprs);
97  pctnode = estimate_expression_value(root, pctnode);
98 
99  if (IsA(pctnode, Const) &&
100  !((Const *) pctnode)->constisnull)
101  {
102  samplefract = DatumGetFloat4(((Const *) pctnode)->constvalue);
103  if (samplefract >= 0 && samplefract <= 100 && !isnan(samplefract))
104  samplefract /= 100.0f;
105  else
106  {
107  /* Default samplefract if the value is bogus */
108  samplefract = 0.1f;
109  }
110  }
111  else
112  {
113  /* Default samplefract if we didn't obtain a non-null Const */
114  samplefract = 0.1f;
115  }
116 
117  /* We'll visit all pages of the baserel */
118  *pages = baserel->pages;
119 
120  *tuples = clamp_row_est(baserel->tuples * samplefract);
121 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2286
double tuples
Definition: pathnodes.h:681
Definition: nodes.h:525
#define linitial(l)
Definition: pg_list.h:195
float float4
Definition: c.h:490
BlockNumber pages
Definition: pathnodes.h:680
#define DatumGetFloat4(X)
Definition: postgres.h:680
double clamp_row_est(double nrows)
Definition: costsize.c:187

◆ tsm_bernoulli_handler()

Datum tsm_bernoulli_handler ( PG_FUNCTION_ARGS  )

Definition at line 65 of file bernoulli.c.

References TsmRoutine::BeginSampleScan, bernoulli_beginsamplescan(), bernoulli_initsamplescan(), bernoulli_nextsampletuple(), bernoulli_samplescangetsamplesize(), TsmRoutine::EndSampleScan, TsmRoutine::InitSampleScan, list_make1_oid, makeNode, TsmRoutine::NextSampleBlock, TsmRoutine::NextSampleTuple, TsmRoutine::parameterTypes, PG_RETURN_POINTER, TsmRoutine::repeatable_across_queries, TsmRoutine::repeatable_across_scans, and TsmRoutine::SampleScanGetSampleSize.

66 {
68 
69  tsm->parameterTypes = list_make1_oid(FLOAT4OID);
70  tsm->repeatable_across_queries = true;
71  tsm->repeatable_across_scans = true;
75  tsm->NextSampleBlock = NULL;
77  tsm->EndSampleScan = NULL;
78 
79  PG_RETURN_POINTER(tsm);
80 }
InitSampleScan_function InitSampleScan
Definition: tsmapi.h:71
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
bool repeatable_across_queries
Definition: tsmapi.h:64
static void bernoulli_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)
Definition: bernoulli.c:136
NextSampleTuple_function NextSampleTuple
Definition: tsmapi.h:74
NextSampleBlock_function NextSampleBlock
Definition: tsmapi.h:73
SampleScanGetSampleSize_function SampleScanGetSampleSize
Definition: tsmapi.h:68
#define list_make1_oid(x1)
Definition: pg_list.h:249
static void bernoulli_initsamplescan(SampleScanState *node, int eflags)
Definition: bernoulli.c:127
#define makeNode(_type_)
Definition: nodes.h:573
static OffsetNumber bernoulli_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
Definition: bernoulli.c:181
BeginSampleScan_function BeginSampleScan
Definition: tsmapi.h:72
bool repeatable_across_scans
Definition: tsmapi.h:65
static void bernoulli_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
Definition: bernoulli.c:86
List * parameterTypes
Definition: tsmapi.h:61
EndSampleScan_function EndSampleScan
Definition: tsmapi.h:75